Skip to content

fix(cutile-compiler): emit the caller's scan dim and identity - #1809

Open
robertream wants to merge 2 commits into
NVIDIA:mainfrom
robertream:fix/scan-dim-identity
Open

robertream wants to merge 2 commits into
NVIDIA:mainfrom
robertream:fix/scan-dim-identity

Conversation

@robertream

Copy link
Copy Markdown

compile_scan_op hardcoded dim=0 and identities=[0], silently ignoring the dim and identity arguments of scan_sum and scan. A scan along any non-leading dimension of a multi-dimensional tile therefore computed the wrong axis, with no compile-time signal since scans preserve shape. Found while building the #1658 rank-by-key kernels: scan_sum(onehot, 1, ...) on a [K, N] tile scanned down K, every within-key rank came back 0, and all equal keys scattered to the same slot. Confirmed on an A10G (sm_86) before the fix and re-verified green after it.

This parses the dim argument the way the reduce intrinsics do (integer literals and const generics, via parse_expr_as_i32), rejects out-of-range dims with a JIT error instead of a panic, and carries the caller's numeric identity literal into the identities attribute with the element's type. Non-literal identities report a clear error.

Written test-first: compile_scan_sum_2d_dim1_test (in cutile/tests/reduce_scan_ops.rs) fails against the old lowering with scan ... dim=0 ... identities=[0] emitted for a dim=1, identity=7 call, and passes with this change. The full reduce_scan_ops suite passes; existing in-tree scans all use dim=0 with identity zero, so their emitted IR is unchanged.

The closure reduce form has the same hardcoded attributes plus a result-type problem and is tracked separately in #1808; the reduce_* intrinsics were never affected.

@robertream
robertream requested a review from elibol as a code owner November 13, 2026 02:58
@copy-pr-bot

copy-pr-bot Bot commented Nov 13, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@robertream

Copy link
Copy Markdown
Author

Added GPU execution tests on top: prefix-sum values along dim 0 and dim 1 of an [8, 16] tile, a dim-1 reverse (suffix-sum) scan, and reduce_sum along both axes, each checked against a host oracle. The whole gpu_execution_ops suite (the 8 existing tests plus these 2) passes on an A10G (sm_86) with this fix applied. The compile-level test pins the emitted attributes; these pin the executed values, which is the only way to catch a wrong-axis scan since scans preserve shape.

compile_scan_op hardcoded dim=0 and identities=[0], silently ignoring the
dim and identity arguments of scan_sum and scan. A scan along any
non-leading dimension of a multi-dimensional tile therefore computed the
wrong axis: scan_sum(onehot, 1, ...) on a [K, N] tile scanned down K and
returned the operand effectively unscanned, with no compile-time signal
since scans preserve shape.

Parse the dim argument like the reduce intrinsics do (integer literals and
const generics via parse_expr_as_i32), reject out-of-range dims with a JIT
error, and carry the caller's numeric identity literal into the identities
attribute with the element's type.

The closure reduce form still reduces along dimension 0 only: honoring its
dim needs a result-shape change to its public signature (it is declared
-> Tile<E, S>), which is a separate change. The reduce_* intrinsics already
honored dim and are unaffected.

Test-first: compile_scan_sum_2d_dim1_test fails against the old lowering
with "scan ... dim=0 ... identities=[0]" and passes with this fix.

Signed-off-by: Robert Ream <[email protected]>
The scan dim fix so far had only a compile-level check that the right IR
is emitted. Add GPU execution tests asserting values: prefix sums along
dim 0 and dim 1 of an [8, 16] tile, a dim-1 reverse (suffix-sum) scan,
and reduce_sum along both axes, each against a host oracle. Scans
preserve shape, so only executed values can catch a wrong-axis scan.

All pass on an A10G (sm_86) together with the existing execution suite.

Signed-off-by: Robert Ream <[email protected]>
@robertream
robertream force-pushed the fix/scan-dim-identity branch from 962ca39 to 831c4a1 Compare November 17, 2026 17:59
robertream added a commit to robertream/cuda-rust that referenced this pull request Nov 17, 2026
…ls (NVIDIA#1658)

Add cutile_kernels::sort with three single-tile-block kernels for [N]
tiles of u32 keys in [0, K), N and K powers of two:

- rank_by_key_probe: each element's rank among earlier elements with the
  same key, plus per-key counts, via a one-hot [N, K] matrix: one
  inclusive prefix sum down N yields within-key ranks, reductions yield
  counts and per-element selections.
- rank_by_key_loop: the same contract computed with one [N] prefix sum
  per key value, the formulation NVIDIA#1658's radix sort shipped with. Kept
  for A/B timing and for K large enough that the one-hot working set
  fails Tile IR assembly (N * K = 32768 i32 elements on sm_86).
- sort_by_key: a stable counting sort composing the one-hot core with an
  exclusive scan of the counts and a pointer-tile scatter, carrying an
  i32 payload.

Each layer is tested against an independent host oracle (tests are
written outside-in: a Rust stable sort for sort_by_key, an exact rank
reference plus structural invariants for the probe), so a bug in one
cannot be masked by the other. All five GPU tests pass on an A10G
(sm_86). An opt-in paired benchmark (cutile::bench::do_bench_paired)
measured the one-hot core 11-15% faster than per-key scans at 1024x8,
1024x16, and 512x32, which is why it backs the production entries.

Both formulations are scan-bound baselines: the issue's intended fix is
a compiler primitive on match.any and warp ballots that kernel code
cannot express. These kernels pin down the API and the correctness
oracles for it.

Builds on the scan dim/identity fix (NVIDIA#1809): the one-hot core scans
along dim 0 so it is correct under the pre-fix compiler too, but the
kernels' tests exercise the fixed semantics.

Signed-off-by: Robert Ream <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant