Skip to content

A row times a matrix runs as vector work, not on the array - #342

Merged
student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
vector-mm-m1
Oct 12, 2026
Merged

student-Jungmin merged 6 commits into
pytorchsim-triton-backendfrom
vector-mm-m1

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Stacked on #341 (its four commits show here until it merges; the change of this PR is the last commit).

What

An mm (or a plain addmm) whose left operand is 1 x K went to the mm template and ran on the systolic array. The TPU runs that shape on its vector unit (vpu_reduce in the torch_xla table).

rewrite_row_mm (post-grad, npu graphs only) replaces it with the row viewed as K x 1, multiplied by the matrix and summed over K. Inductor then emits one elementwise-and-reduce kernel, so the layout is chosen for that kernel from the start and no tl.dot is generated.

Measured

tpuv6e, no cache, fp16; cycles and sim/TPU against torch_xla:

1 x K x N before after sim/TPU before after
384 x 1152 13517 10628 1.33 1.05
768 x 768 10223 9002 0.93 0.82
768 x 3072 39095 32575 1.56 1.30
3072 x 768 40244 31259 1.85 1.44
2048 x 2048 50514 21599 1.80 0.77

512 x 768 x 768 is unchanged (12534).

Tests

  • tests/ops/gemm/test_row_matmul.py (new, in the allowlist and the workflow): mm at two shapes and addmm, functional check on, all pass. Each graph is one npu kernel, no host call, no tl.dot.
  • tests/ops/gemm/test_matmul.py: 11 of 11 still pass.

Limits

  • Only aten.mm / aten.addmm with a static first dimension of 1. A K x 1 right operand, bmm, and a hand-written Triton kernel with a one-row tl.dot still take the array.
  • The sum over K is a different order of additions than the array's; the test's 1e-4 tolerance holds in fp32.
  • The allowlist was not rerun in full.

…ngs its own unit table

systolic_array_split now defaults to round_robin; none turns it off. A
--trace_so run without --unit_table is refused unless the config says none.

Under --models_list a kernel's trace_units.tsv, beside its trace.so, joins
one table the simulator keeps: UnitTable::append shifts its tile_ids behind
the rows already held and the kernel's computes carry the same shift. A
unit the first table does not name is an error. With the split on, a
launched kernel without the file is refused.

core_serial: the serial cases set none; the split cases run on the default;
a default run without a table is refused.
emit_trace writes each sampled tile's unit rows (the vcix units, the CPU's
own, and Issue) from gem5's stats.txt beside trace_cycles.tsv. A standalone
run passes it as --unit_table; a launched kernel is found by TOGSim beside
its trace.so. The autotune launch links it like the cycle table, and a
workdir that has cycles but no unit rows is emitted again.
get_result_from_file looked for 'Systolic Array Utilization(%)', 'MatMul
active cycle', 'DRAM CH[n]' and 'DRAM: AVG BW Util', none of which TOGSim
prints any more, so its core and DRAM results were always empty. It now
reads the per-unit rows ('Core [i] : <unit> utilization(%): ...', and the
Total rows of a several-core run), the per-array rows of a run without the
unit table, and the '[DRAM] channel n | ...' lines. core_metrics['units']
holds every row; the old flat keys are filled from the machine's array unit
and its vector unit. A log that never finished now returns five values like
any other, so the caller that takes the last one still gets the cycles.

tests/system/test_togsim_result.py pins both forms on logs cut from real
runs.
…pute at its full length

util_viewer matched only the per-array and Vector unit lines, which a run
with the unit table does not print, and that is now every frontend run: its
systolic and vector series came out empty. It reads the Systolic (or Msa)
and Vpu rows as well, in the periodic blocks and the final one. Checked on
a 2048x2048x2048 log of each form: 50.66 / 26.39 with the table, 36.33 per
array without it.

trace_timeline drew a compute as compute_cycle - overlapping_cycle. TOGSim
runs it for compute_cycle since the one-compute-at-a-time change, so a
nonzero overlap column drew the slice shorter than it ran.

The test joins scripts/ci/triton_route_passing.txt, and the comments this
branch added to the Python sources come out, per the repository rule.
… not an mm

An mm (or a plain addmm) whose left operand is 1 x K went to the mm
template and ran on the systolic array, one row at a time. The TPU runs
that shape on its vector unit. rewrite_row_mm replaces it in the post-grad
graph with the row viewed as K x 1, multiplied by the matrix and summed
over K, so Inductor emits one elementwise-and-reduce kernel and the layout
is chosen for that kernel from the start; no tl.dot is generated.

tpuv6e, no cache, fp16, cycles and sim/TPU against torch_xla:
  1x384x1152   13517 -> 10628   1.33 -> 1.05
  1x768x768    10223 ->  9002   0.93 -> 0.82
  1x768x3072   39095 -> 32575   1.56 -> 1.30
  1x3072x768   40244 -> 31259   1.85 -> 1.44
  1x2048x2048  50514 -> 21599   1.80 -> 0.77
512x768x768 is unchanged at 12534.

tests/ops/gemm/test_row_matmul.py (mm at two shapes, addmm) passes with the
functional check on; each graph is one npu kernel with no host call.
tests/ops/gemm/test_matmul.py still passes, 11 of 11.
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