-
Notifications
You must be signed in to change notification settings - Fork 68
Use matmul fwd direclty in autograd for performance #1045
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
base: main
Are you sure you want to change the base?
Conversation
7301838 to
ad73fba
Compare
| # grad_mat1 = grad_out @ mat2.T | ||
| grad_mat1 = matmul(grad_out, mat2.T) | ||
|
|
||
| # grad_mat2 = mat1.T @ grad_out | ||
| grad_mat2 = matmul(mat1.T, grad_out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You only need to compute these if requires_grad is set on the inputs.
| # grad_bias = beta * grad_out | ||
| grad_bias = beta * grad_out | ||
|
|
||
| # grad_mat1 = alpha * (grad_out @ mat2.T) | ||
| grad_mat1 = alpha * matmul(grad_out, mat2.T) | ||
|
|
||
| # grad_mat2 = alpha * (mat1.T @ grad_out) | ||
| grad_mat2 = alpha * matmul(mat1.T, grad_out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This results in extra kernels, you should define an epilogue function to put the scaling into the matmul kernel.
Also same issue as above.
Resolving previous matmul bwd implementation for performance concern. In previous PR #748, the matmul bwd was implemented with a specific kernel via two passes, while we can directly call matmul fwd twice given matmul fwd is fully optimized, as @ngimel pointed out. In this PR the matmul_autograd and addmm_autograd are updated to use two matmul fwds instead of a specific matmul_bwd and addmm_bwd. The benchmark/run.py now only calls these updated bwd.
However, the
@helion.kernelannotation does not allow calling another function(matmul_fwd) within the kernel def, so the original matmul_bwd and addmm_bwd are still preserved only as examples in examples/matmul.py, but they are not actually used in benchmark run.