Skip to content

convert _native_multi_head_attention , the fast path of nn.MultiheadAttention - #2899

Open
HuzaifaChaudary wants to merge 2 commits into
apple:mainfrom
HuzaifaChaudary:fix/native-multi-head-attention
Open

HuzaifaChaudary wants to merge 2 commits into
apple:mainfrom
HuzaifaChaudary:fix/native-multi-head-attention

Conversation

@HuzaifaChaudary

Copy link
Copy Markdown

fixes #2311 .

an eval mode nn.MultiheadAttention with batch_first and nothing needing grad runs through aten::_native_multi_head_attention , and there was no converter for it , so any model built on it failed with PyTorch convert function for op '_native_multi_head_attention' not implemented . the only way round it was turning the fast path off in torch .

this adds one . it splits the packed in projection into q , k and v , runs them through the same fused or decomposed sdpa the scaled_dot_product_attention converter uses , and returns the weights too when they are asked for , averaged or per head . the kernel reads its mask as boolean , so any non zero entry is masked out , and the converter does the same .

the tests cover no mask , key padding , attn mask and both , each weights setting , ios18 and older targets , and a flexible batch and sequence . each one checks the traced graph really has the op , because torch skips the fast path when a parameter needs grad . with the converter taken out all 39 fail on the missing op , with it they pass . the sdpa and transformer tests still pass too , 240 in all , on macos arm64 with torch 2.8.0 .

…ttention

an eval mode MultiheadAttention traced with nothing needing grad goes through this op , and it had no converter , so any model built on it failed to convert . the mask is read as boolean the way the kernel reads it , so a non zero entry masks the position out .
…ting

every case checks the traced graph really holds aten::_native_multi_head_attention , since torch skips the fast path when a parameter needs grad . with the converter taken out all 39 fail on the missing op .

This branch has not been deployed

No deployments
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.

Failure tu convert MultiHeadAttention

1 participant