Add support for SDPA for OWLViT and OWLv2#40023
Open
mihaidusmanu wants to merge 10 commits into
Open
Conversation
mihaidusmanu
commented
Aug 8, 2025
| outputs_eager = outputs_eager["language_model_outputs"] | ||
| outputs_sdpa = outputs_sdpa["language_model_outputs"] | ||
| key = "hidden_states" if "hidden_states" in outputs_eager else "decoder_hidden_states" | ||
| elif "text_model_output" in outputs_eager and "owl" in model_class.__name__.lower(): |
Author
There was a problem hiding this comment.
Not very happy about this one...
qubvel
reviewed
Aug 8, 2025
qubvel
left a comment
Contributor
There was a problem hiding this comment.
Thanks for working on this @mihaidusmanu, great work!
Comment on lines
+369
to
+392
| def eager_attention_forward( | ||
| module: nn.Module, | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| value: torch.Tensor, | ||
| attention_mask: Optional[torch.Tensor], | ||
| scaling: float, | ||
| dropout: float = 0.0, | ||
| **kwargs, | ||
| ): | ||
| bsz, num_heads, seq_len, head_dim = query.shape | ||
| proj_shape = (bsz * num_heads, -1, head_dim) | ||
| query = query.reshape(proj_shape) | ||
| key = key.reshape(proj_shape) | ||
| value = value.reshape(proj_shape) | ||
|
|
||
| attn_weights = torch.bmm(query, key.transpose(1, 2)) * scaling | ||
|
|
||
| if attn_weights.size() != (bsz * num_heads, seq_len, seq_len): | ||
| raise ValueError( | ||
| f"Attention weights should be of size {(bsz * num_heads, seq_len, seq_len)}, but is {attn_weights.size()}" | ||
| ) | ||
|
|
||
| if attention_mask is not None: |
Contributor
There was a problem hiding this comment.
Can we reuse one of the existing eager_attention_forward functions defined in transformers?
Author
There was a problem hiding this comment.
Going through the code, I think the BLIP2 eager_attention_forward (so SigLIP without fp32 cast on attention weights) should achieve the same thing so I replaced it by that one.
The only thing missing from the BLIP2 version seems to be the following logic.
# For int8 compatibility, sometimes the `attn_probs` are in `fp32`
attn_probs = attn_probs.to(value_states.dtype)
5 tasks
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: owlv2, owlvit |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Add support for SDPA (scaled_dot_product_attention) for efficient attention to OWLViT and OWLv2 models.
The previous code is used in the eager attention implementation. I roughly followed the SigLIP code for inspiration.
Note that we could do a larger refactory to use the is_causal flag, but I tried to stick as close as possible to the original implementation in this first version.
Before submitting
Who can review?
Maybe @amyeroberts @qubvel @ArthurZucker