Skip to content

Studio: latency-gated dispatch for the fused INT8 GEMM (Triton 3.8 regression on A100 / L4) - #13772

Closed
danielhanchen wants to merge 28 commits into
mainfrom
studio-diffusion-int8-gemm-latency-gate
Closed

danielhanchen wants to merge 28 commits into
mainfrom
studio-diffusion-int8-gemm-latency-gate

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Problem

On torch 2.14 (Triton 3.8), Studio's fused INT8 GEMM gets much slower on A100 and L4. cuBLAS _int_mm on the same shapes is unaffected. Per-tile results are still bit-exact; only speed changed.

Real DiT shapes at 1024 px, each torch version in its own venv on the same VM (median TOPS over 15 shapes):

GPU Triton 3.6 (torch 2.11) Triton 3.7.1 (torch 2.12.1) Triton 3.8 (torch 2.14.1) cuBLAS _int_mm
A100 (sm80), shipped tile 128x128x128 / 4 warps 428 425 58.5 380-434 on all three
L4 (sm89), shipped tile 256x128x128 / 8 warps ~95 ~95 25 ~100
RTX PRO 6000 (sm120) 596-782 582-773 554-770 no regression

The cause is register pressure on Triton 3.8. Any tile that keeps 128 int32 accumulators per thread hits the 255-register cap and spills 138-808 registers to local memory. The shipped A100 tile spills 150-174. The tiles with 64 accumulators per thread spill nothing.

Triton 3.8.0 is still the newest release (torch 2.14.1 pins triton~=3.8.0), so the regression is still there today. Studio's new installs land on torch 2.11 (Triton 3.6) or, on the cu130 / Python 3.13 route, torch 2.13 (Triton 3.7.1). An existing torch 2.14 install is kept as is, and so are environments that bring their own torch 2.14.

Change

diffusion_int8_gemm.py now has a latency gate. The static tile table is no longer trusted blindly across toolchains.

  • Timing. At install, each swapped Linear shape is timed once on the device, in four row buckets: under 384 rows (text streams, timed at 256), under 1024 (timed at 512), under 2048 (timed at 1024) and the rest (timed at 4096). Each timed round is queued behind a short GPU spin, so it measures kernel time and not Python launch cost. There are two interleaved passes and the faster reading is kept.
  • Choice. First the static tile is timed against cuBLAS _int_mm plus one int32 to bf16 pass. The static tile stays unless cuBLAS beats it by 10%. Only when it has regressed are the replacement tiles timed: the per-shape tile and a few extra tiles per arch. A replacement tile has to beat cuBLAS by 10%, otherwise the bucket goes to cuBLAS. A healthy static tile is never swapped for another tile that only wins the short timing: on L4 with Triton 3.7.1 those swaps cost up to 12% per step.
  • cuBLAS shapes. A plain Linear whose shape is assigned to cuBLAS keeps the stock torchao forward. A ConvRot Linear in a cuBLAS slot still runs the fused rotation + act quant kernel (rotq_i8), then cuBLAS _int_mm with torchao's epilogue op for op, which Inductor fuses.
  • Qwen-Image-2.1 ConvRot groups. Qwen-Image-2.1 has its own stock-path fusions for rotated Linears: one rotation + act quant per shared QKV input, the fused SwiGLU region, and the probed act quant on to_out. They used to be gated on the arch alone, so on A100 / L4 / RTX PRO 6000 they never ran, even when the gate sent every shape to cuBLAS. In that case the swap now leaves those groups alone (diffusion_qwenimage21_fused.stock_claims) and the fusions take them, which is exactly what the kill switch runs. With a layer split, each group is judged on its own card's plan. Other models and any group with at least one fused bucket keep the swap.
  • Log. The swap log now reports how many Linears can use the fused GEMM and how many stay on cuBLAS, instead of always saying "N int8 Linear(s) run the fused-dequant GEMM".
  • Correctness. Any newly chosen tile must pass the existing bit-exact probe. A tile that fails to launch falls back to the probed default without dropping the cuBLAS choices.
  • Caching. Timings are saved as JSON under the Studio cache, keyed by GPU, capability, torch, Triton, CUDA, the driver and a hash of the kernel source. The first load on a new toolchain spends the timing once (2-3 s on RTX PRO 6000, 6-9 s on A100, 12-19 s on L4 for FLUX.1 / Qwen-Image shapes); later loads only read the file. The compile cache keys on the gate's cuBLAS slots.
  • Archs. The gate only runs where the fused GEMM is on: sm80, sm89 and sm120. Hopper (sm90), B200 / B300 (sm100 / sm103) and sm121 keep the stock path exactly as before.
  • Switches.
    • UNSLOTH_DIFFUSION_INT8_GEMM_AUTOTUNE=0 restores the previous static table.
    • UNSLOTH_DIFFUSION_INT8_GEMM_TUNE_DIR moves the cache.
    • The existing kill switch UNSLOTH_DIFFUSION_INT8_GEMM=0 still works.

This branch also merges current main. The merge needed one follow-up: main split the swap into a per-card _swap_on, so the gate's "an explicit device does not yield groups" check now gets its own argument.

Results

INT8 GEMM time per denoising step, real FLUX.1 and Qwen-Image linears

Colab, 1024 px, torch==2.14.1+cu130 (Triton 3.8.0) and torch==2.13.0+cu130 (Triton 3.7.1). The shapes are the int8 Linears of one step: FLUX.1-dev with 19 double blocks (4096 image + 512 text rows) and 38 single blocks (4608 rows); Qwen-Image with 60 blocks (4096 image + 256 text rows). Each GEMM is replayed from a CUDA graph, like the default compiled + CUDA graph speed mode. All arms run in one process and are interleaved per shape. Values are ms per step: the median of 3 processes, each of which times its own plan from an empty cache.

  • main is the static table, timed twice in each process. The A/A column is the largest main-vs-main gap.
  • v1 is this PR's previous head (e02fd69).
  • this PR is the current head.
  • kill switch is UNSLOTH_DIFFUSION_INT8_GEMM=0, which is cuBLAS plus the compiled torchao epilogue.
GPU Triton Model main v1 this PR kill switch this PR / main A/A
A100-SXM4-40GB 3.8.0 FLUX.1 1103.4 172.9 173.9 176.7 0.158 1.1%
A100-SXM4-40GB 3.8.0 Qwen-Image 1150.0 177.2 177.3 179.4 0.154 0.8%
A100-SXM4-40GB 3.7.1 FLUX.1 137.1 137.1 137.0 178.9 1.000 0.1%
A100-SXM4-40GB 3.7.1 Qwen-Image 152.4 149.7 146.1 182.1 0.959 0.0%
L4 3.8.0 FLUX.1 2395.6 643.9 703.1 710.5 0.294 7.3%
L4 3.8.0 Qwen-Image 2445.4 633.6 707.1 698.9 0.289 7.1%
L4 3.7.1 FLUX.1 569.8 597.4 582.8 675.7 1.023 4.7%
L4 3.7.1 Qwen-Image 563.0 571.0 558.8 657.0 0.993 6.0%
RTX PRO 6000 3.8.0 FLUX.1 93.6 88.2 88.5 102.1 0.945 0.0%
RTX PRO 6000 3.8.0 Qwen-Image 106.9 98.5 95.4 109.1 0.892 0.1%
RTX PRO 6000 3.7.1 FLUX.1 93.1 87.8 88.0 102.5 0.945 0.0%
RTX PRO 6000 3.7.1 Qwen-Image 106.0 97.7 94.6 109.2 0.893 0.1%
  • Triton 3.8. On A100 the GEMM time per step drops 6.3x (FLUX.1) and 6.5x (Qwen-Image), to within 2% of the kill switch. On L4 it drops 3.4x and 3.5x, level with the kill switch. That L4 VM was noisy (A/A 7%), so the L4 gap between v1 and this PR is not resolved.
  • Triton 3.7.1, where the static table is healthy. On A100 FLUX.1, this PR keeps the static table and is level with main. On Qwen-Image it is 4% faster, because the 256-row text stream goes to cuBLAS. L4 is within that VM's A/A noise. On an earlier, quiet L4 VM (A/A 0.1-0.2%, separate processes per arm), v1 was 12% slower than main on FLUX.1 and Qwen-Image at Triton 3.7.1, because it swapped in alternative tiles that only won the short timing. That is why a healthy static tile now stays. On the current revision the L4 swaps at Triton 3.7.1 are cuBLAS or a replacement for a static tile that lost to cuBLAS.
  • RTX PRO 6000. This PR is 5.5% faster than main on FLUX.1 and 11% faster on Qwen-Image, on both Triton versions.

