Repository navigation
Ulysses: dynamic and longrope RoPE see the whole sequence's positions - #17051
danielhanchen merged 5 commits into
Conversation
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]>
…n-rope-full-positions
# 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]
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
…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]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
On the generic Hugging Face Ulysses path (#16558), the model's
rotary_emb(hidden, position_ids)runs on each rank's shard ofposition_ids. transformers'@dynamic_rope_updatepicks frequencies frommax(position_ids) + 1:original_max_position_embeddingsand the long factors above it;inv_freqpastmax_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):
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_shardkeeps the unshardedposition_idson the manager for the duration of the step.attach_attention_hooks(Ulysses, generic path only) wraps rotary modules whoserope_typeisdynamicorlongrope, including per-layer-type dicts. The wrapper computes cos / sin over the whole sequence's positions and returns this rank's contiguous slice.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.pyhas 105 passed.🤖 Generated with Claude Code