#include #include #include #include #include #include "test.hpp" template void expect_shape(const migraphx::shape& expected, const migraphx::operation& op, Ts... xs) { migraphx::program p; std::vector shapes{xs...}; std::vector args(shapes.size()); std::transform( shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return p.add_outline(s); }); p.add_instruction(op, args); if(p.get_shape() != expected) { std::cout << "FAILED: Incorrect shape for " << op.name() << ": "; std::cout << expected << " != " << p.get_shape() << std::endl; for(auto&& s : shapes) std::cout << " " << s << std::endl; } } template void throws_shape(const migraphx::operation& op, Ts... xs) { migraphx::program p; std::vector shapes{xs...}; std::vector args(shapes.size()); std::transform( shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return p.add_outline(s); }); bool thrown = test::throws([&] { p.add_instruction(op, args); }); if(not thrown) { std::cout << "FAILED: No error found for " << op.name() << ": "; for(auto&& s : shapes) std::cout << " " << s << std::endl; } } template struct always_false : std::false_type { }; template void throws_shape(const migraphx::shape&, Ts...) { static_assert(always_false{}, "An expected shape should not be passed to throws_shape function"); } TEST_CASE(batch_norm_inference_shape) { const size_t channels = 3; migraphx::shape s{migraphx::shape::float_type, {4, channels, 3, 3}}; migraphx::shape vars{migraphx::shape::float_type, {channels}}; expect_shape(s, migraphx::op::batch_norm_inference{}, s, vars, vars, vars, vars); throws_shape(migraphx::op::batch_norm_inference{}, s); throws_shape(migraphx::op::batch_norm_inference{}, s, vars, vars, vars, vars, vars); } TEST_CASE(convolution_shape) { migraphx::shape output{migraphx::shape::float_type, {4, 4, 1, 1}}; migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}}; migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}}; expect_shape(output, migraphx::op::convolution{}, input, weights); throws_shape(migraphx::op::convolution{}, input); migraphx::shape input2{migraphx::shape::float_type, {3, 3}}; migraphx::shape weights2{migraphx::shape::float_type, {3, 3}}; throws_shape(migraphx::op::convolution{}, input2, weights2); throws_shape(migraphx::op::convolution{}, input2, weights); } TEST_CASE(transpose_shape) { migraphx::shape input{migraphx::shape::float_type, {2, 2}}; migraphx::shape output{migraphx::shape::float_type, {2, 2}, {1, 2}}; expect_shape(input, migraphx::op::transpose{{0, 1}}, input); expect_shape(output, migraphx::op::transpose{{1, 0}}, input); throws_shape(migraphx::op::transpose{{1, 2}}, input); } TEST_CASE(contiguous_shape) { migraphx::shape output{migraphx::shape::float_type, {2, 2}}; migraphx::shape input{migraphx::shape::float_type, {2, 2}, {1, 2}}; expect_shape(output, migraphx::op::contiguous{}, input); throws_shape(migraphx::op::contiguous{}, input, input); migraphx::shape single{migraphx::shape::float_type, {2}}; expect_shape(single, migraphx::op::contiguous{}, single); } TEST_CASE(reshape_shape) { migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}}; for(auto&& new_shape : std::vector>{{8, 3, 1, 1}, {1, 3, 4, 2}, {1, 3, 4, 2}}) { std::vector lens(new_shape.size()); std::copy(new_shape.begin(), new_shape.end(), lens.begin()); migraphx::shape output{migraphx::shape::float_type, lens}; expect_shape(output, migraphx::op::reshape{new_shape}, input); } for(auto&& new_shape : std::vector>{{8, 3, 2, 2}, {1, 3, -1, -1}}) { throws_shape(migraphx::op::reshape{new_shape}, input); } } TEST_CASE(flatten_shape) { migraphx::shape input{migraphx::shape::float_type, {2, 4, 6, 8}}; expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 2 * 4 * 6 * 8}}, migraphx::op::flatten{0}, input); expect_shape( migraphx::shape{migraphx::shape::float_type, {2, 4 * 6 * 8}}, migraphx::op::flatten{1}, input); expect_shape( migraphx::shape{migraphx::shape::float_type, {2 * 4, 6 * 8}}, migraphx::op::flatten{2}, input); expect_shape( migraphx::shape{migraphx::shape::float_type, {2 * 4 * 6, 8}}, migraphx::op::flatten{3}, input); expect_shape(migraphx::shape{migraphx::shape::float_type, {2 * 4 * 6 * 8, 1}}, migraphx::op::flatten{4}, input); throws_shape(migraphx::op::flatten{5}, input); } TEST_CASE(slice_shape) { migraphx::shape input{migraphx::shape::int32_type, {2, 2, 3}}; expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}}, migraphx::op::slice{{2}, {1}, {3}}, input); expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}}, migraphx::op::slice{{0, 1, 2}, {0, 0, 1}, {2, 2, 3}}, input); expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 1}, {6, 3, 1}}, migraphx::op::slice{{2}, {2}, {10}}, input); } TEST_CASE(multibroadcast) { { std::vector lens{4, 2, 5, 3}; migraphx::shape input{migraphx::shape::float_type, {2, 1, 3}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 3, 0, 1}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 2, 5, 3}; migraphx::shape input{migraphx::shape::float_type, {2, 1, 1}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 1, 0, 0}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 2, 5, 3}; migraphx::shape input{migraphx::shape::float_type, {5, 1}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 0, 1, 0}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 2, 5, 3}; migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {1, 0, 0, 0}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 2, 5, 3}; migraphx::shape input{migraphx::shape::float_type, {3}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 0, 0, 1}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 4, 1, 3}; migraphx::shape input{migraphx::shape::float_type, {4, 1, 3}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 3, 3, 1}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 1, 1, 3}; migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}}; expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {1, 1, 1, 0}}, migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 1, 3}; migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}}; throws_shape(migraphx::op::multibroadcast{lens}, input); } { std::vector lens{4, 1, 3}; migraphx::shape input{migraphx::shape::float_type, {}}; throws_shape(migraphx::op::multibroadcast{lens}, input); } } int main(int argc, const char* argv[]) { test::run(argc, argv); }