Repository navigation
[TOGSim] A barrier waits for every load its semaphore counted - #320
Merged
Merged
Conversation
YWHyuk
force-pushed
the
togsim-semaphore
branch
from
October 8, 2026 07:14
cbe3a92 to
f21e369
Compare
YWHyuk
changed the base branch from
togsim-indirect-index
to
pytorchsim-triton-backend
October 8, 2026 07:14
YWHyuk
force-pushed
the
togsim-semaphore
branch
from
October 8, 2026 07:17
f21e369 to
9fe7d84
Compare
…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).
YWHyuk
force-pushed
the
togsim-semaphore
branch
from
October 8, 2026 07:57
9fe7d84 to
2fd095a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Primitives
sem). A wait waits for a batch on that semaphore.amount). A barrier waits for every load its semaphore counted.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.--trace_soalready did.--models_list(thelaunchKernelpath the FIFOTOGSimulatordrives) used to fall back to the legacy ONNX TOG when a kernel'strace.sowas refused, logged and continued past a command that threw (exit 0), and let a throw from the finalcycle()terminate. Now a kernel dir with atrace.soruns it or fails. The legacy ONNX TOG still serves a dir withouttrace.so, orTORCHSIM_LEGACY_TOG=1.read_bufs[0](stated intogsim_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
TOGSIM_ABI_VERSIONgoes from 13 to 14..sobuilt against another version (or without the version symbol). The refusal happens when TOGSim opens the producer at the kernel's launch, not before the run starts; the run then exits 1, on--trace_soand on--models_list. A TOGSim older than this PR has no check.Commits
7b21d07A 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_soand--models_list.2fd095aA 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.Key does not exist in subgraph's tag table.memref<512x512xf32>, 1 MB each). The spad is 128 MB, so the dispatch decision is the same.tests/semaphore_batch/run_k1.shpasses (including the refusals of cases 1–3).tests/sram_pieces/run_pieces.shpasses. On the previous binary (semaphore commit only) it fails withsimulation wedged ... spad ... too small.Review fixes (2026-10-08)
e2e,
run.sh, af32efa configs, before =9fe7d84, after =2fd095a:run_k1.shrewait 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; on9fe7d84it 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 (on9fe7d84both exit 0: ONNX fallback / logged error). gemm'strace.sothrough--models_listprints the same statistics on both binaries. A dir withouttrace.sostill takes the legacy ONNX TOG.run_pieces.shscatter case: a 256 B index buffer and a 16 KB output drained by one indirect store; footprint 16640 with max_dispatch 2 (on9fe7d84: 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).Known limits
war_checked=2 war_violation=2). This is a result of the limit above, and this PR does not fix it.--togstage.References
Base change (2026-10-08)
pytorchsim-triton-backend(af32efa). The commits were moved onto it. TheTOGSim/tree is byte-identical to before the move.configs/,af32efadiffers only inHBM3_TPUv6e.yaml(count 32 → 256). PTB Implement tile graph generator #20 measured that difference separately.