Repository navigation
Studio: latency-gated dispatch for the fused INT8 GEMM (Triton 3.8 regression on A100 / L4) - #13772
danielhanchen wants to merge 28 commits into
Conversation
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.
…hout dropping cuBLAS decisions
… 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.
Codex Review SummaryThis 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. |
…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.
|
@codex review |
There was a problem hiding this comment.
💡 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".
| xs = torch.full((m,), 1e-3, device = dev, dtype = torch.bfloat16) | ||
| ws = torch.full((n,), 1e-3, device = dev, dtype = torch.bfloat16) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…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.
…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.
…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.
…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.
…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.
… 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.
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
…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.
…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.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
…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).
|
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. |
Problem
On torch 2.14 (Triton 3.8), Studio's fused INT8 GEMM gets much slower on A100 and L4. cuBLAS
_int_mmon 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):
_int_mmThe 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.pynow has a latency gate. The static tile table is no longer trusted blindly across toolchains._int_mmplus 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.rotq_i8), then cuBLAS_int_mmwith torchao's epilogue op for op, which Inductor fuses.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.UNSLOTH_DIFFUSION_INT8_GEMM_AUTOTUNE=0restores the previous static table.UNSLOTH_DIFFUSION_INT8_GEMM_TUNE_DIRmoves the cache.UNSLOTH_DIFFUSION_INT8_GEMM=0still works.This branch also merges current
main. The merge needed one follow-up:mainsplit 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) andtorch==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.UNSLOTH_DIFFUSION_INT8_GEMM=0, which is cuBLAS plus the compiled torchao epilogue.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).Tests
tests/test_diffusion_int8_gemm.py:deciderule: a healthy static tile stays, and a regressed one goes to cuBLAS or to a tile clearly faster than cuBLAS;UNSLOTH_DIFFUSION_Q21_CONVROT_FUSED=0kill switch, other models, an explicit device and a group on another card all keep the swap;tests/test_diffusion_stream_int8_gemm.pypins the static table, because engagement counts there would otherwise depend on timing.test_no_compiled_variant_spills_to_local_memory, also fails onmainrun alone on the same card and toolchain, so it predates this PR.Not covered