End to end (measured on the earlier revision of this PR)

Colab, torch 2.14.1+cu130, transformer_quant=int8, default speed mode (compiled + CUDA graphs), 1024 px. Each table row ran on one VM in one job, with two repeats per arm. Values are median seconds per step (repeat 1 / repeat 2).

GPU Model main kill switch this PR (v1)
A100-SXM4-40GB (sm80) FLUX.2-klein-4B, 4 steps 0.5410 / 0.5420 0.1304 / 0.1306 0.1305 / 0.1304
A100-SXM4-40GB (sm80) Z-Image-Turbo, 9 steps 0.5922 / 0.5919 0.3036 / 0.3033 0.3008 / 0.3008
A100-SXM4-40GB (sm80) Qwen-Image-2.1 (ConvRot), 20 steps 1.0574 / 1.0571 0.2519 / 0.2520 0.2516 / 0.2519
L4 (sm89) FLUX.2-klein-4B, 4 steps 1.1395 / 1.1776 0.5038 / 0.5047 0.5022 / 0.5077
L4 (sm89) Z-Image-Turbo, 9 steps 2.7465 / 2.7386 1.2274 / 1.2282 1.1323 / 1.1345
RTX PRO 6000 (sm120) FLUX.2-klein-4B, 4 steps 0.0717 / 0.0712 0.0788 / 0.0788 0.0694 / 0.0692
RTX PRO 6000 (sm120) Z-Image-Turbo, 9 steps 0.1706 / 0.1707 0.1752 / 0.1753 0.1706 / 0.1708
  • Images. On A100 klein and Z-Image the PR is pixel-identical to the kill switch on all 8 images. LPIPS against main is 0.05 (klein) and 0.09 (Z-Image), in line with main's own run-to-run difference (0.04 on klein). On Qwen-Image-2.1 it is pixel-identical to the kill switch (3/3), with LPIPS 0.006 against main.
  • B200 (sm100). The fused GEMM does not engage there on main or on this PR, so the gate never runs. Steps take 0.0243 s on main, on this PR and with the kill switch.

Tests

  • New gate tests in tests/test_diffusion_int8_gemm.py:
    • the decide rule: a healthy static tile stays, and a regressed one goes to cuBLAS or to a tile clearly faster than cuBLAS;
    • replacement tiles are timed only after the static tile regressed;
    • the M buckets, including the text-stream bucket;
    • the timing measures GPU time and not launch overhead (real GPU);
    • plan save, load, key mismatch and concurrent saves;
    • fallback of a failed tile;
    • the autotune switch;
    • the Qwen-Image-2.1 yield: an all-cuBLAS plan leaves the QKV / SwiGLU ConvRot groups unswapped and lets their fusions take them. A fused plan, the UNSLOTH_DIFFUSION_Q21_CONVROT_FUSED=0 kill switch, other models, an explicit device and a group on another card all keep the swap;
    • the swap log counts fused vs cuBLAS Linears;
    • a real-GPU check that the swapped Linear is bit-exact against torchao, both compiled and eager.
  • tests/test_diffusion_stream_int8_gemm.py pins the static table, because engagement counts there would otherwise depend on timing.
  • On a B200 (torch 2.14.1, Triton 3.8.0, fused GEMM forced on), the int8 GEMM, streamed int8 GEMM, compile cache, int8 fused, Qwen-Image-2.1 fused, layer split and ConvRot runtime files give 465 passed, 1 failed, 3 skipped. The one failure, test_no_compiled_variant_spills_to_local_memory, also fails on main run alone on the same card and toolchain, so it predates this PR.
  • On CPU the same files give 313 passed, 155 skipped (the CUDA-only tests skip).

Not covered

  • End to end on the current revision. The end-to-end rows above are from the previous revision. The GEMM-per-step table is from the current one.
  • L4 at the few-percent level. The L4 VM for the current revision had 5-7% A/A noise, so on L4 only the Triton 3.8 speedup (3.4x) is resolved.
  • Hopper and Blackwell datacenter parts. The gate does not engage there by construction (no fused GEMM on sm90 / sm100 / sm103). Only B200 ran here.
  • Multi-card layer split with the gate. The per-card path is covered by unit tests only, since this host has no two-card test run for it.

