diff --git a/src/rewrite_pooling.cpp b/src/rewrite_pooling.cpp index d4b2fa8a2f4..b381a8a73f9 100644 --- a/src/rewrite_pooling.cpp +++ b/src/rewrite_pooling.cpp @@ -43,9 +43,7 @@ void rewrite_pooling::apply(module& m) const continue; if(ins->inputs().empty()) continue; - auto&& s = ins->inputs().front()->get_shape(); - if(not s.standard()) - continue; + auto&& s = ins->inputs().front()->get_shape(); auto&& op = any_cast(ins->get_operator()); if(not std::all_of(op.padding.begin(), op.padding.end(), [](auto i) { return i == 0; })) continue; @@ -54,27 +52,18 @@ void rewrite_pooling::apply(module& m) const auto lens = s.lens(); if(not std::equal(lens.begin() + 2, lens.end(), op.lengths.begin(), op.lengths.end())) continue; - std::int64_t n = s.lens()[0]; - std::int64_t c = s.lens()[1]; - auto reshape = m.insert_instruction( - ins, make_op("reshape", {{"dims", {n * c, -1}}}), ins->inputs().front()); - instruction_ref pooling{}; - + std::vector axes(lens.size() - 2); + std::iota(axes.begin(), axes.end(), 2); // average pooling if(op.mode == op::pooling_mode::average) { - pooling = m.insert_instruction(ins, make_op("reduce_mean", {{"axes", {1}}}), reshape); + m.replace_instruction(ins, make_op("reduce_mean", {{"axes", axes}}), ins->inputs()); } // max pooling else { - pooling = m.insert_instruction(ins, make_op("reduce_max", {{"axes", {1}}}), reshape); + m.replace_instruction(ins, make_op("reduce_max", {{"axes", axes}}), ins->inputs()); } - - std::vector rsp_lens(lens.size(), 1); - rsp_lens[0] = n; - rsp_lens[1] = c; - m.replace_instruction(ins, make_op("reshape", {{"dims", rsp_lens}}), pooling); } } diff --git a/src/simplify_reshapes.cpp b/src/simplify_reshapes.cpp index 08ea498e720..cd27b1157f2 100644 --- a/src/simplify_reshapes.cpp +++ b/src/simplify_reshapes.cpp @@ -122,6 +122,11 @@ struct find_nop_reshapes reshapes.insert("pad"); reshapes.insert("slice"); reshapes.insert("transpose"); + reshapes.insert("reduce_mean"); + reshapes.insert("reduce_max"); + reshapes.insert("reduce_min"); + reshapes.insert("reduce_sum"); + reshapes.insert("reduce_prod"); return match::name(reshapes)(match::same_shape(match::arg(0))); } diff --git a/test/rewrite_pooling_test.cpp b/test/rewrite_pooling_test.cpp index f6d85d9679b..802b26e9f98 100644 --- a/test/rewrite_pooling_test.cpp +++ b/test/rewrite_pooling_test.cpp @@ -62,11 +62,8 @@ TEST_CASE(rewrite_pooling_test) auto opt_program = [&](const migraphx::operation& reduce_op) { migraphx::module m; auto input = m.add_parameter("x", s); - auto rsp = m.add_instruction(migraphx::make_op("reshape", {{"dims", {4, -1}}}), input); - auto rdm = m.add_instruction(reduce_op, rsp); - auto ret = - m.add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 1, 1, 1}}}), rdm); - m.add_return({ret}); + auto rdm = m.add_instruction(reduce_op, input); + m.add_return({rdm}); return m; }; @@ -78,8 +75,9 @@ TEST_CASE(rewrite_pooling_test) }; test_rewrite(migraphx::op::pooling_mode::average, - migraphx::make_op("reduce_mean", {{"axes", {1}}})); - test_rewrite(migraphx::op::pooling_mode::max, migraphx::make_op("reduce_max", {{"axes", {1}}})); + migraphx::make_op("reduce_mean", {{"axes", {2, 3, 4}}})); + test_rewrite(migraphx::op::pooling_mode::max, + migraphx::make_op("reduce_max", {{"axes", {2, 3, 4}}})); } TEST_CASE(rewrite_avepooling_na1_test) diff --git a/test/verify/test_reduce_add.cpp b/test/verify/test_reduce_add.cpp new file mode 100644 index 00000000000..e7c1b56b6c1 --- /dev/null +++ b/test/verify/test_reduce_add.cpp @@ -0,0 +1,48 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 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 +#include +#include +#include + +struct test_reduce_add : verify_program +{ + migraphx::program create_program() const + { + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::float_type, {4, 1000, 2, 2}}; + migraphx::shape bs{migraphx::shape::half_type, {1, 32, 128}}; + auto x = mm->add_parameter("x", s); + auto reduce_mean = + mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x); + auto reduce_max = + mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {2, 3}}}), x); + auto add = mm->add_instruction(migraphx::make_op("add"), reduce_mean, reduce_max); + mm->add_return({add}); + return p; + }; +}; diff --git a/test/verify/test_reduce_noop_add.cpp b/test/verify/test_reduce_noop_add.cpp new file mode 100644 index 00000000000..090c2a737d7 --- /dev/null +++ b/test/verify/test_reduce_noop_add.cpp @@ -0,0 +1,48 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2022 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 +#include +#include +#include + +struct test_reduce_noop_add : verify_program +{ + migraphx::program create_program() const + { + migraphx::program p; + auto* mm = p.get_main_module(); + migraphx::shape s{migraphx::shape::float_type, {4, 1000, 1, 1}}; + migraphx::shape bs{migraphx::shape::half_type, {1, 32, 128}}; + auto x = mm->add_parameter("x", s); + auto reduce_mean = + mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x); + auto reduce_max = + mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {2, 3}}}), x); + auto add = mm->add_instruction(migraphx::make_op("add"), reduce_mean, reduce_max); + mm->add_return({add}); + return p; + }; +};