dispatch-profiler
Zero-cgo per-dispatch GPU timestamp profiler for a 32-dispatch synthetic
transformer micro-layer built on github.com/tmc/apple/x/metalcounters.
What it measures
The profiler encodes a 32-dispatch transformer micro-layer (4 stages × 8
independent heads/tiles: rmsnorm_tile, qkv_proj_head, attn_score_head,
mlp_swiglu_tile) and compares barrier-serialized vs.
concurrent/overlapped execution across both Metal counter backends:
- Metal 3 (
MTLCounterSampleBuffer):
- Barrier-serialized (per-dispatch sampled): samples each dispatch's
GPU start/end timestamps (64 samples across 32 passes when
DispatchBoundary=false on Apple Silicon; or 33 samples via
SampleCountersInBufferAtSampleIndexWithBarrier when
DispatchBoundary=true).
- Unsampled baseline: runs the identical 32-pass workload without sample
buffer attachments to isolate per-sample GPU overhead.
- Single serial vs. single concurrent pass: compares
MTLDispatchTypeSerial against MTLDispatchTypeConcurrent with
pass-boundary attachments, and checks the counter span delta against
MTLCommandBuffer.GPUEndTime - GPUStartTime.
- Metal 4 (
MTL4CounterHeap) (when supported):
- Barrier-serialized (33 samples,
MTL4TimestampGranularityPrecise):
all 32 dispatches in a single MTL4ComputeCommandEncoder separated by
dispatch-stage barriers and in-encoder WriteTimestamp.
- Barrier-serialized (2 samples, start/end only): isolates per-sample
WriteTimestamp overhead.
- Concurrent/overlapped (no barriers): runs all 32 independent
dispatches in one
MTL4ComputeCommandEncoder without inter-dispatch
barriers to measure hardware overlap speedup.
Usage
CGO_ENABLED=0 go run ./examples/metal/dispatch-profiler
CGO_ENABLED=0 go run ./examples/metal/dispatch-profiler -threads 8192 -work 128 -iters 9
CGO_ENABLED=0 go test -v -count=1 ./examples/metal/dispatch-profiler
Live Results (Apple M4 Max)
Recorded on Apple M4 Max, CGO_ENABLED=0, default configuration
(dispatches=32, threads=4096, work=96, iters=7, warmup=2),
Capabilities: timestamp=true stage=true dispatch=false m3=true m4=true freq=24000000Hz:
| Mode |
Counter Span |
CmdBuf GPU |
Delta |
Notes |
| Barrier-serialized (64 samples) |
498.29 µs |
498.33 µs |
-0.00 µs |
Sum of 32 dispatch medians: 469.17 µs |
| Barrier-serialized (unsampled) |
— |
379.25 µs |
— |
Overhead: +1.861 µs/sample |
Single serial pass (MTLDispatchTypeSerial) |
378.88 µs |
378.88 µs |
0.00 µs |
2 pass-boundary samples |
Single concurrent pass (MTLDispatchTypeConcurrent) |
74.79 µs |
74.75 µs |
+0.00 µs |
5.07x overlap speedup vs. serial pass |
| Mode |
Counter Span |
Notes |
| Barrier-serialized (33 samples, precise) |
520.29 µs |
Exact telescoping sum of 32 dispatches: 520.29 µs |
| Barrier-serialized (2 samples, start/end) |
392.54 µs |
Overhead: +4.121 µs/sample (31 extra precise samples) |
| Concurrent/overlapped (no barriers) |
74.38 µs |
7.00x overlap speedup vs. barrier-serialized |
| Stage (8 dispatches each) |
Metal 3 (µs) |
Metal 4 (µs) |
rmsnorm_tile[0..7] |
12.00 – 12.29 |
12.17 – 13.38 |
qkv_proj_head[0..7] |
14.04 – 14.33 |
15.12 – 15.67 |
attn_score_head[0..7] |
13.67 – 13.92 |
14.62 – 15.08 |
mlp_swiglu_tile[0..7] |
18.46 – 18.62 |
19.54 – 20.21 |