Skip to content

fix: apply DSv4 RoPE from row-aligned frequencies - #219

Merged
hjh0119 merged 1 commit into
modelscope:mainfrom
shiaho777:fix/dsv4-row-aligned-rope
Oct 9, 2026
Merged

hjh0119 merged 1 commit into
modelscope:mainfrom
shiaho777:fix/dsv4-row-aligned-rope

Conversation

@shiaho777

Copy link
Copy Markdown
Contributor

_apply_mla_rope requires frequency row i to already hold token i's position. It then forwarded cu_seqlens into Megatron's packed RoPE helper. That helper rebuilds positions from the cumulative lengths and, when context parallel is greater than 1, assumes a zigzag split. DeepSeek-V4 packs with a contiguous split, so the rotation is applied to the wrong rows.

The packed lengths are no longer forwarded. The elementwise path uses the frequency row stored for that token. Callers can still pass cu_seqlens. A row-count mismatch still raises.

Checked with python3 -m pytest tests/test_dsv4_mla_rope.py (2 passed). The test loads the function and checks that a packed cu_seqlens reaches apply_rotary_pos_emb as None, and that a shorter frequency table still fails the row-alignment check. flake8 is clean on the changed files.

_apply_mla_rope requires freqs row i to already hold token i's position, then
forwarded cu_seqlens into Megatron's packed RoPE helper. That helper rebuilds
positions from cu_seqlens and, when context parallel is on, assumes a zigzag
split. DSv4 packs with a contiguous split, so those positions are wrong and the
rotation is applied to the wrong rows.

The packed lengths are no longer forwarded. The elementwise path uses the
frequency row that was stored for that token. Callers can still pass cu_seqlens;
a length mismatch still raises.
@hjh0119
hjh0119 merged commit b8238b2 into modelscope:main Oct 9, 2026
1 check passed
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