RL icon indicating copy to clipboard operation
RL copied to clipboard

fix: interleave Eagle3 draft QKV into Megatron's per-group layout

Open isomap opened this issue 5 months ago • 1 comments

What does this PR do ?

Fixes a QKV weight-layout bug in the Eagle3 online draft path: online draft training degraded / stalled speculative-decoding acceptance even though the draft loss was decreasing. After this change, acceptance tracks the draft loss as expected.

Root cause. load_hf_weights_to_eagle / export_eagle_weights_to_hf fuse the draft's attention projections with a naive cat([q, k, v]) (head-major), but Megatron's SelfAttention.get_query_key_value_tensors reads linear_qkv as an interleaved per-query-group layout ([g0: q.. k v | g1: q.. k v | ...]). For GQA models (e.g. Qwen3-1.7B: 16 query heads / 8 KV heads) these layouts differ, so the trainer-side draft attention is fed scrambled q/k/v.

Fix. Reorder HF q/k/v into Megatron's interleaved layout on load (_interleave_qkv) and reverse it on export (_deinterleave_qkv). o_proj, MLP gate/up, and MHA paths are unaffected; for an untrained draft the exported weights are byte-identical to before (no change to the frozen path).

Issues

N/A — bug in the existing Eagle3 online draft path; no tracking issue.

Usage

No usage change. Existing Eagle3 online recipes (examples/configs/recipes/llm/grpo-qwen3-1.7b-1n{4,8}g-megatron-eagle3.yaml) work as before; the draft just trains correctly now.

Before your PR is "Ready for review"

Pre checks:

  • [x] Make sure you read and followed Contributor guidelines
  • [x] Did you write any new necessary tests? — tests/unit/models/megatron/test_eagle_qkv_layout.py
  • [x] Did you run the unit tests and functional tests locally? — unit tests pass (-m mcore); validated end-to-end with a 300-step online GRPO A/B
  • [x] Did you add or update any necessary documentation? — N/A (bugfix to existing feature; no doc change needed)

Additional Information

Validation.

Unit test (test_eagle_qkv_layout.py): asserts the loaded weight satisfies Megatron's q/k/v split contract, the load↔export round-trip, and both GQA and MHA configs.

End-to-end 300-step online GRPO A/B (Qwen3-1.7B + AngelSlim/Qwen3-1.7B_eagle3, 4×H100):

acceptance length (first→last) draft loss
Fixed (interleaved) 1.94 → 2.53 (+30%), plateau ~2.52 1.69 → 0.87
Buggy (naive-cat) flat ~1.88 8.5 → 3.0
W B Chart 6_12_2026, 4_03_37 PM W B Chart 6_12_2026, 4_03_43 PM

isomap avatar Jun 12 '26 23:06 isomap

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

copy-pr-bot[bot] avatar Jun 12 '26 23:06 copy-pr-bot[bot]

/ok to test f6ae77f

yuki-97 avatar Jun 16 '26 06:06 yuki-97