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

[WIP] Upgrade FlashAttention to 2.0 #796

Closed
wants to merge 3 commits into from

Conversation

ghunkins
Copy link

What does this PR do?

This PR updates the symlink to FlashAttention 2.0 released in v1.0.9 of flash-attention. All credit goes to Tridao for the implementation.

According to reported results, this can enable a 2-4x speed-up.

FlashAttention speedup on A100 80GB SXM5 with FP16/BF16

This PR is a WIP and still needs testing and documentation updates. Additionally, the heuristics for choosing the underlying implementation likely needs to be updated per this comment.

Before submitting

  • [Y] Did you have fun?
    • Make sure you had fun coding 🙃
  • [Y] Did you read the contributor guideline?
  • [Y] Was this discussed/approved via a Github issue? (no need for typos, doc improvements)
  • [N] Did you make sure to update the docs?
  • [N] Did you write any new necessary tests?
  • [N] Did you update the changelog? (if needed)

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

@facebook-github-bot
Copy link
Contributor

Hi @ghunkins!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 17, 2023
@ghunkins
Copy link
Author

ghunkins commented Jul 17, 2023

Testing using the diffusers library, but I receive the below error.

import torch
from diffusers import DiffusionPipeline
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp

# load pipe
pipe = DiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
).to('cuda')

# enable xformers with flash attention
pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)

# run the pipe
image = pipe(prompt="a cute cat").images[0]
ValueError: Operator `memory_efficient_attention` does not support inputs:
     query       : shape=(16, 256, 1, 160) (torch.float16)
     key         : shape=(16, 256, 1, 160) (torch.float16)
     value       : shape=(16, 256, 1, 160) (torch.float16)
     attn_bias   : <class 'NoneType'>
     p           : 0.0
`flshattF` is not supported because:
    max(query.shape[-1] != value.shape[-1]) > 128

@ghunkins
Copy link
Author

Closing. I think someone with more context needs eyes on this.

@ghunkins ghunkins closed this Jul 17, 2023
@Birch-san
Copy link

Birch-san commented Jul 21, 2023

Testing using the diffusers library, but I receive the below error.

`flshattF` is not supported because:
    max(query.shape[-1] != value.shape[-1]) > 128

@ghunkins this is probably because SDXL's VAE uses a mid-block whose self-attention is 512-dim (albeit just 1 head). this exceeds the maximum head dim that flash attn 2's forward pass supports (256). nevermind; I see that this is SD1.5, you've already disabled it on the VAE, and this problem happened on a 160-dim layer. that is weird; 160-dim was supposed to be supported. why is it saying the max dim is 128? is that assertion a vestige from flash attn 1 that needs updating, or is flash attn 2 not detected at all?

@ghunkins
Copy link
Author

@Birch-san The xformers team is tackling this, you can see #795 for context! I haven't had time to test yet, but based on the conversation it sounds like the pre-release wheels already support FlashAttention 2.0 on certain GPUs.

pip install xformers --pre

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants