Skip to content

[TOGSim] A barrier waits for every load its semaphore counted - #320

Merged
YWHyuk merged 2 commits into
pytorchsim-triton-backendfrom
togsim-semaphore
Oct 8, 2026
Merged

YWHyuk merged 2 commits into
pytorchsim-triton-backendfrom
togsim-semaphore

Conversation

@YWHyuk

@YWHyuk YWHyuk commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

  • Until now a barrier waited only for the last DMA of its slot.
  • When a slot held several async loads, compute could start before an earlier load had finished.
  • No measured kernel had two or more async loads in a slot before a barrier, so the defect never showed.
  • Peeling will soon split one transfer into N DMAs. At that point this defect becomes wrong timing.

Primitives

  • A transfer signals a semaphore (sem). A wait waits for a batch on that semaphore.
  • A wait counts the elements moved (amount). A barrier waits for every load its semaphore counted.
  • A re-wait (a wait on a semaphore whose batch was already waited) adds no dependency edge and leaves the buffers' writers unchanged. The data already passed that barrier; a compute that rewrote the buffer since (an in-place update) stays the writer its readers wait for. Before, the re-wait made the barrier the buffers' only writer again and dropped that RAW edge.
  • Four cases are hard errors:
    • never signaled: the wait is on a semaphore that no load signalled.
    • another element count: the wait's element count differs from the signalled loads' element count.
    • re-wait expects another element count: a re-wait's count differs from the count of the wait it repeats.
    • unwaited load batch: no wait waits for a signalled load batch.
  • These errors and a refused producer stop the run with exit 1 on both entry points. --trace_so already did. --models_list (the launchKernel path the FIFO TOGSimulator drives) used to fall back to the legacy ONNX TOG when a kernel's trace.so was refused, logged and continued past a command that threw (exit 0), and let a throw from the final cycle() terminate. Now a kernel dir with a trace.so runs it or fails. The legacy ONNX TOG still serves a dir without trace.so, or TORCHSIM_LEGACY_TOG=1.
  • A store wait is not lowered to the TOG or TOGSim. TOGSim does not model the WAR of an MVOUT. Lowered, it would have nothing to wait for and would only leave an unpaired barrier (that is the alibi abort seen in WP0).
  • A spad buffer is sized by the sum of the DMA bytes that fill one version, not by its last DMA. For a buffer filled by several piece DMAs, the scheduler's decision now matches the real occupancy.
  • A DMA sizes only the buffers it moves: what a load fills, and the buffer a store drains, read_bufs[0] (stated in togsim_runtime.h; it is the order PTB already emits). An indirect store's index buffer, read for addresses only, is no longer sized by the store's data tile.

ABI 13 → 14

Commits

  • 7b21d07 A barrier waits for every load its semaphore counted (trace ABI 14). A re-wait keeps an in-place writer. A producer built against another ABI is refused, and refusals and semaphore errors exit 1 on --trace_so and --models_list.
  • 2fd095a A spad buffer is as large as the DMAs that fill one version; an indirect store does not size its index buffer.

Verification

Run with PTB run.sh, --timing --tog-grouping segment.

kernel baseline (WP0) this PR footprint before → after (bytes)
gemm 25666 25666 196608 → 196608
softmax 1410 1410 65536 → 65536
gather_index_computed_per_program 1522 1522 32768 → 32768
tile_gemm_subtiled_accumulator_load 16174 16174 2097152 → 3145728
tile_gemm_subtiled_operand_loads 12370 12370 2097152 → 3145728
selective_scan 2511 2511 1024 → 1024
gather_masked_deep_in_loop 6024 6024 395264 → 395264
causal_conv3d_state 9095 9095 6144 → 6144
alibi abort 2780 81920 → 81920
loop_store_waited_in_the_next_trip abort 385 6144 → 6144
  • The cycles of all 8 baseline kernels are unchanged.
  • alibi now passes. Before, it stopped on an unpaired store-after wait with Key does not exist in subgraph's tag table.
  • The WP4 size fix does not change cycles. Only the footprint of the two subtiled kernels changes, from 2 MB to 3 MB. 3 MB equals three spad globals (memref<512x512xf32>, 1 MB each). The spad is 128 MB, so the dispatch decision is the same.
  • tests/semaphore_batch/run_k1.sh passes (including the refusals of cases 1–3).
  • tests/sram_pieces/run_pieces.sh passes. On the previous binary (semaphore commit only) it fails with simulation wedged ... spad ... too small.

