-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
Optimize Qwen2VL vision model by precomputing cos/sin embeds before ViT blocks #35837
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. |
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.
Super super late review! Yeah LGTM and it is welcome!
q = q.float() | ||
k = k.float() | ||
cos = cos.unsqueeze(-2) | ||
sin = sin.unsqueeze(-2) | ||
q_embed = (q * cos) + (rotate_half(q) * sin) | ||
k_embed = (k * cos) + (rotate_half(k) * sin) | ||
q_embed = q_embed.to(orig_q_dtype) | ||
k_embed = k_embed.to(orig_k_dtype) |
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.
q = q.float() | |
k = k.float() | |
cos = cos.unsqueeze(-2) | |
sin = sin.unsqueeze(-2) | |
q_embed = (q * cos) + (rotate_half(q) * sin) | |
k_embed = (k * cos) + (rotate_half(k) * sin) | |
q_embed = q_embed.to(orig_q_dtype) | |
k_embed = k_embed.to(orig_k_dtype) | |
q, k = q.float(), k.float() | |
cos, sin = cos.unsqueeze(-2), sin.unsqueeze(-2) | |
q_embed = (q * cos) + (rotate_half(q) * sin) | |
k_embed = (k * cos) + (rotate_half(k) * sin) | |
q_embed = q_embed.to(orig_q_dtype) | |
k_embed = k_embed.to(orig_k_dtype) |
would just like it to be a tad less verbose!
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! Already fixed.
cos = cos.chunk(2, dim=-1)[0].contiguous() | ||
sin = sin.chunk(2, dim=-1)[0].contiguous() |
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.
why do we need chunk here?
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.
Because they use apply_rotary_emb
provided by flash-attn
when attn impl is FA2. It only needs half of the cos/sin embeddings, i.e. [seq_len, head_dim//2]
, while the naive apply_rotary_pos_emb_vision
for eager/sdpa requires full embeds ([seq_len, head_dim]
). Conventionally we pass full embeddings to this API so I have to slice it for FA2. Do you have an elegant and efficient implementation for this?
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.
nope thanks for explaining!
Could you update to fix the conflicts? 🤗 |
8c22089
to
7990710
Compare
Conflicts are just solved. |
40d625d
to
65b620d
Compare
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!
cos = cos.chunk(2, dim=-1)[0].contiguous() | ||
sin = sin.chunk(2, dim=-1)[0].contiguous() |
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.
nope thanks for explaining!
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