-
Notifications
You must be signed in to change notification settings - Fork 599
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
Add conversions for 1x1 conv_2d to matmul #18736
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Can you make the PR description more descriptive when it is ready for review? Here are some examples: https://google.github.io/eng-practices/review/developer/cl-descriptions.html |
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.
Can you support the NCHW case as well? I added a suggestion for how to do it.
Additional thought: It may be a reasonable idea to create a linalg.generic op with expanded H and W dims (so one M/N contraction dim for each of the N, H, and W convolution dims). Having fewer reshapes is probably better at GlobalOptimization level, but I am not sure if it is worth it yet. I think the collapse_shape on the H and W dimensions can typically be propagated through the producers/consumers, so it might not make a difference. You may have a better idea of which form is better for dispatch region formation.
Also, this additional thought is not a requirement for this PR, just an idea to try afterwards.
compiler/src/iree/compiler/GlobalOptimization/Convert1X1FilterConv2DToMatmul.cpp
Outdated
Show resolved
Hide resolved
Use linalg::generalizeNamedOp to generalize 1x1 conv and then remove the unit extent affine symbols from input's affine map. Additionally, clean up the test cases (since expand/extract is introduced). Just check that the affine maps are correct. Signed-off-by: Ian Wood <[email protected]>
b934b78
to
c93d89d
Compare
Signed-off-by: Ian Wood <[email protected]>
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.
I think we may not even need this pass anymore. Does it still work if you simply add a case for 1x1 filters here?
iree/compiler/src/iree/compiler/GlobalOptimization/GeneralizeLinalgNamedOps.cpp
Lines 40 to 48 in c6056d1
if (isa_and_nonnull<linalg::AbsOp, linalg::AddOp, linalg::BroadcastOp, | |
linalg::CeilOp, linalg::CopyOp, linalg::DivOp, | |
linalg::DivUnsignedOp, linalg::ElemwiseBinaryOp, | |
linalg::ElemwiseUnaryOp, linalg::ExpOp, linalg::FloorOp, | |
linalg::LogOp, linalg::MapOp, linalg::MaxOp, | |
linalg::MulOp, linalg::NegFOp, linalg::ReduceOp, | |
linalg::SubOp, linalg::TransposeOp>( | |
linalgOp.getOperation())) { | |
namedOpCandidates.push_back(linalgOp); |
I would expect generalizing the op would be enough, and hopefully the indexing maps will simplify to contraction maps.
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
932c9d2
to
fc4d11f
Compare
The affine_maps dont get simplified to contraction maps until |
Nice, that's what I was hoping for! As long as the unit dims are getting folded then it should be good. |
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.
I think changes to generalize is fine, but maybe keep the conv-> matmul conversion pass around (doesnt have to be used in the global optimization pass pipeline).
Convert 1x1 conv_2d to
linalg.matmul
ops when the HW dimensions are dynamic and convertlinalg.conv_2d_nhwc_hwcf
when the N dimension is not 1. No change tolinalg.conv_2d_nchw_fchw
currently (see linked issue for discussion). Matmul is simpler and easier for the compiler to understand, allowing for better optimizations.