Sub-cubic, finite-ring modular CRT, and Hadamard-INT8-NAX matrix multiplication kernels for Apple Silicon (MLX / Metal 4).
mlx-tachyon is an experimental-to-production research library for Apple MLX targeting Apple M5-series Neural Accelerators (mpp::tensor_ops::matmul2d). On M1βM4 or CPU environments, it detects the lack of Metal 4 NAX at runtime and falls back cleanly to mx.matmul with a warning.
Apple's built-in mx.matmul already reaches ~93% of M5 Max's nominal 16-bit mlx-tachyon explores algorithmic ways to improve speed or precision on top of Apple Silicon's hardware:
Floating-point without INT8 quantization (mode="no_quant"): -
EqFP16-NAX (float32): Exact IEEE-754 power-of-2 row equilibration across$10^{\pm 38}$ dynamic range + FP16 NAX with FP32 accumulation (rel_L2 = 2.93e-4 vsFP64 ,+29% to+41% faster than MLX's default relaxedfloat32 on M5). #
Auto-compiled pure-dyadic Smirnov Performs$\langle 3,3,6; 40 \rangle$ :$40/54 = 0.741\times$ $1.350\times$ fewer), delivering up to69.0 effective TFLOP/s (51.1 actual hardware TFLOP/s) on$15360^3$ bfloat16 /float16 (+30% to+49% wall-clock speedup overmx.matmul ).
2.
3.
Finite-ring modular sub-cubic arithmetic (mode="strict_fp32" & fast_matmul_fp64): - Executes pure-dyadic Smirnov $\langle 3,3,6; 40 \rangle$ inside pairwise-coprimeodd residue rings$\mathbb{Z}/m_s\mathbb{Z}$ (0.000e+0 bit-exact homomorphism vs direct CRT β eliminating floating-point sub-cubic instability), reconstructed via 72-bit triple-float CRT on 8-bit integer NAX cores.
- Delivers strict IEEE-754
float32(rel_L2 = 3.82e-7,+26%to+70%faster than M5 Max's strict-FP32 NAX path) and15-digit IEEE-754float64(rel_L2 = 5.91e-15,max|err| = 9.38e-16) at5.9β6.8 effective TFLOP/s (12.2Γβ13.7Γ faster than Apple M5 Max CPU AccelerateFP64on$6144^3$ β$9216^3$!).
- Executes pure-dyadic Smirnov
Lossy INT8 LLM training kernels (mode="fast_train" & tachyonize(model)): - Maps bfloat16 /float16 /float32 onto M5 Max's 122.4 TOPS INT8 NAX using bilinear diagonal channel balancing ($\Lambda \Lambda^{-1} = I$ ), vectorized atomic-max column transforms, and optional reproducible unbiased stochastic rounding (~1.2% rel L2 noise, zero bias).
- Drop-in
tachyonize(model)accelerates real end-to-end Qwen3-style training steps (including RMSNorm, QK-Norm, RoPE, causal SDPA, cross-entropy, backward, and AdamW) by**+10.0%** (2L1.3B) to**+20.5%** (1L8B) with identical 3-seed loss (mean_gap = -0.0010) and gradient norms.
- Maps
High-precision 2-slice Ozaki (mode="high_prec"): - 2-slice residual RHT-INT8 ( rel_L2 = 8.23e-5 vsFP64 ), ~9.3Γ closer toFP64 than MLX's default relaxedfloat32 on M5.
8. 2-slice residual RHT-INT8 (
Experimental modes: mode="experimental_ultra" (Direct-INT8 Smirnov-40, ~3.1% rel L2) and opt-in allow_experimental=True Kronecker ~1.7e-3 in fp16 at ~6.2% in INT8 at
$\langle 6,6,12; 280 \rangle$ ($\ge 18432^3$ ,$\ge 24576^3$ ) are explicitly marked experimental due to narrow activation thresholds or higher approximation noise.
| Workload | Recommended API | Expected Error vs FP64 | Expected Wall-Clock Gain |
|---|---|---|---|
Dense float64 scientific computing ( |
fast_matmul_fp64(A, B) |
5.9e-15 (15-digit IEEE) |
12Γβ13.7Γ vs CPU AccelerateFP64 |
Strict IEEE-754 float32 ( |
fast_matmul(..., mode="strict_fp32") |
3.8e-7 |
+26% to +70% vs strict-FP32 NAX |
Relaxed float32 dense GEMMs ( |
fast_matmul(..., mode="no_quant") |
2.9e-4(vs MLX M5 7.7e-4) |
+29% to +72% vs MLX fp32 |
Large fp16/ bf16 GEMMs ( |
fast_matmul(..., mode="no_quant") |
8.4e-4 (fp16 ) /1.8e-3 (bf16 ) |
+18% to +49% vs MLXfp16 /bf16 |
LLM training (>= 1Bβ8B, bf16) |
tachyonize(model, stochastic=True) |
~1.2e-2 (unbiased, zero drift) |
+10% to +20.5% end-to-end step (+41% linear ops on 8B) |
Single-token decode (M = 1) or small matrices (< 1024) |
Native mx.matmul |
β | No gain (auto-falls back cleanly) |
Hardware & MLX Pin: Tested on Apple M5 Max (applegpu_g17s, 128 GB unified memory, macOS 27.2.1) with MLX 0.32.4 ( pyproject.toml pins mlx>=0.32.0,<0.34.0). On M1βM4 Macs, has_metal4_nax() returns False and fast_matmul falls back cleanly to mx.matmul.
git clone https://github.com/davedx/mlx-tachyon.git
cd mlx-tachyon
pip install -e .
You don't need to memorize algorithm names β pass max_rel_err=... and return_info=True to see the exact path taken, padding applied, expected error range, and physical-ops ratio:
import mlx.core as mx
from mlx_tachyon import fast_matmul, fast_matmul_fp64
A = mx.random.normal((4096, 4096)).astype(mx.float32)
B = mx.random.normal((4096, 4096)).astype(mx.float32)
C, info = fast_matmul(A, B, layout="NT", max_rel_err=1e-3, return_info=True)
print(info)
A_bf = A.astype(mx.bfloat16)
B_bf = B.astype(mx.bfloat16)
C_train = fast_matmul(A_bf, B_bf, layout="NT", mode="fast_train", stochastic=True, seed=42)
import numpy as np
C64, info64 = fast_matmul_fp64(np.random.randn(2048, 2048), np.random.randn(2048, 2048), return_info=True)
python
import mlx.nn as nn
from mlx_tachyon import tachyonize
report = tachyonize(model, min_features=1024, skip_names=("lm_head", "embed"), stochastic=True, seed=42)
print("Swapped layers:", len(report["replaced"]))
All single-GEMM benchmarks below were measured on an idle Apple M5 Max (applegpu_g17s), macOS 27.2.1, MLX 0.32.4. We report both Effective TFLOP/s (Actual Physical Hardware Ops/s (actual_hw_ops_ratio Γ Effective) alongside measured relative L2 error vs FP64.
Before comparing float32 paths, we empirically probed what MLX 0.32.4 mx.matmul(float32) actually computes on applegpu_g17s using x = 1 + 0.75 Β· 2β»ΒΉβ° = 1.0007324219:
mx.matmul(float32)returned1.0000000000β confirming MLX defaults to Metal 4'srelaxed_precision=trueNAX path on M5, whichtruncatesfloat32inputs toward zero to a 10-bit TF32-like mantissa (rel_L2 = 7.68e-4vsFP64).- IEEE
float16round-to-nearest-even (EqFP16-NAX) returned1.0009765625(rel_L2 = 2.93e-4vsFP64, 2.6Γ lower error than truncate-to-zero TF32). - Strict IEEE-754
float32NAX (relaxed_precision=false) runs at11.2β14.8 TFLOP/s on M5 Max. Below we compare againstboth baselines explicitly.
| Mode / Path | Shape & Dtype | Baseline (vs) | Effective TFLOP/s | Actual HW Ops/s | Wall-Clock Gain | Measured rel_L2 vs FP64 |
|---|---|---|---|---|---|---|
fast_matmul_fp64 ( CRT S=14 + Smirnov ) |
9216Β³`` fp64 |
CPU Accelerate FP64 (0.49 TF ) |
6.79 TF | 70.4 TOPS (INT8) | 13.73Γ | 5.91e-15(15-digit IEEE) |
mode="strict_fp32" ( CRT S=7 + Smirnov ) |
9216Β³`` fp32 |
NAX strict fp32 (11.24 TF ) |
14.16 TF | 73.4 TOPS (INT8) | +26.0%(up to +46%) | 3.82e-7(strict IEEE) |
mode="high_prec" ( Ozaki-2 2-slice) |
4096Β³`` fp32 |
MLX relaxed fp32 (40.3 TF ) |
~48 TF | ~144 TOPS (INT8) | ~+19% | 8.23e-5(9.3Γ < MLX M5) |
mode="no_quant" ( EqFP16-NAX ) |
4096Γ4096Γ14336`` fp32 |
MLX relaxed fp32 (34.6 TF ) |
48.83 TF | 48.83 TF (FP16) | +41.3% | 2.93e-4(2.6Γ < MLX M5) |
mode="no_quant" ( Smirnov336 ,40/54 ) |
15360Β³`` bf16 |
MLX bf16 (52.16 TF ) |
68.99 TF | 51.10 TF (FP16) | +32.3% | 1.83e-3(MLX bf16: 1.66e-3) |
mode="no_quant" ( Smirnov336 ,40/54 ) |
18432Β³`` fp16 |
MLX fp16 (46.16 TF ) |
68.86 TF | 51.01 TF (FP16) | +49.2% | 8.35e-4(MLX fp16: 2.07e-4) |
Experimental:Kronecker-280 (280/432 ) |
24576Β³`` fp16 |
MLX fp16 (42.37 TF ) |
77.00 TF | 49.91 TF (FP16) | +81.7% | 1.68e-3 |
| Mode / Path | Workload & Dtype | Baseline (MLX bf16 ) |
Effective TFLOP/s | Actual HW Ops/s | Wall-Clock Gain | Measured rel_L2 / Cosine |
|---|---|---|---|---|---|---|
mode="fast_train" ( DB-RHT-INT8-NAX ) |
11-workload geomean ( NT/NN/TN )bf16 |
40β59 TF | 75β110.5 TF | 75β110.5 TOPS | +38% to +43% | 1.19e-2 ( cos=0.99993 ) |
mode="fast_train" (21-GEMM 8B layer) |
Full Llama-3-8B fwd+bwd linear ops bf16 |
110.72 ms |
68.24 TF | 68.24 TOPS | +41.0% (1.41Γ) | 1.19e-2 ( cos=0.99993 ) |
Experimental:mode="experimental_ultra" |
18432Β³`` bf16 (Smirnov-40 INT8) |
48.50 TF |
129.09 TF | 95.62 TOPS | +166.2% (2.66Γ) | 3.09e-2 ( cos=0.99952 ) |
Experimental:Kronecker-280 INT8 |
24576Β³`` bf16 (280/432 INT8) |
42.26 TF |
131.50 TF | 85.23 TOPS | +211.2% (3.11Γ) | 6.19e-2 ( cos=0.99809 ) |
Full Qwen3-style LLM Training Step (examples/bench_llm_e2e_step.py, via tachyonize(model)): Includes RMSNorm, QK-Norm, RoPE, fused causal SDPA, SwiGLU MLP, cross-entropy loss, full backward pass, andfp32 AdamW update: #
760m-wide (2L,d=1536, ffn=4096, M=4096 ):43,395 β 45,355 tok/s (+4.5% end-to-end; higher on full 24L models where embeddings/LM head amortize). #
1.3b-wide (2L,d=2048, ffn=5632, M=4096 ):29,098 β 32,010 tok/s (+10.0% end-to-end). #
llama3-8b (1L,d=4096, ffn=14336, M=4096 ):13,172 β 15,878 tok/s (+20.5% end-to-end, even without shared-input RHT reuse). #
3-Seed Convergence & Stability (120 AdamW steps, fp32 master weights + bf16 compute): Native1.8621 Β± 0.3305 vs Tachyon1.8611 Β± 0.3092 (mean gap -0.0010 ), with gradient-norm mean/max matching to within0.17% (2.339/7.099 vs2.341/7.097 ). #
Real FP64 Linear Solver (examples/fp64_iterative_refinement.py): Mixed-precision FP32-LU + GPU fast_matmul_fp64 iterative refinement on dense$4096 \times 4096$ systems converges to full machine precision (backward_err = 1.32e-16 , rel_err = 1.05e-14 ; GPU residual GEMM beats CPU Accelerate FP64 above$N = 4096$ and reaches12β14Γ at$6144^3$ β$9216^3$).
See BENCHMARKING.md for timing methodology & caveats, VERIFICATION.md for 5-seed 95% CI error bars & raw spot checks, and RESEARCH_REPORT.md for full derivations.
mlx-tachyon/
βββ mlx_tachyon/ # Installable Python package (v0.3.0)
β βββ __init__.py # Public API: fast_matmul, fast_matmul_fp64, FastLinear, tachyonize
β βββ core.py # DispatchInfo, max_rel_err contracts, 3D/batched & safety guards
β βββ hw.py # Runtime M5/Metal 4 NAX detection & explicit int32/extent guards
β βββ kernels.py # Deterministic cached .metal template
β βββ smirnov336.py # Auto-compiler for Smirnov <3,3,6; 40>
β βββ modular_crt.py # Finite-Ring Modular Sub-Cubic CRT-RNS-NAX (strict_fp32 & fp64)
β βββ kronecker280.py # Experimental Register-Fused Kronecker <6,6,12; 280>
β βββ metal/ # Readable, diffable .metal headers & kernel templates
β βββ third_party/ # Upstream CeCILL-B Smirnov <3,3,6; 40> program & license
βββ tests/ # 25 unit & property-based tests + error-bar report runner
β βββ error_metrics.py # Full-matrix bias/stddev/quantiles, 95% CI & raw sampling
β βββ test_correctness.py # All modes, layouts, dtypes, gradients & adversarial inputs
β βββ test_properties.py # All remainders mod 3/6/12/24/48/64/128, contracts & guards
β βββ verify_report.py # CLI runner (`python3 -m tests.verify_report`)
βββ examples/ # Real end-to-end application benchmarks
β βββ bench_llm_e2e_step.py # Full Qwen3-style training step (tok/s) & 3-seed convergence
β βββ fp64_iterative_refinement.py# Mixed-precision FP32-LU + GPU FP64 iterative refinement solver
βββ benchmarks/ # MLX baseline & DCE-proof M5 Max silicon probes
βββ experiments/ # All 14 chronological research & prototype scripts
βββ results/ # Raw JSON benchmark & training logs
βββ BENCHMARKING.md # How performance is measured, DCE guards & caveats
βββ VERIFICATION.md # Unit test results, 95% CI error bars & raw spot checks
βββ RESEARCH_REPORT.md # Full technical report & roofline analysis
βββ NOTICE # Third-party software & academic attributions
βββ LICENSE # MIT License
βββ pyproject.toml
python3 -m tests.verify_report
python3 -m tests.verify_report --large --seeds 5
python3 examples/bench_llm_e2e_step.py
python3 examples/fp64_iterative_refinement.py
python3 benchmarks/benchmark_baseline.py
python3 benchmarks/probes/probe_dce_proof.py
See NOTICE for full software and academic attributions, including the upstream CeCILL-B V1 pure-dyadic Smirnov mlx_tachyon/third_party/FMMa_3_3_6.m) by Jean-Guillaume Dumas, ClΓ©ment Pernet, and Alexandre Sedoglavic (jgdumas/Fast-Matrix-Multiplication, based on A. V. Smirnov 2013), Karstadt & Schwartz (2017/2020), Ozaki et al. (2012/2025), Fawzi et al. / AlphaTensor (2022), and Apple MLX / Metal 4.
mlx-tachyon is released under the MIT License (LICENSE); included third-party file FMMa_3_3_6.m remains under CeCILL-B V1 (Licence_CeCILL-B_V1-en.txt).