-
Notifications
You must be signed in to change notification settings - Fork 87
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
Rewrite 1x1 convolutions to gemm #3568
Open
pfultz2
wants to merge
19
commits into
develop
Choose a base branch
from
rewrite-dot
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−3
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b833e45
Add rewrite_dot
pfultz2 3acd4ea
Format
pfultz2 baaa8e1
Add env var to enable
pfultz2 37c45cc
Merge branch 'develop' into rewrite-dot
pfultz2 47048e8
Use batch gemm
pfultz2 412e1e5
Merge branch 'develop' into rewrite-dot
pfultz2 d54ae5d
Support nchw layout as well
pfultz2 2f6e052
Format
pfultz2 e9fd119
Merge branch 'develop' into rewrite-dot
pfultz2 81cb444
Use different heuristic
pfultz2 a1540d0
Adjust threshold
pfultz2 7b8978f
Add unit tests
pfultz2 84a941c
Format
pfultz2 afe621f
Merge branch 'develop' into rewrite-dot
CharlieL7 912752c
Merge branch 'develop' into rewrite-dot
pfultz2 7f35c2a
Skip group conv for now
pfultz2 5988a94
Merge branch 'develop' into rewrite-dot
causten 7bceaa4
Merge branch 'develop' into rewrite-dot
causten 0374a01
Merge branch 'develop' into rewrite-dot
lakhinderwalia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef MIGRAPHX_GUARD_MIGRAPHX_REWRITE_DOT_HPP | ||
#define MIGRAPHX_GUARD_MIGRAPHX_REWRITE_DOT_HPP | ||
|
||
#include <migraphx/config.hpp> | ||
#include <string> | ||
|
||
namespace migraphx { | ||
inline namespace MIGRAPHX_INLINE_NS { | ||
|
||
struct module; | ||
|
||
struct MIGRAPHX_EXPORT rewrite_dot | ||
{ | ||
std::string name() const { return "rewrite_dot"; } | ||
void apply(module& m) const; | ||
}; | ||
|
||
} // namespace MIGRAPHX_INLINE_NS | ||
} // namespace migraphx | ||
#endif // MIGRAPHX_GUARD_MIGRAPHX_REWRITE_DOT_HPP |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
*/ | ||
#include <migraphx/rewrite_dot.hpp> | ||
#include <migraphx/module.hpp> | ||
#include <migraphx/matcher.hpp> | ||
#include <migraphx/make_op.hpp> | ||
#include <migraphx/permutation.hpp> | ||
|
||
namespace migraphx { | ||
inline namespace MIGRAPHX_INLINE_NS { | ||
|
||
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DISABLE_REWRITE_DOT); | ||
|
||
namespace { | ||
|
||
MIGRAPHX_PRED_MATCHER(conv_1x1, instruction_ref ins) | ||
{ | ||
if(ins->name() != "convolution") | ||
return false; | ||
auto v = ins->get_operator().to_value(); | ||
if(v.at("group").to<int>() != 1) | ||
return false; | ||
if(not all_of(v.at("stride"), [](const value& x) { return x.to<std::size_t>() == 1; })) | ||
return false; | ||
if(not all_of(v.at("padding"), [](const value& x) { return x.to<std::size_t>() == 0; })) | ||
return false; | ||
if(not all_of(v.at("dilation"), [](const value& x) { return x.to<std::size_t>() == 1; })) | ||
return false; | ||
auto w = ins->inputs().at(1)->get_shape(); | ||
return std::all_of(w.lens().begin() + 2, w.lens().end(), [](std::size_t i) { return i == 1; }); | ||
} | ||
|
||
struct find_1x1_convolution | ||
{ | ||
auto matcher() const { return conv_1x1(match::arg(1)(match::is_constant())); } | ||
|
||
void apply(module& m, const match::matcher_result& r) const | ||
{ | ||
auto ins = r.result; | ||
|
||
auto input = ins->inputs().front(); | ||
auto weights = ins->inputs().back(); | ||
|
||
std::vector<int64_t> sq_axes(ins->get_shape().ndim() - 2); | ||
std::iota(sq_axes.begin(), sq_axes.end(), 2); | ||
auto sq_weights = | ||
m.insert_instruction(ins, make_op("squeeze", {{"axes", sq_axes}}), weights); | ||
|
||
if(ins->get_shape().transposed()) | ||
{ | ||
std::vector<int64_t> aperm(ins->get_shape().ndim()); | ||
std::iota(aperm.begin(), aperm.end(), 0); | ||
std::rotate(aperm.begin() + 1, aperm.begin() + 2, aperm.end()); | ||
auto a_mat = | ||
m.insert_instruction(ins, make_op("transpose", {{"permutation", aperm}}), input); | ||
|
||
auto transpose = m.insert_instruction( | ||
ins, make_op("transpose", {{"permutation", {1, 0}}}), sq_weights); | ||
auto b_lens = a_mat->get_shape().lens(); | ||
copy(transpose->get_shape().lens(), b_lens.end() - 2); | ||
auto b_mat = m.insert_instruction( | ||
ins, make_op("multibroadcast", {{"out_lens", b_lens}}), transpose); | ||
|
||
auto dot = m.insert_instruction(ins, make_op("dot"), a_mat, b_mat); | ||
m.replace_instruction( | ||
ins, make_op("transpose", {{"permutation", invert_permutation(aperm)}}), dot); | ||
} | ||
else | ||
{ | ||
auto batch_dim = ins->get_shape().lens().front(); | ||
auto m_dim = std::accumulate(input->get_shape().lens().begin() + 2, | ||
input->get_shape().lens().end(), | ||
1, | ||
std::multiplies<>{}); | ||
auto n_dim = weights->get_shape().lens()[0]; | ||
auto k_dim = weights->get_shape().lens()[1]; | ||
auto a_mat = m.insert_instruction( | ||
ins, | ||
make_op("multibroadcast", {{"out_lens", {batch_dim, n_dim, k_dim}}}), | ||
sq_weights); | ||
auto b_mat = m.insert_instruction( | ||
ins, make_op("reshape", {{"dims", {batch_dim, k_dim, m_dim}}}), input); | ||
auto dot = m.insert_instruction(ins, make_op("dot"), a_mat, b_mat); | ||
m.replace_instruction( | ||
ins, make_op("reshape", {{"dims", ins->get_shape().lens()}}), dot); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void rewrite_dot::apply(module& m) const | ||
{ | ||
if(enabled(MIGRAPHX_DISABLE_REWRITE_DOT{})) | ||
return; | ||
match::find_matches(m, find_1x1_convolution{}); | ||
} | ||
|
||
} // namespace MIGRAPHX_INLINE_NS | ||
} // namespace migraphx |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
#include <migraphx/rewrite_dot.hpp> | ||
#include <migraphx/pass_manager.hpp> | ||
#include <migraphx/module.hpp> | ||
#include <migraphx/make_op.hpp> | ||
#include <migraphx/generate.hpp> | ||
#include <migraphx/dead_code_elimination.hpp> | ||
#include <test.hpp> | ||
|
||
void run_pass(migraphx::module& m) | ||
{ | ||
migraphx::run_passes(m, {migraphx::rewrite_dot{}, migraphx::dead_code_elimination{}}); | ||
} | ||
|
||
TEST_CASE(nchw_conv_1x1) | ||
{ | ||
migraphx::shape s1{migraphx::shape::float_type, {64, 128, 28, 28}}; | ||
migraphx::shape s2{migraphx::shape::float_type, {512, 128, 1, 1}}; | ||
migraphx::module m1; | ||
{ | ||
auto x = m1.add_parameter("x", s1); | ||
auto w = m1.add_literal(migraphx::generate_literal(s2)); | ||
auto conv = m1.add_instruction(migraphx::make_op("convolution"), x, w); | ||
m1.add_return({conv}); | ||
} | ||
run_pass(m1); | ||
migraphx::module m2; | ||
{ | ||
auto x = m2.add_parameter("x", s1); | ||
auto w = m2.add_literal(migraphx::generate_literal(s2)); | ||
auto squeeze = m2.add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), w); | ||
auto broadcast = m2.add_instruction( | ||
migraphx::make_op("multibroadcast", {{"out_lens", {64, 512, 128}}}), squeeze); | ||
auto reshape1 = | ||
m2.add_instruction(migraphx::make_op("reshape", {{"dims", {64, 128, 784}}}), x); | ||
auto dot = m2.add_instruction(migraphx::make_op("dot"), broadcast, reshape1); | ||
auto reshape2 = | ||
m2.add_instruction(migraphx::make_op("reshape", {{"dims", {64, 512, 28, 28}}}), dot); | ||
m2.add_return({reshape2}); | ||
} | ||
EXPECT(m1.sort() == m2.sort()); | ||
} | ||
|
||
TEST_CASE(nhwc_conv_1x1) | ||
{ | ||
auto s1 = migraphx::shape::from_permutation( | ||
migraphx::shape::float_type, {64, 128, 28, 28}, {0, 2, 3, 1}); | ||
auto s2 = migraphx::shape::from_permutation( | ||
migraphx::shape::float_type, {512, 128, 1, 1}, {0, 2, 3, 1}); | ||
migraphx::module m1; | ||
{ | ||
auto x = m1.add_parameter("x", s1); | ||
auto w = m1.add_literal(migraphx::generate_literal(s2)); | ||
auto conv = m1.add_instruction(migraphx::make_op("convolution"), x, w); | ||
m1.add_return({conv}); | ||
} | ||
run_pass(m1); | ||
migraphx::module m2; | ||
{ | ||
auto x = m2.add_parameter("x", s1); | ||
auto w = m2.add_literal(migraphx::generate_literal(s2)); | ||
auto squeeze = m2.add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), w); | ||
auto transpose1 = | ||
m2.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), squeeze); | ||
auto broadcast = m2.add_instruction( | ||
migraphx::make_op("multibroadcast", {{"out_lens", {64, 28, 128, 512}}}), transpose1); | ||
auto transpose2 = | ||
m2.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), x); | ||
auto dot = m2.add_instruction(migraphx::make_op("dot"), transpose2, broadcast); | ||
auto transpose3 = m2.add_instruction( | ||
migraphx::make_op("transpose", {{"permutation", {0, 3, 1, 2}}}), dot); | ||
m2.add_return({transpose3}); | ||
} | ||
EXPECT(m1.sort() == m2.sort()); | ||
} | ||
|
||
TEST_CASE(nhwc_group_conv_1x1) | ||
{ | ||
auto s1 = migraphx::shape::from_permutation( | ||
migraphx::shape::float_type, {64, 192, 83, 83}, {0, 2, 3, 1}); | ||
auto s2 = migraphx::shape::from_permutation( | ||
migraphx::shape::float_type, {84, 96, 1, 1}, {0, 2, 3, 1}); | ||
migraphx::module m1; | ||
{ | ||
auto x = m1.add_parameter("x", s1); | ||
auto w = m1.add_literal(migraphx::generate_literal(s2)); | ||
auto conv = m1.add_instruction(migraphx::make_op("convolution", {{"group", 2}}), x, w); | ||
m1.add_return({conv}); | ||
} | ||
migraphx::module m2 = m1; | ||
run_pass(m1); | ||
EXPECT(m1.sort() == m2.sort()); | ||
} | ||
|
||
int main(int argc, const char* argv[]) { test::run(argc, argv); } |
Oops, something went wrong.
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.
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.
Might be worthwhile to have a 1x1x1 test case too since its allowed here to make sure it generalizes to the 3D case