Skip to content

Studio H3 / H3-Image: SageAttention inside the strided processor, no per-call device sync on sm89 - #17639

Merged
danielhanchen merged 14 commits into
mainfrom
h3-strided-sage
Nov 13, 2026
Merged

danielhanchen merged 14 commits into
mainfrom
h3-strided-sage

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Stacked on #17589 (base branch studio-transformers-5-5-4, which loads the SageAttention hub build). Retarget to main once that merges.

MiniMax-H3 / H3-Image attention with SageAttention got slower, not faster, because of two problems. This PR fixes both. On an L4 the DiT's attention time per 2048x2048 image drops from 2.8 s (native flash) to 2.0 s with Sage, quantisers included. The output is bit-identical to the existing Sage route.

  1. Sage ran in the stock processor. Any Sage backend (sage / sage_hub) sent each H3 attention call back to the stock processor. That lost Studio's strided layout, the fused q/k RMSNorm + RoPE and the shared QKV act quant. The strided processor now does all of that itself. It then passes the NHD q / k / v it already holds to the same guarded dispatch the stock processor uses, so calls Sage cannot take still run native. H3 video also installs the strided processor when Sage is the engaged backend. Kill switch: UNSLOTH_H3_STRIDED_SAGE=0.
  2. The hub build synchronizes after every call on sm89. Its sm89 route calls torch.cuda.synchronize() after every attention launch. On the streamed tier that drained the weight prefetch 300 times per image and left the GPU idle for 2.2 s of a 14.3 s denoise. The loader now wraps the build's sageattn. On sm89 it runs the build's own quantisers and attention op with the same arguments, in the same order, without the sync.
    • The build's CUDA ops launch on the default stream whatever stream is current; that is what the sync was hiding. The wrapper therefore runs the whole call on the default stream and orders it against a caller on another stream with stream waits.
    • Other cards, causal or LSE calls, grouped heads and head dims other than 64 / 128 call the build as shipped. Kill switch: UNSLOTH_SAGE_SM89_NO_SYNC=0.

Results

Colab L4, streamed int8 tier, torch 2.13.0+cu130, 6 steps, seed-matched. Base is the same tree without these commits, with the per-card attention race from #17615 applied to both arms (on L4 it picks native flash on base and Sage with this PR). Each arm was rendered twice and the repeats were byte-identical. Denoise times are in seconds.

base base repeat this PR this PR repeat
2048 t2i, s/img 17.44 17.35 16.86 16.83
2048 t2i, denoise 14.06 14.02 13.52 13.51
2720x1536 edit, first, s/img 53.10 51.60 46.55 46.56
2720x1536 edit, repeat, s/img 42.40 42.18 37.33 37.34
2720x1536 edit, repeat, denoise 41.0 41.0 36.1 36.1
1024 t2i, s/img 8.39 8.38 8.40 8.41

A second L4 VM gave the same deltas: 2048 went from 16.63 / 16.72 to 16.22 / 16.19 s/img, and the edit repeat from 41.45 / 41.71 to 36.63 / 36.68 s/img. At 1024 Sage is a tie: 0.01 to 0.03 s slower.

The table below separates the two fixes. All three arms force Sage (UNSLOTH_H3_IMAGE_INT8_ATTENTION=1). The first two rows come from one L4 VM (base 13.45 s denoise there) and the last from another (base 13.33 s).

Sage route 2048 denoise s edit repeat denoise s images
stock processor, sync (before) 15.58 41.5 reference
strided processor, sync 14.32 36.8 byte-identical
strided processor, no sync (this PR) 12.94 35.5 byte-identical

L4 profile of one 2048 render (DiT window):

base (flash) this PR (Sage)
attention kernel, per call 9.21 ms 5.30 ms
Sage quantisers, per call n/a 1.40 ms
DiT window 14.13 s 13.62 s
idle inside the window 17 ms 21 ms (2237 ms with the sync, on the first VM)

On the edit the attention kernel drops from 50.6 to 28.8 ms per call, plus 3.6 ms of quantisers. The DiT window goes from 40.9 to 36.1 s.

On an A100 (sm80, resident), the race keeps native flash, as before: a block took 6.85 ms with Sage against 6.69 ms native. With Sage forced, 2048 is a tie (3.50 s denoise both) and the edit is 4% faster (10.7 to 10.2 s).

Accuracy

The two fixes do not change any value: every Sage render here is byte-identical to the same render through the existing Sage route. Whether a card uses the checkpoint's INT8 attention at all is a separate policy: on this branch it is used as the checkpoint declares; #17615 adds a per-card race that keeps it only where it times faster, and with this PR that race picks Sage on L4 (native on A100). For that decision, Sage against native:

  • Against the native render on the same VM: LPIPS 0.06 to 0.20 on the 2048 prompts and 0.016 / 0.048 on the edit. The A/A floor is 0: native repeats are byte-identical, and so are native flash against native cuDNN.
  • Against a bf16 DiT render (RTX PRO 6000, same seeds), mean over the same 8 renders:
