Repository navigation
feat(cutile): add tile-level rank-by-key and stable sort-by-key kernels (#1658) - #1814
Open
robertream wants to merge 3 commits into
Open
robertream wants to merge 3 commits into
robertream wants to merge 3 commits into
Conversation
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
force-pushed
the
feat/cutile-rank-sort
branch
from
November 17, 2026 18:03
e4f3ee7 to
183f7b0
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ofu32keys in[0, K),NandKpowers 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 downNfor 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 largerK: the one-hot working set fails Tile IR assembly atN * K = 32768i32 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 ani32payload.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 intorun_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.anyand 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/scanemitted a hardcodeddim=0andidentities=[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.