Review fixes (2026-10-08)

e2e, run.sh, af32efa configs, before = 9fe7d84, after = 2fd095a:

kernel before after
gemm 24579 24579
softmax 1776 1776
gather_index_computed_per_program 1625 1625
vcix_systolic_matmul 3793 3793
tile_gemm_subtiled_accumulator_load 17438 17438
tile_gemm_subtiled_operand_loads 15727 15727
selective_scan 2565 2565
gather_masked_deep_in_loop 4342 4342
causal_conv3d_state 9095 9095
alibi 2848 2848
loop_store_waited_in_the_next_trip 385 385
  • All 11 statistics blocks are md5-identical before and after.
  • Re-wait: these kernels have 30 re-waits (gather_masked_deep_in_loop 8, accumulator_load 14, operand_loads 8). In 6 of them (accumulator_load) an in-place vector compute rewrote the batch's buffer before the re-wait, so the old bridge dropped that edge. The first reader after it is a vector op too, and the in-order VPU pipeline already ordered it, so the cycles do not move.
  • New test, run_k1.sh rewait case: load A on sem 3, wait, A = f(A) in place on the cross-lane unit (2000 cycles), wait sem 3 again, a vector compute reads A. The reader issues @2164 after f finishes @2163; on 9fe7d84 it issues @164. Its variant re-waiting for 64 of 128 elements exits 1.
  • --models_list, tested with a trace file launching one kernel dir: a producer with ABI 13 and the K1 wrong-count producer now exit 1 with the error (on 9fe7d84 both exit 0: ONNX fallback / logged error). gemm's trace.so through --models_list prints the same statistics on both binaries. A dir without trace.so still takes the legacy ONNX TOG.
  • Index buffers, measured on 19 compiled kernels against the declared spad globals: kv_cache_write 32 B was sized 512, moe_permute 256 → 4096, selective_state_update 16 → 2048, ptr_scatter_i64_index_plus_strided (i64) 1024 → 512. All four are written by a compute, so they are now unsized, like any buffer no DMA moves (a gather load never sized its index buffer either). New test, run_pieces.sh scatter case: a 256 B index buffer and a 16 KB output drained by one indirect store; footprint 16640 with max_dispatch 2 (on 9fe7d84: 32768, max_dispatch 1). Replayed on a stub cycle table, kv_cache_write, moe_permute, selective_state_update and istft_overlap_add give the same cycles on both binaries (752/1013/10107/2030).
  • Running state stored every trip: in selective_scan, causal_conv3d_state, gated_delta_rule, segment_sum_decay, selective_state_update, lightning_attn, retention_chunk, ssd_chunk, rwkv_token_shift and causal_conv1d_update every buffer is sized exactly as its spad global. No in-place re-drain inflates a version there.

Known limits

  • TOGSim does not model the WAR of an MVOUT. It does not stop spad reuse after a store.
  • gather_masked_deep_in_loop shows 2 WAR violations in the WP0 instrumentation (war_checked=2 war_violation=2). This is a result of the limit above, and this PR does not fix it.
  • The gem5 timeouts of gated_delta_rule and segment_sum_decay are unrelated to this PR. Both pass up to the --tog stage.
  • If stores drain the same bytes of one version more than once, the drain sum is larger than the real one. Measured: 1 case in 19 kernels, istft_overlap_add, which stores its 2048 B buffer twice back to back and sizes it 4096. The trace cannot tell this from a buffer drained in pieces; that needs each DMA's spad offset or the buffer size in the trace (an ABI change), so it is not fixed here.
  • The code that guards against two WakeBar events in the same cycle has never executed.

References

Base change (2026-10-08)

  • The base is now pytorchsim-triton-backend (af32efa). The commits were moved onto it. The TOGSim/ tree is byte-identical to before the move.
  • In configs/, af32efa differs only in HBM3_TPUv6e.yaml (count 32 → 256). PTB Implement tile graph generator #20 measured that difference separately.
  • The e2e cycles measured on this branch equal the af32efa values of PTB Implement tile graph generator #20: gemm 24579, softmax 1776, gather_index_computed_per_program 1625, vcix_systolic_matmul 3793.

