ds4 icon indicating copy to clipboard operation
ds4 copied to clipboard

--layers maps the shard as N disjoint Metal buffers even when the range spans the whole file (~77x decode slowdown)

Open IzD680 opened this issue 3 months ago • 2 comments


Summary

When ds4-server is started with --layers, the Metal model map is split into one shared buffer per tensor span instead of a single overlapping mapping. On the same machine, with the same binary and the same model family, this costs roughly 77x in decode throughput.

The restriction appears to be a no-op in terms of what is mapped — the reported span is essentially the entire shard file — so the cost looks like pure mapping fragmentation rather than a smaller working set.

Environment

  • Apple Silicon Mac Studio, 512 GB unified memory, Metal backend
  • Two hosts, pipeline parallelism (--role coordinator / --role worker)
  • --ctx 393216, --threads 8, --prefill-chunk 4096
  • Model split into two shards by layer range

What the log shows

With --layers:

restricting metal model map to layers 0:30 (156 spans, 426.09 GiB tensor span)
...
mapped 436323.72 MiB from offset 5.06 MiB
...
156 disjoint shared buffers across 156 tensor spans

The second half of the split reports the same shape:

153 disjoint shared buffers across 153 tensor spans

The restriction restricts nothing. The shard file is 436,323.72 MiB ≈ 426.10 GiB, and the mapped tensor span is 426.09 GiB — the ~10 MiB difference matches the 5.06 MiB header offset. The engine is laying down the same bytes in 156 pieces instead of one.

Without --layers, on the same machine and the same binary, a single-process run of a full model maps as:

1 overlapping shared buffers

Measured impact

configuration mapping decode
single process, no --layers 1 overlapping shared buffers 10.01 t/s
coordinator, --layers 0:30 156 disjoint shared buffers 0.13 t/s

Same box. Same binary. ~77x.

The network is not involved

Per-token telemetry from the coordinator, repeated identically for every token:

distributed telemetry: request=2 hop=0 layers=31:60 pos=11 tokens=1
  eval=2744.908ms   downstream_wait=0.000ms   forward_send=0.000ms

downstream_wait and forward_send are exactly zero, not merely small. Wire traffic is ~0.11 MiB in / ~0.49 MiB out per decode step. The time is entirely compute:

  • worker, 30 layers: ~2,742 ms/token (σ < 7 ms over 8 tokens)
  • coordinator, 31 layers: ~4,965 ms/token (by difference from 7,707 ms/token total)

That is roughly 91–160 ms per layer per token, on hardware where the same engine runs a whole model at 33 t/s.

Both halves are equally affected, which rules out one bad host.

Not memory, not cold cache

Residency time fell from 58,183 ms to 10,000 ms across a restart (warm file cache) and the decode rate did not change at all. Paging and load time are not the cause.

Reproducibility

Reproduced on two separate dates across five runs, including a --ssd-streaming variant, which did not help:

day 1, 07:20   3 tokens / 1667.469 s  = 0.002 t/s
day 1, 08:45   8 tokens /  266.808 s  = 0.03  t/s
day 1, 08:54   7 tokens /  574.693 s  = 0.01  t/s
day 2, 03:xx   8 tokens /   61.65  s  = 0.13  t/s

The same 156 disjoint … 156 tensor spans line is present in the day-1 logs, so this is not specific to a single launch or to startup ordering (restarting with corrected startup order changed nothing).

Why --tensor-parallel is not a workaround here

--role coordinator rejects a start without --layers:

--role coordinator requires --layers

and --tensor-parallel requires the full model on both hosts rather than per-host shards, so on a sharded deployment there is no way to reach a non-fragmented mapping through the CLI.

Suggested fix

Either coalesce adjacent tensor spans when building the restricted map, or skip the restriction entirely when the requested layer range already covers the whole file — which, per the numbers above, is the common case for a per-shard file.

Minor, separate

The binary exits with rc=0 on argument-validation errors. ./ds4-server --role coordinator -m <shard> --ctx 4096 prints --role coordinator requires --layers and still returns 0, so scripts cannot detect a failed launch by exit status and must grep the log. Two probes cost nothing because validation runs before the single-instance guard and before model load — that part is good.

IzD680 avatar Aug 21 '26 09:08 IzD680

Picking this up — will look at Metal model-map restriction under --layers so adjacent tensor spans coalesce (or the restriction is skipped when the range already covers the shard), instead of one disjoint buffer per span.

paride-cmd avatar Sep 03 '26 02:09 paride-cmd

Additional data point: macOS 27 Beta 8 (26A5425a) — 156→4 buffer coalescing does NOT recover speed; no residency churn

We hit the same ~77x decode collapse on 2× Mac Studio M3 Ultra 512 GB (coordinator 0:30 / worker 31:output, DeepSeek-V4-Pro-Q4K split GGUFs, direct Thunderbolt bridge, ping 0.6 ms). We can add a data point that partly complicates the fragmentation story — and a timeline that points at an OS regression.

Timeline (same two hosts, same model files, same scripts)

Period macOS Result
Aug 28–31 27.0 Beta 2 (coordinator) / Beta 4 (worker) 10–13 t/s, matching the README's 11.47 t/s
Sep 1–3 27.0 Beta 8 (26A5425a) on both 0.14–0.19 t/s

Beta 8 (26A5425a) was released Aug 31; both machines landed on it within the Sep 1–3 window, exactly when the slowdown began.

Measurements (ctx 131072; identical at ctx 1048576)

Per-token distributed profile, steady state:

dist decode profile: span request=N layers=0:30 local≈3200ms remote≈2870ms total≈6100ms hidden=0.11MiB rc=0
remote request=N send≈0.05ms wait_result≈2850ms kind=2 payload=0.49MiB

Same shape as both reports: downstream_wait=0.000ms, forward_send=0.000ms — wire is idle.

Kernels are healthy. DS4_METAL_MOE_ONE_STAGE_PROFILE=1 on the coordinator shows path=pair_swiglu with per-layer stages of gate_up ≈ 0.5 ms / down ≈ 0.4 ms — ~1 ms per layer, ~31 ms for all 31 local layers.

GPU is ~97% idle. DS4_METAL_GPU_BUSY_PROFILE=1: ~412 ms GPU-busy accumulated over 448 command buffers while wall time is ~6.1 s/token. sample of the coordinator mid-decode: 87% of main-thread samples in dist_recv_result_alloc → dist_read_frame_header → __recvfrom, 13% in ds4_gpu_end_commands → -[_MTLCommandBuffer waitUntilCompleted]; the worker's threads are 100% parked in recvfrom/cond_wait/accept. Both sides starve waiting on command-buffer completion signaling.

Where we differ from #501 — no residency churn

vm_stat sampled repeatedly during decode on the coordinator: "Pages wired down" stable at ≈ 433 GiB (28.37M→28.40M→28.40M pages over ~12 s), Swapins/Swapouts = 0, Pageins ≈ 10 pages/3 s (≈50 KB/s). The per-token wired collapse (433↔38 GiB) from #501's update does not occur here — residency is stable, completion is just slow.

Where we differ from #845 — coalescing the map to 4 buffers does not help (on Beta 8)

We patched ds4.c (ds4_engine_open_internal, load_slice branch): when the sorted span union covers the whole shard file (covered + 64 MiB ≥ file, Metal-only gate), it now installs 4 contiguous ≤128 GiB overlapping spans (inter-span overlap = round_up(max_tensor_bytes, page) + page, preserving the every-tensor-within-one-view invariant) instead of the 156 fragmented ones. Log confirms:

ds4: layer slice spans cover the whole model file (426.09 of 426.10 GiB); mapping as 4 contiguous capped metal spans
ds4: Metal mapped mmaped model as 4 disjoint shared buffers across 4 tensor spans

Decode stayed at 0.16 t/s. So on Beta 8, buffer count is not the operative variable — consistent with a driver-level per-command-buffer completion regression rather than pure mapping fragmentation. (Note: our first attempt mapped the whole shard as uncapped ~217 GiB views via the range path and failed slice eval outright — the ≤128 GiB cap that ds4_gpu_add_model_view_range(use_default_view_cap=true) applies matters, as the code comment about maxBufferLength says.)

Ruled out (each measured, not assumed)

  • App version: Aug-29 build vs Sep-1 build, byte-for-byte behavior — identical 0.14–0.18 t/s.
  • CTX size: 1048576 vs 131072 — identical.
  • Swap/paging: Swapouts = 0, Pageins ≈ 0 during decode on both hosts.
  • Env toggles, each tested alone on both hosts: DS4_METAL_NO_RESIDENCY, DS4_METAL_ENABLE_Q4_EXPERT_TABLE (log shows preloaded PRO Q4 expert tables for layers 0:30 in 0.00s — suspiciously instant, looks like a silent no-op), DS4_METAL_ENABLE_Q4_SELECTED_EXPERT_VIEWS, DS4_METAL_DISABLE_PRE_M5_DECODE_PORTS, DS4_METAL_MATH_SAFE — none changed the rate.
  • DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1 + DS4_METAL_UNRETAINED_COMMAND_BUFFERS=1 together → immediate Caused GPU Hang Error (00000003:kIOGPUCommandBufferCallbackErrorHang); once, not reproducible in normal runs (no hangs in 3 h of system log otherwise).
  • iogpu.wired_limit_mb: 480000 on both hosts — no change.
  • Control: DeepSeek V4 Flash (MXFP4, mapped as 1 overlapping shared buffer, 145 GiB) on the same host/OS/build → 42.4 t/s decode, at or above this machine's historical baseline. So the Metal stack is fine for that workload shape; the ~430 GiB multi-view Pro mapping is what Beta 8 chokes on.

Takeaway

On macOS 27 Beta 8 the failure mode looks like per-command-buffer completion latency (~28 ms wall per CB vs ~1 ms GPU-busy, ~112 CBs/token) rather than residency churn (#501) or fragmentation per se (#845 — coalescing to 4 buffers did not rescue). If others on 27.0 betas can A/B Beta 7 vs Beta 8, that would pin it. Happy to provide full logs, sample outputs, or test patches.

Environment: 2× Mac Studio (Mac15,14) M3 Ultra 512 GB; ds4 main 110afdd (+ the local mapping patch above, happy to share the diff); DeepSeek-V4-Pro-Q4K-Layers00-30.gguf / Layers-31-output.gguf; iogpu.wired_limit_mb=480000.

zhangjun8823-gif avatar Sep 04 '26 09:09 zhangjun8823-gif