Skip to content

Fix YaRN RoPE frequency blending to match Hugging Face - #2428

Open
MohammadHijjawi97 wants to merge 1 commit into
Lightning-AI:mainfrom
MohammadHijjawi97:fix/yarn-rope-ramp-direction
Open

MohammadHijjawi97 wants to merge 1 commit into
Lightning-AI:mainfrom
MohammadHijjawi97:fix/yarn-rope-ramp-direction

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown

What does this PR do?

build_rope_cache applied the YaRN ramp in the wrong direction: high-frequency dimensions were interpolated (divided by factor) and low-frequency dimensions were kept unscaled. YaRN and the Hugging Face implementation do the opposite (inv_freq_extrapolation_factor = 1 - linear_ramp_factor(...)). As a result, any model configured with YaRN through rope_adjustments produced cos/sin tables that differ from Hugging Face by up to 2.1.

This PR also aligns the remaining YaRN details with transformers.modeling_rope_utils._compute_yarn_parameters:

  • default attention factor is 0.1 * ln(factor) + 1 unless both mscale and mscale_all_dim are given (previously 1.0, which matters e.g. for Qwen3 with YaRN)
  • upper correction bound is clamped to n_elem - 1
  • attention_factor and truncate are now passed through from rope_adjustments

tests/test_yarn.py did not catch this because it feeds LitGPT's own cos/sin into the HF block. The new test_rope_yarn in tests/test_rope.py compares the YaRN cos/sin tables and rotated q/k directly with LlamaRotaryEmbedding (Qwen3-style and DeepSeek-V3-style settings). It fails on main and passes with this change.

Measured on Qwen/Qwen3-0.6B

LitGPT vs Hugging Face (fp32, eager attention, CPU), 1024 tokens, YaRN factor=4.0, original_max_position_embeddings=32768 (as recommended on the Qwen3 model card), rope_adjustments=dict(factor=4.0, beta_fast=32.0, beta_slow=1.0, original_max_seq_len=32768):

Before:

[YaRN factor=4] rope cos max|diff|=2.139e+00  logits max|diff|=2.784e+01 mean|diff|=2.305e+00  argmax agreement=33.79%  HF NLL=2.1046  LitGPT NLL=4.3060

After:

[YaRN factor=4] rope cos max|diff|=0.000e+00  logits max|diff|=3.347e-04 mean|diff|=5.948e-06  argmax agreement=100.00%  HF NLL=2.1046  LitGPT NLL=2.1046

Without rope scaling, both before and after: logits max|diff|=2.565e-04, argmax agreement=100.00%.

Note: configs that set only one of mscale / mscale_all_dim now get HF's get_mscale(factor) for cos/sin; the separate self.mscale softmax scaling in attention/MLA is unchanged. tests/test_model.py, test_rope.py, test_yarn.py, test_deepseek_moe.py, test_multihead_latent_attention.py pass locally on CPU (one stablelm test needs network); ruff and codespell clean.

`build_rope_cache` used the YaRN ramp the wrong way round: the
high-frequency dimensions were interpolated (divided by `factor`) and the
low-frequency dimensions were kept unscaled, which is the opposite of
YaRN and of the Hugging Face implementation. With YaRN enabled, the
cos/sin tables differed from Hugging Face by up to 2.1 and model outputs
were badly degraded (Qwen3-0.6B with factor 4: argmax agreement with
Hugging Face 33.79%, NLL 4.31 instead of 2.10).

Also align the remaining YaRN details with the reference:
- the default attention factor is `0.1 * ln(factor) + 1` when `mscale`
  and `mscale_all_dim` are not both given (it was 1.0)
- the upper correction bound is clamped to `n_elem - 1`
- `attention_factor` and `truncate` are passed through from
  `rope_adjustments`

Add a test that compares the YaRN cos/sin tables with Hugging Face.

This branch has not been deployed

No deployments
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.

1 participant