Repository navigation
FP8 TMA block GEMM: cut per-call descriptor host cost - #15372
Conversation
…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.
|
@codex review |
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. |
There was a problem hiding this comment.
💡 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".
| 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 {}) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
…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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
[skip ci]
Problem
Since #15136, every block FP8 GEMM that takes the TMA kernel on sm_100 builds three
TensorDescriptors per call throughTensorDescriptor.from_tensor(unsloth/kernels/fp8.py,_launch_fp8_tma_gemm).from_tensorvalidates every field again on each call, andA.view/C.viewadd two more tensor ops. For short eager GEMMs that host work is a visible part of the call.Fix
_fp8_tma_descriptorbuilds each descriptor from the tensor-independent fields (block shape, padding, and so on) thatfrom_tensorvalidated once per (dtype, block shape). Per call it sets only the live tensor, shape and strides. A contiguous tensor ofrows * colselements 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 throughfrom_tensoras before, which reads the real strides and still raises on an empty dimension._fp8_tma_gemm_okalready 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
.dataswapped is read fresh on the next call. Every call returns a new descriptor object with its ownblock_shapelist, 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.How to read this:
Dropped eager threshold, same harness (pointer kernel vs TMA, GPU-bound, microseconds per call):
Tests
tests/test_fp8_tma_eager_host_cost.py(CPU, no GPU needed):from_tensorfor fp8 / bf16 / fp16; warm launches never callfrom_tensor; a sliced row pitch falls back tofrom_tensor; the cache keeps no tensor alive; an empty dimension still raises..dataswapped 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