Triton 3.8 (torch 2.14) slows the shipped sm80 / sm89 int8 tiles 4-7x
(A100 128x128x128 tile ~430 -> ~59 TOPS, L4 ~96 -> ~25 TOPS on DiT shapes)
while cuBLAS _int_mm is unchanged. Time the probed tile, the per-shape tile
and a few arch candidates against cuBLAS once per (N, K, M bucket) at
install, keep the fastest (fused wins ties), persist per toolchain, and keep
the stock torchao forward where cuBLAS wins. Alternative tiles still pass
the bit-exact probe. UNSLOTH_DIFFUSION_INT8_GEMM_AUTOTUNE=0 restores the
static table.
…gs its plan

The cuBLAS arm already pays a full int32 -> bf16 pass, more than the stock
path moves once Inductor fuses the epilogue, so a 0.92 margin left A100 /
Triton 3.8 FLUX.2-klein on fused tiles 1.7% slower than the kill switch.
… 10%

A new tile must also beat cuBLAS by 10%: on A100 / Triton 3.8 the near-tie
buckets that went to a new fused tile cost 1.7% (FLUX.2-klein) and 3%
(Z-Image) s/step against all cuBLAS, and a tile measured end to end on its
toolchain should not leave on a timing tie.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 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-10-22T14:54:47.287676Z e852b51 Manual request
🔒 Security Review ⚠️ Failed 2026-10-14T21:40:42.505641Z
We couldn’t complete this review. Comment @codex security review to try again.
5432383 PR opened
ℹ️ 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[bot]

This comment was marked as resolved.

…shape-tile threshold, 10% alt margin, private RNG

UNSLOTH_DIFFUSION_INT8_GEMM_TILES=0 now times only the default tile vs cuBLAS, cached under its own key.
M buckets gain a boundary at _SHAPE_MIN_M so small M is timed against the tile it actually runs (tune version 2).
An alternative tile must beat the static tile by _CUBLAS_MARGIN, not 3%.
_measure fills operands from a private CUDA generator instead of the default one.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: faf017a7df

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

Comment on lines +1054 to +1055
xs = torch.full((m,), 1e-3, device = dev, dtype = torch.bfloat16)
ws = torch.full((n,), 1e-3, device = dev, dtype = torch.bfloat16)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Benchmark the runtime scale-dtype specialization

For v2 weights on torchao versions that produce fp32 activation scales, or for prequant checkpoints with fp32 weight scales, runtime _launch specializes XS_FP32 and/or WS_FP32 to true, but tuning always supplies bf16 scales here. Because the plan is keyed only by (N, K, bucket), these modules inherit a tile/cuBLAS choice measured against a different Triton specialization and epilogue, so the latency gate can select a slower path for the supported fp32-scale models. Include the scale-dtype variant in the tuning inputs and plan key, or benchmark every variant that will consume the plan.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Leaving this one as is. The scale dtype only changes the epilogue: timing bf16 and fp32 scale variants on four DiT shapes, every tile shifted by a similar amount and the ranking only changed inside 2 to 6% near-ties, which the 10% margin already absorbs. The cuBLAS arm is timed the same way for every variant, which leans toward cuBLAS rather than a slower fused path. Keying the plan on scale dtype would need the activation scale dtype resolved before fused_for, so I would rather follow up if a real model regresses.

danielhanchen and others added 2 commits October 14, 2026 15:34
…ans when autotune is off, re-decide after a failed probe

UNSLOTH_DIFFUSION_INT8_GEMM_TILES joins _GRAPH_SWITCHES since it can flip a shape between the fused op and cuBLAS.
install() with AUTOTUNE=0 drops the device's earlier plan so the static table applies.
An alternative tile that fails the bit-exact probe is removed and the bucket re-decided among the remaining arms.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…the tune key, merge concurrent tune saves

model_fingerprint adds the gate's cuBLAS slots for the transformer's shapes (in-memory plan, else the stored file; no timing), since those trace the stock graph instead of the fused op. Fused-only plans keep the old key.
toolchain_key includes the NVIDIA driver version (NVML, else the CUDA driver API version).
_save_plan merges slots and timings another process stored since our load before replacing the file.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 2 commits October 14, 2026 15:55
…ning, lock tune-file saves

begin() records how to recompute the gate's cuBLAS identity (weakref to the transformer, CUDA index); the save re-keys the context when tuning after begin() changed it, so the next load hits.
_save_plan holds a filelock around the reload, merge and replace so concurrent processes keep each other's slots.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…gistration consistent with restore

