Skip to content

Studio int8: run the raced CUTLASS GEMM on T4 native W8A8 linears (FLUX.3 klein -13% per step, byte-identical) - #18441

Merged
danielhanchen merged 4 commits into
mainfrom
studio-t4-int8-cutlass-reach
Nov 20, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-t4-int8-cutlass-reach

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_q skips 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 always torch._int_mm (cuBLAS).

Census of one default 1024 render on Colab T4:

model cuBLAS _int_mm share of kernel time CUTLASS during the render
FLUX.3 klein INT8 2673 calls, 8553 ms 46% 0
Qwen-Image-2.1 INT8 1280 calls, 6077 ms 41% 0

Fix

  • _w8a8_codes is split out of _w8a8_rows. Existing callers get the same inductor graph.
  • A new _forward_cutlass16 path 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.
  • Everything else keeps today's path, and a compile failure turns the new path off.
  • The counter is cutlass16_calls, and UNSLOTH_DIFFUSION_INT8_CUTLASS=0 turns 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.

model main s/step this PR s/step change
FLUX.3 klein INT8 4.180 / 4.416 / 4.423 3.737 / 3.876 / 3.829 -13.3%; 21.2 to 18.7 s per image
Qwen-Image-2.1 INT8 (8 steps, streamed) 6.54 / 7.12 / 5.74 6.47 / 6.53 / 4.99 tie (streaming noise 1.38 s)
  • Exactness: PNG md5s are identical across all 6 arms for each model.
  • Engagement: every compiled INT8 call (5346 of 5346) goes to CUTLASS. The profiled render makes 1782 CUTLASS calls and 0 cuBLAS INT8 calls.
  • B200 (correctness only): compiled _w8a8_codes plus the eager epilogue equals compiled _w8a8_rows bit for bit on 5 shapes.
  • Memory: unchanged.
    • torch's reserved peak on Qwen-Image-2.1 is 14708 MiB on main and 14700 MiB here. Memory outside torch is 166 MB in both arms, and only the render process holds GPU memory.
    • Standalone INT8 layers at the Qwen and FLUX.3 shapes add 0 MB outside torch.

Tests

tests/test_diffusion_native_quant_cutlass16.py has 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.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T20:36:56.669444Z 46d7d89 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 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: 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".

Comment on lines +916 to +918
out = self._forward_cutlass16(x2, rot_h, rows)
if out is not None:
return out.reshape(*x.shape[:-1], self.out_features)

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 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 👍 / 👎.

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.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 66f0764fd8

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 46d7d89e0f

ℹ️ 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 8c5ca83 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-t4-int8-cutlass-reach branch November 20, 2026 00:52
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…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.
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