ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

GLM-5.3-Flash Metal: prefill fails for prompts > 4096 tokens (compact indexed path) — HTTP 500 'metal GLM-5.3 prefill failed at token 0'

Open aj-img opened this issue 3 months ago • 8 comments

Summary

On the glm-5.3-flash branch (a60a2a0), GLM 5.3 Flash on Metal fails to prefill any prompt longer than 4096 tokens. Short prompts work fine. This breaks any long-context use (e.g. 50K-token prompts) on the very hardware the branch advertises (128GB M-series).

Environment

  • Machine: MacBook Pro M5 Max, 128 GB unified memory, macOS 27
  • Backend: Metal
  • Branch: glm-5.3-flash @ a60a2a0 ("Add GLM 5.3 Flash inference")
  • Model: antirez/glm-5.3-flash-gguf → GLM-5.3-Flash-Q2.gguf (~90 GiB, resident)

Repro

./ds4-server -m gguf/GLM-5.3-Flash-Q2.gguf --metal --ctx 1000000 --port 4005

Then POST a chat completion with a prompt of N tokens (N varied below):

curl -s -X POST http://127.0.0.1:4005/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"Say OK. <N tokens>"}],"max_tokens":5}'

Results

Prompt tokens HTTP Body
3000 200 ok
4096 200 ok
5000 500 {"error":{"message":"metal GLM-5.3 prefill failed at token 0","type":"invalid_request_error"}}
8000+ 500 same

Fails in ~2s regardless of size — it's an immediate path failure, not an OOM/timeout.

Server log (successful boot)

ds4: GLM session ctx=1000000 (model max=1048576); full-attention prefill/work cap=4096; compact indexed decode is used beyond the cap
ds4: GLM graph allocating full-attention KV cache: work_ctx=4096 layers=0:44 (45) f16 2.75 GiB
ds4: GLM graph allocating compact DSA cache: rows=1000000 logical_ctx=1000000 kv_layers=45 indexer_layers=11 f16 11.15 GiB

The 4096 boundary matches full-attention prefill/work cap=4096; the compact indexed prefill path (used beyond the cap) is what fails on Metal.

Expected

Prompts up to --ctx (here 1M) should prefill. A 50K-token prompt is the advertised use case on 128GB machines.

Notes

  • Also observed: at --ctx 1000000 --mtp the server OOMs Metal during decode (kIOGPUCommandBufferCallbackErrorOutOfMemory at layer 44 'FFN mHC expand'); at --ctx 512000 --mtp it works. MTP is not the primary issue here — the >4096 prefill failure happens with MTP off.
  • Related: #595/#608 are the same compact-indexed prefill signature on CUDA/ROCm for GLM-5.2.

aj-img avatar Aug 28 '26 18:08 aj-img

Reproduced on different hardware, but the numbers point at memory rather than prompt length.

Machine: MacBook Pro M3 Max, 128 GB, macOS 26.4 (Darwin 25.4.0), Metal, branch glm-5.3-flash @ a60a2a0, GLM-5.3-Flash-Q2.gguf.

With your exact server flags (--metal --ctx 1000000) the failure starts earlier than 4096 for me — 2500 words already fails, 2000 passes. That is the 2048-token prefill chunk boundary, not 4096.

The server log has the real error:

ds4: Metal command batch failed: Insufficient Memory (00000008:kIOGPUCommandBufferCallbackErrorOutOfMemory)
ds4: GLM layer-major prefill failed at layer 44 stage 'FFN mHC expand' (pos 0, tokens 2048)

Same prompt, same chunk size, only --ctx changed:

--ctx planned 8000-word prompt
1000000 KV 13.90 GiB + buffers 4.25 + model 89.87 = 108.01 GiB fails
65536 KV 3.48 GiB + buffers 4.12 + model 89.87 = 97.47 GiB ok

sysctl iogpu.wired_limit_mb is 0 here, so the Metal working set is the macOS default, roughly 107.5 GiB on a 128 GB machine. At --ctx 1000000 the planned 108.01 GiB is already above that before any transient buffer, and the first full prefill chunk pushes it over. Short prompts survive because their transient allocation is small.

So the prompt-length threshold looks like a symptom: bigger chunk, bigger transient, earlier OOM. The differing thresholds (yours at 4096 on M5 Max / macOS 27, mine at 2048) fit that reading.

This also looks like the same budget model as #889 — there the guard refuses with a fixed 110 GiB per-rank ceiling on a 512 GB machine, here it accepts a plan that exceeds the actual working-set limit.

CLI is unaffected at sane contexts: ./ds4 --ctx 32768 prefills a 6023-token prompt at 194 t/s.

eauchs avatar Aug 28 '26 19:08 eauchs

Confirmed it is the Metal working-set limit, with a controlled experiment: same binary, same flags, same prompts, only iogpu.wired_limit_mb changed.

sudo sysctl iogpu.wired_limit_mb=114688   # 112 GiB, was 0 (macOS default ~107.5 GiB here)