save_async and _write_bundle re-key before the saved short-circuit and mark a re-keyed context dirty, so a bundle hit whose first tune picked cuBLAS is saved under the new key.
restore() marks the context released under _live_lock (now an RLock); a re-key after that no longer registers the new dir, which nothing would unregister.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…drop the re-key path

The key stays the stored-plan identity read at begin(). When a first-time latency-gate tune after begin() changes the cuBLAS slots, that load's bundle is not saved, so nothing lands under a stale key; the next load keys on the stored plan, compiles once and saves. Removes the save-time re-key, the released flag and the RLock.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

… stop re-saving a stale-plan context

With UNSLOTH_DIFFUSION_INT8_GEMM_TILES=0 tune() uses the default tile as the status quo even when per-shape tiles were probed earlier in the process.
A context whose save was rejected for a changed int8 plan is marked int8_stale, so save_async and save return early instead of re-queueing and re-checking after every render.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 8e85f74931

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

pre-commit-ci Bot and others added 3 commits October 14, 2026 23:44
…s to their stock-path fusion, log what runs

Where the gate sends every bucket of a shape to cuBLAS, the swapped forward still ran rotq_i8 + cuBLAS per Linear,
and because the arch gate alone decided the Qwen-Image-2.1 QKV / SwiGLU shared-rotation fusions, those stayed off.
The kill switch got them, so it was faster (A100, 0.2504 vs 0.2557 s/step). Those groups now stay unswapped when the
gate is all cuBLAS for their shapes, and the swap log counts fused vs cuBLAS Linears.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…2.1 fusions after a layer split

The in-memory plan now remembers the tune key (toolchain + tiles mode) it was filled under. A process that switches
UNSLOTH_DIFFUSION_INT8_GEMM_TILES between loads no longer keys the compile cache on, or reuses untimed, the other
mode's plan.

