Repository navigation
[blip_2] Support attn_implementation dispatch - #46401
Conversation
| class Blip2QFormerModel(Blip2PreTrainedModel): | ||
| config: Blip2QFormerConfig | ||
|
|
||
| _supports_attention_backend = False # adds position on attn weights before last matmul |
There was a problem hiding this comment.
have to set it explicitly to True here and in other models
| config: Blip2QFormerConfig | ||
|
|
||
| _supports_attention_backend = False # adds position on attn weights before last matmul | ||
| _supports_flash_attn = False |
There was a problem hiding this comment.
i think we can do FA and flex now, no?
There was a problem hiding this comment.
FA still looks blocked by the QFormer fp32 path, flex does not seem blocked in the same way as FA, but flex fails with attention dropout in training, and QFormer attention recording expects attention weights while flex returns LSE instead. Perhaps we should keep this PR to SDPA content only?👀
| key_layer = self.transpose_for_scores(self.key(hidden_states)) | ||
| value_layer = self.transpose_for_scores(self.value(hidden_states)) | ||
|
|
||
| mixed_query_layer = self.query(hidden_states) | ||
|
|
||
| query_layer = self.transpose_for_scores(mixed_query_layer) | ||
|
|
||
| # Take the dot product between "query" and "key" to get the raw attention scores. | ||
| attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) | ||
|
|
||
| attention_scores = attention_scores / math.sqrt(self.attention_head_size) | ||
|
|
||
| if attention_mask is not None: | ||
| # Apply the attention mask is (precomputed for all layers in BertModel forward() function) | ||
| attention_scores = attention_scores + attention_mask | ||
| current_states = hidden_states |
There was a problem hiding this comment.
ohh nice, i forgot we got rid of those position_embeddings which weren't used by official ckpt
|
For the test case |
vasqu
left a comment
There was a problem hiding this comment.
Just a few smaller comments but overall looks good already
| def save_attn_gradients(self, attn_gradients): | ||
| self.attn_gradients = attn_gradients | ||
|
|
||
| def get_attn_gradients(self): | ||
| return self.attn_gradients | ||
|
|
||
| def save_attention_map(self, attention_map): | ||
| self.attention_map = attention_map | ||
|
|
||
| def get_attention_map(self): | ||
| return self.attention_map | ||
|
|
||
| def transpose_for_scores(self, x): | ||
| new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size) | ||
| x = x.view(*new_x_shape) | ||
| return x.permute(0, 2, 1, 3) |
There was a problem hiding this comment.
Oh god that was ugly 😬 glad to get rid of this
| _supports_attention_backend = True | ||
| _supports_sdpa = True | ||
| _supports_flash_attn = False # Q-Former is kept in fp32, which blocks reliable Flash Attention dispatch. | ||
| _supports_flex_attn = False |
There was a problem hiding this comment.
Flex attention should work no?
There was a problem hiding this comment.
Yes, updated here.
| _supports_attention_backend = False # adds position on attn weights before last matmul | ||
| _supports_attention_backend = True | ||
| _supports_sdpa = True | ||
| _supports_flash_attn = False |
There was a problem hiding this comment.
Lets add comments when why not
There was a problem hiding this comment.
Previously unverified. Verified now, updated the flag and related tests.
| _supports_sdpa = True | ||
| _supports_flash_attn = False | ||
| _supports_sdpa = False | ||
| _supports_flex_attn = False |
Can we force eager attention at load time instead for now? |
Done. |
vasqu
left a comment
There was a problem hiding this comment.
Just some smaller things but overall ready. Biggest thing to fix is imo to properly override _prepare_config_headdim instead of skipping as this affects the whole flex and FA test suite
| self.scale = self.head_dim**-0.5 | ||
| self.is_causal = False | ||
| self.attention_dropout = config.attention_dropout | ||
| self.dropout = config.attention_dropout |
There was a problem hiding this comment.
Ah I was missing this context previously: You named it attention_dropout because of this attribute naming here. This makes more sense then. Let's revert this back to attention dropout then (otherwise we are slightly breaking)
| pass | ||
|
|
||
| @unittest.skip(reason="_prepare_config_headdim breaks the qformer/vision cross-attention hidden-size invariant") | ||
| def test_flex_attention_with_grads(self): |
There was a problem hiding this comment.
Could we fix that with a small override?
| def test_flash_attn_2_inference_equivalence_right_padding(self): | ||
| pass | ||
|
|
||
| @unittest.skip(reason="_prepare_config_headdim breaks the qformer/vision cross-attention hidden-size invariant") |
There was a problem hiding this comment.
same here, I think we should rather override _prepare_config_headdim to account for these models
|
Friendly ping @vasqu |
vasqu
left a comment
There was a problem hiding this comment.
LGTM, just see my last comments but nothing major
| ) | ||
|
|
||
| if encoder_attention_mask is not None: | ||
| query_embedding_output = embedding_output[:, :query_length, :] if query_length > 0 else embedding_output |
There was a problem hiding this comment.
Oh where did you find this issue arising? Ig this is the issue potentially on cross attn?
There was a problem hiding this comment.
Yes, it's on the cross-attention path. The Q-Former's cross-attention queries come only from the query tokens, but embedding_output also contains the text tokens. create_bidirectional_mask infers the query dimension from inputs_embeds, so passing the full embedding_output produced a mask whose query dim didn't match the actual cross-attention query length. I found it in the error of the test case.
There was a problem hiding this comment.
Comments added.
| @require_bitsandbytes | ||
| @pytest.mark.flash_attn_test | ||
| @slow | ||
| def test_flash_attn_2_fp32_ln(self): |
There was a problem hiding this comment.
Can we add small comments for the overrides as to why?
|
[For maintainers] Suggested jobs to run (before merge) run-slow: blip_2, instructblip, instructblipvideo |
|
run-slow: blip_2, instructblip, instructblipvideo |
|
Just checking with slow CI but merging afterwards 🤗 |
|
This comment contains models: ["models/blip_2", "models/instructblip", "models/instructblipvideo"] |
|
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. |
* [blip_2] Support attn_implementation=sdpa dispatch * Refine * Update * Doc * CI check-repository-consistency * Revert self.attention_dropout * override test cases * rename func * Add comments
What does this PR do?
As per the title.