pad_rewrite_test.cpp 2.92 KB
Newer Older
Khalique's avatar
Khalique committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/pad_rewrite.hpp>
#include <migraphx/instruction.hpp>
#include <basic_ops.hpp>
#include <migraphx/operators.hpp>
#include <test.hpp>

struct pad_rewrite_target
{
    std::string name() const { return "pad_rewrite"; }
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
    {
        return {migraphx::pad_rewrite{}, migraphx::dead_code_elimination{}};
    }
    migraphx::context get_context() const { return {}; }
};

migraphx::instruction_ref create_im2col(migraphx::instruction_ref& l_img, size_t channels, migraphx::program& p)
{
    size_t f[2]    = {1, 1};
    std::vector<int32_t> weights(channels * f[0] * f[1]);

    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    return p.add_instruction(migraphx::op::im2col{}, l_img, l_weights);
}

migraphx::instruction_ref create_conv(migraphx::instruction_ref& l_img, size_t channels, migraphx::program& p)
{
    migraphx::shape s_weights{migraphx::shape::int32_type, {4, channels, 3, 3}};
    std::vector<int32_t> weights(4 * channels * 3 * 3);

    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    return p.add_instruction(migraphx::op::convolution{}, l_img, l_weights);
}

TEST_CASE(rewrite_test)
{
    migraphx::program p;

    size_t img_dim[2] = {2, 2};
    size_t channels = 1;
    std::vector<int32_t> input(channels * img_dim[0] * img_dim[1]);
    std::iota(input.begin(), input.end(), 0);

    migraphx::shape s_img{migraphx::shape::int32_type, {1, channels, img_dim[0], img_dim[1]}};
    auto l_img   = p.add_literal(migraphx::literal{s_img, input});
    auto padded_img = p.add_instruction(migraphx::op::pad{{0,0,1,1,0,0,1,1}}, l_img);

    auto l0 = create_im2col(padded_img, channels, p);
    auto l1 = create_conv(padded_img, channels, p);
    auto l2 = p.add_instruction(migraphx::op::pooling{}, padded_img);
    p.add_instruction(migraphx::op::identity{}, l0, l1, l2);

    p.compile(pad_rewrite_target{});
    EXPECT(std::none_of(p.begin(), p.end(), [](const migraphx::instruction& ins) {
        return ins.name() == "pad";
    }));
}

TEST_CASE(rewrite_test_asymmetric)
{
    migraphx::program p;
    size_t img_dim[2] = {2, 2};
    size_t channels = 1;
    std::vector<int32_t> input(channels * img_dim[0] * img_dim[1]);
    std::iota(input.begin(), input.end(), 0);

    migraphx::shape s_img{migraphx::shape::int32_type, {1, channels, img_dim[0], img_dim[1]}};
    auto l_img   = p.add_literal(migraphx::literal{s_img, input});
    auto padded_img = p.add_instruction(migraphx::op::pad{{0,0,0,0,0,0,2,2}}, l_img);

    create_im2col(padded_img, channels, p);

    p.compile(pad_rewrite_target{});
    EXPECT(std::any_of(p.begin(), p.end(), [](const migraphx::instruction& ins) {
        return ins.name() == "pad";
    }));
}

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