Repository navigation
Qwen-Image-2.1 fails every render at 20+ steps: FBCache cannot run on a prefix KV cache - #11546
Merged
Merged
Conversation
|
You have reached your Codex usage limits for security reviews. Please try again later. |
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. |
LeoBorcherding
added a commit
to LeoBorcherding/unsloth
that referenced
this pull request
Sep 22, 2026
…ustify FBCache metadata The crash this branch opened against was already fixed on its base by unslothai#11546, whose _reuses_prefix_kv refuses the cache before the registry is consulted, so the entry is restored and only the warning is kept. The earlier cause was wrong too: the sequence shortens between step 0 and step 1 under prefix KV reuse, not between the cond and uncond passes, which cache_context keeps in separate states anyway. Also reverts the pre-commit.ci formatter churn in five unrelated files.
shaojun
pushed a commit
to shaojun/unsloth
that referenced
this pull request
Sep 29, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Qwen-Image-2.1 fails every generation on
mainat 20 steps or more. The load succeeds, the first denoise step succeeds, and the second one dies:Why
Qwen-Image-2.1 caches the prompt and condition-image prefix K and V.
QwenImage21Transformer2DModel.forwardruns the first step over the whole joint sequence to populate that cache, and every later step over the target image tokens alone:So the per-block sequence length changes between step 0 and step 1: 4096 + 201 prompt tokens, then 4096. FBCache stores the first block's residual at step 0 and subtracts the next step's from it elementwise, and reuses the remaining blocks' cached residual the same way. Neither survives a length change.
The step-count rule is what makes this the default rather than an opt-in:
FBCACHE_MIN_STEPSis 20, so an unset cache request engages at 20 steps, which is the step count our own model card recommends for this family.The fix
Refuse the cache when the pipeline drives a prefix KV cache, before
enable_cacherather than after, and run uncached with a logged reason. Read structurally instead of by family name: the transformer's forward accepts akv_cache_modeand the pipeline's denoise loop passes one. Qwen-Image-2.1 is the only family Studio routes there today, but FLUX.2 klein KV and Wan-Animate-2 have the same shape upstream and are covered without another edit.The guard is deliberately conservative in one direction. A checkpoint that carries the parameter but whose pipeline never populates the cache keeps a constant length and would have been safe, and it loses the cache anyway. That costs speed on a model we have not seen. Guessing the other way costs a failed render on one we have.
Evidence
Differential on a B200, both arms identical except for which
diffusion_cache.pyis loaded: same model, prompt, 20 steps, 1024px, seed 42,true_cfg_scale4.0. diffusers is the exact commitdiffusers-main.txtpins (80c7ed26), so this is the tree an install actually gets.mainfbcacheRuntimeErroratfirst_block_cache.py:139, step 2None, refused with a reasonThe refusal reads:
The base arm reproduces the reported failure at the same file and line. The token counts differ only because my prompt is shorter than the reported one (4117 against 4297); the mismatch is the prompt length either way.
What this does not disable
The guard's predicate, evaluated over the whole installed diffusers tree: 2 of 350 pipelines drive a prefix KV cache (
qwenimage21,flux2_klein_kv). The other 348 are untouched and keep FBCache exactly as before. Three transformers acceptkv_cache_mode; the third, Wan-Animate-2, has no pipeline that drives one, and the guard's "the loop must actually pass it" half is what keeps that one cached. There is a test for each of those three cases.Tests
test_diffusion_cache.py: 65 passed. Three new cases, each failing on the parent commit for the right reason:enable_cacheis never calledkv_cache_modeinside a loop that never passes one still engagestest_diffusion_gguf_compile.pydoes not collect in my environment (bitsandbytes CUDA setup), which is unrelated to this change and reproduces without it.Diffusers pin
Checked, no change needed. 0.41.0 is still unreleased, so
diffusers-pin.txtstays at 0.40.0 anddiffusers-main.txtstays at80c7ed26. Upstream main is 7 commits ahead of that pin and none of them touchqwenimage21,first_block_cacheorhooks/, so the defect is live on upstream main too and moving the pin would not have fixed it.Follow-up, not in this PR
A shape-aware FBCache could keep the speed here by storing only the target-token slice of the prefill residual, which would make both stored tensors line up from step 1 on. That means patching diffusers hook internals, and the prefill residual carries cross-attention the decode steps never recompute, so whether reuse is even sound needs measuring rather than asserting. Worth doing separately; not worth blocking a broken default on.
Pre-existing failures, checked not assumed
The wider diffusion selection has three failures on this branch:
All three fail identically on a pristine
origin/mainworktree at the same commit this branch forks from, so they are already red and none of them touch the file this PR changes. They still need fixing, separately.