A layer split can place a Qwen-Image-2.1 group that was yielded to the stock-path QKV / SwiGLU fusion on an
all-cuBLAS card onto a card whose plan keeps a fused bucket. after_layer_split now gives such groups their Linears
back before the per-card swap, so that card runs its fused GEMM.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@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: e852b51593

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
…very shipped tile spills (unslothai#17036)

## Problem

On Triton 3.8 (torch 2.14), Studio's fused-dequant int8 GEMM runs 4-8x
slower on A100 (sm80) and RTX 40 / L4 (sm89) than on Triton 3.7.1, and
the tile unlisted sm8x cards (RTX 30 / A10 / A6000, sm86; Orin, sm87)
would use spills the same way. The output is still bit-exact; only speed
changed. Studio's cu130 route allows torch up to `<2.15`
(`_CU130_TORCH_CEILING` in `install.sh`), so these users can be on
Triton 3.8 today. On the same card, the kernel then loses to cuBLAS
`_int_mm` by a wide margin, so int8 image generation gets much slower
than the kill switch (`UNSLOTH_DIFFUSION_INT8_GEMM=0`).

Cause: since 3.8.0, Triton's sm8x prefetch pass keeps the next tile's
8-bit operands live across the loop one byte per register
(triton-lang/triton#12486, still open, and Triton 3.9.0 behaves the
same). Any int8 tile holding 128 int32 accumulators per thread (`BLOCK_M
* BLOCK_N / (32 * num_warps)`) then hits the 255-register cap and
spills. Offline compile for sm_89 / sm_80, ptxas `-v`:

| tile (BM, BN, BK, GROUP_M, warps, stages) | acc / thread | Triton
3.7.1 regs / spill bytes | Triton 3.8.0 | Triton 3.9.0 |
|---|---|---|---|---|
| (256, 128, 128, 8, 8, 3) shipped sm89 | 128 | 252 / 0 | 255 / 700 |
255 / 816 |
| (128, 128, 128, 8, 4, 3) shipped sm80 | 128 | 253 / 0 | 255 / 764 |
255 / 764 |
| (128, 128, 64, 8, 4, 4) | 128 | 219 / 0 | 255 / 644 | 255 / 644 |
| (256, 64, 128, 8, 8, 3) | 64 | 170 / 0 | 254 / 0 | 254 / 0 |

The 3.8 PTX spills under both the 12.8 and 12.9 ptxas, so it is Triton's
codegen, not ptxas. A plain int8 `tl.dot` loop with no Studio epilogue
spills the same way.

The upstream report pins it to the sm8x prefetch pass, and the other
archs Studio runs this kernel on do not spill. `_FALLBACK_CONFIG` (128,
128, 64, 8, 4, 4), offline compile, ptxas `-v`:

| arch | Triton 3.7.1 regs / spill bytes | Triton 3.8.0 |
|---|---|---|
| sm86 / sm87 (unlisted) | 219 / 0 | 255 / 644 |
| sm90 (unlisted) | 203 / 0 | 193 / 0 |
| sm120 (also its default and TMA tile) | 220 / 0 | 217 / 0 |
| sm121 (unlisted) | 220 / 0 | 217 / 0 |

sm120's deep-K tile (256, 128, 128, 8, 8, 3) is spill-free on both
versions too (224 registers).

## Change

On Triton 3.8 or newer, every sm8x card stays on the stock torchao +
cuBLAS path in auto mode: `arch_config` returns None for sm80 and sm89,
and `unlisted_wanted` returns False for the unlisted sm86 / sm87, so
they skip the probe and the load-time latency check (which would have
sent them to cuBLAS anyway, after timing a spilling kernel). Nothing
else moves:

- Triton 3.7.1 and older: same tiles as before on every arch.
- sm120 (RTX PRO 6000 / RTX 50): unchanged, including its TMA tile and
deep-K shape rule. Those tiles hold 128 accumulators per thread but
compile spill-free on Triton 3.8 (table above), and unslothai#13772 measured RTX
PRO 6000 on Triton 3.8 with no slowdown. I did not re-time sm120 for
this PR.
- Unlisted sm90 / sm121: unchanged (no spill), still behind their probe
+ latency check.
- `UNSLOTH_DIFFUSION_INT8_GEMM=1` or
`UNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=1` (force) still runs the kernel,
as before.
- The listed archs' shared-memory fallback to `_FALLBACK_CONFIG` cannot
reach a spilling tile any more: on Triton 3.8 sm80 / sm89 never probe,
and sm120 does not spill.

I first tried picking spill-free 64-accumulator tiles for Triton 3.8.
The best one recovers most of the loss, but it still trails cuBLAS
`_int_mm` on both archs (table below), and end to end it is slower than
the stock path. So stock is the faster and simpler choice there.

The version check is host-side, cached once (`_triton_at_least`), and
runs before any trace: no new kernel specialisation, no recompiles, no
graph breaks.

## Results

Colab, one VM per GPU, two venvs on the same VM: torch 2.13.0+cu130 /
Triton 3.7.1 and torch 2.14.1+cu130 / Triton 3.8.0 (Triton 3.9.0 swapped
into the 2.14 venv for the sweep only). Real DiT shapes (FLUX.2-klein,
Z-Image, Wan 2.1 linears) at M = 4096 / 8192 / 16384, 24 shapes in
total, three interleaved rounds. Geometric-mean TOPS, int8 in, bf16 out
with the full epilogue for the Triton arms. `_int_mm` is cuBLAS alone
(no epilogue, so a lower bound on the stock path's GEMM time). The A/A
spread over rounds was 0.1-0.8% (A100) and 1.7-5.4% (L4) median per arm.

| GPU | Triton | shipped tile | best spill-free tile | cuBLAS `_int_mm`
|
|---|---|---|---|---|
| A100-SXM4-40GB | 3.7.1 | 436.3 | 391.9 | 395.9 |
| A100-SXM4-40GB | 3.8.0 | 56.4 | 284.0 (64, 128, 128, 8, 4, 4) | 402.3
|
| A100-SXM4-40GB | 3.9.0 | 56.3 | 283.5 (64, 128, 128, 8, 4, 4) | 402.1
|
| L4 | 3.7.1 | 116.1 | 107.9 | 110.0 |
| L4 | 3.8.0 | 29.6 | 88.4 (256, 64, 128, 8, 8, 3) | 109.8 |
| L4 | 3.9.0 | 28.1 | 89.0 (256, 64, 128, 8, 8, 3) | 109.0 |

Every one of the 13 tiles swept is bit-identical to the torchao eager
epilogue on all 24 shapes and passes Studio's own probe (bias, fp32
scales, ragged M / N / K), on every Triton version, except one tile that
needs 120 KB of shared memory and does not launch on L4.

### Whole image

Z-Image-Turbo, 1024x1024, 8 steps, torchao int8 dynamic quant with
Studio's Linear filter (239 Linears), Studio's fused GEMM installed,
repeated blocks compiled. Median seconds per step over 3 rounds; each
row is one process, arms inside a process interleaved.

| GPU | Triton | main | this PR | kill switch | best spill-free tile |
this PR / main |
|---|---|---|---|---|---|---|
| A100-SXM4-40GB | 3.7.1 | 0.1998 | 0.1992 | 0.2143 | 0.2201 | 0.997 |
| A100-SXM4-40GB | 3.8.0 | 0.9595 | 0.2147 | 0.2153 | 0.2515 | 0.224 |
| L4 | 3.7.1 | 0.7351 | 0.7408 | 0.8010 | 0.8106 | 1.008 |
| L4 | 3.8.0 | 1.9574 | 0.7989 | 0.8196 | 0.8762 | 0.408 |

Round-to-round spread per arm was at most 0.5% on A100 and 3.2% on L4
(L4 is power capped). On Triton 3.8 this PR is 4.5x faster per step than
main on A100 and 2.4x faster on L4, level with the kill switch. On
Triton 3.7.1 main and this PR run the same tile; the gap is inside the
noise.

- **main** is the current table, **this PR** is this branch's own
selection (on Triton 3.8 it installs nothing on these archs, so it runs
the stock path), **kill switch** is `UNSLOTH_DIFFUSION_INT8_GEMM=0`,
**best spill-free tile** is the fastest tile from the sweep above forced
onto the same model.
- Inside one process the shipped tile and the best spill-free tile give
pixel-identical images, as the kernel-level check predicts.

## Overlap

- unslothai#13772 attacks the same regression with a per-shape latency-gated
dispatcher and a timing cache. This PR is the minimal static version of
its outcome on sm80 / sm89 (cuBLAS on Triton 3.8, the shipped tiles on
3.7.1) and can land first; unslothai#13772 can build on it or replace it.
- unslothai#17026 (merged) races `_FALLBACK_CONFIG` against (128, 128, 128, 8, 4,
3) and (256, 128, 128, 8, 8, 3) on unlisted archs, then keeps the
existing latency check against cuBLAS. All three tiles hold 128
accumulators per thread and spill on Triton 3.8 for sm8x, and neither a
64-accumulator tile nor `_int_mm` is a race candidate, so on Triton 3.8
this PR keeps sm86 / sm87 out of that path (`unlisted_wanted`) instead
of timing spilling kernels at load. `_unlisted_tile` and the race are
untouched, and sm90 / sm121 still race as before. This branch already
merges current main, including unslothai#17026.

