fast-weight-loader
Zero-cgo benchmark and verification example for github.com/tmc/apple/x/metalio
(MTLIOCommandQueue, MTLIOFileHandle, MTLIOCommandBuffer).
What it does
- Generates a deterministic multi-tensor
float32 weight file in $TMPDIR
(default 64 MiB, 16 tensors; automatically removed on exit).
- Streams the file into a shared
MTLBuffer across four paths:
metalio (single): one Queue.LoadBuffer call for the whole file.
metalio (batch N): one Queue.LoadBatch call encoding N concurrent
tensor loads into a single MTLIOCommandBuffer.
posix os.ReadFile+copy: POSIX read into Go heap + copy into MTLBuffer.
posix mmap+copy: syscall.Mmap + copy into MTLBuffer.
- Verifies exact byte-for-byte and CRC32 parity across all four paths.
- Runs a Metal compute reduction kernel (
sum_squares_f32) directly over the
x/metalio-loaded MTLBuffer and compares against a float64 CPU reference.
Usage
CGO_ENABLED=0 go run ./examples/metal/fast-weight-loader
CGO_ENABLED=0 go run ./examples/metal/fast-weight-loader -size-mib 128 -tensors 32 -iters 5
CGO_ENABLED=0 go test -v ./examples/metal/fast-weight-loader
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, default 64 MiB (67,108,864 bytes,
16 tensors, iters=5, warmup=1):
Parity & Compute Sanity
- Byte & CRC32 parity:
exact=true, crc32=0xaacc2788 across all 4 paths
(single, batch 16, os.ReadFile+copy, mmap+copy).
- Metal compute
sum_squares_f32 sanity: gpu=1398106.893066,
cpu=1398108.833250, rel_err=1.388e-06.
Throughput & Latency (Warm Page Cache)
| Path |
Median |
Mean |
Min |
Median GiB/s |
metalio (single) |
9.941 ms |
11.068 ms |
8.637 ms |
6.29 |
metalio (batch 16) |
2.640 ms |
3.608 ms |
2.048 ms |
23.67 |
posix os.ReadFile+copy |
15.036 ms |
19.731 ms |
12.508 ms |
4.16 |
posix mmap+copy |
8.211 ms |
8.294 ms |
7.863 ms |
7.61 |
Batching per-tensor reads into a concurrent MTLIOCommandQueue (metalio (batch 16))
achieves 23.67 GiB/s median (5.69x over os.ReadFile+copy, 3.11x over
mmap+copy) by letting Metal fan out parallel I/O transfers directly into unified
MTLBuffer memory without an intermediate Go heap allocation.