|
| 1 | +# M6 de-risk — `mlx::core::compile` microbenchmark |
| 2 | + |
| 3 | +Phase-1 measurement for PLAN milestone M6. The PLAN gates the whole |
| 4 | +milestone on a ≥20% win from wrapping `mlx::core::compile`: |
| 5 | + |
| 6 | +> Equivalence tests rerun with `mlx_compile: true`; benchmark speedup on |
| 7 | +> a Qwen3 forward pass. **If <20% win, drop.** |
| 8 | +> |
| 9 | +> — `PLAN.md:186-189` |
| 10 | +
|
| 11 | +Rather than pay the full Backend/Compiler integration cost to find out, |
| 12 | +we answer the question in pure C++ against the vendored MLX 0.25.1 on the |
| 13 | +same Apple Silicon target Emily runs on. If compile doesn't help a |
| 14 | +transformer block in raw C++, it can't help under BEAM. |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +- Binary: `bench/native/compile_microbench.cpp` |
| 19 | +- Harness: `mix bench.native` (Mix task that invokes the `bench-native` |
| 20 | + target in the root `Makefile` with the same env elixir_make sets) |
| 21 | +- MLX: 0.25.1 (cocoa-xu prebuilt) |
| 22 | +- Host: M-series Mac (Metal GPU) |
| 23 | +- Each benchmark runs 50-iteration warmup + 1000 measured iterations |
| 24 | + (500 for seq=512), reporting min/median/p95 wall-time per iteration. |
| 25 | +- Both variants call `mx::eval(out); mx::synchronize()` at the end of |
| 26 | + every iteration so compile vs. uncompiled are compared apples-to-apples. |
| 27 | + |
| 28 | +## Results |
| 29 | + |
| 30 | +### Sanity: 8-op elementwise chain (1M elements) |
| 31 | + |
| 32 | +Validates the harness: a pure elementwise workload is exactly what |
| 33 | +`mx::compile` is designed to fuse. |
| 34 | + |
| 35 | +| Device | Variant | min (ms) | median (ms) | p95 (ms) | |
| 36 | +|--------|------------|---------:|------------:|---------:| |
| 37 | +| GPU | uncompiled | 1.656 | 1.746 | 2.016 | |
| 38 | +| GPU | compiled | 0.552 | 0.628 | 0.727 | |
| 39 | +| CPU | uncompiled | 1.423 | 1.463 | 1.625 | |
| 40 | +| CPU | compiled | 0.986 | 0.997 | 1.036 | |
| 41 | + |
| 42 | +**GPU speedup: 2.78× median** (fusion collapses 8 kernel launches into 1). |
| 43 | +**CPU speedup: 1.47× median**. Harness verified. |
| 44 | + |
| 45 | +### Transformer block — Qwen3-0.6B-shaped (seq=128) |
| 46 | + |
| 47 | +RMSNorm → Q/K/V proj → SDPA (matmul, scale, softmax, matmul) → output |
| 48 | +proj → residual → RMSNorm → SwiGLU FFN → residual. hidden=1024, |
| 49 | +heads=16, head_dim=64, intermediate=2816, batch=1. |
| 50 | + |
| 51 | +| Device | Variant | min (ms) | median (ms) | p95 (ms) | |
| 52 | +|--------|------------|---------:|------------:|---------:| |
| 53 | +| GPU | uncompiled | 2.588 | 3.072 | 3.964 | |
| 54 | +| GPU | compiled | 2.624 | 2.943 | 3.536 | |
| 55 | +| CPU | uncompiled | 6.835 | 7.151 | 8.109 | |
| 56 | +| CPU | compiled | 7.792 | 8.082 | 8.810 | |
| 57 | + |
| 58 | +**GPU speedup: 1.04× median — FAILS 1.20× gate.** |
| 59 | +**CPU speedup: 0.88× median — compile is slower on CPU.** |
| 60 | + |
| 61 | +### Transformer block — longer seq (seq=512) |
| 62 | + |
| 63 | +Tests whether scaling attention (which grows O(seq²)) shifts the fusion |
| 64 | +ratio. It does not. |
| 65 | + |
| 66 | +| Device | Variant | min (ms) | median (ms) | p95 (ms) | |
| 67 | +|--------|------------|---------:|------------:|---------:| |
| 68 | +| GPU | uncompiled | 10.804 | 11.463 | 12.068 | |
| 69 | +| GPU | compiled | 10.206 | 10.758 | 11.240 | |
| 70 | +| CPU | uncompiled | 21.067 | 21.744 | 22.524 | |
| 71 | +| CPU | compiled | 25.750 | 26.544 | 27.340 | |
| 72 | + |
| 73 | +**GPU speedup: 1.07× median — FAILS 1.20× gate.** |
| 74 | +**CPU speedup: 0.82× median.** |
| 75 | + |
| 76 | +## Interpretation |
| 77 | + |
| 78 | +1. The harness is correct: a pure-elementwise sanity workload yields |
| 79 | + the expected 2-3× compile win. |
| 80 | +2. A transformer block is matmul-dominated. MLX's `mx::compile` fuses |
| 81 | + elementwise chains but does **not** fuse matmul kernels with their |
| 82 | + surrounding elementwise ops. The fusion surface (RMSNorm chains, |
| 83 | + softmax neighbourhood, SwiGLU's silu×up) is a small fraction of |
| 84 | + block runtime, bounding the whole-block speedup to single-digit |
| 85 | + percent on GPU. |
| 86 | +3. On CPU, compile is a **regression**. Tape-replay overhead exceeds |
| 87 | + fusion gains for workloads that aren't Metal-kernel-launch-bound. |
| 88 | +4. Scaling sequence length (128 → 512) does not materially change the |
| 89 | + ratio. This isn't a "small workload" problem; it's a workload-shape |
| 90 | + problem. |
| 91 | + |
| 92 | +## Decision |
| 93 | + |
| 94 | +**Drop M6.** The Phase-1 gate is not met and the measurement explains |
| 95 | +why in a way that Phase-2/3 BEAM integration cannot change: the |
| 96 | +BEAM-integrated compile path cannot outperform its C++ ceiling, and |
| 97 | +that ceiling is 1.04–1.10× on the target workload (transformer |
| 98 | +inference). |
| 99 | + |
| 100 | +The microbench source and harness remain in `bench/native/` so this |
| 101 | +result can be re-measured against future MLX releases — if MLX adds |
| 102 | +matmul-adjacent fusion (e.g. bias-fused matmul or attention fusion |
| 103 | +outside `fast::scaled_dot_product_attention`), M6 becomes worth |
| 104 | +revisiting. |
| 105 | + |
| 106 | +## Reproduce |
| 107 | + |
| 108 | +```bash |
| 109 | +mix bench.native # default: warmup 50, iters 1000, seq 128 |
| 110 | +mix bench.native -- --seq 512 --iters 500 |
| 111 | +mix bench.native -- --warmup 20 --iters 200 # quick smoke run |
| 112 | +``` |
0 commit comments