-
Notifications
You must be signed in to change notification settings - Fork 31.3k
SDPA for T5 Attention #31167
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
base: main
Are you sure you want to change the base?
SDPA for T5 Attention #31167
Conversation
ArthurZucker
left a comment
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.
Hey! Could you make sure the cis go green!? 🤗
Hi! Im sorry, what is cis? |
|
Hey! It is the integration tests right below this message that are all red! |
|
passed except for the quality |
ArthurZucker
left a comment
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 working on this! Let's try to re-use what we have in other modeling codes to have constant standards 🤗
| def shape(states): | ||
| """projection""" | ||
| return states.view(batch_size, -1, self.n_heads, self.key_value_proj_dim).transpose(1, 2) | ||
|
|
||
| def unshape(states): | ||
| """reshape""" | ||
| return states.transpose(1, 2).contiguous().view(batch_size, -1, self.inner_dim) |
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.
no let's remove these one liners
| def project(hidden_states, proj_layer, key_value_states, past_key_value): | ||
| """projects hidden states correctly to key/query states""" | ||
| if key_value_states is None: | ||
| # self-attn | ||
| # (batch_size, n_heads, seq_length, dim_per_head) | ||
| hidden_states = shape(proj_layer(hidden_states)) | ||
| elif past_key_value is None: | ||
| # cross-attn | ||
| # (batch_size, n_heads, seq_length, dim_per_head) | ||
| hidden_states = shape(proj_layer(key_value_states)) | ||
|
|
||
| if past_key_value is not None: | ||
| if key_value_states is None: | ||
| # self-attn | ||
| # (batch_size, n_heads, key_length, dim_per_head) | ||
| hidden_states = torch.cat([past_key_value, hidden_states], dim=2) | ||
| elif past_key_value.shape[2] != key_value_states.shape[1]: | ||
| # checking that the `sequence_length` of the `past_key_value` is the same as | ||
| # the provided `key_value_states` to support prefix tuning | ||
| # cross-attn | ||
| # (batch_size, n_heads, seq_length, dim_per_head) | ||
| hidden_states = shape(proj_layer(key_value_states)) | ||
| else: | ||
| # cross-attn | ||
| hidden_states = past_key_value | ||
| return hidden_states |
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.
to remove
| # get query states | ||
| query_states = shape(self.q(hidden_states)) # (batch_size, n_heads, seq_length, dim_per_head) | ||
|
|
||
| # get key/value states | ||
| key_states = project( | ||
| hidden_states, self.k, key_value_states, past_key_value[0] if past_key_value is not None else None | ||
| ) | ||
| value_states = project( | ||
| hidden_states, self.v, key_value_states, past_key_value[1] if past_key_value is not None else None | ||
| ) | ||
|
|
||
| if position_bias is None: | ||
| if not self.has_relative_attention_bias: | ||
| position_bias = torch.zeros( | ||
| (1, self.n_heads, real_seq_length, key_length), | ||
| device=query_states.device, dtype=query_states.dtype | ||
| ) | ||
| if self.gradient_checkpointing and self.training: | ||
| position_bias.requires_grad = True | ||
| else: | ||
| position_bias = self.compute_bias(real_seq_length, key_length, device=query_states.device) | ||
|
|
||
| # if key and values are already calculated | ||
| # we want only the last query position bias | ||
| if past_key_value is not None: | ||
| position_bias = position_bias[:, :, -hidden_states.size(1) :, :] | ||
|
|
||
| if mask is not None: | ||
| position_bias = position_bias + mask # (batch_size, n_heads, seq_length, key_length) |
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.
the closer we are from UMT5 or WhisperSDPAAttention, the better! 🤗
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.
noted, i will try to patch it
|
@huseinzol05 @ArthurZucker how are we feeling about these changes now? |
ArthurZucker
left a comment
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.
What does this PR do?
SDPA for T5 Attention