Skip to content

Studio: let an explicit FBCache request engage on Qwen-Image-2.1 and other prefix-KV models - #11713

Merged
danielhanchen merged 2 commits into
mainfrom
studio-prefix-kv-fbcache
Sep 24, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-prefix-kv-fbcache

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Problem

On Qwen-Image-2.1, transformer_cache=fbcache never engages. Studio refuses First-Block-Cache for every transformer that reuses a prefix KV cache (_reuses_prefix_kv). These transformers run step 0 over the prompt plus the target tokens (4096 + 201 at 1024px) and every later step over the target alone. FBCache subtracts the stored head-block residual elementwise, so step 1 would raise The size of tensor a (4096) must match the size of tensor b (4297).

A ComfyUI comparison on Qwen-Image-2.1 showed the cost of that refusal. ComfyUI's EasyCache was the only setting where ComfyUI came out ahead, while Studio's step cache silently did nothing.

Fix

  • install_fbcache_length_guard() wraps diffusers' FBCHeadBlockHook._should_compute_remaining_blocks. It is process-wide and idempotent. When the stored head residual or tail residual has a different shape from this step's, the hook runs the full pass. Stored residuals from a different sequence can never be reused, so "compute" is the only correct answer. The full pass then stores residuals at the new length, and later steps cache normally. Calls where the length is unchanged go through the original function untouched.
  • apply_step_cache(..., length_changes_ok=False): only an explicit fbcache request passes True. The automatic policy still refuses these families, so default renders are unchanged. At 40 steps the default threshold skips about half the steps, which is too lossy to turn on silently.
  • An explicit request that does not engage now reports requested, but this model does not support step caching; running uncached. Before, it reported plain requested while the value was off.
  • Video is unchanged: video.py does not pass the flag.

Measurements

Qwen-Image-2.1, bf16, speed default, 1024px, CFG 1, B200. Other tenants were active on the GPU, so absolute times are inflated. Each run brackets the cached arms with an uncached arm on the same GPU. LPIPS (alex) is measured against the uncached images at the same step count.

25 steps, 8 prompts:

Arm Head calls skipped LPIPS mean / max Steady s/image
off (start / end) - 0.0001 (end vs start) 2.70 / 2.20
fbcache, default threshold 0.08 97 / 315 (31%) 0.0097 / 0.029 1.81
fbcache 0.12 154 / 315 (49%) 0.110 / 0.286 1.35
fbcache 0.2 212 / 315 (67%) 0.174 / 0.339 1.02

40 steps (the family default), 4 prompts, threshold 0.08: 54% of steps skipped, LPIPS 0.04 to 0.17 (mean 0.089), steady 6.80 s to 4.09 s. For reference, ComfyUI's EasyCache at its default setting skipped 14 of 25 steps at LPIPS 0.083 on the same model. That skip rate at 40 steps is why the automatic policy stays off for these families.

After this change, the automatic setting still leaves the cache off (0 head calls), and an explicit fbcache engages (44 of 135 skipped on a short run).

AMD gfx1151 (Linux and Windows)

Strix Halo (Radeon 8060S), Qwen-Image-2.1 bf16, 1024x1024, 20 steps, CFG 4, eager (Studio does not compile on ROCm), torch 2.11 ROCm, no HF token on the runners. Each arm runs in a fresh process: this branch uncached (ref), this branch with transformer_cache=fbcache (fb), and main with fbcache requested (base_fb). Seconds are per image, and the first image is a warm-up.

Host ref fb base_fb (main) fb LPIPS vs ref
Linux (ROCm 7.13) 1021.9, 219.5 174.3, 176.4 221.9, 215.8 0.0047, 0.0072
Windows 11 (AMD gfx1151 wheels) 286.9, 524.3 212.7, 221.8 266.4, 277.8 0.0046, 0.0080
  • On this branch the cache engages on both OSes: the log shows diffusion.cache: fbcache engaged and status resolves transformer_cache to fbcache.
  • On main the same request is still refused (off), and its renders are pixel-identical to the uncached reference (LPIPS 0), so the base arm reproduces the defect.
  • Linux: about 20% faster per image, 219.5 s down to 176.4 s.
  • Windows: the uncached reference's second image (524.3 s) is an outlier; the warm-up render took 286.9 s. The uncached base arm (266.4 to 277.8 s) is the fair comparison there, and against it fbcache is again about 20% faster (212.7 to 221.8 s).

Tests

  • New tests:
    • The guard recomputes on a head-length change and on a tail-length change, defers to the original on equal lengths, and installs only once.
    • A prefix-KV transformer still refuses on the automatic path and engages on an explicit request once the guard is in.
    • Without the guard it refuses as before.
  • test_diffusion*.py and test_video*.py: 3828 passed. The 16 failures are the same environment-only ones as on main: 15 in test_diffusion_dataset_api.py and one GGUF Q8_0 registry case.

Not covered

  • FLUX.2 klein KV takes the same path. Its hidden-states sequence shrinks while the text stream keeps its length, so the guard covers it, but it has not been run on a GPU.
  • Wan-Animate-2 (video) keeps the old refusal.

Qwen-Image-2.1 (and FLUX.2 klein KV) run step 0 over prompt + target tokens and
later steps over the target alone, so First-Block-Cache's residual subtraction
raised and Studio refused the cache outright. A process-wide guard on the head
hook now forces a full pass whenever the stored residuals were taken at another
sequence length; later steps cache normally. Only an explicit fbcache request
uses it: the automatic policy keeps these families uncached, since at 40 steps
the default threshold skips about half the steps. An explicit request that does
not engage now says so in the status reason.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T01:24:26.009587Z 3ebb775 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 5928a920d5

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 3ebb77584b

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 2be14e0 into main Sep 24, 2026
52 of 53 checks passed
@danielhanchen
danielhanchen deleted the studio-prefix-kv-fbcache branch September 24, 2026 07:18
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…other prefix-KV models (unslothai#11713)

* Studio: let an explicit FBCache request engage on prefix-KV image models

Qwen-Image-2.1 (and FLUX.2 klein KV) run step 0 over prompt + target tokens and
later steps over the target alone, so First-Block-Cache's residual subtraction
raised and Studio refused the cache outright. A process-wide guard on the head
hook now forces a full pass whenever the stored residuals were taken at another
sequence length; later steps cache normally. Only an explicit fbcache request
uses it: the automatic policy keeps these families uncached, since at 40 steps
the default threshold skips about half the steps. An explicit request that does
not engage now says so in the status reason.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant