Repository navigation
Studio int8: run the raced CUTLASS GEMM on T4 native W8A8 linears (FLUX.3 klein -13% per step, byte-identical) - #18441
Conversation
On a T4 the native int8 W8A8 linears (FLUX.3 klein, Qwen-Image-2.1 INT8) run the compiled _w8a8_rows function, whose traced GEMM is cuBLAS _int_mm plus an fp32 epilogue. The prebuilt CUTLASS tiles that native_prepare races per shape were never called: a census over one default FLUX.3 klein render found 2673 cuBLAS int8 GEMMs (46% of kernel time) and 0 CUTLASS launches. For an fp16, bias-free linear whose race won, the forward now runs the compiled rotation + act quant (_w8a8_codes, the same prologue _w8a8_rows traces) and the CUTLASS kernel with the native epilogue, (int32 * xs) * ws in fp32 with one cast, which the load-time probe proves equal to the stock maths. A shape whose race kept cuBLAS, a bias, bf16 or autocast keep the compiled _w8a8_rows unchanged.
|
@codex review |
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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4adda68d4e
ℹ️ 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".
| out = self._forward_cutlass16(x2, rot_h, rows) | ||
| if out is not None: | ||
| return out.reshape(*x.shape[:-1], self.out_features) |
There was a problem hiding this comment.
Preserve the row memory budget in the CUTLASS path
For large fp16 inputs on T4 whose race selected CUTLASS, this early return bypasses the chunking below, so _w8a8_codes materializes the rotated activation and int8 codes for every row at once. This can substantially increase peak memory for wide-input projections despite eliminating the int32 GEMM output: a 65536-row input with 14336 features alone produces 896 MiB of codes, whereas the existing row budget limits each codes chunk to 32 MiB. Consequently, high-resolution or long-sequence renders that previously fit can now OOM. Apply the CUTLASS path per bounded row chunk, or decline it when the input exceeds the existing chunk budget.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 66f0764. _forward_cutlass16 now uses the fused path's row budget: past it, the codes and the CUTLASS GEMM run per chunk into one output, so codes are never made for every row at once. If any chunk's row class (the small or large race slot) kept cuBLAS, the call declines before computing any codes. Checked on a Colab T4 (torch 2.13 cu130, i8gemm 1.0.0) with the native W8A8 fp16 layers at the FLUX.3 and Qwen-Image shapes, M = 512 / 4608 / 9216 and 512 / 4352 / 8448, against main's routing and the previous head. Outputs are bit-identical on every layer and M. CUTLASS runs on every chunk: 19 launches against 9 on the previous head for FLUX.3, 18 against 9 for Qwen. New CPU tests cover the chunk sizes, output order and the up-front decline.
…d path's budget [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
…ard wiring [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
…8575) #18441 routed T4 fp16 native W8A8 linears through the raced CUTLASS GEMM. Building the activation codes already lets a CUDA OOM propagate, but an OOM from `native_gemm` itself (its fp16 output is allocated inside the call) was caught by the generic `except Exception` handler. That handler cleared every `_NATIVE_PLAN` entry, for every device and shape, and then ran `torch._int_mm`, whose int32 accumulator is larger than the fp16 output that just failed to allocate. So an OOM at the GEMM turned into a second, bigger allocation on a card that was already full, and later forwards on unrelated layers and cards lost their tile plans. The eager `_int_mm_from_q` path (#17486) had the same handler. ## Change Both `native_gemm` call sites in `diffusion_native_quant.py` re-raise `torch.cuda.OutOfMemoryError` before the generic handler, matching the codes step. Every other kernel failure keeps the existing behaviour (plan reset, stock `_int_mm` maths). ## Tests Added to `tests/test_diffusion_native_quant_cutlass16.py`, CPU only, with `native_gemm` stubbed: - `test_cutlass16_gemm_oom_propagates_and_keeps_plans`: an OOM from the GEMM reaches the caller, `torch._int_mm` is never called, and both plan entries (this layer, plus another device and shape) are untouched. Fails on main (OOM swallowed, `_int_mm` retried, plans cleared), passes here. - `test_eager_native_gemm_oom_propagates_and_keeps_plans`: the same for the eager `_int_mm_from_q` path. Fails on main, passes here. - `test_cutlass16_gemm_fault_still_falls_back`: a non-OOM GEMM failure still clears the plans and returns the `_int_mm` result. Passes on main and here. Suite (`tests/test_diffusion_native_quant*.py`, `tests/test_diffusion_int8*.py`, `tests/test_diffusion_native_w8a8*.py`): 1001 passed, 343 skipped, 4 failed on both main and this branch, with the same failing IDs (`test_diffusion_int8_attention_toggle.py::test_request_models_carry_the_field_and_old_clients_send_nothing` and three `test_diffusion_int8_fused.py::test_fused_flux_single_*`). These failures were already there before this change. ## Not proven here No GPU run. The tests stub the GEMM, so they don't show a real OOM on a T4. That would take FLUX.3 klein INT8 on a T4 with `gpu_ballast.py` holding memory so the CUTLASS output allocation fails: on main the step should either hit a second OOM in `_int_mm` or recover with `_NATIVE_PLAN` empty, and here the OOM should surface once with `_NATIVE_PLAN` unchanged.
Problem
On T4 (sm_75), the load-time race picks a CUTLASS INT8 tile for every native W8A8 shape, but a default render never runs one.
_int_mm_from_qskips its CUTLASS branch while compiled code is tracing (not torch.compiler.is_compiling()). On T4, every native W8A8 call goes through_forward_fused, which runs the compiled_w8a8_rows, and its GEMM is alwaystorch._int_mm(cuBLAS).Census of one default 1024 render on Colab T4:
_int_mmFix
_w8a8_codesis split out of_w8a8_rows. Existing callers get the same inductor graph._forward_cutlass16path runs the compiled activation quant and then the raced CUTLASS GEMM, with its fused epilogue. It covers fp16 linears without a bias whose race picked a tile.cutlass16_calls, andUNSLOTH_DIFFUSION_INT8_CUTLASS=0turns the path off.Results
Colab T4 high-RAM, torch 2.13.0+cu130, i8gemm wheel 1.0.0. Arms ran in b h h b b h order, base 8ef13a7.
_w8a8_codesplus the eager epilogue equals compiled_w8a8_rowsbit for bit on 5 shapes.Tests
tests/test_diffusion_native_quant_cutlass16.pyhas 13 new CPU tests. The native quant, grouped INT8, native W8A8, INT8 CUTLASS and step-graph suites give 481 passed and 23 skipped.Not measured: a T4 denoiser compiled by the user's speed setting, Windows T4, Kaggle T4, and the torch 2.6 / transformers 4.57.6 floors.