Skip to content

feat: support flash attention 2 in qwen2 vl vision blocks #2721

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

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

drbh
Copy link
Collaborator

@drbh drbh commented Nov 4, 2024

This PR adds support for flash attention within the vision blocks of qwen2vl. This improves latency and reduces spikes in memory usage from sdpa attention (helps avoid oom'ing on large images).

running

text-generation-launcher --model-id Qwen/Qwen2-VL-7B-Instruct

on a single L4's with a small model this has a ~5% perf improvement: prev: ~67ms per token current: ~63ms per token.

ps** it's likely the performance improvement scales with the size/number of layers however I have not benchmarked any larger models to measure the difference.

)
attn_output = attn_output.transpose(0, 1)
# calc maximum sequence length for any batch
max_seqlen = torch.max(cu_seqlens[1:] - cu_seqlens[:-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be interesting to compute this value once in the Qwen2VisionModel.forward instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

great point, updated in the latest commit to only calculate the max_seqlen once!

@drbh
Copy link
Collaborator Author

drbh commented Nov 18, 2024

optimistically merging as the max_seqlen comment was addressed, these changes are needed in a in progress PR, and the only failing tests are unrelated to this PR (failing elsewhere and unrelated code).

Watching for regressions and will revet if required!

@drbh drbh merged commit 38cff84 into main Nov 18, 2024
10 of 12 checks passed
@drbh drbh deleted the support-flash-qwen2-vl branch November 18, 2024 17:46
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.

2 participants