Repository navigation
[rl] generator: size hybrid attention pages with V's head size (Kimi K3) - #5955
Conversation
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 2m 14s —— View job Approve -- please address the items before merging (missing human-written summary; add an upstream vLLM TODO). See the review. |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. This is a minimal, local fix for a real crash at KV-cache init. It needs an upstream TODO and the human-written summary.
What this PR does. For hybrid models, vLLM sizes the shared attention page from K's head size and uses it for V as well. Kimi K3's full-attention V head is smaller, so the real page is smaller than the stride. vLLM won't split a padded block, and batch-invariant FA4 on SM100 needs 16-token kernel blocks, so init crashes. The unequal-K/V backend now overrides customize_spec to report the real head_size_v, which makes the estimated page equal the real page.
Example.
Kimi K3, batch-invariant FA4 on GB300 (kernel block 16)
before: page estimate = K + K head bytes -> stride 638976 != real page 532480 -> vLLM refuses split -> crash
after: page estimate = K + V head bytes = 532480 -> split OK -> trainer/generator logprob diff 0
Equal-K/V models: backend not selected, no change.
Must fix
- PR description -- "Summary (human-written)" is empty. The author needs to fill it in.
torchtitan/rl/generator/vllm/attention/attention.py:128-- this works around a vLLM gap (hybrid page sizing ignoreshead_size_v) but has no TODO or upstream link (inline).
Should consider
torchtitan/rl/generator/vllm/attention/attention.py:131--cls.head_size_vis a process-wide class attribute, so a model with two different V head sizes would silently get the wrong page size (inline).
Details
- Gate: the problem is real (crash on the current nightly and on vLLM main, per the description). The fix stays on the torchtitan-owned backend subclass and touches no shared code, so it's at the right layer within torchtitan. The underlying bug is in vLLM, which is why the TODO is needed.
- Scope: the override only applies to
TorchTitanVarlenInnerAttentionDiffKVBackend, which is selected only whenvalue_head_dim != head_dim(attention.py:461-466). Other models and backends are untouched. - Numerics: no compute change. The KV block size changes on every GPU for Kimi K3 (2xK -> K+V bytes per token), which changes generator memory and how many blocks fit. The description says so. Trainer numerics are unaffected.
dataclasses.replaceon a spec dataclass is a shallow copy. That's fine here because the spec fields are scalars/dtypes.
Not verified: vLLM's customize_spec call site and that it runs on the spec used for hybrid page unification (vLLM source wasn't available in this environment); behavior on H100/B200 (the PR was tested on GB300 only).
| # For a hybrid model, vLLM sizes the attention page from the model's head size | ||
| # alone, so V gets K's head size and the shared page outgrows the layer's real | ||
| # page. vLLM then cannot split a block into smaller kernel blocks (FA4 on SM100). |
There was a problem hiding this comment.
This works around a vLLM bug: hybrid page sizing ignores head_size_v. vLLM's own FlashAttentionDiffKVBackend should hit the same padded-block split failure for any native hybrid model with unequal K/V, so the fix belongs in vLLM. Without a TODO, this override stays after upstream fixes it, and the next person to touch it can't tell whether it's still needed. Add a TODO with the vLLM issue/PR link (open one if none exists), e.g.:
| # For a hybrid model, vLLM sizes the attention page from the model's head size | |
| # alone, so V gets K's head size and the shared page outgrows the layer's real | |
| # page. vLLM then cannot split a block into smaller kernel blocks (FA4 on SM100). | |
| # For a hybrid model, vLLM sizes the attention page from the model's head size | |
| # alone, so V gets K's head size and the shared page outgrows the layer's real | |
| # page. vLLM then cannot split a block into smaller kernel blocks (FA4 on SM100). | |
| # TODO(vllm-project/vllm#<issue>): drop once vLLM's hybrid page sizing uses | |
| # head_size_v. |
There was a problem hiding this comment.
Reply from claude: Done in 88bc77f. The root cause is in vLLM: its hybrid page estimate builds a spec without head_size_v (vllm/platforms/interface.py:1037-1048 on main), and its DiffKV backends don't fix it in customize_spec.
It became a crash when #5870 changed batch-invariant FA4 kernel blocks from MultipleOf(16) to [16].
The comment now says this. The TODO names the vLLM change that deletes the override: DiffKV backends set head_size_v in customize_spec. A vLLM-side fix is drafted at felipemello1/vllm#2.
Filing upstream is the author's call, so the code carries no link.
| # For a hybrid model, vLLM sizes the attention page from the model's head size | ||
| # alone, so V gets K's head size and the shared page outgrows the layer's real | ||
| # page. vLLM then cannot split a block into smaller kernel blocks (FA4 on SM100). | ||
| return replace(super().customize_spec(spec), head_size_v=cls.head_size_v) |
There was a problem hiding this comment.
cls.head_size_v is the process-wide class attribute that FlashAttentionDiffKVBackend.set_head_size_v sets (attention.py:462). The last VLLMInnerAttention built wins. That's fine for Kimi K3, where every unequal-K/V layer has the same V head size. But a model with two different value_head_dims would silently get the wrong page size for one of them, and it would only show up as a KV-cache layout mismatch at runtime. If you want the invariant to be explicit, raise in VLLMInnerAttention.__init__ when head_size_v is already set to a different value. Otherwise, a one-line comment noting the single-V-size assumption is enough.
There was a problem hiding this comment.
Reply from claude: Added a comment in 88bc77f instead of a raise.
vLLM's DiffKV backends keep V's head size on the class (flash_attn_diffkv.py:37,44-45). So one V head size per process is vLLM's assumption, not one this PR adds. Kimi K3 has one.
A raise in VLLMInnerAttention.__init__ would also fire falsely when one process builds two models with different V sizes one after another, as a test session can.
vLLM sizes a hybrid model's attention page from the model's head size alone, so a layer with a different V head size (Kimi K3) gets a padded block stride. vLLM refuses to split such a block into the smaller kernel blocks that FA4 uses on SM100, so Kimi K3 RL failed at KV-cache init on GB300. The DiffKV backend's customize_spec now sets V's head size. This changes Kimi K3's KV block size on every GPU.
…e vLLM fix Set head_size_v on the spec before calling super(), the order vLLM's own fix uses, so the parent sees the real V head size. The comment now says what is wrong in vLLM (its hybrid page estimate builds a spec without head_size_v), notes the one-V-head-size-per-process assumption vLLM's DiffKV backends already make, and names the vLLM change that deletes this override.
058306e to
88bc77f
Compare
|
Reply from claude: @claude please review again. Fixes are in the new commits; each of your comments has a reply in its thread. |
|
Claude finished @felipemello1's task in 55s —— View job Approve -- please address the items before merging (human-written summary is still empty; add an upstream vLLM link to the TODO). See the review. |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The new commit settles both code comments from the last review. What's left is the human-written summary and an upstream link in the TODO.
Re-review of 88bc77f.
Resolved:
- Single-V-head-size assumption (
attention.py:131-132): it's now documented. The author's reasoning holds:set_head_size_v(attention.py:465) stores V's head size on vLLM's class, so the one-V-size-per-process limit comes from vLLM. A raise inVLLMInnerAttention.__init__would also fire by mistake when one test session builds models one after another. - TODO exit condition (
attention.py:133): the TODO now names the vLLM change that removes the override, so a future reader can tell when to delete it. - Order change (
attention.py:134):replacenow runs beforesuper().customize_spec, the same order as vLLM's own fix. Per the description, vLLM's basecustomize_specreturns the spec unchanged, so both orders give the same result. No new issues.
Must fix
- PR description: "Summary (human-written)" is still empty. The author has to write it; agents leave it alone.
torchtitan/rl/generator/vllm/attention/attention.py:133: partly resolved. The TODO names the fix but has no link. CLAUDE.md principle 3 asks for an upstream issue link on workarounds for upstream gaps. Once felipemello1/vllm#2 is opened against vllm-project/vllm (or an issue is filed), add the link, e.g.TODO(vllm-project/vllm#NNNN). If you don't plan to file upstream before merging, linking the draft is better than nothing.
A maintainer still needs to review: this changes Kimi K3's generator KV block size on every GPU, and it was only tested on GB300.
Not verified: vLLM's customize_spec call site in hybrid page unification. vLLM source isn't available in this environment.
Summary (human-written)
Agent details
TLDR: Kimi K3 batch-invariant RL crashes at KV-cache init on GB300 with the current nightly, so main's
rl_grpo_kimi_k3_debug_batch_invarianttest is red. vLLM sizes Kimi K3's attention page with K's head size for V too, then refuses to split the padded block. The unequal-K/V attention backend now reports V's head size. This PR alone turns the test green. Bottom of a 3-PR stack, based on main: #5955 (this) -> #5498 -> #5956.Problem
head_size_v(vllm/platforms/interface.py:854-863on the pinned nightly,:1037-1048on vLLM main), so V gets K's head size. vLLM's DiffKV backends don't correct it incustomize_spec.MultipleOf(16)to exactly[16], so vLLM must split each KV block, and it refuses to split a padded block.uses_compressed_mla_cachekeeps materialized K/V (the unequal-K/V backend) only in batch-invariant mode. It crashes at KV-cache init on GB300, on the current nightly and on vLLM main.Solution
TorchTitanVarlenInnerAttentionDiffKVBackend.customize_specsetshead_size_vto the layer's V head size, then calls vLLM's. vLLM's page estimate then equals the real page. This is the order vLLM's own fix uses.set_head_size_v), so this assumes one V head size per process, as vLLM does. Kimi K3 has one.head_size_vincustomize_spec. A vLLM-side fix is drafted at [Bugfix][Attention] Count DiffKV's V head size in hybrid KV page sizing felipemello1/vllm#2.Results
Rows ran at 69cc40a54, this PR's code before the rebase. The stack was then rebased onto main 7d88c59, which adds one FSDP commit that touches none of these files.
058306e is this PR's first commit alone:
replaceafter vLLM'scustomize_specinstead of before. Both give the same spec, because vLLM's basecustomize_specreturns the spec unchanged.* Ran with the next PR's API fixes underneath. They don't touch the attention page code.
NOTE:
ciflow/b200tag. H100 CI never splits blocks, so it never hit the crash.Test plan
python -m pytest tests/rl/unit_tests/cputorchrun --nproc-per-node=1 -m pytest tests/rl/unit_tests/gpu/test_bitwise_parity.py::TestBitwiseParityKimiK3DebugVarlenpython -m tests.integration_tests.run_tests <out> --ngpu 4 --test_suite rl_h100 --test_name rl_grpo_kimi_k3_debug_batch_invariant