Skip to content

fix: wire MLX memory in serve, so decode does not stall after a long prefill - #30

Closed
ivanfioravanti wants to merge 1 commit into
ashhart:mainfrom
ivanfioravanti:fix/wired-limit-decode-stall
Closed

ivanfioravanti wants to merge 1 commit into
ashhart:mainfrom
ivanfioravanti:fix/wired-limit-decode-stall

Conversation

@ivanfioravanti

@ivanfioravanti ivanfioravanti commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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 ms once 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_rows and 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):

  • Restoring 0.3.4's exact prefill on top of 0.3.4.1 removes the stall.
  • Disabling the new SparseAttention split, the GC, and mx.clear_cache() after prefill leaves it in place.
  • A throwaway fused forward after each prefill chunk removes it, and so does setting the wired limit.

Change

tensorfold serve sets MLX's wired limit to max_recommended_working_set_size, as mlx_lm does. It is one line in cmd_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:

context 0.3.4 0.3.4.1 this PR
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
128k - - 74
248k - - 94

The slow round warnings 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

…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
@Moutonc

Moutonc commented Sep 27, 2026

Copy link
Copy Markdown

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
change is most sensitive to, since max_recommended_working_set_size scales with installed RAM and
your numbers are from a 512 GB machine.

Short version: confirmed, and the bug is much worse at 256 GB than at 512 GB. v0.3.4.1 drops to
2.6 tok/s at an 8K context here (113 s for a 256-token reply). This PR restores it to 108.1
tok/s
and is also the fastest cold prefill of the three builds.

Setup. macOS 27.0, Python 3.12.12, MLX 0.32.2, mlx-lm 0.31.3.
Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP @ dadefa8066.
tensorfold serve --context 65536 --no-thinking --snapshot-dir none. Arms are v0.3.4, v0.3.4.1
and this PR's head, each a fresh server, same prompt sequence, medians as noted.

Decode after a cold prefill, tok/s (256-token replies, thinking off, synthetic filler prompt):

context v0.3.4 v0.3.4.1 this PR
0 80.5 33.2 102.7
8K 104.5 2.6 108.1
16K 102.2 4.5 105.0
32K 101.0 8.3 104.7

Cold TTFT, s:

context v0.3.4 v0.3.4.1 this PR
8K 26.62 13.92 6.87
16K 28.97 15.22 9.84
32K 55.47 29.17 20.25

Three things beyond a plain confirmation:

  1. The severity really is RAM-scaled. You measured ~1.5–2x on 512 GB; at 256 GB with a 105 GB
    model the same workload is up to 40x down. That fits the mechanism exactly — less headroom,
    more eviction.
  2. At 16K, v0.3.4.1 does not recover on a warm repeat (3.7 tok/s on a second run of the same
    prompt), so it is not only the first round after a prefill once the context is large enough.
  3. This PR also speeds up prefill, which the description doesn't claim: ~1,165 tok/s at 8K
    against ~300 for v0.3.4 and ~690 for v0.3.4.1.

Exactness was re-checked per arm (greedy, drafted vs "draft": false, byte-compare): identical on
every arm
, including the 2.6 tok/s one. Only speed moved.


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 ~/.cache/tensorfold/prefix-snapshots in
place. Because snapshots persist across restarts, each arm started warmer than the last — @8k cold
TTFT went 27.1 s → 11.6 s → 2.6 s across the three arms — and the PR arm never did enough prefill
to trigger the bug it was meant to test.
It scored a healthy 106.9 tok/s for entirely the wrong
reason. --snapshot-dir none fixes it; every number above is from the clean pass. Might be worth a
line in the recipes for anyone comparing builds.

Separately: tools/bench_openai.py can't run against a server at default settings. Its
fibonacci-raw prompt is kind: "completion", and with --thinking on (the default) the tokens go
to reasoning_content while text stays empty, so first is never set and it dies with
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'. @GDACONSULT noted this
against 0.3.4.1 in #27; it reproduces on v0.3.4 too, so it isn't a 0.3.4.1 regression.
--no-thinking works around it. Happy to open that as its own issue if useful.

Also happy to share the long-context script — bench_openai.py has only the two short prompts, and
this regression only shows past ~8K, so there's currently no harness in-repo that would have caught
it. It's ~60 lines, synthetic prompt and seeded filler, talks to any OpenAI-compatible endpoint.

Caveats: n=1 per cell (2 runs; the short-prompt rows are medians of 5 seeds). The filler uses one
fixed seed, so the 16K prompt's first 8K equals the 8K prompt — within an arm run1 is cold and run2
is cached, and arm-vs-arm is like-for-like, but absolute "cold prefill at 16K" isn't. Arms ran in
ascending order, so this PR measured on the warmest machine, which if anything understates it.

@ashhart

ashhart commented Sep 27, 2026

Copy link
Copy Markdown
Owner

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.

@ashhart ashhart closed this Sep 27, 2026
drowzeys added a commit to drowzeys/TensorFold that referenced this pull request Oct 5, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants