flash-sdpa-bench
Zero-cgo (CGO_ENABLED=0) Apple first-party numerical and steady-state speed
oracle built on MPSGraphExecutable via purego metalperformanceshadersgraph,
metal, and foundation bindings.
What it exercises
- Causal Scaled Dot-Product Attention (SDPA) compiled to
MPSGraphExecutable:
- On macOS 27+ (when
MPSGraphSDPADescriptor is present in the runtime), uses
MPSGraph.ScaledDotProductAttentionWithQueryTensorKeyTensorValueTensorDescriptorName
with MPSGraphSDPADescriptor.SetIsCausal(true) and optional SetSinksTensor.
- On macOS 15–26 (where
MPSGraphSDPADescriptor is not yet in the runtime),
automatically falls back to
ScaledDotProductAttentionWithQueryTensorKeyTensorValueTensorMaskTensorScaleName
with a [1, 1, S, S] causal additive mask (0 on/below diagonal, -Inf above).
- Quantized Int8 / Int4 Dequantize + Matrix Multiplication compiled to
MPSGraphExecutable:
MPSDataTypeInt8: per-axis
DequantizeTensorScaleTensorZeroPointTensorDataTypeAxisName followed by
MatrixMultiplicationWithPrimaryTensorSecondaryTensorName.
MPSDataTypeInt4: group-aware
DequantizeTensorScaleTensorZeroPointTensorDataTypeName (2D broadcastable
scale/zero-point, packed two's-complement 4-bit nibbles) followed by
MatrixMultiplicationWithPrimaryTensorSecondaryTensorName.
- Pure-Go
float64 CPU reference oracle checking numerical parity before
running steady-state benchmarks (FP16 < 1e-2, BF16 < 2e-2 max abs err for
causal SDPA; < 1e-2 FP16 / < 3e-2 BF16 for dequant+matmul).
Usage
CGO_ENABLED=0 go test -v -count=1 ./examples/metalperformanceshadersgraph/flash-sdpa-bench
CGO_ENABLED=0 go run ./examples/metalperformanceshadersgraph/flash-sdpa-bench
Configurable flags:
-seq (default 256,1024,2048): comma-separated SDPA sequence lengths
-batch (default 1), -heads (default 16), -head-dim (default 128)
-dtype (default both): fp16, bf16, or both
-sinks (default false): also benchmark causal SDPA with attention sinks (macOS 27+)
-warmup (default 3), -iters (default 10)
-m (default 1024), -k (default 4096), -n (default 4096): dequant+matmul dimensions
-verify-only: run numerical parity checks only
Measured Results
Measured on Apple M4 Max, macOS 26.6.2 (25G83), Go 1.27.0 (darwin/arm64, CGO_ENABLED=0),
using the maskTensor causal SDPA path (macOS < 27):
Numerical Parity vs float64 CPU Reference
| Case |
Max Abs Err |
Mean Abs Err |
RMSE |
Tolerance |
Status |
causal-sdpa-fp16 (1x4x64x64) |
0.000122 |
0.000009 |
0.000015 |
< 1e-2 |
PASS |
causal-sdpa-bf16 (1x4x64x64) |
0.000974 |
0.000070 |
0.000116 |
< 2e-2 |
PASS |
causal-sdpa-sinks-fp16 |
— |
— |
— |
— |
SKIP (macOS 27+) |
dequant-int8-matmul-fp16 (32x64x48) |
0.000781 |
0.000134 |
0.000182 |
< 1e-2 |
PASS |
dequant-int4-matmul-fp16 (32x64x48) |
0.000867 |
0.000095 |
0.000136 |
< 1e-2 |
PASS |
dequant-int8-matmul-bf16 (32x64x48) |
0.008103 |
0.001082 |
0.001543 |
< 3e-2 |
PASS |
dequant-int4-matmul-bf16 (32x64x48) |
0.007969 |
0.000773 |
0.001131 |
< 3e-2 |
PASS |
Causal SDPA Steady-State (B=1, H=16, D=128, warmup=3, iters=10)
Effective causal FLOPs = 2 * B * H * S^2 * D (lower-triangular half of 4 * B * H * S^2 * D).
| DType |
SeqLen |
Median (ms) |
Min (ms) |
Mean (ms) |
Eff. TFLOP/s |
fp16 |
256 |
0.765 |
0.576 |
0.851 |
0.35 |
fp16 |
1024 |
1.911 |
1.587 |
2.735 |
2.25 |
fp16 |
2048 |
4.156 |
3.826 |
4.269 |
4.13 |
bf16 |
256 |
0.588 |
0.518 |
0.603 |
0.46 |
bf16 |
1024 |
1.908 |
1.458 |
2.056 |
2.25 |
bf16 |
2048 |
5.020 |
4.440 |
5.573 |
3.42 |
Quantized Dequant + Matmul Steady-State (M=1024, K=4096, N=4096, axis=1)
Effective FLOPs = 2 * M * K * N (34.36 GFLOPs).
| Quant |
DType |
Median (ms) |
Min (ms) |
Mean (ms) |
Eff. TFLOP/s |
int8 |
fp16 |
4.878 |
3.846 |
4.925 |
7.04 |
int8 |
bf16 |
3.123 |
2.884 |
3.237 |
11.00 |
int4 |
fp16 |
5.988 |
4.290 |
6.983 |
5.74 |
int4 |
bf16 |
4.627 |
4.133 |
5.188 |
7.43 |
Note: MPSNDArrayQuantizedMatrixMultiplication on macOS 26 requires float16 or
float32 scale tensors for fused int4 dequant+matmul, so the int4/bf16 case
uses float16 scales with bfloat16 activations and outputs.