onnxruntime
cf053667 - [webgpu ] Optimize flash attention for Nvidia (#25777)

Commit
358 days ago
[webgpu ] Optimize flash attention for Nvidia (#25777) In the flash attention algorithm, each thread in a subgroup needs to access the same range (0-15) of data in workgroup memory `q_tile` and `v_tile`. If we use `subgroupShuffle`, there will be bank conflicts for `var k_local = k_tile[capped_sg_id][i];` since the sg_size is 32 and thread16~thread31 are accessing the same bank address. To avoid the bank conflicts, we can directly access the same address in workgroup memory by all threads which is a broadcast and well optimized in the NV GPUs. See ~10% improvement for phi4 prefill (1K) in NV RTX 2000 Ada. And as the input gets longer(total_sequence_length), the optimization effect gets better (~12% for 2K). Before ``` Batch size: 1, prompt tokens: 1000, tokens to generate: 128 Prompt processing (time to first token): avg (us): 2.0991e+06 avg (tokens/s): 476.394 p50 (us): 2.08457e+06 stddev (us): 36140.3 n: 5 * 1000 token(s) Token generation: avg (us): 25477.8 avg (tokens/s): 39.2498 p50 (us): 25028.2 stddev (us): 4841.89 n: 635 * 1 token(s) ``` After ``` Batch size: 1, prompt tokens: 1000, tokens to generate: 128 Prompt processing (time to first token): avg (us): 1.91138e+06 avg (tokens/s): 523.183 p50 (us): 1.92379e+06 stddev (us): 44768 n: 5 * 1000 token(s) Token generation: avg (us): 25237.2 avg (tokens/s): 39.624 p50 (us): 24860.9 stddev (us): 4874.52 n: 635 * 1 token(s) ```
Author
Parents
Loading