[Bug] DeepSeek-V4.1-Flash + DSPARK: unbounded SparsePrefillWorkspace allocation OOM-crashes the whole TP group
Checklist
- I searched related issues but found no solution.
- The bug persists in the latest version.
- Issues without environment info and a minimal reproducible demo are hard to resolve and may receive no feedback.
- If this is not a bug report but a general question, please start a discussion at https://github.com/sgl-project/sglang/discussions. Otherwise, it will be closed.
- Please use English. Otherwise, it will be closed.
Describe the bug
Serving DeepSeek-V4.1-Flash with DSPARK speculative decoding (--speculative-algorithm DSPARK) and fp8_e4m3 KV cache on TP2, 2×B300 (288GB), the server passes startup and serves normal traffic fine, but a deep queue of long-context requests (170k–212k prompt tokens each) draining into a single prefill step reliably OOM-crashes the whole TP group:
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 27.20 GiB.
GPU 0 has a total capacity of 267.68 GiB of which 26.77 GiB is free. Including
non-PyTorch memory, this process has 240.52 GiB memory in use. Of the allocated
memory 235.23 GiB is allocated by PyTorch, with 5.54 GiB allocated in private
pools (e.g., CUDA Graphs), and 1.43 GiB is reserved by PyTorch but unallocated.Both TP ranks hit the same error at the same time. The scheduler raises, and the whole server process exits (kill_process_tree). Every in-flight request drops. A second replica took over the failover traffic and hit the same crash 12 minutes later, at a smaller allocation size (15.40 GiB tried, 12.74 GiB free). So this is not a one-off. It reproduces whenever the queue depth and context length are large enough compared to whatever memory happens to be free at the time.
Traceback (identical shape on both occurrences, both ranks):
Scheduler hit an exception: Traceback (most recent call last):
File "sglang/srt/managers/scheduler.py", line 5696, in run_scheduler_process
scheduler.run_event_loop()
File "sglang/srt/managers/scheduler.py", line 1839, in run_event_loop
... dispatch_event_loop -> forward
File "sglang/srt/layers/attention/deepseek_v4_backend.py", line 3904, in _forward_prefill_sparse
workspace = self.sparse_prefill_workspace.get(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "sglang/srt/layers/attention/dsv4/sparse_prefill_utils.py", line 100, in get
self._buffer = torch.empty(
^^^^^^^^^^^^
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 27.20 GiB. ...Batch state at the moment of the first crash: #running-req: 0, #queue-req: 73, #pending-token: 337719 (a 73-request queue draining at once). Second occurrence: #running-req: 4, #queue-req: 16, #pending-token: 388518.
Analysis. SparsePrefillWorkspace.get() in sparse_prefill_utils.py reallocates its buffer with a plain torch.empty(...) call whenever the requested size is bigger than the current buffer. There is no check before this call:
def get(self, num_tokens, dtype=torch.bfloat16):
assert num_tokens > 0
current_capacity = self._buffer.shape[0] if self._buffer is not None else 0
current_dtype = self._buffer.dtype if self._buffer is not None else None
if num_tokens > current_capacity or dtype != current_dtype:
self._buffer = torch.empty(
(num_tokens, 1, WORKSPACE_DIM), dtype=dtype, device=self.device,
)
return self._buffer[:num_tokens]It is called from _forward_prefill_sparse with num_tokens = n_compressed + cache.swa_token_ids.shape[0]. That is the size of the compressed-region gather plus the SWA-region gather for every request in that prefill step. There is no mem_get_info check and no chunking anywhere in this path.
This is not bounded by --max-prefill-tokens. That flag only caps new tokens admitted per step. The crashing batch shows #new-token: 16384, right at that cap, so the throttle was respected and it still was not enough. It is also not bounded by --chunked-prefill-size. Grepping the file shows this flag is never read or enforced against this workspace. So a deep queue of long-context requests can push n_compressed + swa_token_ids well past what the new-token cap limits, and the allocation just tries for whatever that adds up to. There is no fallback.
This looks like the same bug class as #30505 (unbudgeted prefill transients in DSA's flash_mla_with_kvcache on GLM-5.2) and the budget-check gap we already fixed in the DSV4 indexer's MQA-logits path (related to #34155 and #33246). But this is a different call site, SparsePrefillWorkspace, and it has no budget or chunking guard at all.
Suggestion. Add the same kind of guard used for the DSV4 indexer's MQA-logits budget check. Read torch.cuda.mem_get_info() before the torch.empty(...) call in SparsePrefillWorkspace.get(). Then either chunk the prefill batch to fit, or raise an error that can be retried, instead of letting an oversized allocation take down the whole TP group. A single request failing is a much better outcome than the whole server crashing on one bad batch.
Source: sgl-project/sglang