Repository navigation
Description
Summary
torch.matmul(a, b) (a: M x K, b: K x N, fp16) simulated on the tpuv6e config is compared with the same op measured on a real TPU v6e through torch_xla. After the fixes listed below the two agree on large shapes, but 23 of 62 representative shapes are still more than 20% off, and the misses are not random: they sit in three regions of the table. This issue asks for the cause of each region. It is written so that someone with no context from the earlier work can pick it up.
Mean absolute percentage error over the 62 shapes is 20.4% (it was 59.6% before the fixes); 44 of 62 are within 25%.
What is compared
- TPU side:
tpu-perf-archive/data/v6e/gemm/torchxla.csv, columnlatency_ns(per-step median, torch_xla, cross-program prefetch ON, the default). 230 shapes; the 62 used here are 7 layers (K x N) times every M the table has. - Simulator side: one
torch.matmulthroughtorch.npu.launch_model, configsystolic_ws_256x256_c1_simple_noc_tpuv6e_timing_only.ymlwithl2d_type: nocacheas the only change (v6e has no L2 data cache).sim_ns = total cycles / 3.502(core at 3502 MHz). - The number below is
sim_ns / TPU latency_ns. 1.00 is a match, below 1 the simulator is faster than the TPU.
Stack that produced the table
| piece | commit | note |
|---|---|---|
| PyTorchSim (this repo) | ee5f8a4, branch shared-load-tag |
= pytorchsim-triton-backend c9a279b + #346 (open) |
| PyTorchSim-Triton-Backend | 9252bab, branch dma-live-extent |
= main 6942cfc + PyTorchSim-Triton-Backend#57 (open) |
| gem5 / Spike | vcix-accelerator 7586e23 (gem5 vcix-v0.3.2, Spike 751354f) |
Already merged and therefore in the table: PyTorchSim-Triton-Backend#54 (a stream walks only the live subtiles of a masked block), #341 (systolic_array_split on by default, unit table from gem5), #342 (a 1 x K row times a matrix runs as vector work), #343 (the mm tile budget counts C as the kernel holds it).
Result: sim / TPU
| M | 384x1152 | 768x768 | 768x3072 | 2048x2048 | 2048x3072 | 3072x768 | 4096x4096 |
|---|---|---|---|---|---|---|---|
| 1 | 1.05 | 0.82 | 1.25 | 0.72 | 1.13 | 1.44 | 1.91 |
| 32 | 0.37 | 0.36 | 0.72 | 0.77 | 0.90 | 0.58 | 0.96 |
| 128 | 0.44 | 0.42 | 0.71 | 0.84 | 0.91 | 0.63 | 0.97 |
| 256 | 0.53 | 0.50 | 0.70 | 0.79 | 0.86 | 0.69 | n/a |
| 512 | 0.63 | 0.57 | 0.89 | 0.83 | 0.87 | 0.89 | 0.91 |
| 1024 | 0.76 | 0.72 | 0.98 | 0.81 | 0.87 | 0.88 | 0.97 |
| 2048 | 0.96 | 0.81 | 1.11 | 0.94 | 1.01 | 0.99 | 0.98 |
| 4096 | 1.07 | 0.96 | 1.25 | 1.03 | 1.11 | 0.99 | 1.05 |
| 8192 | 1.19 | 1.01 | 1.19 | 1.01 | 1.05 | 0.83 | 1.03 |
Bold = more than 20% off. 256x4096x4096 is n/a because its TPU row is not status == ok.
| range | shapes | MAPE | median sim/TPU | within 25% |
|---|---|---|---|---|
| M = 1 | 7 | 32.0% | 1.13 | 4 |
| M = 32..256 | 20 | 31.7% | 0.70 | 8 |
| M = 512..1024 | 14 | 17.3% | 0.87 | 11 |
| M >= 2048 | 21 | 7.9% | 1.01 | 21 |
19 shapes are below 0.8, 4 are above 1.2.
The 23 shapes more than 20% off
TPU columns are from the profiler breakdown in the same CSV (ns): compute_us, prefetch_wait_us, other_ops_us, module_gap_us.
| M x K x N | sim cycles | sim ns | TPU ns | sim/TPU | TPU compute | TPU prefetch wait | TPU other ops | TPU gap | path |
|---|---|---|---|---|---|---|---|---|---|
| 32 x 384 x 1152 | 3925 | 1121 | 2995 | 0.37 | 1349 | 1290 | 0 | 313 | mxu |
| 128 x 384 x 1152 | 5090 | 1453 | 3324 | 0.44 | 1705 | 1287 | 0 | 313 | mxu |
| 256 x 384 x 1152 | 7091 | 2025 | 3853 | 0.53 | 2230 | 1286 | 0 | 313 | mxu |
| 512 x 384 x 1152 | 10508 | 3001 | 4744 | 0.63 | 3139 | 1284 | 0 | 301 | mxu |
| 1024 x 384 x 1152 | 17279 | 4934 | 6506 | 0.76 | 4911 | 1284 | -0 | 299 | mxu |
| 32 x 768 x 768 | 4128 | 1179 | 3235 | 0.36 | 1411 | 1467 | 0 | 315 | mxu |
| 128 x 768 x 768 | 5272 | 1505 | 3574 | 0.42 | 1755 | 1477 | 0 | 313 | mxu |
| 256 x 768 x 768 | 7010 | 2002 | 4006 | 0.50 | 2230 | 1470 | 0 | 289 | mxu |
| 512 x 768 x 768 | 9976 | 2849 | 4986 | 0.57 | 3210 | 1467 | -0 | 299 | mxu |
| 1024 x 768 x 768 | 16850 | 4812 | 6686 | 0.72 | 4640 | 1738 | 0 | 291 | mxu |
| 1 x 768 x 3072 | 31305 | 8939 | 7152 | 1.25 | 5788 | 0 | 1039 | 322 | vpu_reduce |
| 32 x 768 x 3072 | 15274 | 4362 | 6071 | 0.72 | 1990 | 3733 | -0 | 315 | mxu |
| 128 x 768 x 3072 | 17123 | 4889 | 6924 | 0.71 | 2869 | 3727 | 0 | 295 | mxu |
| 256 x 768 x 3072 | 20303 | 5798 | 8246 | 0.70 | 4213 | 3733 | -0 | 296 | mxu |
| 4096 x 768 x 3072 | 159917 | 45664 | 36560 | 1.25 | 31524 | 4731 | 0 | 306 | mxu |
| 1 x 3072 x 768 | 31258 | 8926 | 6216 | 1.44 | 4816 | 0 | 1050 | 322 | vpu_reduce |
| 32 x 3072 x 768 | 12498 | 3569 | 6155 | 0.58 | 2068 | 3745 | 0 | 319 | mxu |
| 128 x 3072 x 768 | 15245 | 4353 | 6931 | 0.63 | 2889 | 3737 | -0 | 290 | mxu |
| 256 x 3072 x 768 | 20195 | 5767 | 8334 | 0.69 | 4277 | 3736 | -0 | 315 | mxu |
| 1 x 2048 x 2048 | 20163 | 5758 | 8031 | 0.72 | 6614 | 0 | 1061 | 321 | vpu_reduce |
| 32 x 2048 x 2048 | 24340 | 6950 | 9005 | 0.77 | 2619 | 6058 | 0 | 316 | mxu |
| 256 x 2048 x 2048 | 33022 | 9429 | 11864 | 0.79 | 5502 | 6046 | -0 | 315 | mxu |
| 1 x 4096 x 4096 | 160883 | 45940 | 24096 | 1.91 | 22644 | 0 | 1100 | 319 | vpu_reduce |
The three regions
A. Small M on the MXU path: the simulator is too fast (19 shapes, 0.36 .. 0.79). Worst on the small layers (384x1152, 768x768) and getting better as M grows. Also the whole 3072x768 and 768x3072 columns for M <= 256.
B. M = 1 (vector path): mixed. 1x4096x4096 is 1.91, 1x3072x768 is 1.44, 1x768x3072 is 1.25, but 1x2048x2048 is 0.72.
C. 768x3072 at large M: the simulator is too slow (4096: 1.25, and 2048 / 8192 at 1.11 / 1.19 just under the line).
What is already known
These are measured facts; the interpretation after each is marked as such.
-
Per-row cost matches, the constant does not (region A). Fitting latency against M for M = 32..512:
layer ns per row of M intercept at M = 0 384x1152 TPU 3.67 2878 ns simulator (before Conv Bias fined grained DMA #57 / [TOGSim] A load of what a buffer already holds is not read again #346) 3.84 1164 ns 768x768 TPU 3.65 3104 ns simulator (before Conv Bias fined grained DMA #57 / [TOGSim] A load of what a buffer already holds is not read again #346) 3.24 1182 ns Adding a constant 1.5 us to every MXU shape of that earlier sweep took the small-layer MAPE from 44% to 8%. Interpretation: a fixed per-op cost of roughly 1.7 .. 1.9 us is missing. The fit has not been redone on the current stack.
-
What one TPU step looks like in the raw trace (32x768x768, steady-state step, ns from step start):
event start duration lead-in, no event 0 313 constant over M copy-start313 12 starts the prefetch of W (1.18 MB) copy-done326 1441 blocks until W has landed fusion(convolution fusion)1769 1531 the only event that grows with M - The compute is one trace event; nothing inside it is visible. Its
memory_access_breakdownshows it reads x from HBM, takes the prefetched W and writes y back, so the TPU's "compute time" includes moving the input and the output. fusionstarts 1.1 ns aftercopy-doneends, in every step: the prefetch does not overlap the compute.- Every op has a floor of about 0.44 .. 0.57 us (a
reshapeof 1.5 KB takes 435 ns). - The optimized HLO carries
estimated_cyclesfor the fusion: 4821 / 5452 / 6580 for M = 32 / 128 / 256 on 768x768, about 4600 at M = 0. The compiler itself predicts the fixed part. - TPU compute intercept by layer, in cycles at 3502 MHz: 4441 (384x1152), 4809 (768x768), 6306 (768x3072), 6444 (3072x768), 6935 (2048x2048), 8450 (2048x3072), 15344 (4096x4096). That is about 4500 + 42 per 256x256 weight subtile, so it is not one array traversal (511 cycles) per subtile.
- The trace states no clock, core count or number of MXUs.
- The compute is one trace event; nothing inside it is visible. Its
-
Which operand the TPU prefetches (all 237 traces): never more than one. On the MXU path it is the larger of x and W in 198 of 210 traces; the 12 exceptions all have a 128 MB operand, which is not prefetched. M = 1 has no prefetch at all.
-
Prefetch off makes the TPU faster on most shapes. An earlier JAX run (2026-09-28,
--xla_msa_max_cross_program_prefetches=0) measured 43 shapes with prefetch on and off: 34 are faster with it off, typically 7 .. 20%, up to 31% (512x4096x4096). Against that off table the current simulator has a median ratio of 1.01 on the 23 shapes that overlap, MAPE 21.6%. Which of the two TPU tables is the right reference is not settled; the request from the user is to compare against prefetch off, and the off table does not cover the small layers where region A is worst. -
Region C, read from a TOGSim trace of 8192x768x3072 before Conv Bias fined grained DMA #57 / [TOGSim] A load of what a buffer already holds is not read again #346. The four M tiles each re-read all of W, and the array sat idle 54,000 of 320,825 cycles waiting for the third tile's copy of W, which shared the DRAM with the second tile's 12.6 MB store. [TOGSim] A load of what a buffer already holds is not read again #346 removed the re-read (309,622 -> 269,075 cycles). What is left of the gap on this column has not been broken down again.
-
Region B, 1x4096x4096. The reduce node is 1081 cycles while the vector unit is busy 11% of the run and Issue 1.6%: most of the node is neither unit working. Interpretation, not checked in the IR: the sum over K is lowered as a serial chain of dependent adds, so it is latency bound. 1x2048x2048 being fast (0.72) looks like region A's missing constant: the TPU spends 1.0 us in two extra ops (
reduce+reshape) plus a 0.3 us lead-in. -
systolic_array_splitand small layers. With the split on, a node whose rows exceed 256 is shortened as if it ran on both arrays. The TPU's compute slope per row per weight subtile is 1.25 .. 1.27 cycles on the two small layers and falls to 0.58 on 4096x4096. Turning the split off moves 1024x768x768 from 0.72 to 0.85 and leaves 32x768x768 at 0.36, so it explains part of region A at mid M and none of it at small M. The trace cannot confirm how many MXUs a small op uses.
Tried and dropped
- Cutting A, B and C into one transfer per 256 subtile and tracing the stream as stretches that each wait for their own pieces. Functionally fine (Spike matches), but on the shapes that finished the error rose (prefetch-on MAPE 32.7% -> 39.6% over 52 shapes): small shapes got faster still, and wide-weight shapes got much slower for a reason that was not found (32x4096x4096: 97,345 -> 162,136 cycles with the same bytes moved and the same DMA-active cycles). Reverted; nothing of it is on any branch.
- Trying more autotune block candidates (14 instead of 4): at most 5% better on one shape, nothing on the others.
- Lifting Triton's power-of-two block constraint: not possible below the Python check, TTGIR linear layouts cannot express a dimension like 384.
What to find out
- Region A: what the fixed ~1.8 us on the TPU physically is, and whether the simulator should model it (and where: per kernel launch, per DMA, per compute). Start from the lead-in (0.3 us), the prefetch's fixed part (~0.73 us at zero bytes) and the fusion's intercept (~1.28 us). Lay the simulator's own timeline of 32x768x768 next to the TPU's table above, phase by phase, before proposing a constant.
- Region A, mid M: whether a small op should be split over both arrays at all (item 7).
- Region B: confirm or refute the serial-chain reading of the M = 1 reduce in
09-custom.mlir, and explain why 768x3072 / 3072x768 / 4096x4096 are slow while 2048x2048 is fast. - Region C: re-break down 4096x768x3072 and 8192x768x3072 on the current stack. This layer's output (N = 3072) is large and its block is padded to 4096.
- Reference: decide between the prefetch-on and prefetch-off TPU tables. If off, the 62 shapes need a TPU re-measurement with
--xla_msa_max_cross_program_prefetches=0.
Please report each cause with the measurement that shows it, and say so when something is an inference.
How to reproduce
On the measurement machine (/opt/torchsim-local):
cd /opt/torchsim-local/run/sim_v6e_hw
source /opt/torchsim-local/run/fe-main-env.sh # frontend clone run/fe-split + PTB checkout + gem5/Spike of vcix 7586e23
# one shape, keeps its dump:
TOGSIM_CONFIG=$PWD/configs/tpuv6e_default_nocache.yml python sim_gemm.py 32 768 768
# the 62 shapes (10 in parallel, resumable, results_<tag>.csv):
python run_all.py --tag <tag> --jobs 10 --config configs/tpuv6e_default_nocache.yml \
--kn 384x1152,768x768,768x3072,3072x768,2048x2048,2048x3072,4096x4096
- Launch long sweeps with
(setsid nohup ... &); a plain background job dies with the session. - A TOGSim timeline of one kernel: run
TOGSim/build/bin/Simulator --config <yml> --trace_so <launch dir>/trace.so --cycle_table .../trace_cycles.tsv --unit_table .../trace_units.tsv --log_level traceon thelaunch-*directory a run leaves in its dump. run/fe-splitand the PTB checkout must sit on the commits in the stack table, with TOGSim rebuilt (cd TOGSim/build && make).
Where the data is (measurement machine)
| what | path |
|---|---|
| this table's simulator results | run/sim_v6e_hw/results_t43_shared_c9a279b_57.csv, logs in out_t43_shared_c9a279b_57/ |
| the sweep before #57 / #346 | run/sim_v6e_hw/results_t42_77b3a87_29470ec.csv |
| TPU table (prefetch on) | tpu-perf-archive/data/v6e/gemm/torchxla.csv, columns in data/v6e/gemm/README.md |
| TPU raw traces | tpuv6e-gemm-torchxla/profiles/tb.tar.xz |
| trace analysis already done | run/tpu_trace_probe/ (timelines.txt, more.txt, est_cycles.csv, scripts/) |
| TPU prefetch on / off (JAX) | /workspace/tpuv6e_validation/data/JM_20260928_gemm_nocpp/shard_0/{on,off}/xla.csv, flag in flag.txt |
| per-shape sim / TPU breakdown script | run/sim_v6e_hw/probe/t42_break.py (edit the results file name) |
Conventions used so far
- The comparison is judged on these 62 shapes, not the full 230.
- Latency is the per-step median.
- Error is the mean of
|sim - TPU| / TPU.
Activity
student-Jungmin commentedon Oct 12, 2026
Update: both open PRs the table depends on are merged.
pytorchsim-triton-backendaaa3ee8 (#346 merged; same tree asshared-load-tagee5f8a4)maina9fa3ea (PyTorchSim-Triton-Backend#57 merged; same tree asdma-live-extent9252bab)So the table reproduces from the two default branches. PyTorchSim-Triton-Backend#60 (open) moves that repository's
third_party/PyTorchSimpin to aaa3ee8; it only matters for the compiler's own tests, the frontend runs use this repository's TOGSim.student-Jungmin commentedon Oct 12, 2026
Region A: why the simulator is fast at small M -- cause found
In one line: the simulator charges nothing for starting a kernel, a DMA or a compute node; the TPU pays about 2.4 us of such costs per op. Moving bytes and pushing rows are not faster in the simulator -- they are slightly slower. At large M the work is tens of us and the 2.4 us is buried; at small M the work is ~1 us and the 2.4 us is the whole gap. The ratio climbing from 0.36 (M = 32) to 0.76 (M = 1024) is that dilution, not the model getting better.
Phase by phase, 32x768x768 (TPU 3235 ns, simulator 1179 ns)
TPU side from the profiler trace (step 484; the device trace holds exactly four events in the step:
dependency-wait0.1 ns at 1.3 ns,copy-start12 ns at 313 ns,copy-done1441 ns,fusion1531 ns; nothing is nested inside any of them). Simulator side from a TOGSim--log_level tracereplay of the kernel (block (32, 512, 1024), two programs;run/sim_v6e_hw/probe/why_32x768x768/).Take the TPU's three start costs out (313 + 732 + 1391 = 2436 ns) and about 800 ns of work remains -- less than the simulator's 1179 ns.
The same thing over the whole small-layer region
Fit over M = 32..1024 on the current stack (ns = slope x M + intercept):
Slopes agree within 5-9% (simulator slower); intercepts differ by 1.9-2.1 us for every M.
TPU - simulatorover the 20 shapes with M <= 256 is 1.83-2.07 us on the two small layers and 0.9-2.6 us on the wide ones (there the simulator's higher per-row cost offsets part of it).What the TPU's cost is, and what it is not
Checked against the alternatives, because "a constant" is the shape of the evidence, not a mechanism:
prefetch_wait = 732 ns + bytes / 1.58 TB/s, residuals within +-15 ns from 0.28 MB to 70 MB; adding the operand's rows and columns as regressors gives coefficients of 0.001-0.002 ns.1391 ns + 11.0 ns per 256x256 weight subtile(the 11 ns/subtile is 128 KB at ~12 TB/s, i.e. VMEM-to-MXU weight streaming -- an inference). For a 9-subtile layer that part is 99 ns; the 1.39 us remains.reshapetakes 435 ns and areduce569 ns. The MXU fusion's 1.39 us is that per-op floor plus ~0.9 us more that only the MXU op has (pipeline fill, weight latch, result drain -- not separable in the trace).estimated_cycleson the fusion extrapolates to ~4600 cycles at M = 0, so the compiler's cost model carries the same fixed part.So the precise statement is: per HLO op about 0.5 us, per MXU op about 1.4 us, per separate async copy op about 0.73 us (with prefetch on), plus 0.31 us per step before the first op -- all paid once per op regardless of size. In TOGSim all four are zero: the kernel issues at cycle 2, a DMA costs one issue cycle plus 10 icnt cycles and the DRAM latency, a compute node starts the cycle after its barrier, and the gem5 sample measures the kernel body only. There is no config knob for any of them (
icnt_latency_cyclesand the DRAM latency are the only latencies in the yaml).What this does and does not settle
Reproduce
student-Jungmin commentedon Oct 12, 2026
Decision on item 5: the reference is the prefetch-ON table
The torch_xla table (
tpu-perf-archive/data/v6e/gemm/torchxla.csv, cross-program prefetch on, the default) stays the reference. No TPU re-measurement with prefetch off, and the prefetch-off comparison above is informational only.Items 3 (M = 1) and 4 (768x3072 at large M) are being worked on next; item 2 (split on small layers) waits on how item 1's start costs get modelled.
student-Jungmin commentedon Oct 12, 2026
Items 3 and 4: causes found
Method for both: run the shape with its dump kept (
run/sim_v6e_hw/probe/why_run.sh M K N), replay the kernel in TOGSim with--log_level trace, extract per-work-item phases (probe/tile_phases.py <timeline> <raw log>), and replay the same trace ondram_type: simple(configs/tpuv6e_default_nocache_simpledram.yml) to separate the DRAM model from the rest. Logs underrun/npot_probe/why_*.Item 3: M = 1 (vector path)
The kernel after #342 is Inductor's reduce kernel:
XBLOCK = 256columns of N per program,R0_BLOCK = K(whole K at once),N / 256programs. Per program: a load of x as a[256, K]tile with stride 0 on the lane axis (256 copies of the 8-16 KB row, 2 MB for K = 4096; skipped after the first program by #346), the W column block[256 cols x K rows](fp16: 512 B contiguous per K row, rows2Nbytes apart), thenK / 128chunk nodes of 24 + 46 cycles (convert, multiply, accumulate into a 128-wide vector carried through the loop), a final 128 -> 1 reduce node (517 .. 1081 cycles), and a 512 B store. The accumulate loop is a serial chain through the loop-carried accumulator (confirmed in09-custom.mlir:affine.for ... iter_argswitharith.addfon the carried value).(a) 1x4096x4096 is slow because ramulator serves the strided W-block read at half rate. All 32 channels are active, but each channel returns 1,472 reads per 200-cycle interval (825 GB/s total) against 2,928 (1,641 GB/s, the peak) for 1x2048x2048. The W block is 512 B chunks at 8 KB stride (4 KB for 2048). The same trace on the simple DRAM model takes 66,472 cycles (sim/TPU would be 0.79). Changing the address mapper does not help (MOP4CLXOR: 164,377; ChRaBaRoCo: 2,932,834), so it is not the mapper choice; what inside the HBM3 model halves the per-channel rate at this stride was not pinned down. The TPU never issues this pattern: its W is stored tiled (
T(8,128)), so a 256-column block is contiguous.(b) 1x3072x768 and 1x768x3072 are bound by the serial reduce chain, not by DRAM. Simple DRAM changes them by 2%. Computes run one at a time per core, programs after each other, so the serialized compute alone (26.2k / 29.2k cycles) exceeds the TPU's whole op (21.7k / 25.0k cycles at 3502 MHz). Per MAC that is 81-90 MACs/cycle against the TPU's ~140 (2.36 M MACs in 4.8 us of compute); the vector unit is busy only 23% of the run, the rest of each node is latency (spad load, convert, mul, add on the carried value), i.e. the chain is latency bound as suspected.
(c) 1x2048x2048 at 0.72 is region A: it runs at DRAM peak (DMA active 81%, 1,641 GB/s), and the gap is the TPU's start costs (three ops at ~0.5 us each plus the 0.3 us lead-in).
Secondary, measured but small: the broadcast x load is materialized as a
[256, K]DRAM read (2 MB for 8 KB of data, 4,650 cycles once per kernel on 4096); and one work-item is dispatched last instead of in order (block 3 of 12 on 1x768x3072; subgraphs 1 and 3 of 16 on 1x4096x4096), so its load and compute run alone at the end (about one program's time). Where TOGSim reorders it was not found inTileGraph::allocate_subgraph/refill.Item 4: 768x3072 at large M (4096: 1.25, 8192: 1.19)
Block (2048, 4096, 1024), one M tile per 2048 rows (2 / 4 tiles). Per tile: A 3 MB; W 4.7 MB once (shared by #346); matmul node sampled at 80,381 cycles, 43,517 under the split; vector nodes 3,912 (accumulator init) + 6,549 (fp32 -> fp16 convert); store of C 2048 x 3072 fp16 = 12.6 MB.
Timeline of 4096x768x3072 (159,917 cycles = 45.7 us; TPU 36.6 us = 128k cycles, of which compute 110k, prefetch 16.5k):
Our compute (108k) equals the TPU's (110k, which includes writing y). The excess is structural:
Nothing here is specific to N = 3072; the same structure is in every layer, it just weighs most where the output is large (N = 3072 at 2 bytes, padded block 4096) and the per-tile compute is short relative to it. 8192x3072x768 (same work, output 4x smaller) is at 0.83 for the same reason in reverse.
Status of the issue's list
[Frontend] An npu mm the compiler's copy_node.plan holds runs the res…