op_shape_test.cpp 8.8 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
5
6
7
#include <sstream>
#include "test.hpp"

Paul's avatar
Paul committed
8
template <class... Ts>
Paul's avatar
Paul committed
9
void expect_shape(const migraphx::shape& expected, const migraphx::operation& op, Ts... xs)
10
{
Paul's avatar
Paul committed
11
12
13
    migraphx::program p;
    std::vector<migraphx::shape> shapes{xs...};
    std::vector<migraphx::instruction_ref> args(shapes.size());
Paul's avatar
Paul committed
14
15
    std::transform(
        shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return p.add_outline(s); });
16
    p.add_instruction(op, args);
Paul's avatar
Paul committed
17
18
    if(p.get_shape() != expected)
    {
19
20
        std::cout << "FAILED: Incorrect shape for " << op.name() << ": ";
        std::cout << expected << " != " << p.get_shape() << std::endl;
Paul's avatar
Paul committed
21
        for(auto&& s : shapes)
22
23
24
25
            std::cout << "    " << s << std::endl;
    }
}

Paul's avatar
Paul committed
26
template <class... Ts>
Paul's avatar
Paul committed
27
void throws_shape(const migraphx::operation& op, Ts... xs)
28
{
Paul's avatar
Paul committed
29
30
31
    migraphx::program p;
    std::vector<migraphx::shape> shapes{xs...};
    std::vector<migraphx::instruction_ref> args(shapes.size());
Paul's avatar
Paul committed
32
33
    std::transform(
        shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return p.add_outline(s); });
34
    bool thrown = test::throws([&] { p.add_instruction(op, args); });
Paul's avatar
Paul committed
35
36
    if(not thrown)
    {
37
        std::cout << "FAILED: No error found for " << op.name() << ": ";
Paul's avatar
Paul committed
38
        for(auto&& s : shapes)
39
40
41
42
            std::cout << "    " << s << std::endl;
    }
}

Paul's avatar
Paul committed
43
44
45
46
template <class...>
struct always_false : std::false_type
{
};
47

Paul's avatar
Paul committed
48
template <class... Ts>
Paul's avatar
Paul committed
49
void throws_shape(const migraphx::shape&, Ts...)
50
{
Paul's avatar
Paul committed
51
52
    static_assert(always_false<Ts...>{},
                  "An expected shape should not be passed to throws_shape function");
53
54
}

Paul's avatar
Paul committed
55
TEST_CASE(batch_norm_inference_shape)
56
57
{
    const size_t channels = 3;
Paul's avatar
Paul committed
58
59
60
61
62
    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);
63
64
}

Paul's avatar
Paul committed
65
TEST_CASE(convolution_shape)
66
{
Paul's avatar
Paul committed
67
68
69
70
71
72
73
74
75
76
    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);
77
78
}

Paul's avatar
Paul committed
79
TEST_CASE(transpose_shape)
80
{
Paul's avatar
Paul committed
81
82
83
84
85
    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);
86
87
}

Paul's avatar
Paul committed
88
TEST_CASE(contiguous_shape)
89
{
Paul's avatar
Paul committed
90
91
92
93
    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);
Paul's avatar
Paul committed
94

Paul's avatar
Paul committed
95
96
    migraphx::shape single{migraphx::shape::float_type, {2}};
    expect_shape(single, migraphx::op::contiguous{}, single);
97
98
}

Paul's avatar
Paul committed
99
TEST_CASE(reshape_shape)
100
{
Paul's avatar
Paul committed
101
    migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}};
Paul's avatar
Paul committed
102
103
    for(auto&& new_shape :
        std::vector<std::vector<int64_t>>{{8, 3, 1, 1}, {1, 3, 4, 2}, {1, 3, 4, 2}})
104
105
106
    {
        std::vector<std::size_t> lens(new_shape.size());
        std::copy(new_shape.begin(), new_shape.end(), lens.begin());
Paul's avatar
Paul committed
107
108
        migraphx::shape output{migraphx::shape::float_type, lens};
        expect_shape(output, migraphx::op::reshape{new_shape}, input);
109
110
    }

Paul's avatar
Paul committed
111
    for(auto&& new_shape : std::vector<std::vector<int64_t>>{{8, 3, 2, 2}, {1, 3, -1, -1}})
