philipjohnbasile commited on
Commit
9c04b0e
Β·
verified Β·
1 Parent(s): 0fe9d9b

Upload research/mlx_speed_deepdive.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. research/mlx_speed_deepdive.md +38 -0
research/mlx_speed_deepdive.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Can we code MLX ourselves for more decode speed? (June 2026 deep dive)
2
+
3
+ **Question:** the agentic coder's one weakness is decode speed (~11–14 tok/s). Can custom MLX/Metal code beat it?
4
+
5
+ ## The wall (unchanged, re-confirmed)
6
+ Decode is **memory-bandwidth-bound** β€” generation speed β‰ˆ *bytes moved per token* (every token streams the active
7
+ expert weights through the memory bus). TTFT is compute-bound (and the M5 fixes that); decode is not. Our **#33 fused
8
+ MoE dequant-matmul Metal kernel already IS the 11–14 tok/s** β€” it maxed the compute side. You cannot out-*compute*
9
+ bandwidth. "More speed" = fewer bytes/token, or faster per-byte processing on M5 hardware.
10
+
11
+ ## Lever 1 β€” NVFP4 on the M5 hardware path *(easy, no custom code, biggest, do first)*
12
+ - **We already have it** (probed: `mx.quantize(..., mode="nvfp4", group_size=16)` works on our MLX 0.31.2).
13
+ - **Round-trip fidelity:** affine-3bit mean|err| **0.159** vs **NVFP4-4bit 0.077** β€” *half the quantization damage.*
14
+ - **M5 hardware path:** the Neural Accelerators have a native NVFP4 dequant+matmul; a 35B-A3B MoE went **58β†’112 tok/s
15
+ (2Γ—)** on M5 Max with NVFP4. It's 4-bit (more bytes than our 3-bit) but the hardware path + better fidelity net a win.
16
+ - **Action:** re-quantize the 4-bit layers in **#59** as NVFP4 (group 16), keep the middle 3-bit affine; measure decode.
17
+ Zero custom code β€” just the quant mode + a `04b/24b` NVFP4 path.
18
+
19
+ ## Lever 2 β€” a Metal-4 **TensorOps** fused-MoE kernel *(the "code it ourselves" win, ~30–60%)*
20
+ - **#33's kernel predates the M5** β€” it uses plain Metal, not the **Neural Accelerators' hardware matmul** (Metal 4
21
+ TensorOps). MLX gets 30–60% on M5 by using TensorOps; a *custom* MoE kernel that does too captures that for our model.
22
+ - **How:** `mx.fast.metal_kernel` + Metal 4 **TensorOps** β€” slice the gather'd expert weights into tiles, tile-wise
23
+ matmul on the Neural Accelerators, keep data in cache (WWDC26 session 330 "Optimize custom ML operations with Metal
24
+ tensors" is the recipe). Fuse: expert-gather β†’ NVFP4-dequant β†’ matmul β†’ accumulate, in one dispatch.
25
+ - **Effort:** real Metal work (~weeks). Reference impls exist (TurboQuant-MLX fused kernels; simdgroup-MMA int4-MoE kernels).
26
+
27
+ ## Honest ceiling
28
+ Neither lever breaks the bandwidth wall β€” they reduce bytes (NVFP4) and speed per-byte (TensorOps on the Accelerators).
29
+ Realistic: **11–14 β†’ ~20–28 tok/s** from NVFP4+M5 alone; **~30+** with the custom TensorOps kernel. A genuine **2–3Γ—**,
30
+ not 100+. Speculative decode stays dead on this MoE (any multi-token verify reloads ~all experts β€” the bandwidth wall again).
31
+
32
+ ## Plan (CPU now β†’ GPU later)
33
+ 1. **NVFP4 re-quant** β€” wire `nvfp4` (group 16) into `04b`/`24b` as the 4-bit-layer format for #59; re-quant; **measure decode** (the proof).
34
+ 2. If the gain lands and we want more β†’ **write the Metal-4 TensorOps fused-MoE kernel** (the 30–60% lever); benchmark vs #33.
35
+ 3. Keep the verify-everything stack (constrained decode, compiler-steer) intact β€” none of this touches it.
36
+
37
+ *Sources: MLX custom Metal kernels (ml-explore docs 0.31.2) Β· `mx.fast.metal_kernel` Β· WWDC26 #330 Metal tensors Β·
38
+ Apple "LLMs with MLX + M5 Neural Accelerators" Β· TurboQuant-MLX fused kernels Β· MLX-vs-llama.cpp M5 benchmarks (yage.ai 2026) Β· our SPEED.md root-cause.*