Repository navigation
Studio H3-Image: reuse the reference VAE posterior, trim after load, keep INT8 attention only where faster - #17615
Conversation
…ders Every edit render ran each reference picture through the VAE encoder again, about 1.1 s for a 2720x1536 picture on an RTX PRO 6000, although only the posterior sample depends on the seed. The pipeline now keeps the posterior per picture content (blake2b of the pixels, the VAE, its dtype and device; 8 entries) and draws each render's sample from it with that render's generator, so the latents are bit-identical to encoding again. release() drops the cache. UNSLOTH_H3_IMAGE_REF_CACHE=0 encodes every time.
…first render The staged upload, the QKV fusion and the GEMM / attention probes left their freed blocks reserved: on an RTX PRO 6000 the int8 resident load held 58.5 GB reserved over 51.4 GB allocated, so the card showed about 7 GB more in use than the model needs. The loader now runs gc and empty_cache once at the end on CUDA. UNSLOTH_H3_IMAGE_TRIM_AFTER_LOAD=0 keeps the blocks reserved.
…imes faster With the SageAttention kernels-hub build loading again, the INT8 DiT routes its blocks to it as the checkpoint declares. On an A100 80GB that made the denoiser slower: 4.1 s against 3.6 s per 2048x2048 image with the native flash path, which also keeps Studio's strided layout and fused q/k norm + RoPE (the sage route falls back to the stock processor). After the probe, the loader now times one resident DiT block's attention module with each backend on a 2048x2048-sized packed sequence and keeps INT8 attention only when it is more than 2% faster. No resident block or a failed timing keeps the INT8 route as before. UNSLOTH_H3_IMAGE_INT8_ATTENTION=1 forces it without the race, =0 keeps native attention.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…per-call device sync on sm89 (#17639) ## 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.
Summary
Three independent MiniMax-H3-Image loader / pipeline changes found while profiling 2048 px renders on an RTX PRO 6000, an A100 80GB and an L4. Each has its own kill switch, and none changes output.
UNSLOTH_H3_IMAGE_REF_CACHE=0turns it off). Every edit render ran each reference picture through the VAE encoder again, although only the posterior sample depends on the seed. The pipeline now keeps the posterior per picture content (blake2b of the pixels, plus the VAE, its dtype and device; 8 entries) and draws each render's sample from it with that render's generator.release()drops the cache.UNSLOTH_H3_IMAGE_TRIM_AFTER_LOAD=0). The staged upload, QKV fusion and GEMM / attention probes left their freed blocks reserved: on an RTX PRO 6000 the INT8 resident load held 58.5 GB reserved over 51.4 GB allocated. The loader now runsgcandempty_cacheonce at the end of a CUDA load.UNSLOTH_H3_IMAGE_INT8_ATTENTION=1forces it,=0keeps native attention). When the SageAttention build loads, the INT8 DiT routes its blocks to it as the checkpoint declares. On an A100 80GB that was slower than the native flash path, which also keeps Studio's strided layout and fused q/k norm + RoPE. After the existing probe, the loader times one resident DiT block's attention with each backend on a 2048x2048-sized packed sequence and keeps INT8 attention only when it is more than 2% faster. No resident block, or a failed timing, keeps the INT8 route as before.Results
Measured as per-commit deltas on one integration branch (these three commits plus other pending H3-Image work), steady-state 2048 px renders, 6 steps, seed-matched noise:
The attention race only matters where the SageAttention kernel loads. On a card where it is faster, the INT8 route is kept as before.
Tests
test_diffusion_minimax_h3_image.py: a repeated reference samples the cached posterior bit for bit, and INT8 attention stays off where it loses the race.test_diffusion_minimax_h3_image.py+test_diffusion_minimax_h3_image_speed.pyon CPU: 122 passed on this branch, 120 passed on main.Not covered