Repository navigation
perf(glm5_next): route verify rows like one-token decode - #4324
Conversation
|
looks to be faster for dflash as well (against mlx 0.32.3) Though I think it's more of an issue with the batch verify |
|
Thanks for checking this with DFlash. That fits the mechanism: DFlash verification goes through the same multi-row verify block, so it was hitting the same Agreed that the underlying cost is in the batched verify router (the TF32 block-GEMM replay). This PR sidesteps it by reusing the one-row router for each verify row, which also keeps verify routing consistent with decode. A faster batched router kernel would be a separate change. |
jundot
left a comment
There was a problem hiding this comment.
I tested this on an M5 Max with a smaller 20-layer GLM-5.3-Flash MTP model. Decode went from 30.1 to 31.4 tok/s, and the verify step was faster in every single run. It's a smaller model on a smaller chip than your M5 Ultra setup, but the improvement showed up consistently.
The change itself is small and easy to follow, and your NLL comparison made it easy to accept that the outputs change a bit. Thanks for the careful measurements and the clear write-up. After merging I'll do a small cleanup on main to remove the old verify router path that's no longer used and to trim the related comments and tests. LGTM, merging.
Drop the GLM moe_router_rows kernel and its warm-up now that verify rows use the one-row router, and fold the new GLM router and routed A8 tests into the existing modules.
Summary
GLM-5.3 MTP verification blocks (batch size 1, 2–8 rows,
n_group == 1, fused path on M5 GPUs with neural accelerators, "NAX") currently route experts withmoe_router_rows, which replays MLX's TF32 block GEMM. This PR routes each verification row with the existing one-tokenmoe_routerinstead, so every row gets the same fp32 routing arithmetic as the decode step it verifies.The block replay is the slow part of verification routing. An earlier per-layer microbenchmark (layer 10, MLX 0.32.2, a different GLM-5.3 checkpoint) measured about 99/108/108 µs per MoE layer at 2/3/4 rows for the block replay versus 11/23/23 µs row by row. End to end on an M5 Ultra this raises decode throughput by +15.1% on a 28.9K-token prompt and +20.2% on short prompts.
Trade-off: one-row fp32 routing is not bit-identical to the TF32 block GEMM, so outputs can change. On real activations the router logits differed by at most 4.8e-6 and no top-8 expert set changed in 210 rows. There is no new environment switch; unsupported configurations keep the existing fallback, and one-token decode is unchanged.
After this change
moe_router_rowsis no longer called by production, and the one-time eager warm-up in_check_verify_routerthat existed for its first-use check is redundant. I left both in place to keep the diff minimal and am happy to remove them here or in a follow-up.Validation
Setup: Mac Studio M5 Ultra (80-core GPU, 256 GB), macOS 27.0, Python 3.12, official MLX/MLX-metal 0.32.3 with the GLM native extensions rebuilt; unmodified GLM-5.3-Flash 4-bit MTP checkpoint; upstream defaults in both arms (context-copy on, stock TF32, adaptive MTP with four draft tokens), temperature 0. Measurements used this patch applied to
7f3a87f0. This branch (9ebe097f) is the same patch (identical patch-id) rebased onto79f4488e; the four commits in between do not touch the changed files or GLM/MTP code, and the GPU measurements were not rerun after the rebase.Throughput
Three alternating
main/PR pairs per workload. Each arm ran in a freshly started server with a one-token warm-up excluded. Values are the median of the three suite medians; decode throughput is completion tokens divided by the time after the first streamed token.mainEvery pair improved (long +24.8/+13.7/+15.1%, short +17.2/+20.2/+20.9%). Per-request TTFT medians: 12.60→12.62 s (long), 0.167→0.163 s (short). Long prompts carried a per-round nonce so rounds did not share a prefix; short prompts were cold in round 1 and reused all but 2 prompt tokens from each arm's own cache in rounds 2–3. The first short-prompt attempt was interrupted by unrelated GPU load, so all three short pairs were rerun with fresh caches. Memory was not separately instrumented. Greedy text differed in 27/27 pairs, so this benchmark makes no output-identity claim.
Routing numerics
These compare identical inputs across dispatches; they do not make whole GLM forwards row-exact.
Quality (NLL, measured earlier on MLX 0.32.2)
Same 4-bit checkpoint, 240 fresh 512-token held-out windows, 122,640 scored targets per arm, window-paired bootstrap (20,000 resamples). STEP1 is non-speculative one-token scoring; STEP4 is teacher-forced scoring through 4-token verification blocks.
main) − STEP1Against the existing block path, a predeclared +0.15% non-inferiority margin was not met. Against non-speculative decoding, no difference was detected for this PR, while the block path did differ. This is not a proof of equivalence, and NLL was not remeasured on MLX 0.32.3.
Tests
New
tests/test_glm53_verify_router.py(10 cases): row-wise equality with one-token routing at 2/3/4/8 rows, expert selection, ineligible blocks (batch size 2, 9 rows, grouped routing), disabled fusion and declined-kernel fallback.On the same machine and environment (
7f3a87f0→ this patch):tests/test_glm*.py,tests/test_mlx_vlm_glm5_next_compat.py,tests/test_mlx_lm_mtp_patch.py): 850 → 860 passed, 7 skipped each.python -m pytest): 17,313 → 17,323 passed, 298 skipped each. Both runs have the same 12 pre-existing failures (test_dspark_attention_matches_stock_m1_fallback, a native-vs-stock attention comparison on this machine) and no new failures.Reproduction
Start one server per arm (
mainand this branch) with the same model and settings, then run each suite against it, alternating arm order per round (main/PR, PR/main,main/PR):inputs.jsonholds three long prompts per round ({"long": {"0": {...}, "1": {...}, "2": {...}}}) and six short prompts ({"short": {...}}). I can share the exact input file on request.replay_benchmark.pyChecklist