
苹果 M5 神经加速器矩阵乘法提速最高达 211%
来源标题:Up to 211% faster matrix multiplication on modern Apple Silicon
研究库 mlx-tachyon 为 Apple Silicon 提供亚三次、有限环模 CRT 和 Hadamard-INT8-NAX 矩阵乘法内核,在 M5 神经加速器上配合 MLX/Metal 4 使用,最高比基线快 211%。Apple 自带 mx.matmul 已达 16 位峰值约 93%,该项目在算法层面进一步提升速度或精度。M1-M4 或 CPU 环境会检测到缺少 Metal 4 NAX 后自动回退到 mx.matmul 并发出警告。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
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 $O(N^3)$ peak (~59 out of 63.8 TFLOP/s). mlx-tachyon explores algorithmic ways to improve speed or precision on top of Apple Silicon's hardware:
Experimental modes: mode="experimental_ultra" (Direct-INT8 Smirnov-40, ~3.1% rel L2) and opt-in allow_experimental=True Kronecker $\langle 6,6,12; 280 \rangle$ (~1.7e-3 in fp16 at $\ge 18432^3$, ~6.2% in INT8 at $\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 ($\ge 6144^3$) fast_matmul_fp64(A, B)
5.9e-15 (15-digit IEEE)
12×–13.7× vs CPU Accelerate FP64
Strict IEEE-754 float32 ($\ge 6144^3$) fast_matmul(..., mode="strict_fp32") 3.8e-7
+26% to +70% vs strict-FP32 NAX
Relaxed float32 dense GEMMs ($\ge 2048^3$) fast_matmul(..., mode="no_quant")
2.9e-4 (vs MLX M5 7.7e-4)
+29% to +72% vs MLX fp32
Large fp16/bf16 GEMMs ($\ge 12288^3$, zero INT8) fast_matmul(..., mode="no_quant")
8.4e-4 (fp16) / 1.8e-3 (bf16)
+18% to +49% vs MLX fp16/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.
Install
git clone https://github.com/davedx/mlx-tachyon.git cd mlx-tachyon pip install -e .
- Accuracy-Contract Dispatch & Transparent Path Reporting
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)
Let mlx-tachyon choose the fastest path meeting your error contract
C, info = fast_matmul(A, B, layout="NT", max_rel_err=1e-3, return_info=True) print(info)
DispatchInfo(path='eq_fp16_nax', ..., expected_rel_err=(0.00025, 0.00035), actual_hw_ops_ratio=1.0, reason='ok')
Or pick a mode explicitly (supports 2D and batched 3D+ inputs, deterministic seed for SR)
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)
15-digit IEEE-754 double precision on GPU (accepts NumPy float64 arrays)
import numpy as np C64, info64 = fast_matmul_fp64(np.random.randn(2048, 2048), np.random.randn(2048, 2048), return_info=True)
- One-Line LLM Model Conversion (tachyonize)
import mlx.nn as nn from mlx_tachyon import tachyonize
Replaces eligible nn.Linear / CastLinear layers with FastLinear (preserves fp32 master weights & skips lm_head/embed)
report = tachyonize(model, min_features=1024, skip_names=("lm_head", "embed"), stochastic=True, seed=42) print("Swapped layers:", len(report["replaced"]))
Benchmarks & Technical Results (Separated by Precision Class)
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 ($2MKN / t_{\text{median}}$) and 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:
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
Table 2: Lossy INT8 Paths (Always Paired With Measured Error)
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)
Table 3: Real End-to-End Application Benchmarks
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, and fp32 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): Native 1.8621 ± 0.3305 vs Tachyon 1.8611 ± 0.3092 (mean gap -0.0010), with gradient-norm mean/max matching to within 0.17% (2.339/7.099 vs 2.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 reaches 12–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 loader
│ ├── 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
How to Verify the Results Yourself
1. Full 25-test correctness, property-based & 5-seed error-bar suite (~20s GPU)
python3 -m tests.verify_report
Include huge 6144x6144x12288 & 9216^3 dispatch tests
python3 -m tests.verify_report --large --seeds 5
2. Real end-to-end LLM training step & 3-seed stability check
python3 examples/bench_llm_e2e_step.py
3. Real FP64 iterative-refinement linear solver demo
python3 examples/fp64_iterative_refinement.py
4. Baseline & DCE-proof hardware probes
python3 benchmarks/benchmark_baseline.py python3 benchmarks/probes/probe_dce_proof.py
Attribution & License
See NOTICE for full software and academic attributions, including the upstream CeCILL-B V1 pure-dyadic Smirnov $\langle 3,3,6; 40 \rangle$ straight-line program (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).
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。
