CUDA: server process dies during long prefill at ~512K tokens (ctx=1048576, managed KV cache) instead of returning an error
Environment
- Hardware: Jetson AGX Thor developer kit (sm_110, 128 GB unified memory, MAXN power mode)
- Build: current main @ ee7e840,
make cuda CUDA_ARCH=sm_110, CUDA 13.0 - Model: Flash 0731 Q2 (
DeepSeek-V4-Flash-IQ2XXS-w2Q2K-AProjQ8-SExpQ8-OutQ8-chat-v2-imatrix-0731.gguf, 80.8 GiB, resident) - Server:
./ds4-server -m ds4flash.gguf --cuda --ctx 1048576 -t 8(managed KV cache mode activates at this ctx)
Reproduction
Send a single /v1/chat/completions streaming request whose user message tokenizes to 1,012,393 tokens (real text, concatenated novel slices). The server accepts the request (it is below the configured context) and starts chunked prefill.
Startup memory lines:
ds4: memory detail: ctx=1048576 prefill_cap=4096 raw_kv_rows=4352 compressed_kv_rows=262146 backend=cuda
ds4: CUDA using managed KV cache for ctx=1048576 (kv cache 13.79 GiB, context buffers 21.79 GiB); this may degrade performance but is needed for very large contexts
1030 10:39:17 ds4-server: context buffers 22319.75 MiB (ctx=1048576, backend=cuda, prefill_chunk=4096, raw_kv_rows=4352, compressed_kv_rows=262146)
Prefill progresses normally (~211 t/s average, declining from ~260 to ~154 t/s as KV grows) for 40.5 minutes, then the process dies at exactly chunk 512,000/1,012,393 (50.6%):
1030 12:10:38 ... prefill chunk 507904/1012393 (50.2%) chunk=155.07 t/s avg=212.06 t/s 2395.144s
1030 12:11:05 ... prefill chunk 512000/1012393 (50.6%) chunk=153.75 t/s avg=211.41 t/s 2421.784s
ds4: Metal synchronize after chunked prefill failure also failed
At the moment of death, physical memory was ~120/122 GiB per free -g. There is no further log line, no request completion record, no error response — the HTTP stream just ends (client sees EOF mid-prefill) and the process is gone.
Two issues
- OOM should degrade gracefully. With
--ctx 1048576the server happily accepts a ~1M-token request, but the combination of resident 80.8 GiB weights + 21.79 GiB context buffers + growing managed KV + prefill transient buffers exhausts unified memory around 512K tokens of committed KV. Ideally the engine would return a 5xx/insufficient-memory error to the client and keep serving other sessions; today the whole server dies. A--max-committed-kvguard or an admission check (estimated KV commit vs. available physical memory) before accepting a request would make huge contexts fail safely. - Wrong backend string on CUDA. The last line says
Metal synchronize after chunked prefill failure also failedon a CUDA-only build — the message appears to be shared with the Metal backend and not specialized per backend.
Extra data points
- The same server served a 100,775-token prompt fine at ~353 t/s prefill (285.6 s) earlier the same day at the same ctx setting; the crash is depth-dependent, not prompt-dependent.
- After the crash,
--ctx 262144runs the non-managed KV path with 5.86 GiB context buffers and is stable — we switched production to 256K. - I have the full 126-line prefill progress log if useful.
Thanks for ds4 — the Thor numbers with PR #121 are great (decode 12.65 → 15.31 tok/s here).
Follow-up data from bisecting the ceiling on the same machine/setup:
ctx=524288 (512K, also managed KV mode) survives a full-depth request. A 506,491-token prompt prefills in 2268.7 s (319→166 t/s as KV grows), answers two needle-retrieval questions planted at 20% and 50% depth correctly, decodes at 200.9 ms/token, and the server stays alive (peak ~117 GiB of 122).
So the 1M-ctx crash root cause is now clear: at the same committed-KV depth (~512K tokens), ctx=1048576 carries 21.79 GiB of context buffers vs 11.08 GiB at ctx=524288 — the extra 10.7 GiB is what exhausts unified memory. The buffer size scales with ctx, so large-ctx configurations die even when the request would fit in a smaller-ctx configuration.
Two related behaviors observed while testing, in case they are useful:
- Client disconnect does not cancel an in-flight prefill. After the client vanished, the server kept prefilling the orphaned 506K request for ~40 minutes of GPU time.
- The orphaned request then terminated silently at 88.1% — no log line, no error — leaving ~10 GiB of KV committed that was not released afterwards (the server kept answering new requests fine).
Also confirmed while scanning the managed-KV threshold: ctx=262144 takes the non-managed path (buffers 5.86 GiB); ctx=393216+ was not scanned, 524288/786432/1048576 all select managed mode.