Skip to content

feat(gpt-oss): add YaRN RoPE extensions with mscale for extended context - #2216

Merged
tianyu-l merged 5 commits into
pytorch:mainfrom
eous:yarn-rope-extensions
Feb 5, 2026
Merged

tianyu-l merged 5 commits into
pytorch:mainfrom
eous:yarn-rope-extensions

Conversation

@eous

@eous eous commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI review requested due to automatic review settings January 8, 2026 23:50
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 8, 2026

Copilot AI 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.

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_cache with 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 tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tianyu-l
tianyu-l requested a review from shuhuayu January 20, 2026 06:45
@shuhuayu

Copy link
Copy Markdown
Contributor

@eous thanks for the fix! Left some comments.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@shuhuayu

Copy link
Copy Markdown
Contributor

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 [1, 2, 3, 4, 5, 6], in deepseek v3, it uses pairs [1, 2], [3, 4], [5, 6], see https://github.com/deepseek-ai/DeepSeek-V3/blob/9b4e9788e4a3a731f7567338ed15d3ec549ce03b/inference/model.py#L390; in gptoss it uses pairs [1, 4], [2, 5], [3, 6], see https://github.com/openai/gpt-oss/blob/599476783c6f88508dab8577808b5ead5cbee8d2/gpt_oss/torch/model.py#L57.

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 YaRN type RoPE scaling like this one https://huggingface.co/Qwen/Qwen3-14B-AWQ/blob/refs%2Fpr%2F2/config.json.

@tianyu-l

Copy link
Copy Markdown
Contributor

@shuhuayu
I see. It sounds that if we keep two rotary embedding impls, we need two corresponding yarn impls.

@shuhuayu

Copy link
Copy Markdown
Contributor

@shuhuayu I see. It sounds that if we keep two rotary embedding impls, we need two corresponding yarn impls.

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 models/utils.py, where one can use configs to choose how to apply RoPE, and what types of scaling to use.

@eous

eous commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor Author

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
sin = freqs.sin() * 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.

@shuhuayu

shuhuayu commented Jan 27, 2026 •

Copy link
Copy Markdown
Contributor

@eous Thanks for the update, LGTM! Please rebase to fix conflict and lint.

eous added 4 commits January 28, 2026 18:07
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
@eous
eous force-pushed the yarn-rope-extensions branch from dc83b57 to 8d76ffc Compare January 29, 2026 02:08
@shuhuayu

Copy link
Copy Markdown
Contributor

@eous There is still a lint issue, can you help fix?

@eous

eous commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Sorry about the delay, ran ufmt and it made a couple of corrections.

@tianyu-l
tianyu-l merged commit 6278dcc into pytorch:main Feb 5, 2026
10 of 11 checks passed
@eous
eous deleted the yarn-rope-extensions branch February 6, 2026 00:55
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
…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.
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants