-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Optimize Qwen2VL vision model by precomputing cos/sin embeds before ViT blocks #35837
base: main
Are you sure you want to change the base?
Conversation
cc @zucchini-nlp for vlms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes total sense, thanks! One tiny nit, we'd need to keep backwards compatibility for the vision attention classes and add a small deprecation warning
As an example of how it was in llama:
transformers/src/transformers/models/llama/modeling_llama.py
Lines 347 to 356 in c2820c9
if position_embeddings is None: | |
logger.warning_once( | |
"The attention layers in this model are transitioning from computing the RoPE embeddings internally " | |
"through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " | |
"`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " | |
"removed and `position_embeddings` will be mandatory." | |
) | |
cos, sin = self.rotary_emb(value_states, position_ids) | |
else: | |
cos, sin = position_embeddings |
2e912f3
to
343e47e
Compare
I just rebased and the CI is failing on this check. Is it related to this PR?
|
@li-plus Right, you need to add the changes in a "modular_model.py" file and then running |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for iterating. Left a small comment and we need to make CI green. Approving and after changes feel free to @ ArthurZucker , the core maintainer
For the core maintainer: not sure if we need to keep the signature in functions (apply_rotary_pos_emb_vision
) for BC
343e47e
to
64c2827
Compare
I see. It seems that this change also affects the implementation of Qwen2.5VL since it reuses the modules in Qwen2VL. I need to get a Qwen2.5VL model to ensure it's bug-free. |
@li-plus running |
64c2827
to
8c22089
Compare
Running |
@ArthurZucker Hi, would you take a look at this PR? It's been approved and the CI turns green. |
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. |
What does this PR do?
The current implementation of Qwen2VL recomputes cos/sin embeddings and repeats them on each layer of the ViT model. This is computational inefficient. This PR attempts to precompute the rotary embeddings at the beginning of the ViT forward function to save computation and memory.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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.
@ArthurZucker @amyeroberts @qubvel