Skip to content

Fix dynamic RoPE not resetting inv_freq when layer_type is None#46624

Merged
zucchini-nlp merged 3 commits into
huggingface:mainfrom
Incheonkirin:fix-dynamic-rope-reset-none-layer-type
Jun 15, 2026
Merged

Fix dynamic RoPE not resetting inv_freq when layer_type is None#46624
zucchini-nlp merged 3 commits into
huggingface:mainfrom
Incheonkirin:fix-dynamic-rope-reset-none-layer-type

Conversation

@Incheonkirin

Copy link
Copy Markdown
Contributor

What does this PR do?

On the layer_type=None path of dynamic_frequency_update (src/transformers/modeling_rope_utils.py), the cached length is read from self.max_seq_len_cached but written with setattr(self, f"{layer_type}_max_seq_len_cached", ...), which creates a stray None_max_seq_len_cached attribute. self.max_seq_len_cached is therefore never updated after a long sequence, so the reset branch (max_seq_len_cached > self.original_max_seq_len) cannot run when a short sequence follows: the rotary embedding keeps the dynamically-scaled inv_freq instead of restoring the original one.

The other buffer/attribute writes in dynamic_frequency_update already use prefix ("" when layer_type is None, f"{layer_type}_" otherwise). This switches the two cached-length writes to prefix as well, so the write matches the read. The layer_type is not None path is unchanged, since prefix == f"{layer_type}_" there.

Added a regression test that runs a dynamic RoPE embedding through a long sequence and then a short one, checking that the cache length is updated and inv_freq resets.

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@zucchini-nlp

dynamic_frequency_update reads self.max_seq_len_cached but writes the
updated length to f"{layer_type}_max_seq_len_cached" (lines 110, 118).
When layer_type is None this targets a stray None_max_seq_len_cached
attribute, so self.max_seq_len_cached is never updated: the reset branch
never fires and inv_freq stays scaled after a long sequence instead of
resetting for the following short sequences.

Use prefix for both setattr calls so the write matches the read at
line 91. The layer_type is not None path is unchanged. Add a regression
test driving a long then short sequence.
@Incheonkirin

Copy link
Copy Markdown
Contributor Author

Note on the current failing pipelines_torch check: the failed parallel node appears to have stopped before pytest while downloading the shared hub cache:

curl: (35) TLS connect error: error:0A000126:SSL routines::unexpected eof while reading

The following failures are downstream of that (tests_output.txt was never created). The targeted RoPE test itself passes locally, and the other CircleCI test contexts are green.

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch

torch.testing.assert_close(inv_freq, default_inv_freq / factor)
torch.testing.assert_close(inv_freq, EXPECTED_INV_FREQ)

def test_dynamic_rope_resets_after_long_sequence(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks!

@zucchini-nlp zucchini-nlp enabled auto-merge June 15, 2026 09:59
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp zucchini-nlp disabled auto-merge June 15, 2026 13:07
@zucchini-nlp zucchini-nlp enabled auto-merge June 15, 2026 13:07
@zucchini-nlp zucchini-nlp added this pull request to the merge queue Jun 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

Merged via the queue into huggingface:main with commit 63ddf94 Jun 15, 2026
119 of 121 checks passed
@Incheonkirin

Copy link
Copy Markdown
Contributor Author

Thanks @zucchini-nlp! That was a sneaky one. Appreciate the quick review and merge.

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.

3 participants