Skip to content

[CI] The bundle builds topk for the compiler's measured router rows, at f32 - #395

Closed
student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
ci/bundle-topk
Closed

student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
ci/bundle-topk

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Builds the MoE router's topk for the compiler's vector suite, so that PyTorchSim-Triton-Backend's validation can time it (PSAL-POSTECH/PyTorchSim-Triton-Backend#208).

What changes

  • scripts/ci/bundle.py adds the suite ("vector", "tpu_v6e_topk_torchxla.csv", ...).
    • It runs torch.topk(x, K, dim=-1) with K from the row. The rows are Qwen3-30B-A3B's router, p f32 [T, 128] with T 1..8192 and K 8.
    • T and E are read as the vector suite's R and C, and the workload is named vector_topk_<T>x<E>x<K>_f32.
  • Each suite now carries its input dtype.
    • A row is a workload when its dtype_in is the suite's, and its inputs are built in that dtype.
    • The other suites stay bf16, so their workloads, names and manifests are unchanged.
  • tests/system/test_ci_bundle.py pins the workload list, names, dtypes and the topk call. It is added to the pytorchsim_test.yml matrix. It emits no kernel, so it is not in the allowlist.

Checked locally on b6a18ad:

  • Each topk workload launches one fused kernel.
  • XBLOCK is min(T, 128), and xnumel is 1 at T 1. These are the blocks at which tpu-perf-archive data/v6e/topk_block was measured, and [Test] Add YOLOv5 test file #208's block check confirms them.

Order

This change needs the compiler commit that adds validation/vector/tpu_v6e_topk_torchxla.csv (#208). Against a compiler pin without that file, bundle.py fails to open it. So this PR is a draft until #208 is merged and the compiler pin here includes it.

…at f32

The compiler's vector suite now measures the MoE router's top-8 of 128
(validation/vector/tpu_v6e_topk_torchxla.csv, p f32 [T, 128]). bundle.py
takes it as program topk with R = T, C = E and K from the row, runs
torch.topk(x, K, dim=-1), and names it vector_topk_<T>x<E>x<K>_f32.

Each suite now carries its input dtype: a row is a workload when its
dtype_in is the suite's, and its inputs are built in that dtype. The
other suites stay bf16, so their workloads and names are unchanged.

tests/system/test_ci_bundle.py pins the workload list, names, dtypes
and the topk call.

This commit needs the compiler commit that adds
validation/vector/tpu_v6e_topk_torchxla.csv (PyTorchSim-Triton-Backend,
"validation/vector compares topk with ..."); against a compiler without
that file bundle.py fails to open it.
@YWHyuk

YWHyuk commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Maintainer review: on hold until the points below are resolved.

  1. K should vary, not only K = 8. Every row of the measured table (validation/vector/tpu_v6e_topk_torchxla.csv, and tpu-perf-archive data/v6e/topk/torchxla.csv behind it) is the router shape E = 128, K = 8, so the bundle pins topk at one K. The kernel's rounds, blocks and cost all depend on K, and topk is used beyond the router. The suite needs rows over a range of K (and E), which means measurements for them first: a request to tpu-perf-archive, then the compiler table, then this PR's workload list.
  2. Conflicts. [CI] The bundle builds Qwen3-30B-A3B's GEMMs, from the compiler's table of them #400 and [CI] The bundle carries validation/layer, under the compiler's CI cap, and names each call left to aten #401 (merged) changed SUITES and the inputs maker in scripts/ci/bundle.py (inputs are randn(...), the layer suite is capped by the compiler's under_cap). This needs a rebase onto pytorchsim-triton-backend 55ce67e, and tests/system/test_ci_bundle.py's pinned list must take the 48 Qwen3 GEMM and 96 layer workloads.

The dependency in the description is met: PTB #208 is merged and the compiler pin (32b15ae) carries tpu_v6e_topk_torchxla.csv.

🤖 Generated with Claude Code

https://claude.ai/code/session_011cGNfHFySFsi7feZKKvq7e

@YWHyuk YWHyuk closed this Oct 22, 2026
@YWHyuk
YWHyuk deleted the ci/bundle-topk branch October 23, 2026 03:05
YWHyuk pushed a commit that referenced this pull request Oct 23, 2026
…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.
YWHyuk pushed a commit that referenced this pull request Oct 23, 2026
…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.
student-Jungmin added a commit that referenced this pull request Oct 31, 2026
…de.tiles

The frontend of this branch reads contract.copy_node.tiles, which the pinned compiler did not have, so the
route sweep stopped at 'nothing emitted' on most of its models and tests/system/test_copy_node.py raised an
AttributeError. PSAL-POSTECH/PyTorchSim-Triton-Backend#394 adds it, along with the layout and addressing
work this branch was measured against, the EUP functions and their Coprocessor pin, and the gather and
N-pair fixes of #393 and #395.

The grid this branch reports was run against that compiler.
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.

2 participants