## Not covered

- sm120 on Triton 3.8: not re-timed here (offline compile and unslothai#13772's
measurement only); its tiles are unchanged.
- sm86 / sm87 on a real card: not measured (no such Colab tier). The
decision rests on the identical spill (644 bytes, the same as sm80 /
sm89) and on A100 / L4, where no tile beat cuBLAS on Triton 3.8.

## Tests

- `test_triton38_keeps_every_sm8x_stock`: on Triton 3.8+ sm80 / sm89
return None in auto mode and keep their tile under force; unlisted sm86
/ sm87 are not wanted in auto and still are under force; sm90 / sm121 /
sm120 unchanged; the pre-3.8 table unchanged.
- `test_triton38_runtime_turns_sm80_sm89_stock`: the same through a
Triton 3.8 version string, without the new helper (fails on main).
- `test_triton_at_least_parses_the_installed_version`: 3.7.1, 3.8.0,
3.9.0+git, 4.0.
- `test_arch_gate_auto` and the unlisted-gate tests now pin the pre-3.8
behaviour, so they no longer depend on the Triton the test host has
installed.
- Every test file that imports the int8 GEMM (15 files): 1178 passed
with Triton 3.7.1 and with Triton 3.8.0 importable (CPU host; the GPU
kernel checks ran as the sweep above).
@danielhanchen

Copy link
Copy Markdown
Member Author

Closing as superseded by main: #17036 keeps sm8x cards on cuBLAS under Triton 3.8+ (the regression this targeted), the latency check for untuned archs landed in #15654 and #17026, and this PR's own Triton 3.8 numbers sit within 1.6% of what main now runs on A100 and inside the L4 A/A spread. The branch also conflicts with main in 4 of its 7 files.

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