Repository navigation
feat(gpt-oss): add YaRN RoPE extensions with mscale for extended context - #2216
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements YaRN (Yet another RoPE extensioN) to extend GPT-OSS context length from 4096 to 131072 tokens (32x extension). YaRN applies frequency-dependent scaling corrections to RoPE embeddings and an mscale attention factor to maintain model quality at extended context lengths.
Key changes:
- Add YaRN helper functions for computing frequency correction ranges and smooth interpolation
- Extend
precompute_rope_cachewith YaRN scaling parameters (rope_factor, beta_fast/slow, original_seq_len) - Apply mscale attention scaling factor to softmax_scale in the Attention module
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tianyu-l
left a comment
There was a problem hiding this comment.
@wwwjn @shuhuayu
It seems gpt-oss supports yarn.
https://huggingface.co/openai/gpt-oss-120b/blob/main/config.json#L75
We have yarn configs but not yarn code https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/gpt_oss/model/args.py#L68
Could you help review?
|
@eous thanks for the fix! Left some comments. |
tianyu-l
left a comment
There was a problem hiding this comment.
I wonder if we can import and reuse the yarn implementation in DeepSeekV3? https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/deepseek_v3/model/model.py
I plan to consolidate the rotary embeddings implementations into one anyway.
@shuhuayu also wonder why our qwen3 doesn't have YaRN, maybe we should add?
|
We can use most parts of the yarn implementations in deepseek v3 except one difference. When computing pairs to rotate in hidden dimension, for example when it is For Qwen3, I double checked that all our listed models do not enable RoPE scaling per HuggingFace configs. We can add it make optional (by default set it as off) since some Qwen 3 models do require |
|
@shuhuayu |
Yes, we have two ways to apply RoPE as we see above, and we can couple it with different RoPE scaling techniques along hidden dimension, since they are orthogonal. After this fix, as you suggested we may consider consolidating the rotary embeddings implementations into |
|
Thanks @shuhuayu for pointing me to the official GPT-OSS implementation. While verifying our implementation against it, I discovered our mscale (concentration) was being applied incorrectly — we had it on softmax_scale instead of on the cos/sin values in the RoPE cache. The official applies: cos = freqs.cos() * concentration This gives mscale² scaling on attention logits (since both Q and K are rotated). Our previous approach only achieved mscale scaling — about 25% weaker than intended for rope_factor=32. I pushed a fix and a followup commit to address review feedback. |
|
@eous Thanks for the update, LGTM! Please rebase to fix conflict and lint. |
Implement YaRN (Yet another RoPE extensioN) for context length extension: - Add frequency correction functions for smooth RoPE interpolation - Implement precompute_rope_cache with YaRN scaling (rope_factor, beta_fast/slow) - Add mscale attention scaling factor for numerical stability at extended lengths - Apply mscale to softmax_scale in Attention module YaRN allows extending GPT-OSS from 4096 to 131072 tokens (32x) while maintaining perplexity. The mscale factor (0.1 * ln(rope_factor) + 1.0) compensates for the attention distribution changes at longer contexts.
- Return zeros instead of perturbing max_val in degenerate range case - Clarify base parameter comment (function default vs config value)
Move mscale application from attention softmax_scale to the RoPE cache cos/sin values. This matches the official GPT-OSS implementation where mscale (concentration) is applied to both cos and sin: cos = freqs.cos() * mscale sin = freqs.sin() * mscale Since both Q and K are rotated with scaled cos/sin, attention logits are scaled by mscale², preserving attention sharpness at extended context lengths. The previous implementation only achieved mscale scaling (25% weaker than intended for rope_factor=32).
Address PR review feedback: - Inline 4 helper functions into precompute_rope_cache for clarity - Rename beta_fast/beta_slow to ntk_beta/ntk_alpha to match reference - Fix condition: check only rope_factor > 1.0 (not seq_len check) - Fix ramp calculation: use raw floats instead of floor/ceil - Add assertion to validate YaRN parameters - Clean up comments
dc83b57 to
8d76ffc
Compare
|
@eous There is still a lint issue, can you help fix? |
|
Sorry about the delay, ran ufmt and it made a couple of corrections. |
…ext (pytorch#2216) Implement YaRN (Yet another RoPE extensioN) for context length extension: - Add frequency correction functions for smooth RoPE interpolation - Implement precompute_rope_cache with YaRN scaling (rope_factor, beta_fast/slow) - Add mscale attention scaling factor for numerical stability at extended lengths - Apply mscale to softmax_scale in Attention module YaRN allows extending GPT-OSS from 4096 to 131072 tokens (32x) while maintaining perplexity. The mscale factor (0.1 * ln(rope_factor) + 1.0) compensates for the attention distribution changes at longer contexts.
…ext (pytorch#2216) Implement YaRN (Yet another RoPE extensioN) for context length extension: - Add frequency correction functions for smooth RoPE interpolation - Implement precompute_rope_cache with YaRN scaling (rope_factor, beta_fast/slow) - Add mscale attention scaling factor for numerical stability at extended lengths - Apply mscale to softmax_scale in Attention module YaRN allows extending GPT-OSS from 4096 to 131072 tokens (32x) while maintaining perplexity. The mscale factor (0.1 * ln(rope_factor) + 1.0) compensates for the attention distribution changes at longer contexts.
Implement YaRN (Yet another RoPE extensioN) for context length extension:
YaRN allows extending GPT-OSS from 4096 to 131072 tokens (32x) while maintaining perplexity. The mscale factor (0.1 * ln(rope_factor) + 1.0) compensates for the attention distribution changes at longer contexts.