Skip to content

feat(cutile): add tile-level rank-by-key and stable sort-by-key kernels (#1658) - #1814

Open
robertream wants to merge 3 commits into
NVIDIA:mainfrom
robertream:feat/cutile-rank-sort
Open

robertream wants to merge 3 commits into
NVIDIA:mainfrom
robertream:feat/cutile-rank-sort

Conversation

@robertream

Copy link
Copy Markdown

Adds cutile_kernels::sort, the tile-level rank-by-key building block and stable sort from #1658, with GPU-verified correctness oracles and an A/B benchmark of the two candidate formulations.

Kernels (single tile block, [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. A one-hot [N, K] matrix turns the per-key work into dense tile ops: one inclusive prefix sum down N for ranks, reductions for counts.
  • rank_by_key_loop: the same contract with one [N] prefix sum per key value, the formulation the issue's radix sort shipped with. Kept for larger K: the one-hot working set fails Tile IR assembly at N * K = 32768 i32 elements on sm_86, while the loop form carries only [N] tiles.
  • 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.

Testing. The GPU tests are written outside-in with independent host oracles per layer (a Rust stable sort for sort_by_key; an exact rank reference plus permutation/stability/sort-readiness invariants for the probe), so a bug in one layer cannot be masked by the other. All five pass on an A10G (sm_86), wired into run_gpu_tests.sh. Compile-level smoke cases cover all three kernels at the test shapes.

Benchmark. An opt-in paired benchmark (cutile::bench::do_bench_paired, strict A/B alternation) measured the one-hot core 11-15% faster than per-key scans on the A10G: 0.017 vs 0.019 ms at 1024x8, 0.024 vs 0.027 ms at 1024x16, 0.024 vs 0.028 ms at 512x32 (medians). That is why it backs the production entries; #1658 measured the opposite inside a full radix sort, so the loop variant stays available for comparison on other workloads.

Both formulations are scan-bound baselines. The issue's intended endgame is a compiler primitive on match.any and warp ballots, which kernel code cannot express; this PR pins down the API shape and the correctness oracles such a primitive must satisfy. It does not close #1658.

Dependency. The first commit is #1809 (scan_sum/scan emitted a hardcoded dim=0 and identities=[0]; found by these kernels when every within-key rank came back zero on device). The kernels scan along dim 0 and are correct under the pre-fix compiler too, but their tests exercise the fixed semantics. Once #1809 merges this rebases to a single commit.

@robertream
robertream requested a review from elibol as a code owner November 14, 2026 00:18
@copy-pr-bot

copy-pr-bot Bot commented Nov 14, 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.

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]>
…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]>
@robertream
robertream force-pushed the feat/cutile-rank-sort branch from e4f3ee7 to 183f7b0 Compare November 17, 2026 18:03

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.

cuTile: tile-level sort / rank-by-key primitive (and maybe a device radix sort)

1 participant