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 MultiHeadAttention(MHA) #24079

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

Conversation

peishenyan
Copy link
Contributor

Description

Adds support for MultiHeadAttention via WebNN matmul, transpose, reshape, and other operations that follow the logic in the MHA subgraph below

 Abbreviatios: B is batch_size, S is sequence_length, W is hidden_size, P is past_sequence_length
               N is number of attention heads, H is head size, and W=N*H, h=Sqrt(H)
    Notes: If the datatype of the inputs (qkv and past kv) is float16, we cast them to float32 to ensure data precision.

                 query     key     value
                   |        |        |
           q_Reshape   k_Reshape   v_Reshape  (shape=B,S,H,N)
                   |        |        |
          q_Transpose  k_Transpose v_Transpose (perm=0,2,1,3)
             \           /           |
              \         /            |
present_key<---\----Concat <---------|----past_key
               |      |              |
               |  opt_k_transpose    |
               \  (0,1,3,2)          |
                \    /               |  past_value
                qk_MatMul            |     /
                     |  scale        |    /
                     |   /           |   /
                  qk_Div           Concat------> present_value
                      |              |
                      |              /
                     Add <----------/---------------attention_bias
                      |            /
                    Softmax       /
                       \         /
                        \       /
                      qkv_MatMul
                             |
                          Transpose (perm=0,2,1,3)
                             |
                          Reshape---(shape=B,P,W)
                             |
                           output

Motivation and Context

@peishenyan peishenyan marked this pull request as draft March 18, 2025 08:20
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