render LPIPS vs bf16
int8 GEMM + native attention, L4 0.082
int8 GEMM + Sage, L4 0.089
int8 GEMM + native attention, A100 0.074
int8 GEMM + Sage, A100 0.097

Tests

  • tests/test_video_minimax_h3_attn.py adds CPU tests:
    • with a stand-in guarded backend, Sage runs inside the strided processor (the stock processor is never called) and matches the stock route bit for bit, fused and unfused;
    • the kill switch, the backends that still go to the stock processor, a masked call, the install routing table, and the H3 video gate.
  • tests/test_diffusion_sage_hub.py adds CPU tests for the sm89 wrapper: the ops and arguments it runs, every fallback, the kill switch, other cards, and builds without the sm89 parts.
  • On the L4 VM, the no-sync route is bit-identical to the build's sageattn at the 2048 and edit shapes, called from both the default stream and a side stream.

The main env pin moves from 5.5.0 to 5.5.4 (extras-no-deps, no-torch-runtime,
single-env constraints and the darwin override), and the Gemma 4 sidecar
(.venv_t5_550) moves with it. The highest pre-installed sidecar (.venv_t5_510)
moves from 5.10.2 to 5.13.1; its directory and tier key stay, so existing
installs rebuild it in place. 5.13.1 checks safetensors>=0.8.0 at import and
the sidecar installs --no-deps, so studio.txt now asks for safetensors>=0.8.0
on every platform (it was Windows only). The config-parse log reads the
sidecar versions from the tier table instead of hardcoding them.
… kernels package

kernels 0.12.3, the release transformers < 5.13 can import next to, reads the
hub's model-type copy of kernels-community/sage-attention, whose builds stop at
torch 2.12, so on torch 2.13 sage never engaged. The new loader fetches the one
build variant this torch needs from the kernel-type repo (the model-type copy
is the fallback), reads metadata.json first so an unsupported card downloads
nothing else, checks the build's CUDA archs and sha256 digests, and imports it
under a private module name. kernels.get_kernel stays as the fallback, and
transformers' own kernels import is untouched.

kernels_compat now pins the on-demand kernels install on the oldest
transformers that will import it: the main env or any sidecar, since the
sidecars install --no-deps over the same env.
…all on sm89

The hub build's sm89 route (INT8 Q.K, FP8 P.V) calls torch.cuda.synchronize() after every attention launch. That
drains every stream once per call, including a streamed DiT's weight prefetch, so the host stops running ahead of
the GPU. The loader now wraps the build's sageattn: on sm89 it runs the same quantisers and attention op with the
same arguments in the same order, without the sync, so the output is bit-identical.

The build's CUDA ops launch on the default stream whatever stream is current, which is what the sync papered over
for its own default-stream callers. The wrapper therefore runs the whole call on the default stream and orders it
against a caller on another stream with stream waits (and record_stream for the tensors that cross), so a side
stream caller gets the right answer without a host sync.

Other cards, causal or LSE calls, grouped heads and head dims other than 64 / 128 call the build as shipped.
UNSLOTH_SAGE_SM89_NO_SYNC=0 turns the wrapper off.
…processor

A Sage backend (the kernels-hub build or a pip SageAttention 2) used to send every H3 attention call back to the
stock processor, which drops Studio's strided layout, the fused q/k RMSNorm + RoPE and the shared QKV act quant. The
strided processor now runs those itself and hands the NHD q / k / v it already holds to the same guarded dispatch
the stock processor uses, so calls Sage cannot take still run native. Only the work around the kernel changes: the
kernel sees the same q / k / v as on the stock route.

H3 video installs the strided processor for an engaged Sage backend too. UNSLOTH_H3_STRIDED_SAGE=0 restores the
stock processor for Sage.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 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-13T12:51:29.961254Z fad79fe 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: b6db346b52

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

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: b6db346b52

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

…_torch

AMD SDK / Radeon wheels leave torch.version.hip unset and carry the ROCm tag
only in torch.__version__; route the hub loader's CUDA-build check through the
shared is_rocm_torch helper like every other diffusion gate, so such a wheel
never resolves a CUDA build variant. Adds tests for the tag-only and stock ROCm
wheels and a CUDA control.
… floor, main)

# Conflicts:
#	studio/backend/tests/test_diffusion_sage_hub.py
@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. 🎉

Reviewed commit: fad79fe68d

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

Base automatically changed from studio-transformers-5-5-4 to main November 13, 2026 13:08
# Conflicts:
#	studio/backend/core/inference/diffusion_sage_hub.py
#	studio/backend/tests/test_diffusion_sage_hub.py

[skip ci]
@danielhanchen
danielhanchen merged commit 04713f8 into main Nov 13, 2026
@danielhanchen
danielhanchen deleted the h3-strided-sage branch November 13, 2026 13:11
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