Repository navigation
[Frontend] topk on the npu is one Triton kernel that stores its values and indices once each - #439
Merged
Merged
Conversation
…s and indices once each rewrite_topk replaced aten.topk with K rounds of amax, first index and mask, then cat. Inductor fused the rounds into one kernel but made the cat of 2K reductions into 2K column views of the outputs: a kernel with 16 output pointers for k = 8, and 16 DMAs a program on TOGSim. With the TOG of the T 8192 router kernel cut to 2 contiguous stores and nothing else changed, TOGSim went 94331 -> 81815 cycles; 16 contiguous stores stayed at 93388, so the cost is the count of DMAs. Every other kernel of the compiler's validation bundle has one output. frontend/topk.py registers aten::topk's npu kernel at AutogradPrivateUse1 and PrivateUse1 (the route attention takes, torch.library only) and calls torchsim::topk, a triton_op. Its kernel takes a row on each lane, runs the same rounds and gathers the K values and indices of its rows in registers, then stores each once. topk_plan is the one gate: the last dim of a floating input whose row fits the scratchpad budget; a program takes vector_lanes rows (those past the end masked), so a call of fewer rows keeps the same layout instead of spreading the row over the lanes. A call the plan declines, and an eager call, run the rounds as aten ops; a declined one is logged with its reason. The backward scatters the values' gradient to the indices, aten's topk backward. rewrite_topk is removed: no aten.topk on the npu reaches the post-grad graph. tests/ops/sort/test_topk.py pins the routes and the values against a stable CPU sort: rows a whole number of programs and not, a row length that is not a power of two, smallest, bf16, ties to the first index, a dim that is not the last (rounds), the gradient, eager and inference_mode, and one launch counted per call. It joins the allowlist. Timed with PyTorchSim-Triton-Backend's validation/vector harness on its main 7fb7195 (TOGSim 601fa78, configs/tpuv6e.yml), the bundle built by scripts/ci/bundle.py with the topk suite (PyTorchSim #395) and the measured tables of PyTorchSim-Triton-Backend #208: the Qwen3-30B-A3B router topk, f32 [T, 128], k 8, T 1..8192, TOGSim over tpu-perf-archive topk_block pallas_t_lanes. Geomean 2.160 -> 0.945, within 10% 0 -> 9 of 10. T 1 is 0.73: its v6e row includes a 0.99 us layout copy XLA adds around the (1, 128) block. T >= 256 is 0.92-0.98, on the fast side.
This was referenced Oct 23, 2026
Merged
YWHyuk
marked this pull request as ready for review
October 23, 2026 13:24
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.
Split out of #433 by @student-Jungmin (commit 1c43abc), author kept. In #433 it sat after the snake-case rename (ad69116); here it is in the pre-refactor layout, so
rewrite_topkand itsinstall_onceline are removed fromrewrite_fx_graph.pyunder their old names. Otherwise the patch is the original.What
rewrite_topkreplacedaten.topkwith K rounds of amax, first index and mask, then cat. Inductor fused the rounds into one kernel but made the cat of 2K reductions into 2K column views of the outputs: 16 output pointers for k = 8, and 16 DMAs a program on TOGSim. With the TOG of the T 8192 router kernel cut to 2 contiguous stores and nothing else changed, TOGSim went 94331 → 81815 cycles; 16 contiguous stores stayed at 93388, so the cost is the count of DMAs.frontend/topk.pyregistersaten::topk's npu kernel at AutogradPrivateUse1 and PrivateUse1 and callstorchsim::topk, a triton_op: a row on each lane, the same rounds, the K values and indices gathered in registers and stored once each.topk_planis the one gate (last dim of a floating input whose row fits the scratchpad budget); a declined call and an eager call run the rounds as aten ops, a declined one logged with its reason. The backward scatters the values' gradient to the indices.rewrite_topkis removed.Measured by the author (PTB validation/vector harness, TOGSim on
configs/tpuv6e.yml, Qwen3-30B-A3B router topk f32 [T, 128], k 8, T 1..8192, vs tpu-perf-archive): geomean 2.160 → 0.945, within 10% 0 → 9 of 10.Tests
tests/ops/sort/test_topk.pypins routes and values against a stable CPU sort, and joins the allowlist. Run on this branch (functional mode, timing off, 52 s): all 12 cases PASS (router, ragged_rows, row_not_pow2, smallest, bf16, ties, dim0, grad, eager, inference_mode, inference_mode_compiled, counts).Split of #433
#433 (@student-Jungmin, branch
refactor/post-grad-pass) is split into four PRs. #433 itself is left as it is.split/433-complex-scalarpytorchsim-triton-backendsplit/433-npu-topkpytorchsim-triton-backendsplit/433-grouped-mm-fallbackgrouped_mm_fallbacktakesaten._grouped_mm's npu kernel (6886059)pytorchsim-triton-backendsplit/433-post-grad-passpytorchsim-triton-backendThe three feature PRs each apply to the current base on their own, in the pre-refactor file layout (
rewrite_fx_graph.py). #439 and #440 both remove a rewrite fromrewrite_fx_graph.pynext to each other, so whichever merges second needs a trivial rebase; #441 already holds that resolution. Applying #438 → #439 → #440 → #441 gives exactly the tree of #433's head 9ff24d3 rebased onto the current base (git diffempty).🤖 Generated with Claude Code
https://claude.ai/code/session_011cGNfHFySFsi7feZKKvq7e