-
Notifications
You must be signed in to change notification settings - Fork 89
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 layout with pointwise op #2886
Changes from 4 commits
69c1554
e590405
d16aafa
7802ec3
38696e7
7676bd8
7695661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,87 @@ TEST_CASE(pointwise_contiguous) | |
EXPECT(p1 == p2); | ||
} | ||
|
||
TEST_CASE(pointwise_layout_convolution) | ||
{ | ||
migraphx::shape s1{migraphx::shape::float_type, {2, 320, 128, 128}}; | ||
migraphx::shape s2{migraphx::shape::float_type, {320, 320, 3, 3}, {2880, 1, 960, 320}}; | ||
migraphx::shape s3{migraphx::shape::float_type, {2, 320, 128, 128}, {5242880, 1, 40960, 320}}; | ||
migraphx::shape s4{migraphx::shape::int8_type, {41943040}}; // workspace for gpu::convolution | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think its a good idea to hardcode the workspace which could change. Maybe its better to run lowering(and possibly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just test for the pass. It will not invoke "compute()". Therefore i think it is okay to have any value for workspace. I can put a comment about that. |
||
|
||
auto create_program = [=]() { | ||
migraphx::program p; | ||
auto* mm = p.get_main_module(); | ||
auto x1 = mm->add_parameter("x1", s1); | ||
auto x2 = mm->add_parameter("x2", s1); | ||
auto weights = mm->add_parameter("weights", s2); | ||
auto alloc = migraphx::make_op("allocate", {{"shape", to_value(s1)}}); | ||
auto alloc_ins = mm->add_instruction(alloc); | ||
auto* pwm = create_pointwise_module( | ||
p, "main:pointwise0", {x1, x2}, [=](auto* pm, const auto& inputs) { | ||
auto mul_ins = pm->add_instruction(migraphx::make_op("mul"), inputs[0], inputs[1]); | ||
return pm->add_instruction(migraphx::make_op("sigmoid"), mul_ins); | ||
}); | ||
auto pw_ins = | ||
mm->add_instruction(make_precompile_op("pointwise"), {x1, x2, alloc_ins}, {pwm}); | ||
|
||
auto alloc_ins2 = | ||
mm->add_instruction(migraphx::make_op("allocate", {{"shape", to_value(s3)}})); | ||
auto layout_op = migraphx::make_op("layout", {{"permutation", {0, 2, 3, 1}}}); | ||
auto layout_ins = mm->add_instruction(make_precompile_op(layout_op), {pw_ins, alloc_ins2}); | ||
auto conv_op = migraphx::make_op("convolution", {{"padding", {1, 1, 1, 1}}}); | ||
auto alloc_ins3 = | ||
mm->add_instruction(migraphx::make_op("allocate", {{"shape", to_value(s4)}})); | ||
auto alloc_ins4 = | ||
mm->add_instruction(migraphx::make_op("allocate", {{"shape", to_value(s3)}})); | ||
auto conv = | ||
mm->add_instruction(migraphx::make_op("gpu::convolution", {{"op", conv_op.to_value()}}), | ||
layout_ins, | ||
weights, | ||
alloc_ins3, | ||
alloc_ins4); | ||
mm->add_return({conv}); | ||
return p; | ||
}; | ||
auto create_fused_program = [=]() { | ||
migraphx::program p; | ||
auto* mm = p.get_main_module(); | ||
auto x1 = mm->add_parameter("x1", s1); | ||
auto x2 = mm->add_parameter("x2", s1); | ||
auto weights = mm->add_parameter("weights", s2); | ||
auto alloc = migraphx::make_op("allocate", {{"shape", to_value(s3)}}); | ||
auto alloc_ins = mm->add_instruction(alloc); | ||
auto* pwm = create_pointwise_module( | ||
p, "main:pointwise0", {x1, x2}, [=](auto* pm, const auto& inputs) { | ||
auto mul_ins = pm->add_instruction(migraphx::make_op("mul"), inputs[0], inputs[1]); | ||
return pm->add_instruction(migraphx::make_op("sigmoid"), mul_ins); | ||
}); | ||
auto pw_op = migraphx::make_op("pointwise"); | ||
auto pre_comp_op = migraphx::make_op( | ||
"gpu::precompile_op", | ||
{{"op", migraphx::to_value(pw_op)}, {"output_shape", migraphx::to_value(s3)}}); | ||
|
||
auto pw_ins = mm->add_instruction(pre_comp_op, {x1, x2, alloc_ins}, {pwm}); | ||
|
||
auto conv_op = migraphx::make_op("convolution", {{"padding", {1, 1, 1, 1}}}); | ||
auto alloc_ins2 = | ||
mm->add_instruction(migraphx::make_op("allocate", {{"shape", to_value(s4)}})); | ||
auto alloc_ins3 = | ||
mm->add_instruction(migraphx::make_op("allocate", {{"shape", to_value(s3)}})); | ||
auto conv = | ||
mm->add_instruction(migraphx::make_op("gpu::convolution", {{"op", conv_op.to_value()}}), | ||
pw_ins, | ||
weights, | ||
alloc_ins2, | ||
alloc_ins3); | ||
mm->add_return({conv}); | ||
return p; | ||
}; | ||
migraphx::program p1 = create_program(); | ||
run_pass(p1); | ||
migraphx::program p2 = create_fused_program(); | ||
EXPECT(p1 == p2); | ||
} | ||
|
||
TEST_CASE(concat_pointwise_contiguous) | ||
{ | ||
migraphx::shape s1 = migraphx::shape::from_permutation( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 "verify_program.hpp" | ||
#include <migraphx/program.hpp> | ||
#include <migraphx/generate.hpp> | ||
#include <migraphx/make_op.hpp> | ||
|
||
template <migraphx::shape::type_t DType> | ||
struct test_pointwise_conv_nhwc : verify_program<test_pointwise_conv_nhwc<DType>> | ||
{ | ||
migraphx::program create_program() const | ||
{ | ||
migraphx::program p; | ||
auto* mm = p.get_main_module(); | ||
auto x = mm->add_parameter("x", {DType, {1, 8, 4, 4}}); | ||
auto w = mm->add_literal(migraphx::generate_literal({DType, {2, 8, 3, 3}}, 1)); | ||
auto v = mm->add_parameter("v", {DType, {2, 8, 3, 3}}); | ||
auto y = mm->add_parameter("y", {DType, {1, 8, 4, 4}}); | ||
auto mul = mm->add_instruction(migraphx::make_op("mul"), x, y); | ||
auto sigmoid = mm->add_instruction(migraphx::make_op("sigmoid"), mul); | ||
auto layout_ins = mm->add_instruction( | ||
migraphx::make_op("layout", {{"permutation", {0, 2, 3, 1}}}), sigmoid); | ||
auto add_ins = mm->add_instruction(migraphx::make_op("add"), w, v); | ||
auto layout_w = mm->add_instruction( | ||
migraphx::make_op("layout", {{"permutation", {0, 2, 3, 1}}}), add_ins); | ||
mm->add_instruction(migraphx::make_op("convolution"), layout_ins, layout_w); | ||
return p; | ||
} | ||
std::string section() const { return "conv"; } | ||
}; | ||
|
||
template struct test_pointwise_conv_nhwc<migraphx::shape::float_type>; | ||
template struct test_pointwise_conv_nhwc<migraphx::shape::fp8e4m3fnuz_type>; |
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.
Maybe this should be named
find_pointwise_layout_contiguous
to make it clear it works for contiguous as well as layout.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.
Done.