The server plan is byte-for-byte identical across both runs — 108.01 GiB planned, chunk=2048 score_rows=268 score_scratch=255.58 MiB — so neither the prefill chunk nor the score scratch is the cause.

prompt default limit 112 GiB
3000 words 500 prefill failed at token 0 200
5000 words 500 200
8000 words 500 200

Coherence at the raised limit, --ctx 1000000, 8024 prompt tokens: 8000 words of filler followed by Ignore the text above. Reply with exactly: BANANA returns BANANA, with sane reasoning content. No degeneration.

So at --ctx 1000000 the planned 108.01 GiB is already above the default working set before any transient allocation, and the first 2048-token chunk pushes it over at layer 44 'FFN mHC expand'. The guard accepts a plan the device cannot honour.

That is the mirror image of #889, where the guard refuses a plan the device could honour because of the fixed 110 GiB per-rank ceiling. Both would be covered by sizing the budget from the actual Metal recommendedMaxWorkingSetSize (and iogpu.wired_limit_mb when set) instead of a constant, or at minimum by naming the ceiling in the refusal/failure message so the sysctl workaround is discoverable.

Note the sysctl does not survive a reboot.

eauchs avatar Aug 28 '26 20:08 eauchs

Does not reproduce here. Possibly macOS-version-dependent — I'm on macOS 26.6.2, the report says macOS 27.

Environment

Matched to the report as closely as I could:

  • MacBook Pro, M5 Max, 128 GB, Metal
  • Branch glm-5.3-flash @ a60a2a0
  • antirez/glm-5.3-flash-gguf → GLM-5.3-Flash-Q2.gguf (89.9 GiB, resident, warmed)
  • --ctx 1000000
  • macOS 26.6.2 ← the only difference I can see

Boot line is the same:

GLM session ctx=1000000 (model max=1048576);
full-attention prefill/work cap=4096; compact indexed decode is used beyond the cap

Results

prompt tokens HTTP time
5,016 200 15.1 s
10,016 200 30.4 s
20,016 200 58.7 s

Linear, roughly 340 tok/s prefill, no failures. Same curl shape as the repro (max_tokens: 5, single user message padded to length).

Separately, during an agent workload on the same build I prefilled 39,903 tokens repeatedly at ~361 tok/s, also without error — so the compact indexed path appears healthy on 26.6.2 well beyond the 4096 cap.

Suggestion

If nobody else on 26.x can reproduce it, macOS 27 looks like the discriminator rather than prompt size — which would point at the Metal driver rather than the indexed prefill path itself.

Happy to run anything specific on 26.6.2 if a comparison would help narrow it.

evandhoffman avatar Aug 29 '26 18:08 evandhoffman

Before chasing the macOS version, could you post sysctl iogpu.wired_limit_mb on that machine?

Your own notes suggest it is not at the default. From evanwtf/local-llm#32:

Memory, now that #30 is applied — The ceiling is 112.00 GiB (was 107.52).

If it still reads 114688, that alone explains the non-reproduction: the planned 108.01 GiB fits under a 112 GiB ceiling and fails under the 107.52 GiB default. Same binary, same branch, same model, same --ctx — only the ceiling differs, which is exactly the pair I measured above (three prompts going from HTTP 500 to 200 with nothing changed but that sysctl).

That would make our two results agree rather than conflict, and it would put the discriminator on the Metal working set rather than on macOS 27.

Worth noting the sysctl does not survive a reboot, so it is easy to have it set from earlier work without it being top of mind — you flagged that yourself in #32.

Separately, thank you for was 107.52. I had only been able to estimate the stock ceiling at "roughly 107.5 GiB" and said so; that is now a measured number from a second machine, and it lands within 0.02 GiB. It also matches the shape of the problem: the fixed 110 GiB guard in #893 sits above the real default ceiling on 128 GiB hosts, so the guard accepts plans the device cannot honour — the mirror of the 512 GiB case that PR fixes.

If your sysctl does read 0, then macOS 27 becomes the interesting lead and I would want to know, since my measurements are on 26.4.

eauchs avatar Aug 29 '26 18:08 eauchs

You are right, and my previous comment was wrong. sysctl iogpu.wired_limit_mb on this machine:

iogpu.wired_limit_mb: 114688          # 112.00 GiB

Confirmed independently via Metal rather than the sysctl alone:

device                      : Apple M5 Max
recommendedMaxWorkingSetSize: 112.00 GiB

So the ceiling was raised here from earlier work and I did not check it before reporting a non-reproduction. Your arithmetic holds exactly: 108.01 GiB planned fits under 112 GiB and does not fit under the ~107.52 GiB default. Our results agree; they never conflicted.

Two further corrections to what I posted:

macOS is ruled out by your own data, not just weakened. I suggested macOS 27 as the discriminator. You reproduce on 26.4 and I do not reproduce on 26.6.2 — an older OS reproduces and a newer one does not, which the version hypothesis cannot explain. The working set does.

I also posted into a thread where you had already run the controlled experiment, same binary and flags with only the sysctl changed, three prompts going 500 → 200. I read the issue body and not the comments before commenting. Apologies for the noise.

The one number worth keeping

The stock ceiling on this machine, measured before the sysctl was applied (evanwtf/local-llm#30), was:

recommendedMaxWorkingSetSize: 107.52 GiB     # 128 GiB M5 Max, macOS 26.x, default

That is a second machine agreeing with your ~107.5 GiB estimate to within 0.02 GiB, and it sharpens the guard argument: a fixed 110 GiB constant sits above the real default ceiling on 128 GiB hosts, so the guard accepts plans the device cannot honour — exactly the failure here, and the mirror of the 512 GiB case in #889/#893.

Sizing the budget from recommendedMaxWorkingSetSize (which already reflects iogpu.wired_limit_mb when set — 112.00 GiB above, 107.52 GiB without) would cover both directions with one source of truth. Failing that, naming the ceiling in the failure message would at least make the sysctl workaround discoverable; it took a 128 GiB machine three hours to find it independently.

Offer

I can run the reverted-sysctl confirmation on 26.6.2 if a third data point at the default ceiling is useful — it would isolate OS version from working set completely, since your 500s are on 26.4 and mine would be on 26.6.2 with the same ceiling. Say the word.

evandhoffman avatar Aug 29 '26 19:08 evandhoffman

Thanks for checking and for saying so plainly — and the macOS point is now stronger than I put it. I only said the working set explained the difference; you noted that an older OS reproduces and a newer one does not, which the version hypothesis cannot account for at all. That is your argument, not mine, and it closes the question rather than weakening it.

The number I am keeping from this is 107.52 GiB measured on stock, since it turns my "roughly 107.5" into a figure from a second machine. I have carried it over to #893, where the guard is actually being changed.

No noise from my side either — you reported a non-reproduction with the full environment and the boot line, which is what made it resolvable in one exchange.

eauchs avatar Aug 29 '26 19:08 eauchs

b0c31af fixes the large-host half of this — the unconditional 110 GiB cap is gone and iogpu.wired_limit_mb is now read. Thank you. The 128 GiB half is still open, and it is now one measurable step away.

The new code keeps the 110 GiB budget on 128 GiB hosts:

/* Preserve the proven 110 GiB resident-Q2 budget on 128 GiB hosts
 * without imposing that reference-machine limit on larger Macs. */

But the stock ceiling on those hosts is 107.52 GiB, measured on two machines of different chip generations:

machine stock with iogpu.wired_limit_mb = 114688
M5 Max 128 GB, macOS 26.x (@evandhoffman) 107.52 GiB 112.00 GiB
M3 Max 128 GB, macOS 26.4 (this issue) — 112.00 GiB

So a plan of 108.01 GiB passes the guard and then fails on the device, which is the failure reported at the top of this issue: OOM at layer 44 FFN mHC expand on the first 2048-token chunk, with 57 GiB nominally free. Raising the sysctl and changing nothing else turns the same three prompts from HTTP 500 into 200.

The gap is that the sysctl alone cannot answer the question. When iogpu.wired_limit_mb is 0 it tells you nothing about the real ceiling; the Metal query does, and it already folds in the override when one is set:

id<MTLDevice> d = MTLCreateSystemDefaultDevice();
d.recommendedMaxWorkingSetSize    // 107.52 GiB stock, 112.00 GiB with the sysctl raised

recommendedMaxWorkingSetSize does not appear in ds4.c, ds4_metal.m or ds4_gpu.h as of b0c31af. Bounding the budget by it would replace both the 110 GiB constant and the separate sysctl read with a single source of truth that is correct on every host size, and it needs no per-class reserve.

If a fixed budget is preferred for 128 GiB hosts, min(110 GiB, recommendedMaxWorkingSetSize) would at least stop the guard from accepting an over-budget plan. Naming the ceiling in the failure message would also make the workaround discoverable — today the only signal is an OOM deep in the graph.

Happy to test a patch on a stock 128 GB M3 Max, including with the sysctl reset to 0 for the default path.

eauchs avatar Aug 30 '26 10:08 eauchs

Opened #909 for this, building on eauchs's analysis above: the guard budgets from host RAM, so any plan that lands between the Metal working-set ceiling and the host budget gets admitted and then dies at the first prefill chunk. #909 clamps the budget at recommendedMaxWorkingSetSize minus 2 GiB, so those plans refuse at open with the ceiling and the sysctl remedy printed. A raised iogpu.wired_limit_mb still lifts the budget exactly as the 112 GiB experiment showed. Note the original Q2 ctx-1M repro no longer over-plans on the current branch tip (104.18 GiB here since the expanded full-attention KV skip), so the crash needs a plan past the ceiling to trigger now.

gilbert-barajas avatar Aug 30 '26 15:08 gilbert-barajas