Repository navigation
Add conversions for 1x1 conv_2d to matmul - #18736
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 |
Max191
left a comment
There was a problem hiding this comment.
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.
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]>
Max191
left a comment
There was a problem hiding this comment.
I think we may not even need this pass anymore. Does it still work if you simply add a case for 1x1 filters here?
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. |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
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).
This reverts commit ade2659. Signed-off-by: Ian Wood <[email protected]>
Max191
left a comment
There was a problem hiding this comment.
Mostly looks good, just one comment and one nit.
Also, I think you can remove the Convert1x1FilterConv2DToMatmul pass from GlobalOptimization/Passes.cpp now (or can be a follow up).
Do we need to look into the dispatch count regression for Unet? It is only 2 dispatches, and the perf seems to have improved, so maybe it is okay. Just make sure to update the golden dispatch counts once it is ready to land.
|
@Max191 I asked Ian to keep the pass itself cause it might be used by plugins. Let's leave the pass as is for now and delete it later if we want to. |
Oh yeah, I didn't mean to remove the whole pass file. I just meant remove it from the pass pipeline in GlobalOptimization. |
Signed-off-by: Ian Wood <[email protected]>
Max191
left a comment
There was a problem hiding this comment.
Looks good! Just make sure to resolve the CI failures before landing (update golden dispatch counts).
Signed-off-by: Ian Wood <[email protected]>
Convert1X1FilterConv2DToMatmul: handle dynamic cases and conversion to `linalg.generic` representing a broadcasted batch matmul. This pass is kept because it is used in plugins. GeneralizeLinalgNamedOps: generalize conv ops to `linalg.generic` ops when possible. Converting more ops to linalg.generic ops allows for better reshape propagation and fusion opportunities. Also, removed Convert1X1FilterConv2DToMatmulPass from global optimization because generalize named ops would have already generalized any convolutions that were possible to convert. Signed-off-by: Ian Wood <[email protected]> Signed-off-by: Elias Joseph <[email protected]>
Convert 1x1 conv_2d to
linalg.matmulops when the HW dimensions are dynamic and convertlinalg.conv_2d_nhwc_hwcfwhen the N dimension is not 1. No change tolinalg.conv_2d_nchw_fchwcurrently (see linked issue for discussion). Matmul is simpler and easier for the compiler to understand, allowing for better optimizations.