112
    {
Paul's avatar
Paul committed
113
        throws_shape(migraphx::op::reshape{new_shape}, input);
114
115
116
    }
}

Paul's avatar
Paul committed
117
TEST_CASE(flatten_shape)
118
{
Paul's avatar
Paul committed
119
120
121
    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},
Scott Thornton's avatar
Scott Thornton committed
122
                 input);
Paul's avatar
Paul committed
123
124
125
126
127
128
129
130
131
    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);
Paul's avatar
Paul committed
132
133
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2 * 4 * 6 * 8, 1}},
                 migraphx::op::flatten{4},
Scott Thornton's avatar
Scott Thornton committed
134
                 input);
Paul's avatar
Paul committed
135
    throws_shape(migraphx::op::flatten{5}, input);
136
137
}

Paul's avatar
Paul committed
138
TEST_CASE(slice_shape)
Scott Thornton's avatar
Scott Thornton committed
139
{
Paul's avatar
Paul committed
140
141
142
    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}},
Scott Thornton's avatar
Scott Thornton committed
143
                 input);
Paul's avatar
Paul committed
144
145
    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}},
Scott Thornton's avatar
Scott Thornton committed
146
                 input);
Paul's avatar
Paul committed
147
148
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 1}, {6, 3, 1}},
                 migraphx::op::slice{{2}, {2}, {10}},
Scott Thornton's avatar
Scott Thornton committed
149
150
                 input);
}
Scott Thornton's avatar
Scott Thornton committed
151

wsttiger's avatar
wsttiger committed
152
TEST_CASE(multibroadcast)
Scott Thornton's avatar
Scott Thornton committed
153
154
{
    {
155
        std::vector<std::size_t> lens{4, 2, 5, 3};
Paul's avatar
Paul committed
156
157
158
        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},
159
                     input);
Scott Thornton's avatar
Scott Thornton committed
160
161
    }
    {
162
        std::vector<std::size_t> lens{4, 2, 5, 3};
Paul's avatar
Paul committed
163
164
165
        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},
166
                     input);
Scott Thornton's avatar
Scott Thornton committed
167
168
    }
    {
169
        std::vector<std::size_t> lens{4, 2, 5, 3};
Paul's avatar
Paul committed
170
171
172
        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},
173
                     input);
Scott Thornton's avatar
Scott Thornton committed
174
175
    }
    {
176
        std::vector<std::size_t> lens{4, 2, 5, 3};
Paul's avatar
Paul committed
177
178
179
        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},
180
181
182
183
                     input);
    }
    {
        std::vector<std::size_t> lens{4, 2, 5, 3};
Paul's avatar
Paul committed
184
185
186
        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},
187
188
                     input);
    }
189
190
    {
        std::vector<std::size_t> lens{4, 4, 1, 3};
Paul's avatar
Paul committed
191
192
193
        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},
194
195
                     input);
    }
196
197
    {
        std::vector<std::size_t> lens{4, 1, 1, 3};
Paul's avatar
Paul committed
198
199
200
        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},
201
202
203
204
                     input);
    }
    {
        std::vector<std::size_t> lens{4, 1, 3};
Paul's avatar
Paul committed
205
206
        migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}};
        throws_shape(migraphx::op::multibroadcast{lens}, input);
Scott Thornton's avatar
Scott Thornton committed
207
208
    }
    {
209
        std::vector<std::size_t> lens{4, 1, 3};
Paul's avatar
Paul committed
210
211
        migraphx::shape input{migraphx::shape::float_type, {}};
        throws_shape(migraphx::op::multibroadcast{lens}, input);
Scott Thornton's avatar
Scott Thornton committed
212
213
214
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
215
TEST_CASE(gather)
216
217
218
219
220
221
{
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
        std::size_t axis = 1;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 6, 4, 5}},
Shucai Xiao's avatar
Shucai Xiao committed
222
223
224
                     migraphx::op::gather{axis},
                     input,
                     indices);
225
226
227
228
229
230
231
232
233
234
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
        std::size_t axis = 4;
        throws_shape(migraphx::op::gather{axis}, input, indices);
    }
}

Paul's avatar
Paul committed
235
int main(int argc, const char* argv[]) { test::run(argc, argv); }