Repository navigation
Keep native RoPE scaling when extending context; carry rope_theta for linear - #7028
Conversation
… linear When max_seq_length exceeds a model's native window, the loader overwrote the model's rope_scaling with linear scaling. For models that already ship a scaled RoPE (llama3/yarn/longrope) that is far worse for long context, and on transformers v5 the linear dict omitted rope_theta (v5 keeps it under rope_parameters), so the rotary base fell back to 10000 and broke past ~8K tokens. Keep the native scaling and just widen the window; only synthesize linear for plain-RoPE models, and carry rope_theta so v5 keeps the real base.
|
@codex review |
1 similar comment
|
@codex review |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the _extended_rope_scaling helper function to handle RoPE scaling when extending a model's sequence length beyond its native window. It ensures that native scaled RoPE (such as llama3, yarn, or longrope) is preserved rather than being overwritten with linear scaling, and carries over rope_theta for plain RoPE to prevent transformers v5 from falling back to a default base value. Additionally, a unit test has been added to verify this behavior. There are no review comments, so no feedback is provided.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5f4e72dcf4
ℹ️ 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".
| "factor": factor, | ||
| "rope_theta": _get_rope_theta(config), | ||
| }, existing_type | ||
| return None, existing_type |
There was a problem hiding this comment.
Keep unsupported native RoPE types loadable
When the existing config is yarn (or another non-linear/llama3/longrope type), this returns None, so the loader preserves that native rope_scaling whenever max_seq_length exceeds the config window. However the patched attention constructor in unsloth/models/_utils.py::patch_llama_rope_scaling only handles linear, llama3, and longrope before raising ValueError, so a YaRN Llama checkpoint that previously loaded via the linear override now fails during model construction. Either keep the override for types the patcher cannot instantiate, or add matching native support there first.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch. Fixed in a3f6d17: _extended_rope_scaling now preserves only native llama3; yarn (and any type patch_llama_rope_scaling cannot rebuild) falls back to the linear override, so model construction no longer raises. The linear fallback still carries rope_theta.
| "factor": factor, | ||
| "rope_theta": _get_rope_theta(config), | ||
| }, existing_type | ||
| return None, existing_type |
There was a problem hiding this comment.
Don’t keep nested-only longrope configs
When a Llama longrope config stores original_max_position_embeddings only inside rope_scaling/rope_parameters, preserving the native scaling here sends it into the patched longrope constructor path, which reads self.config.original_max_position_embeddings in unsloth/models/_utils.py::patch_llama_rope_scaling instead of the nested value. Extending those checkpoints past their config window now raises AttributeError, whereas the previous linear override avoided that path; either copy the original length onto the config or keep linear until the longrope branch reads the nested key.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch. Fixed in a3f6d17: only llama3 is kept native now, so a longrope config falls back to the linear override and never reaches the longrope constructor path that reads a top-level original_max_position_embeddings.
…ack otherwise The patched attention constructor (patch_llama_rope_scaling) rebuilds only linear, llama3 and longrope and its longrope branch reads a top-level original_max_position_embeddings, so preserving yarn or a nested-only longrope config would raise during construction on transformers <= 4.47.1. Keep only llama3 native; yarn/longrope/other types fall back to the linear override, still carrying rope_theta.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
Summary
When
max_seq_lengthexceeds a model's native context window, the loader replaced the model'srope_scalingwith kaiokendev linear scaling. That has two problems:llama3/yarn/longrope), overwriting it with linear is far worse for long context. Llama-3.1'sllama3scaling extends gracefully well past its trained window, while linear collapses.{"type": "linear", "factor": F}carried norope_theta. On transformers v5 (which storesrope_thetaunderrope_parameters) this replaces the whole block and drops the real theta, so the rotary base falls back to10000and RoPE breaks past a few thousand tokens.This keeps a model's native scaled RoPE and just widens the window. Linear is synthesized only for plain RoPE models, and it now carries
rope_thetaso v5 keeps the real base.Repro
Loading
unsloth/Llama-3.1-8B(native131072,llama3factor 8, theta500000) atmax_seq_length = 262144and measuring the perplexity of one fixed 2048 token passage placed at increasing absolute positions.Before, with the linear override:
On v5 the base drops to
10000(the load logsCould not apply RoPE scaling 'linear' from config ... falling back to unscaled RoPE).After, keeping native
llama3and widening the window:Extension method compared directly at 256K (same passage, correct base
500000, only the rotaryinv_freqswapped):Native
llama3stays flat out to 257K (2x the trained window), while linear and yarn both blow up. Plain RoPE models (for exampleunsloth/Qwen2.5-0.5B, theta1000000) now extend withrotary.base = 1000000instead of10000.Change
_extended_rope_scaling(config, factor)returnsNoneto keep a nativellama3/yarn/longropescaling, else a linear dict that carriesrope_theta. The loader uses it in place of the unconditional linear override.Tests
tests/utils/test_rope_scaling_drift.py::test_extended_rope_scaling_keeps_native_and_carries_theta(CPU): allama3config is kept untouched, and a v5 style plain config gets linear scaling that carriesrope_theta. Full file: 15 passed, 1 skipped.