Skip to content

[Frontend] aten.sort on the npu as one Triton kernel (DO NOT MERGE: waits on PTB #335) - #447

Closed
student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
sort-npu
Closed

student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
sort-npu

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

DO NOT MERGE. A draft kept so this work is not forgotten. It waits on PSAL-POSTECH/PyTorchSim-Triton-Backend#335 (the compiler's tl.gather lowering): with the compiler this branch pins, the kernel does not lower. It may also be redone as a CustomFusedKernel (frontend/dispatcher/) once that layout lands.

aten.sort.stable and aten.sort on the npu go to one Triton kernel (torchsim::sort, pytorchsim/frontend/sort.py) instead of an extern aten call that the simulator never times.

  • Kernel. A bitonic network per row whose partners tl.gather reads, as v6e sorts (vset.pattern + vperm on the cross-lane unit).
    • A program sorts one row of the last axis as an [L, D] tile, the row 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. It is carried as two int32 words with the int32 index, so every sort is stable.
    • Keys: int32, int64, float32.
  • Declined calls run the same network in aten ops, with the reason logged: another dtype, another axis, the scratchpad budget, a gradient.

Measured locally

Setup:

Every row runs as the sort kernel and the row gather, with no CPU fallback.

T sort, sim / v6e row gather, sim / v6e
128 1.13 1.05
256 1.12 1.08
512 1.15 1.10
1024 1.97 1.11

Open issues:

  • T = 1024: the sort is limited by register pressure (a sort of 8192 keys).
  • T ≥ 2048: rows past 8192 keys are refused by the compiler (a lane broadcast above one LMUL-8 group) until it tiles the sort.
  • Whole-op time: the v6e number also holds XLA's int64 split custom-calls (about 2.9 us) and gaps, so the whole-op ratio is lower than these per-op ones.

Checked: argsort of 1024 and 8192 int64 keys, float32 [4, 300] descending, int32 [2, 64] equal to torch. The aten-op network equals torch on CPU, including NaN, -0 and bf16. No test file is added yet.

…rk per row whose partners tl.gather reads

sort.stable and sort take int32, int64 and float32 keys along the last axis. A program sorts one row
as an [L, D] tile, the row padded to a power of two n on L = min(lanes, n) lanes; the key is one
int64 (float bits made ordered with NaN last and -0 as +0, flipped when descending) carried as two
int32 words with the int32 index, so every sort is stable. A call the kernel declines (another
dtype, another axis, the scratchpad budget, a gradient) runs the same network in aten ops, logged.

Measured locally (PTB PR #335 checkout, timing mode): Qwen3-30B-A3B dispatch rows T 1..1024 run as
the sort kernel and the row gather, no CPU fallback. A row past 8192 is refused by the compiler (a
lane broadcast above one LMUL-8 group).
@student-Jungmin

Copy link
Copy Markdown
Collaborator Author

Moved into the frontend refactor as part 6/6, #460: the same kernel as a CustomFusedKernel in pytorchsim/frontend/dispatcher/sort.py. It still waits on PTB #335. Closing this in favour of #460; the branch is left as it is.

student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…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.

It waits on PSAL-POSTECH/PyTorchSim-Triton-Backend#335: with the compiler this branch pins (PTB
70221f4), the kernel stops at transform with tt.gather left unlowered, so test_sort fails here. The
fallback matches torch.sort(stable=True) exactly on 34 cases (float32 with NaN, -0, +-inf; int32;
int64; every dim, both orders); test_topk, test_add, test_eager and test_patch_scope pass. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…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.

It waits on PSAL-POSTECH/PyTorchSim-Triton-Backend#335: with the compiler this branch pins (PTB
70221f4), the kernel stops at transform with tt.gather left unlowered, so test_sort fails here. The
fallback matches torch.sort(stable=True) exactly on 34 cases (float32 with NaN, -0, +-inf; int32;
int64; every dim, both orders); test_topk, test_add, test_eager and test_patch_scope pass. #447's
measurements (sim/v6e 1.13-1.15 at T 128-512 on PTB #335) are in that PR.
student-Jungmin added a commit that referenced this pull request Oct 24, 2026
…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.
@YWHyuk
YWHyuk deleted the sort-npu branch October 27, 2026 07:47
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