Skip to content

feat(wan): run Wan 2.2 attention on SageAttention when enabled - #9665

Draft
Pfannkuchensack wants to merge 30 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/sage-attention-wan
Draft

Pfannkuchensack wants to merge 30 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/sage-attention-wan

Conversation

@Pfannkuchensack

@Pfannkuchensack Pfannkuchensack commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Note

Stacked PR 8 of 8: merge after #9664. It targets main, so until the PRs before it merge, its diff also shows their commits and the merges that carry them forward. This PR's own change is one commit, cd6e58c52d.

Summary

With attention_backend: sage, Wan 2.2 denoising ("Denoise - Wan 2.2" and "Denoise Video - Wan 2.2") enters the SageAttention scope from #9657, so its eligible attention calls run on SageAttention. Everything else stays on PyTorch SDPA, and the default auto is unchanged. The docs page lists Wan 2.2 under Supported models.

Related Issues / Discussions

Stacked on #9657 (SageAttention backend) and #9664, the PR before it. Closes #9529, which asks for SageAttention to speed up Wan 2.2. That report is Wan 2.2 A14B on an RTX 5090 Laptop GPU (compute capability 12.0, per-warp FP8 kernel); neither was measured here.

QA Instructions

Wan 2.2 TI2V-5B GGUF Q8 through "Denoise Video - Wan 2.2", model loaded, three arms per seed as described in #9657:

1280×704, 121 frames
Step, default SDPA 10419 ms
Step, cuDNN-first SDPA 7890 ms
Step, SageAttention 6358 ms (−39 %)
Whole generation 380.9 → 247.9 s (−35 %)
Peak VRAM in the denoise scope unchanged
Per-frame LPIPS against default SDPA, median (cuDNN-first) 0.104 (0.079)
Flicker ratio against default SDPA 0.998
  • All 120 attention calls per step ran on SageAttention, including the text cross-attention with 512 text tokens; none fell back.
  • Accuracy on real activations, every call of step 0 (120 calls): worst relative L2 0.076, in the text cross-attention; the heads with low cosine there contribute almost nothing (output norm 0.08 against a median of 5.7).
  • Not measured: the image invocation and the A14B models, which enter the same scope. For A14B at 1280×720 with 81 frames, a self-attention call needed about 1.9 GB extra peak memory on SageAttention in the microbenchmark.
  • The timings and LPIPS come from the measurement prototype, which still used sageattn()'s default K smoothing; the selection feat(attention): add opt-in SageAttention backend for diffusion models #9657 ships is faster per call.

To try it: install SageAttention as the docs page from #9657 describes, set attention_backend: sage, restart and generate with Wan 2.2. The first generation logs SageAttention ... serves diffusion-model attention on cuda:0 (...) with the GPU and kernel name, and with log_level: debug every generation logs how many calls ran on SageAttention and why the others did not. With attention_backend: auto the output is bit-identical to main.

Checks: ruff clean; pytest tests/app/invocations passes with the whole stack applied (2090 passed). No call-site test was added: this change only enters the scope, whose behavior #9657 tests.

Review

No material findings for this call site. Remaining: the image invocation and A14B are unmeasured.

Compatibility / Rollout

Merge after #9664. No API or persisted-state change; only attention_backend: sage behaves differently.

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Meaningful regression coverage added / updated where needed; obsolete tests/code removed
  • Persisted-state and API changes include required migrations / compatibility validation
  • Relevant performance/efficiency opportunities considered; material claims have evidence
  • Material review findings resolved and relevant checks rerun
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

New `attention_backend: auto | sage` setting; with `sage`, eligible PyTorch SDPA calls inside a denoising scope run on SageAttention 2.2 and every other call keeps SDPA unchanged.
Each GPU's first SageAttention call is checked against SDPA and a failing GPU falls back for the session; no model family enters the scope yet.
Adds the SageAttention docs page and the regenerated config schema.
With `attention_backend: sage`, FLUX.1 denoising, including ControlNets inside the loop, enters the SageAttention scope.
RTX 4090, FLUX.1 dev FP8: 16.1 s → 14.0 s at 1024², 84.2 s → 54.8 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, FLUX.2 denoising enters the SageAttention scope.
RTX 4090, FLUX.2 Klein 9B FP8: 15.6 s → 15.1 s at 1024², 27.1 s → 23.1 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, Z-Image denoising enters the SageAttention scope; Z-Image Control masks its attention and keeps SDPA.
RTX 4090, Z-Image FP8: 10.4 s → 10.0 s at 1024², 33.7 s → 25.9 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, Qwen-Image denoising enters the SageAttention scope.
RTX 4090, Qwen-Image 2512 GGUF Q4_K_M: 78.8 s → 73.5 s at 1024², 208.9 s → 150.3 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, Krea-2 denoising enters the SageAttention scope unless INVOKE_KREA2_SDPA_BACKEND pins a PyTorch kernel.
RTX 4090, Krea-2 Turbo GGUF Q4_K_M: 11.5 s → 11.0 s at 1024², 42.7 s → 36.2 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, SDXL denoising and tiled multi-diffusion enter the SageAttention scope; SD1.5 and SD2 do not.
RTX 4090: 4.4 s → 4.2 s at 1024², 19.6 s → 16.1 s at 2048², peak VRAM unchanged; a blind review judged no image worse.
With `attention_backend: sage`, LTX-2 denoising enters the SageAttention scope; video self-attention qualifies, while the masked text cross-attention and the short audio attention keep SDPA.
RTX 4090, LTX-2.5 distilled int8 at 1248×704, 121 frames: 72.0 s → 63.4 s, peak VRAM unchanged, no added flicker.
With `attention_backend: sage`, Wan 2.2 image and video denoising enter the SageAttention scope.
RTX 4090, Wan 2.2 TI2V-5B GGUF Q8 at 1280×704, 121 frames: 380.9 s → 247.9 s, peak VRAM unchanged, no added flicker.
@github-actions github-actions Bot added python PRs that change python files invocations PRs that change invocations backend PRs that change backend files services PRs that change app services python-tests PRs that change python tests docs PRs that change docs labels Oct 3, 2026
…ent SageAttention failures

sm86 now runs the Triton kernel upstream's sageattn picks there, and sm87 gets none; the Windows build's CUDA routing for both is unexplained and unmeasured here.
Out-of-memory and Triton cache errors fall back to SDPA for that call (three retire the device), and the first-call check runs per precision and head size.
The kernel runs with the tensors' device current, so a single-device install on cuda:1 uses SageAttention too.
An out-of-memory error now moves only the rest of that generation to SDPA, logged once, instead of counting toward retiring the device; out-of-memory is recognised by the shared is_oom_error.
Only Triton cache failures in a row retire a device, and a success resets the count.
# Conflicts:
#	invokeai/frontend/api/openapi.json

This branch has not been deployed

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

Labels

7.0.0 backend PRs that change backend files docs PRs that change docs invocations PRs that change invocations python PRs that change python files python-tests PRs that change python tests services PRs that change app services

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[enhancement]: Requesting addition of flash-attn and sage/sage2-attn optimizations in InvokeAI

3 participants