Skip to content

feat(flux): run FLUX.1 attention on SageAttention when enabled - #9658

Merged
lstein merged 17 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/sage-attention-flux1
Oct 16, 2026
Merged

lstein merged 17 commits into
invoke-ai:mainfrom
Pfannkuchensack:feat/sage-attention-flux1

Conversation

@Pfannkuchensack

@Pfannkuchensack Pfannkuchensack commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Note

Stacked PR 1 of 8: merge after #9657. 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, e864d62574.

Summary

With attention_backend: sage, FLUX.1 denoising enters the SageAttention scope from #9657, so its eligible attention calls run on SageAttention, including ControlNets that run inside the denoising loop. Masked attention, such as regional prompting, and everything outside the loop stay on PyTorch SDPA. The default auto is unchanged. The docs page lists FLUX.1 under Supported models.

Related Issues / Discussions

Stacked on #9657 (SageAttention backend).

QA Instructions

FLUX.1 dev FP8, model loaded, three arms per seed as described in #9657:

1024² 2048²
Step, default SDPA 703 ms 3730 ms
Step, cuDNN-first SDPA 662 ms 3082 ms
Step, SageAttention 606 ms (−14 %) 2314 ms (−38 %)
Whole generation 16.1 → 14.0 s (−13 %) 84.2 → 54.8 s (−35 %)
Peak VRAM in the denoise scope unchanged unchanged
LPIPS against default SDPA, median (cuDNN-first) 0.024 (0.015) 0.024 (0.009)
  • Blind side-by-side review: 8 of 8 pairs equal.
  • Every attention call of the transformer qualified; none fell back to SDPA.
  • Accuracy on real activations, every call of step 0 at 1024² (114 calls): worst cosine 0.9994, relative L2 0.024 against fp32.
  • The timings, LPIPS and blind review 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. For one seed at 1024², LPIPS against SDPA dropped from 0.128 to 0.041 with it.

To try it: install SageAttention as the docs page from #9657 describes, set attention_backend: sage, restart and generate with FLUX.1. 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.

Compatibility / Rollout

Merge after #9657. 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.
@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
@Pfannkuchensack
Pfannkuchensack marked this pull request as ready for review October 12, 2026 20:44
…lower with SageAttention

On Windows the driver moves memory to shared system memory instead of raising out-of-memory, so the fallback never triggers and the extra memory per call slows every step; issue invoke-ai#9831 tracks a fix.
# Conflicts:
#	invokeai/app/services/config/config_default.py
#	invokeai/frontend/api/openapi.json
#	invokeai/frontend/api/schema.ts

@JPPhoto JPPhoto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@lstein
lstein enabled auto-merge October 16, 2026 01:03
@lstein
lstein merged commit 861210e into invoke-ai:main Oct 16, 2026
17 checks passed
lstein added a commit that referenced this pull request Oct 16, 2026
> [!NOTE]
> Stacked PR 2 of 8: merge after #9658. 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,
[`a1e97b7b09`](Pfannkuchensack@a1e97b7).

## Summary

With `attention_backend: sage`, FLUX.2 denoising 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 FLUX.2 Klein 9B under Supported
models.

## Related Issues / Discussions

Stacked on #9657 (SageAttention backend) and #9658, the PR before it.

## QA Instructions

FLUX.2 Klein 9B FP8, model loaded, three arms per seed as described in
#9657:

|  | 1024² | 2048² |
| --- | --- | --- |
| Step, default SDPA | 719 ms | 3583 ms |
| Step, cuDNN-first SDPA | 699 ms | 2960 ms |
| Step, SageAttention | 657 ms (−9 %) | 2483 ms (−31 %) |
| Whole generation | 15.6 → 15.1 s (−3 %) | 27.1 → 23.1 s (−15 %) |
| Peak VRAM in the denoise scope | unchanged | unchanged |
| LPIPS against default SDPA, median (cuDNN-first) | 0.043 (0.011) |
0.059 (0.018) |

- Blind side-by-side review: 8 of 8 pairs equal. The pairs with the
largest LPIPS (0.14 at both sizes) show the same scene, sharpness and
detail; they differ in composition details such as pose and background,
the kind of drift the exact cuDNN swap also causes, only larger.
- Every attention call qualified; none fell back to SDPA.
- Accuracy on real activations, every call of step 0 (32 calls): worst
cosine 0.9993, relative L2 0.026.
- Only Klein 9B was measured; the other FLUX.2 variants enter the same
scope.
- The timings, LPIPS and blind review come from the measurement
prototype, which still used `sageattn()`'s default K smoothing; the
selection #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 FLUX.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.

## Compatibility / Rollout

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

## Checklist

- [x] _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_
- [x] _Relevant performance/efficiency opportunities considered;
material claims have evidence_
- [x] _Material review findings resolved and relevant checks rerun_
- [x] _Documentation added / updated (if applicable)_
- [ ] _Updated `What's New` copy (if doing a release after this PR)_
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.

3 participants