Repository navigation
Studio: let an explicit FBCache request engage on Qwen-Image-2.1 and other prefix-KV models - #11713
Conversation
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.
|
@codex review |
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: Didn't find any major issues. Keep them coming! 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
…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>
Problem
On Qwen-Image-2.1,
transformer_cache=fbcachenever 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 raiseThe 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 explicitfbcacherequest passesTrue. 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.requested, but this model does not support step caching; running uncached. Before, it reported plainrequestedwhile the value wasoff.video.pydoes 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:
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
fbcacheengages (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), andmainwithfbcacherequested (base_fb). Seconds are per image, and the first image is a warm-up.diffusion.cache: fbcache engagedand status resolvestransformer_cachetofbcache.mainthe 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.Tests
test_diffusion*.pyandtest_video*.py: 3828 passed. The 16 failures are the same environment-only ones as onmain: 15 intest_diffusion_dataset_api.pyand one GGUF Q8_0 registry case.Not covered