@YWHyuk
YWHyuk changed the base branch from togsim-indirect-index to pytorchsim-triton-backend October 8, 2026 07:14
YWHyuk added 2 commits October 8, 2026 16:56
…e last one

Trace ABI 14. togsim_dma carries the semaphore it signals (uint64 sem) in
place of (tag_id, tag_slot), and togsim_memory_barrier(ctx, sem, expected)
states how many elements the wait expects. The bridge keeps an open batch
per semaphore in each work item: every async load appends to it, and the
barrier takes a DONE edge from each load in the batch and a wait key per
load, so it releases only when all of them have arrived. Before, the bridge
paired a barrier with the last load issued on the slot, and a barrier over
two loads released when the second (small) one landed, ahead of the first.

Instruction gains wait keys (none set -> the barrier's own tag key, so the
legacy ONNX and SparseCore paths are unchanged); Core issues a MEMORY_BAR
against all its keys and WakeBar re-checks them, finishing a barrier once.
The bridge refuses four things as errors naming work item, key, sem, sum
and expected: a wait whose batch sums to another count, a re-wait that
expects another count than the wait it repeats, a wait on a semaphore never
signaled, and a load batch left unwaited at flush. A re-wait (a wait on a
semaphore whose batch was already waited) adds no edge and leaves the
buffers' writers alone: the data already passed that barrier, and a compute
that rewrote the buffer since (an in-place update) stays the writer its
readers wait for.

Both entry points stop with exit 1 on those errors and on a refused
producer. --trace_so did; --models_list (the launchKernel path the FIFO
TOGSimulator drives) fell back to the legacy ONNX TOG when a kernel's
trace.so was refused, logged and continued past a command that threw, and
let a throw from the final cycle() terminate. Now a kernel dir with a
trace.so runs it or fails, any command that throws ends the run with its
message, and the legacy ONNX TOG still serves a dir without trace.so (or
TORCHSIM_LEGACY_TOG=1).

A producer built against another ABI is refused, not run. togsim_runtime.h
claimed this but nothing compared. The header now defines a weak
togsim_producer_abi_version in every producer TU (TOGSim's own TUs, built
with TOGSIM_HOST, only declare it), and LazyProducer::open dlsym's it and
refuses a producer that lacks it or carries another version, naming both.
Stale comments fixed: tag_slot never drove an SRAM double-buffer model.

Checked: tests/semaphore_batch/run_k1.sh -- two loads on sem 7, the first
strided so it lands @2583 and the second @150; the barrier finishes @2584
(previous bridge: @151, before the first load). Cases 1-3 (wrong count,
never signaled, unwaited) exit 1 with their refusal. The re-wait case --
load A on sem 3, wait, A = f(A) in place on the cross-lane unit for 2000
cycles, wait sem 3 again, a vector compute reads A -- issues the reader
@2164 after f finishes @2163 (previous bridge: @164, the barrier became
A's writer again); its variant re-waiting for 64 of 128 elements exits
1. A producer without the version symbol and one built against another ABI both exit 1, on
--trace_so and on --models_list (previous: exit 0 after an ONNX fallback;
a semaphore error was logged and the run exited 0); a dir without trace.so
still takes the ONNX TOG, and gemm's trace.so through --models_list prints
the same statistics on both binaries. gemm,
softmax, gather_index_computed_per_program,
tile_gemm_subtiled_{accumulator_load,operand_loads}, selective_scan,
gather_masked_deep_in_loop and causal_conv3d_state give the same cycles and
md5-identical statistics as before (25666/1410/1522/16174/12370/2511/6024/
9095 with the 6dc8600 configs). With PTB's ABI 14 lowering alibi runs
(2780) instead of aborting on 'Key does not exist in subgraph's tag table'.
With the af32efa configs the re-wait change leaves every cycle count and
statistics block unchanged (gemm 24579, softmax 1776,
gather_index_computed_per_program 1625, vcix_systolic_matmul 3793,
tile_gemm_subtiled_{accumulator_load,operand_loads} 17438/15727,
selective_scan 2565, gather_masked_deep_in_loop 4342, causal_conv3d_state
9095, alibi 2848, loop_store_waited_in_the_next_trip 385). Of their 30
re-waits, 6 (accumulator_load) follow an in-place vector write of the
batch's buffer, and its first reader is a vector op too, so the in-order
VPU pipeline already ordered what the dropped edge should have.
… not its last DMA

The bridge sized each spad buffer by the last DMA record that touched it
(buf_bytes[b] = rec_bytes(t)). A buffer the compiler fills or drains in
pieces therefore counted as one piece. That size feeds the work-item's spad
footprint, which decides whether two dispatches share the spad, and the
footprint of a compute that opens the buffer. The per-load occupancy
(try_occupy_sram) already summed the pieces into one version, so the two
disagreed: the scheduler admitted two work-items the spad could not hold.

sram_schedule() now sums, per buffer version, the bytes of the loads that
fill it and of the stores that drain it, under the same version rule as
before (a load or fresh compute write opens a version, a consuming read
closes it); a buffer is its largest version. A reload after a consuming
read is a new version and is not added. index() keeps the largest single
record as the floor for a buffer no version tracks. try_occupy_sram is
unchanged: each piece occupies its own bytes in the shared version.

Only the buffers a DMA moves are sized by it: what a load fills, and the
buffer a store drains, read_bufs[0] (togsim_runtime.h now says so; it is
the order the producer already emits). An indirect store also lists its
index buffer, which it reads for addresses; that buffer used to take the
store's data-tile bytes. Across 19 compiled kernels (scatter, gather and
running-state), against the declared spad globals: kv_cache_write's index
buffer 32 B was sized 512, moe_permute's 256 -> 4096,
selective_state_update's 16 -> 2048, ptr_scatter_i64_index_plus_strided's
i64 index 1024 -> 512. All four are written by a compute, so they are now
unsized like any buffer no DMA moves; a gather load never sized its index
buffer either.

Not fixed, because the trace cannot tell it apart from pieces: two stores
that drain the same bytes of one version add up. istft_overlap_add stores
its 2048 B buffer twice back to back (two DRAM rows) and sizes it 4096. An
in-place compute between re-drains (a running state stored every trip)
would do the same; none of the running-state kernels measured does
(selective_scan, causal_conv3d_state, gated_delta_rule, segment_sum_decay,
selective_state_update, lightning_attn, retention_chunk, ssd_chunk,
rwkv_token_shift, causal_conv1d_update: every buffer equals its global).
Telling them apart needs each DMA's spad offset or the buffer's size in
the trace, an ABI change.

Checked: tests/sram_pieces/run_pieces.sh -- a 16 KB load buffer in four
4 KB pieces, reloaded after a read, and a 16 KB output drained by four
4 KB stores, two work-items on a 48 KB spad. Footprint 32768 with
max_dispatch 1 each; before, it is 8192 with max_dispatch 2 and the
run wedges ("spad ... too small"). Its scatter case -- a 256 B index
buffer loaded, a 16 KB output computed and drained by one indirect store
reading both -- schedules 16640 B with max_dispatch 2; sizing the index
buffer by the store gives 32768 and max_dispatch 1.
semaphore_batch/run_k1.sh passes.
Replaying the traces of the kernels above: footprint moves only for
tile_gemm_subtiled_{operand_loads,accumulator_load}, 2097152 -> 3145728
(three 512x512xf32 spad globals); cycles are unchanged everywhere
(25666/1410/1522/16174/12370/2511/6024/9095, alibi 2780, K2 385) since
the tpuv6e spad is 128 MB. With the af32efa configs the e2e cycles and
statistics blocks of gemm, softmax, gather_index_computed_per_program,
vcix_systolic_matmul, the two subtiled kernels, selective_scan,
gather_masked_deep_in_loop, causal_conv3d_state, alibi and
loop_store_waited_in_the_next_trip are unchanged by the index-buffer rule
(24579/1776/1625/3793/17438/15727/2565/4342/9095/2848/385); so are
kv_cache_write, moe_permute, selective_state_update and istft_overlap_add
replayed on a stub cycle table (752/1013/10107/2030).
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