Downloads · 30 days
3
100% of all-time downloads
EnsueAI/metal-int4-sdpa
metal-int4-sdpa is a machine learning model from EnsueAI. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for kernels.
Core attention kernel from Open-TQ-Metal.
Downloads · 30 days
3
100% of all-time downloads
All-time downloads
3
Public
Repo size
578 KB
Likes
7
Public
Click a slice to open those files.
.md5.4 KB · 78%
From the Hugging Face model README
Core attention kernel from Open-TQ-Metal.
Open-TQ-Metal is a Metal-native implementation of fused compressed-domain attention built by Ensue. The full release enables Llama 3.1 70B at 128K context on a single 64GB Mac, 48x faster attention at 128K context, and includes a C++ inference engine, multiple attention kernels, a 330-experiment cross-architecture analysis, and a paper.
This is the Metal implementation of TurboQuant's fused compressed-domain attention (Zandieh et al., ICLR 2026): A fused int4 Scaled Dot-Product Attention Metal kernel for Apple Silicon (M1/M2/M3/M4).
Computes softmax(Q @ dequant(K_int4)^T * scale) @ dequant(V_int4) in a single
kernel dispatch with online softmax — never materializing full K/V or score matrices.
Benchmarked on M1 Max (32 GPU cores, 64GB unified memory). 56 experiments conducted.
| Config | N | Fused int4 | MLX int4 | Speedup |
|---|---|---|---|---|
| Gemma 4 sliding (sw=1024) | 4096 | 0.34ms | 3.81ms | 11.1x |
| Gemma 4 sliding (sw=1024) | 8192 | 0.40ms | 5.79ms | 14.3x |
| Gemma 4 full (D=512) | 8192 | 0.98ms | 7.87ms | 8.1x |
| Llama 3.1 (D=128) | 8192 | 0.89ms | 2.53ms | 2.9x |
| Config | N | Fused int4 | Native SDPA | Speedup |
|---|---|---|---|---|
| Gemma 4 (D=256) | 2048 | 0.45ms | 1.35ms | 3.0x |
| Llama 3.1 (D=128) | 8192 | 0.88ms | 1.53ms | 1.7x |
| Context | SDPA/token | SDPA ceiling | int4 KV |
|---|---|---|---|
| 4K | 5.1ms | 195 tok/s | 30 MB |
| 8K | 7.2ms | 138 tok/s | 60 MB |
| 32K | 19.6ms | 51 tok/s | 240 MB |
| 128K | 88ms | 11 tok/s | 960 MB |
6.4x KV cache compression — 128K context in 960 MB int4 vs 6.1 GB FP32.
| Metric | Value |
|---|---|
| Cosine similarity (int4 vs FP32 output) | 0.992 |
| Attention KL divergence | 0.004 |
| Top-10 token overlap | 90% |
| Kernel numerical precision | < 1e-6 relative error |
| Deterministic | Yes (bit-identical across runs) |
value = scale * nibble + biasimport kernels
sdpa_int4 = kernels.get_kernel("EnsueAI/metal-int4-sdpa", "sdpa_int4")
# Single-token decode
output = sdpa_int4.sdpa_int4(
queries, # (num_heads, D) float32, MPS
k_quant, # (num_kv_heads, N, D//8) uint32, MPS
k_scales, # (num_kv_heads, N, D//64) float32, MPS
k_biases, # (num_kv_heads, N, D//64) float32, MPS
v_quant, # (num_kv_heads, N, D//8) uint32, MPS
v_scales, # (num_kv_heads, N, D//64) float32, MPS
v_biases, # (num_kv_heads, N, D//64) float32, MPS
gqa_factor=4, # num_heads // num_kv_heads
N=2048, # sequence length
scale=0.0625, # 1/sqrt(D)
sliding_window=0, # 0 = full attention
)
Ported from TurboQuant's sdpa_int4_vector kernel.
Paper: TurboQuant (arxiv.org/abs/2504.19874), ICLR 2026.