Skip to content

[Refactor 9/9] aten.sort on the npu as Sort, a CustomFusedKernel - #460

Merged
student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
refactor/r441-8-sort
Oct 24, 2026
Merged

student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
refactor/r441-8-sort

Conversation

@student-Jungmin

@student-Jungmin student-Jungmin commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Draft: waiting for verification (CI and the validation reports) before review.

Part 9 of 9 of the frontend refactor. It is one commit of this part's final state. Stacked on #463 (refactor/r441-7d-spad-budget); review only this PR's commit.

What

#447's sort, moved into the dispatcher's one form: pytorchsim/frontend/dispatcher/sort.py, Sort(CustomFusedKernel). It takes aten.sort.stable and aten.sort, as torchsim::sort.

  • Kernel (unchanged from [Frontend] aten.sort on the npu as one Triton kernel (DO NOT MERGE: waits on PTB #335) #447). A bitonic network per row whose partners tl.gather reads, as v6e sorts. A program sorts one row of the last axis as an [L, D] tile, padded to a power of two n, on L = min(lanes, n) lanes. The key is one int64 (a float's bits made ordered, NaN last, -0 as +0, flipped when descending), carried as two int32 words with the int32 index, so every sort is stable. Keys: int32, int64, float32.
  • plan declines another dtype, another axis, an empty or 0-d input, and a row past the scratchpad budget. A gradient declines, as in every CustomFusedKernel. The fallback runs the same network in aten ops.

Pin

The compiler pin moves 70221f4 -> 2abb4a3, PSAL-POSTECH/PyTorchSim-Triton-Backend main with #335 (the tl.gather lowering this kernel needs). TOGSim stays 5791272, vcix d2d9b27.

Checked locally (PTB 2abb4a3, this branch's pin)

  • test_sort 24/24 in functional mode and in timing mode; the sort kernel compiles and runs, and only the non-last-axis cases decline, as intended.
  • test_topk, test_add 5/5, test_matmul 11/11, test_grouped_mm 6/6, test_custom_fused_kernel, test_eager, test_patch_scope 8/8, test_togsim_result, test_cache_identity and test_select (timing mode) pass.
  • Known gap: test_conv_decomposition 6/7. depthwise_grad stops at lower ("index tile ... no one entry per position of its gather") until fit-transfers folds a gather's tile-shaped index buffer with its data tile PyTorchSim-Triton-Backend#351; the pin moves again once it merges.

#447's measurements on PTB #335 (sort sim/v6e 1.13-1.15 at T 128-512; 1.97 at T 1024, register pressure) are in #447, which this replaces.

@student-Jungmin student-Jungmin changed the title [Refactor 6/6] aten.sort on the npu as Sort, a CustomFusedKernel (DO NOT MERGE: waits on PTB #335) [Refactor 7/7] aten.sort on the npu as Sort, a CustomFusedKernel (DO NOT MERGE: waits on PTB #335) Oct 24, 2026
@student-Jungmin
student-Jungmin changed the base branch from refactor/r441-7-simulator to refactor/r441-7b-togsim-result October 24, 2026 12:16
@student-Jungmin student-Jungmin changed the title [Refactor 7/7] aten.sort on the npu as Sort, a CustomFusedKernel (DO NOT MERGE: waits on PTB #335) [Refactor 8/8] aten.sort on the npu as Sort, a CustomFusedKernel (DO NOT MERGE: waits on PTB #335) Oct 24, 2026
@student-Jungmin
student-Jungmin changed the base branch from refactor/r441-7b-togsim-result to refactor/r441-7c-cleanup October 24, 2026 12:52
…onic network per row through tl.gather

#447's sort (aten.sort.stable and aten.sort, torchsim::sort) in the dispatcher's one form. A program
sorts one row of the last axis as an [L, D] tile, padded to a power of two n, on L = min(lanes, n)
lanes; the key is one int64 (a float's bits made ordered, NaN last, -0 as +0, flipped when
descending) carried as two int32 words with the int32 index, so every sort is stable. Keys int32,
int64 and float32. plan declines another dtype, another axis, an empty or 0-d input and a row past
the scratchpad budget; a gradient declines as every CustomFusedKernel's does; the fallback runs the
same network in aten ops.

The compiler pin moves 70221f4 -> 2abb4a3, PSAL-POSTECH/PyTorchSim-Triton-Backend main with #335
(tt.gather lowered), which this kernel needs; TOGSim stays 5791272. On that pin test_sort passes
24/24 in functional and in timing mode, and test_topk, test_add, test_matmul, test_grouped_mm,
test_custom_fused_kernel, test_eager, test_patch_scope, test_togsim_result and test_cache_identity
pass. test_conv_decomposition's depthwise_grad still fails at lower ("index tile ... no one entry
per position of its gather") until PSAL-POSTECH/PyTorchSim-Triton-Backend#351. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
@student-Jungmin student-Jungmin changed the title [Refactor 8/8] aten.sort on the npu as Sort, a CustomFusedKernel (DO NOT MERGE: waits on PTB #335) [Refactor 9/9] aten.sort on the npu as Sort, a CustomFusedKernel Oct 24, 2026
@student-Jungmin
student-Jungmin changed the base branch from refactor/r441-7c-cleanup to refactor/r441-7d-spad-budget October 24, 2026 13:19
@student-Jungmin
student-Jungmin marked this pull request as ready for review October 24, 2026 14:01
@student-Jungmin
student-Jungmin changed the base branch from refactor/r441-7d-spad-budget to pytorchsim-triton-backend October 24, 2026 14:01
@student-Jungmin
student-Jungmin merged commit 3ebc1a0 into pytorchsim-triton-backend Oct 24, 2026
@YWHyuk
YWHyuk deleted the refactor/r441-8-sort branch October 27, 2026 07:46
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