Skip to content

[Frontend] transformers' grouped_mm_fallback takes aten._grouped_mm's npu kernel; the dense rewrite goes - #458

Closed
student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
rebase/433-grouped-mm-fallback
Closed

student-Jungmin wants to merge 1 commit into
pytorchsim-triton-backendfrom
rebase/433-grouped-mm-fallback

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Supersedes #440, rebased onto pytorchsim-triton-backend dd80d8c (#443, kernel object format 8). Same commit and author; only the base moved.

The one conflict: rewrite_fx_graph.py. The commit was made while topk still lived there, and #439 has since moved it out. The resolution keeps upstream's file and removes only what this commit removes: the dense grouped_mm_fallback rewrite and its install line. prims goes too, now that nothing uses it.

Run on this branch at PTB 522042a (the pin): test_add 5/5, test_matmul 11/11, test_grouped_mm 6/6, test_topk, test_eager, test_patch_scope 8/8, test_cache_identity pass.


Original description (#440):

Split out of #433 by @student-Jungmin (commit 6886059), author kept. In #433 it sat after the snake-case rename and the topk commit; here it applies to the current base alone, in the pre-refactor layout: rewrite_grouped_mm (its old name) and its helpers are removed,. The grouped_mm.py docstring keeps the base's wording from #418 ("reads as runtime attributes").

What

transformers 5.x calls torch.ops.transformers.grouped_mm_fallback instead of aten._grouped_mm under torch.compile when the weight is not bf16. rewrite_fx_graph rewrote it into a dense form: every expert's mm over every row, masked and summed, E times the work. grouped_mm.install now registers the same entry (_aten_grouped_mm: same triton_op, kernel, gate and math route) at AutogradPrivateUse1 and PrivateUse1 of transformers::grouped_mm_fallback too. It outranks transformers' Autograd and CompositeExplicitAutograd kernels on the npu only; the CPU keeps transformers' kernel.

Tests

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. Run on this branch (functional mode, timing off, 128 s): routed, few_rows, eager, grad, fallback PASS.

Split of #433

#433 (@student-Jungmin, branch refactor/post-grad-pass) is split into four PRs. #433 itself is left as it is.

PR branch content base
#438 split/433-complex-scalar complex scalar × complex value (31747c8) pytorchsim-triton-backend
#439 split/433-npu-topk topk on the npu is one Triton kernel (1c43abc) pytorchsim-triton-backend
#440 split/433-grouped-mm-fallback transformers' grouped_mm_fallback takes aten._grouped_mm's npu kernel (6886059) pytorchsim-triton-backend
#441 split/433-post-grad-pass the 9 refactor commits squashed into 3, on top of #438 + #439 + #440 pytorchsim-triton-backend

The 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 from rewrite_fx_graph.py next 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 diff empty).

🤖 Generated with Claude Code

https://claude.ai/code/session_011cGNfHFySFsi7feZKKvq7e

… 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_grouped_mm,
_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.
@student-Jungmin

Copy link
Copy Markdown
Collaborator Author

Folded into the refactor's part 1, #449, which now starts from pytorchsim-triton-backend and carries these commits' changes; closing.

@YWHyuk
YWHyuk deleted the rebase/433-grouped-mm-fallback 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