Skip to content

Preallocate the static KV cache with config.head_dim - #8389

Merged
delock merged 2 commits into
deepspeedai:masterfrom
ebarkhordar:fix/static-cache-config-head-dim
Sep 3, 2026
Merged

Preallocate the static KV cache with config.head_dim#8389
delock merged 2 commits into
deepspeedai:masterfrom
ebarkhordar:fix/static-cache-config-head-dim

Conversation

@ebarkhordar

Copy link
Copy Markdown
Contributor

DeepSpeedStaticCache preallocates its KV buffers with hidden_size // num_attention_heads. Models that set head_dim explicitly (Qwen3, Gemma, Llama 4) break that identity, so the buffers get the wrong last dimension and the prefill copy in _generate_graph dies before the graph is ever captured:

RuntimeError: The size of tensor a (16) must match the size of tensor b (32) at non-singleton dimension 3

Now it reads head_dim off the config and falls back to the division when the attribute is missing or None (older configs set it to None).

To reproduce on CPU: a tiny Qwen3 with hidden_size=64, num_attention_heads=4, head_dim=32, prefilled through the HF StaticCache the way _generate_graph does, then the copy loop at hybrid_engine_rollout.py:346 verbatim. The prefill cache comes back (1, 2, 8, 32) and DeepSpeedStaticCache allocates (1, 2, 8, 16). With head_dim=16 the same script is fine on both sides, which is why nothing has caught this so far.

Two tests in tests/unit/runtime/rollout/. The head_dim one fails on master. No GPU on this machine, so the capture and replay path itself is not exercised, only the allocation and the copy that feeds it.

DeepSpeedStaticCache sized its buffers with hidden_size // num_attention_heads,
which is wrong for any model that sets head_dim explicitly. The prefill copy in
_generate_graph then fails on a shape mismatch.

Signed-off-by: Ehsan Barkhordar <realbarkhordar@gmail.com>

@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: c6bea0c8c4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

num_heads = getattr(text_config, "num_key_value_heads", getattr(text_config, "num_attention_heads", 1))
head_dim = getattr(text_config, "hidden_size", 1) // getattr(text_config, "num_attention_heads", 1)
# head_dim is not always hidden_size // num_attention_heads, so prefer the config value
head_dim = getattr(text_config, "head_dim", None) or (getattr(text_config, "hidden_size", 1) //

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the mandatory sign-off trailer

This is a non-merge commit, but the commit message for e7c1229c51d6ba3c4896f6681196c8e2a2e11e1c has no Signed-off-by trailer, so it does not satisfy the repository's commit requirements; recreate the commit with --signoff before merging.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The commit on this PR is c6bea0c and its message ends with the Signed-off-by trailer, and the DCO check is green. The sha in the comment above, e7c1229, is not in this repo at all (the API returns 422 for it).

@delock
delock self-requested a review September 2, 2026 01:17
@delock
delock enabled auto-merge September 3, 2026 08:40
@delock
delock added this pull request to the merge queue Sep 3, 2026
Merged via the queue into deepspeedai:master with commit e74c707 Sep 3, 2026
13 checks passed
@ebarkhordar
ebarkhordar deleted the fix/static-cache-config-head-dim branch September 3, 2026 09:54
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.

2 participants