Add fuse_attention pass for torch.onnx SDPA graphs - #321
Open
take-cheeze wants to merge 1 commit into
Open
Conversation
Fuse the sub-graph that `torch.onnx.export` (the TorchScript exporter) emits for `scaled_dot_product_attention` into a single ONNX `Attention` operator (opset 23+). The exported pattern scales Q and the transposed K by `sqrt(scale)`, multiplies them, optionally adds an attention mask, applies Softmax over the last axis and multiplies by V. Anchored on the final `MatMul(Softmax(...), V)`, the pass recognizes this shape and rewrites it to `Attention(Q, K, V[, attn_mask])`: - explicit `scale=` (constant sqrt(scale) factors) -> `scale` attribute set to the product of the two factors - default scale (the `1/sqrt(head_size)` sub-graph torch derives from `Shape(Q)`) -> `scale` left implicit, matching Attention's default - additive / causal masks -> fed through as `attn_mask` Guarded on opset >= 23 (where `Attention` exists) so lower-opset models are left untouched. Tests cover hand-built graphs mirroring the exporter output plus graphs produced by the real torch.onnx exporter. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CHJoCKJ1p5YFm2ZMwdwaWV Signed-off-by: take-cheeze <takechi101010@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fuse the sub-graph that
torch.onnx.export(the TorchScript exporter) emits forscaled_dot_product_attentioninto a single ONNXAttentionoperator (opset 23+).The exported pattern scales Q and the transposed K by
sqrt(scale), multiplies them, optionally adds an attention mask, applies Softmax over the last axis and multiplies by V. Anchored on the finalMatMul(Softmax(...), V), the pass recognizes this shape and rewrites it toAttention(Q, K, V[, attn_mask]):scale=(constant sqrt(scale) factors) ->scaleattribute set to the product of the two factors1/sqrt(head_size)sub-graph torch derives fromShape(Q)) ->scaleleft implicit, matching Attention's defaultattn_maskGuarded on opset >= 23 (where
Attentionexists) so lower-opset models are left untouched. Tests cover hand-built graphs mirroring the exporter output plus graphs produced by the real torch.onnx exporter.Claude-Session: https://claude.ai/code/session_01CHJoCKJ1p5YFm2ZMwdwaWV