Skip to content

Conversation

@sanchit-gandhi
Copy link
Contributor

@sanchit-gandhi sanchit-gandhi commented Jul 2, 2024

What does this PR do?

Fixes #31166 (comment). Previously, we were performing three shape operations on the q_proj tensor:

  1. Reshape to (bsz, tgt_len, num_heads, head_dim)
  2. Transpose to (bsz, num_heads, tgt_len, head_dim)
  3. Transpose to (bsz, tgt_len, num_heads, head_dim)

Clearly 2 and 3 are not required! This PR fixes the modelling code accordingly.

@HuggingFaceDocBuilderDev

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.

Copy link
Contributor

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Could you check for similar patterns in the library in FA2, it's possible we're extra transposing all over the place

@sanchit-gandhi sanchit-gandhi changed the title [whisper] remove un-necessary transpose for fa2 attention [modelling] remove un-necessary transpose for fa2 attention Jul 3, 2024
@sanchit-gandhi
Copy link
Contributor Author

sanchit-gandhi commented Jul 3, 2024

The pattern appears in most LLaMA-style attention modules, but here the back-to-back transpose operations are required, since there's a step in between the transpose ops:

  1. Reshape to (bsz, tgt_len, num_heads, head_dim)
  2. Transpose to (bsz, num_heads, tgt_len, head_dim)
  3. Apply rotary pos embeddings (note that this requires the shape produced from 2)
  4. Transpose to (bsz, tgt_len, num_heads, head_dim)

This leaves only two additional models where there are redundant transpose ops: Idefics2 and Jamba, changes for which I've pushed to the PR.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good find!

@sanchit-gandhi sanchit-gandhi merged commit 2782aad into huggingface:main Jul 23, 2024
@sanchit-gandhi sanchit-gandhi deleted the whisper-transpose branch July 23, 2024 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants