Skip to content

[cutedsl] Read the reduction shared memory budget of the kernel's device - #4812

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

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

Conversation

@choijon5

@choijon5 choijon5 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[cutedsl] Read the reduction shared memory budget of the kernel's device

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

No kernel's performance changes on a host whose CUDA devices are one part,
which is where it was measured. On a host of different parts, a kernel that
is bound or rendered while another part is the current device is now priced
for its own device, which changes the stages its plan admits; no such host
was at hand, so that was not measured.

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

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

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

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

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

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

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

Review notes:

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

autoreview passed: claude, codex

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

No kernel's performance changes on a host whose CUDA devices are one part,
which is where it was measured.  On a host of different parts, a kernel that
is bound or rendered while another part is the current device is now priced
for its own device, which changes the stages its plan admits; no such host
was at hand, so that was not measured.

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

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

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

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

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

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

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

autoreview passed: claude, codex

stack-info: PR: #4812, branch: choijon5/cute-norm-bwd/2
@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/1 branch from 6fe6283 to 9aadf84 Compare November 28, 2026 17:16
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/2 branch from 1b9abae to 0dd6819 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/1 to main November 28, 2026 20:36
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/2 branch from 0dd6819 to 6fdab53 Compare November 28, 2026 20:37
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/1 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/1 to main November 29, 2026 18:14
@choijon5
choijon5 force-pushed the choijon5/cute-norm-bwd/2 branch from 6fdab53 to 604ae30 Compare November 29, 2026 18:15
@choijon5
choijon5 changed the base branch from main to choijon5/cute-norm-bwd/1 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 changed the base branch from choijon5/cute-norm-bwd/1 to main November 29, 2026 20:07
@choijon5
choijon5 merged commit f3c99af 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