-
Notifications
You must be signed in to change notification settings - Fork 33.9k
🚨 [v5] Refactor RoPE for layer types #39847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zucchini-nlp
merged 103 commits into
huggingface:main
from
zucchini-nlp:rope-refactor-version-2
Oct 17, 2025
Merged
Changes from 72 commits
Commits
Show all changes
103 commits
Select commit
Hold shift + click to select a range
30aaa21
update
zucchini-nlp 0e5d07b
batch update model code
zucchini-nlp 0712f62
typos
zucchini-nlp 4fc7355
too many diffs, dump
zucchini-nlp b616240
dump again
zucchini-nlp 06cd2a8
another dump
zucchini-nlp f66ad57
fix copies
zucchini-nlp 7dc077f
make `rope_scaling_dict` self attr
zucchini-nlp 4ac0f18
fix a few more tests
zucchini-nlp 9ad42e9
another update
zucchini-nlp 98944d5
fix a few more tests, hopefully last ones
zucchini-nlp 1213769
fox copies
zucchini-nlp d787da7
a huuuge merge conflict resolved!
zucchini-nlp 00d4b3d
fix copies again
zucchini-nlp f9d4de3
fix newly added models, I hate rebasing on main
zucchini-nlp d695f5a
update config files
zucchini-nlp 303f218
modular files
zucchini-nlp 3229fba
fix rope utils test
zucchini-nlp 1914d82
docstring has to be indented more, why?
zucchini-nlp fccb637
oops forgot to update some modualr files
zucchini-nlp 709c414
copy from doesn't copy decorators?
zucchini-nlp b00d90c
fix overriden test as well
zucchini-nlp c8120cf
add a new test
zucchini-nlp a352362
fix failing tests again
zucchini-nlp 2f54cb3
update docstrings
zucchini-nlp 11edd47
fix phi3
zucchini-nlp 6bc850d
Merge branch 'main' into rope-refactor-version-2
zucchini-nlp 206f03d
fix two models
zucchini-nlp 8a9085f
fix copies
zucchini-nlp 88328d2
forgot to add
zucchini-nlp 5324748
stupid bug from modular conversion
zucchini-nlp 62518de
Merge remote-tracking branch 'upstream/main' into rope-refactor-versi…
zucchini-nlp 01e79a8
fix slow tests
zucchini-nlp 1a4ccc7
update to call rotary emb once per model forward
zucchini-nlp 13d30ad
3K tests failing?!
zucchini-nlp df6a3c5
update
zucchini-nlp 5c129c5
update more models
zucchini-nlp c54bf4a
fix copies
zucchini-nlp 9b2b357
fix the rest of tests hopefully
zucchini-nlp 373a8f3
merge main
zucchini-nlp 54ad1dc
fix after rebase
zucchini-nlp c576edb
fix the rope tests
zucchini-nlp dc9ad8d
fix docs omni
zucchini-nlp 686b1a7
change a bit
zucchini-nlp 182a600
models with layer types
zucchini-nlp fdf6818
why it was deleted?
zucchini-nlp c786413
fix a few tests
zucchini-nlp 722541f
fix last test!
zucchini-nlp 6a7321e
delete extra empty lines
zucchini-nlp 5a9cb98
add a test case
zucchini-nlp 403f56f
more changes
zucchini-nlp a6c4124
fix models
zucchini-nlp 193eb23
typing hint for nested rope params
zucchini-nlp d1eeb42
merge main
zucchini-nlp 235ef33
missed when resolving conflicts
zucchini-nlp 6efc183
delete layer types and fix typo
zucchini-nlp cf7097d
fix copies
zucchini-nlp 234f233
fix copies
zucchini-nlp a425778
update docs text
zucchini-nlp 8886f85
docs
zucchini-nlp 52192d9
huuge update all models
zucchini-nlp 74a4b4f
fix copies
zucchini-nlp 362e88d
rename attr to align with new format
zucchini-nlp f458fd0
delete redundant rope tests
zucchini-nlp a7cf992
trigger ci
zucchini-nlp 05cd1ec
merge main
zucchini-nlp f3172fa
update the case
zucchini-nlp 5739b6e
this is why i hate rebasing
zucchini-nlp ecde27c
maybe fixed?
zucchini-nlp 9ecfa5e
oops
zucchini-nlp 5e37957
now fix?
zucchini-nlp 2b279e0
fix last tests and copies
zucchini-nlp 9617211
merge main
zucchini-nlp 878a933
fix copies?
zucchini-nlp 617f1ae
fix minimax and gemma3n
zucchini-nlp b983014
update typo
zucchini-nlp f7c5043
deprecation end version
zucchini-nlp 07fa630
final fix copies :fingers-crossed:
zucchini-nlp 89beae3
oh my, add the docs in toctree
zucchini-nlp 721de32
oke, this is really the last fix
zucchini-nlp d530a86
kill me please...
zucchini-nlp a5b397b
fix copies and hope that tests won't start failing again
zucchini-nlp c66337c
use rope scaling if saved
zucchini-nlp 1bfc013
fix slow tests
zucchini-nlp baf23d9
fix cwm and unrelated deepseek
zucchini-nlp 3de40c3
Merge branch 'main' into rope-refactor-version-2
zucchini-nlp 87278a9
fix last
zucchini-nlp 9dcd94d
update
zucchini-nlp dca1b94
hope it works now, it took so long
zucchini-nlp fcdff3b
lets keep None for now, I will try to remove after checking tests
zucchini-nlp 9f6d963
some more fixes, i find and replace does not always find all cases
zucchini-nlp ff23a68
last fix of tests
zucchini-nlp e4617dc
Merge branch 'main' (commit b84c0b31) into rope-refactor-version-2
ydshieh 3c955d2
arthur's comment for extra foreward kwargs
zucchini-nlp b4186d1
delete unused code
zucchini-nlp cd17598
Merge branch 'main' into rope-refactor-version-2
zucchini-nlp 717ddc6
fix slow qwen tests
zucchini-nlp 5fdae07
delete layer types from models
zucchini-nlp 51ca43a
faulty modular conversion
zucchini-nlp 080a742
fix qwen omni
zucchini-nlp 1049305
merge main
zucchini-nlp 5f126b7
fix copies and style
zucchini-nlp 1cba3b8
address my comment
zucchini-nlp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <!--Copyright 2020 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
|
|
||
| # Utilities for Rotary Embedding | ||
|
|
||
| This page explains how the Rotary Embedding is computed and applied in Transformers and what types of RoPE are supported. | ||
|
|
||
|
|
||
| ## Overview | ||
|
|
||
| Rotary Position Embeddings are a technique used to inject positional information into attention mechanisms without relying on explicit position encodings. | ||
| Instead of adding position vectors to token embeddings, RoPE rotates query and key vectors in the complex plane according to their positions enabling relative positional awareness and better extrapolation to unseen sequence lengths. | ||
|
|
||
| The Transformers library provides a flexible and extensible implementation of various RoPE types defined in `[`~modeling_rope_utils.ROPE_VALIDATION_FUNCTIONS`]`, including both the default and scaled variants: | ||
|
|
||
| | Rope Type | Description | | ||
| |------------|-------------| | ||
| | `"default"` | Standard rotary embedding as in LLaMA. | | ||
| | `"linear"` | Linear-scaled RoPE which allows longer context windows. | | ||
| | `"dynamic"` | NTK-aware scaling computed by rescaling frequency base (`θ`) for longer context. | | ||
| | `"yarn"` | YaRN scaling variant providing smoother extrapolation and stability. | | ||
| | `"longrope"` | [LongRoPE](https://github.com/microsoft/LongRoPE) scaling as in Phi-2 model series. | | ||
| | `"llama3"` | RoPE scaling as in Llama3.1. | | ||
|
|
||
|
|
||
| # Configuration in Model Configs | ||
|
|
||
| To enable and customize rotary embeddings, add a `rope_scaling` field to your model’s configuration file (`config.json`). This field controls the RoPE behavior across model layers. Note that each RoPE variant defines its own set of expected keys and missing keys will raise an error. See the example below which creates a llama config with default RoPE parameters: | ||
|
|
||
|
|
||
| ```python | ||
| from transformers import LlamaConfig | ||
|
|
||
| config = LlamaConfig() | ||
| config.rope_scaling = { | ||
| "rope_type": "default", # type of RoPE to use | ||
| "rope_theta": 10000.0 # base frequency parameter | ||
| } | ||
|
|
||
| # If we want to apply a scaled RoPE type, we need to pass extra parameters | ||
| config.rope_scaling = { | ||
| "rope_type": "linear", | ||
| "rope_theta": 10000.0, | ||
| "factor": 8.0 # scale factor for context extension | ||
| } | ||
| ``` | ||
|
|
||
| ## Per-Layer-Type RoPE Configuration | ||
|
|
||
| Some models such as Gemma-3 use different layer types with different attention mechanisms, i.e. "full attention" in some blocks and "sliding-window attention" in others. Transformers supports specifying distinct RoPE parameters per layer type for these models. In this case, `rope_scaling` should be a nested dictionary, where top-level keys correspond to `config.layer_types` and values are per-type RoPE parameters. During model initialization, each decoder layer will automatically look up the matching RoPE configuration based on its declared layer type. | ||
|
|
||
|
|
||
| ```python | ||
| from transformers import Gemma3Config | ||
|
|
||
| config = Gemma3Config() | ||
| config.rope_scaling = { | ||
| "full_attention": { | ||
| "rope_type": "dynamic", | ||
| "rope_theta": 1000000.0, | ||
| "factor": 8.0, | ||
| "original_max_position_embeddings": 8096, | ||
| }, | ||
| "sliding_attention": { | ||
| "rope_type": "default", | ||
| "rope_theta": 10000.0, | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # Utilities | ||
|
|
||
| [[autodoc]] RopeParameters | ||
| - __call__ | ||
|
|
||
|
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.