Skip to content

fix(attention): read per-runner kv cache dtype off model_runner - #32251

Merged
ch-wan merged 1 commit into
mainfrom
cheng/fix-kv-cache-dtype-per-runner
Jul 24, 2026
Merged

ch-wan merged 1 commit into
mainfrom
cheng/fix-kv-cache-dtype-per-runner

Conversation

@ch-wan

@ch-wan ch-wan commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

The FlashAttention and XPU attention backends resolved kv_cache_dtype_str from the
process-global published model config (get_model().kv_cache_dtype). That global
reflects only the target runner: a draft (speculative) worker configures its own kv cache
dtype but does not publish it, so a draft backend would inherit the target's dtype string
and mis-select its fp8 code paths whenever the draft's kv cache dtype differs from the
target's.

Read the dtype string straight from model_runner.kv_cache_dtype_str instead, which is
set per runner during kv-cache configuration before any attention backend is initialized,
so each backend — target or draft — uses its own resolved dtype.

Also drop the getattr(..., server_args.kv_cache_dtype) fallback in the lightning and
dual-chunk backends: kv_cache_dtype_str is always set on a real runner before backend
init, so the fallback only masked setup bugs.

Test

  • test/manual/attention/test_flashattn_backend.py mock updated to set the attribute
    directly; the non-CP tests pass.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #30054771755
Latest PR Test (Extra): ❌ Run #30054771718

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@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: 6597fcfc65

ℹ️ 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".

from sglang.srt.runtime_context import get_model

self.kv_cache_dtype_str = get_model().kv_cache_dtype
self.kv_cache_dtype_str = model_runner.kv_cache_dtype_str

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 Initialize the new dtype field in all runner fixtures

When GPU attention tests construct this backend with their mock runners, this direct access raises AttributeError: only test/manual/attention/test_flashattn_backend.py was updated, while test_flashattn_mla_backend.MockModelRunner and the dense-attention MockModelRunner still omit kv_cache_dtype_str. The same omission exists in MockLightningModelRunner and DualChunkMockModelRunner for the other direct accesses introduced here, so the corresponding attention-unittest fixtures also fail during backend construction rather than exercising kernels. Please update every in-repo runner fixture that instantiates these backends, or retain a compatible per-runner fallback.

Useful? React with 👍 / 👎.

@ch-wan

ch-wan commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

The FlashAttention and XPU attention backends resolved `kv_cache_dtype_str`
from the process-global published model config via `get_model().kv_cache_dtype`.
That global reflects only the target runner: a draft (speculative) worker
configures its own kv cache dtype but does not publish it, so a draft backend
would inherit the target's dtype string and mis-select its fp8 code paths
whenever the draft's kv cache dtype differs from the target's.

Read the dtype string straight from `model_runner.kv_cache_dtype_str` instead,
which is set per runner during kv-cache configuration before any attention
backend is initialized, so each backend — target or draft — uses its own
resolved dtype.

Also drop the `getattr(..., server_args.kv_cache_dtype)` fallback in the
lightning and dual-chunk backends: `kv_cache_dtype_str` is always set on a real
runner before backend init, so the fallback only masked setup bugs.

Because the backends now read the attribute directly, the attention unit-test
mock model runners (the shared attention_unittest kit and the manual
FlashAttention backend tests) set `kv_cache_dtype_str` to match a real runner.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ch-wan
ch-wan force-pushed the cheng/fix-kv-cache-dtype-per-runner branch from b86ddc7 to 987d44a Compare July 23, 2026 23:57
@ch-wan
ch-wan merged commit eac7c7d into main Jul 24, 2026
154 of 167 checks passed
@ch-wan
ch-wan deleted the cheng/fix-kv-cache-dtype-per-runner branch July 24, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant