Repository navigation
Studio: account for DeepSeek-V4 compute buffer in context auto-fit - #6940
Conversation
DeepSeek-V4-Flash's lightning indexer plus compressed sparse attention reserve a large context-scaling compute buffer that _compute_buffer_ctx_bytes did not model (the KQ-mask and dequant-scratch rates both miss it, even with an f16 cache). Measured on UD-Q4_K_XL at ub 512 it is about 65.5 GiB at 1M context, which the mask estimate puts near 1.5 GiB, so the auto-fit kept the full 1M train context and llama-server OOM'd allocating the ~70 GB buffer, then spilled to CPU (~4 tok/s). Add a deepseek4-gated flat plus per-token term so the fit caps the context (about 256k on a B200) and the model stays fully on GPU.
There was a problem hiding this comment.
Code Review
This pull request introduces context-scaling compute buffer estimations specifically for the DeepSeek-V4 (deepseek4) architecture in the llama.cpp backend. It adds constants and logic to account for its lightning indexer and sparse attention compute buffer overhead, preventing out-of-memory (OOM) issues during auto-fit. Additionally, a comprehensive suite of unit tests has been added to verify these estimations. There are no review comments, and I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d753959195
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| return int( | ||
| self._DSV4_CTX_COMPUTE_FLAT_BYTES | ||
| + self._DSV4_CTX_COMPUTE_BYTES_PER_TOK * n_ctx * ub_scale | ||
| ) |
There was a problem hiding this comment.
Honor KV type for DeepSeek-V4 compute sizing
For DeepSeek-V4 launches where the user explicitly sets a quantized KV cache (for example --cache-type-k q8_0 --cache-type-v q8_0, which this loader preserves and forwards), the total DSV4 graph reserve is not the same as the default f16-cache case. Returning the same Flash-derived reserve for every cache_type_kv makes the VRAM fit pessimistic for those runs, so Studio can unnecessarily shrink auto context or fall back to --fit/CPU offload even though the quantized-cache configuration would fit on GPU.
Useful? React with 👍 / 👎.
| return int( | ||
| self._DSV4_CTX_COMPUTE_FLAT_BYTES | ||
| + self._DSV4_CTX_COMPUTE_BYTES_PER_TOK * n_ctx * ub_scale | ||
| ) |
There was a problem hiding this comment.
Use binary tensor fit for DSV4 flat reserve
When tensor parallel is requested with MTP off/default, _plan_tensor_parallel uses a proportional cap because it assumes KV plus context compute is linear through the origin. Adding this fixed 2 GiB DSV4 term violates that assumption: if the requested context does not fit, the proportional cap is higher than the true (budget - flat) / slope solution, so tensor mode can still pick a context whose KV+compute exceeds the pool and launch without a --fit fallback.
Useful? React with 👍 / 👎.
Summary
Studio's context auto-fit under-reserves VRAM for DeepSeek-V4-Flash, so it keeps the model's full 1M train context, the compute-graph reserve OOMs, and llama.cpp spills layers to CPU. Inference then runs at a few tokens per second instead of tens.
Root cause
_compute_buffer_ctx_bytesmodels the context-linear compute buffer as the flash-attn KQ mask (f16 cache) or an n_embd-scaled dequant scratch (quantized cache). Neither covers DeepSeek-V4's lightning indexer plus compressed sparse attention, which reserve a much larger context-scaling buffer. On UD-Q4_K_XL (ubatch 512) that buffer is about 65.5 GiB at 1M context; the KQ-mask estimate puts it near 1.5 GiB. So the fit sees weights 144 GB + KV 20 GB, concludes the full 1M fits, keeps it, and llama-server dies:Fix
A
deepseek4-gated term in_compute_buffer_ctx_bytes: a flat indexer scratch plus a per-token rate, ubatch-scaled, applied for any KV cache type (the indexer buffer is present with an f16 cache too). The values are anchored to the measured buffer (about 2 GiB at 16k growing to about 65.5 GiB at 1M). Non-deepseek4 models are untouched.Before / After (UD-Q4_K_XL on a 183 GB B200)
Tests
Adds
TestContextBufferDSV4(7 cases): covers the measured 1M buffer, stays within about 1.3x, fires for an f16 cache, is KV-type independent, holds the ~2 GiB floor at 16k, scales with context and ubatch, and a regression guard that non-deepseek4 models keep the mask-only rate.pytest studio/backend/tests/test_compute_buffer.pypasses 51/51.Pairs with #6908, which registers DeepSeek-V4-Flash-GGUF and its reasoning ladder in Studio.