async-prefetch-ring
Zero-cgo out-of-core SSD-to-GPU transformer layer-streaming demo on Apple Silicon
(//go:build darwin, CGO_ENABLED=0).
Streams N compressed transformer layer shards (default: 16 × 8 MiB = 128 MiB)
from $TMPDIR through a 2-slot ping-pong Metal placement heap (16 MiB
total GPU resident weight memory — an 8.0x memory reduction), overlapping
asynchronous MTLIOCommandQueue decompression of layer i+1 with GPU compute
of layer i via MTLSharedEvent synchronization.
What it composes
x/compression + metal.MTLIOCreateCompressionContext: generates N
deterministic quantized FP16-like layer shards, packs each into a chunked
.mtlio file (LZFSE or LZ4) in $TMPDIR, and cross-checks layer 0 with
compression.Encode / compression.Decode for raw codec ratio and parity.
x/metalio: opens all N compressed shards via OpenCompressedFile and
drives an asynchronous MTLIOCommandQueue loading layer i+1 into ring slot
(i+1)%2.
x/metalheap: allocates a single MTLHeapTypePlacement heap sized
strictly to 2 × alignedLayerSize (16 MiB), sub-allocates two fixed ring
slots (slots[0], slots[1]), proves a 3rd slot fails with ErrOutOfMemory,
and commits the heap to an MTLResidencySet attached to the compute queue.
MTLSharedEvent: coordinates lockstep producer-consumer handoff between
the IO queue and the compute queue without CPU stalls:
ioCB[i] waits for ioDoneEvent >= i (one active multi-core IO
decompression at a time) and, for i >= 2, waits for
computeDoneEvent >= i-1 (slot i%2 released by layer i-2), then loads
layer i and signals ioDoneEvent = i+1.
compCB[i] waits for ioDoneEvent >= i+1, runs transformer_layer_step
across every weight in slots[i%2], and signals computeDoneEvent = i+1.
x/metalcounters: attaches Metal 3 timestamp sampling
(MTLCounterSampleBuffer) to every layer's compute pass to measure GPU
compute time, inter-layer GPU idle gaps, GPU utilization, and overlap
efficiency.
Disk (.mtlio shards in $TMPDIR)
│ x/metalio (MTLIOCommandQueue, OpenCompressedFile)
▼
┌──────────────────────────────────────────────────────────┐
│ x/metalheap 2-Slot Placement Heap (16 MiB, bounded) │
│ MTLResidencySet: AllocatedSize = 16,777,216 bytes │
│ │
│ slot[0] (8 MiB): layer i ──► Metal Compute (GPU) │
│ slot[1] (8 MiB): layer i+1 ◄── Async MTLIO Prefetch │
└──────────────────────────────────────────────────────────┘
▲ │
└── MTLSharedEvent (computeDone) ────┘
└── MTLSharedEvent (ioDone) ────► Compute waits
Usage
CGO_ENABLED=0 go vet ./examples/metal/async-prefetch-ring
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/metal/async-prefetch-ring
CGO_ENABLED=0 go run ./examples/metal/async-prefetch-ring
CGO_ENABLED=0 go run ./examples/metal/async-prefetch-ring -codec lz4
Live Results (Apple M4 Max, macOS 26.6.2)
Recorded on Apple M4 Max (UnifiedMemory=true), macOS 26.6.2 (Build 25G83),
Go 1.27.0 (darwin/arm64), CGO_ENABLED=0, 16 layers × 8 MiB = 128.00 MiB
uncompressed (chunk=65536 B, compute-iters=256, threads=32768, iters=3,
warmup=1):
Memory Bound & Parity
- 2-slot placement heap:
16.00 MiB (16,777,216 bytes) vs 128.00 MiB
total model weights (8.0x resident memory reduction).
MTLResidencySet: AllocatedSize = 16,777,216 bytes (activeSlots=2,
3rd-slot allocation rejected with metalheap.ErrOutOfMemory).
- Final layer output checksum:
bit_identical=true (CRC32=0xaf018b8f)
across serial and overlapped modes for both LZFSE and LZ4.
Serial vs. Overlapped Prefetch Ring
| Codec |
Disk MiB |
Ratio |
Mode |
Wall (ms) |
GPU (ms) |
Idle Gap (ms) |
GPU Util |
Eff. GiB/s |
Speedup |
Overlap Eff. |
LZFSE |
72.38 |
1.77x |
serial load-then-compute |
1820.70 |
110.26 |
1654.16 |
6.1% |
0.07 |
1.00x |
— |
LZFSE |
72.38 |
1.77x |
overlapped prefetch ring |
1373.91 |
91.73 |
1081.71 |
6.7% |
0.09 |
1.33x |
90.5% |
LZ4 |
111.71 |
1.15x |
serial load-then-compute |
361.43 |
71.74 |
271.65 |
19.9% |
0.35 |
1.00x |
— |
LZ4 |
111.71 |
1.15x |
overlapped prefetch ring |
132.99 |
53.32 |
45.82 |
40.1% |
0.94 |
2.72x |
100.0% |
Notes
- Both
LZFSE (1.77x on-disk compression, 72.38 MiB for 128 MiB weights)
and LZ4 (1.15x, 111.71 MiB) match x/compression.Encode/Decode raw
ratios (roundtrip=true).
- In
LZ4 mode, overlapping async MTLIOCommandQueue prefetch with GPU compute
cuts wall time from 361.43 ms to 132.99 ms (2.72x speedup, 0.94 GiB/s
effective streaming+compute throughput) and cuts x/metalcounters inter-layer
GPU idle bubbles by 5.9x (271.65 ms → 45.82 ms), eliminating both
serial IO waits and per-layer CPU round-trip stalls.