Skip to content

[GEMM Timing Validation][Frontend] An npu mm the compiler's copy_node.plan holds runs the resident mm, as v6e XLA runs it - #457

Merged
student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
gemm-copy-node
Oct 24, 2026
Merged

student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
gemm-copy-node

Conversation

@student-Jungmin

@student-Jungmin student-Jungmin commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

The front-end half of PSAL-POSTECH/PyTorchSim-Triton-Backend#347, which has the motivation. In short: v6e XLA runs a GEMM in two steps. A cross-program prefetch first copies one operand whole into VMEM, with nothing beside it. The fusion then walks the output tiles streaming the other operand, K whole.

Turning the TPU's prefetch off, looking at compute-bound GEMMs only, and forbidding the K split did not close the sim's fast gap. So the npu now builds the kernel the TPU runs.

What it does

  • pytorchsim/frontend/inductor/autotune/mm_resident.py: the resident mm, an Inductor TritonTemplate.
    • One program loads the held operand whole.
    • It then walks the output tiles two at a time into two buffers, streaming the other operand, K whole.
    • Each tile is stored through store_output, so an epilogue fuses per tile, as in the mm template.
    • Its grid is (1, 1, 1) whatever the launch.
  • Its tile is the compiler's: the one the matrix unit walks least for, never narrower than Target.matmul_width.
  • Which operand, and what tile, comes from the compiler's copy_node.plan. The rule lives in one place and is read here.
    • XLA's order: the larger operand that fits the whole scratchpad beside the streamed tiles, else the smaller, none for a vector product.
    • The compiler makes that load the kernel's copy node and reads the operand held in the kernel's TOG.
  • Where the plan holds an operand, the template is offered alone, its one config annotated tile_source "planned".
    • It is not raced against the K-split mm template on TOGSim cycles: the exposed copy is the point, and it would always lose.
    • Every other mm keeps the mm template, with the reason logged: declined by the plan, another dtype or out_dtype, another device.
    • This changes the default mm path for those shapes.
  • timing.simulate_standalone runs a kernel object's copy nodes before the kernel, also in a session, and reports the sum with the parts beside it. template_timing times through it, and NPUChoices (registration.py) marks the config template_record.PLANNED.
  • The kernel object is format 9 (copy_nodes).

It uses the same Inductor extension points the mm heuristic already uses: TritonTemplate, register_template_heuristic, V.choices. There is no public path that keeps epilogue fusion for a GEMM. A triton_op would make the kernel opaque to Inductor.

Tests

tests/system/test_copy_node.py:

  • the offer and its declines, and the config;
  • relu(mm) fused into the one kernel;
  • inference_mode, eager and grad, with values checked in functional mode;
  • the timed copy node and the sum.

Compiler pin

third_party/pytorchsim-triton-backend.json moves 2abb4a3 -> 6f88e2b, PyTorchSim-Triton-Backend main with PSAL-POSTECH/PyTorchSim-Triton-Backend#347 merged (kernel format 9, contract copy_node, the copy node and the held TOG) and #351. TOGSim stays 5791272.

Numbers

Local, stacked with PSAL-POSTECH/PyTorchSim-Triton-Backend#347, the 106 CI gemm shapes against the v6e (prefetch on, minus the lead-in):

  • within ±10%: 16 → 85
  • within ±20%: 49 → 104
  • geomean: 0.816 → 0.946
  • MAPE: 20.0% → 7.8%

The full table is on #347. This PR's CI builds the bundle on the moved pin and measures it on the maintainer baseline.

Default change: every npu mm that copy_node.plan holds (M and N above 1, an operand fitting the scratchpad) now builds the resident mm instead of the timed mm template. In the gemm suite that is 97 of 110 shapes; M = 1 keeps its path. tests/system/test_select.py now times a bmm: an mm the plan holds is planned, and nothing of it is timed.

…ident mm, as v6e XLA runs it

v6e XLA runs a GEMM in two steps: a cross-program prefetch copies one operand whole into VMEM (the larger one
that fits, the smaller when the larger is the whole VMEM, none for a vector product), then the fusion walks the
output tiles streaming the other operand, K whole. The resident mm template
(pytorchsim/frontend/inductor/autotune/mm_resident.py) is that program: one program loads the held operand
whole, then walks the output tiles two at a time into two buffers, each stored through store_output so an
epilogue fuses per tile. Which operand and what tile is the compiler's copy_node.plan (one statement of it, read
here); the compiler emits that load as the kernel's copy node and its TOG reads the operand held.

Where the plan holds an operand the template is offered alone (it would never win a race on TOGSim cycles
against the K-split mm template, the exposed copy being the point), its one config annotated tile_source
"planned"; every other mm (declined by the plan, another dtype or out_dtype, another device) keeps the mm
template, the reason logged. This changes the default mm path for those shapes. NPUChoices (registration.py)
narrows the offer and marks the config template_record.PLANNED; its heuristic is in template_kernel_configs.py.

timing.simulate_standalone runs a kernel object's copy nodes before the kernel (also in a session) and reports
the sum, the parts beside it (kernel_cycles, copy_node_cycles); template_timing times through it. The kernel
object is format 9 (copy_nodes).

The compiler pin moves 2abb4a3 -> 6f88e2b, PSAL-POSTECH/PyTorchSim-Triton-Backend main with #347 (kernel format
9, contract copy_node, the copy node and the held TOG) and #351; TOGSim stays 5791272.

tests/system/test_copy_node.py: the offer and its declines, the config, relu fused into the one kernel,
inference_mode, eager and grad with values (functional mode), the timed copy node and the sum.

tests/system/test_select.py times a bmm: an mm the plan holds is planned, and nothing of it is timed.
@student-Jungmin
student-Jungmin marked this pull request as ready for review October 24, 2026 14:17
@student-Jungmin
student-Jungmin merged commit 338b42d into pytorchsim-triton-backend Oct 24, 2026
2 of 3 checks passed
@YWHyuk
YWHyuk deleted the gemm-copy-node branch October 27, 2026 07:46
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