Skip to content
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

Design of xxxAttention, xxxFlashAttention and xxxSdpaAttention #27988

Closed
ccdv-ai opened this issue Dec 13, 2023 · 2 comments
Closed

Design of xxxAttention, xxxFlashAttention and xxxSdpaAttention #27988

ccdv-ai opened this issue Dec 13, 2023 · 2 comments

Comments

@ccdv-ai
Copy link

ccdv-ai commented Dec 13, 2023

Hey
Following the addition of torch.nn.functional.scaled_dot_product_attention (#26572), there is a lot of deduplicated code between the xxxAttention, xxxFlashAttention2 and xxxSdpaAttention classes. The main differences between the classes lie in the attention computation, the rest being the same (Q, K, V computation, cross attention and cache logic etc...).

Wouldn't it be simpler to offload the attention computation in a new shared file making the modeling files cleaner and simplify the use of these optimizations for older models? This would also ease the addition of new variants of attention in the future if there is any.

@fxmarty
Copy link
Contributor

fxmarty commented Dec 13, 2023

Thank you. It's a tradeoff to have between the "one-model one-file" philosophy (https://huggingface.co/blog/transformers-design-philosophy) and offloading code to other files/classes.

We already kind of deviate from the "one-model one-file" philosophy with the KV cache refactor (https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_attn_mask_utils.py) and attention mask refactor (https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_attn_mask_utils.py), and one can indeed argue that we could do the same for the attention.

Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

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

No branches or pull requests

2 participants