compressed-weight-stream
Zero-cgo benchmark and verification example comparing Metal native compressed
weight loading (MTLIOCreateCompressionContext + x/metalio.OpenCompressedFile)
against uncompressed x/metalio and CPU x/compression.Decode + copy.
What it does
- Generates a deterministic synthetic quantized/FP16-like weight tensor in
$TMPDIR (default 64 MiB; automatically removed on exit) with clustered
small FP16 exponents, quantized mantissas, and ~25% zero sparsity.
- Packs
.mtlio compressed files using Metal's native
MTLIOCreateCompressionContext, MTLIOCompressionContextAppendData, and
MTLIOFlushAndDestroyCompressionContext for both LZ4 and LZFSE.
- Encodes matching raw
LZ4 and LZFSE files using x/compression.Encode.
- Loads all five representations into a shared
MTLBuffer and compares
on-disk size, compression ratio, latency, effective uncompressed GiB/s, and
exact byte-for-byte + CRC32 parity:
metalio (uncompressed): x/metalio.OpenFile + Queue.LoadBuffer.
metalio (LZ4 .mtlio): x/metalio.OpenCompressedFile(..., MTLIOCompressionMethodLZ4) + Queue.LoadBuffer.
metalio (LZFSE .mtlio): x/metalio.OpenCompressedFile(..., MTLIOCompressionMethodLZFSE) + Queue.LoadBuffer.
cpu x/compression (LZ4)+copy: os.ReadFile + x/compression.Decode(LZ4, ...) + copy into MTLBuffer.
cpu x/compression (LZFSE)+copy: os.ReadFile + x/compression.Decode(LZFSE, ...) + copy into MTLBuffer.
Usage
CGO_ENABLED=0 go run ./examples/metal/compressed-weight-stream
CGO_ENABLED=0 go run ./examples/metal/compressed-weight-stream -size-mib 128 -iters 5
CGO_ENABLED=0 go test -v ./examples/metal/compressed-weight-stream
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,
MTLIO chunk size 65,536 bytes, iters=5, warmup=1):
Parity
- Byte & CRC32 parity:
all_exact=true, crc32=0x74c44f1d across all 5 paths.
Compression Ratio & Throughput (Warm Page Cache)
| Path |
Disk MiB |
Ratio |
Median |
Mean |
Min |
Eff. GiB/s |
CRC32 |
Exact |
metalio (uncompressed) |
64.00 |
1.00x |
5.797ms |
6.905ms |
5.265ms |
10.78 |
0x74c44f1d |
true |
metalio (LZ4 .mtlio) |
55.85 |
1.15x |
53.851ms |
60.747ms |
38.547ms |
1.16 |
0x74c44f1d |
true |
metalio (LZFSE .mtlio) |
36.19 |
1.77x |
113.204ms |
118.189ms |
111.517ms |
0.55 |
0x74c44f1d |
true |
cpu x/compression (LZ4)+copy |
55.55 |
1.15x |
48.732ms |
47.079ms |
41.424ms |
1.28 |
0x74c44f1d |
true |
cpu x/compression (LZFSE)+copy |
36.18 |
1.77x |
109.435ms |
114.335ms |
106.14ms |
0.57 |
0x74c44f1d |
true |
Notes
- LZFSE shrinks the synthetic quantized/FP16-like tensor by 1.77x (
64.00 MiB -> 36.19 MiB
on disk), compared to 1.15x (55.85 MiB) for LZ4.
- Under warm page cache, single-buffer
metalio (LZ4 .mtlio) (1.16 GiB/s) and
metalio (LZFSE .mtlio) (0.55 GiB/s) match CPU x/compression.Decode + copy
(1.28 GiB/s / 0.57 GiB/s) while decoding directly into the target MTLBuffer
without allocating an intermediate Go heap buffer.
- Uncompressed
metalio (10.78 GiB/s) is memory-copy bound under warm page cache;
compressed .mtlio trades CPU/decompression throughput for smaller storage footprint
and reduced cold-storage I/O.