Repository navigation
fix: wire MLX memory in serve, so decode does not stall after a long prefill - #30
ivanfioravanti wants to merge 1 commit into
Conversation
…prefill
Since 0.3.4.1 a prompt prefills through MLX's forward while decode runs the fused kernels. The buffers only
the fused path reads (stacked projections, router rows, PLE tables) sat unused through a long prefill and left
GPU residency, and the first decode round waited ~1 s to bring them back ("slow round ~1000 ms" in the log from
~16k tokens up). In a 128-token reply that one round halved the measured decode speed.
serve now sets MLX's wired limit to the recommended working set, as mlx_lm does. Flash Next, M3 Ultra, fresh
prompts, temperature 1.0 (tok/s):
context 0.3.4 0.3.4.1 this
8k 101-106 71-74 115-122
16k 109-113 60-64 105-112
32k 127-139 63-87 92-98
64k - 55-60 111-118
|
Posted by Claude (Anthropic's AI assistant) on behalf of @Moutonc. Independent verification of this PR on a Mac Studio M3 Ultra, 256 GB — the configuration this Short version: confirmed, and the bug is much worse at 256 GB than at 512 GB. v0.3.4.1 drops to Setup. macOS 27.0, Python 3.12.12, MLX 0.32.2, mlx-lm 0.31.3. Decode after a cold prefill, tok/s (256-token replies, thinking off, synthetic filler prompt):
Cold TTFT, s:
Three things beyond a plain confirmation:
Exactness was re-checked per arm (greedy, drafted vs A benchmarking note that cost us a run, in case it helps others verifying this. Our first pass restarted the server per arm but left Separately: Also happy to share the long-context script — Caveats: n=1 per cell (2 runs; the short-prompt rows are medians of 5 seeds). The filler uses one |
|
Thanks @ivanfioravanti for diagnosing the first-decode stall and tracing it to buffers losing residency during prefill. Wiring memory removes the stall but costs about 13% decode in our measurements, and #29's prefill path removes it with no decode cost, so we're closing this unmerged. Your diagnosis is credited in the 0.3.5 release notes. |
…nd the RoCE reductions roce.py takes a rail list (TF_GLM53_ROCE_HCA, else NCCL_IB_HCA, comma separated, NCCL '=' / ':port' syntax tolerated) with per-device GID indices (TF_GLM53_ROCE_GIDS; b12x takes one index, so differing GIDs fall back to the first device); TF_GLM53_RAILS=1 keeps one device. Before, a comma list became one bogus device name. tools/tp4_run.sh: RAILS=2 (default) scans each node read-only for up RoCE netdevs with an IPv4 RoCE v2 GID, orders them by subnet (NODES[r]'s first) so rail i is one subnet on every node (matched by subnet, not name: .2 has swapped enumeration before), refuses mismatched subnets, and passes NCCL_IB_HCA, the GID (or NCCL's own per-device pick when they differ) and NCCL_CROSS_NIC=0, or CROSS_NIC=1 + SUBNET_AWARE_ROUTING when ibverbs order != subnet order. RAILS=1 or HCA=... restores one device. 'tp4_run.sh rails' prints the result. tp4_roce_big.py uses the same rail list. From MiaAI-Lab GLM-5.3-Flash v1.3.3 ashhart#30 (two rails: ~1.8x prompt all-gather). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since 0.3.4.1, decode throughput at 8k+ contexts drops to roughly half of 0.3.4, and the server logs
[tensorfold] slow round ~1000 msonce a prompt passes about 16k tokens.Cause
Decode rounds are not slower. The first decode round after each long prefill stalls for about 1 s, and every later round runs at normal speed (20-30 ms). In a 128-token reply that one round halves the measured tok/s.
In 0.3.4.1 prompts prefill through MLX's forward, while decode runs the fused kernels. Some buffers are read only by the fused path:
FusedDecode's stacked projections,router_rowsand the concatenated PLE tables. During a prefill of tens of seconds nothing touches them. The server never sets an MLX wired limit, so they leave GPU residency, and the first fused round waits to bring them back. Timing each layer of that round puts ~700 ms in the MoE layers and ~280 ms around the PLE layer. 0.3.4 did not show this because its prefill ran through the same fused kernels.How it was isolated (Flash Next, M3 Ultra):
SparseAttentionsplit, the GC, andmx.clear_cache()after prefill leaves it in place.Change
tensorfold servesets MLX's wired limit tomax_recommended_working_set_size, as mlx_lm does. It is one line incmd_serve, and it covers every family whose prefill and decode use different kernels.Results
Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP, M3 Ultra 512 GB, fresh prompts (code context), temperature 1.0, 128 tokens, decode tok/s:The
slow roundwarnings are gone and prefill speed is unchanged.Not covered: the test suite was not run on this branch (no test exercises
cmd_serve), and only Flash Next was measured.🤖 Generated with Claude Code