Repository navigation
[cutedsl] Guard nn.Parameter launch arguments in the C++ relaunch guard - #4822
Merged
Merged
Conversation
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: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
force-pushed
the
choijon5/cute-norm-bwd/11
branch
from
November 28, 2026 17:16
dec7ad4 to
540c40b
Compare
choijon5
added a commit
that referenced
this pull request
Nov 28, 2026
Kernels: those with a fast relaunch entry whose first launch passed an
`nn.Parameter` among its tensors; later launches with the same argument
types and metadata take the C++ guard instead of the generated Python one.
examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as
TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter):
the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated
code and no device time changes.
The fast relaunch checks a launch's tensors in one C++ call (Dynamo's
`TensorGuards`) and falls to a generated Python guard when that call
misses. `cxx_tensor_guards` built the C++ guard only when the type of every
tensor argument was exactly `torch.Tensor`. A kernel that is handed a
module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of
its launches ran the Python one. TritonBench's rms_norm passes its weight
that way.
`TensorGuards` pins the exact Python type of each argument it is built
from, and a Parameter reads its metadata (dtype, sizes, strides, device,
data pointer) as a plain tensor does, so the guard holds for it as it is.
`cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any
other subclass, one of Parameter included, may answer for its metadata
itself and stays with the Python guards, which ask it.
B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight
a Parameter; host time of back-to-back calls, in us:
before after
launcher (default_cute_launcher) 9.3 6.4
kernel call 26.9 25.7
autograd backward call 73.5 70.9
Each figure is the median of 15 batches of 200 calls, and each row is a run
of its own. The launcher's row resolves the change: inside it the generated
Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows
below it hold that launch but do not resolve it: the kernel call read 24.5
to 27.9 us across runs of one build. With a plain tensor for the weight the
launcher takes 6.2 us before and 6.5 us after, which is that noise: an
entry built from plain tensors takes the path it took.
Compatibility: CuTe only; no change to generated code or configs. A
relaunch entry built from a Parameter now carries a C++ guard that pins the
Parameter type. A plain tensor passed in its place misses the C++ check and
is decided by the Python guards, which decided every launch of such an
entry before.
Tests:
- test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that
accepts another Parameter of the same metadata and misses for a plain
tensor and for another shape; a Tensor subclass and a Parameter subclass
get none.
- test/test_cute_fast_relaunch.py (device): a kernel launched with a
Parameter relaunches through the C++ check; a plain tensor in its place
misses that check and still relaunches, without the marshalling launch.
Testing: B200, CUDA 13.2. The launcher modules
(test_cute_launcher_alignment.py, test_cute_fast_relaunch.py,
test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute
--gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed.
`./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 28, 2026 17:16
7624326 to
39cb678
Compare
This was referenced Nov 28, 2026
choijon5
marked this pull request as draft
November 28, 2026 20:37
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 28, 2026 20:37
39cb678 to
2f6411f
Compare
choijon5
added a commit
that referenced
this pull request
Nov 28, 2026
Kernels: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
marked this pull request as ready for review
November 28, 2026 20:38
jansel
approved these changes
Nov 29, 2026
choijon5
marked this pull request as draft
November 29, 2026 18:14
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 29, 2026 18:15
2f6411f to
78f1ef5
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
marked this pull request as ready for review
November 29, 2026 18:15
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
…w sm_90 (#4811) 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. 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
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 29, 2026 20:08
d170065 to
f913fea
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
…f order (#4813) 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; 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
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: every kernel with a host sum tail that a search runs with `cute_host_paired_sum` "mapped" or "narrow", on any Torch build but the one audited one: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel gets back the host time of proofs that could not succeed: 73.7 us to 54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below). No generated kernel changes, and no launch takes another time on the device. TritonBench times `layer_norm-bwd` and `rms_norm-bwd` around the Python call, so those readings carried the cost wherever the search picked one of the two values. 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. With `cute_host_paired_sum` "mapped" or "narrow" the generated host function hands its sum tails to a runtime helper (`try_paired_sum_cast`, `try_single_sum_cast`, `try_full_sum`). The helper launches one kernel that reproduces ATen's addition tree, or returns None and the original statements run. The tree is audited for one Torch build (`_compatible_aten_sum`: the build's git version and four header hashes), and the helpers asked for the build last, after the proofs of the dispatch state, the tensors and the plan. On every other build each call of the kernel ran those proofs and then declined: the device ran what "off" runs, and the host paid for the proofs. That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's CuTe test jobs install the current nightly, and the benchmark jobs a release. The autotuner does not see the cost: it times the device, where the three values are the same kernels on such a build, and the compiler seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of them wins by noise then costs every call its proofs. The helpers now ask for the build first. The answer is cached, so a declined call costs a function call. Nothing else changes: an admitted call runs the same proofs and the same kernel, a declined one the original statements, and the generated code is the same. B200, torch 2.15.0+cu132 (not the audited build), examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned layout; host time of the kernel call at the parent commit and at this one, median of 300 calls, in us: cute_host_paired_sum off mapped narrow parent 54.5 73.7 73.4 this commit 54.0 54.1 54.3 A declining call of the helper alone, 100000 calls: paired 12.3 us before and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel picked "mapped" at 4096 features and "narrow" at 6144. One more order is fixed in `try_full_sum`. It computed a divisor's reciprocal, with tensors, before it looked at the dispatch state, so a mode of the caller saw those operations. Under `torch.device("cuda")` every call of a scaled full sum built two device scalars, divided them and synchronized before it declined, which is device work that these calls no longer do; under `torch.device("meta")` the call raised instead of declining. The reciprocal is now computed after that check. Compatibility: CuTe only, no change to generated code, configs or results. On the audited build the first call of a helper reads the four headers even when its other proofs would have declined; that happens once in a process, as before. Two test modules of the helpers had fallen behind the launcher, unseen: test/test_cute_host_paired_sum_abi.py and test/test_cute_host_single_sum_abi.py ran on the audited build only (their skip compared the build's git version), which no CI job installs. - Their stand-in launches reached the launcher's CUDA context check (#4456), which the modules forbid as they do every use of the device, and they asserted the compile options of before the division option. They skip the check for the stand-in launch and assert the launcher's current options. - They run on every build now. What they check (the launches the bound host makes, with their arguments, streams and launchers) does not depend on the installed ATen, so they override the helpers' answer about the build (`_compatible_aten_sum`, and `_compatible_aten_half_cast` for the single sum's FP16 outputs) next to the tensor and dispatch checks they already override, and the skip is gone. The skip was also narrower than the gate it stood for: a build of that git version with other headers, or for ROCm, would have run the tests and failed them. - They are `cpu_only` modules now, which the CPU lane runs without a device. One query on their path needed one: the launcher asks the CUDA runtime whether its stream is being captured, which raises in a process that sees no device. The tests answer it next to the context check (the stand-in launches are eager ones). `test/shard_durations.json` loses the modules' two zero placeholders: a module outside the GPU lane has no entry there. - They no longer depend on the launches before them, nor change the ones after. The launcher keeps the SM count of each device for the process (`_CUTE_NUM_SM_CACHE`): an earlier launch on cuda:0 answered in place of the stand-in count of 148 that the tests assert, and the stand-in count stayed behind for later launches. A B200 has 148 SMs, so neither showed here. The tests run on an empty copy of the cache. On the audited build their 6 tests fail at origin/main and pass here; on every other build they were skipped and now run. Tests: - Three new tests, one for each helper: with the build declined the helper returns None and enters none of its other proofs (they are patched to raise, the query for a function mode included, and the arguments are no tensors). - A new test calls `try_full_sum` with a divisor under a function mode, a dispatch mode and a device mode: it declines without an operation. It fails on the old order. - The tests of the other reasons to decline (CPU tensors, dispatch modes, subclasses and duals, unsupported 1-D tensors) admit the build, so that an unaudited build is not what declines in them. - Two tests made the build check raise, to show that a singleton plan declines before it. They admit the build now and keep the launcher raising, and the single sum its half-cast header check. Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules of the helpers on both lanes at this commit, with GPU 6 (test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py, test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py, test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment): GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main (f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules), CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack run on: GPU lane 9 passed, 40 skipped (the tests that need the audited build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed ones, where they were skipped. The lanes at the tip of the stack, which holds this commit, on torch 2.15.0, run while the two ABI modules were still in the GPU lane: `scripts/run_tests.py --backend cute --gpus 2,3`, GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane 27318 passed, 684 skipped, 1 failed (`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 nightly). `./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. Review notes: - Not valid here: "a declined full sum can clobber its own input" (helion/_compiler/cute/host_full_sum.py, which this commit does not touch). The defect is real and on origin/main; the next commit of the stack fixes it: "[cutedsl] Fix the full host sum's fallback and its divisor's reciprocal". - Not valid here: "five source-generation tests of test/test_cute_host_full_sum.py are gated on the ATen build and sit outside a cpu_only module". It predates this commit, which leaves those tests alone; the same next commit makes the module `cpu_only` and drops the gate from them. autoreview passed: claude, codex stack-info: PR: #4815, branch: choijon5/cute-norm-bwd/5
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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; two fixes to what runs when the helper of a full host sum declines, which it does on every Torch build but the audited one. Under `cute_host_paired_sum` "mapped" or "narrow", a kernel whose host tail assigns a full sum to the name of its operand (`out = torch.sum(out)`) or of its divisor (`n = torch.sum(out) / n`) raised a TypeError, and a scaled sum (`torch.sum(out) / x.size(0)`) evaluated its divisor twice. A third fix is to the helper's own arithmetic: it multiplied by another reciprocal than ATen does for divisors such as 1.1. 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. With "mapped" or "narrow" a full host sum goes to a runtime helper (`try_full_sum`), and the original statement runs when the helper returns None. Two things kept that statement from running as it was written. Its operand. The rewrite assigned the helper's answer to the statement's target: out = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if out is None: out = torch.sum(out) so the fallback read None where the statement reads its target. `lower_host_full_sum` now gives the answer a name of its own for such a statement and assigns the target from it when the helper answered: _helion_full_sum = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if _helion_full_sum is None: out = torch.sum(out) else: out = _helion_full_sum Its divisor. The call took the divisor as an argument (`divisor=x.size(0)`), so every call evaluated it before the helper decided, and the fallback of a declined call evaluated it again. The proof admits tensor metadata in a divisor (`x.size(0)`, `x.numel()`, `x.shape[0]`) and called that safe to evaluate twice. A function mode of the caller sees those calls, and so does a tensor subclass with `__torch_function__`: on a declined call each saw one more than the statement makes, ahead of the sum, where the helper's rule is that a declined call leaves the exact original call. This predates the stack (d32e9ca, the divisor support). The site now hands the divisor over as a function, total = _cute_try_full_sum(out, mean=False, divisor_fn=lambda: x.size(0), _launcher=_launcher) and `try_full_sum` does not call it on a build whose sums it declines, nor under a function or dispatch mode of the caller. There the statement evaluates its divisor once, in its own order: that is every build but the audited one, and every call under a mode. Replayed on CPU over an operand that records the reads of its size, a declined call reads it twice at origin/main (before the helper and in the fallback) and once here. Otherwise, on the audited build, the helper calls the function where the argument was evaluated, ahead of its proofs, so those calls behave as they did: a call that the helper answers has evaluated the divisor before the sum, and a call that its proofs decline has evaluated it once before the statement does so again. KNOWN_GAPS.md records that (cute-full-sum-evaluates-its-divisor-early). Calling the function after the proofs instead would let the divisor's code invalidate them before the launch (the operand's storage, device or length), so they would have to run a second time on each answered call, and the sum would still be read after the divisor. Both forms are reproduced on the audited build (Testing), which no CI job runs. The reciprocal. The helper's kernel multiplies the sum by a factor, and `_reciprocal_factor` computed it as one over the divisor's FP32 value, in FP32. That was ATen's formula until pytorch/pytorch#169507 (December 2025). Since then `div_true_kernel_cuda` takes the reciprocal in FP64 and narrows it, inv_b = static_cast<opmath_t>( high_prec_t(1.0) / iter.scalar_value<high_prec_t>(2)); with `high_prec_t = double`, and multiplies in FP32 (`MulFunctor<opmath_t>`): aten/src/ATen/native/cuda/BinaryDivTrueKernel.cu at the audited revision (PyTorch main 6d5f66bd5b) and at both earlier pins (a970aa8b98, e2dee2a4b9). The pins' audit covered the reduction sources, not this kernel; the pin's comment in helion/runtime/cute/paired_sum.py now names it for the next one. The two reciprocals differ for a divisor that is no FP32 number of at most 24 bits: for 1.1 ATen multiplies by 0.9090909361839294 and the helper by 0.9090908765792847. So on the one build where the helper answers, a scaled sum could differ from the statement in its last bits; the divisors of the existing tests (3, 7, 0.25, 1037, row counts) are FP32 numbers, for which both agree. Measured on a B200 with torch 2.15.0+cu132, which has the same kernel: of 40000 random `float` divisors 10536 tell the two apart, of 25000 random `int` divisors 5997, and on 300 of each ATen's quotient of an FP32 one was the narrowed FP64 reciprocal every time. On the audited build itself the helper's scaled sum of 4096 random FP32 numbers differed from the statement's for 1418 of 2400 sums at the parent commit (400 tensors by the divisors 1.1, 0.03, 16.1, 2**24 + 1, 3 and 1037), and for none here. `_reciprocal_factor` now takes the reciprocal that way. Two more things about the value it reads: - Of an instance of a subclass of `int` or `float` it reads the value that the instance stores (`int.__index__`, `float.__float__`), which is the value ATen divides by (it takes the stored double of whatever passes `PyFloat_Check`, and an `int`'s digits). It asked `float()` of the instance, which a subclass may answer with another value. - It takes an `int` from -2**63 up to 2**64, the integers that ATen wraps (measured: it raises for -(2**63) - 1 and for 2**64). The helper had a reciprocal for every `int` that `float()` converts, so it answered where the statement raises; such a divisor now declines. `try_full_sum` keeps `divisor`, the value, next to `divisor_fn`: a standalone file that helion/autotuner/aot_compile.py wrote before this change passes it, and keeps the behavior it had. The tests of this rewrite read generated host code, yet five of them were skipped on every build but the audited one, and the module ran in the GPU lane. test/test_cute_host_full_sum.py is now a `cpu_only` module that renders on CPU: its codegen tests run on every build, without a GPU, and its `cuda` items run on the device; those of them that launch the helper's kernel still need the audited build. The device half of the helper's decline test is a `cuda` item of its own, which now holds the rank case as a tensor that passes every other proof. The short-tensor fallback test, whose sum the helper declines on every build, no longer asks for the audited one. Compatibility: CuTe only. Generated host code changes for a full sum whose statement reads its target (the answer's own name; a later site of that kernel that needs a name of its own takes the next free one) and for a scaled sum (`divisor_fn=lambda: ...` where it had `divisor=...`). Other kernels render as before. On the audited build a scaled sum that the helper answers is multiplied by the reciprocal that ATen multiplies by, which changes its last bits for divisors that are no FP32 numbers of at most 24 bits; a divisor that is an instance of a subclass of `int` or `float` is taken by the value it stores, and an `int` that ATen raises for is left to the statement. Tests (test/test_cute_host_full_sum.py): - The two rebinding forms render the answer under its own name; the rendered statements, run on CPU with a stand-in helper, leave the original statement's result when it declines and its answer when it does not (one render per kernel). - The rendered statements of a scaled sum, replayed from the render that the existing divisor test makes, over an operand that counts the reads of its size: one read whether the stand-in helper declines or answers. - The helper on CPU: with the build declined, or under a function, a dispatch or a device mode, it does not read a divisor function; with the build admitted and no mode it reads it once before a proof declines the call (the known gap's second form, which the test pins); the reciprocal of plain numbers, of 1.1 and 2**54 + 2**30 + 1 (which tell the formulas apart), of -2**63, 2**63 and 2**64 - 1, of a `float` subclass whose `float()` doubles its value and of an `int` subclass whose `float()` and index add one; none for a `bool`, a tensor, a string, a complex number, -(2**63) - 1, 2**64 and 1 << 80. - The helper on the device with a stand-in launcher, so on every build: a divisor function is read once and gives the same launch as the value; the two subclasses launch with the reciprocal of what they store; a tensor or `bool` divisor and a mean with a divisor decline, as do the tensors the helper does not take (short, misaligned, strided, rank-2, fp16, grad-requiring). - On the audited build (skipped elsewhere): for 19 divisors (1.1, 0.03, 16.1, 2**24 + 1, 2**54 + 2**30 + 1, 2**63 + 2**40 + 1, 1e300, 1e-300, both zeros and the two subclasses among them) ATen's quotient of one is the factor, and its quotient of 4096 random numbers is their FP32 product with the factor; ATen raises for -(2**63) - 1 and 2**64. The test of the helper's own kernel on divisors gets 1.1 and 2**24 + 1. - On the device both rebinding kernels, with a sum the helper declines on every build, return what the original statements compute. Testing: B200, CUDA 13.2. On the audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment), the six test modules of the host sum helpers on both lanes with GPU 6 (test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py, test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py, test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py): GPU lane 38 passed, CPU lane 201 passed, none skipped, so the helper's kernel ran its tests, the ATen comparison of the reciprocal among them. The known gap's forms, run there: an answered call whose divisor function zeroes the operand returns 0 where the statement gives 64; a call on a CPU, a strided or an fp16 tensor reads its divisor once and declines; under a device mode the divisor is not read. On torch 2.15.0+cu132, which the lanes of this stack run on, `scripts/run_tests.py --backend cute --gpus 4 test/test_cute_host_full_sum.py`: GPU lane 4 passed, 34 skipped (the tests that need the audited build); CPU lane 18 passed. test/test_autoreview.py, which checks the form of the entries of KNOWN_GAPS.md: 157 passed. With the lowering and the helper of the parent commit, the tests of the rebinding forms, of the scaled sum's rendered call and reads and of the reciprocal fail. `./lint.sh check` passes. Lanes at the tip of the stack, which holds this commit, on origin/main 9a31efc: `scripts/run_tests.py --backend cute --gpus 0,4`, CPU lane 27430 passed, 689 skipped, 1 failed (test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition, which fails at 9a31efc as well); GPU lane 16014 passed, 2384 skipped, 3 failed on a timeout while other suites loaded the machine (test_cute_standalone.py::test_native_source_edit_without_helion, test_examples.py::TestExamples::test_linear_monkeypatch_gated_delta_rule, test_handoff_bundle.py::test_namedtuple_bundle_replays_in_fresh_process) and passed when run again alone. `scripts/run_tests.py --backend triton --gpus 1,4`, GPU lane 3408 passed, 2411 skipped, 2 failed (test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and ::test_task_accumulators_get_their_own_rings, which fail at 9a31efc as well); CPU lane 5168 passed, 632 skipped. Review notes: - Deferred: "an answered call evaluates the divisor before the sum" and "a call that evaluated the divisor can still decline". KNOWN_GAPS.md, cute-full-sum-evaluates-its-divisor-early. At origin/main (f615a9b) the rendered call is `divisor=x.size(0)`: the replay above reads the divisor before the helper on every call and again in the fallback of every declined one, on every build. This commit leaves both forms on the audited build as they were, outside a mode of the caller, and removes the second on every other build and under such a mode. - Not taken: "run the proofs again after the divisor". The helper calls the function ahead of its proofs, where the argument was evaluated, so the divisor's code cannot invalidate a proof that already ran. autoreview passed: claude, codex stack-info: PR: #4816, branch: choijon5/cute-norm-bwd/6
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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 that get faster: none; a move of test helpers, with no change to the compiler or to generated code. 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. Four test modules share the harness that test_cute_resident_reductions.py defined. test_cute_resident_pipeline_depth.py, test_cute_resident_serial_output.py and test_cute_resident_local_tree.py imported `_source`, `_rewrite`, `_namespace` and `_Tensor` from it, and two of them imported `_run_pipeline` from the pipeline depth module. Shared test helpers belong in helion/_testing, and the commits above this one change the harness. The definitions move to helion/_testing/cute_resident_reductions.py as they were, with four `cast`s for the type checker, which reads helion/ and not test/. The serial output test patched `_namespace` on the pipeline depth module, where `_run_pipeline` read it; it patches the new module. Testing: the four modules under HELION_BACKEND=cute without a GPU, 1326 passed, the count before the move (they hold no GPU test). `./lint.sh check` on the five files 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: #4817, branch: choijon5/cute-norm-bwd/7
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), origin/main (f615a9b) -> this commit: - 2560 features: 352.5 us -> 37.5 us - 6144: 2294.6 us -> 68.6 us - 7680: 2285.6 us -> 95.4 us - 9216: no CuTe result -> 91.3 us - 10752: no CuTe result -> 233.3 us - 12800: no CuTe result -> 310.0 us - 14336: no CuTe result -> 173.1 us - 15872: no CuTe result -> 611.4 us 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 next three commits of the stack seed and reload these rows (their thread counts, 4-wide vectors for 32-bit rows, rows too wide for the registers), which the widths from 10752 on still need. A row that is a power of two renders the code it rendered (641 of 641 configs of five shapes compared). The times are the autotuner's, of the kernel alone. TritonBench's speedup figure times the Python call, which at these sizes moved by tens of percent between runs on this shared machine (2560 features: 0.46x to 0.80x of the best baseline at the same kernel time), so it is not quoted here. The normalization backward kernels (examples/layer_norm.py::layer_norm_bwd, examples/rms_norm.py::rms_norm_bwd) keep one accumulator of a row's length per CTA and are fastest on the resident reduction schedules, which hold a row in registers across its two passes. Those schedules ran only rows whose length is a power of two. At any other length the kernels fell to the computed fragment owner, which keeps the row in shared memory: about 20x slower at 6144 and 7680 fp32 features, and from 9216 features on no config compiled at all ("computed fragments need 294976 shared bytes"), so TritonBench's layer_norm-bwd had no CuTe result for five of its ten inputs. The default config was refused at every such length ("reduction marker is nested in a different lane owner"), so the kernels did not compile without a search. Two things kept such rows off the resident schedules: - A factory tensor of static dims is padded to powers of two, so `weight.new_zeros(n)` is typed 4096 for n = 2560. An op between it and `x[tile, :]` pins the slice's dim to 4096, and the reductions over the pinned values take a second dim: the feature axis has two block ids, and the resident plan needs one. `Backend.joins_factory_extents` (default off) lets a backend have device IR type the factory tensors on the dim of the full slices instead, as `hl.zeros([tile, n])` already is (`CompileEnvironment.renew_factory_pinned_extents`, next to the renewal of pinned subscript extents). An uninitialized tensor of a joined extent (`torch.empty`, `new_empty`) is created as zeros, because Inductor lowers `aten.empty` from a literal size. A factory that asks for a memory format other than the contiguous default is not joined: the zeros twin takes none. (A kernel cannot spell a memory format, type propagation refuses the value; the mode guards the call all the same.) CuTe asks for the join only for the loop nest the resident family schedules, with nothing else in it (`serial_row_nest`): one root loop over one row loop, a `for` loop over `hl.tile` or `hl.grid`, whose calls are of a closed list. A `while` loop over the rows keeps the padded literal: the family plans none, and the lowering that runs one refuses the joined dim ("lane reduction under a guard whose thread uniformity cannot be proven"), where origin/main renders it. A `for` loop over a Python sequence keeps the padded literal as well: type propagation marks it as a device loop, but the kernel unrolls it and no row loop is left. The list holds the loops' tiles, the language's tensor factories, Python's own functions, and, as torch functions and as tensor methods, elementwise math, reductions over a dim, casts and tensor factories. The operation decides and not its spelling, so the callee is read from its type: a matrix product in any form (`@`, `torch.mm`, a `Tensor.mm` that the kernel saved under a name, `torch.addmm`, `torch.baddbmm`), an index tensor (`hl.arange`, `torch.arange`), every other callable of the language (`hl.dot`, `hl.tile_index`, atomics) and a helper function, whose body the host AST does not show, keep the padded literal, which their lowerings read. A call in the branch of a constant `if` that type propagation skips is not part of the nest: layer_norm_bwd without a bias (`compute_bias_grad=False`) is the same nest as with one. - A persistent row is padded to a power of two and masked. A resident row now runs unpadded on the threads its config names when they tile it: whole warps of any count, or a power of two of at most one warp (`resident_row_layout`). The row's sums over the CTA combine the warps' partials in subgroups of the next power of two, whose members past the warp count bring zero (`_cute_resident_sums`, `_cute_resident_sums_disjoint`). The joined dim is every config's of such a kernel, not the resident ones' alone: the default config now renders, walking the row in masked lanes of its own length, and matches eager autograd. The lowering refuses what these rows newly reach: - the local summation tree on a thread whose lanes are no power of two (it pairs complete subtrees; 128 threads over 2560 features carry 20 each); - a staged row that is no whole number of its copies (16 bytes each, or the whole of a shorter row): the last copy would run into the next slot; - row stores whose Int32 offsets could wrap into each other. The proof that the rows of a store are apart relied on the row's span dividing 2**32, which a power of two does; for any other span, no difference of two rows may come within a span of 2**32. The heuristic's plan accepts a row length that is no power of two, so the existing seed families apply where they tile the row; the local-tree twins are planted only where the tree is complete, and the 8-wide vector rows only for rows of whole vectors. Compatibility: CuTe only. A kernel of this nest whose row length is no power of two now has one reduction dim for the row instead of two, so its per-block config lists (num_threads, cute_vector_widths, ...) are one entry shorter; configs saved for such a kernel by an older version do not transfer. These kernels had no resident config before, their default config did not compile, and at 9216 features and above none did. The resident schedule knobs are no longer inert for them, so a random config of their search space renders as often as for a row that is a power of two, which is less often than before (layer_norm_bwd at 4096 x 2560 fp32: 60 of 300 random configs against 119 of 300; the others are refused as "resident reduction schedules cannot share a computed fragment root"), while every compiler seed renders (14 of 14, against 0 of 2). With `static_shapes=False` the accumulators' dim is typed by the padded literal itself: nothing is joined and such a binding compiles as before. Triton and Pallas are unchanged: the hook's default keeps the padded literal, and the factory change acts on joined extents only. The documentation of the `cute_unpadded_rows` option now says that a resident row does not read it. Tests: - test/test_cute_resident_row_widths.py (new; CPU, with `cuda` cases): the nest's IR has one dim for the row in both examples and in layer_norm_bwd without a bias, and other nests keep the padded literal (a second row loop, `hl.tile_index`, `hl.arange`, `torch.arange`, a helper call, `@`, a saved `mm` method, a `while` loop over the rows, whose default config renders as it did, a `for` loop over a Python tuple in the row loop's place, and a loop without a row loop); the functions and methods a nest may call; every padded factory takes the joined dim, and one with a memory format keeps its padded literal; the default config renders; the thread counts that run a row unpadded and those that are refused; the local tree and the staged row of whole copies. On the device both examples match eager autograd at 2560, 5632, 640 and 96 features (fp32 and bf16): staged, unstaged, in a four-row group, on the serial row schedule and under the default config. So do layer_norm_bwd without a bias and a kernel with an accumulator of every tensor factory. A CPU test renders each device case and adds up its static shared memory: the staged ones stay within the 48 KiB that libNVVM compiles below sm_90, which the per-block check holds a stage to there (the bottom commit of the stack), so no case asks a device for a stage it refuses. - test/test_cute_resident_reduction_runtime.py: the sums of blocks of 3, 5, 15 and 31 warps, with one and three statistics, in both sum helpers, interpreted on CPU. - test/test_cute_resident_reductions.py: the Int32 row-store proof for a span that is no power of two, and the staged row of whole copies. - `test/shard_durations.json` records the new module (13.8 s in the CuTe GPU lane). Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27366 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); the three modules of the Tests paragraph and test/test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1284 passed. 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 and the same 1 failed; `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, ptxas C7600, the same at origin/main), CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. Earlier runs, on origin/main 57009cb and before the three fixes at the bottom of the stack went under this commit (they change nothing on this host). The whole GPU lane at this commit, before its device cases shrank: `scripts/run_tests.py --backend cute --gpus 2,6`, 16013 passed, 2370 skipped. Differential fuzz against eager autograd on this B200: the compiler seeds, the default config and 60 random configs of nine cases (layer_norm_bwd, 100 rows of 2560, 1000, 1002, 2016 and 4096 fp32 features, 2560 bf16 and 2564 fp16; rms_norm_bwd, 100 rows of 2560 fp32 and 6144 bf16): 339 configs compiled and matched within 2e-3 (2e-2 for the 16-bit dtypes), 351 were refused by the compiler, none was wrong and none crashed. The cold autotunes of the lead ran with origin/main at f615a9b and this commit on it, before #4763 to #4767 landed, which leave these kernels as they were: at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767. The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4818, branch: choijon5/cute-norm-bwd/8
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
… of two 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: the same rows as the commit below this one (examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two): the search now starts from the thread counts that measure fastest, where it started from a lattice of powers of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), the commit below -> this commit: - 7680 features: 95.4 us -> 85.6 us - 10752: 233.3 us -> 105.4 us - 12800: 310.0 us -> 138.2 us - 15872: 611.4 us -> 223.4 us 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. 2560, 6144, 9216 and 14336 features end where they did (37.4, 69.2, 91.2 and 167.9 us against 37.5, 68.6, 91.3 and 173.1). The resident reduction seeds come from a lattice of power-of-two thread counts. A row whose length is no power of two gets an awkward share per thread from them, or none: 7680 features are 60 a thread over 128 threads and tile neither 256 nor 512 threads in 8-wide vectors, and 9216 over 128 threads are 72 a thread, whose carries and row values leave the registers. The search's thread counts are powers of two as well, so no neighbor of a lattice seed reaches the counts in between. The previous commit lets such rows run on the resident schedules; this one seeds them. Pinned layouts of examples/layer_norm.py::layer_norm_bwd on a B200 (4096 rows, fp32, kernel time in us). "staged" is the pipelined schedule, x2 and x4 its row groups, and the last column the count that gives a thread 16 features: features 256 threads 512 threads 16 features a thread 2560 23.5 staged x4 33.1 staged 33.2 staged (160 threads) 6144 48.7 staged x2 - 52.4 staged (384) 7680 63.0 staged 63.4 staged 65.3 staged (480) 9216 71.0 staged - 73.8 staged (576) 10752 81.5 staged 84.3 staged 131.8 staged (672) 12800 121.1 unstaged 150.0 unstaged 151.7 unstaged (800) 14336 150.3 unstaged 152.9 unstaged - 15872 222.0 unstaged 200.2 unstaged 198.4 unstaged (992) 256 threads are the fastest count up to 10752 features: each thread then has the 255 registers a thread can address, and the block the whole register file of its SM. The staged schedule wins while the registers hold a thread's values; from 12800 features on they do not (50 features a thread, five values each), and the unstaged schedule is faster. At 15872 features the stage does not fit two rows, and half the features a thread run 10% faster. - `_tiled_threads`: the whole-warp thread counts that tile the row nearest to 256 and to 512. - `_tiled_entries`: for each of them a seed with the widest of 4-, 2- and 1-wide vectors that tiles a thread's features. The first count leads with the largest staged row group of four and two rows that the stage holds (four-row groups ran 2560 features 28% faster than single rows, two-row groups 6144 features 7%), then each count is seeded staged with single rows and unstaged. No count is seeded staged when the stage does not hold two rows, so that the primary seed is not one whose stage the shared memory refuses. That holds for the estimate's reading of a row: the kernel's row tensors are as wide as the row and each is loaded once. It is no promise that the seed renders: the lowering stages only the loads it proves aligned to 16 bytes, and refuses a staged seed when it proves that of none. - `_staged_rows`: an estimate of the rows of features that the target's shared memory stages, from the tensors the kernel loads whose last dim is the row's length. The shared memory is what a block of the device opts in to and, below sm_90, no more than the 48 KiB of static allocations that the per-block check holds a stage to there (the bottom commit of the stack). It shapes the seeds only: the lowering charges each config's stage exactly and refuses the one that exceeds the shared memory. - The entries lead the family's list for such a row, before the lattice's, and get the occupancy twins that the lattice's entries get. A twin's row group is at most the rows its CTA runs, as the 8-wide entries' is (256 rows on 148 SMs are two rows a CTA: a group of two, where four would stage masked rows). A row that is a power of two keeps its seeds: `_tiled_entries` returns none for it. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel whose rows are no power of two changes, and with it the fingerprint of its autotune cache entry. Tests (test/test_cute_resident_row_widths.py, CPU): the thread counts of six row lengths, among them one that 256 and 512 threads do not tile and one that no whole warps tile; the entries for stages of eleven, four, three and one rows and for an unknown stage; the staged-row estimate of layer_norm_bwd at three fp32 widths and one bf16 width, and for a model of an sm_80 target, which stages two rows of 2560 fp32 features where sm_100 stages eleven, and whose primary seed is then the one-row stage and renders; the seeds of layer_norm_bwd at 2560 features; the row group of an occupancy twin at 256 rows; and at 15872 features a render of the primary seed, whose staged twin the lowering refuses and renders with the shared memory doubled. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27376 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); test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py and test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1294 passed. The seeds are read on a device by the tests of the GPU lane, which 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead ran with this commit on origin/main f615a9b, before #4763 to #4767 landed and before the estimate took the static limit below sm_90; none of these changes a seed of these inputs on this device (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4819, branch: choijon5/cute-norm-bwd/9
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction kernels whose rows are 32-bit: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd with fp32 inputs. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, origin/main (f615a9b) -> this commit: - layer_norm-bwd (4096, 4096): 56.9 us -> 49.8 us - layer_norm-bwd (4096, 1024): 24.8 us -> 24.7 us - rms_norm-bwd (2048, 1024), (2048, 2048), (2048, 4096) and (2048, 16384): 14.8, 19.5, 30.0 and 93.2 us -> 14.6, 19.1, 29.6 and 93.2 us - rms_norm-bwd (2048, 32768): a search ends in one of two kinds of layout at either commit. origin/main, four searches (the nightly's search seed at f615a9b, seeds 1 and 2, and the nightly's seed again at 57009cb): 365.4, 321.6, 324.6 and 807.4 us. This commit, three searches (the nightly's seed, then seeds 1 and 2): 809.4, 242.8 and 802.5 us. The next commit seeds a layout that reloads this row, and its search ends at 201.8 us 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 five other inputs up to 16384 features end where they did, within 0.4 us: the vector width is a knob, which the search moves from the 8-wide seeds by itself. Pinned, the seeds take 21% to 29% less kernel time 4-wide than 8-wide (4096 rows of layer_norm_bwd): 13.9 -> 9.9 us at 1024 features, 25.6 -> 20.1 us at 2048, 45.5 -> 34.3 us at 4096 and 88.0 -> 64.9 us at 8192. Kernels with 16-bit rows keep their seeds. At 32768 features of rms_norm_bwd no seed holds the row in its threads' registers, at origin/main or here: the search starts from seeds that spill (the 256-thread one runs 2267 us) and ends where its random path takes it: in a layout of 240 to 365 us, or in one of about 805 us, which is where one of origin/main's four searches and two of this commit's three ended. The twin that this commit adds at that width spills as well. The layout that origin/main's first search found renders the same kernel at this commit and runs 365 us pinned, as the search times it (the call with its host sum; the kernel alone takes 319.6 us of it). The commit above seeds a reloading layout for this row (195.7 us of kernel time), which a search cannot end behind, so the two are best landed together. The resident seed lattice's 256- and 512-thread entries and the vector rows carry 8-wide feature vectors: 16 bytes of a 16-bit row, which is one async copy or one vector load. Of a 32-bit row 16 bytes are 4 features, and an 8-wide vector is two copies. The 4-wide layout took 21% to 29% less time on fp32 rows (B200, examples/layer_norm.py::layer_norm_bwd, 4096 rows, staged, kernel time in us): features threads row group 8-wide 4-wide 1024 128 4 13.9 9.9 2048 256 2 25.6 20.1 4096 256 2 45.5 34.3 4096 512 2 47.4 35.7 8192 256 1 88.0 64.9 8192 512 1 86.0 65.1 For a kernel whose rows are all 32-bit (`_packet_vector`: the features in 16 bytes of the tensors the kernel loads whose last dim is the row's length), the family repeats its 8-wide entries 4-wide. The twins follow the existing seeds, which keep their rank, and a layout the list already holds is not repeated. A kernel of 16-bit rows keeps its seeds, and so does one whose rows' element sizes differ or are unknown. A 32-bit row is twice the bytes of a 16-bit one, so a staged twin takes the largest row group, of its entry's halved down to one, whose two slots the shared memory holds (`_staged_rows`), and no staged twin is seeded when not even one row's slots fit. Of 4096 fp32 features the stage holds seven rows: the four-row group of 512 threads becomes two. Of 8192 it holds three: every staged twin is one row. Those are the layouts of the table, which all render. Of 16384 features the twins are the unstaged ones. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel with 32-bit rows grows, and with it the fingerprint of its autotune cache entry changes. Tests (test/test_cute_resident_packet_seeds.py, new, CPU): the packet of fp32, bf16 and fp16 rows; for fp32 rows of 1024, 2048, 4096, 8192 and 16384 features the seeds are the previous ones followed by the listed 4-wide twins, in order; at 8192 features the one-row staged twin renders, and its two-row form, which the lowering refuses, renders with the shared memory doubled; a bf16 row's seeds are unchanged. The module holds no GPU test, so `test/shard_durations.json` has no entry for it. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27386 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); the new module and the four resident row modules beside it (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py) on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1304 passed. The commit changes seeds only; the tests that read them on a device 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead: origin/main at f615a9b and at 57009cb (seeds 1 and 2, and the nightly's seed again for the widest rms_norm-bwd input), this commit on each; #4763 to #4767, which landed in between and since, leave the seeds and the code of these kernels as they were (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4820, branch: choijon5/cute-norm-bwd/10
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction rows too wide for their threads' registers: examples/layer_norm.py::layer_norm_bwd from 12800 fp32 features and examples/rms_norm.py::rms_norm_bwd at 32768. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, the commit below -> this commit: - rms_norm-bwd (2048, 32768): 809.4 us -> 201.8 us (origin/main: 321.6 to 807.4 us over four searches) - layer_norm-bwd (4096, 12800): 138.2 us -> 126.1 us - layer_norm-bwd (4096, 14336): 171.0 us -> 134.1 us 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 other inputs end where they did, 10752 and 15872 features among them (103.5 us -> 105.5 us and 222.2 us -> 218.0 us). Each of the three searches now ends at a reloading layout that the commit seeds: pinned, the best layout that holds the row's values against the best that reloads them is 121.2 us -> 103.5 us of kernel time at 12800 features of layer_norm_bwd, 151.2 us -> 111.5 us at 14336, and 319.6 us -> 195.7 us at 32768 of rms_norm_bwd, where the holding layout is the one that origin/main's first search found. A layout whose threads hold their row gains nothing from a reload (layer_norm_bwd at 10752 features over 256 threads: 81.6 us held, 84.2 us reloading), and such a seed gets no reloading twin. The resident reduction schedules run a row in two passes. The first reduces it; the second reads, for each feature, values the first computed, which a thread holds in register fragments: five values a feature in examples/layer_norm.py::layer_norm_bwd, three in examples/rms_norm.py::rms_norm_bwd. A thread addresses 255 registers and shares the 65536 of its SM with the other threads of its block. Past that the values go to the thread's local memory, and both passes take several times as long. No thread count avoids it for a wide row: 1024 threads have 64 registers each. - `cute_reduction_reloads` "gmem" now applies to the resident schedules (`ResidentReductionOptions.reload_blocks`). The second pass of such a row holds none of the first pass's values in registers: it runs the statements that computed them again, on the row's loads, which come from the stage under the pipelined schedule and from global memory, where the row is still in L2, under the resident one. The knob is the per-block one that the rolled reductions read, with the meaning it has there. The other modes, among them the default "auto", hold the values as before. - Three rows keep their registers under "gmem" too, so every config that compiled before compiles now. A row of the deferred serial row schedule, which has no barrier after the second pass (the next group's copies would land in the slot the pass still reads). A row whose second pass reads a value computed from a carry, which holds the next row's value by then. And a row whose reload fails a proof that the holding pass passes: the statements that run again bring what they read into the second pass, and a row group runs a row's own statements again for each of its rows, which it proves for statements that load nothing and compute an integer or a boolean or assign a floating-point literal, so a group that would run an FP16 value of the row again keeps the holding pass (`_plan` tries the reloading pass and falls back). - A row that the holding pass refuses can compile under "gmem": that pass keeps FP32 values only, and a boolean of the feature that the second pass reads is computed again. - Seeds: where an estimate says that a seed's row passes its threads' registers (`_feature_values`, `_spills`), the family plants a reloading twin after the existing seeds, when there is one. A staged seed keeps its layout, and has no twin when the stage does not hold the two slots of its row group. An unstaged one reads global memory twice, which is faster the more warps load at once: its twin runs on the most whole warps that tile the row in 16-byte vectors, and it has none when no whole warps do. Twins that come out as one layout are seeded once. The estimate places seeds only; the lowering takes any config. Pinned layouts on a B200 (fp32, kernel time in us, best layout of each kind; "held" keeps the values in registers): kernel, rows x features held reloading layer_norm_bwd 4096 x 10752 81.6 staged 84.2 staged layer_norm_bwd 4096 x 12800 121.2 unstaged 103.5 staged layer_norm_bwd 4096 x 14336 151.2 unstaged 111.5 staged layer_norm_bwd 4096 x 15872 198.4 unstaged 205.5 unstaged rms_norm_bwd 2048 x 16384 86.4 unstaged 85.4 unstaged rms_norm_bwd 2048 x 32768 319.6 unstaged 195.7 unstaged At 10752 features 256 threads hold the row (42 features a thread) and a reload gains nothing: those seeds get no twin. The lattice's 128-thread staged seed carries 84 features a thread there and gets one, as it does from 6144 features on. At 15872 the stage does not fit two rows, and the unstaged reload is on par. At 32768 features of rms_norm_bwd the held layout is the one that origin/main's first search found (1024 threads in two-row groups, one row a CTA); the family's own held seed of 256 threads runs 2267 us. Reloading from global memory is slower than holding wherever the registers fit (7680 features over 256 threads: 196.5 us against 77.7 us), which is why only "gmem" selects it. Compatibility: CuTe only. A config with a resident schedule and "gmem" for the row's reduction rendered the holding pass before and renders the reloading one now: the same results from other code. The family's seeds said "register", and "auto" is unchanged, so no seed or default config renders differently. A kernel gets compiler seeds: the reloading twins of its seeds whose row passes their threads' registers by the estimate (`_spills`), where a twin exists (the Seeds item above) and its coarse tile is within the kernel's block sizes, each layout once. They change the fingerprint of its autotune cache entry. The rule reads the features a thread carries, so no width is a threshold. Examples, fp32, at the row counts of TritonBench: layer_norm_bwd gets the twin of its 128-thread staged seed at 6144, 7680, 9216 and 10752 features (48 features a thread and more) and none at 5120, four twins at 12800 and 14336, one at 15872 and 16384, and two at 3104, which only 32 threads tile; rms_norm_bwd gets none at 6144 and two from 7680 (60 features a thread), staged up to 8192 and unstaged at 16384 and 32768. Each unstaged twin is also seeded with the local tree. A resident config with "gmem" can now compile a row that the holding pass refuses (above). The one change outside the backend is the docstring of `CuteReductionReloadSpec` in helion/autotuner/config_spec.py, which now describes the knob on the resident schedules: the rows that keep their registers under "gmem" and the row that compiles under it alone. Tests: - test/test_cute_resident_reductions.py runs the rewritten loop nest on CPU against the loop's closed form: a reloading second pass holds no register fragment and loads each row twice from global memory, or once when staged (two widths, two row groups, both schedules); a value computed from a carry stays in registers; a row group that would run an FP16 value of the row again keeps the holding pass, where a group of one row reloads (both rewrites are checked to be the resident form); a row whose second pass reads a boolean of the feature is rewritten under "gmem" only, and matches the closed form. - test/test_cute_resident_row_widths.py renders both examples: the reloading row's fragments are gone and its vector loads double (12800 features staged, 32768 unstaged); a deferred row keeps its registers; `_spills` at the boundaries of the two limits; the reloading seeds of both examples at six widths (12800, 10752, 6144 and 3104 features of layer_norm_bwd, 32768 and 7680 of rms_norm_bwd) and the four widths that seed none; the twins follow the previous seeds, which keep their rank. Three `cuda` cases run reloading rows against eager autograd; the staged one is 2048 features wide, and a CPU test adds up the static shared memory of each case: at most the 48 KiB that libNVVM compiles below sm_90. - test/test_cute_resident_packet_seeds.py leaves the reloading seeds out of the lists it compares. - Differential fuzz against eager autograd on a B200, at this commit before the two fixes at the bottom of the stack went under it (the per-block check of a stage and the device of the reduction budget, which change nothing on this host): the compiler seeds, 10 random configs and 80 random resident configs with "gmem", for each of eleven cases (layer_norm_bwd at 100 x 1000, 100 x 2560 and 100 x 4096 fp32, 100 x 2560 bf16, 64 x 12800 fp32 and 64 x 7680 fp16; rms_norm_bwd at 100 x 2560 and 100 x 4096 fp32, 100 x 6144 bf16, 64 x 16384 fp16 and 64 x 32768 fp32). 750 configs compiled and matched, 407 of them with a reloading pass; the compiler refused 595; none was wrong and none crashed. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27408 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); the five resident row modules (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py, test_cute_resident_packet_seeds.py) on both lanes with GPU 4, GPU lane 13 passed, CPU lane 1326 passed. The GPU lane ran at the tip of the stack, the commit above this one, which adds the Parameter guard in other files: `scripts/run_tests.py --backend cute --gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped. For the docstring in helion/autotuner/config_spec.py, the Triton and Pallas interpreter lanes ran at the tip 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, ptxas C7600, the same at origin/main); CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. The cold autotunes that the lead gives for this commit ran at the tip of the stack on origin/main 57009cb, before #4766 and #4767 landed and before the fixes at the stack's bottom were reworked: the commit above changes the host path of a launch and no kernel, those fixes nothing on this host, and #4766 and #4767 nothing in these kernels (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4821, branch: choijon5/cute-norm-bwd/11
choijon5
force-pushed
the
choijon5/cute-norm-bwd/11
branch
from
November 29, 2026 20:09
94786f3 to
a8366f4
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 29, 2026 20:10
f913fea to
886e30f
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: every kernel with a host sum tail that a search runs with `cute_host_paired_sum` "mapped" or "narrow", on any Torch build but the one audited one: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel gets back the host time of proofs that could not succeed: 73.7 us to 54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below). No generated kernel changes, and no launch takes another time on the device. TritonBench times `layer_norm-bwd` and `rms_norm-bwd` around the Python call, so those readings carried the cost wherever the search picked one of the two values. 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. With `cute_host_paired_sum` "mapped" or "narrow" the generated host function hands its sum tails to a runtime helper (`try_paired_sum_cast`, `try_single_sum_cast`, `try_full_sum`). The helper launches one kernel that reproduces ATen's addition tree, or returns None and the original statements run. The tree is audited for one Torch build (`_compatible_aten_sum`: the build's git version and four header hashes), and the helpers asked for the build last, after the proofs of the dispatch state, the tensors and the plan. On every other build each call of the kernel ran those proofs and then declined: the device ran what "off" runs, and the host paid for the proofs. That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's CuTe test jobs install the current nightly, and the benchmark jobs a release. The autotuner does not see the cost: it times the device, where the three values are the same kernels on such a build, and the compiler seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of them wins by noise then costs every call its proofs. The helpers now ask for the build first. The answer is cached, so a declined call costs a function call. Nothing else changes: an admitted call runs the same proofs and the same kernel, a declined one the original statements, and the generated code is the same. B200, torch 2.15.0+cu132 (not the audited build), examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned layout; host time of the kernel call at the parent commit and at this one, median of 300 calls, in us: cute_host_paired_sum off mapped narrow parent 54.5 73.7 73.4 this commit 54.0 54.1 54.3 A declining call of the helper alone, 100000 calls: paired 12.3 us before and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel picked "mapped" at 4096 features and "narrow" at 6144. One more order is fixed in `try_full_sum`. It computed a divisor's reciprocal, with tensors, before it looked at the dispatch state, so a mode of the caller saw those operations. Under `torch.device("cuda")` every call of a scaled full sum built two device scalars, divided them and synchronized before it declined, which is device work that these calls no longer do; under `torch.device("meta")` the call raised instead of declining. The reciprocal is now computed after that check. Compatibility: CuTe only, no change to generated code, configs or results. On the audited build the first call of a helper reads the four headers even when its other proofs would have declined; that happens once in a process, as before. Two test modules of the helpers had fallen behind the launcher, unseen: test/test_cute_host_paired_sum_abi.py and test/test_cute_host_single_sum_abi.py ran on the audited build only (their skip compared the build's git version), which no CI job installs. - Their stand-in launches reached the launcher's CUDA context check (#4456), which the modules forbid as they do every use of the device, and they asserted the compile options of before the division option. They skip the check for the stand-in launch and assert the launcher's current options. - They run on every build now. What they check (the launches the bound host makes, with their arguments, streams and launchers) does not depend on the installed ATen, so they override the helpers' answer about the build (`_compatible_aten_sum`, and `_compatible_aten_half_cast` for the single sum's FP16 outputs) next to the tensor and dispatch checks they already override, and the skip is gone. The skip was also narrower than the gate it stood for: a build of that git version with other headers, or for ROCm, would have run the tests and failed them. - They are `cpu_only` modules now, which the CPU lane runs without a device. One query on their path needed one: the launcher asks the CUDA runtime whether its stream is being captured, which raises in a process that sees no device. The tests answer it next to the context check (the stand-in launches are eager ones). `test/shard_durations.json` loses the modules' two zero placeholders: a module outside the GPU lane has no entry there. - They no longer depend on the launches before them, nor change the ones after. The launcher keeps the SM count of each device for the process (`_CUTE_NUM_SM_CACHE`): an earlier launch on cuda:0 answered in place of the stand-in count of 148 that the tests assert, and the stand-in count stayed behind for later launches. A B200 has 148 SMs, so neither showed here. The tests run on an empty copy of the cache. On the audited build their 6 tests fail at origin/main and pass here; on every other build they were skipped and now run. Tests: - Three new tests, one for each helper: with the build declined the helper returns None and enters none of its other proofs (they are patched to raise, the query for a function mode included, and the arguments are no tensors). - A new test calls `try_full_sum` with a divisor under a function mode, a dispatch mode and a device mode: it declines without an operation. It fails on the old order. - The tests of the other reasons to decline (CPU tensors, dispatch modes, subclasses and duals, unsupported 1-D tensors) admit the build, so that an unaudited build is not what declines in them. - Two tests made the build check raise, to show that a singleton plan declines before it. They admit the build now and keep the launcher raising, and the single sum its half-cast header check. Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules of the helpers on both lanes at this commit, with GPU 6 (test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py, test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py, test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment): GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main (f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules), CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack run on: GPU lane 9 passed, 40 skipped (the tests that need the audited build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed ones, where they were skipped. The lanes at the tip of the stack, which holds this commit, on torch 2.15.0, run while the two ABI modules were still in the GPU lane: `scripts/run_tests.py --backend cute --gpus 2,3`, GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane 27318 passed, 684 skipped, 1 failed (`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 nightly). `./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. Review notes: - Not valid here: "a declined full sum can clobber its own input" (helion/_compiler/cute/host_full_sum.py, which this commit does not touch). The defect is real and on origin/main; the next commit of the stack fixes it: "[cutedsl] Fix the full host sum's fallback and its divisor's reciprocal". - Not valid here: "five source-generation tests of test/test_cute_host_full_sum.py are gated on the ATen build and sit outside a cpu_only module". It predates this commit, which leaves those tests alone; the same next commit makes the module `cpu_only` and drops the gate from them. autoreview passed: claude, codex
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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; two fixes to what runs when the helper of a full host sum declines, which it does on every Torch build but the audited one. Under `cute_host_paired_sum` "mapped" or "narrow", a kernel whose host tail assigns a full sum to the name of its operand (`out = torch.sum(out)`) or of its divisor (`n = torch.sum(out) / n`) raised a TypeError, and a scaled sum (`torch.sum(out) / x.size(0)`) evaluated its divisor twice. A third fix is to the helper's own arithmetic: it multiplied by another reciprocal than ATen does for divisors such as 1.1. 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. With "mapped" or "narrow" a full host sum goes to a runtime helper (`try_full_sum`), and the original statement runs when the helper returns None. Two things kept that statement from running as it was written. Its operand. The rewrite assigned the helper's answer to the statement's target: out = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if out is None: out = torch.sum(out) so the fallback read None where the statement reads its target. `lower_host_full_sum` now gives the answer a name of its own for such a statement and assigns the target from it when the helper answered: _helion_full_sum = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if _helion_full_sum is None: out = torch.sum(out) else: out = _helion_full_sum Its divisor. The call took the divisor as an argument (`divisor=x.size(0)`), so every call evaluated it before the helper decided, and the fallback of a declined call evaluated it again. The proof admits tensor metadata in a divisor (`x.size(0)`, `x.numel()`, `x.shape[0]`) and called that safe to evaluate twice. A function mode of the caller sees those calls, and so does a tensor subclass with `__torch_function__`: on a declined call each saw one more than the statement makes, ahead of the sum, where the helper's rule is that a declined call leaves the exact original call. This predates the stack (d32e9ca, the divisor support). The site now hands the divisor over as a function, total = _cute_try_full_sum(out, mean=False, divisor_fn=lambda: x.size(0), _launcher=_launcher) and `try_full_sum` does not call it on a build whose sums it declines, nor under a function or dispatch mode of the caller. There the statement evaluates its divisor once, in its own order: that is every build but the audited one, and every call under a mode. Replayed on CPU over an operand that records the reads of its size, a declined call reads it twice at origin/main (before the helper and in the fallback) and once here. Otherwise, on the audited build, the helper calls the function where the argument was evaluated, ahead of its proofs, so those calls behave as they did: a call that the helper answers has evaluated the divisor before the sum, and a call that its proofs decline has evaluated it once before the statement does so again. KNOWN_GAPS.md records that (cute-full-sum-evaluates-its-divisor-early). Calling the function after the proofs instead would let the divisor's code invalidate them before the launch (the operand's storage, device or length), so they would have to run a second time on each answered call, and the sum would still be read after the divisor. Both forms are reproduced on the audited build (Testing), which no CI job runs. The reciprocal. The helper's kernel multiplies the sum by a factor, and `_reciprocal_factor` computed it as one over the divisor's FP32 value, in FP32. That was ATen's formula until pytorch/pytorch#169507 (December 2025). Since then `div_true_kernel_cuda` takes the reciprocal in FP64 and narrows it, inv_b = static_cast<opmath_t>( high_prec_t(1.0) / iter.scalar_value<high_prec_t>(2)); with `high_prec_t = double`, and multiplies in FP32 (`MulFunctor<opmath_t>`): aten/src/ATen/native/cuda/BinaryDivTrueKernel.cu at the audited revision (PyTorch main 6d5f66bd5b) and at both earlier pins (a970aa8b98, e2dee2a4b9). The pins' audit covered the reduction sources, not this kernel; the pin's comment in helion/runtime/cute/paired_sum.py now names it for the next one. The two reciprocals differ for a divisor that is no FP32 number of at most 24 bits: for 1.1 ATen multiplies by 0.9090909361839294 and the helper by 0.9090908765792847. So on the one build where the helper answers, a scaled sum could differ from the statement in its last bits; the divisors of the existing tests (3, 7, 0.25, 1037, row counts) are FP32 numbers, for which both agree. Measured on a B200 with torch 2.15.0+cu132, which has the same kernel: of 40000 random `float` divisors 10536 tell the two apart, of 25000 random `int` divisors 5997, and on 300 of each ATen's quotient of an FP32 one was the narrowed FP64 reciprocal every time. On the audited build itself the helper's scaled sum of 4096 random FP32 numbers differed from the statement's for 1418 of 2400 sums at the parent commit (400 tensors by the divisors 1.1, 0.03, 16.1, 2**24 + 1, 3 and 1037), and for none here. `_reciprocal_factor` now takes the reciprocal that way. Two more things about the value it reads: - Of an instance of a subclass of `int` or `float` it reads the value that the instance stores (`int.__index__`, `float.__float__`), which is the value ATen divides by (it takes the stored double of whatever passes `PyFloat_Check`, and an `int`'s digits). It asked `float()` of the instance, which a subclass may answer with another value. - It takes an `int` from -2**63 up to 2**64, the integers that ATen wraps (measured: it raises for -(2**63) - 1 and for 2**64). The helper had a reciprocal for every `int` that `float()` converts, so it answered where the statement raises; such a divisor now declines. `try_full_sum` keeps `divisor`, the value, next to `divisor_fn`: a standalone file that helion/autotuner/aot_compile.py wrote before this change passes it, and keeps the behavior it had. The tests of this rewrite read generated host code, yet five of them were skipped on every build but the audited one, and the module ran in the GPU lane. test/test_cute_host_full_sum.py is now a `cpu_only` module that renders on CPU: its codegen tests run on every build, without a GPU, and its `cuda` items run on the device; those of them that launch the helper's kernel still need the audited build. The device half of the helper's decline test is a `cuda` item of its own, which now holds the rank case as a tensor that passes every other proof. The short-tensor fallback test, whose sum the helper declines on every build, no longer asks for the audited one. Compatibility: CuTe only. Generated host code changes for a full sum whose statement reads its target (the answer's own name; a later site of that kernel that needs a name of its own takes the next free one) and for a scaled sum (`divisor_fn=lambda: ...` where it had `divisor=...`). Other kernels render as before. On the audited build a scaled sum that the helper answers is multiplied by the reciprocal that ATen multiplies by, which changes its last bits for divisors that are no FP32 numbers of at most 24 bits; a divisor that is an instance of a subclass of `int` or `float` is taken by the value it stores, and an `int` that ATen raises for is left to the statement. Tests (test/test_cute_host_full_sum.py): - The two rebinding forms render the answer under its own name; the rendered statements, run on CPU with a stand-in helper, leave the original statement's result when it declines and its answer when it does not (one render per kernel). - The rendered statements of a scaled sum, replayed from the render that the existing divisor test makes, over an operand that counts the reads of its size: one read whether the stand-in helper declines or answers. - The helper on CPU: with the build declined, or under a function, a dispatch or a device mode, it does not read a divisor function; with the build admitted and no mode it reads it once before a proof declines the call (the known gap's second form, which the test pins); the reciprocal of plain numbers, of 1.1 and 2**54 + 2**30 + 1 (which tell the formulas apart), of -2**63, 2**63 and 2**64 - 1, of a `float` subclass whose `float()` doubles its value and of an `int` subclass whose `float()` and index add one; none for a `bool`, a tensor, a string, a complex number, -(2**63) - 1, 2**64 and 1 << 80. - The helper on the device with a stand-in launcher, so on every build: a divisor function is read once and gives the same launch as the value; the two subclasses launch with the reciprocal of what they store; a tensor or `bool` divisor and a mean with a divisor decline, as do the tensors the helper does not take (short, misaligned, strided, rank-2, fp16, grad-requiring). - On the audited build (skipped elsewhere): for 19 divisors (1.1, 0.03, 16.1, 2**24 + 1, 2**54 + 2**30 + 1, 2**63 + 2**40 + 1, 1e300, 1e-300, both zeros and the two subclasses among them) ATen's quotient of one is the factor, and its quotient of 4096 random numbers is their FP32 product with the factor; ATen raises for -(2**63) - 1 and 2**64. The test of the helper's own kernel on divisors gets 1.1 and 2**24 + 1. - On the device both rebinding kernels, with a sum the helper declines on every build, return what the original statements compute. Testing: B200, CUDA 13.2. On the audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment), the six test modules of the host sum helpers on both lanes with GPU 6 (test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py, test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py, test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py): GPU lane 38 passed, CPU lane 201 passed, none skipped, so the helper's kernel ran its tests, the ATen comparison of the reciprocal among them. The known gap's forms, run there: an answered call whose divisor function zeroes the operand returns 0 where the statement gives 64; a call on a CPU, a strided or an fp16 tensor reads its divisor once and declines; under a device mode the divisor is not read. On torch 2.15.0+cu132, which the lanes of this stack run on, `scripts/run_tests.py --backend cute --gpus 4 test/test_cute_host_full_sum.py`: GPU lane 4 passed, 34 skipped (the tests that need the audited build); CPU lane 18 passed. test/test_autoreview.py, which checks the form of the entries of KNOWN_GAPS.md: 157 passed. With the lowering and the helper of the parent commit, the tests of the rebinding forms, of the scaled sum's rendered call and reads and of the reciprocal fail. `./lint.sh check` passes. Lanes at the tip of the stack, which holds this commit, on origin/main 9a31efc: `scripts/run_tests.py --backend cute --gpus 0,4`, CPU lane 27430 passed, 689 skipped, 1 failed (test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition, which fails at 9a31efc as well); GPU lane 16014 passed, 2384 skipped, 3 failed on a timeout while other suites loaded the machine (test_cute_standalone.py::test_native_source_edit_without_helion, test_examples.py::TestExamples::test_linear_monkeypatch_gated_delta_rule, test_handoff_bundle.py::test_namedtuple_bundle_replays_in_fresh_process) and passed when run again alone. `scripts/run_tests.py --backend triton --gpus 1,4`, GPU lane 3408 passed, 2411 skipped, 2 failed (test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and ::test_task_accumulators_get_their_own_rings, which fail at 9a31efc as well); CPU lane 5168 passed, 632 skipped. Review notes: - Deferred: "an answered call evaluates the divisor before the sum" and "a call that evaluated the divisor can still decline". KNOWN_GAPS.md, cute-full-sum-evaluates-its-divisor-early. At origin/main (f615a9b) the rendered call is `divisor=x.size(0)`: the replay above reads the divisor before the helper on every call and again in the fallback of every declined one, on every build. This commit leaves both forms on the audited build as they were, outside a mode of the caller, and removes the second on every other build and under such a mode. - Not taken: "run the proofs again after the divisor". The helper calls the function ahead of its proofs, where the argument was evaluated, so the divisor's code cannot invalidate a proof that already ran. autoreview passed: claude, codex stack-info: PR: #4816, branch: choijon5/cute-norm-bwd/6
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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 that get faster: none; a move of test helpers, with no change to the compiler or to generated code. 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. Four test modules share the harness that test_cute_resident_reductions.py defined. test_cute_resident_pipeline_depth.py, test_cute_resident_serial_output.py and test_cute_resident_local_tree.py imported `_source`, `_rewrite`, `_namespace` and `_Tensor` from it, and two of them imported `_run_pipeline` from the pipeline depth module. Shared test helpers belong in helion/_testing, and the commits above this one change the harness. The definitions move to helion/_testing/cute_resident_reductions.py as they were, with four `cast`s for the type checker, which reads helion/ and not test/. The serial output test patched `_namespace` on the pipeline depth module, where `_run_pipeline` read it; it patches the new module. Testing: the four modules under HELION_BACKEND=cute without a GPU, 1326 passed, the count before the move (they hold no GPU test). `./lint.sh check` on the five files 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: #4817, branch: choijon5/cute-norm-bwd/7
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), origin/main (f615a9b) -> this commit: - 2560 features: 352.5 us -> 37.5 us - 6144: 2294.6 us -> 68.6 us - 7680: 2285.6 us -> 95.4 us - 9216: no CuTe result -> 91.3 us - 10752: no CuTe result -> 233.3 us - 12800: no CuTe result -> 310.0 us - 14336: no CuTe result -> 173.1 us - 15872: no CuTe result -> 611.4 us 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 next three commits of the stack seed and reload these rows (their thread counts, 4-wide vectors for 32-bit rows, rows too wide for the registers), which the widths from 10752 on still need. A row that is a power of two renders the code it rendered (641 of 641 configs of five shapes compared). The times are the autotuner's, of the kernel alone. TritonBench's speedup figure times the Python call, which at these sizes moved by tens of percent between runs on this shared machine (2560 features: 0.46x to 0.80x of the best baseline at the same kernel time), so it is not quoted here. The normalization backward kernels (examples/layer_norm.py::layer_norm_bwd, examples/rms_norm.py::rms_norm_bwd) keep one accumulator of a row's length per CTA and are fastest on the resident reduction schedules, which hold a row in registers across its two passes. Those schedules ran only rows whose length is a power of two. At any other length the kernels fell to the computed fragment owner, which keeps the row in shared memory: about 20x slower at 6144 and 7680 fp32 features, and from 9216 features on no config compiled at all ("computed fragments need 294976 shared bytes"), so TritonBench's layer_norm-bwd had no CuTe result for five of its ten inputs. The default config was refused at every such length ("reduction marker is nested in a different lane owner"), so the kernels did not compile without a search. Two things kept such rows off the resident schedules: - A factory tensor of static dims is padded to powers of two, so `weight.new_zeros(n)` is typed 4096 for n = 2560. An op between it and `x[tile, :]` pins the slice's dim to 4096, and the reductions over the pinned values take a second dim: the feature axis has two block ids, and the resident plan needs one. `Backend.joins_factory_extents` (default off) lets a backend have device IR type the factory tensors on the dim of the full slices instead, as `hl.zeros([tile, n])` already is (`CompileEnvironment.renew_factory_pinned_extents`, next to the renewal of pinned subscript extents). An uninitialized tensor of a joined extent (`torch.empty`, `new_empty`) is created as zeros, because Inductor lowers `aten.empty` from a literal size. A factory that asks for a memory format other than the contiguous default is not joined: the zeros twin takes none. (A kernel cannot spell a memory format, type propagation refuses the value; the mode guards the call all the same.) CuTe asks for the join only for the loop nest the resident family schedules, with nothing else in it (`serial_row_nest`): one root loop over one row loop, a `for` loop over `hl.tile` or `hl.grid`, whose calls are of a closed list. A `while` loop over the rows keeps the padded literal: the family plans none, and the lowering that runs one refuses the joined dim ("lane reduction under a guard whose thread uniformity cannot be proven"), where origin/main renders it. A `for` loop over a Python sequence keeps the padded literal as well: type propagation marks it as a device loop, but the kernel unrolls it and no row loop is left. The list holds the loops' tiles, the language's tensor factories, Python's own functions, and, as torch functions and as tensor methods, elementwise math, reductions over a dim, casts and tensor factories. The operation decides and not its spelling, so the callee is read from its type: a matrix product in any form (`@`, `torch.mm`, a `Tensor.mm` that the kernel saved under a name, `torch.addmm`, `torch.baddbmm`), an index tensor (`hl.arange`, `torch.arange`), every other callable of the language (`hl.dot`, `hl.tile_index`, atomics) and a helper function, whose body the host AST does not show, keep the padded literal, which their lowerings read. A call in the branch of a constant `if` that type propagation skips is not part of the nest: layer_norm_bwd without a bias (`compute_bias_grad=False`) is the same nest as with one. - A persistent row is padded to a power of two and masked. A resident row now runs unpadded on the threads its config names when they tile it: whole warps of any count, or a power of two of at most one warp (`resident_row_layout`). The row's sums over the CTA combine the warps' partials in subgroups of the next power of two, whose members past the warp count bring zero (`_cute_resident_sums`, `_cute_resident_sums_disjoint`). The joined dim is every config's of such a kernel, not the resident ones' alone: the default config now renders, walking the row in masked lanes of its own length, and matches eager autograd. The lowering refuses what these rows newly reach: - the local summation tree on a thread whose lanes are no power of two (it pairs complete subtrees; 128 threads over 2560 features carry 20 each); - a staged row that is no whole number of its copies (16 bytes each, or the whole of a shorter row): the last copy would run into the next slot; - row stores whose Int32 offsets could wrap into each other. The proof that the rows of a store are apart relied on the row's span dividing 2**32, which a power of two does; for any other span, no difference of two rows may come within a span of 2**32. The heuristic's plan accepts a row length that is no power of two, so the existing seed families apply where they tile the row; the local-tree twins are planted only where the tree is complete, and the 8-wide vector rows only for rows of whole vectors. Compatibility: CuTe only. A kernel of this nest whose row length is no power of two now has one reduction dim for the row instead of two, so its per-block config lists (num_threads, cute_vector_widths, ...) are one entry shorter; configs saved for such a kernel by an older version do not transfer. These kernels had no resident config before, their default config did not compile, and at 9216 features and above none did. The resident schedule knobs are no longer inert for them, so a random config of their search space renders as often as for a row that is a power of two, which is less often than before (layer_norm_bwd at 4096 x 2560 fp32: 60 of 300 random configs against 119 of 300; the others are refused as "resident reduction schedules cannot share a computed fragment root"), while every compiler seed renders (14 of 14, against 0 of 2). With `static_shapes=False` the accumulators' dim is typed by the padded literal itself: nothing is joined and such a binding compiles as before. Triton and Pallas are unchanged: the hook's default keeps the padded literal, and the factory change acts on joined extents only. The documentation of the `cute_unpadded_rows` option now says that a resident row does not read it. Tests: - test/test_cute_resident_row_widths.py (new; CPU, with `cuda` cases): the nest's IR has one dim for the row in both examples and in layer_norm_bwd without a bias, and other nests keep the padded literal (a second row loop, `hl.tile_index`, `hl.arange`, `torch.arange`, a helper call, `@`, a saved `mm` method, a `while` loop over the rows, whose default config renders as it did, a `for` loop over a Python tuple in the row loop's place, and a loop without a row loop); the functions and methods a nest may call; every padded factory takes the joined dim, and one with a memory format keeps its padded literal; the default config renders; the thread counts that run a row unpadded and those that are refused; the local tree and the staged row of whole copies. On the device both examples match eager autograd at 2560, 5632, 640 and 96 features (fp32 and bf16): staged, unstaged, in a four-row group, on the serial row schedule and under the default config. So do layer_norm_bwd without a bias and a kernel with an accumulator of every tensor factory. A CPU test renders each device case and adds up its static shared memory: the staged ones stay within the 48 KiB that libNVVM compiles below sm_90, which the per-block check holds a stage to there (the bottom commit of the stack), so no case asks a device for a stage it refuses. - test/test_cute_resident_reduction_runtime.py: the sums of blocks of 3, 5, 15 and 31 warps, with one and three statistics, in both sum helpers, interpreted on CPU. - test/test_cute_resident_reductions.py: the Int32 row-store proof for a span that is no power of two, and the staged row of whole copies. - `test/shard_durations.json` records the new module (13.8 s in the CuTe GPU lane). Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27366 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); the three modules of the Tests paragraph and test/test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1284 passed. 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 and the same 1 failed; `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, ptxas C7600, the same at origin/main), CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. Earlier runs, on origin/main 57009cb and before the three fixes at the bottom of the stack went under this commit (they change nothing on this host). The whole GPU lane at this commit, before its device cases shrank: `scripts/run_tests.py --backend cute --gpus 2,6`, 16013 passed, 2370 skipped. Differential fuzz against eager autograd on this B200: the compiler seeds, the default config and 60 random configs of nine cases (layer_norm_bwd, 100 rows of 2560, 1000, 1002, 2016 and 4096 fp32 features, 2560 bf16 and 2564 fp16; rms_norm_bwd, 100 rows of 2560 fp32 and 6144 bf16): 339 configs compiled and matched within 2e-3 (2e-2 for the 16-bit dtypes), 351 were refused by the compiler, none was wrong and none crashed. The cold autotunes of the lead ran with origin/main at f615a9b and this commit on it, before #4763 to #4767 landed, which leave these kernels as they were: at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767. The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4818, branch: choijon5/cute-norm-bwd/8
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
… of two 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: the same rows as the commit below this one (examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two): the search now starts from the thread counts that measure fastest, where it started from a lattice of powers of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), the commit below -> this commit: - 7680 features: 95.4 us -> 85.6 us - 10752: 233.3 us -> 105.4 us - 12800: 310.0 us -> 138.2 us - 15872: 611.4 us -> 223.4 us 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. 2560, 6144, 9216 and 14336 features end where they did (37.4, 69.2, 91.2 and 167.9 us against 37.5, 68.6, 91.3 and 173.1). The resident reduction seeds come from a lattice of power-of-two thread counts. A row whose length is no power of two gets an awkward share per thread from them, or none: 7680 features are 60 a thread over 128 threads and tile neither 256 nor 512 threads in 8-wide vectors, and 9216 over 128 threads are 72 a thread, whose carries and row values leave the registers. The search's thread counts are powers of two as well, so no neighbor of a lattice seed reaches the counts in between. The previous commit lets such rows run on the resident schedules; this one seeds them. Pinned layouts of examples/layer_norm.py::layer_norm_bwd on a B200 (4096 rows, fp32, kernel time in us). "staged" is the pipelined schedule, x2 and x4 its row groups, and the last column the count that gives a thread 16 features: features 256 threads 512 threads 16 features a thread 2560 23.5 staged x4 33.1 staged 33.2 staged (160 threads) 6144 48.7 staged x2 - 52.4 staged (384) 7680 63.0 staged 63.4 staged 65.3 staged (480) 9216 71.0 staged - 73.8 staged (576) 10752 81.5 staged 84.3 staged 131.8 staged (672) 12800 121.1 unstaged 150.0 unstaged 151.7 unstaged (800) 14336 150.3 unstaged 152.9 unstaged - 15872 222.0 unstaged 200.2 unstaged 198.4 unstaged (992) 256 threads are the fastest count up to 10752 features: each thread then has the 255 registers a thread can address, and the block the whole register file of its SM. The staged schedule wins while the registers hold a thread's values; from 12800 features on they do not (50 features a thread, five values each), and the unstaged schedule is faster. At 15872 features the stage does not fit two rows, and half the features a thread run 10% faster. - `_tiled_threads`: the whole-warp thread counts that tile the row nearest to 256 and to 512. - `_tiled_entries`: for each of them a seed with the widest of 4-, 2- and 1-wide vectors that tiles a thread's features. The first count leads with the largest staged row group of four and two rows that the stage holds (four-row groups ran 2560 features 28% faster than single rows, two-row groups 6144 features 7%), then each count is seeded staged with single rows and unstaged. No count is seeded staged when the stage does not hold two rows, so that the primary seed is not one whose stage the shared memory refuses. That holds for the estimate's reading of a row: the kernel's row tensors are as wide as the row and each is loaded once. It is no promise that the seed renders: the lowering stages only the loads it proves aligned to 16 bytes, and refuses a staged seed when it proves that of none. - `_staged_rows`: an estimate of the rows of features that the target's shared memory stages, from the tensors the kernel loads whose last dim is the row's length. The shared memory is what a block of the device opts in to and, below sm_90, no more than the 48 KiB of static allocations that the per-block check holds a stage to there (the bottom commit of the stack). It shapes the seeds only: the lowering charges each config's stage exactly and refuses the one that exceeds the shared memory. - The entries lead the family's list for such a row, before the lattice's, and get the occupancy twins that the lattice's entries get. A twin's row group is at most the rows its CTA runs, as the 8-wide entries' is (256 rows on 148 SMs are two rows a CTA: a group of two, where four would stage masked rows). A row that is a power of two keeps its seeds: `_tiled_entries` returns none for it. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel whose rows are no power of two changes, and with it the fingerprint of its autotune cache entry. Tests (test/test_cute_resident_row_widths.py, CPU): the thread counts of six row lengths, among them one that 256 and 512 threads do not tile and one that no whole warps tile; the entries for stages of eleven, four, three and one rows and for an unknown stage; the staged-row estimate of layer_norm_bwd at three fp32 widths and one bf16 width, and for a model of an sm_80 target, which stages two rows of 2560 fp32 features where sm_100 stages eleven, and whose primary seed is then the one-row stage and renders; the seeds of layer_norm_bwd at 2560 features; the row group of an occupancy twin at 256 rows; and at 15872 features a render of the primary seed, whose staged twin the lowering refuses and renders with the shared memory doubled. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27376 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); test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py and test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1294 passed. The seeds are read on a device by the tests of the GPU lane, which 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead ran with this commit on origin/main f615a9b, before #4763 to #4767 landed and before the estimate took the static limit below sm_90; none of these changes a seed of these inputs on this device (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4819, branch: choijon5/cute-norm-bwd/9
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction kernels whose rows are 32-bit: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd with fp32 inputs. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, origin/main (f615a9b) -> this commit: - layer_norm-bwd (4096, 4096): 56.9 us -> 49.8 us - layer_norm-bwd (4096, 1024): 24.8 us -> 24.7 us - rms_norm-bwd (2048, 1024), (2048, 2048), (2048, 4096) and (2048, 16384): 14.8, 19.5, 30.0 and 93.2 us -> 14.6, 19.1, 29.6 and 93.2 us - rms_norm-bwd (2048, 32768): a search ends in one of two kinds of layout at either commit. origin/main, four searches (the nightly's search seed at f615a9b, seeds 1 and 2, and the nightly's seed again at 57009cb): 365.4, 321.6, 324.6 and 807.4 us. This commit, three searches (the nightly's seed, then seeds 1 and 2): 809.4, 242.8 and 802.5 us. The next commit seeds a layout that reloads this row, and its search ends at 201.8 us 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 five other inputs up to 16384 features end where they did, within 0.4 us: the vector width is a knob, which the search moves from the 8-wide seeds by itself. Pinned, the seeds take 21% to 29% less kernel time 4-wide than 8-wide (4096 rows of layer_norm_bwd): 13.9 -> 9.9 us at 1024 features, 25.6 -> 20.1 us at 2048, 45.5 -> 34.3 us at 4096 and 88.0 -> 64.9 us at 8192. Kernels with 16-bit rows keep their seeds. At 32768 features of rms_norm_bwd no seed holds the row in its threads' registers, at origin/main or here: the search starts from seeds that spill (the 256-thread one runs 2267 us) and ends where its random path takes it: in a layout of 240 to 365 us, or in one of about 805 us, which is where one of origin/main's four searches and two of this commit's three ended. The twin that this commit adds at that width spills as well. The layout that origin/main's first search found renders the same kernel at this commit and runs 365 us pinned, as the search times it (the call with its host sum; the kernel alone takes 319.6 us of it). The commit above seeds a reloading layout for this row (195.7 us of kernel time), which a search cannot end behind, so the two are best landed together. The resident seed lattice's 256- and 512-thread entries and the vector rows carry 8-wide feature vectors: 16 bytes of a 16-bit row, which is one async copy or one vector load. Of a 32-bit row 16 bytes are 4 features, and an 8-wide vector is two copies. The 4-wide layout took 21% to 29% less time on fp32 rows (B200, examples/layer_norm.py::layer_norm_bwd, 4096 rows, staged, kernel time in us): features threads row group 8-wide 4-wide 1024 128 4 13.9 9.9 2048 256 2 25.6 20.1 4096 256 2 45.5 34.3 4096 512 2 47.4 35.7 8192 256 1 88.0 64.9 8192 512 1 86.0 65.1 For a kernel whose rows are all 32-bit (`_packet_vector`: the features in 16 bytes of the tensors the kernel loads whose last dim is the row's length), the family repeats its 8-wide entries 4-wide. The twins follow the existing seeds, which keep their rank, and a layout the list already holds is not repeated. A kernel of 16-bit rows keeps its seeds, and so does one whose rows' element sizes differ or are unknown. A 32-bit row is twice the bytes of a 16-bit one, so a staged twin takes the largest row group, of its entry's halved down to one, whose two slots the shared memory holds (`_staged_rows`), and no staged twin is seeded when not even one row's slots fit. Of 4096 fp32 features the stage holds seven rows: the four-row group of 512 threads becomes two. Of 8192 it holds three: every staged twin is one row. Those are the layouts of the table, which all render. Of 16384 features the twins are the unstaged ones. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel with 32-bit rows grows, and with it the fingerprint of its autotune cache entry changes. Tests (test/test_cute_resident_packet_seeds.py, new, CPU): the packet of fp32, bf16 and fp16 rows; for fp32 rows of 1024, 2048, 4096, 8192 and 16384 features the seeds are the previous ones followed by the listed 4-wide twins, in order; at 8192 features the one-row staged twin renders, and its two-row form, which the lowering refuses, renders with the shared memory doubled; a bf16 row's seeds are unchanged. The module holds no GPU test, so `test/shard_durations.json` has no entry for it. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27386 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); the new module and the four resident row modules beside it (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py) on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1304 passed. The commit changes seeds only; the tests that read them on a device 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead: origin/main at f615a9b and at 57009cb (seeds 1 and 2, and the nightly's seed again for the widest rms_norm-bwd input), this commit on each; #4763 to #4767, which landed in between and since, leave the seeds and the code of these kernels as they were (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4820, branch: choijon5/cute-norm-bwd/10
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction rows too wide for their threads' registers: examples/layer_norm.py::layer_norm_bwd from 12800 fp32 features and examples/rms_norm.py::rms_norm_bwd at 32768. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, the commit below -> this commit: - rms_norm-bwd (2048, 32768): 809.4 us -> 201.8 us (origin/main: 321.6 to 807.4 us over four searches) - layer_norm-bwd (4096, 12800): 138.2 us -> 126.1 us - layer_norm-bwd (4096, 14336): 171.0 us -> 134.1 us 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 other inputs end where they did, 10752 and 15872 features among them (103.5 us -> 105.5 us and 222.2 us -> 218.0 us). Each of the three searches now ends at a reloading layout that the commit seeds: pinned, the best layout that holds the row's values against the best that reloads them is 121.2 us -> 103.5 us of kernel time at 12800 features of layer_norm_bwd, 151.2 us -> 111.5 us at 14336, and 319.6 us -> 195.7 us at 32768 of rms_norm_bwd, where the holding layout is the one that origin/main's first search found. A layout whose threads hold their row gains nothing from a reload (layer_norm_bwd at 10752 features over 256 threads: 81.6 us held, 84.2 us reloading), and such a seed gets no reloading twin. The resident reduction schedules run a row in two passes. The first reduces it; the second reads, for each feature, values the first computed, which a thread holds in register fragments: five values a feature in examples/layer_norm.py::layer_norm_bwd, three in examples/rms_norm.py::rms_norm_bwd. A thread addresses 255 registers and shares the 65536 of its SM with the other threads of its block. Past that the values go to the thread's local memory, and both passes take several times as long. No thread count avoids it for a wide row: 1024 threads have 64 registers each. - `cute_reduction_reloads` "gmem" now applies to the resident schedules (`ResidentReductionOptions.reload_blocks`). The second pass of such a row holds none of the first pass's values in registers: it runs the statements that computed them again, on the row's loads, which come from the stage under the pipelined schedule and from global memory, where the row is still in L2, under the resident one. The knob is the per-block one that the rolled reductions read, with the meaning it has there. The other modes, among them the default "auto", hold the values as before. - Three rows keep their registers under "gmem" too, so every config that compiled before compiles now. A row of the deferred serial row schedule, which has no barrier after the second pass (the next group's copies would land in the slot the pass still reads). A row whose second pass reads a value computed from a carry, which holds the next row's value by then. And a row whose reload fails a proof that the holding pass passes: the statements that run again bring what they read into the second pass, and a row group runs a row's own statements again for each of its rows, which it proves for statements that load nothing and compute an integer or a boolean or assign a floating-point literal, so a group that would run an FP16 value of the row again keeps the holding pass (`_plan` tries the reloading pass and falls back). - A row that the holding pass refuses can compile under "gmem": that pass keeps FP32 values only, and a boolean of the feature that the second pass reads is computed again. - Seeds: where an estimate says that a seed's row passes its threads' registers (`_feature_values`, `_spills`), the family plants a reloading twin after the existing seeds, when there is one. A staged seed keeps its layout, and has no twin when the stage does not hold the two slots of its row group. An unstaged one reads global memory twice, which is faster the more warps load at once: its twin runs on the most whole warps that tile the row in 16-byte vectors, and it has none when no whole warps do. Twins that come out as one layout are seeded once. The estimate places seeds only; the lowering takes any config. Pinned layouts on a B200 (fp32, kernel time in us, best layout of each kind; "held" keeps the values in registers): kernel, rows x features held reloading layer_norm_bwd 4096 x 10752 81.6 staged 84.2 staged layer_norm_bwd 4096 x 12800 121.2 unstaged 103.5 staged layer_norm_bwd 4096 x 14336 151.2 unstaged 111.5 staged layer_norm_bwd 4096 x 15872 198.4 unstaged 205.5 unstaged rms_norm_bwd 2048 x 16384 86.4 unstaged 85.4 unstaged rms_norm_bwd 2048 x 32768 319.6 unstaged 195.7 unstaged At 10752 features 256 threads hold the row (42 features a thread) and a reload gains nothing: those seeds get no twin. The lattice's 128-thread staged seed carries 84 features a thread there and gets one, as it does from 6144 features on. At 15872 the stage does not fit two rows, and the unstaged reload is on par. At 32768 features of rms_norm_bwd the held layout is the one that origin/main's first search found (1024 threads in two-row groups, one row a CTA); the family's own held seed of 256 threads runs 2267 us. Reloading from global memory is slower than holding wherever the registers fit (7680 features over 256 threads: 196.5 us against 77.7 us), which is why only "gmem" selects it. Compatibility: CuTe only. A config with a resident schedule and "gmem" for the row's reduction rendered the holding pass before and renders the reloading one now: the same results from other code. The family's seeds said "register", and "auto" is unchanged, so no seed or default config renders differently. A kernel gets compiler seeds: the reloading twins of its seeds whose row passes their threads' registers by the estimate (`_spills`), where a twin exists (the Seeds item above) and its coarse tile is within the kernel's block sizes, each layout once. They change the fingerprint of its autotune cache entry. The rule reads the features a thread carries, so no width is a threshold. Examples, fp32, at the row counts of TritonBench: layer_norm_bwd gets the twin of its 128-thread staged seed at 6144, 7680, 9216 and 10752 features (48 features a thread and more) and none at 5120, four twins at 12800 and 14336, one at 15872 and 16384, and two at 3104, which only 32 threads tile; rms_norm_bwd gets none at 6144 and two from 7680 (60 features a thread), staged up to 8192 and unstaged at 16384 and 32768. Each unstaged twin is also seeded with the local tree. A resident config with "gmem" can now compile a row that the holding pass refuses (above). The one change outside the backend is the docstring of `CuteReductionReloadSpec` in helion/autotuner/config_spec.py, which now describes the knob on the resident schedules: the rows that keep their registers under "gmem" and the row that compiles under it alone. Tests: - test/test_cute_resident_reductions.py runs the rewritten loop nest on CPU against the loop's closed form: a reloading second pass holds no register fragment and loads each row twice from global memory, or once when staged (two widths, two row groups, both schedules); a value computed from a carry stays in registers; a row group that would run an FP16 value of the row again keeps the holding pass, where a group of one row reloads (both rewrites are checked to be the resident form); a row whose second pass reads a boolean of the feature is rewritten under "gmem" only, and matches the closed form. - test/test_cute_resident_row_widths.py renders both examples: the reloading row's fragments are gone and its vector loads double (12800 features staged, 32768 unstaged); a deferred row keeps its registers; `_spills` at the boundaries of the two limits; the reloading seeds of both examples at six widths (12800, 10752, 6144 and 3104 features of layer_norm_bwd, 32768 and 7680 of rms_norm_bwd) and the four widths that seed none; the twins follow the previous seeds, which keep their rank. Three `cuda` cases run reloading rows against eager autograd; the staged one is 2048 features wide, and a CPU test adds up the static shared memory of each case: at most the 48 KiB that libNVVM compiles below sm_90. - test/test_cute_resident_packet_seeds.py leaves the reloading seeds out of the lists it compares. - Differential fuzz against eager autograd on a B200, at this commit before the two fixes at the bottom of the stack went under it (the per-block check of a stage and the device of the reduction budget, which change nothing on this host): the compiler seeds, 10 random configs and 80 random resident configs with "gmem", for each of eleven cases (layer_norm_bwd at 100 x 1000, 100 x 2560 and 100 x 4096 fp32, 100 x 2560 bf16, 64 x 12800 fp32 and 64 x 7680 fp16; rms_norm_bwd at 100 x 2560 and 100 x 4096 fp32, 100 x 6144 bf16, 64 x 16384 fp16 and 64 x 32768 fp32). 750 configs compiled and matched, 407 of them with a reloading pass; the compiler refused 595; none was wrong and none crashed. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27408 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); the five resident row modules (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py, test_cute_resident_packet_seeds.py) on both lanes with GPU 4, GPU lane 13 passed, CPU lane 1326 passed. The GPU lane ran at the tip of the stack, the commit above this one, which adds the Parameter guard in other files: `scripts/run_tests.py --backend cute --gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped. For the docstring in helion/autotuner/config_spec.py, the Triton and Pallas interpreter lanes ran at the tip 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, ptxas C7600, the same at origin/main); CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. The cold autotunes that the lead gives for this commit ran at the tip of the stack on origin/main 57009cb, before #4766 and #4767 landed and before the fixes at the stack's bottom were reworked: the commit above changes the host path of a launch and no kernel, those fixes nothing on this host, and #4766 and #4767 nothing in these kernels (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4821, branch: choijon5/cute-norm-bwd/11
choijon5
force-pushed
the
choijon5/cute-norm-bwd/11
branch
from
November 29, 2026 20:10
a8366f4 to
b78f030
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
choijon5
force-pushed
the
choijon5/cute-norm-bwd/12
branch
from
November 29, 2026 20:11
886e30f to
b9bc730
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
…al (#4816) 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; two fixes to what runs when the helper of a full host sum declines, which it does on every Torch build but the audited one. Under `cute_host_paired_sum` "mapped" or "narrow", a kernel whose host tail assigns a full sum to the name of its operand (`out = torch.sum(out)`) or of its divisor (`n = torch.sum(out) / n`) raised a TypeError, and a scaled sum (`torch.sum(out) / x.size(0)`) evaluated its divisor twice. A third fix is to the helper's own arithmetic: it multiplied by another reciprocal than ATen does for divisors such as 1.1. 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. With "mapped" or "narrow" a full host sum goes to a runtime helper (`try_full_sum`), and the original statement runs when the helper returns None. Two things kept that statement from running as it was written. Its operand. The rewrite assigned the helper's answer to the statement's target: out = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if out is None: out = torch.sum(out) so the fallback read None where the statement reads its target. `lower_host_full_sum` now gives the answer a name of its own for such a statement and assigns the target from it when the helper answered: _helion_full_sum = _cute_try_full_sum(out, mean=False, _launcher=_launcher) if _helion_full_sum is None: out = torch.sum(out) else: out = _helion_full_sum Its divisor. The call took the divisor as an argument (`divisor=x.size(0)`), so every call evaluated it before the helper decided, and the fallback of a declined call evaluated it again. The proof admits tensor metadata in a divisor (`x.size(0)`, `x.numel()`, `x.shape[0]`) and called that safe to evaluate twice. A function mode of the caller sees those calls, and so does a tensor subclass with `__torch_function__`: on a declined call each saw one more than the statement makes, ahead of the sum, where the helper's rule is that a declined call leaves the exact original call. This predates the stack (d32e9ca, the divisor support). The site now hands the divisor over as a function, total = _cute_try_full_sum(out, mean=False, divisor_fn=lambda: x.size(0), _launcher=_launcher) and `try_full_sum` does not call it on a build whose sums it declines, nor under a function or dispatch mode of the caller. There the statement evaluates its divisor once, in its own order: that is every build but the audited one, and every call under a mode. Replayed on CPU over an operand that records the reads of its size, a declined call reads it twice at origin/main (before the helper and in the fallback) and once here. Otherwise, on the audited build, the helper calls the function where the argument was evaluated, ahead of its proofs, so those calls behave as they did: a call that the helper answers has evaluated the divisor before the sum, and a call that its proofs decline has evaluated it once before the statement does so again. KNOWN_GAPS.md records that (cute-full-sum-evaluates-its-divisor-early). Calling the function after the proofs instead would let the divisor's code invalidate them before the launch (the operand's storage, device or length), so they would have to run a second time on each answered call, and the sum would still be read after the divisor. Both forms are reproduced on the audited build (Testing), which no CI job runs. The reciprocal. The helper's kernel multiplies the sum by a factor, and `_reciprocal_factor` computed it as one over the divisor's FP32 value, in FP32. That was ATen's formula until pytorch/pytorch#169507 (December 2025). Since then `div_true_kernel_cuda` takes the reciprocal in FP64 and narrows it, inv_b = static_cast<opmath_t>( high_prec_t(1.0) / iter.scalar_value<high_prec_t>(2)); with `high_prec_t = double`, and multiplies in FP32 (`MulFunctor<opmath_t>`): aten/src/ATen/native/cuda/BinaryDivTrueKernel.cu at the audited revision (PyTorch main 6d5f66bd5b) and at both earlier pins (a970aa8b98, e2dee2a4b9). The pins' audit covered the reduction sources, not this kernel; the pin's comment in helion/runtime/cute/paired_sum.py now names it for the next one. The two reciprocals differ for a divisor that is no FP32 number of at most 24 bits: for 1.1 ATen multiplies by 0.9090909361839294 and the helper by 0.9090908765792847. So on the one build where the helper answers, a scaled sum could differ from the statement in its last bits; the divisors of the existing tests (3, 7, 0.25, 1037, row counts) are FP32 numbers, for which both agree. Measured on a B200 with torch 2.15.0+cu132, which has the same kernel: of 40000 random `float` divisors 10536 tell the two apart, of 25000 random `int` divisors 5997, and on 300 of each ATen's quotient of an FP32 one was the narrowed FP64 reciprocal every time. On the audited build itself the helper's scaled sum of 4096 random FP32 numbers differed from the statement's for 1418 of 2400 sums at the parent commit (400 tensors by the divisors 1.1, 0.03, 16.1, 2**24 + 1, 3 and 1037), and for none here. `_reciprocal_factor` now takes the reciprocal that way. Two more things about the value it reads: - Of an instance of a subclass of `int` or `float` it reads the value that the instance stores (`int.__index__`, `float.__float__`), which is the value ATen divides by (it takes the stored double of whatever passes `PyFloat_Check`, and an `int`'s digits). It asked `float()` of the instance, which a subclass may answer with another value. - It takes an `int` from -2**63 up to 2**64, the integers that ATen wraps (measured: it raises for -(2**63) - 1 and for 2**64). The helper had a reciprocal for every `int` that `float()` converts, so it answered where the statement raises; such a divisor now declines. `try_full_sum` keeps `divisor`, the value, next to `divisor_fn`: a standalone file that helion/autotuner/aot_compile.py wrote before this change passes it, and keeps the behavior it had. The tests of this rewrite read generated host code, yet five of them were skipped on every build but the audited one, and the module ran in the GPU lane. test/test_cute_host_full_sum.py is now a `cpu_only` module that renders on CPU: its codegen tests run on every build, without a GPU, and its `cuda` items run on the device; those of them that launch the helper's kernel still need the audited build. The device half of the helper's decline test is a `cuda` item of its own, which now holds the rank case as a tensor that passes every other proof. The short-tensor fallback test, whose sum the helper declines on every build, no longer asks for the audited one. Compatibility: CuTe only. Generated host code changes for a full sum whose statement reads its target (the answer's own name; a later site of that kernel that needs a name of its own takes the next free one) and for a scaled sum (`divisor_fn=lambda: ...` where it had `divisor=...`). Other kernels render as before. On the audited build a scaled sum that the helper answers is multiplied by the reciprocal that ATen multiplies by, which changes its last bits for divisors that are no FP32 numbers of at most 24 bits; a divisor that is an instance of a subclass of `int` or `float` is taken by the value it stores, and an `int` that ATen raises for is left to the statement. Tests (test/test_cute_host_full_sum.py): - The two rebinding forms render the answer under its own name; the rendered statements, run on CPU with a stand-in helper, leave the original statement's result when it declines and its answer when it does not (one render per kernel). - The rendered statements of a scaled sum, replayed from the render that the existing divisor test makes, over an operand that counts the reads of its size: one read whether the stand-in helper declines or answers. - The helper on CPU: with the build declined, or under a function, a dispatch or a device mode, it does not read a divisor function; with the build admitted and no mode it reads it once before a proof declines the call (the known gap's second form, which the test pins); the reciprocal of plain numbers, of 1.1 and 2**54 + 2**30 + 1 (which tell the formulas apart), of -2**63, 2**63 and 2**64 - 1, of a `float` subclass whose `float()` doubles its value and of an `int` subclass whose `float()` and index add one; none for a `bool`, a tensor, a string, a complex number, -(2**63) - 1, 2**64 and 1 << 80. - The helper on the device with a stand-in launcher, so on every build: a divisor function is read once and gives the same launch as the value; the two subclasses launch with the reciprocal of what they store; a tensor or `bool` divisor and a mean with a divisor decline, as do the tensors the helper does not take (short, misaligned, strided, rank-2, fp16, grad-requiring). - On the audited build (skipped elsewhere): for 19 divisors (1.1, 0.03, 16.1, 2**24 + 1, 2**54 + 2**30 + 1, 2**63 + 2**40 + 1, 1e300, 1e-300, both zeros and the two subclasses among them) ATen's quotient of one is the factor, and its quotient of 4096 random numbers is their FP32 product with the factor; ATen raises for -(2**63) - 1 and 2**64. The test of the helper's own kernel on divisors gets 1.1 and 2**24 + 1. - On the device both rebinding kernels, with a sum the helper declines on every build, return what the original statements compute. Testing: B200, CUDA 13.2. On the audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment), the six test modules of the host sum helpers on both lanes with GPU 6 (test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py, test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py, test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py): GPU lane 38 passed, CPU lane 201 passed, none skipped, so the helper's kernel ran its tests, the ATen comparison of the reciprocal among them. The known gap's forms, run there: an answered call whose divisor function zeroes the operand returns 0 where the statement gives 64; a call on a CPU, a strided or an fp16 tensor reads its divisor once and declines; under a device mode the divisor is not read. On torch 2.15.0+cu132, which the lanes of this stack run on, `scripts/run_tests.py --backend cute --gpus 4 test/test_cute_host_full_sum.py`: GPU lane 4 passed, 34 skipped (the tests that need the audited build); CPU lane 18 passed. test/test_autoreview.py, which checks the form of the entries of KNOWN_GAPS.md: 157 passed. With the lowering and the helper of the parent commit, the tests of the rebinding forms, of the scaled sum's rendered call and reads and of the reciprocal fail. `./lint.sh check` passes. Lanes at the tip of the stack, which holds this commit, on origin/main 9a31efc: `scripts/run_tests.py --backend cute --gpus 0,4`, CPU lane 27430 passed, 689 skipped, 1 failed (test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition, which fails at 9a31efc as well); GPU lane 16014 passed, 2384 skipped, 3 failed on a timeout while other suites loaded the machine (test_cute_standalone.py::test_native_source_edit_without_helion, test_examples.py::TestExamples::test_linear_monkeypatch_gated_delta_rule, test_handoff_bundle.py::test_namedtuple_bundle_replays_in_fresh_process) and passed when run again alone. `scripts/run_tests.py --backend triton --gpus 1,4`, GPU lane 3408 passed, 2411 skipped, 2 failed (test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and ::test_task_accumulators_get_their_own_rings, which fail at 9a31efc as well); CPU lane 5168 passed, 632 skipped. Review notes: - Deferred: "an answered call evaluates the divisor before the sum" and "a call that evaluated the divisor can still decline". KNOWN_GAPS.md, cute-full-sum-evaluates-its-divisor-early. At origin/main (f615a9b) the rendered call is `divisor=x.size(0)`: the replay above reads the divisor before the helper on every call and again in the fallback of every declined one, on every build. This commit leaves both forms on the audited build as they were, outside a mode of the caller, and removes the second on every other build and under such a mode. - Not taken: "run the proofs again after the divisor". The helper calls the function ahead of its proofs, where the argument was evaluated, so the divisor's code cannot invalidate a proof that already ran. autoreview passed: claude, codex
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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 that get faster: none; a move of test helpers, with no change to the compiler or to generated code. 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. Four test modules share the harness that test_cute_resident_reductions.py defined. test_cute_resident_pipeline_depth.py, test_cute_resident_serial_output.py and test_cute_resident_local_tree.py imported `_source`, `_rewrite`, `_namespace` and `_Tensor` from it, and two of them imported `_run_pipeline` from the pipeline depth module. Shared test helpers belong in helion/_testing, and the commits above this one change the harness. The definitions move to helion/_testing/cute_resident_reductions.py as they were, with four `cast`s for the type checker, which reads helion/ and not test/. The serial output test patched `_namespace` on the pipeline depth module, where `_run_pipeline` read it; it patches the new module. Testing: the four modules under HELION_BACKEND=cute without a GPU, 1326 passed, the count before the move (they hold no GPU test). `./lint.sh check` on the five files 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: #4817, branch: choijon5/cute-norm-bwd/7
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), origin/main (f615a9b) -> this commit: - 2560 features: 352.5 us -> 37.5 us - 6144: 2294.6 us -> 68.6 us - 7680: 2285.6 us -> 95.4 us - 9216: no CuTe result -> 91.3 us - 10752: no CuTe result -> 233.3 us - 12800: no CuTe result -> 310.0 us - 14336: no CuTe result -> 173.1 us - 15872: no CuTe result -> 611.4 us 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 next three commits of the stack seed and reload these rows (their thread counts, 4-wide vectors for 32-bit rows, rows too wide for the registers), which the widths from 10752 on still need. A row that is a power of two renders the code it rendered (641 of 641 configs of five shapes compared). The times are the autotuner's, of the kernel alone. TritonBench's speedup figure times the Python call, which at these sizes moved by tens of percent between runs on this shared machine (2560 features: 0.46x to 0.80x of the best baseline at the same kernel time), so it is not quoted here. The normalization backward kernels (examples/layer_norm.py::layer_norm_bwd, examples/rms_norm.py::rms_norm_bwd) keep one accumulator of a row's length per CTA and are fastest on the resident reduction schedules, which hold a row in registers across its two passes. Those schedules ran only rows whose length is a power of two. At any other length the kernels fell to the computed fragment owner, which keeps the row in shared memory: about 20x slower at 6144 and 7680 fp32 features, and from 9216 features on no config compiled at all ("computed fragments need 294976 shared bytes"), so TritonBench's layer_norm-bwd had no CuTe result for five of its ten inputs. The default config was refused at every such length ("reduction marker is nested in a different lane owner"), so the kernels did not compile without a search. Two things kept such rows off the resident schedules: - A factory tensor of static dims is padded to powers of two, so `weight.new_zeros(n)` is typed 4096 for n = 2560. An op between it and `x[tile, :]` pins the slice's dim to 4096, and the reductions over the pinned values take a second dim: the feature axis has two block ids, and the resident plan needs one. `Backend.joins_factory_extents` (default off) lets a backend have device IR type the factory tensors on the dim of the full slices instead, as `hl.zeros([tile, n])` already is (`CompileEnvironment.renew_factory_pinned_extents`, next to the renewal of pinned subscript extents). An uninitialized tensor of a joined extent (`torch.empty`, `new_empty`) is created as zeros, because Inductor lowers `aten.empty` from a literal size. A factory that asks for a memory format other than the contiguous default is not joined: the zeros twin takes none. (A kernel cannot spell a memory format, type propagation refuses the value; the mode guards the call all the same.) CuTe asks for the join only for the loop nest the resident family schedules, with nothing else in it (`serial_row_nest`): one root loop over one row loop, a `for` loop over `hl.tile` or `hl.grid`, whose calls are of a closed list. A `while` loop over the rows keeps the padded literal: the family plans none, and the lowering that runs one refuses the joined dim ("lane reduction under a guard whose thread uniformity cannot be proven"), where origin/main renders it. A `for` loop over a Python sequence keeps the padded literal as well: type propagation marks it as a device loop, but the kernel unrolls it and no row loop is left. The list holds the loops' tiles, the language's tensor factories, Python's own functions, and, as torch functions and as tensor methods, elementwise math, reductions over a dim, casts and tensor factories. The operation decides and not its spelling, so the callee is read from its type: a matrix product in any form (`@`, `torch.mm`, a `Tensor.mm` that the kernel saved under a name, `torch.addmm`, `torch.baddbmm`), an index tensor (`hl.arange`, `torch.arange`), every other callable of the language (`hl.dot`, `hl.tile_index`, atomics) and a helper function, whose body the host AST does not show, keep the padded literal, which their lowerings read. A call in the branch of a constant `if` that type propagation skips is not part of the nest: layer_norm_bwd without a bias (`compute_bias_grad=False`) is the same nest as with one. - A persistent row is padded to a power of two and masked. A resident row now runs unpadded on the threads its config names when they tile it: whole warps of any count, or a power of two of at most one warp (`resident_row_layout`). The row's sums over the CTA combine the warps' partials in subgroups of the next power of two, whose members past the warp count bring zero (`_cute_resident_sums`, `_cute_resident_sums_disjoint`). The joined dim is every config's of such a kernel, not the resident ones' alone: the default config now renders, walking the row in masked lanes of its own length, and matches eager autograd. The lowering refuses what these rows newly reach: - the local summation tree on a thread whose lanes are no power of two (it pairs complete subtrees; 128 threads over 2560 features carry 20 each); - a staged row that is no whole number of its copies (16 bytes each, or the whole of a shorter row): the last copy would run into the next slot; - row stores whose Int32 offsets could wrap into each other. The proof that the rows of a store are apart relied on the row's span dividing 2**32, which a power of two does; for any other span, no difference of two rows may come within a span of 2**32. The heuristic's plan accepts a row length that is no power of two, so the existing seed families apply where they tile the row; the local-tree twins are planted only where the tree is complete, and the 8-wide vector rows only for rows of whole vectors. Compatibility: CuTe only. A kernel of this nest whose row length is no power of two now has one reduction dim for the row instead of two, so its per-block config lists (num_threads, cute_vector_widths, ...) are one entry shorter; configs saved for such a kernel by an older version do not transfer. These kernels had no resident config before, their default config did not compile, and at 9216 features and above none did. The resident schedule knobs are no longer inert for them, so a random config of their search space renders as often as for a row that is a power of two, which is less often than before (layer_norm_bwd at 4096 x 2560 fp32: 60 of 300 random configs against 119 of 300; the others are refused as "resident reduction schedules cannot share a computed fragment root"), while every compiler seed renders (14 of 14, against 0 of 2). With `static_shapes=False` the accumulators' dim is typed by the padded literal itself: nothing is joined and such a binding compiles as before. Triton and Pallas are unchanged: the hook's default keeps the padded literal, and the factory change acts on joined extents only. The documentation of the `cute_unpadded_rows` option now says that a resident row does not read it. Tests: - test/test_cute_resident_row_widths.py (new; CPU, with `cuda` cases): the nest's IR has one dim for the row in both examples and in layer_norm_bwd without a bias, and other nests keep the padded literal (a second row loop, `hl.tile_index`, `hl.arange`, `torch.arange`, a helper call, `@`, a saved `mm` method, a `while` loop over the rows, whose default config renders as it did, a `for` loop over a Python tuple in the row loop's place, and a loop without a row loop); the functions and methods a nest may call; every padded factory takes the joined dim, and one with a memory format keeps its padded literal; the default config renders; the thread counts that run a row unpadded and those that are refused; the local tree and the staged row of whole copies. On the device both examples match eager autograd at 2560, 5632, 640 and 96 features (fp32 and bf16): staged, unstaged, in a four-row group, on the serial row schedule and under the default config. So do layer_norm_bwd without a bias and a kernel with an accumulator of every tensor factory. A CPU test renders each device case and adds up its static shared memory: the staged ones stay within the 48 KiB that libNVVM compiles below sm_90, which the per-block check holds a stage to there (the bottom commit of the stack), so no case asks a device for a stage it refuses. - test/test_cute_resident_reduction_runtime.py: the sums of blocks of 3, 5, 15 and 31 warps, with one and three statistics, in both sum helpers, interpreted on CPU. - test/test_cute_resident_reductions.py: the Int32 row-store proof for a span that is no power of two, and the staged row of whole copies. - `test/shard_durations.json` records the new module (13.8 s in the CuTe GPU lane). Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27366 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); the three modules of the Tests paragraph and test/test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1284 passed. 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 and the same 1 failed; `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, ptxas C7600, the same at origin/main), CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. Earlier runs, on origin/main 57009cb and before the three fixes at the bottom of the stack went under this commit (they change nothing on this host). The whole GPU lane at this commit, before its device cases shrank: `scripts/run_tests.py --backend cute --gpus 2,6`, 16013 passed, 2370 skipped. Differential fuzz against eager autograd on this B200: the compiler seeds, the default config and 60 random configs of nine cases (layer_norm_bwd, 100 rows of 2560, 1000, 1002, 2016 and 4096 fp32 features, 2560 bf16 and 2564 fp16; rms_norm_bwd, 100 rows of 2560 fp32 and 6144 bf16): 339 configs compiled and matched within 2e-3 (2e-2 for the 16-bit dtypes), 351 were refused by the compiler, none was wrong and none crashed. The cold autotunes of the lead ran with origin/main at f615a9b and this commit on it, before #4763 to #4767 landed, which leave these kernels as they were: at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767. The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4818, branch: choijon5/cute-norm-bwd/8
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
… of two 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: the same rows as the commit below this one (examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd at row lengths that are no power of two): the search now starts from the thread counts that measure fastest, where it started from a lattice of powers of two. TritonBench's layer_norm-bwd on a B200 (4096 rows, fp32, one cold full autotune per input, the time the search measured for its winner), the commit below -> this commit: - 7680 features: 95.4 us -> 85.6 us - 10752: 233.3 us -> 105.4 us - 12800: 310.0 us -> 138.2 us - 15872: 611.4 us -> 223.4 us 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. 2560, 6144, 9216 and 14336 features end where they did (37.4, 69.2, 91.2 and 167.9 us against 37.5, 68.6, 91.3 and 173.1). The resident reduction seeds come from a lattice of power-of-two thread counts. A row whose length is no power of two gets an awkward share per thread from them, or none: 7680 features are 60 a thread over 128 threads and tile neither 256 nor 512 threads in 8-wide vectors, and 9216 over 128 threads are 72 a thread, whose carries and row values leave the registers. The search's thread counts are powers of two as well, so no neighbor of a lattice seed reaches the counts in between. The previous commit lets such rows run on the resident schedules; this one seeds them. Pinned layouts of examples/layer_norm.py::layer_norm_bwd on a B200 (4096 rows, fp32, kernel time in us). "staged" is the pipelined schedule, x2 and x4 its row groups, and the last column the count that gives a thread 16 features: features 256 threads 512 threads 16 features a thread 2560 23.5 staged x4 33.1 staged 33.2 staged (160 threads) 6144 48.7 staged x2 - 52.4 staged (384) 7680 63.0 staged 63.4 staged 65.3 staged (480) 9216 71.0 staged - 73.8 staged (576) 10752 81.5 staged 84.3 staged 131.8 staged (672) 12800 121.1 unstaged 150.0 unstaged 151.7 unstaged (800) 14336 150.3 unstaged 152.9 unstaged - 15872 222.0 unstaged 200.2 unstaged 198.4 unstaged (992) 256 threads are the fastest count up to 10752 features: each thread then has the 255 registers a thread can address, and the block the whole register file of its SM. The staged schedule wins while the registers hold a thread's values; from 12800 features on they do not (50 features a thread, five values each), and the unstaged schedule is faster. At 15872 features the stage does not fit two rows, and half the features a thread run 10% faster. - `_tiled_threads`: the whole-warp thread counts that tile the row nearest to 256 and to 512. - `_tiled_entries`: for each of them a seed with the widest of 4-, 2- and 1-wide vectors that tiles a thread's features. The first count leads with the largest staged row group of four and two rows that the stage holds (four-row groups ran 2560 features 28% faster than single rows, two-row groups 6144 features 7%), then each count is seeded staged with single rows and unstaged. No count is seeded staged when the stage does not hold two rows, so that the primary seed is not one whose stage the shared memory refuses. That holds for the estimate's reading of a row: the kernel's row tensors are as wide as the row and each is loaded once. It is no promise that the seed renders: the lowering stages only the loads it proves aligned to 16 bytes, and refuses a staged seed when it proves that of none. - `_staged_rows`: an estimate of the rows of features that the target's shared memory stages, from the tensors the kernel loads whose last dim is the row's length. The shared memory is what a block of the device opts in to and, below sm_90, no more than the 48 KiB of static allocations that the per-block check holds a stage to there (the bottom commit of the stack). It shapes the seeds only: the lowering charges each config's stage exactly and refuses the one that exceeds the shared memory. - The entries lead the family's list for such a row, before the lattice's, and get the occupancy twins that the lattice's entries get. A twin's row group is at most the rows its CTA runs, as the 8-wide entries' is (256 rows on 148 SMs are two rows a CTA: a group of two, where four would stage masked rows). A row that is a power of two keeps its seeds: `_tiled_entries` returns none for it. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel whose rows are no power of two changes, and with it the fingerprint of its autotune cache entry. Tests (test/test_cute_resident_row_widths.py, CPU): the thread counts of six row lengths, among them one that 256 and 512 threads do not tile and one that no whole warps tile; the entries for stages of eleven, four, three and one rows and for an unknown stage; the staged-row estimate of layer_norm_bwd at three fp32 widths and one bf16 width, and for a model of an sm_80 target, which stages two rows of 2560 fp32 features where sm_100 stages eleven, and whose primary seed is then the one-row stage and renders; the seeds of layer_norm_bwd at 2560 features; the row group of an occupancy twin at 256 rows; and at 15872 features a render of the primary seed, whose staged twin the lowering refuses and renders with the shared memory doubled. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27376 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); test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py and test_cute_resident_reductions_codegen.py on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1294 passed. The seeds are read on a device by the tests of the GPU lane, which 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead ran with this commit on origin/main f615a9b, before #4763 to #4767 landed and before the estimate took the static limit below sm_90; none of these changes a seed of these inputs on this device (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4819, branch: choijon5/cute-norm-bwd/9
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction kernels whose rows are 32-bit: examples/layer_norm.py::layer_norm_bwd and examples/rms_norm.py::rms_norm_bwd with fp32 inputs. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, origin/main (f615a9b) -> this commit: - layer_norm-bwd (4096, 4096): 56.9 us -> 49.8 us - layer_norm-bwd (4096, 1024): 24.8 us -> 24.7 us - rms_norm-bwd (2048, 1024), (2048, 2048), (2048, 4096) and (2048, 16384): 14.8, 19.5, 30.0 and 93.2 us -> 14.6, 19.1, 29.6 and 93.2 us - rms_norm-bwd (2048, 32768): a search ends in one of two kinds of layout at either commit. origin/main, four searches (the nightly's search seed at f615a9b, seeds 1 and 2, and the nightly's seed again at 57009cb): 365.4, 321.6, 324.6 and 807.4 us. This commit, three searches (the nightly's seed, then seeds 1 and 2): 809.4, 242.8 and 802.5 us. The next commit seeds a layout that reloads this row, and its search ends at 201.8 us 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 five other inputs up to 16384 features end where they did, within 0.4 us: the vector width is a knob, which the search moves from the 8-wide seeds by itself. Pinned, the seeds take 21% to 29% less kernel time 4-wide than 8-wide (4096 rows of layer_norm_bwd): 13.9 -> 9.9 us at 1024 features, 25.6 -> 20.1 us at 2048, 45.5 -> 34.3 us at 4096 and 88.0 -> 64.9 us at 8192. Kernels with 16-bit rows keep their seeds. At 32768 features of rms_norm_bwd no seed holds the row in its threads' registers, at origin/main or here: the search starts from seeds that spill (the 256-thread one runs 2267 us) and ends where its random path takes it: in a layout of 240 to 365 us, or in one of about 805 us, which is where one of origin/main's four searches and two of this commit's three ended. The twin that this commit adds at that width spills as well. The layout that origin/main's first search found renders the same kernel at this commit and runs 365 us pinned, as the search times it (the call with its host sum; the kernel alone takes 319.6 us of it). The commit above seeds a reloading layout for this row (195.7 us of kernel time), which a search cannot end behind, so the two are best landed together. The resident seed lattice's 256- and 512-thread entries and the vector rows carry 8-wide feature vectors: 16 bytes of a 16-bit row, which is one async copy or one vector load. Of a 32-bit row 16 bytes are 4 features, and an 8-wide vector is two copies. The 4-wide layout took 21% to 29% less time on fp32 rows (B200, examples/layer_norm.py::layer_norm_bwd, 4096 rows, staged, kernel time in us): features threads row group 8-wide 4-wide 1024 128 4 13.9 9.9 2048 256 2 25.6 20.1 4096 256 2 45.5 34.3 4096 512 2 47.4 35.7 8192 256 1 88.0 64.9 8192 512 1 86.0 65.1 For a kernel whose rows are all 32-bit (`_packet_vector`: the features in 16 bytes of the tensors the kernel loads whose last dim is the row's length), the family repeats its 8-wide entries 4-wide. The twins follow the existing seeds, which keep their rank, and a layout the list already holds is not repeated. A kernel of 16-bit rows keeps its seeds, and so does one whose rows' element sizes differ or are unknown. A 32-bit row is twice the bytes of a 16-bit one, so a staged twin takes the largest row group, of its entry's halved down to one, whose two slots the shared memory holds (`_staged_rows`), and no staged twin is seeded when not even one row's slots fit. Of 4096 fp32 features the stage holds seven rows: the four-row group of 512 threads becomes two. Of 8192 it holds three: every staged twin is one row. Those are the layouts of the table, which all render. Of 16384 features the twins are the unstaged ones. Compatibility: CuTe only, autotuner seeds only. The compiler seed list of a resident reduction kernel with 32-bit rows grows, and with it the fingerprint of its autotune cache entry changes. Tests (test/test_cute_resident_packet_seeds.py, new, CPU): the packet of fp32, bf16 and fp16 rows; for fp32 rows of 1024, 2048, 4096, 8192 and 16384 features the seeds are the previous ones followed by the listed 4-wide twins, in order; at 8192 features the one-row staged twin renders, and its two-row form, which the lowering refuses, renders with the shared memory doubled; a bf16 row's seeds are unchanged. The module holds no GPU test, so `test/shard_durations.json` has no entry for it. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27386 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); the new module and the four resident row modules beside it (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py) on both lanes with GPU 4, GPU lane 10 passed, CPU lane 1304 passed. The commit changes seeds only; the tests that read them on a device 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; CPU lane 27408 passed, 684 skipped and the same 1 failed. The cold autotunes of the lead: origin/main at f615a9b and at 57009cb (seeds 1 and 2, and the nightly's seed again for the widest rms_norm-bwd input), this commit on each; #4763 to #4767, which landed in between and since, leave the seeds and the code of these kernels as they were (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4820, branch: choijon5/cute-norm-bwd/10
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: resident reduction rows too wide for their threads' registers: examples/layer_norm.py::layer_norm_bwd from 12800 fp32 features and examples/rms_norm.py::rms_norm_bwd at 32768. TritonBench on a B200, one cold full autotune per input, the time the search measured for its winner, the commit below -> this commit: - rms_norm-bwd (2048, 32768): 809.4 us -> 201.8 us (origin/main: 321.6 to 807.4 us over four searches) - layer_norm-bwd (4096, 12800): 138.2 us -> 126.1 us - layer_norm-bwd (4096, 14336): 171.0 us -> 134.1 us 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 other inputs end where they did, 10752 and 15872 features among them (103.5 us -> 105.5 us and 222.2 us -> 218.0 us). Each of the three searches now ends at a reloading layout that the commit seeds: pinned, the best layout that holds the row's values against the best that reloads them is 121.2 us -> 103.5 us of kernel time at 12800 features of layer_norm_bwd, 151.2 us -> 111.5 us at 14336, and 319.6 us -> 195.7 us at 32768 of rms_norm_bwd, where the holding layout is the one that origin/main's first search found. A layout whose threads hold their row gains nothing from a reload (layer_norm_bwd at 10752 features over 256 threads: 81.6 us held, 84.2 us reloading), and such a seed gets no reloading twin. The resident reduction schedules run a row in two passes. The first reduces it; the second reads, for each feature, values the first computed, which a thread holds in register fragments: five values a feature in examples/layer_norm.py::layer_norm_bwd, three in examples/rms_norm.py::rms_norm_bwd. A thread addresses 255 registers and shares the 65536 of its SM with the other threads of its block. Past that the values go to the thread's local memory, and both passes take several times as long. No thread count avoids it for a wide row: 1024 threads have 64 registers each. - `cute_reduction_reloads` "gmem" now applies to the resident schedules (`ResidentReductionOptions.reload_blocks`). The second pass of such a row holds none of the first pass's values in registers: it runs the statements that computed them again, on the row's loads, which come from the stage under the pipelined schedule and from global memory, where the row is still in L2, under the resident one. The knob is the per-block one that the rolled reductions read, with the meaning it has there. The other modes, among them the default "auto", hold the values as before. - Three rows keep their registers under "gmem" too, so every config that compiled before compiles now. A row of the deferred serial row schedule, which has no barrier after the second pass (the next group's copies would land in the slot the pass still reads). A row whose second pass reads a value computed from a carry, which holds the next row's value by then. And a row whose reload fails a proof that the holding pass passes: the statements that run again bring what they read into the second pass, and a row group runs a row's own statements again for each of its rows, which it proves for statements that load nothing and compute an integer or a boolean or assign a floating-point literal, so a group that would run an FP16 value of the row again keeps the holding pass (`_plan` tries the reloading pass and falls back). - A row that the holding pass refuses can compile under "gmem": that pass keeps FP32 values only, and a boolean of the feature that the second pass reads is computed again. - Seeds: where an estimate says that a seed's row passes its threads' registers (`_feature_values`, `_spills`), the family plants a reloading twin after the existing seeds, when there is one. A staged seed keeps its layout, and has no twin when the stage does not hold the two slots of its row group. An unstaged one reads global memory twice, which is faster the more warps load at once: its twin runs on the most whole warps that tile the row in 16-byte vectors, and it has none when no whole warps do. Twins that come out as one layout are seeded once. The estimate places seeds only; the lowering takes any config. Pinned layouts on a B200 (fp32, kernel time in us, best layout of each kind; "held" keeps the values in registers): kernel, rows x features held reloading layer_norm_bwd 4096 x 10752 81.6 staged 84.2 staged layer_norm_bwd 4096 x 12800 121.2 unstaged 103.5 staged layer_norm_bwd 4096 x 14336 151.2 unstaged 111.5 staged layer_norm_bwd 4096 x 15872 198.4 unstaged 205.5 unstaged rms_norm_bwd 2048 x 16384 86.4 unstaged 85.4 unstaged rms_norm_bwd 2048 x 32768 319.6 unstaged 195.7 unstaged At 10752 features 256 threads hold the row (42 features a thread) and a reload gains nothing: those seeds get no twin. The lattice's 128-thread staged seed carries 84 features a thread there and gets one, as it does from 6144 features on. At 15872 the stage does not fit two rows, and the unstaged reload is on par. At 32768 features of rms_norm_bwd the held layout is the one that origin/main's first search found (1024 threads in two-row groups, one row a CTA); the family's own held seed of 256 threads runs 2267 us. Reloading from global memory is slower than holding wherever the registers fit (7680 features over 256 threads: 196.5 us against 77.7 us), which is why only "gmem" selects it. Compatibility: CuTe only. A config with a resident schedule and "gmem" for the row's reduction rendered the holding pass before and renders the reloading one now: the same results from other code. The family's seeds said "register", and "auto" is unchanged, so no seed or default config renders differently. A kernel gets compiler seeds: the reloading twins of its seeds whose row passes their threads' registers by the estimate (`_spills`), where a twin exists (the Seeds item above) and its coarse tile is within the kernel's block sizes, each layout once. They change the fingerprint of its autotune cache entry. The rule reads the features a thread carries, so no width is a threshold. Examples, fp32, at the row counts of TritonBench: layer_norm_bwd gets the twin of its 128-thread staged seed at 6144, 7680, 9216 and 10752 features (48 features a thread and more) and none at 5120, four twins at 12800 and 14336, one at 15872 and 16384, and two at 3104, which only 32 threads tile; rms_norm_bwd gets none at 6144 and two from 7680 (60 features a thread), staged up to 8192 and unstaged at 16384 and 32768. Each unstaged twin is also seeded with the local tree. A resident config with "gmem" can now compile a row that the holding pass refuses (above). The one change outside the backend is the docstring of `CuteReductionReloadSpec` in helion/autotuner/config_spec.py, which now describes the knob on the resident schedules: the rows that keep their registers under "gmem" and the row that compiles under it alone. Tests: - test/test_cute_resident_reductions.py runs the rewritten loop nest on CPU against the loop's closed form: a reloading second pass holds no register fragment and loads each row twice from global memory, or once when staged (two widths, two row groups, both schedules); a value computed from a carry stays in registers; a row group that would run an FP16 value of the row again keeps the holding pass, where a group of one row reloads (both rewrites are checked to be the resident form); a row whose second pass reads a boolean of the feature is rewritten under "gmem" only, and matches the closed form. - test/test_cute_resident_row_widths.py renders both examples: the reloading row's fragments are gone and its vector loads double (12800 features staged, 32768 unstaged); a deferred row keeps its registers; `_spills` at the boundaries of the two limits; the reloading seeds of both examples at six widths (12800, 10752, 6144 and 3104 features of layer_norm_bwd, 32768 and 7680 of rms_norm_bwd) and the four widths that seed none; the twins follow the previous seeds, which keep their rank. Three `cuda` cases run reloading rows against eager autograd; the staged one is 2048 features wide, and a CPU test adds up the static shared memory of each case: at most the 48 KiB that libNVVM compiles below sm_90. - test/test_cute_resident_packet_seeds.py leaves the reloading seeds out of the lists it compares. - Differential fuzz against eager autograd on a B200, at this commit before the two fixes at the bottom of the stack went under it (the per-block check of a stage and the device of the reduction budget, which change nothing on this host): the compiler seeds, 10 random configs and 80 random resident configs with "gmem", for each of eleven cases (layer_norm_bwd at 100 x 1000, 100 x 2560 and 100 x 4096 fp32, 100 x 2560 bf16, 64 x 12800 fp32 and 64 x 7680 fp16; rms_norm_bwd at 100 x 2560 and 100 x 4096 fp32, 100 x 6144 bf16, 64 x 16384 fp16 and 64 x 32768 fp32). 750 configs compiled and matched, 407 of them with a reloading pass; the compiler refused 595; none was wrong and none crashed. Testing: B200, CUDA 13.2, torch 2.15.0+cu132, on origin/main 74ac871. At this commit: `scripts/run_tests.py --backend cute --lane cpu`, 27408 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); the five resident row modules (test_cute_resident_row_widths.py, test_cute_resident_reductions.py, test_cute_resident_reduction_runtime.py, test_cute_resident_reductions_codegen.py, test_cute_resident_packet_seeds.py) on both lanes with GPU 4, GPU lane 13 passed, CPU lane 1326 passed. The GPU lane ran at the tip of the stack, the commit above this one, which adds the Parameter guard in other files: `scripts/run_tests.py --backend cute --gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped. For the docstring in helion/autotuner/config_spec.py, the Triton and Pallas interpreter lanes ran at the tip 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, ptxas C7600, the same at origin/main); CPU lane 5168 passed, 627 skipped; `scripts/run_tests.py --backend pallas-interpret test/test_pallas.py`, 272 passed, 44 skipped. The cold autotunes that the lead gives for this commit ran at the tip of the stack on origin/main 57009cb, before #4766 and #4767 landed and before the fixes at the stack's bottom were reworked: the commit above changes the host path of a launch and no kernel, those fixes nothing on this host, and #4766 and #4767 nothing in these kernels (at the tip of the stack, the compiler seeds, the default config and 40 random configs of each of the 15 TritonBench inputs (1023 configs) render the same code, or are refused alike, before and after the rebase onto #4766 and the one onto #4767). The lanes ran before the last review of the stack's bottom commit 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. autoreview passed: claude, codex stack-info: PR: #4821, branch: choijon5/cute-norm-bwd/11
choijon5
force-pushed
the
choijon5/cute-norm-bwd/11
branch
from
November 29, 2026 20:12
b78f030 to
3e173e9
Compare
choijon5
added a commit
that referenced
this pull request
Nov 29, 2026
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: those with a fast relaunch entry whose first launch passed an `nn.Parameter` among its tensors; later launches with the same argument types and metadata take the C++ guard instead of the generated Python one. examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter): the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated code and no device time changes. 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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's `TensorGuards`) and falls to a generated Python guard when that call misses. `cxx_tensor_guards` built the C++ guard only when the type of every tensor argument was exactly `torch.Tensor`. A kernel that is handed a module's weight, an `nn.Parameter`, therefore had no C++ guard, and each of its launches ran the Python one. TritonBench's rms_norm passes its weight that way. `TensorGuards` pins the exact Python type of each argument it is built from, and a Parameter reads its metadata (dtype, sizes, strides, device, data pointer) as a plain tensor does, so the guard holds for it as it is. `cxx_tensor_guards` now takes `torch.Tensor` and `torch.nn.Parameter`. Any other subclass, one of Parameter included, may answer for its metadata itself and stays with the Python guards, which ask it. B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight a Parameter; host time of back-to-back calls, in us: before after launcher (default_cute_launcher) 9.3 6.4 kernel call 26.9 25.7 autograd backward call 73.5 70.9 Each figure is the median of 15 batches of 200 calls, and each row is a run of its own. The launcher's row resolves the change: inside it the generated Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows below it hold that launch but do not resolve it: the kernel call read 24.5 to 27.9 us across runs of one build. With a plain tensor for the weight the launcher takes 6.2 us before and 6.5 us after, which is that noise: an entry built from plain tensors takes the path it took. Compatibility: CuTe only; no change to generated code or configs. A relaunch entry built from a Parameter now carries a C++ guard that pins the Parameter type. A plain tensor passed in its place misses the C++ check and is decided by the Python guards, which decided every launch of such an entry before. Tests: - test/test_cute_launcher_alignment.py: a Parameter gets a C++ guard that accepts another Parameter of the same metadata and misses for a plain tensor and for another shape; a Tensor subclass and a Parameter subclass get none. - test/test_cute_fast_relaunch.py (device): a kernel launched with a Parameter relaunches through the C++ check; a plain tensor in its place misses that check and still relaunches, without the marshalling launch. Testing: B200, CUDA 13.2. The launcher modules (test_cute_launcher_alignment.py, test_cute_fast_relaunch.py, test_cute_launcher_arg_signature.py), `scripts/run_tests.py --backend cute --gpus 0 <modules>`: GPU lane 61 passed, CPU lane 166 passed. `./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, 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: #4822, branch: choijon5/cute-norm-bwd/12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked PRs:
[cutedsl] Guard nn.Parameter launch arguments in the C++ relaunch guard
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: those with a fast relaunch entry whose first launch passed an
nn.Parameteramong its tensors; later launches with the same argumenttypes and metadata take the C++ guard instead of the generated Python one.
examples/rms_norm.py::rms_norm_bwd at 2048 x 1024 fp32 on a B200, called as
TritonBench's rms_norm-bwd calls it (the weight is a module's Parameter):
the launcher takes 9.3 us -> 6.4 us of host time a launch. No generated
code and no device time changes.
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.
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.
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.
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 fast relaunch checks a launch's tensors in one C++ call (Dynamo's
TensorGuards) and falls to a generated Python guard when that callmisses.
cxx_tensor_guardsbuilt the C++ guard only when the type of everytensor argument was exactly
torch.Tensor. A kernel that is handed amodule's weight, an
nn.Parameter, therefore had no C++ guard, and each ofits launches ran the Python one. TritonBench's rms_norm passes its weight
that way.
TensorGuardspins the exact Python type of each argument it is builtfrom, and a Parameter reads its metadata (dtype, sizes, strides, device,
data pointer) as a plain tensor does, so the guard holds for it as it is.
cxx_tensor_guardsnow takestorch.Tensorandtorch.nn.Parameter. Anyother subclass, one of Parameter included, may answer for its metadata
itself and stays with the Python guards, which ask it.
B200, examples/rms_norm.py::rms_norm_bwd, 2048 x 1024 fp32 with the weight
a Parameter; host time of back-to-back calls, in us:
launcher (default_cute_launcher) 9.3 6.4
kernel call 26.9 25.7
autograd backward call 73.5 70.9
Each figure is the median of 15 batches of 200 calls, and each row is a run
of its own. The launcher's row resolves the change: inside it the generated
Python guard takes 2.9 to 3.4 us and the C++ check 0.45 us. The two rows
below it hold that launch but do not resolve it: the kernel call read 24.5
to 27.9 us across runs of one build. With a plain tensor for the weight the
launcher takes 6.2 us before and 6.5 us after, which is that noise: an
entry built from plain tensors takes the path it took.
Compatibility: CuTe only; no change to generated code or configs. A
relaunch entry built from a Parameter now carries a C++ guard that pins the
Parameter type. A plain tensor passed in its place misses the C++ check and
is decided by the Python guards, which decided every launch of such an
entry before.
Tests:
accepts another Parameter of the same metadata and misses for a plain
tensor and for another shape; a Tensor subclass and a Parameter subclass
get none.
Parameter relaunches through the C++ check; a plain tensor in its place
misses that check and still relaunches, without the marshalling launch.
Testing: B200, CUDA 13.2. The launcher modules
(test_cute_launcher_alignment.py, test_cute_fast_relaunch.py,
test_cute_launcher_arg_signature.py),
scripts/run_tests.py --backend cute --gpus 0 <modules>: GPU lane 61 passed, CPU lane 166 passed../lint.sh checkpasses. The stack was since rebased onto origin/main74ac871 (after #4766 and #4767 landed, without conflicts), and the lanes
ran again at the tip of the stack, this commit:
scripts/run_tests.py --backend cute --gpus 4,5,6,7, GPU lane 16010 passed, 2374 skipped; CPUlane 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