Repository navigation
Fix YaRN RoPE frequency blending to match Hugging Face - #2428
Open
MohammadHijjawi97 wants to merge 1 commit into
Open
MohammadHijjawi97 wants to merge 1 commit into
MohammadHijjawi97 wants to merge 1 commit into
Conversation
`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.
MohammadHijjawi97
requested review from
andyland,
k223kim,
lianakoleva and
t-vi
as code owners
November 28, 2026 22:21
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
build_rope_cacheapplied the YaRN ramp in the wrong direction: high-frequency dimensions were interpolated (divided byfactor) 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 throughrope_adjustmentsproduced 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:0.1 * ln(factor) + 1unless bothmscaleandmscale_all_dimare given (previously 1.0, which matters e.g. for Qwen3 with YaRN)n_elem - 1attention_factorandtruncateare now passed through fromrope_adjustmentstests/test_yarn.pydid not catch this because it feeds LitGPT's own cos/sin into the HF block. The newtest_rope_yarnintests/test_rope.pycompares the YaRN cos/sin tables and rotated q/k directly withLlamaRotaryEmbedding(Qwen3-style and DeepSeek-V3-style settings). It fails onmainand 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:
After:
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_dimnow get HF'sget_mscale(factor)for cos/sin; the separateself.mscalesoftmax scaling in attention/MLA is unchanged.tests/test_model.py,test_rope.py,test_yarn.py,test_deepseek_moe.py,test_multihead_latent_attention.pypass locally on CPU (one stablelm test needs network); ruff and codespell clean.