- Notifications
You must be signed in to change notification settings - Fork475
feat: add more caching methods#1066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
rmatif wants to merge12 commits intoleejet:masterChoose a base branch fromrmatif:add-ucache
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
fb88d86 add ucache
rmatiff347010 add cache-mode and cache-option
rmatif148bfdf add decay rate and relative threshold
rmatifc59f414 use single unified struct
rmatif78230da use actual scheduler sigmas for ucache bounds
rmatif186038e add cache-dit
rmatifb176dfd fix Fn/Bn handling
rmatiff04166f named parameter
rmatifc6f0e22 add reset param to ucache
rmatifffbe00a adapt to upstream refactor (common.hpp)
rmatife49d1ab add documentation
rmatifd26f06a cleanup
rmatifFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
922 changes: 922 additions & 0 deletionscache_dit.hpp
Large diffs are not rendered by default.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
126 changes: 126 additions & 0 deletionsdocs/caching.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| ## Caching | ||
| Caching methods accelerate diffusion inference by reusing intermediate computations when changes between steps are small. | ||
| ### Cache Modes | ||
| | Mode | Target | Description | | ||
| |------|--------|-------------| | ||
| | `ucache` | UNET models | Condition-level caching with error tracking | | ||
| | `easycache` | DiT models | Condition-level cache | | ||
| | `dbcache` | DiT models | Block-level L1 residual threshold | | ||
| | `taylorseer` | DiT models | Taylor series approximation | | ||
| | `cache-dit` | DiT models | Combined DBCache + TaylorSeer | | ||
| ### UCache (UNET Models) | ||
| UCache caches the residual difference (output - input) and reuses it when input changes are below threshold. | ||
| ```bash | ||
| sd-cli -m model.safetensors -p "a cat" --cache-mode ucache --cache-option "threshold=1.5" | ||
| ``` | ||
| #### Parameters | ||
| | Parameter | Description | Default | | ||
| |-----------|-------------|---------| | ||
| | `threshold` | Error threshold for reuse decision | 1.0 | | ||
| | `start` | Start caching at this percent of steps | 0.15 | | ||
| | `end` | Stop caching at this percent of steps | 0.95 | | ||
| | `decay` | Error decay rate (0-1) | 1.0 | | ||
| | `relative` | Scale threshold by output norm (0/1) | 1 | | ||
| | `reset` | Reset error after computing (0/1) | 1 | | ||
| #### Reset Parameter | ||
| The `reset` parameter controls error accumulation behavior: | ||
| - `reset=1` (default): Resets accumulated error after each computed step. More aggressive caching, works well with most samplers. | ||
| - `reset=0`: Keeps error accumulated. More conservative, recommended for `euler_a` sampler. | ||
| ### EasyCache (DiT Models) | ||
| Condition-level caching for DiT models. Caches and reuses outputs when input changes are below threshold. | ||
| ```bash | ||
| --cache-mode easycache --cache-option "threshold=0.3" | ||
| ``` | ||
| #### Parameters | ||
| | Parameter | Description | Default | | ||
| |-----------|-------------|---------| | ||
| | `threshold` | Input change threshold for reuse | 0.2 | | ||
| | `start` | Start caching at this percent of steps | 0.15 | | ||
| | `end` | Stop caching at this percent of steps | 0.95 | | ||
| ### Cache-DIT (DiT Models) | ||
| For DiT models like FLUX and QWEN, use block-level caching modes. | ||
| #### DBCache | ||
| Caches blocks based on L1 residual difference threshold: | ||
| ```bash | ||
| --cache-mode dbcache --cache-option "threshold=0.25,warmup=4" | ||
| ``` | ||
| #### TaylorSeer | ||
| Uses Taylor series approximation to predict block outputs: | ||
| ```bash | ||
| --cache-mode taylorseer | ||
| ``` | ||
| #### Cache-DIT (Combined) | ||
| Combines DBCache and TaylorSeer: | ||
| ```bash | ||
| --cache-mode cache-dit --cache-preset fast | ||
| ``` | ||
| #### Parameters | ||
| | Parameter | Description | Default | | ||
| |-----------|-------------|---------| | ||
| | `Fn` | Front blocks to always compute | 8 | | ||
| | `Bn` | Back blocks to always compute | 0 | | ||
| | `threshold` | L1 residual difference threshold | 0.08 | | ||
| | `warmup` | Steps before caching starts | 8 | | ||
| #### Presets | ||
| Available presets: `slow`, `medium`, `fast`, `ultra` (or `s`, `m`, `f`, `u`). | ||
| ```bash | ||
| --cache-mode cache-dit --cache-preset fast | ||
| ``` | ||
| #### SCM Options | ||
| Steps Computation Mask controls which steps can be cached: | ||
| ```bash | ||
| --scm-mask "1,1,1,1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,1" | ||
| ``` | ||
| Mask values: `1` = compute, `0` = can cache. | ||
| | Policy | Description | | ||
| |--------|-------------| | ||
| | `dynamic` | Check threshold before caching | | ||
| | `static` | Always cache on cacheable steps | | ||
| ```bash | ||
| --scm-policy dynamic | ||
| ``` | ||
| ### Performance Tips | ||
| - Start with default thresholds and adjust based on output quality | ||
| - Lower threshold = better quality, less speedup | ||
| - Higher threshold = more speedup, potential quality loss | ||
| - More steps generally means more caching opportunities |
9 changes: 8 additions & 1 deletionexamples/cli/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionsexamples/cli/main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.