Skip to content

perf: store Qwen Image 2.1 prefix cache as F16 with flash attention - #2044

Closed
RChang06 wants to merge 1 commit into
leejet:masterfrom
RChang06:perf/qwen-image-2.1-f16-prefix-cache
Closed

RChang06 wants to merge 1 commit into
leejet:masterfrom
RChang06:perf/qwen-image-2.1-f16-prefix-cache

Conversation

@RChang06

@RChang06 RChang06 commented Sep 24, 2026 •

Copy link
Copy Markdown

Summary

See #2040

  • Store the Qwen Image 2.1 prefix K/V cache as F16 when flash attention is on, since flash attention converts K and V to F16 before the kernel anyway. Output is unchanged, cache memory is halved. Without flash attention, with sage attention, or with --attn-scale, it stays FP32.
  • Store, reuse-path concat, and validator all take the type from one prefix_cache_type() helper. the validator now checks the expected type instead of FP32 only.

Additional Information

All outputs compared by SHA-256 of the decoded RGBA pixels at a fixed seed. Every base/PR pair matched.

RTX 4090 24 GB, qwen_image_2.1-Q8_0.gguf, Qwen3VL-8B-Instruct-Q4_K_M.gguf, mmproj-Qwen3VL-8B-Instruct-F16.gguf, 1024x1024, euler, seed 42, --diffusion-fa --offload-to-cpu:

test master (FP32 cache) this PR (F16 cache) pixels
1 reference, 20 steps cached, 1.20 s/step cached, 1.20 s/step identical
2 references, 20 steps does not fit, falls back, 4.13 s/step, 98.5 s cached, 1.36 s/step, 36.5 s identical
2 references, cfg 4 + negative, --max-vram 16 does not fit, falls back, 8.20 s/step first condition cached, second falls back, 8.29 s/step identical
no flash attention (FP32 kept) fallback fallback identical
--sage-attn (FP32 kept) fallback fallback identical
--attn-scale 2 (FP32 kept) fallback fallback identical
--model-args qwen_image_2_1_prefix_cache=false same as uncached master identical

For two 1024x1024 references (8227-token prefix) the FP32 cache allocation asked for 16967 MB and failed; the F16 one needs 8730 MB and fits next to the weights.

Quadro T2000 4 GB, qwen_image_2.1-Q4_K.gguf, one 512x512 reference (assets/qwen/qwen_image_2.1.png), prompt change 'qwen2.1.cpp' to 'sd.cpp', 8 steps, euler, seed 42, --diffusion-fa --offload-to-cpu:

master this PR
cache FP32, does not fit, falls back after a failed prefill F16, cached (1052 tokens)
per step 25.0 s 12.0 s
sampling, 8 steps 236 s 111 s
pixel hash db04ec5b… db04ec5b…

master (left) vs this PR (right), same image:
compare_master_vs_pr_t2000

Checklist

@RChang06
RChang06 force-pushed the perf/qwen-image-2.1-f16-prefix-cache branch 2 times, most recently from c9b9c68 to 4c5df68 Compare September 24, 2026 12:36
@RChang06
RChang06 force-pushed the perf/qwen-image-2.1-f16-prefix-cache branch from 4c5df68 to 01c53c9 Compare September 24, 2026 12:39
@leejet

leejet commented Sep 24, 2026

Copy link
Copy Markdown
Owner

Superseded by #2045 .

@leejet leejet closed this Sep 24, 2026
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