Skip to content

[Common] Add a register-resident CuTeDSL kernel for gated MXFP8 activations - #3853

Draft
tdophung wants to merge 9 commits into
NVIDIA:mainfrom
tdophung:teddy/mxfp8-cutedsl-gated-g2r
Draft

tdophung wants to merge 9 commits into
NVIDIA:mainfrom
tdophung:teddy/mxfp8-cutedsl-gated-g2r

Conversation

@tdophung

@tdophung tdophung commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stacked on #3761: this branch is #3761 (at d31d9a8) plus four commits, so please review only the last four until #3761 merges.

Adds a register-resident CuTeDSL kernel for the gated MXFP8 activations (MXFP8GatedQuantizeG2RKernel): SwiGLU, GeGLU, ReGLU, QGeGLU, SReGLU, GLU, clamped SwiGLU and SiTU-GLU, forward and backward. Each thread loads its rows of the act and gate halves (plus the gradient in backward) into registers, applies the gated op there with gated_elementwise_op, and quantizes both directions straight from registers. The shared-memory kernel on main (MXFP8GatedQuantizeG2SKernel) stages each tile through shared memory when it writes both directions, and this skips that. Output bytes match the CUDA kernel's.

The new kernel takes BF16 input with columnwise output; MXFP8GatedQuantizeEntry tries it first. The shared-memory kernel keeps the rest: FP16/FP32 input, rowwise-only output, colwise scales that aren't 8-byte aligned, more than 65535 row bands, and the swizzled backward when a half isn't a multiple of 128 columns. The bidimensional kernel's register-tile quantizer is factored out so both kernels use it; the non-gated kernel compiles to the same SASS.

