Skip to content

[Frontend] aten._grouped_mm on the npu is one Triton kernel whose blocks read their expert from launch data - #412

Merged
YWHyuk merged 1 commit into
pytorchsim-triton-backendfrom
frontend/grouped-mm
Oct 23, 2026
Merged

YWHyuk merged 1 commit into
pytorchsim-triton-backendfrom
frontend/grouped-mm

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

torch.nn.functional.grouped_mm (Qwen3-30B-A3B's MoE experts) reaches the npu as aten._grouped_mm and runs on the host: the simulator prices it at zero and the layer suite marks every layer row incomplete.

Change

  • pytorchsim/frontend/grouped_mm.py: the op's npu kernel is registered through torch.library at AutogradPrivateUse1 and PrivateUse1, as attention's is, and calls torchsim::grouped_mm, a triton_op in vLLM's fused_moe form.
    • A program is a block of at most BM rows of one group; it reads its group, first row and row count from a table and multiplies by that group's B. A block without rows runs its K loop zero times.
    • The grid is the most blocks any offs needs ((s + g·(BM−1)) // BM), and no more than the row count s.
    • BM is the lanes; BN and BK the largest tile of the machine's mm rule (tiling.gemm_tile_candidates).
    • A call the kernel does not take (bias, out_dtype, other ranks, a gradient) and every eager call run group by group with matmul; the first are logged with why.
  • pytorchsim/simulator/timing.py: each argument the TOG reads as data (tile_types.json's data_args) is appended to the launch's shape_args.

Depends on PSAL-POSTECH/PyTorchSim-Triton-Backend#282 (the TOG takes the table as data) and a compiler pin past it; draft until then. Without it the compile stops at tog.

Tests: tests/ops/gemm/test_grouped_mm.py (in the allowlist and the workflow matrix): routed (uneven groups, empty ones, one past BM: one launch, nothing left to aten), few rows, eager, gradients. All four pass locally on this branch with PSAL-POSTECH/PyTorchSim-Triton-Backend#282's compiler (functional mode on).

Numbers: local only until the pin moves; see PSAL-POSTECH/PyTorchSim-Triton-Backend#282.

…cks read their expert from launch data

torch.nn.functional.grouped_mm (Qwen3-30B-A3B's MoE experts) reached the npu
as aten._grouped_mm and ran on the host: the simulator priced it at zero.
Its npu kernel is now registered through torch.library at AutogradPrivateUse1
and PrivateUse1, as attention's is, and calls torchsim::grouped_mm, a
triton_op in vLLM's fused_moe form: a program is a block of at most BM rows of
one group, its group, first row and row count read from a table; a block
without rows runs its K loop zero times. The grid is the most blocks any offs
needs, and no more than the row count. BM is the lanes, BN and BK the largest
tile of the machine's mm rule. A call the kernel does not take (bias,
out_dtype, other ranks, a gradient) and every eager call run group by group
with matmul; the first are logged with why.

timing.py appends each argument the TOG reads as data (tile_types'
data_args) to the launch's shape_args.

Needs the compiler's TOG data args (PTB, local: eec250c, d9f390c, a0a47b3).
tests/ops/gemm/test_grouped_mm.py pins the route (one launch, nothing left to
aten), few rows, eager and gradients.
@YWHyuk
YWHyuk marked this pull request as ready for review October 23, 2026 03:00
@YWHyuk
YWHyuk merged commit a5d1fbd into pytorchsim-triton-backend Oct 23, 2026
@YWHyuk
YWHyuk deleted the frontend/grouped-mm branch October 23, 2026 03:05
YWHyuk added a commit that referenced this pull request Oct 23, 2026
…me attributes replace them

a5d1fbd (#412) appended the elements of every argument tile_types.json lists as data_args to the
launch's shape_args (timing.data_values), so the TOG could read the grouped GEMM's group, start and
row tables from the launch. That is the compiler side of PTB #282 (torchsim.data), which was
rejected and is closed; no compiler the pin can reach writes data_args. PTB #315's runtime
attributes replace it: the functional run records each value the TOG reads from data under
runtime/runtime_attribute/, and TOGSim reads those records (the commit before this one), so the
values never travel through shape_args.

data_values and _write_extents' data argument are removed and shape_args is the grid again, as
before #412. The grouped_mm frontend, its registration and tests/ops/gemm/test_grouped_mm.py stay;
the frontend's docstring now says the TOG reads the table as runtime attributes.
YWHyuk added a commit that referenced this pull request Oct 23, 2026
…me attributes replace them

a5d1fbd (#412) appended the elements of every argument tile_types.json lists as data_args to the
launch's shape_args (timing.data_values), so the TOG could read the grouped GEMM's group, start and
row tables from the launch. That is the compiler side of PTB #282 (torchsim.data), which was
rejected and is closed; no compiler the pin can reach writes data_args. PTB #315's runtime
attributes replace it: the functional run records each value the TOG reads from data under
runtime/runtime_attribute/, and TOGSim reads those records (the commit before this one), so the
values never travel through shape_args.

data_values and _write_extents' data argument are removed and shape_args is the grid again, as
before #412. The grouped_mm frontend, its registration and tests/ops/gemm/test_grouped_mm.py stay;
the frontend's docstring now says the TOG reads the table as runtime attributes.
YWHyuk added a commit that referenced this pull request Oct 23, 2026
…me attributes replace them

a5d1fbd (#412) appended the elements of every argument tile_types.json lists as data_args to the
launch's shape_args (timing.data_values), so the TOG could read the grouped GEMM's group, start and
row tables from the launch. That is the compiler side of PTB #282 (torchsim.data), which was
rejected and is closed; no compiler the pin can reach writes data_args. PTB #315's runtime
attributes replace it: the functional run records each value the TOG reads from data under
runtime/runtime_attribute/, and TOGSim reads those records (the commit before this one), so the
values never travel through shape_args.

data_values and _write_extents' data argument are removed and shape_args is the grid again, as
before #412. The grouped_mm frontend, its registration and tests/ops/gemm/test_grouped_mm.py stay;
the frontend's docstring now says the TOG reads the table as runtime attributes.
YWHyuk pushed a commit that referenced this pull request Oct 23, 2026
… npu kernel; the dense rewrite goes

transformers 5.x calls torch.ops.transformers.grouped_mm_fallback instead of
aten._grouped_mm under torch.compile when the weight is not bf16
(integrations/moe.py _can_use_grouped_mm). rewrite_fx_graph rewrote it into a
dense form: every expert's mm over every row, masked and summed, E times the
work, which TOGSim then priced. grouped_mm.install now registers the same entry
(_aten_grouped_mm, so the same triton_op and kernel, gate and math route) at
AutogradPrivateUse1 and PrivateUse1 of transformers::grouped_mm_fallback too.
The registration precedes transformers' own definition of the op and outranks
its Autograd and CompositeExplicitAutograd kernels on the npu only; the CPU
keeps transformers' kernel. rewrite_transformers_grouped_mm_fallback,
_grouped_mm_target and _emit_dense_grouped_mm are removed.

Needs aten._grouped_mm's npu kernel (PyTorchSim #412, on pytorchsim-triton-backend
as a5d1fbd) and, for timing, the compiler's TOG data args
(PyTorchSim-Triton-Backend #282, not yet in the pinned PTB).

tests/ops/gemm/test_grouped_mm.py: case fallback runs the fallback op at f32
compiled and asks for one grouped-kernel launch and no extern call.
YWHyuk added a commit that referenced this pull request Oct 23, 2026
…me attributes replace them

a5d1fbd (#412) appended the elements of every argument tile_types.json lists as data_args to the
launch's shape_args (timing.data_values), so the TOG could read the grouped GEMM's group, start and
row tables from the launch. That is the compiler side of PTB #282 (torchsim.data), which was
rejected and is closed; no compiler the pin can reach writes data_args. PTB #315's runtime
attributes replace it: the functional run records each value the TOG reads from data under
runtime/runtime_attribute/, and TOGSim reads those records (the commit before this one), so the
values never travel through shape_args.

data_values and _write_extents' data argument are removed and shape_args is the grid again, as
before #412. The grouped_mm frontend, its registration and tests/ops/gemm/test_grouped_mm.py stay;
the frontend's docstring now says the TOG reads the table as runtime attributes.
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