Skip to content

[cutedsl] Decline resident carries the rewrite reads or updates out of order - #4813

Merged
choijon5 merged 1 commit into
mainfrom
choijon5/cute-norm-bwd/3
Nov 29, 2026
Merged

choijon5 merged 1 commit into
mainfrom
choijon5/cute-norm-bwd/3

Conversation

@choijon5

@choijon5 choijon5 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[cutedsl] Decline resident carries the rewrite reads or updates out of order

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

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

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

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

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

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

    carry = cutlass.Float32(row)
    

    was classified with the row's statements (_tainted drops a name that
    is assigned from values that do not vary) and ran ahead of the feature
    loop, whose load then replaced the new value by the old one and stored
    that back. Every schedule of the family did so: resident, grouped and
    pipelined. _plan now declines a loop in which a carry ends up not
    varying.

  • A reduction's input is added to its sum when the feature's statements
    have run (_emit), wherever the row has the reduction. A row assigns a
    name once, so the input holds then what the reduction read, unless it is
    a carry that the row updates below the reduction:

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

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

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

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

Tests (test/test_cute_resident_reductions.py):

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

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

autoreview passed: claude, codex

…f order

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

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

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

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

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

      carry = cutlass.Float32(row)

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

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

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

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

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

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

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

autoreview passed: claude, codex

stack-info: PR: #4813, branch: choijon5/cute-norm-bwd/3
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 28, 2026
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/2 branch from 1b9abae to 0dd6819 Compare November 28, 2026 17:16
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from 5635d40 to 1669288 Compare November 28, 2026 17:16
@choijon5
choijon5 marked this pull request as draft November 28, 2026 20:36
@choijon5
choijon5 changed the base branch from choijon5/cute-norm-bwd/2 to main November 28, 2026 20:36
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from 1669288 to a9dec31 Compare November 28, 2026 20:37
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/2 November 28, 2026 20:37
@choijon5
choijon5 marked this pull request as ready for review November 28, 2026 20:38
@choijon5
choijon5 marked this pull request as draft November 29, 2026 18:14
@choijon5
choijon5 changed the base branch from choijon5/cute-norm-bwd/2 to main November 29, 2026 18:14
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from a9dec31 to e6b95ab Compare November 29, 2026 18:15
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/2 November 29, 2026 18:15
@choijon5
choijon5 marked this pull request as ready for review November 29, 2026 18:15
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/2 branch from 604ae30 to 4c19a1c Compare November 29, 2026 20:06
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from e6b95ab to 1e4f86a Compare November 29, 2026 20:06
choijon5 added a commit that referenced this pull request Nov 29, 2026
…f order

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

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

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

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

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

      carry = cutlass.Float32(row)

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

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

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

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

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

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

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

autoreview passed: claude, codex

stack-info: PR: #4813, branch: choijon5/cute-norm-bwd/3
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from 1e4f86a to 2be3f27 Compare November 29, 2026 20:08
@choijon5
choijon5 changed the base branch from choijon5/cute-norm-bwd/2 to main November 29, 2026 20:08
@choijon5
choijon5 merged commit 255e3a3 into main Nov 29, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants