Skip to content

The mm tile budget counts C as the kernel holds it - #343

Merged
student-Jungmin merged 7 commits into
pytorchsim-triton-backendfrom
mm-tile-acc-budget
Oct 12, 2026
Merged

student-Jungmin merged 7 commits into
pytorchsim-triton-backendfrom
mm-tile-acc-budget

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Stacked on #341 and #342 (their commits show here until they merge; the change of this PR is the last commit).

What

_gemm_tile_candidates charged C at the input width, once. The mm template sums half-precision inputs in an fp32 accumulator and stores C through a second buffer of the output type, so an fp16 kernel holds 6 bytes per element of C where 2 were budgeted.

Tiles the list admitted then failed in the compiler as a scratchpad overflow. They are the largest, so they filled the top-k that autotune times:

  • 8192x2048x2048: three of four failed; the one left (BLOCK_N 512) reads all of A once per N block, 176 MB moved for 72 MB of operands.
  • 8192x768x3072: all four failed.

C is now charged at the accumulator's width, plus the output's when that differs.

Measured

tpuv6e, no cache, fp16; cycles and sim/TPU against torch_xla. "before" is #341 + #342 with PTB #54.

M x K x N before after sim/TPU before after
8192 x 2048 x 2048 608754 387862 1.58 1.01
8192 x 3072 x 768 324971 241750 1.11 0.83
8192 x 768 x 768 99517 92942 1.09 1.02
8192 x 384 x 1152 102545 99340 1.23 1.19
8192 x 768 x 3072 320825 320825 1.42 1.42
2048 x 2048 x 2048 128976 116637 1.04 0.94
2048 x 3072 x 768 112813 106948 1.31 1.24
2048 x 768 x 3072 92579 92579 1.17 1.17
512 x 2048 x 2048 48779 48779 0.90 0.90
512 x 768 x 768 12534 9974 0.72 0.57

Every autotune candidate of the four M = 8192 shapes now compiles.

Tests

tests/ops/gemm, functional check on: test_matmul 11 of 11, test_bmm 7 of 7, test_row_matmul 3 of 3.

Notes

  • Two shapes move further below the TPU (8192x3072x768 0.83, 512x768x768 0.57).
  • Whether the second buffer for C is needed at all is a compiler question, not touched here.
  • A size-2 input is taken to be a half float; an int16 mm would be over-charged.

…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.
… in fp32, stored again

gemm_tile_candidates charged C at the input width, once. The mm template
sums half-precision inputs in an fp32 accumulator and stores C through a
second buffer of the output type, so an fp16 kernel holds 6 bytes per
element of C where 2 were budgeted. Tiles the list admitted then failed in
the compiler as a scratchpad overflow, and they are the largest, so they
filled the top-k autotune times: for 8192x2048x2048 three of four failed
and the one left, BLOCK_N 512, read all of A once per N block (176 MB moved
for 72 MB of operands); for 8192x768x3072 all four failed.

C is now charged at the accumulator's width, plus the output's when that
differs. tpuv6e, no cache, fp16, cycles and sim/TPU against torch_xla:
  8192x2048x2048  608754 -> 387862   1.58 -> 1.01
  8192x3072x768   324971 -> 241750   1.11 -> 0.83
  8192x768x768     99517 ->  92942   1.09 -> 1.02
  8192x384x1152   102545 ->  99340   1.23 -> 1.19
  8192x768x3072   320825 -> 320825   1.42
  2048x2048x2048  128976 -> 116637   1.04 -> 0.94
  2048x3072x768   112813 -> 106948   1.31 -> 1.24
  2048x768x3072    92579 ->  92579   1.17
  512x2048x2048    48779 ->  48779   0.90
  512x768x768      12534 ->   9974   0.72 -> 0.57
Every candidate of the four large shapes now compiles.

tests/ops/gemm: test_matmul 11 of 11, test_bmm 7 of 7, test_row_matmul 3
of 3, functional check on.
@student-Jungmin
student-Jungmin merged commit 3e42fdc into pytorchsim-triton-backend Oct 12, 2026
1 check passed
@student-Jungmin
student-Jungmin deleted the mm-tile-acc-budget branch October 21, 2026 12:30
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