The four commits:

  1. the kernel
  2. the warps that reduce a column's amax also write its scale (the same change [Common] Add BF16 activation tables and fused dbias to the CuTeDSL register-resident MXFP8 kernel #3761 makes in the non-gated kernel), and each CTA does one 32-row band
  3. SwiGLU's sigmoid uses rcp.approx + one Newton step when every value in the thread's row is in range, which gives the same bits as the division there, and the division otherwise (same as [Common] Add BF16 activation tables and fused dbias to the CuTeDSL register-resident MXFP8 kernel #3761's SiLU commit)
  4. the register kernel also takes the backward with GEMM-swizzled scales when each half is a multiple of 128 columns; before, that went to the shared-memory kernel

One test note: test_gated_swiglu_all_bf16_act_values folds a rowwise E8M0 NaN scale (0xFF) into 0 when that block's FP8 data is all NaN, on both backends, before the bitwise compare. Only the swizzled backward with N % 128 != 0 hits it: main's shared-memory kernel writes 0xFF there and CUDA writes 0. Both dequantize to NaN, and we agreed earlier as a team that the scale of an all-NaN block doesn't matter, so the test doesn't hold the shared-memory kernel to CUDA's choice. Everything else is still compared bit for bit.

Perf on one GB200 against #3761 (d31d9a8), same GPU (BF16 in, E4M3 out, both directions, swizzled scales unless noted, median of 500 calls with L2 flushed, #3761/this PR/this PR/#3761):

Op Shape #3761 (us) this PR (us) Speedup
SwiGLU input 4096x36864 133.10 81.98 1.62x
SwiGLU input 16384x36864 495.65 293.38 1.69x
dSwiGLU grad 4096x18432 239.06 115.89 2.06x
dSwiGLU grad 16384x18432 924.66 425.98 2.17x
dSwiGLU, compact scales grad 4096x18432 234.46 117.78 1.99x
dSwiGLU, compact scales grad 16384x18432 902.13 427.01 2.11x
SwiGLU, columnwise only input 4096x36864 91.93 68.38 1.34x
GeGLU input 4096x36864 109.57 100.54 1.09x
dSwiGLU, compact scales grad 993x7200 35.54 23.26 1.53x

Kernel time alone (CUPTI) agrees, and the output bytes are identical between the two in every row. Calls that stay on the shared-memory kernel (FP16/FP32 input, rowwise-only output, a half that isn't a multiple of 128 columns) and the non-gated GeLU kernel run at the same speed as on #3761 (0.99x-1.00x).

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • CuTeDSL/cast/mxfp8/quantize_mxfp8_gated_g2r_bidim.py (new): the register-resident gated kernel
  • CuTeDSL/cast/mxfp8/quantize_mxfp8_g2r_bidim.py: the register-tile quantizer (quantize_mxfp8_rmem_tile_bidimensional, thread_tile_bounds) factored out and shared
  • CuTeDSL/cast/mxfp8/quantize_mxfp8_gated_g2s.py: MXFP8GatedQuantizeEntry dispatches to the register kernel where it applies; gated_elementwise_op takes the fast-reciprocal choice (FAST_RCP, off by default)
  • tests/pytorch/mxfp8/test_mxfp8_cutedsl_backend.py: every BF16 act value through SwiGLU, and the swizzled backward on row and column tails for every dtype and direction

Tested on GB200 with CuTeDSL on: test_mxfp8_cutedsl_backend.py -k gated 1040 passed with tables off and on, no skips; the fused cast and gated activation C++ tests 2307 passed, *CastMXFP8ActExact* 3528 passed (non-gated bytes unchanged); compute-sanitizer racecheck and synccheck clean. Per call, the swizzled BF16 backward with columnwise output ran the register kernel 129 times and the shared-memory kernel 33 times (the N % 128 != 0 cases), with no CUDA fallback. Not run: the JAX gated tests, SM 10.0.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

tdophung and others added 8 commits November 18, 2026 08:51
…gister-resident MXFP8 kernel

Add opt-in BF16 lookup tables (NVTE_ACTIVATION_TABLE=1) for GeLU, SiLU and
their derivatives in the MXFP8 quantize. TE common builds each table on first
use, once per device. The CuTeDSL register-resident kernel reads the tables
from shared memory; the CUDA generic kernel and the other CuTeDSL kernels
compute the values the tables hold, so both backends write the same bytes.

The CuTeDSL register-resident kernel now also takes activation derivatives,
on 256-column tiles, with fused dbias and amax, any row count, column tails,
and FP16 and FP32 inputs. The CuTeDSL kernels add dbias in the CUDA kernels'
row order, except FP32 dbias with rowwise-only output, where CUDA adds row
pairs (r, r + 32) first. They give a block of only NaNs the CUDA kernels'
scale.

test_activation_table checks every BF16 input of each table against the
computed activation. test_cast_mxfp8_act_exact checks the fused quantize bit
for bit against the activation computed per element and then quantized, with
the tables on and off. The C++ QA scripts run test_activation_table, and
the dSiLU table cases of test_cast_mxfp8_act_exact, again with
NVTE_ACTIVATION_TABLE_EXACT_DSILU=1, which is read once per process. The
PyTorch and JAX CuTeDSL backend tests name table-reading kernels as TE does,
so they also run with NVTE_ACTIVATION_TABLE=1.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
…er-resident MXFP8 kernel

The threads that reduce the column amaxes of a tile now also write the colwise
scales of their own columns, where the first warp row wrote them after the
reduction:

- On 256-column tiles, 128 reducers fill four of the eight warps. The other
  four skip the reduction and the scale stores in a warp-uniform loop instead
  of running them predicated off. With a column tail, a reducer writes only
  the columns inside the tensor.
- On GEMM-swizzled 512-column tiles outside the tail kernel, every thread
  reduces, and the scale stores, a byte at a time, are spread over all eight
  warps instead of issued by warp 0 alone.

Compact 512-column scales and the 512-column tail kernel keep the first warp
row's write, and FP16 and FP32 inputs keep their own colwise path. Each scale
takes the same value and address, so the output bytes do not change.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
…d when it reads a table

The register-resident kernel launches clusters of CTAs on adjacent column
tiles: up to 4 on 512-column tiles of large tensors and up to 8 on 256-column
tiles. The kernel uses no cluster-scope feature; co-scheduling adjacent columns
raises the rate at which memory delivers a cast's reads. A tile that reads an
activation table is bound by its shared-memory lookups instead, so the cluster
buys it nothing, while whole clusters can leave CTA slots of the GPU idle.

Launch the configs that read a table (BF16 GeLU and SiLU forward) with cluster
width 1. Casts and activations without a table keep their cluster widths, and
the derivatives already launched unclustered. Only the launch geometry
changes, so the output bytes do not change.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
CuTeDSL SiLU computes x * sigmoid(x) as util/math.h does, with the IEEE
reciprocal of 1 + exp(-x). ptxas guards that reciprocal with a range check and
a slow path on every element, which made BF16 SiLU forward without a table
slower than computing x / (1 + exp(-x)).

sigmoid() gains fast_rcp. For 1 <= y < 2^126, rcp_f32_in_range issues the
approximate reciprocal and the one Newton step that rcp.rn.f32 takes in that
range, without its range check, so it returns the same bits; 1 + exp(-x) is in
that range for every x >= SIGMOID_FAST_RCP_MIN (-80). The register-resident
kernel's BF16 SiLU forward without a table checks once per thread row, with
min_nan_bf16x2, that every value is at least -80: such rows take the fast
reciprocal, and rows holding a smaller value or a NaN keep the division. The
output bytes do not change. The other kernels and dSiLU keep the division.

test_silu_fast_reciprocal_rows compares SiLU forward without a table with
the CUDA backend bit for bit on 512- and 256-column tiles, both directions
and colwise only, with both scale layouts. Each 16 columns of a row hold one
-100, at another position in each of 16 consecutive rows, so a row check that
misses any value of the thread row fails.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
…ations

Add MXFP8GatedQuantizeG2RKernel, a register-resident CuTeDSL kernel for every
gated activation. A CTA covers 256 output columns and walks 32-row bands. Each
thread loads its rows of the act and gate halves (and of the incoming
gradient, in backward) into registers, computes the gated op there with
gated_elementwise_op, rounds it to BF16 as the CUDA kernels do, and quantizes
the band in both directions from registers. The backward quantizes its two
output halves one after the other, with the gate half's scales a pointer
offset into the scale tensors.

The register-tile quantizer of the bidimensional kernel is factored out
(quantize_mxfp8_rmem_tile_bidimensional) and shared by both kernels; the
bidimensional kernel writes the same bytes as before.

MXFP8GatedQuantizeEntry picks it for BF16 input with columnwise output, and
the shared-memory gated kernel otherwise: FP16 or FP32 input, rowwise-only
output, columnwise scales not 8-byte aligned, more than 65535 row bands, or
backward with GEMM-swizzled scales.

Co-authored-by: Kaining Zhong <kainingz@nvidia.com>
Signed-off-by: Teddy Do <tdophung@nvidia.com>
…ter kernel

The gated register kernel now launches one CTA per 32-row band instead of
walking several bands per CTA. Unlike the bidimensional kernel, it has no
per-CTA setup (a table) to amortize over a walk. At 4096 tokens this doubles
the grid, which is faster, mostly in the backward.

Also correct the input-load comment. Output bytes are unchanged.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
…ed SwiGLU register kernel

SwiGLU's sigmoid divides with an IEEE reciprocal that ptxas guards with a
per-element range check and a slow path; in the gated register kernel that is
about a third of the forward's instructions.

The gated register kernel checks each 8-value thread row once: when the
NaN-propagating minimum of its act values is at least SIGMOID_FAST_RCP_MIN,
the row's SwiGLU, forward and backward, takes sigmoid's fast reciprocal;
otherwise (a smaller value, -inf or NaN) it divides as before.
gated_elementwise_op takes the choice as FAST_RCP, False by default, so the
shared-memory gated kernel and the other activations are unchanged.

Output bytes are unchanged. A new test runs SwiGLU over every BF16 act value
against the CUDA backend, on a shape with row and column tails too. For a
block with no finite value it accepts either backend's scale: CUDA writes 0
and the shared-memory gated kernel NaN, and both dequantize to NaN.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
…egister kernel

The gated register kernel now also takes the backward with GEMM-swizzled
scales when N % 128 == 0, where the gate half's scales start on a
128-column scale tile and stay a pointer offset. Other N, FP16, FP32 and
rowwise-only output stay on the shared-memory kernel.

A new test runs the swizzled backward for every input dtype and output
direction on a shape with a row tail and a 128-column tail.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
@tdophung
tdophung force-pushed the teddy/mxfp8-cutedsl-gated-g2r branch from 628bdae to d87d225 Compare November 18, 2026 23:11
@kainzhong kainzhong self-assigned this Nov 19, 2026

This branch has not been deployed

No deployments
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.

2 participants