Skip to content

Commit

Permalink
Make new matcher and one test
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieL7 committed Sep 15, 2023
1 parent aba5cb5 commit d49dd16
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/simplify_dyn_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <migraphx/simplify_dyn_ops.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/literal.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand Down Expand Up @@ -131,10 +132,44 @@ struct find_const_4in_slice
}
};

/**
* Simplify dimensions_of to a literal when the input arugment has a static shape
*/
struct find_static_dimensions_of
{
auto matcher() const
{
return match::name("dimensions_of")(match::arg(0)(match::static_shape()));
}

void apply(module& m, const match::matcher_result& mr) const
{
auto ins = mr.result;
auto input = ins->inputs().at(0);
auto dimensions_of_value = ins->get_operator().to_value();
auto start = dimensions_of_value.at("start").to<std::size_t>();
auto end = dimensions_of_value.at("end").to<std::size_t>();
std::size_t output_ndim = end - start;
std::vector<int64_t> vec_shape(output_ndim);
migraphx::shape s(migraphx::shape::int64_type, {output_ndim});
std::vector<std::size_t> input_lens = input->get_shape().lens();
std::transform(input_lens.begin() + start,
input_lens.begin() + end,
vec_shape.begin(),
[](auto i) { return int64_t(i); });
migraphx::shape output_shape{migraphx::shape::int64_type, {end - start}};
auto lit_ins = m.add_literal(migraphx::literal{output_shape, vec_shape});
m.replace_instruction(ins, lit_ins);
}
};

void simplify_dyn_ops::apply(module& m) const
{
match::find_matches(
m, find_static_2in_broadcasts{}, find_const_3in_slice{}, find_const_4in_slice{});
match::find_matches(m,
find_static_2in_broadcasts{},
find_const_3in_slice{},
find_const_4in_slice{},
find_static_dimensions_of{});
}

} // namespace MIGRAPHX_INLINE_NS
Expand Down
28 changes: 28 additions & 0 deletions test/simplify_dyn_ops_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,32 @@ TEST_CASE(const_slice_4input)
EXPECT(m0 == m1);
}

TEST_CASE(static_dimensions_of)
{
migraphx::module m0;
{
migraphx::shape s{migraphx::shape::float_type, {2, 4, 4}};
m0.add_parameter("data", s);
migraphx::shape lit_shape{migraphx::shape::int64_type, {3}};
;
std::vector<int64_t> lit_data = {2, 4, 4};
auto lit_ins = m0.add_literal(migraphx::literal{lit_shape, lit_data});
m0.add_return({lit_ins});
}

// dead_code_elimination will get rid of atan
migraphx::module m1;
{
migraphx::shape s{migraphx::shape::float_type, {2, 4, 4}};
auto input = m1.add_parameter("data", s);
auto atan_ins = m1.add_instruction(migraphx::make_op("atan"), input);
auto dimensions_of_ins =
m1.add_instruction(migraphx::make_op("dimensions_of", {{"end", 3}}), atan_ins);
m1.add_return({dimensions_of_ins});
}
run_pass(m1);

EXPECT(m0 == m1);
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }

0 comments on commit d49dd16

Please sign in to comment.