Repository navigation
[cutedsl] Run resident reduction rows whose length is no power of two - #4818
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: 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
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 28, 2026 17:16
54e3dcd to
ee0b06f
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 28, 2026 17:16
9109f36 to
682935f
Compare
This was referenced Nov 28, 2026
This was referenced Nov 28, 2026
choijon5
marked this pull request as draft
November 28, 2026 20:36
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 28, 2026 20:37
682935f to
93a81d3
Compare
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/8
branch
from
November 29, 2026 18:15
93a81d3 to
dfb545e
Compare
choijon5
marked this pull request as ready for review
November 29, 2026 18:15
choijon5
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 29, 2026 20:06
29e9e3e to
7b16a2e
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:07
dfb545e to
dd749aa
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: 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
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 29, 2026 20:08
7b16a2e to
dcbbebf
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:08
dd749aa to
f65f982
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: 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
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 29, 2026 20:09
dcbbebf to
c4477a3
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:09
f65f982 to
50ef0a5
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: 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
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 29, 2026 20:10
c4477a3 to
03b504c
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:10
50ef0a5 to
7e38ef7
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: 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
force-pushed
the
choijon5/cute-norm-bwd/7
branch
from
November 29, 2026 20:11
03b504c to
04ac742
Compare
choijon5
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:11
7e38ef7 to
ff6357a
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: 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
force-pushed
the
choijon5/cute-norm-bwd/8
branch
from
November 29, 2026 20:12
ff6357a to
3a1c245
Compare
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] Run resident reduction rows whose length is no power 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: 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:
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 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:
weight.new_zeros(n)is typed 4096 for n = 2560. An op between it andx[tile, :]pins the slice's dim to 4096, and the reductions over thepinned values take a second dim: the feature axis has two block ids, and
the resident plan needs one.
Backend.joins_factory_extents(defaultoff) 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 ofpinned subscript extents). An uninitialized tensor of a joined extent
(
torch.empty,new_empty) is created as zeros, because Inductor lowersaten.emptyfrom a literal size. A factory that asks for a memoryformat 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 overone row loop, a
forloop overhl.tileorhl.grid, whose calls are ofa closed list. A
whileloop over the rows keeps the padded literal: thefamily 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
forloop over a Python sequence keepsthe 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, aTensor.mmthat the kernel savedunder 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 thehost AST does not show, keep the padded literal, which their lowerings
read. A call in the branch of a constant
ifthat type propagation skipsis not part of the nest: layer_norm_bwd without a bias
(
compute_bias_grad=False) is the same nest as with one.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:
pairs complete subtrees; 128 threads over 2560 features carry 20 each);
whole of a shorter row): the last copy would run into the next slot;
the rows of a store are apart relied on the row's span dividing 232,
which a power of two does; for any other span, no difference of two rows
may come within a span of 232.
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=Falsetheaccumulators' 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_rowsoptionnow says that a resident row does not read it.
Tests:
cudacases): thenest'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,@, asaved
mmmethod, awhileloop over the rows, whose default configrenders as it did, a
forloop over a Python tuple in the row loop'splace, 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.
5, 15 and 31 warps, with one and three statistics, in both sum helpers,
interpreted on CPU.
span that is no power of two, and the staged row of whole copies.
test/shard_durations.jsonrecords the new module (13.8 s in the CuTeGPU 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 lane16010 passed, 2374 skipped, CPU lane 27408 passed, 684 skipped and the same
1 failed;
scripts/run_tests.py --backend triton --gpus 5, GPU lane 3408passed, 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, 2370skipped. 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 checkpasses.autoreview passed: claude, codex