Skip to content

Ulysses: dynamic and longrope RoPE see the whole sequence's positions - #17051

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
lonexreb:fix/cp-ulysses-seq-len-rope-full-positions
Nov 11, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
lonexreb:fix/cp-ulysses-seq-len-rope-full-positions

Conversation

@lonexreb

@lonexreb lonexreb commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

On the generic Hugging Face Ulysses path (#16558), the model's rotary_emb(hidden, position_ids) runs on each rank's shard of position_ids. transformers' @dynamic_rope_update picks frequencies from max(position_ids) + 1:

  • longrope (Phi-3 / 3.5 / 4-mini long context) uses the short factors at or below original_max_position_embeddings and the long factors above it;
  • dynamic NTK recomputes inv_freq past max_position_embeddings.

So ranks rotate with different frequencies than a single process would, and keys from different frequency sets are attended together after the all-to-all. Long sequences are exactly where CP gets used. Unsloth's fast Llama path is not affected (it sets rope_seq_len = kv_seq_len * cp_manager.size).

Parity against one process (loss and all-reduced per-parameter grads, gloo CPU):

case main this PR
Phi-3 longrope, orig 10, seq 16, cp 2 max grad diff 1.2e-3, dloss 1.8e-5 1.5e-8, dloss 0
Phi-3 longrope, orig 20, seq 32, cp 4 6.5e-4 2.2e-8
Llama dynamic NTK 5.1e-5 (~3000x the plain-RoPE baseline) 1.9e-8
control: orig >= seq 3.0e-8 3.0e-8

At a realistic size (orig 4096, seq 16384, cp 4), rank 0's longrope cos is off by up to 2.4, and dynamic NTK ranks 0–2 are off by about 2.0.

Fix:

  • _ulysses_shard keeps the unsharded position_ids on the manager for the duration of the step.
  • attach_attention_hooks (Ulysses, generic path only) wraps rotary modules whose rope_type is dynamic or longrope, including per-layer-type dicts. The wrapper computes cos / sin over the whole sequence's positions and returns this rank's contiguous slice.
  • No collective is added. Outside a CP step, or when the shapes do not match a contiguous shard, the original forward runs unchanged.

Test: test_ulysses_seq_len_rope_uses_the_whole_sequence[dynamic|longrope] builds a tiny Llama (dynamic) or Phi-3 (longrope) where rank 0's shard stays within the original length while the whole sequence exceeds it. Both cases fail on main and pass here; tests/test_context_parallel.py has 105 passed.

🤖 Generated with Claude Code

transformers' dynamic_rope_update picks frequencies from max(position_ids) + 1:
longrope switches from short to long factors past
original_max_position_embeddings, dynamic NTK recomputes inv_freq. On the
generic Hugging Face path the rotary embedding runs on each rank's shard of
position_ids, so ranks rotate with different frequencies than one process
would (Phi-3 longrope, cp 2: max grad diff 1.2e-3 vs ~1e-8 for plain RoPE; at
orig 4096 / seq 16384 / cp 4 rank 0's cos is off by 2.4). Unsloth's fast
Llama path already scales rope_seq_len by the cp size.

_ulysses_shard keeps the unsharded position_ids, and attach_attention_hooks
wraps dynamic / longrope rotary modules on the generic path to compute cos /
sin over them and keep this rank's slice. No collective is added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@danielhanchen danielhanchen self-assigned this Nov 11, 2026
# Conflicts:
#	tests/test_context_parallel.py
#	unsloth/context_parallel.py
transformers 4.57 ignores rope_parameters and falls back to default RoPE,
so the test passed without the fix there. rope_scaling is read by 4.57 and
converted by 5.x; assert the rope type so neither corner passes vacuously.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T11:00:01.328163Z 4554673 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: bb992b267f

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

Comment thread unsloth/context_parallel.py Resolved
…the sequence's largest

dynamic / longrope only need max(position_ids) + 1 to pick frequencies, so append the
largest position to the local slice and drop it afterwards. The returned cos / sin are
this rank's size instead of views into full-sequence tables kept alive for backward.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 455467305e

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

@danielhanchen
danielhanchen merged commit ccbd70b into unslothai:main Nov 11, 2026
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.

2 participants