ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

GLM 5.3 Metal: multi-minute silent stalls mid-decode in long tool sessions (chunks otherwise ~27 t/s)

Open jlkivey opened this issue 3 months ago • 5 comments

I've been running GLM 5.3 Flash Q2 (your quant) as the backend for a local coding agent on a MacBook Pro M5 Max 128GB, ds4 built from ab06d19. Server:

./ds4-server -m gguf/GLM-5.3-Flash-Q2.gguf \
  --vision gguf/GLM-5.3-Flash-Vision-Encoder.gguf \
  --ctx 100000 --port 8092

In a long multi-turn tool-calling session (OpenAI chat endpoint, agent doing tool calls, context grew to ~60k over about an hour) throughput seemed to fall off a cliff, but digging into the log it turns out decode isn't actually slow at depth. Individual chunks keep doing ~27 t/s. What actually happens is the stream stops dead for minutes at a time and then resumes at full speed. Clearest example:

23:42:58 chat ctx=59215..59265:50 gen=300 TOOLS THINKING decoding chunk=27.81 t/s avg=24.12 t/s 12.440s
23:46:37 chat ctx=59265..59315:50 gen=350 TOOLS THINKING decoding chunk=0.23 t/s avg=1.51 t/s 232.236s
23:46:39 chat ctx=59315..59365:50 gen=400 TOOLS THINKING decoding chunk=27.12 t/s avg=1.71 t/s 234.080s

One 50-token chunk took 3m39s and nothing at all was logged in between, then it's straight back to 27 t/s. I count 44 chunks under 1 t/s across the session, so request-level averages crater to ~4 t/s even though the model is fine between stalls.

Single-shot requests never do this at any size I tried. Quick sweep, fresh request each time with a big filler document, temp 0, 80 max tokens:

prompt decode
1k 33.2 t/s
8k 28.8
16k 27.7
32k 26.2
48k 27.4
64k 27.4

so context depth by itself looks innocent, and a cold 67k prefill went through at ~46 t/s overall too. A second agent session with --mtp --ctx 50000 (the settings from your README examples) also showed stalls, though MTP acceptance variance makes that log harder to read.

One thing I have to own up to before you burn time on this: the whole session overlapped with a large download writing to the same internal SSD (I was pulling your Q4_K at 10-20MB/s, ironically). So if these stalls are page cache or IO related, that's plausibly the trigger — though wired memory looked stable around 100GB the whole time and swap stayed flat under 1GB, so the text weights at least should have been pinned. I haven't reproduced with a quiet disk yet; happy to do that run if it helps, or send full server logs.

DeepSeek V4 Flash agent sessions on this same machine (with DSpark) never showed anything like this, but I don't think I had heavy background IO going during those either.

Great work on the GLM support btw, single-shot performance on this thing is excellent, and Q4_K over SSD streaming loading in 5 seconds is wild.

jlkivey avatar Sep 01 '26 13:09 jlkivey

This is very likely related to #783. ds4 buffers the non-thinking response and only returns tokens after the entire response has been fully generated. Even though the total time remains the same, this prevents you from reading an in-progress response.

Most of the models that ds4 supports can output in realtime at a pace that is faster than most people can read, but with the current buffering strategy now there's often a multi-minute delay for long responses where the output looks blank and the session looks completely frozen.

links486 avatar Sep 01 '26 17:09 links486

Thanks, that fits better than my SSD theory. Two extra data points from my logs that support the streaming/buffering angle:

  1. Every stall I recorded was on a streaming request from the agent session. My control sweep (the flat 27 t/s table in the report above) used plain non-streaming completions, up to 64k prompts, and never stalled once. If it were disk or memory I'd expect both paths to suffer; only streaming did.

  2. It's not always the whole response being held either. In the 3m39s example the server was logging decode chunks at ~27 t/s every couple of seconds before and after the gap, within the same response, and then one single 50-token chunk carries the entire 219s (chunk=0.23 t/s, nothing logged in between). So at least on this session it looks like the generation loop itself blocks mid-response, maybe on a write that the buffering path is holding, rather than tokens being generated on time and delivered late.

For what it's worth these were all TOOLS + THINKING requests, which matches the #783 conditions, and I see PR #787 (streaming past a stray ) is still open. Happy to run that branch against the same agent workload and report back if useful, my setup reproduces this within an hour or so of session time.

jlkivey avatar Sep 01 '26 18:09 jlkivey

I see something quite similar with ds 4 flash. Sample from my logs:

