Skip to content

Studio: account for DeepSeek-V4 compute buffer in context auto-fit - #6940

Merged
danielhanchen merged 2 commits into
mainfrom
studio-deepseek-v4-context-autofit
Jul 7, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-deepseek-v4-context-autofit

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_bytes models 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:

ggml_backend_cuda_buffer_type_alloc_buffer: allocating 67094.60 MiB on device 0: cudaMalloc failed: out of memory
graph_reserve: failed to allocate compute buffers

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)

Before After
Auto-fit context 1048576 (full 1M) about 256k
GPU OOM on the ~70 GB compute buffer, spills to CPU fits fully on one GPU
Throughput about 4 tok/s tens of tok/s

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.py passes 51/51.

Pairs with #6908, which registers DeepSeek-V4-Flash-GGUF and its reasoning ladder in Studio.

danielhanchen and others added 2 commits July 7, 2026 14:18
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.
@danielhanchen
danielhanchen merged commit 8efcc17 into main Jul 7, 2026
3 of 47 checks passed
@danielhanchen
danielhanchen deleted the studio-deepseek-v4-context-autofit branch July 7, 2026 14:20

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +3205 to +3208
return int(
self._DSV4_CTX_COMPUTE_FLAT_BYTES
+ self._DSV4_CTX_COMPUTE_BYTES_PER_TOK * n_ctx * ub_scale
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +3205 to +3208
return int(
self._DSV4_CTX_COMPUTE_FLAT_BYTES
+ self._DSV4_CTX_COMPUTE_BYTES_PER_TOK * n_ctx * ub_scale
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant