Skip to content

Studio diffusion: report the attention backend that actually runs - #18418

Merged
danielhanchen merged 6 commits into
mainfrom
studio-attention-sage-honest-status
Nov 19, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-attention-sage-honest-status

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

When a load asks for a specific attention backend (SageAttention, flash, cuDNN, native, or INT8 attention On), Studio now either runs that backend or the status says what ran instead and why. The status is read off the DiT's attention modules, so it never names a backend that no module routes to.

Before this change, an explicit attention_backend="sage" that could not run (SageAttention not installed, its probe failed, unsupported dtype or head_dim, a ROCm / Apple Silicon / CPU load) fell back to SDPA, and the status said value: "native", status: "applied", reason: "diffusers default". The badge stayed hidden, so the page looked as if the pick had been honoured.

Changes

Status (resolved.attention_backend)

  • An explicit pick that did not engage now reads fell_back, or unsupported where the platform or card cannot run it. The reason names the cause, for example:
    • sageattention is not installed and the Hugging Face kernels-hub build could not be loaded (...)
    • SageAttention failed its check on this GPU (self-check: cosine ...)
    • SageAttention serves head_dim <= 128 and this model uses 256
    • SageAttention needs an NVIDIA CUDA GPU and this load runs on Apple Silicon (MPS)
    • cuDNN attention has no kernel for this model's head_dim with this torch/cuDNN build
    • FlashAttention 3 needs a sm90 to below sm100 GPU and this one is sm89
  • INT8 attention On over an explicit pick says the INT8 kernel runs, and on how many modules.
  • The entry carries the module census: engaged (the backend the modules run), modules (how many run it) and routed (every backend's count). A split is named in the reason, for example 40 of 60 attention modules run SageAttention, 20 run native SDPA.
  • The value is never a backend with 0 modules. The image status re-reads the census when a later race moves modules.

Explicit picks are never raced over

  • The image and video loads already kept an explicit pick away from the INT8 races (Studio: per-load INT8 attention Auto / On / Off, MiniMax-H3 video raced INT8 attention by default #18248). Tests now pin this for the strided route, the dispatch route and the INT8 On layering.
  • MiniMax-H3-Image ignored the load's attention pick entirely. It now honours it:
    • an explicit sage pins SageAttention on the DiT blocks, with no race against native and no INT8 race (INT8 only when its control is On);
    • any other explicit pick keeps the checkpoint's SageAttention off;
    • its status gains an attention_backend entry.

Frontend

  • The existing attention badge shows a declined pick as SAGE -> NATIVE, with the backend's reason in the tooltip. unsloth_int8 reads INT8. No new UI elements.
  • The loaded build summary names the backend the modules run.
  • The Attention select keeps the user's own pick when it fell back, so the next load asks for it again (for example after installing SageAttention).

Tests

  • New tests/test_diffusion_attention_honest_status.py (24 tests, fake backends, real module census). It covers:
    • an explicit sage pick winning against the INT8 race, with both the pip and the kernels-hub build;
    • sage missing, probe failure, import failure, head_dim, fp32, and MPS / CPU / ROCm;
    • INT8 On over sage, and an INT8 claim with 0 routed modules;
    • split censuses and the live re-read;
    • native, cuDNN, flash and FlashAttention 3 picks;
    • torch.compile: a compiled DiT that reads each processor's backend, with every status read run between renders. It shows 0 graph breaks, 1 compiled frame before and after (no recompile) and 0 tensor ops on the status path, so no host sync. A deliberate graph_break() is counted first to prove the counters work.
  • Two new MiniMax-H3-Image tests: the explicit pick beats the checkpoint and its races, and the load passes the pick and reports it.
  • New frontend tests/attention-resolved-badge.test.ts (9 tests).
  • All of them fail on main and pass here. The 40 attention, INT8 and H3 backend test files pass: 1518 on main, 1543 here (the 25 new tests). The 36 frontend tests pass, and tsc is clean.

GPU check (Colab L4)

One Colab L4 VM (torch 2.13.0+cu130, diffusers 0.42.1). Studio was installed with ./install.sh --local at the merge base. The head tree is that tree plus this change. Every arm is a fresh process with attention_backend="sage" and speed left at its default (the DiT compiles). FLUX.2-klein-4B renders 768 px in 4 steps. Qwen-Image-2.1 (model_kind=pipeline) renders 768 px in 8 steps.

  • "installed" means the kernels-hub SageAttention 2 build, which is what Studio installs and loads for sage (kernels 0.12.3, no pip sageattention).
  • "not installed" means kernels and sageattention removed, with UNSLOTH_DIFFUSION_ATTENTION_INSTALL=0.
  • Sage calls are counted inside Studio's opaque Sage op, so they are real runtime counts under compile too.
model Sage tree status value / status status reason modules in status modules routed (census) Sage calls per render INT8 calls
FLUX.2-klein-4B installed base sage_hub / applied requested not reported 25 sage_hub 100 (25 x 4 steps) 0
FLUX.2-klein-4B installed head sage_hub / applied requested 25 25 sage_hub 100 0
Qwen-Image-2.1 installed base sage_hub / applied requested not reported 32 sage_hub 224 of 256 0
Qwen-Image-2.1 installed head sage_hub / applied requested 32 32 sage_hub 224 of 256 0
FLUX.2-klein-4B not installed base native / applied diffusers default not reported 25 native 0 0
FLUX.2-klein-4B not installed head native / fell_back sageattention is not installed and the Hugging Face kernels-hub build could not be loaded (the kernels package is not importable) 25 25 native 0 0
Qwen-Image-2.1 not installed base native / applied diffusers default not reported 32 native 0 0
Qwen-Image-2.1 not installed head native / fell_back same as above 32 32 native 0 0
  • The explicit pick is not raced in either tree: 0 INT8 calls in every arm, although both DiTs are on the INT8 Auto list.
  • The status now matches what ran in every arm. On main, the not-installed arms report the fallback as an honoured pick.
  • On Qwen-Image-2.1, 32 of the 256 attention calls per render do not run Sage. That is the same in both trees: Sage's per-call guard sends calls it cannot take to SDPA. This change does not report that per-call split (see below).

Not covered

  • Nothing on the per-call attention path changed: the Sage / FA4 guards, the strided processors, the INT8 op and the dispatch route are untouched. The new code runs at load time and when the status is read. Graph breaks and recompiles were counted on CPU (torch 2.14.1) with Dynamo counters. They were not counted on the GPU arms, which ran torch 2.13. Other torch versions were not run.

  • Per-call fallbacks inside an engaged Sage backend (a masked call, a head_dim above 128 on some modules) are not in the status. Under compile those calls are traced, not counted. The census is per module.

  • pip SageAttention 2 was not run end to end on the GPU. Only the kernels-hub build was; the pip path is covered by the CPU tests.

  • ROCm, Apple Silicon and Windows fallbacks, the MiniMax-H3 video and MiniMax-H3-Image picks, and flash / cuDNN / FlashAttention 3 picks were tested on CPU with fake backends only. None ran on a GPU here.

  • The INT8 On over Sage arm was not run on the GPU.

UI before / after

Images page, loaded diffusers build on a CUDA host, Advanced > Attention set to SageAttention. Left = main (merge base ec3ba179c), right = this PR (f9a0c756f). The status served to each side was produced by that side's own backend code for the same load.

SageAttention requested but not installed (no hub build): main shows no badge, so the UI implies Sage ran. This PR shows SAGE → NATIVE next to Attention, and the tooltip says why.

Attention badge, sage missing: main vs this PR

SageAttention requested with INT8 attention On: main shows no badge. This PR shows SAGE → INT8, and the tooltip names Studio's INT8 kernel and how many attention modules it runs.

Attention badge, INT8 On over a sage pick: main vs this PR

Loaded build summary, same INT8 On load: the Attention row reads UNSLOTH_INT8 on main and INT8 with this PR, naming what the attention modules run.

Loaded build Attention row: main vs this PR

The attention_backend status entry is now read off the DiT's attention
modules instead of the load's request:

- An explicit pick that did not engage (SageAttention not installed, its
  probe failed, dtype or head_dim unsupported, cuDNN without a kernel for
  the head_dim, FlashAttention 3 off Hopper, a non NVIDIA platform) reads
  fell_back or unsupported, names what ran instead and says why. Before,
  it read "applied" with the reason "diffusers default".
- INT8 attention On over an explicit pick says the INT8 kernel runs and
  on how many modules.
- The value is never a backend with 0 modules: the entry carries the
  module census (engaged, modules, routed), and the image status re-reads
  it when a later race moves modules.
- MiniMax-H3-Image now honours the load's attention pick: an explicit
  "sage" pins SageAttention on the DiT blocks with no race against native
  and no INT8 race (INT8 only when its control is On), and any other
  explicit pick keeps the checkpoint's SageAttention off. Its status gains
  an attention_backend entry.
- The existing attention ResolvedBadge shows a declined pick as
  "SAGE -> NATIVE" with the backend's reason in its tooltip, and INT8 as
  "INT8".
- The loaded build summary names the backend the modules run.
- The Attention select keeps the user's own pick when it fell back, so
  the next load asks for it again.
The status path stamps a select-level decline on the pipeline objects only,
never on a DiT that may already be compiled. A new test compiles a DiT whose
forward reads each processor's backend, runs every status read between
renders, and checks there are no graph breaks, no recompiles and no tensor
ops on that path.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T06:04:36.062865Z 53dc156 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. 👍

Reviewed commit: f9a0c756f1

ℹ️ 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 pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 19, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 19, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 19, 2026
…esponse model

The attention_backend entry gained engaged, modules and routed, but the
status response model did not declare them, so pydantic dropped them
before any client saw the module census. Declare them (defaulting to
null) and pin it with a test through both status response models.

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 48aea87de6

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

# Conflicts:
#	studio/backend/core/inference/diffusion.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 53dc156a73

ℹ️ 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 4d93c83 into main Nov 19, 2026
@danielhanchen
danielhanchen deleted the studio-attention-sage-honest-status branch November 19, 2026 17:34
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…ok, H3-Image LoRA reload message) (#18583)

`test_attention_hook_installs_on_cudnn_pins` (#18083) read the source of
`_apply_attention_backend` and looked for the literal `if backend ==
"_native_cudnn" and target is not None and _is_cuda_nvidia(target):`
line. #18418 moved that branch into `_apply_attention_backend_impl`, so
on main the test fails with `ValueError: substring not found` before it
checks anything. The production hook is still installed; only the test
broke, and a test that greps source text would break again on the next
refactor.

## Change

Test only (`tests/test_diffusion_qwenimage21_strided_attn.py`). The test
now runs the real `_apply_attention_backend` on a fake DiT, with the
card checks stubbed (`_is_cuda_nvidia`, `_cudnn_serves_pipe`, kernel
install, ROCm guards) and the Qwen-Image-2.1 and FLUX.3 strided installs
replaced by recorders. No production code changes.

## Tests

- `test_attention_hook_installs_on_cudnn_pins`: a `_native_cudnn` pin on
an NVIDIA target engages, pins the DiT, and calls the Qwen-Image-2.1
strided install with `(pipe, "_native_cudnn", target, logger)`. The old
version fails on main (`ValueError`), the new one passes here. With the
`install_q21_strided(...)` call removed from
`_apply_attention_backend_impl`, the new test fails, so it still guards
the hook.
- `test_attention_hook_skips_other_pins` (2 cases): a cuDNN pin on a
non-NVIDIA target and a `_native_math` pin on NVIDIA engage without
calling either strided install.

Suite (`test_diffusion_qwenimage21_strided_attn.py`,
`test_diffusion_flux3_strided_attn.py`,
`test_diffusion_int8_attention_auto.py`,
`test_diffusion_int8_attention_dispatch.py`,
`test_diffusion_int8_attention_persisted.py`,
`test_diffusion_attention.py`,
`test_diffusion_attention_honest_status.py`,
`test_diffusion_attention_cudnn_head_dim.py`): main 272 passed, 1
skipped, 1 failed (`test_attention_hook_installs_on_cudnn_pins`); this
branch 275 passed, 1 skipped, 0 failed.

## Not proven here

No GPU run; none is needed for a test-only change. The new test fakes
the card and the installs, so it shows the apply calls the hook, not
that the strided processors run on a real NVIDIA card (the existing
`gpu`-marked tests in the same file cover that).

## Second stale test: MiniMax-H3-Image LoRA reload message


`tests/test_diffusion_h3_image_trainer.py::test_backend_runs_only_the_load_time_lora_set_for_h3_image`
also fails on main: #18420 reworded the reload error to the shared
`LORA_RELOAD_REMEDY` ("Reload the model with the LoRA selection: ..."),
but the test still matched the old "Load the model again". The behaviour
it checks is unchanged (a different LoRA set or weight on an H3-Image
load is refused), so the test now matches the current message. The file
goes from 1 failed, 20 passed on main to 21 passed here.
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