Skip to content

[cutedsl] Encode per-group TMA descriptors from 16 bytes of the operand - #4814

Closed
choijon5 wants to merge 4 commits into
choijon5/cute-norm-bwd/3from
choijon5/cute-norm-bwd/4
Closed

choijon5 wants to merge 4 commits into
choijon5/cute-norm-bwd/3from
choijon5/cute-norm-bwd/4

Conversation

@choijon5

@choijon5 choijon5 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[cutedsl] Encode per-group TMA descriptors from 16 bytes of the operand

For the norm backward kernels (layer_norm_bwd in examples/layer_norm.py and
rms_norm_bwd in examples/rms_norm.py, which TritonBench runs as
layer_norm-bwd and rms_norm-bwd): part of their hillclimb on the CuTe
backend.

Kernels: none gets faster; this fixes device loads behind the operand in
the grouped tcgen05 kernels that replace their A/B descriptors per group
(tcgen05_grouped_rna, tcgen05_grouped_tma_rn, the general grouped path
of cute_mma), which could end a launch with CUDA_ERROR_ILLEGAL_ADDRESS.
Their time is unchanged where measured on a B200: 0.997 to 1.000 of the
time before on the 7 cases of the pretuned grouped_gemm, 1.000 to 1.024
on the flat fp32 kernel.

Regression check for attention, which was hillclimbed before this stack:
the 12 pull requests of this stack together (#4811 to #4822) against plain
main (74ac871), on a B200.

  • Generated code, rendered on CPU for the B200 target: all 1,298 configs
    that main renders for 145 attention cases generate the same source with
    the stack, and no seed list changes. The cases are the kernels of
    examples/attention.py (forward and backward), flex_attention.py,
    blackwell_attention.py and fp8_attention.py, and of
    benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
    are each case's default and every compiler seed.
  • The same with the attention stack ([testing] Time the long-kernel probe test against the traced call itself #4797 to [cutedsl] Run the plain causal flash pair in the persistent work-item loop #4810) under both sides: 759
    forward and 626 backward and attention-gym configs (defaults, seeds,
    pinned configs), 7,493 more configs that the search reaches on 12
    forward cases (one knob changed at a time, random draws) and 3,861 with
    random draws on the backward and attention-gym kernels generate the same
    source with and without this stack, and the 11 kernels compiled on the
    GPU (the eight forward shapes below and three fp8 attention inputs) have
    the same MLIR bytecode.
  • Time: the eight forward shapes of examples/attention.py (dense and
    causal, B2 H32 S1024 D64 fp16 to B8 H32 S8192 D128 bf16) at pinned
    configs, with and without this stack, interleaved, a new process for
    every reading, 4 to 8 rounds a shape. The best readings with the stack
    are 0.994 to 1.007 times those without. On this power-capped GPU every
    implementation, cuDNN SDPA included, reads one of two levels 3 to 5%
    apart from process to process; both sides read the same two levels.

A grouped tcgen05 kernel with dynamic A/B descriptors replaces an operand's
TMA descriptor on the device for each group
(TensorMapManager.update_tensormap: address, shape, strides) and loads
through the copy. The host encoded the initial descriptor over the whole
packed operand. The driver marks a descriptor whose dims hold 128 KiB or
more: cuTensorMapEncodeTiled sets bit 21 of the descriptor's second word
from 131072 bytes on, whatever the strides and the L2 promotion. The
replacement keeps the mark, and with it the loads of a group smaller than
128 KiB touch memory far behind the group, 110 KiB behind a one-row group of
128 fp32 columns. The values the kernel computes with are right, so results
did not change. But a small group that ends the packed operand reads behind
the allocation, and when nothing is mapped there the launch fails with
CUDA_ERROR_ILLEGAL_ADDRESS. That error is sticky: the process cannot use
CUDA afterwards.

This is on origin/main for the flat grouped kernel
(tcgen05_grouped_rna, tcgen05_grouped_tma_rn) and for the general
grouped path of cute_mma. It is what makes
test_cute_grouped_fp32_cuda.py::test_grouped_fp32_modes_numerics_and_graph_replay[tma-rn-dynamic-warp-prefix-tail]
fail in the CuTe GPU lane depending on the tests its xdist worker ran before
it: a worker returns cached blocks to the driver between tests, so the
operand can end a mapping. The 668 tests that one such worker ran, in its
order in one process with one xdist worker, reproduce it on origin/main
(667 passed, 1 failed).

  • The device decides which operands it replaces
    (_tcgen05_per_group_tensormaps: every operand of a kernel with dynamic
    A/B descriptors but the shared RHS and a full-allocation operand, whose
    one descriptor serves every group) and records them in the A/B wrapper
    plan as per_group_a_tensormap and per_group_b_tensormap. The flat
    grouped kernel records both. The device emitter read that rule in four
    places; _emit_mma_pipeline now asks the one function once, and its sites
    read the answer.
  • The host wrapper encodes such an operand's descriptor from the first 16
    bytes of the operand along each dim: a part of the operand, at its base
    and with its strides (per_group_atom_lines). The coordinate tensor stays
    the whole operand's. CUTLASS encodes the initial descriptors of its
    grouped GEMMs from small tensors as well: 16 bytes per dim in the CuTe DSL
    example, extents of 1 in the C++ SM100 collective.
  • The output descriptor (dynamic_d_tensormap) is unchanged. With the
    output placed before unmapped memory, the stores of small groups did not
    reach past it and the results were exact.

Compatibility: CuTe only. The A/B wrapper plan of a kernel that replaces
descriptors gains the key of each operand whose descriptor it replaces (both
keys, B's alone with a shared RHS, A's alone for source 128 on one CTA;
three golden journals), and its host wrapper encodes those operands' initial
descriptors from 16 bytes; every other kernel's plan and wrapper are
byte-identical. A kernel module rendered by an older version has no such
keys and keeps the descriptors of the whole operand until it is rendered
again.

Performance is unchanged where measured (B200, cold-L2 graph replays of both
encodings interleaved in one process, time with the fix over time without):
the pretuned grouped_gemm, 7 cases, 0.997 to 1.000; the flat fp32 kernel
with groups of 150 KB to 67 MB, 1.000 to 1.024, where two captures of one
encoding differ by up to 8%. The eight shapes of the pretuned
grouped_gemm_deepgemm replace no descriptor (their A/B plan has neither
key) and render the same wrapper as before.

Tests:

  • test_cute_grouped_fp32_cuda.py places the matmul's operands where their
    mapping ends (_before_unmapped_memory). On origin/main both modes with
    dynamic descriptors then fail with an illegal memory access on every run
    (3 of 3, one process per case); with the fix all five modes pass.
  • per_group_tensormap_operands (helion/_testing/cute_codegen.py) reads,
    from a rendered module, the operands the kernel passes to
    update_tensormap, those its plan records and those the host encodes from
    16 bytes, and asserts that the three agree. The grouped role-local
    renders call it (7 renders; both operands in three families, none in the
    others), and so do the renders of the shared-RHS worklist (B only), of
    source 128 (A with one CTA, both with two) and of the flat kernel (both;
    none with wrapped descriptors).
  • test_cute_grouped_descriptors.py runs the wrapper lines on a stand-in
    for a rank-3 operand view: 16 bytes along each dim for 16- and 32-bit
    elements, at the operand's base and with its strides.
  • test_cute_grouped_source128.py is marked cpu_only as a module. Its
    render test, which this commit changes, only reads generated source and
    ran in the GPU lane, and so did its host-side tests of plans and seeds (21
    cases together). They run in the CPU lane now, without a GPU; the
    module's cuda test stays in the GPU lane.

Testing: B200, CUDA 13.2, on origin/main 379c13d.
scripts/run_tests.py --backend cute: GPU lane on GPUs 4 and 5, 16033
passed, 2382 skipped; CPU lane, 26954 passed, 683 skipped, 1 failed
(test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails the same way on origin/main). These and the runs below are
from before the last review fix, which gave
test_cute_grouped_source128.py its module mark (13 more of its cases move
from the GPU lane to the CPU lane) and reworded a docstring of
test_cute_grouped_fp32_cuda.py; the two modules afterwards, on GPU 5 and
without a GPU: 21 and 24 passed. The grouped and flat grouped
modules with their slow tier on GPU 7: 464 passed, 1 skipped. The 668 tests
of the worker above in one process: 668 passed (origin/main: 667 passed, 1
failed, the tma-rn-dynamic-warp-prefix-tail case). The regression test,
one process per mode, three rounds: 15 of 15 pass (origin/main: both
dynamic modes fail in every round, the three others pass). The 43 GPU tests
of kernels that replace descriptors, with their operands before unmapped
memory: none fails (origin/main: 9 fail, 8 of
test_cute_grouped_source128.py and 1 of the flat kernel).
test_rank3_rhs_grouped_static_dynamic_bk64_runtime[False] and [True]
ran on GPU 4 and passed. scripts/run_tests.py --backend triton on GPU 7:
GPU lane 3410 passed, 2407 skipped, 2 failed
(test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, ptxas C7600, the same on
origin/main); CPU lane 5164 passed, 623 skipped. ./lint.sh check passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: scripts/run_tests.py --backend cute --gpus 4,5,6,7, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); scripts/run_tests.py --backend triton --gpus 5, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

autoreview passed: claude, codex

…w sm_90

No kernel's performance changes: the commit only refuses configs that were
rendered before. Below sm_90 it refuses a pipelined resident stage past the
48 KiB of static shared memory that libNVVM compiles there, where the
compile failed. On every target it refuses a kernel whose stage and other
certain static allocations together pass the per-block maximum. From sm_90
on (B200 among them) a kernel whose only static allocations are a stage that
fits the opt-in maximum of its device renders the code it rendered.

Regression check for attention, which was hillclimbed before this stack:
the 12 pull requests of this stack together (#4811 to #4822) against plain
main (74ac871), on a B200.
- Generated code, rendered on CPU for the B200 target: all 1,298 configs
  that main renders for 145 attention cases generate the same source with
  the stack, and no seed list changes. The cases are the kernels of
  examples/attention.py (forward and backward), flex_attention.py,
  blackwell_attention.py and fp8_attention.py, and of
  benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
  are each case's default and every compiler seed.
- The same with the attention stack (#4797 to #4810) under both sides: 759
  forward and 626 backward and attention-gym configs (defaults, seeds,
  pinned configs), 7,493 more configs that the search reaches on 12
  forward cases (one knob changed at a time, random draws) and 3,861 with
  random draws on the backward and attention-gym kernels generate the same
  source with and without this stack, and the 11 kernels compiled on the
  GPU (the eight forward shapes below and three fp8 attention inputs) have
  the same MLIR bytecode.
- Time: the eight forward shapes of examples/attention.py (dense and
  causal, B2 H32 S1024 D64 fp16 to B8 H32 S8192 D128 bf16) at pinned
  configs, with and without this stack, interleaved, a new process for
  every reading, 4 to 8 rounds a shape. The best readings with the stack
  are 0.994 to 1.007 times those without. On this power-capped GPU every
  implementation, cuDNN SDPA included, reads one of two levels 3 to 5%
  apart from process to process; both sides read the same two levels.

The pipelined resident reduction schedule stages its row group in static
shared memory (`cute.arch.alloc_smem`).  Below sm_90 libNVVM compiles 48 KiB
of static allocations whatever a block of the device opts in to, and fails
past them with "NVVM backend compilation failed"
(`target_facts.static_smem_limit`).  Neither of the two checks that such a
stage passes refused it:
- The plan charges the stage against the opt-in maximum
  (`thread_budget.shared_memory_budget_bytes`: 166912 bytes on an A100),
  together with the reduction's scratch, which is dynamic memory
  (`cutlass.utils.SmemAllocator`) and counts against that maximum alone.
- The per-block check that holds a kernel's static allocations to the
  target's limit before the DSL compiles it
  (`smem_footprint.check_smem_budget`) rejects on the footprint's lower
  bound, which holds an allocation only when code that is certain to run
  reads it.  The stage is read in the row loops, whose range is built from
  the block index, so the bound left it out.

So a stage between 48 KiB and the opt-in maximum was rendered for such a
target and left to the DSL's compile.  At origin/main,
examples/layer_norm.py::layer_norm_bwd with 4096 BF16 features renders for
sm_80 its two-row staged seeds with 65568 bytes of static shared memory and
its four-row one with 131136, and the check reads a footprint of
`SmemFootprint(low=0, high=None, padding=60)` against 49152 bytes.

The per-block check now counts the stage (`smem_footprint(...,
resident_stages=True)`): an allocation bound to a name that a
`_cute_resident_copy_async` fills counts in the lower bound without a read
that the walk can show. The row loops that fill and load it run for every
block that has rows, and the compiler keeps what they load. A copy in code
that the compiler drops does not count, nor a stage that nothing else fills:
a loop without a trip (an unrolled loop of no trips, a dynamic loop over a
static empty range), and the branch that a test over static integers never
takes. The second is the stage's prologue, `if begin < end`, and for a
deeper ring `if begin + trip * rows < end` in an unrolled loop, which the
walk decides from the least and greatest values of the two sides
(`_Walk.interval`: literals, the variable of an unrolled loop, a name bound
once to such an expression, and sums, differences and products of them). A
row loop over a dim of its own with static length zero is written that way:
its bounds are literals. A name is bound once when one statement of the
kernel binds it and the launch does not: the walk counts the targets of the
statements, of whatever context a generated target carries, and a kernel's
formals, so a name that something else can bind again decides no test. The
check adds the stage's bytes to the kernel's other certain allocations and
refuses a sum past the per-block maximum of the kernel's target
(`target_facts.max_smem_per_block`: the opt-in maximum, and no more than 48
KiB below sm_90), naming both numbers. It counts the bytes of the
allocations alone, without alignment padding or dynamic scratch, so a stage
of exactly 48 KiB still renders. The bounds that size the spread pad
(`passes/pdl.py`) leave the stage out, as before: no generated code changes.

Compatibility: CuTe only. Below sm_90 a pipelined resident config whose
stage exceeds 48 KiB is refused, where it was rendered. On every target a
kernel whose stage and other certain static allocations together pass the
per-block maximum is refused, where it was rendered and left to libNVVM. The
plan admits no stage past the opt-in maximum that it reads, which at this
commit is the current device's (the next commit reads the kernel's device):
from sm_90 on, a kernel whose only static allocations are its stage is
unaffected where the two are one part, and where they are not, a stage past
the maximum of the kernel's device is refused here, where libNVVM failed on
it. No kernel that compiled is refused for a stage that the compiler drops:
a row range that is statically empty leaves the stage's copies in a prologue
that is never taken and a row loop without a trip, which the check leaves
out (a row loop over a dim of its own of length zero, at either ring depth).
A row loop over the rows of the block is not such a range, also for a row
dimension of static length zero, which launches an empty grid of the same
code. No sm_8x device was at hand: that a stage past 48 KiB fails in libNVVM
there is the limit that `target_facts` records and that this check enforces
for every other allocation, not a failure reproduced on hardware.

Tests (CPU):
- test/test_cute_smem_footprint.py: a stage read in a loop over the block
  index stays out of the bounds and counts for the per-block check, by its
  bytes (49152 for 12288 FP32 elements, beside dynamic scratch); an
  allocation that no stage copy fills stays out, and so does a stage that
  only dropped code fills: loops without a trip (`range(0)`, a static empty
  range, `range_constexpr(0)`, and a row loop inside one), and a prologue
  whose test is never true (`if 0 < 0`, the untaken side of an `else`, and
  the deeper ring's `if begin + trip * 2 < end` over three trips), while a
  test that some trip passes, or one over the block index, keeps the stage;
  a test over a name follows the name's one binding, also when its target
  carries Load context, and is left open for a name bound twice (a binding
  after the test hides no stage that the test keeps) and for a formal that
  the body binds again. Through `check_smem_budget` itself, for a model of
  an A100: the stage of exactly 49152 bytes passes and one of 49168 bytes
  is refused.
- test/test_cute_resident_reductions_codegen.py: for a model of an A100
  (sm_80, 166912 bytes to opt in to) the one-row stage of layer_norm_bwd at
  4096 BF16 features (32784 bytes) renders and the two-row stage is refused
  as "65568 bytes of shared memory, over the 49152-byte" maximum; for a
  model of an sm_90 part both render.  A kernel whose row loop has a dim of
  its own renders one pipelined seed of each ring depth with the stage's
  copies, over no rows and over four: the check, read with the kernel's
  formals as `check_smem_budget` reads it, counts no stage for the first
  and the stage for the second.
Each of the four tests has cases that fail without the change.

Testing: B200 (sm_100), CUDA 13.2, torch 2.15.0+cu132, on origin/main
74ac871. `scripts/run_tests.py --backend cute --lane cpu` at this commit:
27251 passed, 684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well). No test of the GPU lane reaches the refusal below
sm_90. That lane shows that the stage the check now counts refuses no kernel
of the suite on sm_100; it ran at the tip of the stack, which holds this
commit: `scripts/run_tests.py --backend cute --gpus 4,5,6,7`, GPU lane 16010
passed, 2374 skipped. Those lanes ran before the last review's two
suggestions went into the tests (the binding after the test in
test_cute_smem_footprint.py, the kernel's formals in
test_cute_resident_reductions_codegen.py); the two modules ran again on the
CPU lane at this commit, 62 passed. `./lint.sh check` passes.

autoreview passed: claude, codex

stack-info: PR: #4811, branch: choijon5/cute-norm-bwd/1
No kernel's performance changes on a host whose CUDA devices are one part,
which is where it was measured.  On a host of different parts, a kernel that
is bound or rendered while another part is the current device is now priced
for its own device, which changes the stages its plan admits; no such host
was at hand, so that was not measured.

Regression check for attention, which was hillclimbed before this stack:
the 12 pull requests of this stack together (#4811 to #4822) against plain
main (74ac871), on a B200.
- Generated code, rendered on CPU for the B200 target: all 1,298 configs
  that main renders for 145 attention cases generate the same source with
  the stack, and no seed list changes. The cases are the kernels of
  examples/attention.py (forward and backward), flex_attention.py,
  blackwell_attention.py and fp8_attention.py, and of
  benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
  are each case's default and every compiler seed.
- The same with the attention stack (#4797 to #4810) under both sides: 759
  forward and 626 backward and attention-gym configs (defaults, seeds,
  pinned configs), 7,493 more configs that the search reaches on 12
  forward cases (one knob changed at a time, random draws) and 3,861 with
  random draws on the backward and attention-gym kernels generate the same
  source with and without this stack, and the 11 kernels compiled on the
  GPU (the eight forward shapes below and three fp8 attention inputs) have
  the same MLIR bytecode.
- Time: the eight forward shapes of examples/attention.py (dense and
  causal, B2 H32 S1024 D64 fp16 to B8 H32 S8192 D128 bf16) at pinned
  configs, with and without this stack, interleaved, a new process for
  every reading, 4 to 8 rounds a shape. The best readings with the stack
  are 0.994 to 1.007 times those without. On this power-capped GPU every
  implementation, cuDNN SDPA included, reads one of two levels 3 to 5%
  apart from process to process; both sides read the same two levels.

`thread_budget.shared_memory_budget_bytes` is the shared memory per block
that the reduction strategies price their buffers against: the stage of a
pipelined resident reduction (`lane_loops/root_passes.py`), the register
tile's buffers (`register_tile_reductions.py`) and the grouped shared-memory
reduces (`reduction_emitter.py`). It read the properties of the current CUDA
device. A kernel is compiled for the device of its tensors, which the
compile environment holds. The two differ when a kernel is bound or rendered
while another device is current: a bind or a render launches nothing, so the
launch contract, which has the caller make the tensors' device current
before a call
(test/test_misc.py::TestLauncher::test_correct_result_on_each_cuda_device),
does not cover them. On a host where the two are different parts the budget
was then the other part's. With the tensors on a B200 (232448 bytes to opt
in to) and an sm_120 part current (101376 bytes), the plan refuses the
four-row stage of layer_norm_bwd at 4096 BF16 features, 131136 bytes, which
the B200 holds; the other way round it admits a stage past the device's
maximum, which the per-block check then refuses (the commit "Hold a staged
resident row to the static shared memory below sm_90" makes it count the
stage).

The probe now reads the device of the kernel being compiled
(`CompileEnvironment.current().device`), and the current device when the
kernel's tensors are on no CUDA device (a CPU bind).  It takes the device
as an optional argument, which `TargetFacts.for_device` passes.  A config's
capability and SM count already follow its device
(test/test_testing_helpers.py::test_direct_config_uses_its_device_for_an_implicit_capability).

Compatibility: CuTe only, and only hosts whose current CUDA device is
another part than the one a kernel's tensors are on.  Where every device is
the same part, as on this host, the budget is what it was.  The defect was
found by reading, in the review of the commit named above.  No mixed host
was at hand: the test models the two devices.

Tests (test/test_cute_smem_footprint.py, CPU): with two modelled devices
(101376 bytes the current one, 232448 the kernel's) the probe returns the
kernel's budget for its device and for a compile environment on it, and
the current device's for a CPU bind.  The modelled properties refuse a CPU
device as torch's do, so it is the fallback that answers there.  The test
fails without the change, and without the fallback.

Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871.
`scripts/run_tests.py --backend cute --lane cpu` at this commit: 27252
passed, 684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well). Every reduction strategy reads the probe on a device;
the GPU lane ran at the tip of the stack, which holds this commit:
`scripts/run_tests.py --backend cute --gpus 4,5,6,7`, GPU lane 16010 passed,
2374 skipped (one GPU a job, so the kernel's device is the current one
there). The lane ran before the last review of the commit below changed two
of that commit's tests (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); the two modules ran again at this
commit, 63 passed. `./lint.sh check` passes.

Review notes:
- Not valid: "the default CuTe launcher launches in the current device's
  context, not on the device of the kernel's tensors". That is the launch
  contract of both backends, which
  test/test_misc.py::TestLauncher::test_correct_result_on_each_cuda_device
  states: the caller makes the tensors' device current
  (`torch.cuda.device(N)`) before a call, as with raw Triton. The Triton
  guard that the finding cites (runtime/triton/launcher.py,
  `_empty_function`) is the probe kernel's, not a launch's. Run at
  origin/main on two B200s, cuda:0 current and the tensors on cuda:1: Triton
  raises "Pointer argument cannot be accessed from Triton", CuTe fails with
  "CUDA error: an illegal memory access"; under `torch.cuda.device(1)` both
  return the right values. Whether the CuTe launcher should raise as Triton
  does, or switch devices, changes launch behavior and its fast path: raised
  with the user, not adopted in this stack.

autoreview passed: claude, codex

stack-info: PR: #4812, branch: choijon5/cute-norm-bwd/2
…f order

Kernels: none gets faster; two wrong-result fixes, both on origin/main and
reached by no kernel in the repository. In the AST harness, a resident
reduction row whose carry is assigned a value that is the same for every
feature of the row kept the carry's old value (over three rows the
rewritten loop stored 0 where the loop as written stores 2), and a
reduction of a carry over the features, written above the carry's update,
summed the updated carry (the rewritten loop stored -1 where the loop as
written stores 0).

Regression check for attention, which was hillclimbed before this stack:
the 12 pull requests of this stack together (#4811 to #4822) against plain
main (74ac871), on a B200.
- Generated code, rendered on CPU for the B200 target: all 1,298 configs
  that main renders for 145 attention cases generate the same source with
  the stack, and no seed list changes. The cases are the kernels of
  examples/attention.py (forward and backward), flex_attention.py,
  blackwell_attention.py and fp8_attention.py, and of
  benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
  are each case's default and every compiler seed.
- The same with the attention stack (#4797 to #4810) under both sides: 759
  forward and 626 backward and attention-gym configs (defaults, seeds,
  pinned configs), 7,493 more configs that the search reaches on 12
  forward cases (one knob changed at a time, random draws) and 3,861 with
  random draws on the backward and attention-gym kernels generate the same
  source with and without this stack, and the 11 kernels compiled on the
  GPU (the eight forward shapes below and three fp8 attention inputs) have
  the same MLIR bytecode.
- Time: the eight forward shapes of examples/attention.py (dense and
  causal, B2 H32 S1024 D64 fp16 to B8 H32 S8192 D128 bf16) at pinned
  configs, with and without this stack, interleaved, a new process for
  every reading, 4 to 8 rounds a shape. The best readings with the stack
  are 0.994 to 1.007 times those without. On this power-capped GPU every
  implementation, cuDNN SDPA included, reads one of two levels 3 to 5%
  apart from process to process; both sides read the same two levels.

The resident reduction rewrite (`resident_reductions._plan`) runs the
statements of a row that read nothing of a feature once for the row, ahead
of the loop over the thread's features. That loop loads each feature's
carries from their register fragments, runs the feature's statements, adds
each reduction's input to its sum and stores the carries back.

- An update of a carry that reads nothing of its feature,

      carry = cutlass.Float32(row)

  was classified with the row's statements (`_tainted` drops a name that
  is assigned from values that do not vary) and ran ahead of the feature
  loop, whose load then replaced the new value by the old one and stored
  that back. Every schedule of the family did so: resident, grouped and
  pipelined. `_plan` now declines a loop in which a carry ends up not
  varying.
- A reduction's input is added to its sum when the feature's statements
  have run (`_emit`), wherever the row has the reduction. A row assigns a
  name once, so the input holds then what the reduction read, unless it is
  a carry that the row updates below the reduction:

      total = <sum of carry over the features>   # the carry of the rows above
      carry = old_carry + increment

  `_plan` now declines a loop whose row assigns a reduction's input below
  the reduction. A reduction below the update reads the value that the
  rewrite sums, and keeps its schedules.

A config that asks for a resident schedule is refused for such a loop, as
for any loop the family cannot prove, and every other config lowers it as
written.

Compatibility: CuTe only. A kernel of either form no longer has resident
configs. Those of the second form summed the wrong carry. Those of the
first form stored the wrong carry, with one exception that is declined all
the same, because the check does not look at the value: an update that
gives the carry the value it already has (`carry = cutlass.Float32(initial)`
for a carry that starts as `initial`) stored the right one. The kernels in
the repository update their carries from the row's values (`acc += ...`),
reduce no carry, and render as before.

Tests (test/test_cute_resident_reductions.py):
- The update that reads nothing of its feature joins the cases of
  `test_unknown_effects_aliases_and_loop_carried_values_decline`, which
  asserts that the rewrite leaves the loop as written. Without the fix the
  loop is rewritten, and running both forms on the harness's arrays gives 2
  as written and 0 rewritten, with one row per group, two, and staged.
- `test_a_marker_sums_the_carry_that_its_position_reads` puts the
  harness's second reduction on the carry. Above the update the loop keeps
  its form; without the fix it is rewritten, and the test fails. Below the
  update the loop is rewritten with one row per group, two, and staged, and
  each rewrite stores what the loop as written stores.
- For that comparison the stand-in of the lane reduction in
  `_assert_grouped_rows_match`, which returned 0, adds its input once for
  each feature. Every feature holds the same values in the harness's
  arrays, so that is the reduction's value, and the reductions of the
  unchanged source have zero inputs: the other tests compare what they
  compared.

Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 57009cb.
test/test_cute_resident_reductions.py at this commit: 1139 passed (the
module has no device test). `scripts/run_tests.py --backend cute --lane cpu`
at this commit: 27185 passed, 684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well and passes on the 2.16 nightly). The GPU lane ran with
the tip of the stack, which holds this commit: `scripts/run_tests.py
--backend cute --gpus 2,3`, GPU lane 16024 passed, 2370 skipped; CPU lane
27318 passed, 684 skipped and the same 1 failed. `./lint.sh check` passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: `scripts/run_tests.py --backend cute
--gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); `scripts/run_tests.py
--backend triton --gpus 5`, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

autoreview passed: claude, codex

stack-info: PR: #4813, branch: choijon5/cute-norm-bwd/3
Kernels: none gets faster; this fixes device loads behind the operand in
the grouped tcgen05 kernels that replace their A/B descriptors per group
(`tcgen05_grouped_rna`, `tcgen05_grouped_tma_rn`, the general grouped path
of `cute_mma`), which could end a launch with `CUDA_ERROR_ILLEGAL_ADDRESS`.
Their time is unchanged where measured on a B200: 0.997 to 1.000 of the
time before on the 7 cases of the pretuned `grouped_gemm`, 1.000 to 1.024
on the flat fp32 kernel.

Regression check for attention, which was hillclimbed before this stack:
the 12 pull requests of this stack together (#4811 to #4822) against plain
main (74ac871), on a B200.
- Generated code, rendered on CPU for the B200 target: all 1,298 configs
  that main renders for 145 attention cases generate the same source with
  the stack, and no seed list changes. The cases are the kernels of
  examples/attention.py (forward and backward), flex_attention.py,
  blackwell_attention.py and fp8_attention.py, and of
  benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
  are each case's default and every compiler seed.
- The same with the attention stack (#4797 to #4810) under both sides: 759
  forward and 626 backward and attention-gym configs (defaults, seeds,
  pinned configs), 7,493 more configs that the search reaches on 12
  forward cases (one knob changed at a time, random draws) and 3,861 with
  random draws on the backward and attention-gym kernels generate the same
  source with and without this stack, and the 11 kernels compiled on the
  GPU (the eight forward shapes below and three fp8 attention inputs) have
  the same MLIR bytecode.
- Time: the eight forward shapes of examples/attention.py (dense and
  causal, B2 H32 S1024 D64 fp16 to B8 H32 S8192 D128 bf16) at pinned
  configs, with and without this stack, interleaved, a new process for
  every reading, 4 to 8 rounds a shape. The best readings with the stack
  are 0.994 to 1.007 times those without. On this power-capped GPU every
  implementation, cuDNN SDPA included, reads one of two levels 3 to 5%
  apart from process to process; both sides read the same two levels.

A grouped tcgen05 kernel with dynamic A/B descriptors replaces an operand's
TMA descriptor on the device for each group
(`TensorMapManager.update_tensormap`: address, shape, strides) and loads
through the copy. The host encoded the initial descriptor over the whole
packed operand. The driver marks a descriptor whose dims hold 128 KiB or
more: `cuTensorMapEncodeTiled` sets bit 21 of the descriptor's second word
from 131072 bytes on, whatever the strides and the L2 promotion. The
replacement keeps the mark, and with it the loads of a group smaller than
128 KiB touch memory far behind the group, 110 KiB behind a one-row group of
128 fp32 columns. The values the kernel computes with are right, so results
did not change. But a small group that ends the packed operand reads behind
the allocation, and when nothing is mapped there the launch fails with
`CUDA_ERROR_ILLEGAL_ADDRESS`. That error is sticky: the process cannot use
CUDA afterwards.

This is on origin/main for the flat grouped kernel
(`tcgen05_grouped_rna`, `tcgen05_grouped_tma_rn`) and for the general
grouped path of `cute_mma`. It is what makes
`test_cute_grouped_fp32_cuda.py::test_grouped_fp32_modes_numerics_and_graph_replay[tma-rn-dynamic-warp-prefix-tail]`
fail in the CuTe GPU lane depending on the tests its xdist worker ran before
it: a worker returns cached blocks to the driver between tests, so the
operand can end a mapping. The 668 tests that one such worker ran, in its
order in one process with one xdist worker, reproduce it on origin/main
(667 passed, 1 failed).

- The device decides which operands it replaces
  (`_tcgen05_per_group_tensormaps`: every operand of a kernel with dynamic
  A/B descriptors but the shared RHS and a full-allocation operand, whose
  one descriptor serves every group) and records them in the A/B wrapper
  plan as `per_group_a_tensormap` and `per_group_b_tensormap`. The flat
  grouped kernel records both. The device emitter read that rule in four
  places; `_emit_mma_pipeline` now asks the one function once, and its sites
  read the answer.
- The host wrapper encodes such an operand's descriptor from the first 16
  bytes of the operand along each dim: a part of the operand, at its base
  and with its strides (`per_group_atom_lines`). The coordinate tensor stays
  the whole operand's. CUTLASS encodes the initial descriptors of its
  grouped GEMMs from small tensors as well: 16 bytes per dim in the CuTe DSL
  example, extents of 1 in the C++ SM100 collective.
- The output descriptor (`dynamic_d_tensormap`) is unchanged. With the
  output placed before unmapped memory, the stores of small groups did not
  reach past it and the results were exact.

Compatibility: CuTe only. The A/B wrapper plan of a kernel that replaces
descriptors gains the key of each operand whose descriptor it replaces (both
keys, B's alone with a shared RHS, A's alone for source 128 on one CTA;
three golden journals), and its host wrapper encodes those operands' initial
descriptors from 16 bytes; every other kernel's plan and wrapper are
byte-identical. A kernel module rendered by an older version has no such
keys and keeps the descriptors of the whole operand until it is rendered
again.

Performance is unchanged where measured (B200, cold-L2 graph replays of both
encodings interleaved in one process, time with the fix over time without):
the pretuned `grouped_gemm`, 7 cases, 0.997 to 1.000; the flat fp32 kernel
with groups of 150 KB to 67 MB, 1.000 to 1.024, where two captures of one
encoding differ by up to 8%. The eight shapes of the pretuned
`grouped_gemm_deepgemm` replace no descriptor (their A/B plan has neither
key) and render the same wrapper as before.

Tests:
- `test_cute_grouped_fp32_cuda.py` places the matmul's operands where their
  mapping ends (`_before_unmapped_memory`). On origin/main both modes with
  dynamic descriptors then fail with an illegal memory access on every run
  (3 of 3, one process per case); with the fix all five modes pass.
- `per_group_tensormap_operands` (`helion/_testing/cute_codegen.py`) reads,
  from a rendered module, the operands the kernel passes to
  `update_tensormap`, those its plan records and those the host encodes from
  16 bytes, and asserts that the three agree. The grouped role-local
  renders call it (7 renders; both operands in three families, none in the
  others), and so do the renders of the shared-RHS worklist (B only), of
  source 128 (A with one CTA, both with two) and of the flat kernel (both;
  none with wrapped descriptors).
- `test_cute_grouped_descriptors.py` runs the wrapper lines on a stand-in
  for a rank-3 operand view: 16 bytes along each dim for 16- and 32-bit
  elements, at the operand's base and with its strides.
- `test_cute_grouped_source128.py` is marked `cpu_only` as a module. Its
  render test, which this commit changes, only reads generated source and
  ran in the GPU lane, and so did its host-side tests of plans and seeds (21
  cases together). They run in the CPU lane now, without a GPU; the
  module's `cuda` test stays in the GPU lane.

Testing: B200, CUDA 13.2, on origin/main 379c13d.
`scripts/run_tests.py --backend cute`: GPU lane on GPUs 4 and 5, 16033
passed, 2382 skipped; CPU lane, 26954 passed, 683 skipped, 1 failed
(`test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition`,
which fails the same way on origin/main). These and the runs below are
from before the last review fix, which gave
`test_cute_grouped_source128.py` its module mark (13 more of its cases move
from the GPU lane to the CPU lane) and reworded a docstring of
`test_cute_grouped_fp32_cuda.py`; the two modules afterwards, on GPU 5 and
without a GPU: 21 and 24 passed. The grouped and flat grouped
modules with their slow tier on GPU 7: 464 passed, 1 skipped. The 668 tests
of the worker above in one process: 668 passed (origin/main: 667 passed, 1
failed, the `tma-rn-dynamic-warp-prefix-tail` case). The regression test,
one process per mode, three rounds: 15 of 15 pass (origin/main: both
dynamic modes fail in every round, the three others pass). The 43 GPU tests
of kernels that replace descriptors, with their operands before unmapped
memory: none fails (origin/main: 9 fail, 8 of
`test_cute_grouped_source128.py` and 1 of the flat kernel).
`test_rank3_rhs_grouped_static_dynamic_bk64_runtime[False]` and `[True]`
ran on GPU 4 and passed. `scripts/run_tests.py --backend triton` on GPU 7:
GPU lane 3410 passed, 2407 skipped, 2 failed
(`test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop` and
`::test_task_accumulators_get_their_own_rings`, ptxas C7600, the same on
origin/main); CPU lane 5164 passed, 623 skipped. `./lint.sh check` passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: `scripts/run_tests.py --backend cute
--gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); `scripts/run_tests.py
--backend triton --gpus 5`, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

autoreview passed: claude, codex

stack-info: PR: #4814, branch: choijon5/cute-norm-bwd/4
@choijon5
choijon5 marked this pull request as draft November 28, 2026 20:36
@choijon5
choijon5 changed the base branch from choijon5/cute-norm-bwd/3 to main November 28, 2026 20:36
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/4 branch from b4c5e61 to 2aeb01b Compare November 28, 2026 20:37
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/3 November 28, 2026 20:37
@choijon5
choijon5 marked this pull request as ready for review November 28, 2026 20:38
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from a9dec31 to e6b95ab Compare November 29, 2026 18:15
@choijon5

Copy link
Copy Markdown
Contributor Author

Superseded by #4786, which landed a fix for the same defect (a TensorMap rewritten on the device ended up in the large allocation class). With #4786 on main this change conflicts with it and is no longer needed, so the rest of the stack (#4811-#4813, #4815-#4822) was rebased and resubmitted without it.

@choijon5 choijon5 closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants