Repository navigation
[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
Closed
student-Jungmin wants to merge 1 commit into
student-Jungmin wants to merge 1 commit into
Conversation
…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).
Collaborator
Author
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.
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.
aten.sort.stableandaten.sorton the npu go to one Triton kernel (torchsim::sort,pytorchsim/frontend/sort.py) instead of an extern aten call that the simulator never times.tl.gatherreads, as v6e sorts (vset.pattern+vpermon the cross-lane unit).[L, D]tile, the row padded to a power of twon, onL = min(lanes, n)lanes.Measured locally
Setup:
TORCHSIM_COMPILE_DIR, torch 2.14,tpuv6e, timing mode.scripts/ci/bundle.pydefines it (its first output, the rows in expert order).tpu-perf-archiveqwen3moe_layer2026-10-20, per-op times from its v6e traces.Every row runs as the sort kernel and the row gather, with no CPU fallback.
Open issues:
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.