Skip to content

Unsloth Studio: note that a block's return signature alone does not justify FBCache metadata - #11553

Closed
LeoBorcherding wants to merge 3 commits into
unslothai:mainfrom
LeoBorcherding:fix-qwenimage21-fbcache-registration
Closed

LeoBorcherding wants to merge 3 commits into
unslothai:mainfrom
LeoBorcherding:fix-qwenimage21-fbcache-registration

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Comment only. No behaviour change.

#11546 already fixes the crash this branch was opened for: _reuses_prefix_kv refuses FBCache before the registry is consulted, so removing the QwenImage21TransformerBlock entry changes nothing about it. The entry is restored and only the warning is kept, beside it rather than in place of it.

The cause as originally written was also wrong. The sequence length changes between step 0 and step 1 under prefix KV reuse (4096 image-only at 1024, 4260 joint, 164 text prefix), not between the cond and uncond passes, which cache_context("cond") and cache_context("uncond") keep in separate states.

Verification

From studio/backend, at e3852eb1d2:

pytest tests/test_diffusion_cache_block_registry.py tests/test_diffusion_cache.py
65 passed, 2 skipped

No hardware run, and none is needed: the diff is five lines of comment.

LeoBorcherding and others added 2 commits September 22, 2026 11:03
Registering the block as (0, None) let enable_cache succeed, and every
Qwen-Image 2.1 generation then failed at the first step inside FBCache's
_should_compute_remaining_blocks on a sequence length mismatch: the
transformer hands the block a JOINT text+image sequence, and the pipeline's
cond and uncond passes carry different text lengths.

diffusers ships no metadata for that class. An unregistered class renders
uncached, which costs the step-cache saving; a wrongly registered one cannot
generate at all. Drop the entry and keep the registration machinery for a
block that genuinely needs one.

The test that asserted the registration now asserts its absence, so a re-add
from reading the block's return signature fails loudly.
@LeoBorcherding
LeoBorcherding marked this pull request as ready for review September 22, 2026 17:07
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for chasing this down, but I do not think this one should land as it stands. Three things.

The crash is already fixed on your base. #11546 landed in c23f8b06df and added _reuses_prefix_kv to diffusion_cache.py. apply_step_cache checks it before enable_cache, and for Qwen-Image-2.1 it returns True, so FBCache is refused for the family and the log says transformer reuses a prefix KV cache ... running uncached. Your branch includes that commit (your own diff touches _PrefixKVTransformer, which came with it). So on this branch's base the registry entry is unreachable for this family and dropping it changes nothing about the 500. I confirmed the fix on the reporting hardware before that merged.

The stated cause does not hold. pipeline_qwenimage21.py runs the two passes inside self.transformer.cache_context("cond") and cache_context("uncond"), so the cond and uncond residuals live in separate cache states and cannot be subtracted from each other. The numbers point the other way too: 4096 is exactly the image-only length at 1024 (64x64 patches), 4260 the joint text+image sequence, so the 164 is the text prefix, not the gap between a prompt and a shorter negative prompt. That is the prefix KV signature: step 0 runs kv_cache_mode="extract" over the joint sequence, every later step runs "cached" over the target tokens alone after joint_hidden_states[:, prefix_len:].

Five of the eight files are formatter churn. prequant_safetensors.py, sd_cpp_backend.py, test_diffusion_precision.py, test_diffusion_te_prequant.py and test_prequant_safetensors.py are line-wrapping only and unrelated to the fix. Those should come out whatever happens to the rest.

What I would do with it: drop the code change and keep the comment. The warning against adding a block back on the strength of reading its return signature is worth having, and it belongs next to the entry rather than in place of it. The entry itself should stay, because it is what makes enable_cache fail gracefully rather than raise if the prefix KV condition ever stops holding for this family.

The real open question is still the one you named at the end: whether FBCache can be made correct across a prefix KV denoise loop at all, which would give the step-cache saving back on a 20+ step model. That is worth doing and is a different change.

…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.
@LeoBorcherding LeoBorcherding changed the title Studio: stop registering QwenImage21TransformerBlock for FBCache Unsloth Studio: note that a block's return signature alone does not justify FBCache metadata Sep 22, 2026
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Agreed, dropping the code change. Pushed e3852eb1d2: the entry is back with the warning beside it rather than in place of it, and the cause is restated as prefix KV reuse (step 0 runs kv_cache_mode="extract" over the joint sequence, later steps run "cached" over the target tokens after joint_hidden_states[:, prefix_len:]), not a cond/uncond text-length gap. cache_context("cond") and cache_context("uncond") do keep those residuals in separate states, so that reading was wrong. Title and body updated to match.

The five formatter files are out. They came from 2b80d8ab50, a pre-commit.ci auto-fix pushed on top of the branch, not from the change itself. Net diff is now five lines of comment in one file.

One correction on the last point: an unregistered class does not raise out of apply_step_cache either. enable_cache sits in the try at diffusion_cache.py:426 and the handler at 447 tears down partial hooks and runs uncached. The entry is still worth keeping for the other reason, that it is what a prefix-KV-correct FBCache would need.

On that: happy to take it as a separate change if it is wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants