icb-decode-graph
Zero-cgo Metal Indirect Command Buffer (ICB) decode-graph benchmark built on
github.com/tmc/apple/x/metalicb and github.com/tmc/apple/x/metalcounters.
What it measures
The benchmark constructs an N-layer sequential synthetic transformer decode
step alternating between two compute kernels across ping-pong activation
buffers (actA ↔ actB) and a 256-byte-aligned per-layer parameter table:
- Even layers:
rmsnorm_scale (RMSNorm + affine scale + residual mix)
- Odd layers:
ffn_swiglu_residual (SwiGLU gated FFN micro-op + residual add)
Every layer consumes the output of the previous layer, exercising inter-command
barriers (MTLIndirectComputeCommand.SetBarrier) inside the indirect command
buffer.
It compares two execution paths from the identical initial activation state
across 128 measured decode steps:
- Traditional per-dispatch CPU encoding: encodes all N dispatches
individually (
SetComputePipelineState, SetBufferWithOffsetAtIndex ×3,
DispatchThreadgroupsThreadsPerThreadgroup) on every step.
- ICB single-replay (
x/metalicb.Graph): encodes the N-layer graph once
on the CPU, runs Graph.Optimize once on a blit encoder, and replays the
entire graph on each step with a single Graph.Replay
(UseResourceUsage + ExecuteCommandsInBufferWithRange).
Both paths are timed for CPU encode latency, GPU execution time (via
x/metalcounters pass-boundary timestamps cross-checked against
MTLCommandBuffer.GPUEndTime - GPUStartTime), and end-to-end wall time, and
checked for exact bit-for-bit floating-point parity (maxAbsDiff == 0).
Usage
CGO_ENABLED=0 go run ./examples/metal/icb-decode-graph
CGO_ENABLED=0 go run ./examples/metal/icb-decode-graph -layers 128 -steps 128
CGO_ENABLED=0 go test -v -count=1 ./examples/metal/icb-decode-graph
Live Results (Apple M4 Max)
Recorded on Apple M4 Max, CGO_ENABLED=0, dim=4096, work=16,
steps=128, warmup=8, metalcounters=true:
32 Layers (32 dispatches/step)
ICB one-time setup: build=384.25 µs, optimize=2845.29 µs.
| Mode |
CPU Encode (µs/step) |
GPU Counter (µs/step) |
GPU CmdBuf (µs/step) |
Wall (µs/step) |
| Traditional (per-dispatch) |
330.02 |
172.27 |
172.27 |
1255.19 |
| ICB (single-replay) |
51.67 |
219.54 |
219.54 |
650.71 |
| Speedup (ICB vs Traditional) |
6.39x |
0.78x |
0.78x |
1.93x |
Numerical parity: maxAbsDiff=0, exactBitMatch=true.
128 Layers (128 dispatches/step)
ICB one-time setup: build=2282.50 µs, optimize=2046.29 µs.
| Mode |
CPU Encode (µs/step) |
GPU Counter (µs/step) |
GPU CmdBuf (µs/step) |
Wall (µs/step) |
| Traditional (per-dispatch) |
1633.65 |
679.56 |
679.58 |
7969.31 |
| ICB (single-replay) |
61.48 |
840.83 |
840.85 |
2238.90 |
| Speedup (ICB vs Traditional) |
26.57x |
0.81x |
0.81x |
3.56x |
Numerical parity: maxAbsDiff=0, exactBitMatch=true.