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

Fuse MatMul + Mul/Div by constant #487

Merged
merged 1 commit into from
Dec 26, 2024
Merged

Conversation

robertknight
Copy link
Owner

A subgraph of the form Mul(MatMul(Mul(A, c), Mul(B, d)), e) where all of the Muls are optional and c, d and e are constants can be rewritten as MatMul(A, B, alpha = c * d * e) where alpha is the scaling already handled by the C = alpha * AB + beta * C result that GEMM already computes. Such scaling is common in transformers as part of attention operations (SDPA).

The initial implementation only handles two specific cases of this form which have been seen in real models. This should be generalized to all possible cases.

A subgraph of the form `Mul(MatMul(Mul(A, c), Mul(B, d)), e)` where all of the
`Mul`s are optional and c, d and e are constants can be rewritten as `MatMul(A,
B, alpha = c * d * e)` where `alpha` is the scaling already handled by the
`C = alpha * AB + beta * C` result that GEMM already computes. Such
scaling is common in transformers as part of attention operations (SDPA).

The initial implementation only handles two specific cases of this form which
have been seen in real models. This should be generalized to all possible cases.
@robertknight robertknight merged commit 7f74d0c into main Dec 26, 2024
2 checks passed
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