Repository navigation
Studio int8 GEMM: keep sm8x GPUs on cuBLAS under Triton 3.8+, where every shipped tile spills - #17036
Conversation
…very shipped tile spills
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. Another round soon, 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". |
|
@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". |
# Conflicts: # studio/backend/tests/test_diffusion_int8_gemm_unlisted.py
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
…aced per arch (FLUX.3 klein INT8 -19.8% on L4) (#17486) ## Summary Studio's INT8 ConvRot diffusion path runs its int8 GEMM on a Triton kernel (`i8mm_dq`) where the arch gate allows it, and on cuBLAS `_int_mm` plus a separate dequant everywhere else. That leaves three gaps: - Turing (T4) never gets a fused GEMM: Triton cannot lower the int8 dot on sm75, and the swap only takes bf16 input. - Every sm8x card falls back to cuBLAS on Triton 3.8 (torch 2.14), by design (#17036, triton-lang/triton#12486 register spills). Colab now ships torch 2.14.1 + Triton 3.8.0, so this is the default there today. - Where Triton does run, it trails a compiled CUDA GEMM: on A100 the Triton kernel is at 89-94% of a CUTLASS fused-epilogue GEMM, and on L4 the baseline's CUTLASS GEMM is the largest part of its per-image lead on FLUX.3 klein INT8. This PR adds a prebuilt (ahead-of-time) CUTLASS 2.x int8 GEMM with torchao's dequant epilogue fused into it, for sm75 (fp16 out) and sm80 / sm86 / sm89 (bf16 out). It works the same whatever Triton is installed, is checked bit for bit against today's reference on every card before use, and is raced per weight shape against today's path, so auto mode never picks it where it is slower. ## What it is - `studio/native_kernels/int8_gemm/`: one CUTLASS 2.x s8 x s8 -> s32 GEMM per tile, with an epilogue visitor that reproduces torchao's epilogue op for op: `c * x_scale` in the x_scale dtype (int32 -> fp32 -> bf16 rounds twice, as torch does; an fp16 x_scale computes in fp32, as torchao does), the cast, `* w_scale` (rounded to the output dtype only when the scale has it), `+ bias`, the final cast. Every product and sum uses `__fmul_rn` / `__fadd_rn`, so nothing contracts into an FMA. - sm80 family: 15 tiles, SASS for sm_80, sm_86 and sm_89, bf16 out, x_scale bf16 / fp32, w_scale bf16 / fp32, with and without bias. - sm75 family: 7 tiles, SASS for sm_75, fp16 out, x_scale fp16 / fp32, w_scale fp16 / bf16 / fp32, with and without bias. - The sm75 family also carries the epilogue of Studio's native W8A8 Linear (`diffusion_native_quant`), the module fp16-only cards load FLUX.3 INT8 into: `(c * x_scale) * w_scale` in fp32, then one cast to fp16. - No PTX: other archs never JIT it and keep their current path. - The host side is a plain C ABI (`ui8_abi_version`, `ui8_num_tiles`, `ui8_tile_info`, `ui8_has_kernel`, `ui8_gemm`), linked against the static CUDA runtime, libstdc++ and libgcc. The Linux library needs only glibc. There is no torch or Python ABI in it, so one binary per OS serves every torch minor. It is built with CUDA 12.8, so it also runs on CUDA 13 drivers. - `diffusion_int8_cutlass.py` loads it with ctypes and launches it on torch's current stream, so CUDA graph capture works. Absent, unloadable, wrong ABI, ROCm or CPU torch, or `UNSLOTH_DIFFUSION_INT8_CUTLASS=0`: today's path, unchanged. ## Numerics and the fail-closed probe Every tile is probed once per card for every scale-dtype pair it serves. The probe uses ragged M and N tails, K below and above one tile, with and without bias, M = 1, and int32 products one or two units off a 16-bit rounding midpoint (both signs). Output is compared with `torch.equal` against the generic torchao reference, which matches `diffusion_int8_gemm.reference()` bit for bit for bf16 (unit test). A tile that fails any case is never used. A failed launch at runtime drops the prebuilt kernel on that card and runs the same math on today's path. ## Selection When the int8 Linears are swapped, every distinct weight shape (N, K, scale dtypes, bias) is timed on random operands at M = 512 and M = 4096 against today's path: - the Triton kernel where Studio runs it today; - otherwise cuBLAS `_int_mm` plus one read of its int32 output. This is a lower bound on the stock cost, because the stock epilogue fuses into its consumer under torch.compile. A tile replaces today's path only when it is at least 2% faster. A Linear whose large-M race keeps today's path is not touched, so it keeps the Inductor-fused stock epilogue. Winners persist per card, torch, CUDA, Triton, library build and source hash. Kill switches: - `UNSLOTH_DIFFUSION_INT8_CUTLASS=0`: the whole lever, and the install step. - `=1`: skip the race and always take the fastest probed tile. On fp16-only cards, the native W8A8 Linear races its weight shapes on its first eager forward, before any CUDA graph capture. Its eager `_int_mm` path, the one CUDA graphs record, then runs the won tile in place of `_int_mm` plus the fp32 epilogue. Its own rotation and activation quant are untouched, and so is its compiled path. The fused ConvRot rotation and activation quant (#17230) stay in front of it unchanged. `rotq_h16` / `rotq_h16x` produce the same codes and scales, and only the GEMM behind them changes. On sm75, where neither the rotation kernel nor Triton runs, a won Linear runs the module's own rotation and torchao's own fp16 activation quant, then the fp16 kernel. ## Distribution - `.github/workflows/prebuilt-int8-gemm.yml`, dispatch only, in the same shape as `prebuilt-cuda-wheels.yml`. It builds `unsloth_studio_i8gemm-1.0.0-py3-none-manylinux_2_28_x86_64.whl` (in the manylinux_2_28 container, CUDA 12.8 from NVIDIA's rhel8 repo) and `...-py3-none-win_amd64.whl` (NVIDIA's Authenticode-checked installer, MSVC x64, static runtime). It smoke-loads each wheel the way Studio does on a GPU-less runner, signs with Sigstore under the `release-signing` environment, and uploads to `prebuilt-wheels-cu13` only when `publish: true`. - `install_python_stack._ensure_int8_gemm()` runs after flash-attn. It installs the wheel (about 3.3 MB, `--no-deps`) for an NVIDIA CUDA torch on x86_64 Linux and Windows when the release carries it. Otherwise it does nothing: no published asset, ROCm, macOS, no torch, or `UNSLOTH_STUDIO_SKIP_I8GEMM_INSTALL=1`. Failures only warn. - Windows: the same source builds with nvcc + MSVC; the baseline ships CUTLASS 2.x int8 kernels on Windows from the same headers. The DLL has no dependent DLLs (static CUDA runtime and MSVC runtime), and the loader looks for `unsloth_i8gemm.dll`. The Windows wheel build has not run yet (see below). ## Results Microbench, held (power-settled) CUDA-graph timings at the hot shapes. "Raced" is the per-step int8 GEMM time with the race applied, relative to today's path on that card. | card | stack | today's path | FLUX.3 klein | Qwen-Image-2.1 | MiniMax-H3 | |---|---|---|---|---|---| | A100-40GB | torch 2.14.1, Triton 3.7.1 | Triton `i8mm_dq` | 0.924 | 0.929 | 0.953 | | A100-40GB | torch 2.14.1, Triton 3.8.0 | cuBLAS | 0.723 | 0.763 | 0.771 | | L4 | torch 2.14.1, Triton 3.7.1 | Triton `i8mm_dq` | 0.959 | 0.986 | 1.000 (no shape won) | | L4 | torch 2.14.1, Triton 3.8.0 | cuBLAS | 0.808 | 0.832 | 0.959 | | T4 (Kaggle) | torch 2.11.0 | cuBLAS + epilogue | 0.956 vs `_int_mm` + one read; 0.588 vs `_int_mm` + eager epilogue | 0.930 / 0.644 | not run | Correctness in the same runs: 6.6 to 8.2 billion output elements per card, compared bitwise against the reference. Every probed tile, every scale-dtype pair, with and without bias, ragged and special-value cases: 0 mismatches. The 1-ulp negative control was caught on every card. On A100 and L4 the Triton kernel also matched the same reference: 0 mismatches. End to end, FLUX.3 klein INT8, 1024x1024, base (merge base 57a486b) vs head, each in a fresh process, interleaved: Colab L4, each arm a fresh process (fresh Studio home, so every head run pays its race), base and head interleaved, 6 to 8 images per run: | stack | today's path | base s/image (4 runs) | head s/image (4 runs) | per image | s/step base -> head | |---|---|---|---|---|---| | torch 2.14.1, Triton 3.8.0 (Colab default) | cuBLAS | 10.53 / 10.53 / 10.59 / 10.54 | 8.45 / 8.46 / 8.35 / 8.45 | -19.8% | 0.640 -> 0.510 | | torch 2.13.0, Triton 3.7.1 (fresh CUDA 13 install) | Triton `i8mm_dq` | 8.89 / 8.87 / 8.88 / 8.88 | 8.55 / 8.60 / 8.50 / 8.60 | -3.6% | 0.535 -> 0.516 | - A/A noise: base2, a second base process, landed at 10.54 and 8.84 s/image, inside 0.6% of base. - Head on Triton 3.8 (8.45 s) is faster than base on Triton 3.7 (8.88 s), so a torch 2.14 install no longer loses the fused path. - The race won all 3 of FLUX.3's weight shapes on L4 (3072x3072, 18432x3072, 3072x9216), at both M buckets, against both baselines. 1542 prebuilt-kernel calls per run, no graph breaks, no recompiles. - The race costs about 3.5 s on a cold first load (time to first image 102 -> 106 s on Triton 3.7). It is cached per card and toolchain after that. - Kill switch: headks (`UNSLOTH_DIFFUSION_INT8_CUTLASS=0`) gives 10.53 s/image, 0 prebuilt calls, and images identical to base (8 of 8). Bit-exactness in the product flow: every prebuilt-kernel output during a real FLUX.3 render was compared with the eager reference on the same inputs (`I8_VERIFY`): 750 of 750 calls, 0 mismatching elements, in each of two head processes. Studio's forced Triton kernel gave 598 checked calls, 0 mismatching elements. Images are not bitwise reproducible across processes on this stack, even for the same arm: two head processes differ by LPIPS 0.0055 (max 0.011), and two base processes on torch 2.13 by 0.0061. So head vs base cannot be judged by image identity. Head vs base LPIPS (0.0057 on torch 2.13) is inside that A/A spread. On torch 2.14.1, with four processes (two head, two with Studio's own Triton kernel forced on): - The two forced-Triton processes differ from each other by LPIPS 0.0066. - One head process and one Triton process came out at LPIPS 0.00001. - Every layer was bit-exact in every process. The image-level drift is in the stack, not in either GEMM. On that stack the cuBLAS base was reproducible across processes (8 of 8 images identical), and so was the kill switch against it. Kaggle T4, torch 2.11.0, FLUX.3 klein INT8, four base and four head processes interleaved: - base: 47.15 / 47.21 / 47.16 / 47.17 s/image. - head: 47.18 / 47.10 / 47.12 / 47.15 s/image. No change. FLUX.3 on fp16-only cards loads into Studio's native W8A8 Linear. Its eager forward runs the compiled `_w8a8_rows` function, which keeps cuBLAS and lets Inductor fuse the epilogue into one pass. Against that path the microbench margin on Turing is about 4% of GEMM time, and the prebuilt kernel is not on it. The prebuilt kernel takes the uncompiled `_int_mm` path: - CUDA graph capture; - hosts with no Inductor runtime (for example Windows without Triton), where the microbench shows 0.59x of the eager epilogue. On T4 the probe, the race (551 launches, all bit-exact against the native reference) and the GPU unit tests pass. Images match base at the A/A level: LPIPS 0.00001 for both head vs base and base2 vs base. ## Tests - `tests/test_diffusion_int8_cutlass.py` (CPU): family and switch gates, broken or missing library, ROCm, and the wheel found without importing it. It checks that the generic reference matches Studio's bf16 reference and the fp16 fused path's torchao epilogue bit for bit. It also covers op-body dispatch (winner, lost small-M race, failed launch dropping the plan), the per-card and per-bias plan keys, the race margin, and that the build generator and loader agree on ABI and dtype codes. - With `UNSLOTH_DIFFUSION_INT8_CUTLASS_LIB` set on a GPU: bit-exact launch per probed tile, and launch inside a CUDA graph. - Existing `test_diffusion_int8_gemm*.py`, `test_diffusion_int8_fused.py` and `test_diffusion_fp16_card_int8.py`: unchanged results. ## Not covered here - Windows: the `win_amd64` wheel build and a Windows GPU run did not run. The build steps follow the existing Windows CUDA jobs, and the library is plain C ABI with static runtimes. - The workflow itself (manylinux container build, signing, publish) has not run in CI. The same `build.py` / `make_wheel.py` built and loaded locally (glibc, no GPU) and built on Colab L4 / A100 and Kaggle T4 with nvcc 13.0 for the measured runs. The measured binaries were built on the VMs, not by the workflow. - sm86 (RTX 30) and sm87: no card in the pool. They run the sm_86 SASS, or the sm_80 SASS on sm87, behind the same probe and race. Not measured. - A100 end to end; Qwen-Image-2.1 and MiniMax-H3 end to end. Microbench only. - L4 deep-K shapes (K >= 9216 at large M; FLUX.3's own shapes all won): the prebuilt tiles trail the Triton kernel by up to 26%, so the race keeps Triton there. MiniMax-H3 on L4 with Triton 3.7 gets no gain. - T4 end to end: no gain (above). Wiring the kernel into the compiled native function is left for a follow-up. - Image identity across processes: not reproducible for the fused arms on these stacks (above). Bit-exactness is shown per layer in the product flow and per tile in the probe and microbench. - `test_no_compiled_variant_spills_to_local_memory` fails on the L4 GPU leg (torch 2.14.1, Triton 3.8.0). It forces the existing Triton kernel, which spills there (triton-lang/triton#12486). Not re-run on the base tree.
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_CEILINGininstall.sh), so these users can be on Triton 3.8 today. On the same card, the kernel then loses to cuBLAS_int_mmby 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: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.dotloop 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: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_configreturns None for sm80 and sm89, andunlisted_wantedreturns 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:UNSLOTH_DIFFUSION_INT8_GEMM=1orUNSLOTH_DIFFUSION_INT8_GEMM_UNLISTED=1(force) still runs the kernel, as before._FALLBACK_CONFIGcannot 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_mmon 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_mmis 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._int_mmEvery 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.
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.
UNSLOTH_DIFFUSION_INT8_GEMM=0, best spill-free tile is the fastest tile from the sweep above forced onto the same model.Overlap
_FALLBACK_CONFIGagainst (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_mmis 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_tileand the race are untouched, and sm90 / sm121 still race as before. This branch already merges current main, including Studio diffusion: race the unlisted-arch int8 GEMM tiles held under load (RTX 3090 class: Qwen-Image-2.1 -10%, MiniMax-H3 -9%) #17026.Not covered
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_autoand the unlisted-gate tests now pin the pre-3.8 behaviour, so they no longer depend on the Triton the test host has installed.