Skip to content

[rl] generator: size hybrid attention pages with V's head size (Kimi K3) - #5955

Merged
felipemello1 merged 2 commits into
mainfrom
felipemello1/vllm-v2/1-hybrid-page-size
Nov 8, 2026
Merged

felipemello1 merged 2 commits into
mainfrom
felipemello1/vllm-v2/1-hybrid-page-size

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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_invariant test 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.

Kimi K3 RL test, bytes per KV block (from vLLM's error)
before:  block stride 638976 (V counted at K's head size) != real page 532480 -> crash at KV-cache init
after:   page estimate = real page -> runs, trainer vs generator logprob diff 0

Problem

  1. vLLM's estimate. For a hybrid model, vLLM sizes the attention page from a spec without head_size_v (vllm/platforms/interface.py:854-863 on the pinned nightly, :1037-1048 on vLLM main), so V gets K's head size. vLLM's DiffKV backends don't correct it in customize_spec.
  2. Smaller V. Kimi K3's full attention has a smaller V head, so its real page is smaller than the estimate. The block stride is padded.
  3. Now a crash. [rl] Batch-invariant FA4 runs use exactly 16-token KV pages #5870 changed batch-invariant FA4 kernel blocks from MultipleOf(16) to exactly [16], so vLLM must split each KV block, and it refuses to split a padded block.
  4. Who hits it. Only batch-invariant Kimi K3: uses_compressed_mla_cache keeps 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

  1. TorchTitanVarlenInnerAttentionDiffKVBackend.customize_spec sets head_size_v to 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.
  2. vLLM's DiffKV backends keep V's head size on the class (set_head_size_v), so this assumes one V head size per process, as vLLM does. Kimi K3 has one.
  3. A TODO names the vLLM change that deletes this override: vLLM's DiffKV backends setting head_size_v in customize_spec. A vLLM-side fix is drafted at [Bugfix][Attention] Count DiffKV's V head size in hybrid KV page sizing felipemello1/vllm#2.
  4. Unchanged: every other attention backend, and models whose K and V head sizes are equal.

Results

Numerics: Kimi K3 batch-invariant RL, trainer vs generator logprob diff max 0 at steps 1-3.

pinned nightly, GB300                            GPUs  ran on        result
tests/rl/unit_tests/cpu                          -     69cc40a54     568 passed, 7 skipped
Kimi K3 batch-invariant RL, 3 steps (rl_h100)    4     69cc40a54     passed; logprob diff max 0 at steps 1-3
                                                       058306ee5     passed; logprob diff max 0 at steps 1-3
                                                       without fix * crash at KV-cache init
Kimi K3 bitwise parity (B200 CI's test)          1     with fix *    4 passed

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: replace after vLLM's customize_spec instead of before. Both give the same spec, because vLLM's base customize_spec returns the spec unchanged.

* Ran with the next PR's API fixes underneath. They don't touch the attention page code.

NOTE:

  • This changes Kimi K3's KV block size on every GPU. vLLM sizes the block from the per-token attention page, which drops from 2 x K to K + V head bytes. Tested on GB300 only.
  • B200 CI runs the Kimi K3 parity test only with the ciflow/b200 tag. H100 CI never splits blocks, so it never hit the crash.

Test plan

  • python -m pytest tests/rl/unit_tests/cpu
  • torchrun --nproc-per-node=1 -m pytest tests/rl/unit_tests/gpu/test_bitwise_parity.py::TestBitwiseParityKimiK3DebugVarlen
  • python -m tests.integration_tests.run_tests <out> --ngpu 4 --test_suite rl_h100 --test_name rl_grpo_kimi_k3_debug_batch_invariant

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude 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.

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

  1. PR description -- "Summary (human-written)" is empty. The author needs to fill it in.
  2. torchtitan/rl/generator/vllm/attention/attention.py:128 -- this works around a vLLM gap (hybrid page sizing ignores head_size_v) but has no TODO or upstream link (inline).

Should consider

  • torchtitan/rl/generator/vllm/attention/attention.py:131 -- cls.head_size_v is 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 when value_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.replace on 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).

Comment on lines +128 to +130
# 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).

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.

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.:

Suggested change
# 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.

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.

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)

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.

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.

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.

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.

Felipe Mello added 2 commits November 8, 2026 14:43
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.
@felipemello1
felipemello1 force-pushed the felipemello1/vllm-v2/1-hybrid-page-size branch from 058306e to 88bc77f Compare November 8, 2026 22:44
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review again. Fixes are in the new commits; each of your comments has a reply in its thread.

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude 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.

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 in VLLMInnerAttention.__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): replace now runs before super().customize_spec, the same order as vLLM's own fix. Per the description, vLLM's base customize_spec returns the spec unchanged, so both orders give the same result. No new issues.

Must fix

  1. PR description: "Summary (human-written)" is still empty. The author has to write it; agents leave it alone.
  2. 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.

@felipemello1
felipemello1 marked this pull request as ready for review November 8, 2026 22:53
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 8, 2026
@felipemello1 felipemello1 added the ciflow/b200 Trigger B200 CI label Nov 8, 2026
@felipemello1
felipemello1 merged commit b6d2c53 into main Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants