Skip to content

FP8 TMA block GEMM: cut per-call descriptor host cost - #15372

Merged
danielhanchen merged 9 commits into
mainfrom
dh/s7-fp8-tma-descriptor-host-cost
Oct 27, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
dh/s7-fp8-tma-descriptor-host-cost

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Problem

Since #15136, every block FP8 GEMM that takes the TMA kernel on sm_100 builds three TensorDescriptors per call through TensorDescriptor.from_tensor (unsloth/kernels/fp8.py, _launch_fp8_tma_gemm). from_tensor validates every field again on each call, and A.view / C.view add two more tensor ops. For short eager GEMMs that host work is a visible part of the call.

Fix

_fp8_tma_descriptor builds each descriptor from the tensor-independent fields (block shape, padding, and so on) that from_tensor validated once per (dtype, block shape). Per call it sets only the live tensor, shape and strides. A contiguous tensor of rows * cols elements already has (cols, 1) strides as a 2D view, so no view is built on that path. Anything else (a sliced weight whose row pitch is not its width, an empty dimension) goes through from_tensor as before, which reads the real strides and still raises on an empty dimension. _fp8_tma_gemm_ok already checks the 16-byte alignment of bases and row pitches.

Nothing is cached per tensor: the cache holds no reference to any GEMM's tensors, and a weight that is replaced, edited in place, or has its .data swapped is read fresh on the next call. Every call returns a new descriptor object with its own block_shape list, and Triton encodes the tensor map by value at launch, so threads and captured CUDA graphs never share mutable descriptor state.

Routing is unchanged: the same calls take TMA as on main, in eager, under CUDA graph capture and under torch.compile. An earlier revision also kept eager GEMMs below 2**34 multiply-adds on the pointer kernel. On B200 that kernel is 1.39-1.77x slower on the GPU than TMA at every such shape (table below), so that part was dropped.

B200 timing

B200 (sm_100), torch 2.13.0+cu130, Triton 3.7.1, bf16 output, frozen FP8 weights with 128x128 scales, w8a8_block_fp8_matmul_triton. Main and this branch ran as separate fresh processes, 3 per arm, interleaved, with a second main arm as A/A. Each cell is the median over the 3 processes of the per-process median microseconds per call.

  • eager: back-to-back calls, host-bound at these sizes.
  • eager, GPU-bound: a GPU sleep lets the host queue 50 calls ahead, so CUDA events time the kernels alone.
  • graph: 20 calls captured in one CUDA graph and replayed.
M x N x K eager main eager A/A eager this PR GPU-bound main GPU-bound this PR graph main graph this PR bitwise
128 x 4096 x 4096 63.3 61.2 55.6 18.0 18.0 15.4 15.4 yes
256 x 4096 x 4096 83.8 64.3 55.4 18.5 18.5 15.8 15.8 yes
512 x 4096 x 4096 62.9 61.3 54.4 19.2 19.2 17.8 17.9 yes
1023 x 4096 x 4096 62.2 61.8 55.7 35.0 34.9 32.1 32.1 yes
1024 x 4096 x 4096 62.2 64.4 66.0 35.0 34.9 32.3 32.2 yes
2048 x 4096 x 4096 62.2 63.1 62.1 64.7 64.8 59.7 59.7 yes
4096 x 4096 x 4096 107.7 107.1 107.0 110.7 110.8 106.4 106.1 yes
256 x 14336 x 4096 62.8 63.8 59.9 34.5 34.5 31.4 31.6 yes
2048 x 4096 x 1024 62.6 61.8 54.8 26.7 26.7 24.3 24.3 yes

How to read this:

  • GPU-bound eager and graph times match main within 0.5% at every shape, against an A/A band of at most 1.3% (6.8% and 4.8% at 4096 x 4096 x 4096). Every call took the TMA kernel on both sides: 1 per eager call and 20 per graph.
  • Outputs are bitwise identical to main in every cell, eager and graph.
  • Host-bound eager calls are about 7 us faster (55 vs 62 us) at most sizes. The eager A/A band on this shared host was 3-14% in most cells (39% at 2048 x 4096 x 1024, 72% at 256 x 4096 x 4096), so cells inside it are not a result. At 1024 and 2048 x 4096 x 4096 the difference is inside the band.
  • On CPU, building the three descriptors costs 5.5-6.5 us per launch on main. The first revision of this change cut that to 4.5-5.3 us, about 1 us saved, because it still built the view and compared strides on every call. Checking contiguity instead brings it to 2.0-2.9 us.

Dropped eager threshold, same harness (pointer kernel vs TMA, GPU-bound, microseconds per call):

M x N x K TMA (main) pointer kernel (dropped threshold) ratio
128 x 4096 x 4096 17.7 30.8 1.74
256 x 4096 x 4096 18.4 30.9 1.68
512 x 4096 x 4096 19.2 32.4 1.69
1023 x 4096 x 4096 34.8 60.0 1.72
256 x 14336 x 4096 34.4 60.8 1.77
2048 x 4096 x 1024 26.7 37.0 1.39

Tests

tests/test_fp8_tma_eager_host_cost.py (CPU, no GPU needed):

  • routing: eager GEMMs take TMA at M = 128 to 2048, 2D and batched, and so do graph-captured ones; a capture before any eager call keeps the pointer kernel, as on main.
  • descriptors: fields match from_tensor for fp8 / bf16 / fp16; warm launches never call from_tensor; a sliced row pitch falls back to from_tensor; the cache keeps no tensor alive; an empty dimension still raises.
  • staleness: a weight edited in place, replaced by a new tensor, or with .data swapped to a different shape always gives a descriptor for the current tensor and shape.

On B200, tests/test_fp8_tma_block_gemm.py (including the sm_100 bitwise test of TMA against the pointer kernel) plus the file above: 41 passed.

Not covered

  • sm_103 and other Blackwell parts were not timed; only B200.
  • torchao was blocked in the timing harness so both arms use the Triton launcher. Routing to torchao is unchanged by this PR.
  • 3D inputs and fp32 outputs were checked for descriptor equality on CPU, not timed on GPU.

…er GEMMs on the pointer kernel

Every TMA launch built three TensorDescriptors through from_tensor, which re-validates all fields
(~4 us each) plus two views, and the launcher then encodes the tensor maps. On B200 a 128 x 4096 x 4096
eager GEMM went 42 -> 67 us per call while the kernel itself got faster under CUDA graphs.

Descriptors now copy the fields validated once per (dtype, block shape) and only set the live tensor,
shape and strides per call. Eager GEMMs under 2**34 multiply-adds keep the pointer kernel; graph
capture still takes TMA at any size, and the per-device self check still runs on the first eager
call. Both kernels are bitwise identical, so outputs do not change.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T14:26:10.409883Z 86a100c 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.

@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: 22c2c076a2

ℹ️ 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 thread unsloth/kernels/fp8.py Outdated
fields = _FP8_TMA_DESC_FIELDS.get(key)
if fields is None:
proto = _TensorDescriptor.from_tensor(tensor.view(rows, cols), list(block_shape))
fields = _FP8_TMA_DESC_FIELDS[key] = dict(getattr(proto, "__dict__", None) or {})

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 Exclude the tensor base from cached descriptor fields

For the first TMA call using fp16 or fp32 output, this caches the entire descriptor dictionary, including base, which holds the output tensor's storage alive for the lifetime of the module. Unlike bf16, these output dtypes are not primed by the small self-check, so the retained allocation can be a large production GEMM output and remain allocated even after the model is unloaded. Overwriting desc.base only updates the per-call copy, not the cached dictionary. Cache only tensor-independent fields so completed GEMMs can release their buffers.

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.

Agreed, fixed in 93b6ef1: the per-(dtype, block shape) cache now keeps only the tensor-independent fields; base, shape and strides are set per call and never cached. New test_cached_fields_do_not_keep_the_first_tensor_alive (a weakref to the first tensor is dead after the call). TMA tests 21 passed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 93b6ef1153

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

…n contiguous tensors

The eager size gate divided by N, so an FP8 weight with N = 0 raised ZeroDivisionError where main fell back to the pointer kernel. C.numel() * K is M * N * K directly. A contiguous tensor of rows * cols elements already has (cols, 1) strides as a 2D view, so the view is only built on the from_tensor fallback.
Timed on B200 (sm_100, torch 2.13, Triton 3.7.1, 3 fresh processes per arm, A/A arm): with the host queue ahead of the GPU, the pointer kernel the threshold selected is 1.39-1.77x slower than TMA at every shape below 2**34 multiply-adds (M = 128 to 1023 at N = K = 4096, 256x14336x4096, 2048x4096x1024), against an A/A band of at most 1.2%. No measured shape has a crossover, so eager GEMMs take TMA whenever it routes, as on main. The cheaper descriptors stay.

[skip ci]
@danielhanchen danielhanchen changed the title FP8 TMA block GEMM: cut descriptor host cost and keep small eager GEMMs on the pointer kernel FP8 TMA block GEMM: cut per-call descriptor host cost Oct 26, 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. Keep them coming!

Reviewed commit: 86a100c6ea

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

[skip ci]
@danielhanchen
danielhanchen merged commit a9bcb6c into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the dh/s7-fp8-tma-descriptor-host-cost branch October 27, 2026 10:55
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