0910 15:49:51 ds4-server: chat ctx=119395..119445:50 gen=50 TOOLS DSML_START decoding chunk=29.53 t/s avg=29.53 t/s 1.693s
0910 15:56:20 ds4-server: chat ctx=119445..119494:49 gen=99 TOOLS DSML_START DSML_END decoding chunk=0.13 t/s avg=0.25 t/s 390.251s
0910 15:56:20 ds4-server: tool calls ctx=119353..119395:42 n=1 raw_tool_text=1 ids=[toolu_1331ea301604ed7e8e25354d40f73a2a] names=[Read]
0910 15:56:20 ds4-server: chat ctx=119353..119395:42 gen=99 TOOLS DSML_START DSML_END finish=tool_calls 390.934s

This is quite frequent on a M3 Ultra, 256GB. It has no other busy processes running. I pulled and rebuilt ds4 a day or two ago. ds4 is overwhelmingly dominant in both read and write stats, so I don't believe another process is interfering.

pontus-lund-sequitor avatar Sep 10 '26 14:09 pontus-lund-sequitor

A data point from an M3 Ultra 256 GB (macOS 27.0), DeepSeek V4.1 Flash Q2, ds4-agent in long tool sessions. Same symptom: output stops dead for minutes mid-session, with no log activity.

Sampling the process during a stall:

  • worker thread: ds4_session_sync → ds41_graph_prefill_sweep → ds4_gpu_end_commands → -[_MTLCommandBuffer waitUntilCompleted]
  • Metal dispatch thread: blocked inside -[IOGPUMetalCommandQueue submitCommandBuffers:count:]
  • GPU utilization 0%, one CPU core busy, wired memory swinging by 40–65 GB

So the command buffer never reaches the GPU. The submit itself is stuck in the driver.

In my case both memory protections were off: DS4_METAL_NO_RESIDENCY=1 and DS4_METAL_DISABLE_QUEUE_KEEPALIVE=1 (the #1009 recipe for releasing RAM when idle). The keepalive comment in ds4_metal.m notes the driver drops and rebuilds the process's GPU mappings after ~3 s of idle, and in a tool session that happens between calls. With both variables removed, the exact session that froze (five-file read, one-minute idle, then an 8,437-line file read) completed twice, up to 219.7k context, and a one-shot 115k prompt through ds4-server completed with no stall.

Caveat: this was on macOS 27, and earlier reports here are from macOS 26 without these variables mentioned, so it may not be the only cause. But if you're seeing these stalls with either variable set, it's worth trying without them.

jjakemaness avatar Sep 18 '26 21:09 jjakemaness

Additional, related Metal-stall data point, with a narrower workaround than the one in the earlier comment. That comment had DS4_METAL_NO_RESIDENCY=1 and DS4_METAL_DISABLE_QUEUE_KEEPALIVE=1, and removing both helped. Neither was set in my baseline; my A/B changes only DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET.

  • M3 Ultra / 512 GiB, macOS 27.0 (26A428), DeepSeek V4.1 Flash Q4, 272k configured context. Compared ds4 6e4c285a92a1de11863784b748f196b5d4b88a48 and current 0aaea5a238fb41a35106a551e73c8409dfb751ac.

  • Reproducer: a short, synthetic, naming-shaped non-streaming completion, 44 input tokens, bounded by a 120 s client timeout. On the older revision with the original residency settings, this request hung for 120 s. On the current revision with those same settings, it also hung for 120 s. So updating ds4 alone did not clear this case, and streaming-response buffering cannot explain this particular non-streaming repro.

  • During the current-revision stall the sampled worker was in ds41_graph_prefill_sweep → ds4_gpu_end_commands → ds4_gpu_wait_command_buffer → waitUntilCompleted; a separate Metal submission thread was in IOGPUCommandQueueSubmitCommandBuffers / iokit_user_client_trap. This points to waiting on submission/completion, but it does not establish the exact driver defect or prove that all GPU work is blocked.

  • On the older revision, setting only DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1 let the same 44-token request complete in 6.637 s, and again in 5.326 s after 10 s idle. Restoring the original settings made the same request hang for 120 s again. This option skips queue.addResidencySet; it does not turn off requestResidency, warmup, or queue keepalive. A small independent Metal blit/compute check completed (~14 ms); a 16 KiB mmap-backed/no-copy buffer with requestResidency plus addResidencySet also completed (~11.7 ms), as did the detached variant (~12.3 ms). Those small tests do not reproduce ds4's full workload or identify the underlying cause.

  • On the current revision, with only DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1, the same 44-input-token synthetic request returned valid JSON (15 output tokens) in 6.309 s; after 60 s idle it returned in 4.875 s (both with 0 cached tokens). An additional synthetic 862-input-token retrieval returned all three expected checkpoint values in 5.273 s (29 output tokens). These are bounded short-request results, not a demonstration of reliability at 272k context or in long tool sessions.

Is there a preferred way to investigate queue-residency-set submission under the larger ds4 workload, or a safer workaround than disabling the queue residency set while retaining explicit residency and keepalive? In particular, what correctness or memory-pressure risks should be watched when using this flag?

shawnkoh avatar Sep 24 '26 10:09 shawnkoh