flash-sdpa-bench

command
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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

  1. 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).
  2. 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.
  3. 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.

Documentation

Rendered for darwin/amd64

Overview

Command flash-sdpa-bench is a zero-cgo Apple first-party numerical and speed oracle built on MPSGraphExecutable.

It exercises two core LLM inference primitives through purego Metal and MetalPerformanceShadersGraph bindings:

  • Fused causal Scaled Dot-Product Attention (SDPA) via MPSGraph.ScaledDotProductAttentionWithQueryTensorKeyTensorValueTensorDescriptorName with MPSGraphSDPADescriptor (SetIsCausal, optional SetSinksTensor).
  • Quantized int4/int8 weight dequantization + matrix multiplication via DequantizeTensorScaleTensorZeroPointTensorDataTypeAxisName and MatrixMultiplicationWithPrimaryTensorSecondaryTensorName.

Both primitives are verified against a pure-Go float64 CPU reference before running steady-state MPSGraphExecutable benchmarks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL