eliminate_concat_test.cpp 5.87 KB
Newer Older
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <migraph/eliminate_concat.hpp>
#include <migraph/dead_code_elimination.hpp>
#include <migraph/operators.hpp>
#include <basic_ops.hpp>
#include <test.hpp>

struct concat
{
    concat(std::size_t axis)
    {
        op.axis = axis;
    }
    migraph::op::concat op;
    std::string name() const { return "eliminate_concat::concat"; }
    migraph::shape compute_shape(std::vector<migraph::shape> inputs) const
    {
        return op.compute_shape(inputs);
    }
    migraph::argument
    compute(migraph::context& ctx, const migraph::shape& output_shape, const std::vector<migraph::argument>& args) const
    {
        return {output_shape};
    }
};

struct concat_test_optimization 
{
    /// A unique name used to identify the concat optimization
    std::string name() const
    {
        return "eliminate_concat::concat";
    }
    /// A unique name used to identify the allocate operator
    std::string allocate() const
    {
        return "allocate";
    }
    /// Return the lowered concat operator
    migraph::op::concat get_concat(const migraph::operation& op) const
    {
        return migraph::any_cast<concat>(op).op;
    }
};

struct eliminate_concat_target
{
    std::size_t align = 32;
    std::string name() const { return "eliminate_target"; }
    std::vector<migraph::pass> get_passes(migraph::context&) const
    {
        return {migraph::eliminate_concat{concat_test_optimization{}}, migraph::dead_code_elimination{}};
    }
    migraph::context get_context() const { return {}; }
};

struct allocate
{
    migraph::shape s{};
    std::string name() const { return "allocate"; }
    migraph::shape compute_shape(const std::vector<migraph::shape>& inputs) const
    {
        migraph::check_shapes{inputs}.has(0);
        return s;
    }
    migraph::argument compute(migraph::context&,
                              const migraph::shape& output_shape,
                              const std::vector<migraph::argument>&) const
    {
        return {output_shape};
    }
};

struct fred_op
{
    std::string name() const { return "fred_op"; }
    migraph::shape compute_shape(const std::vector<migraph::shape>& inputs) const
    {
        migraph::check_shapes{inputs}.has(1);
        return inputs.at(0);
    }
    migraph::argument compute(migraph::context&,
                              const migraph::shape& output_shape,
                              const std::vector<migraph::argument>& args) const
    {
        return args.at(0);
    }

};

void basic()
{
    auto create_test_program = []() {
        migraph::program p;
        auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1,2,8,8}}});
        auto p1 = p.add_instruction(fred_op{}, a1);
        auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1,3,8,8}}});
        auto p2 = p.add_instruction(fred_op{}, a2);
        auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1,5,8,8}}});
        auto p3 = p.add_instruction(fred_op{}, a3);
        std::size_t axis = 1;
        auto a4 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1,10,8,8}}});
        auto p4 = p.add_instruction(concat(axis), p1, p2, p3, a4);
        return p;            
    };
    auto create_control_program = []() {
        migraph::program p;
        auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1,10,8,8}}});
        auto l1 = p.add_instruction(migraph::op::load{migraph::shape{migraph::shape::float_type, {1,2,8,8}}, 0}, {a1});
        auto p1 = p.add_instruction(fred_op{}, l1);
        auto l2 = p.add_instruction(migraph::op::load{migraph::shape{migraph::shape::float_type, {1,3,8,8}}, 128}, {a1});
        auto p2 = p.add_instruction(fred_op{}, l2);
        auto l3 = p.add_instruction(migraph::op::load{migraph::shape{migraph::shape::float_type, {1,5,8,8}}, 320}, {a1});
        auto p3 = p.add_instruction(fred_op{}, l3);
        auto i1 = p.add_instruction(migraph::op::identity{}, {a1, p1, p2, p3});
        return p;
    };

    auto p1 = create_test_program();
    auto p2 = create_control_program();
    p1.compile(eliminate_concat_target{});

    EXPECT(p1 == p2);
}

void wont_work()
{
    auto create_test_program = []() {
        migraph::program p;
        auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,2,8,8}}});
        auto p1 = p.add_instruction(fred_op{}, a1);
        auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,3,8,8}}});
        auto p2 = p.add_instruction(fred_op{}, a2);
        auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,5,8,8}}});
        auto p3 = p.add_instruction(fred_op{}, a3);
        std::size_t axis = 1;
        auto a4 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,10,8,8}}});
        auto p4 = p.add_instruction(concat(axis), p1, p2, p3, a4);
        return p;            
    };
    auto create_control_program = []() {
        migraph::program p;
        auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,2,8,8}}});
        auto p1 = p.add_instruction(fred_op{}, a1);
        auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,3,8,8}}});
        auto p2 = p.add_instruction(fred_op{}, a2);
        auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,5,8,8}}});
        auto p3 = p.add_instruction(fred_op{}, a3);
        std::size_t axis = 1;
        auto a4 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2,10,8,8}}});
        auto p4 = p.add_instruction(concat(axis), p1, p2, p3, a4);
        return p;            
    };

    auto p1 = create_test_program();
    auto p2 = create_control_program();
    p1.compile(eliminate_concat_target{});

    EXPECT(p1 == p2);
}

int main()
{
    setenv("MIGRAPH_DISABLE_MEMORY_COLORING", "1", 1);
    basic();
    wont_work();
}