Repository navigation
Studio: stop a compiled Qwen-Image-2.1 render holding 2 GiB of prefix K/V it does not need - #11882
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b825955b7d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _after_prefill(_module, _args, kwargs, _output): | ||
| if kwargs.get("kv_cache_mode") != "extract": | ||
| return None | ||
| cache = kwargs.get("kv_cache") |
There was a problem hiding this comment.
Compact the cache returned by FLUX.2 extract forwards
For FLUX.2 Klein reference-image renders, Flux2KleinPipeline does not pass a kv_cache during the extract call: Flux2Transformer2DModel.forward creates the cache internally and returns it as (output, kv_cache). Consequently this lookup always yields None, so the hook never compacts FLUX.2's double_block_caches or single_block_caches, leaving the compiled full-sequence K/V buffers pinned. The extract hook needs to obtain the returned cache from _output when it is absent from kwargs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right, FLUX.2 builds the cache inside the extract forward and returns it, so the hook never saw it. Fixed in ee44aaf: the hook now takes the cache from the output (the tuple or the kv_cache field) when none was passed in. Regression test: test_a_cache_returned_by_the_prefill_call_is_compacted (both return shapes); it fails without the fix.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
… K/V it does not need (unslothai#11882) * Studio: keep a compiled denoiser's prefix KV cache at its real size * Studio: tests for the prefix KV compaction * Format * Compact the prefix cache a FLUX.2 extract call returns * Trim comments in prefix KV compaction
Why
Qwen-Image-2.1 keeps a prefix KV cache across the denoise: on the first step every block stores the keys and values of the text (and condition image) tokens with
key[:, prefix].clone(), and later steps only recompute the image tokens. Under Studio's regional compile, inductor lowers that clone to a view of the block's freshly allocated full-sequence K/V buffer. The cache then holds every block's whole prefill K/V (text plus all 4096 image tokens at 1024x1024) for the rest of the render, where it needs a few MB.A CUDA memory snapshot taken at VAE decode entry on a compiled int8 render shows it directly: 7.43 GiB of weights, plus two 1.006 GiB groups of 32 blocks each, both allocated inside the compiled attention call, and nothing else. The same pattern reproduces in a five-line
torch.compilefunction on CPU and CUDA (the stored slice reports 8192 bytes of storage for 384 bytes of data).That 2 GiB sits on top of the VAE decode peak, which is the peak of the whole render, so it decides whether a card fits.
What changes
diffusion_prefix_kv.py: a forward hook on a denoiser whoseforwardtakeskv_cacheandkv_cache_mode. After theextract(prefill) call it walks the cache (the one passed in, or the one the call returns, as FLUX.2 does) and replaces every tensor whose storage is larger than its data with its own copy, which frees the full-sequence buffers. Cached steps are not touched._compile_repeated_blocksinstalls the hook on every DiT it compiles. Eager runs already store a real copy, so they are left alone.layer_cacheswithk/v, FLUX.2 klein KVdouble_block_caches/single_block_cacheswithk_ref/v_ref, Wan-Animate-2key/value). Values are unchanged; only where they live changes.Results
Qwen-Image-2.1, 1024x1024, 25 steps, CFG 1, speed
default(regional compile), B200 shared with other jobs.At 1536x1536 (bf16 resident) the cache pins more, since it holds the full sequence: peak 41.34 -> 36.84 GiB, VAE decode entry 27.20 -> 22.70 GiB. Render time is unchanged: at 1024, interleaved on an idle GPU (main, this PR, main, this PR, 6 steady renders each), the fastest renders were 1.81 and 1.69 s before and 1.67 and 1.70 s after (medians 2.01 and 1.70 before, 1.71 and 2.00 after; another job came back onto the GPU during the last arm); at 1536, 6.18 s before and 6.06 to 6.22 s after.
The compaction costs 2.3 ms once per render (32 blocks, K and V), measured on the real cache shape.
Output is bit-identical. With deterministic kernels (cuDNN benchmark off, native attention), bf16 resident before and after gave identical images for all 5 prompts, and so did int8 resident (peak 24.49 -> 22.49 GiB).
Which torch versions are affected: a compiled
k[:, :3].clone()keeps the full buffer on torch 2.10, 2.11, 2.12 and 2.13 (CUDA) and 2.14 (CPU inductor on Linux x64, Linux ARM64 and Apple Silicon macOS, in CI), and is a real copy on 2.6 and 2.8 (CPU inductor), where the hook finds nothing to do.Tests
tests/test_diffusion_prefix_kv.py:QwenImage21KVCacheclass;k[:, :prefix].clone()keeps the full buffer (skips if the installed inductor ever copies it out);_compile_repeated_blocksinstalls the hook.Reverting the
diffusion_speed.pyhunk fails the wiring test.test_diffusion_speed.py,test_diffusion_prefix_kv.py,test_diffusion_backend.py,test_video_backend.py,test_diffusion_cache.py,test_diffusion_cuda_graph.py: 1013 passed, 1 skipped (the premise test skips on torch 2.6 and 2.8, where inductor already copies the slice; on 2.10 to 2.13 it runs).test_diffusion_prefix_kv.pyalso passes on torch 2.6, 2.8, 2.11 and 2.12, and on CPU torch 2.14 on Linux x64, Linux ARM64, macOS 15 (Apple Silicon) and Windows (the premise test skips on Windows, which has no compiler for CPU inductor).Not measured