Skip to content

[webgpu] Flash attention for generation - #23808

Merged
Guenther Schmuelling (guschmue) merged 18 commits into
mainfrom
attention_generate_fa
Apr 8, 2025
Merged

[webgpu] Flash attention for generation#23808
Guenther Schmuelling (guschmue) merged 18 commits into
mainfrom
attention_generate_fa

Conversation

@qjia7

@qjia7 Jiajia Qin (qjia7) commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

This PR adds the flash decoding support to optimization the generation speed when the total sequence length is large. Previously, when the total sequence length is big enough, the softmax and softmax * v shaders will become the bottleneck since it only uses limited gpu cores. In this changes, we add the flash decoding support to split the present key/value based on the total sequence length, then do reduce to get the final result.

On NV RTX 2000 Ada, the TPS becomes 41.4 from 34.4 for 1K tokens for phi4 static kv cache
On Meteor Lake, the TPS becomes 19 from 16 for 1K tokens for phi4 static kv cache

Side effect of this PR:
It adds two extra buffers to store 1) metadata (max and exp_sum in each split), 2) the splited qkv results with shape [B, N, split_k, H], which increase the memory size.

TODO:
Ideally, there should only be two shaders, which can also reduce the intermediate memory. The computeQKT can be merged into split shader and do the final softmax adjustment in the reduce shader. However, I meet some issues that when the total sequence length exceeds some value, the result will become garbage. Since I can't resolve it in a short time, leave it in as TODO to fix it in future.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
@guschmue Guenther Schmuelling (guschmue) added the ep:WebGPU ort-web webgpu provider label Feb 26, 2025
1. Only copy the new kv data for static kv cache
2. Add flash decoding for sequence_length = 1
@qjia7 Jiajia Qin (qjia7) changed the title [WIP] Flash attention for generation [webgpu] Flash attention for generation Mar 11, 2025
@qjia7
Jiajia Qin (qjia7) marked this pull request as draft March 19, 2025 10:12
@qjia7
Jiajia Qin (qjia7) marked this pull request as ready for review March 19, 2025 13:26
@guschmue

Copy link
Copy Markdown
Contributor

can you merge with main?

@qjia7

Copy link
Copy Markdown
Contributor Author

can you merge with main?

Done.

This PR is ready for review. Thanks.

Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc

@qjia7 Jiajia Qin (qjia7) left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rename valid_new_present_shape to copy_kv_shape to help understand. Thanks for your suggestion.

Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
@guschmue

Copy link
Copy Markdown
Contributor

can you merge with main?

@qjia7 Jiajia Qin (qjia7) left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

can you merge with main?

Done

Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
Comment thread onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc
@guschmue
Guenther Schmuelling (guschmue) deleted the attention_generate_fa branch April 8, 2025 15:28
zhaoxul-qti pushed a commit to CodeLinaro/onnxruntime that referenced this pull request Apr 17, 2025
This PR adds the flash decoding support to optimization the generation
speed when the total sequence length is large. Previously, when the
total sequence length is big enough, the softmax and softmax * v shaders
will become the bottleneck since it only uses limited gpu cores. In this
changes, we add the flash decoding support to split the present
key/value based on the total sequence length, then do reduce to get the
final result.

On NV RTX 2000 Ada, the TPS becomes 41.4 from 34.4 for 1K tokens for
phi4 static kv cache
On Meteor Lake, the TPS becomes 19 from 16 for 1K tokens for phi4 static
kv cache

Side effect of this PR:
It adds two extra buffers to store 1) metadata (max and exp_sum in each
split), 2) the splited qkv results with shape [B, N, split_k, H], which
increase the memory size.

TODO:
Ideally, there should only be two shaders, which can also reduce the
intermediate memory. The computeQKT can be merged into split shader and
do the final softmax adjustment in the reduce shader. However, I meet
some issues that when the total sequence length exceeds some value, the
result will become garbage. Since I can't resolve it in a short time,
leave it in as TODO to fix it in future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ep:WebGPU ort-web webgpu provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants