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

[WebNN EP] Support GroupQueryAttention(GQA) #23416

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

peishenyan
Copy link
Contributor

@peishenyan peishenyan commented Jan 17, 2025

Description

Adds support for GroupQueryAttention via WebNN matmul, transpose, reshape, and other operations that follow the logic in the GQA subgraph below.

                 query     key     value
                   |        |        |
           q_Reshape   k_Reshape   v_Reshape  (shape=B,S,H,N)
                   |        |        |
          q_Transpose  k_Transpose v_Transpose
           (0,2,1,3)    (0,2,3,1)   (perm=0,2,1,3)
             \           /           |     past_key
              \         /            |        |
present_key<---\----ScatterND <------|--------+
               |      |              |        |
               |  opt_k_transpose?   |    seqlens_k
               \  (0,1,3,2)          |        |
                \    /               |        +----past_value
                qk_MatMul            |       /
                     |    [B=h]      |      /
                     |   /           |     /
                  qk_Div         ScatterND -----> present_value
                      |              |
                      |              /
                     Add <----------/---------------finfo_min_mask
                      |            /
                    Softmax       /
                       \         /
                        \       /
                      qkv_MatMul
                             |
                          Transpose (perm=0,2,1,3)
                             |
                          Reshape---(shape=B,S,W)
                             |
                           output

 Abbreviatios: 
               B is batch_size, S is sequence_length, W is hidden_size
               N is number of attention heads, H is head size, and W=N*H, h=Sqrt(H)
               B and S could be symbolic. ? means it is optional.
    GQA inputs: query, key value, past_key, past_value, seqlens_k, total_sequence_length
    Notes: If the datatype of the inputs (qkv and past kv) is float16, we cast them to float32 to ensure data precision.

Notes: Now we only support past_sequence_length == total_sequence_length for GQA.

Motivation and Context

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.

1 participant