Metal resident mode: queue residency set floods the kernel log with `required > max` (2× model size)

Aperta
#1,332 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c
Ambito
performance

Direzione di ricerca

Start by reading ds4_metal.m around lines 2213–2229, where g_model_residency_set is created, and trace where addResidencySet: attaches it to the command queue; also check the g_weight_residency_set path mentioned in the issue. Compare resident-mode runs with and without DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1, then run the suggested Metal tests and a benchmark. Done means the repeated required > max errors are resolved without reducing decode performance or causing GPU hangs.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

On an M4 Max with 128 GB, running ds4-server built from current main (b3f5018) with a resident (non-streaming) load of a 96.4 GB DeepSeek V4 Flash GGUF, the kernel rejects GPU command preparation about 75,000 times a minute. Each rejection reports that the command needs twice the size of the Metal model views: 195 GB against maxWired 115.4 GB. With DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1 the count is zero, and decode speed is the same. We have tested the env switch only; we have not tested a code fix.

Repro on main (b3f5018)

make ds4-server
./ds4-server -m <model>.gguf --ctx 32768 --port 8011                                         # A
DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1 ./ds4-server -m <model>.gguf --ctx 32768 --port 8011  # B

Each phase served ~3 minutes of /v1/completions decoding (15 requests × 400 tokens). Afterwards we counted the kernel's lines for ds4-server in each phase's window with log show --predicate 'eventMessage CONTAINS "required > max"'.

A: default B: DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1
window 22:13:04 – 22:16:24 22:16:34 – 22:19:47
required > max lines 246,023 (58,062 · 79,671 · 77,047 · 31,243 per clock minute) 0
FAILED To Free lines 1 0
decode speed (mean per request) 33.1 tok/s 33.5 tok/s
completions 15/15 15/15

A typical line from phase A:

kernel (IOGPUFamily) virtual IOReturn IOGPUCommandDescriptor::prepare(uint32_t) <ds4-server> (<private>) :
  required > max : requiredBytes = <195042312192> preparedBytes : <195041263616>
  maxWired = <115448725504> maxWiredInternal = <14431092736> currentWired = <99449323520>

90% of the lines (222,004) carry exactly requiredBytes = 195042312192. The rest range from 195.04 to 199.81 GB. During the phase, currentWired stayed between 98.3 and 105.0 GB, under maxWired.

The number is exactly twice the model views

ds4-server logs that it mapped the model as 2 overlapping shared buffers:

ds4: Metal model views created in 1.984 ms, residency requested in 2462.423 ms, warmup 14.684 ms (mapped 91913.67 MiB from offset 5.09 MiB)
ds4: Metal mapped mmaped model as 2 overlapping shared buffers

ds4_gpu_add_model_view_range overlaps adjacent views by the largest tensor plus a page. In this model the largest tensor is blk.37.ffn_gate_exps.weight, 1,140,850,688 B (MXFP4). So the two views add up to the mapped size plus about one tensor:

  • requiredBytes / 2 = 97,521,156,096
  • mapped size = file 96,383,787,904 − data offset 5,333,824 = 96,378,454,080
  • difference = 1,142,702,016 = the largest tensor + 1,851,328 B (1.77 MiB)

So requiredBytes ≈ 2 × the summed length of the model views. Those views are exactly the allocations in g_model_residency_set (ds4_metal.m ≈2213–2229). The flag only stops that set from being added to the command queue (addResidencySet:), and with it the count drops to zero. Our reading, not proven: the kernel's per-command budget counts these allocations once through the queue's residency set and again through the command's own use of the same buffers. If that is right, attaching the set to the queue buys nothing in resident mode that requestResidency doesn't already do, and it pushes every command over the wired limit.

Same symptom on a longer run

We first saw this on our own build: upstream fc80bd6 (2026-10-07) plus local patches to server request handling, steering and a lens readout. None of those patches touch residency, the model views or command-buffer setup. That build runs at ctx 393216 and serves for hours. Without the flag it logged about 48,000 lines a minute (48,346 · 49,653 · 39,900 in three consecutive minutes), with the identical requiredBytes = 195042312192. After a relaunch with the flag it logged 0 in the following 61 minutes, with decode unchanged at about 25.5 tok/s at ctx ≈ 94k. That the value is identical at ctx 32k and ctx 393k is why we think it's the model views and not KV.

Without the flag, that server process exited twice in one afternoon while the log was flooding. We have not shown that the flood caused those exits.

Environment

  • macOS 27.0.1, Apple M4 Max, 128 GB, Metal backend
  • ds4-server built from b3f5018 with the stock Makefile
  • Model: DeepSeek V4 Flash, a custom imatrix requant (IQ2_XXS routed experts, Q2_K/Q8_0 elsewhere, some routed-expert layers in MXFP4), 96,383,787,904 bytes
  • No other DS4_METAL_* variables set. In particular DS4_METAL_NO_RESIDENCY and DS4_METAL_UNRETAINED_COMMAND_BUFFERS were not set.

Related, as far as we can tell

  • #931: a commenter found that DS4_METAL_DISABLE_QUEUE_RESIDENCY_SET=1 alone cured 120 s hangs on non-streaming requests, and asked what the flag risks. That is still unanswered. This may be the same cause, seen from the kernel log's side.
  • #845: a commenter got one GPU hang with this flag combined with UNRETAINED_COMMAND_BUFFERS. We have not seen a hang with the flag alone (about an hour on our build, 3 minutes on main).
  • #638: whole mmap-backed views are actually wired on first GPU use, in streaming mode. Ours is different: resident mode, an inflated declared requiredBytes, and actual wired memory stayed under the limit.
  • Since our base, main has added a second queue-attached set for built weights (g_weight_residency_set), behind the same env gate. We haven't looked at whether it contributes.

Not done (happy to do any of these)

  • make test / ./ds4_test --metal-kernels and a ds4-bench before/after with the flag
  • a patch, e.g. not attaching the model set to the queue in resident mode, with a bench CSV
  • a longer soak on main with the flag
Lingua principale
C
Stelle
24.4k
Fork
2.4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di antirez/ds4

Tutte le issue di antirez/ds4

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.