Skip to content

Studio diffusion kernels: keep per-resolution sizes out of the Triton compile key - #16582

Merged
danielhanchen merged 4 commits into
mainfrom
studio-diffusion-triton-resolution-args
Nov 4, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-diffusion-triton-resolution-args

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

The first render at a resolution whose token count or VAE width is not a multiple of 16 recompiled almost every Studio diffusion Triton kernel once (1328x1328 after 1024x1024: 7401 tokens, 166-wide VAE maps). That is about 6 s of cold compiles per process on an A100 before the image starts. Triton specialises plain integer arguments on % 16 and == 1, so those per-resolution sizes were part of the compile key.

This moves only the per-resolution sizes into do_not_specialize. Strides and channel counts stay specialised.

Change

  • diffusion_int8_gemm.py (i8mm_dq, i8mm_dq_tma): M.
  • diffusion_convrot_quant.py (rotq_i8, rotq_h16): M.
  • diffusion_flux2_rope.py, diffusion_moss_fused.py, diffusion_rocm_fused.py (AdaLN): S.
  • diffusion_qwenimage21_eager.py: n_seq. n_rows keeps its hint: as a runtime argument it was 2.5% slower at even token counts on A100. With only n_seq runtime the kernel compiles to the same binary as main at 1024x1024 and 1024x768 (sm75 to sm120), and it keeps one compile (about 0.3 s) at the first odd-token resolution.
  • diffusion_vae_fused.py: GroupNorm partials / combine / apply, row softmax and the DC-AE upsample add join the existing _SHAPE_ARGS table that four of its kernels already used.
  • video_minimax_h3_qknorm.py, video_minimax_h3_vae.py: row counts and spatial sizes.
  • Wan's fused kernels are left as they are: the same change measured 1.4-1.9% slower at 720p there.

Evidence

Kernels driven at the shapes the pipelines run, base vs head, Colab A100 and G4 (RTX PRO 6000), 3 fresh processes per arm, interleaved, do_bench median, A/A spread first. A 48 GiB allocator segment is reserved before any compile: without it the base arm, which compiles more variants, gets a different memory layout and byte-identical binaries read up to 6% apart.

kernel and shape result
first call at a new non-multiple-of-16 resolution (1328x1328 after 1024x1024) about 6 s of recompiles on A100 -> 0 (int8 GEMM 1.33 s -> 0.003 s, ConvRot 1.15 s -> 0.001 s, each VAE GroupNorm 0.44-0.66 s -> 0, FLUX.2 RoPE 0.19 s -> 0, H3 norm 0.48 s -> 0)
int8 GEMM, ConvRot, VAE attention @ 1328x1328 same binary as base (no hint applies at M 7401): no steady-state change
int8 GEMM @ 1024x1024, ConvRot, VAE GroupNorm + SiLU within A/A
AdaLN (ROCm fused), MOSS, H3 VAE q/k-norm identical binaries on sm75 / 80 / 89 / 90 / 100 / 120, gfx1151 and gfx942
MiniMax-H3 _qk_norm_rope_kernel A100 -6.6% / -7.8%, G4 within A/A
Qwen-Image-2.1 _norm_rope_kernel same binary as main at 1024x1024 / 1024x768
H3 VAE _add_residual, VAE _dcae_up_add within A/A
  • Outputs are bit-identical base vs head, except the VAE GroupNorm at multiple-of-16 sizes, where a few elements move by one ulp. Against an fp64 reference over 18 cases its mean and max error match base and are at or below stock torch.group_norm.

Tests

  • New studio/backend/tests/test_diffusion_triton_resolution_args.py (static, CPU): every per-resolution size of these kernels is listed in do_not_specialize. 9 fail on main, 9 pass here.
  • Related diffusion kernel suites pass, apart from one test that fails identically on main in a venv without the pinned huggingface_hub.

Not covered

  • Timed on A100 and G4 (sm120) only; no T4 / L4 / H100 / ROCm timing (kernels with identical binaries cannot change there). No full-pipeline render or LPIPS, the kernels were driven directly at pipeline shapes.
  • NVFP4 act-quant needs sm100+ and was not run.

… compile key

Token counts (int8 GEMM / ConvRot M, RoPE / AdaLN / MOSS S, Qwen-Image rows) and VAE / H3 spatial sizes (HW, W, n_chunks,
frames) were plain ints, so their % 16 specialisation recompiled every kernel the first time a resolution whose
sizes are not multiples of 16 ran (1328x1328 after 1024x1024: about 6 s of cold compiles on A100). They are now in
do_not_specialize (the VAE kernels through the existing _SHAPE_ARGS table); strides and channel counts stay
specialised. Steady-state time unchanged or faster (int8 GEMM -7% at 1328x1328), GroupNorm error vs fp64 unchanged.
Wan's L is left specialised: dns L cost 1.4-1.9% at 720p.
@danielhanchen danielhanchen self-assigned this Nov 3, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T04:51:37.687672Z bf83e9e 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. Swish!

Reviewed commit: 3e81659e08

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

…on-resolution-args

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

[skip ci]
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 4, 2026
…nslothai#16585)

## Summary

Comment-only. Every `tl.constexpr` parameter line in the Unsloth Triton
kernels and the Studio diffusion / video kernels now carries a short
trailing comment naming what it is (`# hd`, `# block size along K`, `#
int8 clamp max`, `# fuse swiglu before rotation`), so a reviewer can
tell at a glance whether a constexpr is a true constant or a per-call
size that would recompile.

- 482 lines across 29 files (`unsloth/kernels/**`,
`unsloth/optimizers/flash_adamw.py`, `studio/backend/core/inference/**`
kernels).
- Not touched: `unsloth/kernels/nf4*.py`, the fast LoRA kernels and the
fast-inference decode path, which are being reworked separately.
- No code changes: `comment_tools.py check --changed
--allow-docstring-edits --strict-ext` passes 29 / 29 files
(AST-compared, comments and whitespace only).

The functional recompile fixes found by the same audit are separate PRs:
unslothai#16582 (Studio diffusion kernels, per-resolution sizes) and unslothai#16584
(layernorm dW, fp8 block matmul, nvfp4 split sum).
Runtime n_rows was 2.5% slower at even token counts on A100; n_seq stays
runtime, so the first odd-token resolution still skips most recompiles.
@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: bf83e9eeea

ℹ️ 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 525e4ab into main Nov 4, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-triton-resolution-args branch November 4, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant