Skip to content

perf: avoid recomputing rotary_emb for each layer in some Google and ModernBERT models#45555

Open
casinca wants to merge 2 commits intohuggingface:mainfrom
casinca:gemma3-optim
Open

perf: avoid recomputing rotary_emb for each layer in some Google and ModernBERT models#45555
casinca wants to merge 2 commits intohuggingface:mainfrom
casinca:gemma3-optim

Conversation

@casinca
Copy link
Copy Markdown
Contributor

@casinca casinca commented Apr 21, 2026

What does this PR do?

Following #45144 (comment)

tl;dr: self.rotary_emb was called N_layers times but only 2 calls are needed (full attn and swa) other calls were overwritting the same dict keys, which was inefficient.
This now gives a little boost depending on config.layer_types size, with the 4B model inference can be ~10% faster (see img below for a benchmark with 300 toks)

See picture below (of the comment #45144 (comment)) for details (comment link will not be directly available after resolve and will just point to the linked PR)

Click to expand image image

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • 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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

It was discussed with @vasqu and also @zucchini-nlp.
I kept Raushan suggestion with just set() (but can also do self.rotary_emb.layer_types it's a minor detail either way.)

@casinca
Copy link
Copy Markdown
Contributor Author

casinca commented Apr 21, 2026

I checked gemma4, they are correctly doing:

self.unique_layer_types = set(self.config.layer_types)

But I'll check if some other Google models are also doing it the unoptimized way

Copy link
Copy Markdown
Contributor

@vasqu vasqu left a comment

Choose a reason for hiding this comment

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

Careful approval, just checking with run-slow + update us if you find other models :D

@vasqu
Copy link
Copy Markdown
Contributor

vasqu commented Apr 21, 2026

run-slow: gemma3

@github-actions
Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/gemma3"]
quantizations: []

@casinca
Copy link
Copy Markdown
Contributor Author

casinca commented Apr 21, 2026

Careful approval, just checking with run-slow + update us if you find other models :D

Yep, 4 other models and upon looking, it seems safe to extend
image

Since it's 1 LoC change, I believe it's not worth extra PR noise, I can broaden the changes here + update title? Let me know what you'd prefer, thanks.

@vasqu
Copy link
Copy Markdown
Contributor

vasqu commented Apr 21, 2026

Yes sounds good! Can you wait for the slow tests to return results before pushing? Other than that, feel free to add all of them here + lets adjust the PR title

@github-actions
Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 6adc6aa0 workflow commit (merge commit)
PR 67620c54 branch commit (from PR)
main 85099df9 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@casinca casinca changed the title perf(gemma3): Avoid recomputing rotary_emb for each layer perf: avoid recomputing rotary_emb for each layer in some Google and ModernBERT models Apr 21, 2026
@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: gemma3, gemma3n, modernbert, modernbert_decoder, t5gemma2

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.

2 participants