Skip to content

[cutedsl] Ask the ATen build first in the host sum helpers - #4815

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

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

Conversation

@choijon5

@choijon5 choijon5 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[cutedsl] Ask the ATen build first in the host sum helpers

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

Kernels: every kernel with a host sum tail that a search runs with
cute_host_paired_sum "mapped" or "narrow", on any Torch build but the one
audited one: examples/layer_norm.py::layer_norm_bwd and
examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel
gets back the host time of proofs that could not succeed: 73.7 us to
54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below).
No generated kernel changes, and no launch takes another time on the
device. TritonBench times layer_norm-bwd and rms_norm-bwd around the
Python call, so those readings carried the cost wherever the search picked
one of the two values.

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

  • Generated code, rendered on CPU for the B200 target: all 1,298 configs
    that main renders for 145 attention cases generate the same source with
    the stack, and no seed list changes. The cases are the kernels of
    examples/attention.py (forward and backward), flex_attention.py,
    blackwell_attention.py and fp8_attention.py, and of
    benchmarks/cute/attnbwd_kernels.py and attngym_kernels.py; the configs
    are each case's default and every compiler seed.
  • The same with the attention stack ([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.

With cute_host_paired_sum "mapped" or "narrow" the generated host function
hands its sum tails to a runtime helper (try_paired_sum_cast,
try_single_sum_cast, try_full_sum). The helper launches one kernel that
reproduces ATen's addition tree, or returns None and the original statements
run. The tree is audited for one Torch build (_compatible_aten_sum: the
build's git version and four header hashes), and the helpers asked for the
build last, after the proofs of the dispatch state, the tensors and the
plan. On every other build each call of the kernel ran those proofs and
then declined: the device ran what "off" runs, and the host paid for the
proofs.

That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's
CuTe test jobs install the current nightly, and the benchmark jobs a
release. The autotuner does not see the cost: it times the device, where
the three values are the same kernels on such a build, and the compiler
seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of
them wins by noise then costs every call its proofs.

The helpers now ask for the build first. The answer is cached, so a
declined call costs a function call. Nothing else changes: an admitted call
runs the same proofs and the same kernel, a declined one the original
statements, and the generated code is the same.

B200, torch 2.15.0+cu132 (not the audited build),
examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned
layout; host time of the kernel call at the parent commit and at this one,
median of 300 calls, in us:

cute_host_paired_sum off mapped narrow
parent 54.5 73.7 73.4
this commit 54.0 54.1 54.3

A declining call of the helper alone, 100000 calls: paired 12.3 us before
and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel
picked "mapped" at 4096 features and "narrow" at 6144.

One more order is fixed in try_full_sum. It computed a divisor's
reciprocal, with tensors, before it looked at the dispatch state, so a mode
of the caller saw those operations. Under torch.device("cuda") every call
of a scaled full sum built two device scalars, divided them and
synchronized before it declined, which is device work that these calls no
longer do; under torch.device("meta") the call raised instead of
declining. The reciprocal is now computed after that check.

Compatibility: CuTe only, no change to generated code, configs or results.
On the audited build the first call of a helper reads the four headers even
when its other proofs would have declined; that happens once in a process,
as before.

Two test modules of the helpers had fallen behind the launcher, unseen:
test/test_cute_host_paired_sum_abi.py and
test/test_cute_host_single_sum_abi.py ran on the audited build only (their
skip compared the build's git version), which no CI job installs.

  • Their stand-in launches reached the launcher's CUDA context check
    ([cutedsl] Fix wrong results, crashes and refusals in generated CuTe code #4456), which the modules forbid as they do every use of the device,
    and they asserted the compile options of before the division option.
    They skip the check for the stand-in launch and assert the launcher's
    current options.
  • They run on every build now. What they check (the launches the bound
    host makes, with their arguments, streams and launchers) does not depend
    on the installed ATen, so they override the helpers' answer about the
    build (_compatible_aten_sum, and _compatible_aten_half_cast for the
    single sum's FP16 outputs) next to the tensor and dispatch checks they
    already override, and the skip is gone. The skip was also narrower than
    the gate it stood for: a build of that git version with other headers,
    or for ROCm, would have run the tests and failed them.
  • They are cpu_only modules now, which the CPU lane runs without a
    device. One query on their path needed one: the launcher asks the CUDA
    runtime whether its stream is being captured, which raises in a process
    that sees no device. The tests answer it next to the context check (the
    stand-in launches are eager ones). test/shard_durations.json loses the
    modules' two zero placeholders: a module outside the GPU lane has no
    entry there.
  • They no longer depend on the launches before them, nor change the ones
    after. The launcher keeps the SM count of each device for the process
    (_CUTE_NUM_SM_CACHE): an earlier launch on cuda:0 answered in place of
    the stand-in count of 148 that the tests assert, and the stand-in count
    stayed behind for later launches. A B200 has 148 SMs, so neither showed
    here. The tests run on an empty copy of the cache.
    On the audited build their 6 tests fail at origin/main and pass here; on
    every other build they were skipped and now run.

Tests:

  • Three new tests, one for each helper: with the build declined the helper
    returns None and enters none of its other proofs (they are patched to
    raise, the query for a function mode included, and the arguments are no
    tensors).
  • A new test calls try_full_sum with a divisor under a function mode, a
    dispatch mode and a device mode: it declines without an operation. It
    fails on the old order.
  • The tests of the other reasons to decline (CPU tensors, dispatch modes,
    subclasses and duals, unsupported 1-D tensors) admit the build, so that
    an unaudited build is not what declines in them.
  • Two tests made the build check raise, to show that a singleton plan
    declines before it. They admit the build now and keep the launcher
    raising, and the single sum its half-cast header check.

Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules
of the helpers on both lanes at this commit, with GPU 6
(test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py,
test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py,
test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the
audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment):
GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main
(f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules),
CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack
run on: GPU lane 9 passed, 40 skipped (the tests that need the audited
build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed
ones, where they were skipped. The lanes at the tip of the stack, which
holds this commit, on torch 2.15.0, run while the two ABI modules were
still in the GPU lane: scripts/run_tests.py --backend cute --gpus 2,3,
GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane
27318 passed, 684 skipped, 1 failed
(test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well and passes on the nightly). ./lint.sh check passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: scripts/run_tests.py --backend cute --gpus 4,5,6,7, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); scripts/run_tests.py --backend triton --gpus 5, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

Review notes:

  • Not valid here: "a declined full sum can clobber its own input"
    (helion/_compiler/cute/host_full_sum.py, which this commit does not
    touch). The defect is real and on origin/main; the next commit of the
    stack fixes it: "[cutedsl] Fix the full host sum's fallback and its
    divisor's reciprocal".
  • Not valid here: "five source-generation tests of
    test/test_cute_host_full_sum.py are gated on the ATen build and sit
    outside a cpu_only module". It predates this commit, which leaves those
    tests alone; the same next commit makes the module cpu_only and drops
    the gate from them.

autoreview passed: claude, codex

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

Kernels: every kernel with a host sum tail that a search runs with
`cute_host_paired_sum` "mapped" or "narrow", on any Torch build but the one
audited one: examples/layer_norm.py::layer_norm_bwd and
examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel
gets back the host time of proofs that could not succeed: 73.7 us to
54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below).
No generated kernel changes, and no launch takes another time on the
device. TritonBench times `layer_norm-bwd` and `rms_norm-bwd` around the
Python call, so those readings carried the cost wherever the search picked
one of the two values.

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

With `cute_host_paired_sum` "mapped" or "narrow" the generated host function
hands its sum tails to a runtime helper (`try_paired_sum_cast`,
`try_single_sum_cast`, `try_full_sum`). The helper launches one kernel that
reproduces ATen's addition tree, or returns None and the original statements
run. The tree is audited for one Torch build (`_compatible_aten_sum`: the
build's git version and four header hashes), and the helpers asked for the
build last, after the proofs of the dispatch state, the tensors and the
plan. On every other build each call of the kernel ran those proofs and
then declined: the device ran what "off" runs, and the host paid for the
proofs.

That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's
CuTe test jobs install the current nightly, and the benchmark jobs a
release. The autotuner does not see the cost: it times the device, where
the three values are the same kernels on such a build, and the compiler
seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of
them wins by noise then costs every call its proofs.

The helpers now ask for the build first. The answer is cached, so a
declined call costs a function call. Nothing else changes: an admitted call
runs the same proofs and the same kernel, a declined one the original
statements, and the generated code is the same.

B200, torch 2.15.0+cu132 (not the audited build),
examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned
layout; host time of the kernel call at the parent commit and at this one,
median of 300 calls, in us:

  cute_host_paired_sum    off    mapped   narrow
  parent                  54.5    73.7     73.4
  this commit             54.0    54.1     54.3

A declining call of the helper alone, 100000 calls: paired 12.3 us before
and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel
picked "mapped" at 4096 features and "narrow" at 6144.

One more order is fixed in `try_full_sum`. It computed a divisor's
reciprocal, with tensors, before it looked at the dispatch state, so a mode
of the caller saw those operations. Under `torch.device("cuda")` every call
of a scaled full sum built two device scalars, divided them and
synchronized before it declined, which is device work that these calls no
longer do; under `torch.device("meta")` the call raised instead of
declining. The reciprocal is now computed after that check.

Compatibility: CuTe only, no change to generated code, configs or results.
On the audited build the first call of a helper reads the four headers even
when its other proofs would have declined; that happens once in a process,
as before.

Two test modules of the helpers had fallen behind the launcher, unseen:
test/test_cute_host_paired_sum_abi.py and
test/test_cute_host_single_sum_abi.py ran on the audited build only (their
skip compared the build's git version), which no CI job installs.
- Their stand-in launches reached the launcher's CUDA context check
  (#4456), which the modules forbid as they do every use of the device,
  and they asserted the compile options of before the division option.
  They skip the check for the stand-in launch and assert the launcher's
  current options.
- They run on every build now. What they check (the launches the bound
  host makes, with their arguments, streams and launchers) does not depend
  on the installed ATen, so they override the helpers' answer about the
  build (`_compatible_aten_sum`, and `_compatible_aten_half_cast` for the
  single sum's FP16 outputs) next to the tensor and dispatch checks they
  already override, and the skip is gone. The skip was also narrower than
  the gate it stood for: a build of that git version with other headers,
  or for ROCm, would have run the tests and failed them.
- They are `cpu_only` modules now, which the CPU lane runs without a
  device. One query on their path needed one: the launcher asks the CUDA
  runtime whether its stream is being captured, which raises in a process
  that sees no device. The tests answer it next to the context check (the
  stand-in launches are eager ones). `test/shard_durations.json` loses the
  modules' two zero placeholders: a module outside the GPU lane has no
  entry there.
- They no longer depend on the launches before them, nor change the ones
  after. The launcher keeps the SM count of each device for the process
  (`_CUTE_NUM_SM_CACHE`): an earlier launch on cuda:0 answered in place of
  the stand-in count of 148 that the tests assert, and the stand-in count
  stayed behind for later launches. A B200 has 148 SMs, so neither showed
  here. The tests run on an empty copy of the cache.
On the audited build their 6 tests fail at origin/main and pass here; on
every other build they were skipped and now run.

Tests:
- Three new tests, one for each helper: with the build declined the helper
  returns None and enters none of its other proofs (they are patched to
  raise, the query for a function mode included, and the arguments are no
  tensors).
- A new test calls `try_full_sum` with a divisor under a function mode, a
  dispatch mode and a device mode: it declines without an operation. It
  fails on the old order.
- The tests of the other reasons to decline (CPU tensors, dispatch modes,
  subclasses and duals, unsupported 1-D tensors) admit the build, so that
  an unaudited build is not what declines in them.
- Two tests made the build check raise, to show that a singleton plan
  declines before it. They admit the build now and keep the launcher
  raising, and the single sum its half-cast header check.

Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules
of the helpers on both lanes at this commit, with GPU 6
(test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py,
test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py,
test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the
audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment):
GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main
(f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules),
CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack
run on: GPU lane 9 passed, 40 skipped (the tests that need the audited
build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed
ones, where they were skipped. The lanes at the tip of the stack, which
holds this commit, on torch 2.15.0, run while the two ABI modules were
still in the GPU lane: `scripts/run_tests.py --backend cute --gpus 2,3`,
GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane
27318 passed, 684 skipped, 1 failed
(`test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition`,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well and passes on the nightly). `./lint.sh check` passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: `scripts/run_tests.py --backend cute
--gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); `scripts/run_tests.py
--backend triton --gpus 5`, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

Review notes:
- Not valid here: "a declined full sum can clobber its own input"
  (helion/_compiler/cute/host_full_sum.py, which this commit does not
  touch). The defect is real and on origin/main; the next commit of the
  stack fixes it: "[cutedsl] Fix the full host sum's fallback and its
  divisor's reciprocal".
- Not valid here: "five source-generation tests of
  test/test_cute_host_full_sum.py are gated on the ATen build and sit
  outside a cpu_only module". It predates this commit, which leaves those
  tests alone; the same next commit makes the module `cpu_only` and drops
  the gate from them.

autoreview passed: claude, codex

stack-info: PR: #4815, branch: choijon5/cute-norm-bwd/5
@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/4 branch from c44a8f3 to b4c5e61 Compare November 28, 2026 17:16
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from c879ad2 to 1fac11a 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/4 to main November 28, 2026 20:36
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from 1fac11a to 19667ca Compare November 28, 2026 20:37
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/4 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/4 to main November 29, 2026 18:14
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from 19667ca to 1daa07c Compare November 29, 2026 18:15
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/3 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/3 branch from e6b95ab to 1e4f86a Compare November 29, 2026 20:06
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from 1daa07c to c99655b Compare November 29, 2026 20:06
choijon5 added a commit that referenced this pull request Nov 29, 2026
For the norm backward kernels (layer_norm_bwd in examples/layer_norm.py and
rms_norm_bwd in examples/rms_norm.py, which TritonBench runs as
layer_norm-bwd and rms_norm-bwd): part of their hillclimb on the CuTe
backend.

Kernels: every kernel with a host sum tail that a search runs with
`cute_host_paired_sum` "mapped" or "narrow", on any Torch build but the one
audited one: examples/layer_norm.py::layer_norm_bwd and
examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel
gets back the host time of proofs that could not succeed: 73.7 us to
54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below).
No generated kernel changes, and no launch takes another time on the
device. TritonBench times `layer_norm-bwd` and `rms_norm-bwd` around the
Python call, so those readings carried the cost wherever the search picked
one of the two values.

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

With `cute_host_paired_sum` "mapped" or "narrow" the generated host function
hands its sum tails to a runtime helper (`try_paired_sum_cast`,
`try_single_sum_cast`, `try_full_sum`). The helper launches one kernel that
reproduces ATen's addition tree, or returns None and the original statements
run. The tree is audited for one Torch build (`_compatible_aten_sum`: the
build's git version and four header hashes), and the helpers asked for the
build last, after the proofs of the dispatch state, the tensors and the
plan. On every other build each call of the kernel ran those proofs and
then declined: the device ran what "off" runs, and the host paid for the
proofs.

That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's
CuTe test jobs install the current nightly, and the benchmark jobs a
release. The autotuner does not see the cost: it times the device, where
the three values are the same kernels on such a build, and the compiler
seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of
them wins by noise then costs every call its proofs.

The helpers now ask for the build first. The answer is cached, so a
declined call costs a function call. Nothing else changes: an admitted call
runs the same proofs and the same kernel, a declined one the original
statements, and the generated code is the same.

B200, torch 2.15.0+cu132 (not the audited build),
examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned
layout; host time of the kernel call at the parent commit and at this one,
median of 300 calls, in us:

  cute_host_paired_sum    off    mapped   narrow
  parent                  54.5    73.7     73.4
  this commit             54.0    54.1     54.3

A declining call of the helper alone, 100000 calls: paired 12.3 us before
and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel
picked "mapped" at 4096 features and "narrow" at 6144.

One more order is fixed in `try_full_sum`. It computed a divisor's
reciprocal, with tensors, before it looked at the dispatch state, so a mode
of the caller saw those operations. Under `torch.device("cuda")` every call
of a scaled full sum built two device scalars, divided them and
synchronized before it declined, which is device work that these calls no
longer do; under `torch.device("meta")` the call raised instead of
declining. The reciprocal is now computed after that check.

Compatibility: CuTe only, no change to generated code, configs or results.
On the audited build the first call of a helper reads the four headers even
when its other proofs would have declined; that happens once in a process,
as before.

Two test modules of the helpers had fallen behind the launcher, unseen:
test/test_cute_host_paired_sum_abi.py and
test/test_cute_host_single_sum_abi.py ran on the audited build only (their
skip compared the build's git version), which no CI job installs.
- Their stand-in launches reached the launcher's CUDA context check
  (#4456), which the modules forbid as they do every use of the device,
  and they asserted the compile options of before the division option.
  They skip the check for the stand-in launch and assert the launcher's
  current options.
- They run on every build now. What they check (the launches the bound
  host makes, with their arguments, streams and launchers) does not depend
  on the installed ATen, so they override the helpers' answer about the
  build (`_compatible_aten_sum`, and `_compatible_aten_half_cast` for the
  single sum's FP16 outputs) next to the tensor and dispatch checks they
  already override, and the skip is gone. The skip was also narrower than
  the gate it stood for: a build of that git version with other headers,
  or for ROCm, would have run the tests and failed them.
- They are `cpu_only` modules now, which the CPU lane runs without a
  device. One query on their path needed one: the launcher asks the CUDA
  runtime whether its stream is being captured, which raises in a process
  that sees no device. The tests answer it next to the context check (the
  stand-in launches are eager ones). `test/shard_durations.json` loses the
  modules' two zero placeholders: a module outside the GPU lane has no
  entry there.
- They no longer depend on the launches before them, nor change the ones
  after. The launcher keeps the SM count of each device for the process
  (`_CUTE_NUM_SM_CACHE`): an earlier launch on cuda:0 answered in place of
  the stand-in count of 148 that the tests assert, and the stand-in count
  stayed behind for later launches. A B200 has 148 SMs, so neither showed
  here. The tests run on an empty copy of the cache.
On the audited build their 6 tests fail at origin/main and pass here; on
every other build they were skipped and now run.

Tests:
- Three new tests, one for each helper: with the build declined the helper
  returns None and enters none of its other proofs (they are patched to
  raise, the query for a function mode included, and the arguments are no
  tensors).
- A new test calls `try_full_sum` with a divisor under a function mode, a
  dispatch mode and a device mode: it declines without an operation. It
  fails on the old order.
- The tests of the other reasons to decline (CPU tensors, dispatch modes,
  subclasses and duals, unsupported 1-D tensors) admit the build, so that
  an unaudited build is not what declines in them.
- Two tests made the build check raise, to show that a singleton plan
  declines before it. They admit the build now and keep the launcher
  raising, and the single sum its half-cast header check.

Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules
of the helpers on both lanes at this commit, with GPU 6
(test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py,
test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py,
test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the
audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment):
GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main
(f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules),
CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack
run on: GPU lane 9 passed, 40 skipped (the tests that need the audited
build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed
ones, where they were skipped. The lanes at the tip of the stack, which
holds this commit, on torch 2.15.0, run while the two ABI modules were
still in the GPU lane: `scripts/run_tests.py --backend cute --gpus 2,3`,
GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane
27318 passed, 684 skipped, 1 failed
(`test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition`,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well and passes on the nightly). `./lint.sh check` passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: `scripts/run_tests.py --backend cute
--gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); `scripts/run_tests.py
--backend triton --gpus 5`, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

Review notes:
- Not valid here: "a declined full sum can clobber its own input"
  (helion/_compiler/cute/host_full_sum.py, which this commit does not
  touch). The defect is real and on origin/main; the next commit of the
  stack fixes it: "[cutedsl] Fix the full host sum's fallback and its
  divisor's reciprocal".
- Not valid here: "five source-generation tests of
  test/test_cute_host_full_sum.py are gated on the ATen build and sit
  outside a cpu_only module". It predates this commit, which leaves those
  tests alone; the same next commit makes the module `cpu_only` and drops
  the gate from them.

autoreview passed: claude, codex

stack-info: PR: #4815, branch: choijon5/cute-norm-bwd/5
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/3 branch from 1e4f86a to 2be3f27 Compare November 29, 2026 20:08
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from c99655b to 32686e6 Compare November 29, 2026 20:08
choijon5 added a commit that referenced this pull request Nov 29, 2026
For the norm backward kernels (layer_norm_bwd in examples/layer_norm.py and
rms_norm_bwd in examples/rms_norm.py, which TritonBench runs as
layer_norm-bwd and rms_norm-bwd): part of their hillclimb on the CuTe
backend.

Kernels: every kernel with a host sum tail that a search runs with
`cute_host_paired_sum` "mapped" or "narrow", on any Torch build but the one
audited one: examples/layer_norm.py::layer_norm_bwd and
examples/rms_norm.py::rms_norm_bwd among them. Each call of such a kernel
gets back the host time of proofs that could not succeed: 73.7 us to
54.1 us a call for layer_norm_bwd at 4096 x 4096 on a B200 (table below).
No generated kernel changes, and no launch takes another time on the
device. TritonBench times `layer_norm-bwd` and `rms_norm-bwd` around the
Python call, so those readings carried the cost wherever the search picked
one of the two values.

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

With `cute_host_paired_sum` "mapped" or "narrow" the generated host function
hands its sum tails to a runtime helper (`try_paired_sum_cast`,
`try_single_sum_cast`, `try_full_sum`). The helper launches one kernel that
reproduces ATen's addition tree, or returns None and the original statements
run. The tree is audited for one Torch build (`_compatible_aten_sum`: the
build's git version and four header hashes), and the helpers asked for the
build last, after the proofs of the dispatch state, the tensors and the
plan. On every other build each call of the kernel ran those proofs and
then declined: the device ran what "off" runs, and the host paid for the
proofs.

That is the usual case. The pin is one nightly (2.16.0.dev20261026); CI's
CuTe test jobs install the current nightly, and the benchmark jobs a
release. The autotuner does not see the cost: it times the device, where
the three values are the same kernels on such a build, and the compiler
seeds of a kernel with a host sum carry "mapped" and "narrow". Whichever of
them wins by noise then costs every call its proofs.

The helpers now ask for the build first. The answer is cached, so a
declined call costs a function call. Nothing else changes: an admitted call
runs the same proofs and the same kernel, a declined one the original
statements, and the generated code is the same.

B200, torch 2.15.0+cu132 (not the audited build),
examples/layer_norm.py::layer_norm_bwd, 4096 x 4096 fp32, one pinned
layout; host time of the kernel call at the parent commit and at this one,
median of 300 calls, in us:

  cute_host_paired_sum    off    mapped   narrow
  parent                  54.5    73.7     73.4
  this commit             54.0    54.1     54.3

A declining call of the helper alone, 100000 calls: paired 12.3 us before
and 0.11 us after, single 7.0 us and 0.11 us. Cold autotunes of this kernel
picked "mapped" at 4096 features and "narrow" at 6144.

One more order is fixed in `try_full_sum`. It computed a divisor's
reciprocal, with tensors, before it looked at the dispatch state, so a mode
of the caller saw those operations. Under `torch.device("cuda")` every call
of a scaled full sum built two device scalars, divided them and
synchronized before it declined, which is device work that these calls no
longer do; under `torch.device("meta")` the call raised instead of
declining. The reciprocal is now computed after that check.

Compatibility: CuTe only, no change to generated code, configs or results.
On the audited build the first call of a helper reads the four headers even
when its other proofs would have declined; that happens once in a process,
as before.

Two test modules of the helpers had fallen behind the launcher, unseen:
test/test_cute_host_paired_sum_abi.py and
test/test_cute_host_single_sum_abi.py ran on the audited build only (their
skip compared the build's git version), which no CI job installs.
- Their stand-in launches reached the launcher's CUDA context check
  (#4456), which the modules forbid as they do every use of the device,
  and they asserted the compile options of before the division option.
  They skip the check for the stand-in launch and assert the launcher's
  current options.
- They run on every build now. What they check (the launches the bound
  host makes, with their arguments, streams and launchers) does not depend
  on the installed ATen, so they override the helpers' answer about the
  build (`_compatible_aten_sum`, and `_compatible_aten_half_cast` for the
  single sum's FP16 outputs) next to the tensor and dispatch checks they
  already override, and the skip is gone. The skip was also narrower than
  the gate it stood for: a build of that git version with other headers,
  or for ROCm, would have run the tests and failed them.
- They are `cpu_only` modules now, which the CPU lane runs without a
  device. One query on their path needed one: the launcher asks the CUDA
  runtime whether its stream is being captured, which raises in a process
  that sees no device. The tests answer it next to the context check (the
  stand-in launches are eager ones). `test/shard_durations.json` loses the
  modules' two zero placeholders: a module outside the GPU lane has no
  entry there.
- They no longer depend on the launches before them, nor change the ones
  after. The launcher keeps the SM count of each device for the process
  (`_CUTE_NUM_SM_CACHE`): an earlier launch on cuda:0 answered in place of
  the stand-in count of 148 that the tests assert, and the stand-in count
  stayed behind for later launches. A B200 has 148 SMs, so neither showed
  here. The tests run on an empty copy of the cache.
On the audited build their 6 tests fail at origin/main and pass here; on
every other build they were skipped and now run.

Tests:
- Three new tests, one for each helper: with the build declined the helper
  returns None and enters none of its other proofs (they are patched to
  raise, the query for a function mode included, and the arguments are no
  tensors).
- A new test calls `try_full_sum` with a divisor under a function mode, a
  dispatch mode and a device mode: it declines without an operation. It
  fails on the old order.
- The tests of the other reasons to decline (CPU tensors, dispatch modes,
  subclasses and duals, unsupported 1-D tensors) admit the build, so that
  an unaudited build is not what declines in them.
- Two tests made the build check raise, to show that a singleton plan
  declines before it. They admit the build now and keep the launcher
  raising, and the single sum its half-cast header check.

Testing: B200, CUDA 13.2, on origin/main 57009cb. The six test modules
of the helpers on both lanes at this commit, with GPU 6
(test_cute_host_full_sum.py, test_cute_paired_sum_runtime.py,
test_cute_single_sum_runtime.py, test_cute_host_paired_sum_abi.py,
test_cute_host_single_sum_abi.py, test_cute_sum_tree_admission.py). On the
audited build (torch 2.16.0.dev20261026+cu130, in a scratch environment):
GPU lane 49 passed, CPU lane 183 passed, none skipped; at origin/main
(f615a9b) there, GPU lane 47 passed and 6 failed (the two ABI modules),
CPU lane 175 passed. On torch 2.15.0+cu132, which the lanes of this stack
run on: GPU lane 9 passed, 40 skipped (the tests that need the audited
build); CPU lane 181 passed, 2 skipped, the 6 ABI tests among the passed
ones, where they were skipped. The lanes at the tip of the stack, which
holds this commit, on torch 2.15.0, run while the two ABI modules were
still in the GPU lane: `scripts/run_tests.py --backend cute --gpus 2,3`,
GPU lane 16024 passed (the 6 ABI tests among them), 2370 skipped; CPU lane
27318 passed, 684 skipped, 1 failed
(`test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition`,
which needs the nightly's decomposition: it fails on torch 2.15.0 at
origin/main as well and passes on the nightly). `./lint.sh check` passes.
The stack was since rebased onto origin/main 74ac871 (after #4766
and #4767 landed, without conflicts), and the lanes ran again at the tip of
the stack, which holds this commit: `scripts/run_tests.py --backend cute
--gpus 4,5,6,7`, GPU lane 16010 passed, 2374 skipped; CPU lane 27408 passed,
684 skipped, 1 failed
(test/test_cute_fragment_assertion.py::test_assertions_in_binary_cross_entropy_decomposition,
which fails on torch 2.15.0 at origin/main as well); `scripts/run_tests.py
--backend triton --gpus 5`, GPU lane 3408 passed, 2409 skipped, 2 failed
(test/test_gluon_ring.py::TestGluonRing::test_two_dots_in_one_loop and
::test_task_accumulators_get_their_own_rings, the same at origin/main); CPU
lane 5168 passed, 627 skipped. Two test modules of the stack's bottom commit
changed in its last review after these runs (test_cute_smem_footprint.py,
test_cute_resident_reductions_codegen.py); they ran again at the tip, 63
passed.

Review notes:
- Not valid here: "a declined full sum can clobber its own input"
  (helion/_compiler/cute/host_full_sum.py, which this commit does not
  touch). The defect is real and on origin/main; the next commit of the
  stack fixes it: "[cutedsl] Fix the full host sum's fallback and its
  divisor's reciprocal".
- Not valid here: "five source-generation tests of
  test/test_cute_host_full_sum.py are gated on the ATen build and sit
  outside a cpu_only module". It predates this commit, which leaves those
  tests alone; the same next commit makes the module `cpu_only` and drops
  the gate from them.

autoreview passed: claude, codex

stack-info: PR: #4815, branch: choijon5/cute-norm-bwd/5
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/5 branch from 32686e6 to 0d16ced Compare November 29, 2026 20:09
@choijon5
choijon5 changed the base branch from choijon5/cute-norm-bwd/3 to main November 29, 2026 20:10
@choijon5
choijon5 merged commit 616f49a 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