cpu_ops_test.cpp 87.9 KB
Newer Older
1
2
#include <iostream>
#include <vector>
Paul's avatar
Paul committed
3
4
5
6
7
#include <migraphx/literal.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/cpu/target.hpp>
#include <migraphx/verify.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
8
#include <migraphx/onnx.hpp>
9
#include "test.hpp"
Scott Thornton's avatar
Scott Thornton committed
10

Khalique's avatar
Khalique committed
11
float sigmoid(float x) { return 1 / (1 + expf(-x)); }
Khalique's avatar
Khalique committed
12

Khalique's avatar
Khalique committed
13
float elu(float a, float x) { return x > 0 ? x : a * std::expm1(x); }
Khalique's avatar
Khalique committed
14

Paul's avatar
Paul committed
15
TEST_CASE(slice_test)
Scott Thornton's avatar
Scott Thornton committed
16
{
Scott Thornton's avatar
Scott Thornton committed
17
    {
Paul's avatar
Paul committed
18
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
19
20
        std::vector<int> data(2 * 2 * 3);
        std::iota(data.begin(), data.end(), 0);
Paul's avatar
Paul committed
21
22
23
24
        migraphx::shape s{migraphx::shape::int32_type, {2, 2, 3}};
        auto l0 = p.add_literal(migraphx::literal{s, data});
        p.add_instruction(migraphx::op::slice{{2}, {1}, {3}}, l0);
        migraphx::shape s2{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}};
Scott Thornton's avatar
Scott Thornton committed
25
        EXPECT(p.get_shape() == s2);
Paul's avatar
Paul committed
26
27
        p.compile(migraphx::cpu::target{});
        migraphx::shape sresult{migraphx::shape::int32_type, {2, 2, 2}, {4, 2, 1}};
Scott Thornton's avatar
Scott Thornton committed
28
29
30
31
        auto result           = p.eval({});
        std::vector<int> gold = {1, 2, 4, 5, 7, 8, 10, 11};
        std::vector<int> results_vector(2 * 2 * 2);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
32
        EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
33
34
35
        EXPECT(result.get_shape() == sresult);
    }
    {
Paul's avatar
Paul committed
36
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
37
38
        std::vector<int> data(2 * 2 * 3);
        std::iota(data.begin(), data.end(), 0);
Paul's avatar
Paul committed
39
40
41
42
        migraphx::shape s{migraphx::shape::int32_type, {2, 2, 3}};
        auto l0 = p.add_literal(migraphx::literal{s, data});
        p.add_instruction(migraphx::op::slice{{0, 1, 2}, {0, 0, 0}, {2, 2, 2}}, l0);
        migraphx::shape s2{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}};
Scott Thornton's avatar
Scott Thornton committed
43
        EXPECT(p.get_shape() == s2);
Paul's avatar
Paul committed
44
45
        p.compile(migraphx::cpu::target{});
        migraphx::shape sresult{migraphx::shape::int32_type, {2, 2, 2}, {4, 2, 1}};
Scott Thornton's avatar
Scott Thornton committed
46
47
48
49
        auto result           = p.eval({});
        std::vector<int> gold = {0, 1, 3, 4, 6, 7, 9, 10};
        std::vector<int> results_vector(2 * 2 * 2);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
50
        EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
51
52
        EXPECT(result.get_shape() == sresult);
    }
53
54
}

Paul's avatar
Paul committed
55
TEST_CASE(concat_test)
56
{
Scott Thornton's avatar
Scott Thornton committed
57
    {
Paul's avatar
Paul committed
58
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
59
60
61
62
        std::size_t axis       = 1;
        std::vector<int> data0 = {0, 1, 5, 6};
        std::vector<int> data1 = {2, 3, 4, 7, 8, 9};
        std::vector<int> data2 = {10, 20};
Paul's avatar
Paul committed
63
64
65
66
67
68
69
70
        migraphx::shape s0{migraphx::shape::int32_type, {2, 2}};
        migraphx::shape s1{migraphx::shape::int32_type, {2, 3}};
        migraphx::shape s2{migraphx::shape::int32_type, {2, 1}};
        auto l0 = p.add_literal(migraphx::literal{s0, data0});
        auto l1 = p.add_literal(migraphx::literal{s1, data1});
        auto l2 = p.add_literal(migraphx::literal{s2, data2});
        p.add_instruction(migraphx::op::concat{axis}, l0, l1, l2);
        p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
71
        auto result           = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
72
        std::vector<int> gold = {0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9, 20};
Scott Thornton's avatar
Scott Thornton committed
73
        std::vector<int> results_vector(2 * 6);
Scott Thornton's avatar
Scott Thornton committed
74
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
75
76
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({2, 6})));
Scott Thornton's avatar
Scott Thornton committed
77
        EXPECT(
Paul's avatar
Paul committed
78
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({6, 1})));
Scott Thornton's avatar
Scott Thornton committed
79
80
    }
    {
Paul's avatar
Paul committed
81
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
82
83
84
85
        std::size_t axis       = 0;
        std::vector<int> data0 = {0, 1, 2, 3};
        std::vector<int> data1 = {4, 5, 6, 7, 8, 9};
        std::vector<int> data2 = {10, 11};
Paul's avatar
Paul committed
86
87
88
89
90
91
92
93
        migraphx::shape s0{migraphx::shape::int32_type, {2, 2}};
        migraphx::shape s1{migraphx::shape::int32_type, {3, 2}};
        migraphx::shape s2{migraphx::shape::int32_type, {1, 2}};
        auto l0 = p.add_literal(migraphx::literal{s0, data0});
        auto l1 = p.add_literal(migraphx::literal{s1, data1});
        auto l2 = p.add_literal(migraphx::literal{s2, data2});
        p.add_instruction(migraphx::op::concat{axis}, l0, l1, l2);
        p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
94
        auto result           = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
95
        std::vector<int> gold = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Scott Thornton's avatar
Scott Thornton committed
96
        std::vector<int> results_vector(6 * 2);
Scott Thornton's avatar
Scott Thornton committed
97
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
98
99
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({6, 2})));
Scott Thornton's avatar
Scott Thornton committed
100
        EXPECT(
Paul's avatar
Paul committed
101
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({2, 1})));
Scott Thornton's avatar
Scott Thornton committed
102
    }
103
104
}

105
106
107
108
109
110
111
112
113
114
115
TEST_CASE(gather_test)
{
    {
        migraphx::program p;

        std::vector<float> data(3 * 3);
        std::iota(data.begin(), data.end(), 0.5);
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto a0 = p.add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
Shucai Xiao's avatar
Shucai Xiao committed
116
        auto a1  = p.add_literal(migraphx::literal{s_indices, indices});
117
        int axis = 0;
118
        p.add_instruction(migraphx::op::gather{axis}, a0, a1);
119
120
121
        p.compile(migraphx::cpu::target{});
        auto result = p.eval({});
        std::vector<float> res_data(4 * 5);
122
        std::vector<float> golden = {0.5f, 1.5f, 2.5f, 6.5f, 7.5f, 8.5f};
123
124
125
126
127
128
129
130
131
132
133
134
135
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }

    {
        migraphx::program p;

        std::vector<float> data(3 * 3);
        std::iota(data.begin(), data.end(), 0.5);
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto a0 = p.add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
Shucai Xiao's avatar
Shucai Xiao committed
136
        auto a1  = p.add_literal(migraphx::literal{s_indices, indices});
137
        int axis = 1;
138
        p.add_instruction(migraphx::op::gather{axis}, a0, a1);
139
140
141
        p.compile(migraphx::cpu::target{});
        auto result = p.eval({});
        std::vector<float> res_data(4 * 5);
142
        std::vector<float> golden = {0.5f, 2.5f, 3.5f, 5.5f, 6.5f, 8.5f};
143
144
145
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }
146
147
148
149
150
151
152
153
154
155
156
157

    {
        migraphx::program p;

        std::vector<float> data(3 * 3);
        std::iota(data.begin(), data.end(), 0.5);
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto a0 = p.add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
        auto a1  = p.add_literal(migraphx::literal{s_indices, indices});
        int axis = -1;
158
        p.add_instruction(migraphx::op::gather{axis}, a0, a1);
159
160
161
        p.compile(migraphx::cpu::target{});
        auto result = p.eval({});
        std::vector<float> res_data(4 * 5);
162
        std::vector<float> golden = {0.5f, 2.5f, 3.5f, 5.5f, 6.5f, 8.5f};
163
164
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
Scott Thornton's avatar
Scott Thornton committed
165
    }
166
167
}

Paul's avatar
Paul committed
168
TEST_CASE(squeeze_test)
Scott Thornton's avatar
Scott Thornton committed
169
{
170
    {
Paul's avatar
Paul committed
171
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
172
        std::vector<float> data(4 * 3 * 3);
Paul's avatar
Paul committed
173
174
175
176
177
        migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 3, 1, 3}};
        auto l0 = p.add_literal(migraphx::literal{s1, data});
        p.add_instruction(migraphx::op::squeeze{{1}}, l0);
        p.compile(migraphx::cpu::target{});
178
        auto result = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
179
        EXPECT(result.get_shape() == s2);
180
181
    }
    {
Paul's avatar
Paul committed
182
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
183
        std::vector<float> data(4 * 3 * 3);
Paul's avatar
Paul committed
184
185
186
187
188
        migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
        auto l0 = p.add_literal(migraphx::literal{s1, data});
        p.add_instruction(migraphx::op::squeeze{{3}}, l0);
        p.compile(migraphx::cpu::target{});
189
        auto result = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
190
        EXPECT(result.get_shape() == s2);
191
192
    }
    {
Paul's avatar
Paul committed
193
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
194
        std::vector<float> data(4 * 3 * 3);
Paul's avatar
Paul committed
195
196
197
198
199
        migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 3, 3}};
        auto l0 = p.add_literal(migraphx::literal{s1, data});
        p.add_instruction(migraphx::op::squeeze{}, l0);
        p.compile(migraphx::cpu::target{});
200
        auto result = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
201
        EXPECT(result.get_shape() == s2);
202
203
204
    }
}

Paul's avatar
Paul committed
205
TEST_CASE(unsqueeze_test)
Scott Thornton's avatar
Scott Thornton committed
206
{
207
    {
Paul's avatar
Paul committed
208
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
209
        std::vector<float> data(4 * 3 * 3);
Paul's avatar
Paul committed
210
211
212
213
214
        migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
        auto l0 = p.add_literal(migraphx::literal{s1, data});
        p.add_instruction(migraphx::op::unsqueeze{{1}}, l0);
        p.compile(migraphx::cpu::target{});
215
        auto result = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
216
217
        EXPECT(result.get_shape() == s2);
    }
218
    {
Paul's avatar
Paul committed
219
        migraphx::program p;
Scott Thornton's avatar
Scott Thornton committed
220
        std::vector<float> data(4 * 3 * 3);
Paul's avatar
Paul committed
221
222
223
224
225
        migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 3, 1, 3}};
        auto l0 = p.add_literal(migraphx::literal{s1, data});
        p.add_instruction(migraphx::op::unsqueeze{{2}}, l0);
        p.compile(migraphx::cpu::target{});
226
        auto result = p.eval({});
Scott Thornton's avatar
Scott Thornton committed
227
228
        EXPECT(result.get_shape() == s2);
    }
229
230
}

Paul's avatar
Paul committed
231
TEST_CASE(globalavgpool_test)
232
{
Paul's avatar
Paul committed
233
234
235
    migraphx::program p;
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    auto op    = migraphx::op::pooling{"average"};
236
    auto lens  = s.lens();
Khalique's avatar
Khalique committed
237
    op.lengths = {lens[2], lens[3]};
238
239

    std::vector<float> data{0.3, 0.2, 0.4, 0.1, 0.8, 0.5, 0.9, 0.1, 0.1, 0.7, 0.1, 0.6};
Paul's avatar
Paul committed
240
    auto l0 = p.add_literal(migraphx::literal{s, data});
241
    p.add_instruction(op, l0);
Paul's avatar
Paul committed
242
    p.compile(migraphx::cpu::target{});
243
244
245
246
247
    auto result = p.eval({});

    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.25, 0.575, 0.375};
Paul's avatar
Paul committed
248
    EXPECT(migraphx::verify_range(results_vector, gold));
249
250
}

Paul's avatar
Paul committed
251
TEST_CASE(globalmaxpool_test)
252
{
Paul's avatar
Paul committed
253
254
255
    migraphx::program p;
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    auto op    = migraphx::op::pooling{"max"};
256
    auto lens  = s.lens();
Khalique's avatar
Khalique committed
257
    op.lengths = {lens[2], lens[3]};
258
259

    std::vector<float> data{0.3, 0.2, 0.4, 0.1, 0.8, 0.5, 0.9, 0.1, 0.1, 0.7, 0.1, 0.6};
Paul's avatar
Paul committed
260
    auto l0 = p.add_literal(migraphx::literal{s, data});
261
    p.add_instruction(op, l0);
Paul's avatar
Paul committed
262
    p.compile(migraphx::cpu::target{});
263
264
265
266
267
    auto result = p.eval({});

    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.4, 0.9, 0.7};
Paul's avatar
Paul committed
268
    EXPECT(migraphx::verify_range(results_vector, gold));
269
270
}

Paul's avatar
Paul committed
271
TEST_CASE(im2col_3x3_no_pad_identity_test)
Scott Thornton's avatar
Scott Thornton committed
272
273
274
275
276
277
278
279
280
281
282
283
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {3, 3};
    std::array<std::size_t, 2> padding{{0, 0}};
    std::array<std::size_t, 2> stride{{1, 1}};
    std::array<std::size_t, 2> dilation{{1, 1}};
    std::size_t channels = 1;

    std::vector<int32_t> weights(channels * f[0] * f[1]);
    std::vector<int32_t> input(channels * size[0] * size[1]);
    std::iota(input.begin(), input.end(), 0);

Paul's avatar
Paul committed
284
285
286
287
288
289
290
    migraphx::program p;
    migraphx::shape s_image{migraphx::shape::int32_type, {1, channels, size[0], size[1]}};
    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_image   = p.add_literal(migraphx::literal{s_image, input});
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    p.add_instruction(migraphx::op::im2col{padding, stride, dilation}, l_image, l_weights);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
291
292
293
294
295
296
    auto result = p.eval({});

    std::size_t col_height = (size[0] - f[0] + 2 * padding[0]) / stride[0] + 1;
    std::size_t col_width  = (size[1] - f[1] + 2 * padding[1]) / stride[1] + 1;
    std::vector<float> results_vector(channels * f[0] * f[1] * col_height * col_width);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
297
    EXPECT(migraphx::verify_range(results_vector, input));
Scott Thornton's avatar
Scott Thornton committed
298
299
}

Paul's avatar
Paul committed
300
TEST_CASE(im2col_3x3_no_pad_test)
Scott Thornton's avatar
Scott Thornton committed
301
302
303
304
305
306
307
308
309
310
311
312
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {4, 4};
    std::array<std::size_t, 2> padding{{0, 0}};
    std::array<std::size_t, 2> stride{{1, 1}};
    std::array<std::size_t, 2> dilation{{1, 1}};
    std::size_t channels = 1;

    std::vector<int32_t> weights(channels * f[0] * f[1]);
    std::vector<int32_t> input(channels * size[0] * size[1]);
    std::iota(input.begin(), input.end(), 0);

Paul's avatar
Paul committed
313
314
315
316
317
318
319
    migraphx::program p;
    migraphx::shape s_image{migraphx::shape::int32_type, {1, channels, size[0], size[1]}};
    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_image   = p.add_literal(migraphx::literal{s_image, input});
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    p.add_instruction(migraphx::op::im2col{padding, stride, dilation}, l_image, l_weights);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
320
321
322
323
324
325
326
327
328
    auto result = p.eval({});

    std::vector<int> correct = {0, 1, 2, 4, 5, 6,  8,  9,  10, 1, 2, 3, 5, 6,  7,  9,  10, 11,
                                4, 5, 6, 8, 9, 10, 12, 13, 14, 5, 6, 7, 9, 10, 11, 13, 14, 15};

    std::size_t col_height = (size[0] - f[0] + 2 * padding[0]) / stride[0] + 1;
    std::size_t col_width  = (size[1] - f[1] + 2 * padding[1]) / stride[1] + 1;
    std::vector<float> results_vector(channels * f[0] * f[1] * col_height * col_width);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
329
    EXPECT(migraphx::verify_range(results_vector, correct));
Scott Thornton's avatar
Scott Thornton committed
330
331
}

Paul's avatar
Paul committed
332
TEST_CASE(im2col_3x3_stride_2_no_pad_test)
Scott Thornton's avatar
Scott Thornton committed
333
334
335
336
337
338
339
340
341
342
343
344
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {6, 6};
    std::array<std::size_t, 2> padding{{0, 0}};
    std::array<std::size_t, 2> stride{{2, 2}};
    std::array<std::size_t, 2> dilation{{1, 1}};
    std::size_t channels = 1;

    std::vector<int32_t> weights(channels * f[0] * f[1]);
    std::vector<int32_t> input(channels * size[0] * size[1]);
    std::iota(input.begin(), input.end(), 0);

Paul's avatar
Paul committed
345
346
347
348
349
350
351
    migraphx::program p;
    migraphx::shape s_image{migraphx::shape::int32_type, {1, channels, size[0], size[1]}};
    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_image   = p.add_literal(migraphx::literal{s_image, input});
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    p.add_instruction(migraphx::op::im2col{padding, stride, dilation}, l_image, l_weights);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
352
353
354
355
356
357
358
359
360
361
    auto result = p.eval({});

    std::vector<int> correct = {0,  1,  2,  6,  7,  8,  12, 13, 14, 2,  3,  4,
                                8,  9,  10, 14, 15, 16, 12, 13, 14, 18, 19, 20,
                                24, 25, 26, 14, 15, 16, 20, 21, 22, 26, 27, 28};

    std::size_t col_height = (size[0] - f[0] + 2 * padding[0]) / stride[0] + 1;
    std::size_t col_width  = (size[1] - f[1] + 2 * padding[1]) / stride[1] + 1;
    std::vector<float> results_vector(channels * f[0] * f[1] * col_height * col_width);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
362
    EXPECT(migraphx::verify_range(results_vector, correct));
Scott Thornton's avatar
Scott Thornton committed
363
364
}

Paul's avatar
Paul committed
365
TEST_CASE(im2col_3x3_with_padding_test)
Scott Thornton's avatar
Scott Thornton committed
366
367
368
369
370
371
372
373
374
375
376
377
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {2, 2};
    std::array<std::size_t, 2> padding{{1, 1}};
    std::array<std::size_t, 2> stride{{1, 1}};
    std::array<std::size_t, 2> dilation{{1, 1}};
    std::size_t channels = 1;

    std::vector<int32_t> weights(channels * f[0] * f[1]);
    std::vector<int32_t> input(channels * size[0] * size[1]);
    std::iota(input.begin(), input.end(), 0);

Paul's avatar
Paul committed
378
379
380
381
382
383
384
    migraphx::program p;
    migraphx::shape s_image{migraphx::shape::int32_type, {1, channels, size[0], size[1]}};
    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_image   = p.add_literal(migraphx::literal{s_image, input});
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    p.add_instruction(migraphx::op::im2col{padding, stride, dilation}, l_image, l_weights);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
385
386
387
388
389
390
391
392
393
    auto result = p.eval({});

    std::vector<int> correct = {0, 0, 0, 0, 0, 1, 0, 2, 3, 0, 0, 0, 0, 1, 0, 2, 3, 0,
                                0, 0, 1, 0, 2, 3, 0, 0, 0, 0, 1, 0, 2, 3, 0, 0, 0, 0};

    std::size_t col_height = (size[0] - f[0] + 2 * padding[0]) / stride[0] + 1;
    std::size_t col_width  = (size[1] - f[1] + 2 * padding[1]) / stride[1] + 1;
    std::vector<float> results_vector(channels * f[0] * f[1] * col_height * col_width);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
394
    EXPECT(migraphx::verify_range(results_vector, correct));
Scott Thornton's avatar
Scott Thornton committed
395
396
}

Paul's avatar
Paul committed
397
TEST_CASE(batch_norm_inference_test)
398
{
Paul's avatar
Paul committed
399
    migraphx::program p;
Paul's avatar
Paul committed
400
401
402
403
404
405
    const size_t width       = 2;
    const size_t height      = 2;
    const size_t channels    = 4;
    const size_t batches     = 2;
    const float x_val        = 8.0;
    const float mean_val     = 2.0;
Paul's avatar
Paul committed
406
    const float variance_val = 4.0;
Paul's avatar
Paul committed
407
408
    const float scale_val    = 2.0f;
    const float bias_val     = 1.0f;
Aditya Atluri's avatar
Aditya Atluri committed
409
410
    const float output_val = scale_val * (x_val - mean_val) / (std::sqrt(variance_val)) + bias_val;

Paul's avatar
Paul committed
411
412
    migraphx::shape s{migraphx::shape::float_type, {batches, channels, height, width}};
    migraphx::shape vars{migraphx::shape::float_type, {channels}};
Aditya Atluri's avatar
Aditya Atluri committed
413
414
415
416
417
418
419
420
421
422
423
424
    std::vector<float> x_data(width * height * channels * batches);
    std::vector<float> scale_data(channels);
    std::vector<float> bias_data(channels);
    std::vector<float> mean_data(channels);
    std::vector<float> variance_data(channels);

    std::fill(x_data.begin(), x_data.end(), x_val);
    std::fill(mean_data.begin(), mean_data.end(), mean_val);
    std::fill(variance_data.begin(), variance_data.end(), variance_val);
    std::fill(scale_data.begin(), scale_data.end(), scale_val);
    std::fill(bias_data.begin(), bias_data.end(), bias_val);

Paul's avatar
Paul committed
425
426
427
428
429
    auto x        = p.add_literal(migraphx::literal{s, x_data});
    auto scale    = p.add_literal(migraphx::literal{vars, scale_data});
    auto bias     = p.add_literal(migraphx::literal{vars, bias_data});
    auto mean     = p.add_literal(migraphx::literal{vars, mean_data});
    auto variance = p.add_literal(migraphx::literal{vars, variance_data});
Aditya Atluri's avatar
Aditya Atluri committed
430

Paul's avatar
Paul committed
431
432
    p.add_instruction(migraphx::op::batch_norm_inference{}, x, scale, bias, mean, variance);
    p.compile(migraphx::cpu::target{});
433
    auto result = p.eval({});
Aditya Atluri's avatar
Aditya Atluri committed
434
435
436
437

    std::vector<float> result_vector(width * height * channels * batches);
    std::vector<float> gold(width * height * channels * batches);
    std::fill(gold.begin(), gold.end(), output_val);
Aditya Atluri's avatar
Aditya Atluri committed
438
    result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });
Aditya Atluri's avatar
Aditya Atluri committed
439

Paul's avatar
Paul committed
440
    EXPECT(migraphx::verify_range(result_vector, gold));
441
442
}

Paul's avatar
Paul committed
443
TEST_CASE(im2col_3x3_with_channels_identity_test)
Scott Thornton's avatar
Scott Thornton committed
444
445
446
447
448
449
450
451
452
453
454
455
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {3, 3};
    std::array<std::size_t, 2> padding{{0, 0}};
    std::array<std::size_t, 2> stride{{1, 1}};
    std::array<std::size_t, 2> dilation{{1, 1}};
    std::size_t channels = 2;

    std::vector<int32_t> weights(channels * f[0] * f[1]);
    std::vector<int32_t> input(channels * size[0] * size[1]);
    std::iota(input.begin(), input.end(), 0);

Paul's avatar
Paul committed
456
457
458
459
460
461
462
    migraphx::program p;
    migraphx::shape s_image{migraphx::shape::int32_type, {1, channels, size[0], size[1]}};
    migraphx::shape s_weights{migraphx::shape::int32_type, {1, channels, f[0], f[1]}};
    auto l_image   = p.add_literal(migraphx::literal{s_image, input});
    auto l_weights = p.add_literal(migraphx::literal{s_weights, weights});
    p.add_instruction(migraphx::op::im2col{padding, stride, dilation}, l_image, l_weights);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
463
464
465
466
467
468
    auto result = p.eval({});

    std::size_t col_height = (size[0] - f[0] + 2 * padding[0]) / stride[0] + 1;
    std::size_t col_width  = (size[1] - f[1] + 2 * padding[1]) / stride[1] + 1;
    std::vector<float> results_vector(channels * f[0] * f[1] * col_height * col_width);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
469
    EXPECT(migraphx::verify_range(results_vector, input));
Scott Thornton's avatar
Scott Thornton committed
470
471
}

Paul's avatar
Paul committed
472
TEST_CASE(exp_test)
473
{
Paul's avatar
Paul committed
474
475
476
477
478
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    p.add_instruction(migraphx::op::exp{}, l);
    p.compile(migraphx::cpu::target{});
479
480
    auto result = p.eval({});
    std::vector<float> results_vector(3);
481
482
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.36787944f, 1.f, 2.71828183f};
Paul's avatar
Paul committed
483
    EXPECT(migraphx::verify_range(results_vector, gold));
484
485
}

Shucai Xiao's avatar
Shucai Xiao committed
486
487
488
489
490
491
492
493
494
495
496
497
TEST_CASE(log_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {1, 2, 3}});
    p.add_instruction(migraphx::op::log{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.0f, 0.6931471806f, 1.0986122887f};
    EXPECT(migraphx::verify_range(results_vector, gold));
498
499
}

Paul's avatar
Paul committed
500
TEST_CASE(sin_test)
501
{
Paul's avatar
Paul committed
502
503
504
505
506
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    p.add_instruction(migraphx::op::sin{}, l);
    p.compile(migraphx::cpu::target{});
507
508
    auto result = p.eval({});
    std::vector<float> results_vector(3);
509
510
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-0.84147098f, 0.f, 0.84147098f};
Paul's avatar
Paul committed
511
    EXPECT(migraphx::verify_range(results_vector, gold));
512
513
}

Paul's avatar
Paul committed
514
TEST_CASE(cos_test)
515
{
Paul's avatar
Paul committed
516
517
518
519
520
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    p.add_instruction(migraphx::op::cos{}, l);
    p.compile(migraphx::cpu::target{});
521
522
    auto result = p.eval({});
    std::vector<float> results_vector(3);
523
524
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.54030231f, 1.f, 0.54030231f};
Paul's avatar
Paul committed
525
    EXPECT(migraphx::verify_range(results_vector, gold));
526
527
}

Paul's avatar
Paul committed
528
TEST_CASE(tan_test)
529
{
Paul's avatar
Paul committed
530
531
532
533
534
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    p.add_instruction(migraphx::op::tan{}, l);
    p.compile(migraphx::cpu::target{});
535
536
    auto result = p.eval({});
    std::vector<float> results_vector(3);
537
538
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-1.55740772f, 0.0f, 1.55740772f};
Paul's avatar
Paul committed
539
    EXPECT(migraphx::verify_range(results_vector, gold));
540
541
}

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
TEST_CASE(asin_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    std::vector<float> data{-0.5f, 0.0f, 0.9f};
    auto l = p.add_literal(migraphx::literal{s, data});
    p.add_instruction(migraphx::op::asin{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-0.5235987756f, 0.f, 1.119769515};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(acos_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::double_type, {3}};
    std::vector<float> data{-0.8f, 0.0f, 1.0f};
    auto l = p.add_literal(migraphx::literal{s, data});
    p.add_instruction(migraphx::op::acos{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {2.4980915448f, 1.5707963268f, 0.0f};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(atan_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::double_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    p.add_instruction(migraphx::op::atan{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-0.7853981634f, 0.0f, 0.7853981634f};
    EXPECT(migraphx::verify_range(results_vector, gold));
584
585
}

Paul's avatar
Paul committed
586
TEST_CASE(add_test)
587
{
Paul's avatar
Paul committed
588
589
590
591
592
593
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = p.add_literal(migraphx::literal{s, {1, 2, 3}});
    p.add_instruction(migraphx::op::add{}, l1, l2);
    p.compile(migraphx::cpu::target{});
594
595
596
597
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0, 2, 4};
Paul's avatar
Paul committed
598
    EXPECT(migraphx::verify_range(results_vector, gold));
599
600
}

Paul's avatar
Paul committed
601
TEST_CASE(broadcast_test)
602
{
Paul's avatar
Paul committed
603
604
    migraphx::program p;
    migraphx::shape a_shape{migraphx::shape::int32_type, {2, 2}};
605
    std::vector<int32_t> a_data{0, 0, 0, 0};
Paul's avatar
Paul committed
606
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
607
    std::vector<int32_t> b_data{-2, -3};
608
    uint64_t axis = 0;
Paul's avatar
Paul committed
609
610
611
612
    auto l1       = p.add_literal(migraphx::literal{a_shape, a_data});
    auto l2       = p.add_literal(migraphx::literal{b_shape, b_data});
    p.add_instruction(migraphx::op::broadcast{axis, l1->get_shape()}, l2);
    p.compile(migraphx::cpu::target{});
613
    auto result = p.eval({});
Paul's avatar
Paul committed
614
    auto output = result.get<int32_t>();
Paul's avatar
Paul committed
615
616
617
618
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -2);
    EXPECT(output(1, 0) == -3);
    EXPECT(output(1, 1) == -3);
619
}
Paul's avatar
Paul committed
620
TEST_CASE(add_broadcast_test)
621
{
622
    {
Paul's avatar
Paul committed
623
624
        migraphx::program p;
        migraphx::shape a_shape{migraphx::shape::float_type, {2, 2, 3}};
625
        std::vector<float> a_data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Paul's avatar
Paul committed
626
        migraphx::shape b_shape{migraphx::shape::float_type, {2, 2}};
627
628
        std::vector<float> b_data{0, -1, -2, -3};
        uint64_t axis = 0;
Paul's avatar
Paul committed
629
630
631
632
633
        auto l1       = p.add_literal(migraphx::literal{a_shape, a_data});
        auto l2       = p.add_literal(migraphx::literal{b_shape, b_data});
        auto l3       = p.add_instruction(migraphx::op::broadcast{axis, l1->get_shape()}, l2);
        p.add_instruction(migraphx::op::add{}, l1, l3);
        p.compile(migraphx::cpu::target{});
634
635
636
637
638
        auto result = p.eval({});
        EXPECT(result.get_shape().packed());
        std::vector<float> results_vector(12);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8};
Paul's avatar
Paul committed
639
        EXPECT(migraphx::verify_range(results_vector, gold));
640
641
    }
    {
Paul's avatar
Paul committed
642
643
        migraphx::program p;
        migraphx::shape a_shape{migraphx::shape::float_type, {2, 2, 3}};
644
        std::vector<float> a_data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
Paul's avatar
Paul committed
645
        migraphx::shape b_shape{migraphx::shape::float_type, {2, 2, 1}};
646
        std::vector<float> b_data{0, -1, -2, -3};
Paul's avatar
Paul committed
647
648
649
650
651
652
        auto l1 = p.add_literal(migraphx::literal{a_shape, a_data});
        auto l2 = p.add_literal(migraphx::literal{b_shape, b_data});
        auto l3 = p.add_instruction(migraphx::op::multibroadcast{{2, 2, 3}}, l1);
        auto l4 = p.add_instruction(migraphx::op::multibroadcast{{2, 2, 3}}, l2);
        p.add_instruction(migraphx::op::add{}, l3, l4);
        p.compile(migraphx::cpu::target{});
653
654
655
656
657
        auto result = p.eval({});
        EXPECT(result.get_shape().packed());
        std::vector<float> results_vector(12);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8};
Paul's avatar
Paul committed
658
        EXPECT(migraphx::verify_range(results_vector, gold));
659
    }
660
661
}

Paul's avatar
Paul committed
662
TEST_CASE(sub_test)
663
{
Paul's avatar
Paul committed
664
665
666
667
668
669
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = p.add_literal(migraphx::literal{s, {1, 2, 3}});
    p.add_instruction(migraphx::op::sub{}, l1, l2);
    p.compile(migraphx::cpu::target{});
670
671
672
673
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-2, -2, -2};
Paul's avatar
Paul committed
674
    EXPECT(migraphx::verify_range(results_vector, gold));
675
676
}

Paul's avatar
Paul committed
677
TEST_CASE(mul_test)
678
{
Paul's avatar
Paul committed
679
680
681
682
683
684
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = p.add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = p.add_literal(migraphx::literal{s, {1, 2, 3}});
    p.add_instruction(migraphx::op::mul{}, l1, l2);
    p.compile(migraphx::cpu::target{});
685
686
687
688
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-1, 0, 3};
Paul's avatar
Paul committed
689
    EXPECT(migraphx::verify_range(results_vector, gold));
690
691
}

Paul's avatar
Paul committed
692
TEST_CASE(div_test)
693
{
Paul's avatar
Paul committed
694
695
696
697
698
699
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = p.add_literal(migraphx::literal{s, {-1.0f, 0.5f, 1.0f}});
    auto l2 = p.add_literal(migraphx::literal{s, {1.0f, 2.0f, 4.0f}});
    p.add_instruction(migraphx::op::div{}, l1, l2);
    p.compile(migraphx::cpu::target{});
700
701
702
703
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-1.f, 0.25f, 0.25f};
Paul's avatar
Paul committed
704
    EXPECT(migraphx::verify_range(results_vector, gold));
705
706
}

Paul's avatar
Paul committed
707
TEST_CASE(relu_test)
Khalique's avatar
Khalique committed
708
{
Paul's avatar
Paul committed
709
710
711
712
713
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1.f, 0.f, 1.f}});
    p.add_instruction(migraphx::op::relu{}, l);
    p.compile(migraphx::cpu::target{});
Khalique's avatar
Khalique committed
714
715
716
717
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.f, 0.f, 1.f};
Paul's avatar
Paul committed
718
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
719
720
}

Paul's avatar
Paul committed
721
TEST_CASE(leaky_relu_test)
Khalique's avatar
Khalique committed
722
{
Paul's avatar
Paul committed
723
724
725
726
727
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = p.add_literal(migraphx::literal{s, {-1.f, 0.f, 1.f}});
    p.add_instruction(migraphx::op::leaky_relu{0.01}, l);
    p.compile(migraphx::cpu::target{});
Khalique's avatar
Khalique committed
728
729
730
731
    auto result = p.eval({});
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-0.01f, 0.f, 1.f};
Paul's avatar
Paul committed
732
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
733
734
}

Khalique's avatar
Khalique committed
735
TEST_CASE(lrn_test)
Khalique's avatar
Khalique committed
736
{
Khalique's avatar
Khalique committed
737
738
739
740
741
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {1, 5, 1, 1}};
    auto l = p.add_literal(migraphx::literal{s, {-2.0f, 1.0f, 0.f, 1.0f, 2.0f}});
    p.add_instruction(migraphx::op::lrn{0.0001, 0.75, 1, 5}, l);
    p.compile(migraphx::cpu::target{});
Khalique's avatar
Khalique committed
742
743
744
    auto result = p.eval({});
    std::vector<float> results_vector(5);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Khalique's avatar
Khalique committed
745
    std::vector<float> gold = {-2 / 1.000075, 1 / 1.00009, 0 / 1.000145, 1 / 1.00009, 2 / 1.000075};
Khalique's avatar
Khalique committed
746
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
747
748
}

Paul's avatar
Paul committed
749
TEST_CASE(imagescaler_test)
Khalique's avatar
Khalique committed
750
{
Paul's avatar
Paul committed
751
752
753
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {1, 3, 2, 2}};
    auto img           = p.add_literal(migraphx::literal{s,
Paul's avatar
Paul committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
                                               {0.2,
                                                0.3,
                                                0.5,
                                                0.4,

                                                0.7,
                                                0.8,
                                                0.1,
                                                0.9,

                                                0.15,
                                                0.25,
                                                0.35,
                                                0.45}});
Khalique's avatar
Khalique committed
768
    auto scale_val     = p.add_literal(2.f);
Paul's avatar
Paul committed
769
770
    auto scaled_tensor = p.add_instruction(migraphx::op::scalar{s}, scale_val);
    auto img_scaled    = p.add_instruction(migraphx::op::mul{}, img, scaled_tensor);
Khalique's avatar
Khalique committed
771
    auto bias_vals     = p.add_literal(
Paul's avatar
Paul committed
772
773
774
775
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {0.01, 0.02, 0.03}});
    auto bias_bcast = p.add_instruction(migraphx::op::broadcast{1, s}, bias_vals);
    p.add_instruction(migraphx::op::add{}, img_scaled, bias_bcast);
    p.compile(migraphx::cpu::target{});
Khalique's avatar
Khalique committed
776
777
778
    auto result = p.eval({});
    std::vector<float> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Khalique's avatar
Khalique committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
    std::vector<float> gold = {0.41,
                               0.61,
                               1.01,
                               0.81,

                               1.42,
                               1.62,
                               0.22,
                               1.82,

                               0.33,
                               0.53,
                               0.73,
                               0.93};
Paul's avatar
Paul committed
793
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
794
795
}

Paul's avatar
Paul committed
796
TEST_CASE(reshape_test)
797
{
Paul's avatar
Paul committed
798
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
799
800
801
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    {
Paul's avatar
Paul committed
802
803
        migraphx::program p;
        auto l                         = p.add_literal(migraphx::literal{a_shape, data});
804
        std::vector<int64_t> new_shape = {8, 3, 1, 1};
Paul's avatar
Paul committed
805
806
        p.add_instruction(migraphx::op::reshape{new_shape}, l);
        p.compile(migraphx::cpu::target{});
807
808
        auto result = p.eval({});
        std::vector<float> results_vector(3);
809
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
810
        EXPECT(migraphx::verify_range(results_vector, data));
811
812
    }
    {
Paul's avatar
Paul committed
813
814
        migraphx::program p;
        auto l                         = p.add_literal(migraphx::literal{a_shape, data});
815
        std::vector<int64_t> new_shape = {1, 3, 4, 2};
Paul's avatar
Paul committed
816
817
        p.add_instruction(migraphx::op::reshape{new_shape}, l);
        p.compile(migraphx::cpu::target{});
818
819
        auto result = p.eval({});
        std::vector<float> results_vector(3);
820
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
821
        EXPECT(migraphx::verify_range(results_vector, data));
822
823
    }
    {
Paul's avatar
Paul committed
824
825
        migraphx::program p;
        auto l                         = p.add_literal(migraphx::literal{a_shape, data});
826
        std::vector<int64_t> new_shape = {1, 3, 4, 2};
Paul's avatar
Paul committed
827
828
        p.add_instruction(migraphx::op::reshape{new_shape}, l);
        p.compile(migraphx::cpu::target{});
829
830
        auto result = p.eval({});
        std::vector<float> results_vector(3);
831
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
832
        EXPECT(migraphx::verify_range(results_vector, data));
833
834
835
    }
}

Paul's avatar
Paul committed
836
template <class T>
837
838
void gemm_test()
{
Paul's avatar
Paul committed
839
    migraphx::program p;
Paul's avatar
Paul committed
840
    std::vector<T> a     = {-0.00925222, 0.56250403, 0.70107397,  0.75402161,  -0.505885,
Paul's avatar
Paul committed
841
842
843
                        1.33628943,  -0.11413,   -0.31270559, 1.59336732,  -0.19361027,
                        -0.91620867, 0.40108416, -0.06969921, 0.68483471,  -0.39906632,
                        -1.66423624, 0.69040076, -1.31490171, -0.11282616, -0.79391814};
Paul's avatar
Paul committed
844
    std::vector<float> b = {6.09568541e-01,
Paul's avatar
Paul committed
845
846
847
848
849
850
851
852
853
854
855
856
857
858
                            -6.10527007e-01,
                            3.66646462e-01,
                            1.18951101e-01,
                            5.58777432e-01,
                            -3.21296298e-01,
                            -5.95997198e-01,
                            -5.01425721e-01,
                            -2.84606807e-01,
                            -5.73673557e-01,
                            -8.99430260e-01,
                            -4.25103093e-01,
                            1.53027987e+00,
                            -3.81407415e-04,
                            -3.29650255e-01};
Paul's avatar
Paul committed
859
    std::vector<float> c = {-1.56327541e+00,
Paul's avatar
Paul committed
860
861
862
863
864
865
866
867
868
869
870
                            -7.09570140e-01,
                            -5.37424982e-01,
                            -2.22994831e-01,
                            -2.15586437e+00,
                            2.09177941e-03,
                            -1.47279677e+00,
                            2.02627040e-01,
                            -6.04527691e-01,
                            -1.29885596e+00,
                            2.16294914e+00,
                            -1.48101497e-01};
Paul's avatar
Paul committed
871
872
873
874
875
876
    migraphx::shape a_shape{migraphx::shape::get_type<T>{}, {4, 5}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
    migraphx::shape b_shape{migraphx::shape::get_type<T>{}, {5, 3}};
    auto bl = p.add_literal(migraphx::literal{b_shape, b});
    p.add_instruction(migraphx::op::dot{}, al, bl);
    p.compile(migraphx::cpu::target{});
877
    auto result = p.eval({});
Paul's avatar
Paul committed
878
    std::vector<T> results_vector(12);
879
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
880
    EXPECT(migraphx::verify_range(c, results_vector));
881
}
Paul's avatar
Paul committed
882
883
TEST_CASE_REGISTER(gemm_test<float>)
TEST_CASE_REGISTER(gemm_test<double>)
884

Paul's avatar
Paul committed
885
TEST_CASE(maxpool_test)
886
{
Paul's avatar
Paul committed
887
    migraphx::program p;
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
    std::vector<float> a = {
        -2.1314404,  -1.63041711, 1.54562736,  1.04625261,  -1.42931843, -0.48703974, 0.4065806,
        -0.1524526,  1.30775225,  0.45538983,  -0.06631992, -1.75332725, 1.33493888,  0.47327688,
        0.36873096,  1.18358743,  -0.34640595, 1.22098756,  0.01946825,  -0.20238149, 0.43348005,
        -0.67991608, -0.83041084, 0.93537551,  0.70241445,  -0.5654031,  -1.30899191, -0.26735824,
        -0.52444768, 1.99097753,  1.86504853,  -0.26506025, 0.26236168,  0.43763575,  0.95300823,
        -1.02733946, -0.74655169, -0.5374338,  -0.28901565, -0.59789604, 0.5310151,   0.99125904,
        0.40609556,  -1.57175648, 0.22031412,  1.45862222,  0.53217483,  1.39087725,  1.00170159,
        -0.87175864, -1.7204628,  -1.72008383, -0.38656762, -0.01443311, 1.46645272,  -1.39995027,
        0.22505587,  -0.43461126, -0.05511411, -0.79950953, -0.01439556, 0.08795211,  1.18943918,
        -0.84079367, -1.73383629, -0.55662078, -0.30626822, -0.67339015, 0.44179603,  0.54316711,
        0.40899998,  -0.27831686, -1.11900508, -0.0881724,  0.35483059,  2.36277103,  -0.04765317,
        -0.36865309, 0.73814237,  1.47151589,  1.36546791,  -0.32649881, -1.0517807,  2.24768877,
        0.68883753,  0.58646208,  -0.91017133, -0.50462508, -0.4013325,  -0.72348958, -0.47368807,
        0.35285577,  -1.01817429, -0.5152272,  0.60321307,  0.43521205,  -0.23733577, 0.66427642,
        0.82949388,  0.82443929,  0.71550399,  0.34561086,  0.68570769,  -0.40718508, -1.20350206,
        0.15793853,  -2.31013632, -0.07934658, -0.09348056, 0.36576006,  2.46601582,  0.11090943,
        0.9144392,   0.56759721,  -0.22112127, -0.21955389, 0.72474903,  -1.28448462, 1.53285873,
        0.37437943,  0.31409341,  1.95433736,  0.91620457,  0.86205518,  1.24365854,  0.19248386,
        0.22526583,  0.13462132,  -0.27561715, -2.06446075, -0.02306402, -1.38278747, 1.1411345,
        1.31293464,  -1.86041689, 1.06763375,  -0.26541466, 1.4545635,   1.11430049,  -0.66491818,
        0.87101674,  0.67768967,  -1.02062869, -1.05031872, -2.2764678,  -2.0200038,  0.37592548,
        -0.26701379, -0.83388507, 0.19403623,  1.00968623,  0.11020003,  1.16736257,  -1.1160326,
        0.47346735,  0.6126079,   -0.19135755, 1.33624589,  -0.29802522, -0.57873946, -1.06555879,
        -0.20686582, 1.36892557,  -0.19937795, 0.8649236,   -1.40126073, 1.53441942,  0.34682792,
        -1.31724346, -1.32898355, 2.40126371,  0.07845283,  1.35732043,  -0.63678312, 0.39429256,
        -1.36487007, -0.31026676, -0.44981545, -0.28994772, -0.14657612, -1.75206447, -0.70612341,
        1.20071781,  -1.64647579, -0.7133292,  0.88494766,  0.52119428,  -2.77387547, 2.07681108,
        -0.90133125, 0.2847338,   0.6174528,   -0.20616426, -0.64263535, -1.08496261, 0.54275119,
        -0.88503587, 0.6629802,   1.47319221,  -1.05829155, -0.97027361, -0.93187737, -1.39954746,
        -0.52359426, -0.14743951, 1.51522756,  0.2078452,   -1.28156149, -1.19363916, -0.78680223,
        -0.89094824, 1.30212069,  -0.77974445, -0.58411664, 0.48764706,  -0.67132682};
    std::vector<float> c = {1.33493888, 1.54562736, 1.22098756, 1.33493888, 1.18358743, 1.99097753,
                            1.00170159, 1.45862222, 1.39087725, 1.46645272, 1.18943918, -0.01443311,
                            1.47151589, 2.36277103, 2.24768877, 0.68883753, 0.82949388, 0.71550399,
                            1.95433736, 2.46601582, 1.53285873, 1.95433736, 1.06763375, 1.4545635,
                            1.33624589, 1.16736257, 0.6126079,  1.36892557, 2.40126371, 1.53441942,
                            0.52119428, 2.07681108, 0.88494766, 1.51522756, 0.54275119, 0.6629802};
Paul's avatar
Paul committed
926
927
928
929
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 6, 6}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
    p.add_instruction(migraphx::op::pooling{"max", {{0, 0}}, {{2, 2}}, {{3, 2}}}, al);
    p.compile(migraphx::cpu::target{});
930
    auto result = p.eval({});
Paul's avatar
Paul committed
931
    // std::cout << result.get_shape() << std::endl;
932
933
    std::vector<float> results_vector(36);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
934
    EXPECT(migraphx::verify_range(results_vector, c));
935
936
}

Paul's avatar
Paul committed
937
TEST_CASE(softmax_test)
938
{
Paul's avatar
Paul committed
939
    migraphx::program p;
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    std::vector<float> a = {
        -5.61869681e-01, 9.07827199e-01,  1.29255986e+00,  3.18533443e-02,  -1.22183852e-03,
        -2.83830553e-01, -1.03245842e+00, -9.28322077e-01, -8.82696748e-01, 1.11327164e-01,
        -9.20038462e-01, 8.47388089e-01,  2.51734018e-01,  1.50563884e+00,  2.23056650e+00,
        -6.17576987e-02, -1.00264274e-01, -6.10369384e-01, 1.17537189e+00,  -2.51560897e-01,
        -8.50333512e-01, -8.03578615e-01, -6.51194930e-01, -2.58137047e-01, 4.65528190e-01,
        3.23284641e-02,  -1.54700470e+00, 1.38096774e+00,  5.39869189e-01,  -7.56884992e-01,
        1.81503093e+00,  -2.11269641e+00, 1.92466557e+00,  1.77230799e+00,  2.21660900e+00,
        1.56777036e+00,  -2.08995026e-03, 3.50566894e-01,  -1.15042710e+00, -1.18577778e+00,
        8.90633047e-01,  -6.63949102e-02, 1.44661188e+00,  1.59215283e+00,  -2.56262213e-01,
        9.39079225e-01,  4.07298543e-02,  3.86590779e-01,  6.09607756e-01,  8.22331488e-01,
        -2.82126725e-01, -9.49052632e-01, -4.24012303e-01, -5.32990396e-01, -3.18386006e+00,
        3.27092171e-01,  -1.33315325e+00, 3.62459183e-01,  3.74710828e-01,  -1.30302286e+00,
        1.79680198e-01,  -4.51832324e-01, 4.34282750e-01,  -7.09520102e-01, 6.20333970e-01,
        -1.28712380e+00, 2.04130828e-01,  -7.70607769e-01, 1.61889160e+00,  -1.50951004e+00,
        -4.10505563e-01, -3.56566496e-02, -1.29747534e+00, -1.49967879e-01, 7.77626812e-01,
        -8.28408226e-02, 2.73412596e-02,  5.79780899e-03,  9.87900198e-02,  -7.95276761e-01,
        -1.38536084e+00, -6.63573861e-01, 3.89783204e-01,  -1.30670881e+00, -7.62425125e-01,
        -4.04883057e-01, 6.24344349e-01,  3.68128955e-01,  -1.01577950e+00, -3.06715906e-01,
        5.67961395e-01,  2.98198581e-01,  -1.63613629e+00, -3.75131965e-01, -6.75393403e-01,
        2.59172034e+00,  6.75538957e-01,  9.07939598e-02,  1.92257717e-01,  -1.21592450e+00,
        -2.73682117e-01, 1.25232983e+00,  -1.39969170e+00, -1.91483587e-01, 2.57732719e-01,
        3.10056299e-01,  1.41833842e+00,  -1.81386679e-01, 3.92868072e-01,  -8.14771175e-01,
        2.02392387e+00,  -9.42091495e-02, -3.77683818e-01, 2.05638766e+00,  2.93796062e-01,
        -6.02131486e-01, 2.70461679e-01,  -8.92358482e-01, 1.04388881e+00,  2.66154885e-01};

    std::vector<float> s = {
        0.30191708, 0.59879845, 0.50029165, 0.24915339, 0.36823985, 0.13190967, 0.0349741,
        0.18750034, 0.21905553, 0.27000085, 0.0547399,  0.56318235, 0.47422904, 0.78964758,
        0.91381913, 0.44601166, 0.47902739, 0.13120073, 0.4449684,  0.18766427, 0.15753111,
        0.07844277, 0.05120674, 0.36648798, 0.14637007, 0.13152322, 0.01560997, 0.29065287,
        0.49196178, 0.10550152, 0.81890774, 0.06369215, 0.62972021, 0.74931765, 0.67285055,
        0.35034987, 0.28612873, 0.31931475, 0.04220394, 0.16093165, 0.22390974, 0.11915915,
        0.3115395,  0.35899726, 0.22190949, 0.57518375, 0.13888834, 0.7753762,  0.4642328,
        0.57055861, 0.21954368, 0.34515455, 0.09486015, 0.40631217, 0.01842281, 0.48770609,
        0.06652815, 0.36023033, 0.42343026, 0.24226256, 0.17348589, 0.44066274, 0.6865865,
        0.17296699, 0.46923906, 0.06921105, 0.3570261,  0.4125829,  0.73165393, 0.15302512,
        0.29499072, 0.33932695, 0.30852377, 0.40762195, 0.40170741, 0.36259529, 0.60848355,
        0.42618036, 0.31721094, 0.02960522, 0.28256637, 0.24389413, 0.2725659,  0.10663581,
        0.27622163, 0.28264219, 0.53652936, 0.09476089, 0.40890986, 0.34848392, 0.32572666,
        0.53076893, 0.11529481, 0.29117745, 0.14625968, 0.8756339,  0.49818122, 0.10656087,
        0.1813329,  0.17664003, 0.21410346, 0.80408043, 0.02315119, 0.27155462, 0.32804728,
        0.13268511, 0.61795473, 0.49703068, 0.41696799, 0.10175809, 0.71028161, 0.29929739,
        0.17377149, 0.76075399, 0.20071237, 0.32632929, 0.36892858, 0.09416146, 0.26656723,
        0.42914796};

Paul's avatar
Paul committed
986
987
988
989
    migraphx::shape a_shape{migraphx::shape::float_type, {5, 3, 4, 2}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
    p.add_instruction(migraphx::op::softmax{}, al);
    p.compile(migraphx::cpu::target{});
990
991
    auto result = p.eval({});
    std::vector<float> results_vector(120);
992
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
993
    EXPECT(migraphx::verify_range(results_vector, s));
Scott Thornton's avatar
Scott Thornton committed
994
995
}

Paul's avatar
Paul committed
996
TEST_CASE(conv2d_test)
997
{
Paul's avatar
Paul committed
998
    migraphx::program p;
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    std::vector<float> a = {
        2.71567607,  -0.9960829,  0.91671127,  0.28140706,  0.63235772,  0.08077253,  0.80927712,
        -0.59108931, -1.05421555, -2.76622486, -0.85044265, -0.52049929, 0.67726439,  -0.65290606,
        0.02345525,  -0.33579525, 0.38901961,  1.05473483,  -1.31188095, 1.8963089,   -0.07265259,
        0.947339,    0.41949373,  -0.70814759, 0.25892952,  1.07311416,  1.2571274,   -0.62318051,
        -0.19951548, -0.94232577, -0.29393643, 0.42292568,  -0.80230367, 1.40909171,  0.63617158,
        0.13900366,  1.09253144,  -0.15265895, 1.54781747,  0.72780299,  1.09189606,  -0.38068101,
        0.97057933,  -0.58958799, 1.56188643,  0.21474874,  0.58725154,  -1.27097559, -0.03024297,
        1.09437096,  -0.4897908,  0.34838957,  -1.31042492, -1.69069934, 0.86956722,  -0.40457946,
        0.46691212,  1.29273605,  0.26464137,  0.22073045,  -1.02178168, 0.22163901,  -1.84387338,
        0.75522131,  -0.45775682, -0.42241111, -1.50944722, 1.07256448,  -1.95876884, -0.28106022,
        0.3341668,   2.13129425,  -1.14728117, -1.06555498, -0.298444,   -0.88322699, -0.65866792,
        -2.06007552, 0.01374334,  0.45612028,  0.52715492,  1.01914406,  -1.72659791, 0.80650896,
        0.16860051,  2.24112225,  -0.78620857, 0.36566174,  -0.07020134, -0.47976932, -0.68230027,
        -0.94711417, -0.54506505, 1.66504931,  -0.71860826, 0.61132306};

    std::vector<float> c = {
        2.82721668e-02,  6.44195229e-02,  1.53499246e-02,  1.72468081e-01,  -6.33238107e-02,
        9.49496776e-02,  1.40258059e-01,  -7.92879611e-02, -1.29301161e-01, 3.11307609e-03,
        -1.90624535e-01, 1.13238767e-01,  -2.80647576e-02, 3.12882811e-02,  -3.52091640e-02,
        3.33581865e-02,  6.43158704e-02,  7.40238279e-02,  -1.00106120e-01, -9.56912562e-02,
        1.44342467e-01,  9.40258950e-02,  6.36333972e-02,  1.66158378e-03,  -8.91554281e-02,
        2.58734226e-02,  1.70919895e-02,  1.78214177e-01,  8.84564668e-02,  8.98126513e-02,
        -1.63809001e-01, 1.37802169e-01,  1.66439757e-01,  -1.45631135e-02, 1.88469887e-04,
        4.76950556e-02,  -1.91969007e-01, -1.76233292e-01, -7.70473927e-02, 1.14828631e-01,
        1.76608220e-01,  -1.50728196e-01, 1.99946314e-02,  -5.88052124e-02, 1.31612435e-01,
        1.61106288e-02,  -1.35080189e-01, 1.49512306e-01,  3.86456847e-02,  1.29330024e-01,
        -3.22975963e-02, -5.60784787e-02, -5.41997552e-02, 4.78562862e-02};

    std::vector<float> s = {0.27039781,
                            0.19105849,
                            -0.06339942,
                            -0.65087199,
                            0.40867025,
                            0.05063812,
                            -0.14907975,
                            0.49018705,
                            -0.49197209,
                            0.33236548,
                            -0.39374301,
                            0.16012701,
                            0.06574871,
                            0.71606487,
                            -0.55201721,
Scott Thornton's avatar
Scott Thornton committed
1043
                            -0.46427044};
Paul's avatar
Paul committed
1044
1045
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
Scott Thornton's avatar
Scott Thornton committed
1046

Paul's avatar
Paul committed
1047
1048
    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = p.add_literal(migraphx::literal{c_shape, c});
Scott Thornton's avatar
Scott Thornton committed
1049

Paul's avatar
Paul committed
1050
1051
    p.add_instruction(migraphx::op::convolution{}, al, cl);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
1052
1053
1054
    auto result = p.eval({});

    std::vector<float> results_vector(16);
1055
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
1056
    EXPECT(migraphx::verify_range(results_vector, s));
Scott Thornton's avatar
Scott Thornton committed
1057
1058
}

Paul's avatar
Paul committed
1059
TEST_CASE(conv2d_padding_test)
1060
{
Paul's avatar
Paul committed
1061
    migraphx::program p;
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
    std::vector<float> a = {
        2.71567607,  -0.9960829,  0.91671127,  0.28140706,  0.63235772,  0.08077253,  0.80927712,
        -0.59108931, -1.05421555, -2.76622486, -0.85044265, -0.52049929, 0.67726439,  -0.65290606,
        0.02345525,  -0.33579525, 0.38901961,  1.05473483,  -1.31188095, 1.8963089,   -0.07265259,
        0.947339,    0.41949373,  -0.70814759, 0.25892952,  1.07311416,  1.2571274,   -0.62318051,
        -0.19951548, -0.94232577, -0.29393643, 0.42292568,  -0.80230367, 1.40909171,  0.63617158,
        0.13900366,  1.09253144,  -0.15265895, 1.54781747,  0.72780299,  1.09189606,  -0.38068101,
        0.97057933,  -0.58958799, 1.56188643,  0.21474874,  0.58725154,  -1.27097559, -0.03024297,
        1.09437096,  -0.4897908,  0.34838957,  -1.31042492, -1.69069934, 0.86956722,  -0.40457946,
        0.46691212,  1.29273605,  0.26464137,  0.22073045,  -1.02178168, 0.22163901,  -1.84387338,
        0.75522131,  -0.45775682, -0.42241111, -1.50944722, 1.07256448,  -1.95876884, -0.28106022,
        0.3341668,   2.13129425,  -1.14728117, -1.06555498, -0.298444,   -0.88322699, -0.65866792,
        -2.06007552, 0.01374334,  0.45612028,  0.52715492,  1.01914406,  -1.72659791, 0.80650896,
        0.16860051,  2.24112225,  -0.78620857, 0.36566174,  -0.07020134, -0.47976932, -0.68230027,
        -0.94711417, -0.54506505, 1.66504931,  -0.71860826, 0.61132306};

    std::vector<float> c = {
        -0.16115488, -0.09800646, -0.05412646, 0.10475694,  0.00555485,  -0.12667653, 0.0458357,
        -0.02656217, -0.16338061, 0.15037455,  0.0102711,   0.01303349,  0.05242859,  0.02034754,
        0.04751867,  -0.17038961, -0.1434752,  -0.10770349, 0.05676742,  -0.15838449, 0.10128359,
        -0.18958683, 0.11954515,  0.10758857,  -0.01058291, -0.12797487, 0.08971019,  0.18793164,
        -0.00881396, -0.06588994, -0.13321903, -0.03300409, 0.01439607,  0.07618178,  -0.11556662,
        0.00764295,  0.12956454,  -0.08937147, -0.12763587, 0.04674943,  0.05765297,  0.11336918,
        0.14747436,  -0.06199479, -0.01166052, -0.12432006, -0.04494537, -0.17581205, 0.09475745,
        0.1149437,   -0.1014564,  0.0274073,   -0.01323579, -0.11092556};

    std::vector<float> s = {
        -0.0201216,  0.40407312,  -0.39005592, -0.0631946,  0.37963012,  -0.64611685, 0.1349397,
        -0.54113752, 0.28533003,  0.27667275,  -0.16442731, -0.181494,   0.30564839,  0.58744538,
        0.32015014,  0.24969585,  -0.27367792, -0.53308117, 0.41236052,  0.26136363,  -0.01489828,
        0.57652152,  -0.38506854, 0.119615,    0.0437076,   0.04779706,  0.57887721,  0.23126155,
        0.05695833,  -0.68200272, 0.02063358,  -0.10267162, 0.8062973,   -0.38149622, -0.40134856,
        -0.03353126, 0.38991132,  -0.3478111,  0.03661491,  0.25783631,  0.62772679,  -0.1961118,
        0.76423508,  -0.36241418, -0.20994355, -0.12368261, -0.9406727,  0.02340185,  -0.08793129,
        -0.02471633, -0.58163726, -0.02211772, -0.42014724, 0.77525634,  0.504951,    -0.20537445,
        -0.20369984, -0.83037728, -1.40423918, -0.46160448, -0.22944322, 0.36074194,  0.49579027,
        0.46527559};

Paul's avatar
Paul committed
1100
1101
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
Scott Thornton's avatar
Scott Thornton committed
1102

Paul's avatar
Paul committed
1103
1104
    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = p.add_literal(migraphx::literal{c_shape, c});
Scott Thornton's avatar
Scott Thornton committed
1105

Paul's avatar
Paul committed
1106
1107
    p.add_instruction(migraphx::op::convolution{{{1, 1}}, {{1, 1}}}, al, cl);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
1108
1109
1110
    auto result = p.eval({});

    std::vector<float> results_vector(64);
1111
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
1112
    EXPECT(migraphx::verify_range(results_vector, s));
1113
1114
}

Paul's avatar
Paul committed
1115
TEST_CASE(conv2d_padding_stride_test)
1116
{
Paul's avatar
Paul committed
1117
    migraphx::program p;
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
    std::vector<float> a = {
        2.71567607,  -0.9960829,  0.91671127,  0.28140706,  0.63235772,  0.08077253,  0.80927712,
        -0.59108931, -1.05421555, -2.76622486, -0.85044265, -0.52049929, 0.67726439,  -0.65290606,
        0.02345525,  -0.33579525, 0.38901961,  1.05473483,  -1.31188095, 1.8963089,   -0.07265259,
        0.947339,    0.41949373,  -0.70814759, 0.25892952,  1.07311416,  1.2571274,   -0.62318051,
        -0.19951548, -0.94232577, -0.29393643, 0.42292568,  -0.80230367, 1.40909171,  0.63617158,
        0.13900366,  1.09253144,  -0.15265895, 1.54781747,  0.72780299,  1.09189606,  -0.38068101,
        0.97057933,  -0.58958799, 1.56188643,  0.21474874,  0.58725154,  -1.27097559, -0.03024297,
        1.09437096,  -0.4897908,  0.34838957,  -1.31042492, -1.69069934, 0.86956722,  -0.40457946,
        0.46691212,  1.29273605,  0.26464137,  0.22073045,  -1.02178168, 0.22163901,  -1.84387338,
        0.75522131,  -0.45775682, -0.42241111, -1.50944722, 1.07256448,  -1.95876884, -0.28106022,
        0.3341668,   2.13129425,  -1.14728117, -1.06555498, -0.298444,   -0.88322699, -0.65866792,
        -2.06007552, 0.01374334,  0.45612028,  0.52715492,  1.01914406,  -1.72659791, 0.80650896,
        0.16860051,  2.24112225,  -0.78620857, 0.36566174,  -0.07020134, -0.47976932, -0.68230027,
        -0.94711417, -0.54506505, 1.66504931,  -0.71860826, 0.61132306};

    std::vector<float> c = {
        -0.14601797, -0.13000923, 0.06521662,  0.06178288,  -0.11083675, 0.10154136,  0.09990512,
        0.06030385,  -0.11374587, -0.17523311, -0.14344215, 0.17802463,  0.06300922,  -0.15325832,
        0.07066704,  0.05166031,  0.00615084,  -0.02606523, 0.08083995,  -0.17913306, 0.0624622,
        0.0735731,   -0.04198661, -0.0164391,  -0.06374192, 0.16569914,  0.10681538,  0.07370754,
        0.02802075,  0.00282027,  0.15104802,  -0.11084409, -0.00197773, 0.07924436,  0.03528272,
        0.04765259,  -0.15896152, 0.07917164,  0.12125669,  -0.1154705,  -0.11999125, 0.12749968,
        -0.06269585, 0.18658121,  -0.03944227, 0.0111798,   -0.17731084, 0.11789055,  -0.09982193,
        0.08142821,  0.0729029,   0.11303909,  0.12735154,  0.03885292};

    std::vector<float> s = {-0.20817225,
                            0.87965256,
                            0.14958936,
                            -1.24887264,
                            -0.06540672,
                            0.20778663,
                            0.40456355,
                            -0.99900877,
                            0.4917807,
                            0.1994698,
                            0.64205718,
                            0.37798831,
                            -0.25315839,
                            0.44276932,
                            -0.16138598,
                            0.79344082};

Paul's avatar
Paul committed
1161
1162
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = p.add_literal(migraphx::literal{a_shape, a});
Scott Thornton's avatar
Scott Thornton committed
1163

Paul's avatar
Paul committed
1164
1165
    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = p.add_literal(migraphx::literal{c_shape, c});
Scott Thornton's avatar
Scott Thornton committed
1166

Paul's avatar
Paul committed
1167
1168
    p.add_instruction(migraphx::op::convolution{{{1, 1}}, {{2, 2}}}, al, cl);
    p.compile(migraphx::cpu::target{});
Scott Thornton's avatar
Scott Thornton committed
1169
1170
1171
    auto result = p.eval({});

    std::vector<float> results_vector(16);
1172
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
1173
    EXPECT(migraphx::verify_range(results_vector, s));
Scott Thornton's avatar
Scott Thornton committed
1174
}
1175

Paul's avatar
Paul committed
1176
TEST_CASE(transpose_test)
1177
{
Paul's avatar
Paul committed
1178
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 2, 2, 3}};
1179
1180
1181
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);

1182
    {
Paul's avatar
Paul committed
1183
1184
        migraphx::program p;
        auto l                    = p.add_literal(migraphx::literal{a_shape, data});
Paul's avatar
Paul committed
1185
        std::vector<int64_t> perm = {0, 3, 1, 2};
Paul's avatar
Paul committed
1186
1187
        p.add_instruction(migraphx::op::transpose{perm}, l);
        p.compile(migraphx::cpu::target{});
1188
        auto result = p.eval({});
1189

Paul's avatar
Paul committed
1190
        result.visit([&](auto output) {
Paul's avatar
Paul committed
1191
            std::vector<size_t> new_lens = {1, 3, 2, 2};
Paul's avatar
Paul committed
1192
1193
            EXPECT(bool{output.get_shape().lens() == new_lens});
        });
1194
1195
    }
    {
Paul's avatar
Paul committed
1196
1197
        migraphx::program p;
        auto l                    = p.add_literal(migraphx::literal{a_shape, data});
Paul's avatar
Paul committed
1198
        std::vector<int64_t> perm = {0, 3, 1, 2};
Paul's avatar
Paul committed
1199
1200
1201
        auto result               = p.add_instruction(migraphx::op::transpose{perm}, l);
        p.add_instruction(migraphx::op::contiguous{}, result);
        p.compile(migraphx::cpu::target{});
1202
1203
1204
1205
1206
        auto result2 = p.eval({});

        std::vector<float> results_vector(12);
        result2.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11};
Paul's avatar
Paul committed
1207
        EXPECT(migraphx::verify_range(results_vector, gold));
1208
    }
1209
1210
}

Paul's avatar
Paul committed
1211
TEST_CASE(contiguous_test)
Paul's avatar
Paul committed
1212
{
Paul's avatar
Paul committed
1213
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 3, 2, 2}, {12, 1, 6, 3}};
1214
1215
1216
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);

Paul's avatar
Paul committed
1217
1218
1219
1220
    migraphx::program p;
    auto l = p.add_literal(migraphx::literal{a_shape, data});
    p.add_instruction(migraphx::op::contiguous{}, l);
    p.compile(migraphx::cpu::target{});
1221
1222
1223
    auto result = p.eval({});

    std::vector<float> results_vector(12);
1224
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
1225
1226
    std::vector<size_t> new_lens    = {1, 3, 2, 2};
    std::vector<size_t> new_strides = {12, 1, 6, 3};
Paul's avatar
Paul committed
1227
    std::vector<float> gold         = {0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11};
Paul's avatar
Paul committed
1228
    EXPECT(migraphx::verify_range(results_vector, gold));
1229
1230
}

Khalique's avatar
Khalique committed
1231
1232
TEST_CASE(identity_test)
{
Khalique's avatar
Khalique committed
1233
1234
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
Khalique's avatar
Khalique committed
1235
    std::vector<int> data{1, 2, 3, 4};
Khalique's avatar
Khalique committed
1236
1237
1238
    auto l = p.add_literal(migraphx::literal{s, data});
    p.add_instruction(migraphx::op::identity{}, l);
    p.compile(migraphx::cpu::target{});
Khalique's avatar
Khalique committed
1239
1240
1241
1242
1243
1244
    auto result = p.eval({});
    std::vector<int> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::equal(data.begin(), data.end(), results_vector.begin()));
}

Khalique's avatar
Khalique committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
TEST_CASE(abs_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 2, -3, 4}});
    p.add_instruction(migraphx::op::abs{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1, 2, 3, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(sigmoid_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = p.add_literal(migraphx::literal{s, {-1, 2, -3, 4}});
    p.add_instruction(migraphx::op::sigmoid{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{sigmoid(-1), sigmoid(2), sigmoid(-3), sigmoid(4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
TEST_CASE(sinh_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = p.add_literal(migraphx::literal{s, {-1.0, 2.0, -3.0, 4.0}});
    p.add_instruction(migraphx::op::sinh{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{sinhf(-1), sinhf(2), sinhf(-3), sinhf(4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(cosh_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = p.add_literal(migraphx::literal{s, {-1.0, 2.0, -3.0, 4.0}});
    p.add_instruction(migraphx::op::cosh{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{coshf(-1), coshf(2), coshf(-3), coshf(4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Khalique's avatar
Khalique committed
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
TEST_CASE(tanh_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = p.add_literal(migraphx::literal{s, {-1.0, 2.0, -3.0, 4.0}});
    p.add_instruction(migraphx::op::tanh{}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{tanhf(-1), tanhf(2), tanhf(-3), tanhf(4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(elu_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
Khalique's avatar
Khalique committed
1319
    auto l      = p.add_literal(migraphx::literal{s, {-1.0, 2.0, -3.0, 4.0}});
Khalique's avatar
Khalique committed
1320
1321
1322
1323
1324
1325
    float alpha = 0.5;
    p.add_instruction(migraphx::op::elu{alpha}, l);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Khalique's avatar
Khalique committed
1326
    std::vector<float> gold{elu(alpha, -1), elu(alpha, 2), elu(alpha, -3), elu(alpha, 4)};
Khalique's avatar
Khalique committed
1327
1328
1329
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Khalique's avatar
Khalique committed
1330
1331
1332
1333
TEST_CASE(max_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
Khalique's avatar
Khalique committed
1334
1335
1336
    auto l0       = p.add_literal(migraphx::literal{s, {1, 4, 3}});
    auto l1       = p.add_literal(migraphx::literal{s, {2, 8, 6}});
    auto l2       = p.add_literal(migraphx::literal{s, {7, 5, 9}});
Khalique's avatar
Khalique committed
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
    auto curr_max = p.add_instruction(migraphx::op::max{}, l0, l1);
    p.add_instruction(migraphx::op::max{}, curr_max, l2);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{7, 8, 9};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(min_test)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::float_type, {3}};
Khalique's avatar
Khalique committed
1351
1352
1353
    auto l0       = p.add_literal(migraphx::literal{s, {1, 4, 3}});
    auto l1       = p.add_literal(migraphx::literal{s, {2, 8, 6}});
    auto l2       = p.add_literal(migraphx::literal{s, {7, 5, 9}});
Khalique's avatar
Khalique committed
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
    auto curr_min = p.add_instruction(migraphx::op::min{}, l0, l1);
    p.add_instruction(migraphx::op::min{}, curr_min, l2);
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1, 4, 3};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

1364
TEST_CASE(rnn_forward)
Shucai Xiao's avatar
Shucai Xiao committed
1365
{
Shucai Xiao's avatar
Shucai Xiao committed
1366
1367
    std::size_t batch_size  = 2;
    std::size_t seq_len     = 2;
1368
    std::size_t hidden_size = 4;
Shucai Xiao's avatar
Shucai Xiao committed
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
    std::size_t input_size  = 3;
    std::size_t num_dirct   = 1;
    std::vector<float> wf_data{0.4691,
                               0.3185,
                               -0.2227,
                               0.4423,
                               -0.0609,
                               -0.2803,
                               0.1744,
                               0.3146,
                               0.4049,
                               -0.3973,
                               -0.0890,
                               -0.1636};
    std::vector<float> rf_data{-0.0456,
                               0.1061,
                               0.1574,
                               -0.4928,
                               -0.4300,
                               -0.1909,
                               -0.0225,
                               -0.2668,
                               0.1840,
                               -0.4453,
                               -0.4896,
                               0.1302,
                               -0.0929,
                               0.3545,
                               -0.4981,
                               0.0616};
    std::vector<float> biasf_data{
        -0.4938, 0.4355, -0.3186, 0.2094, 0.1037, -0.1071, 0.4504, -0.3990};
1401
1402
    std::vector<float> input(seq_len * batch_size * input_size, 0);
    input[0] = input[1] = 1.0;
Shucai Xiao's avatar
Shucai Xiao committed
1403
    float clip          = 0.0f;
Shucai Xiao's avatar
Shucai Xiao committed
1404
    {
1405
1406
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

Shucai Xiao's avatar
Shucai Xiao committed
1407
        migraphx::program p;
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, wf_data});
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, rf_data});
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, biasf_data});
Shucai Xiao's avatar
Shucai Xiao committed
1420
        auto und  = p.add_instruction(migraphx::op::undefined{});
1421
1422

        p.add_instruction(migraphx::op::rnn{hidden_size,
Shucai Xiao's avatar
Shucai Xiao committed
1423
1424
1425
1426
1427
1428
1429
                                            {migraphx::op::tanh{}, migraphx::op::tanh{}},
                                            migraphx::op::rnn::forward,
                                            clip},
                          seq,
                          w,
                          r,
                          bias,
1430
                          und,
Shucai Xiao's avatar
Shucai Xiao committed
1431
                          ih);
1432
1433
1434
1435
        p.compile(migraphx::cpu::target{});
        auto hs_concat = p.eval({});
        std::vector<float> hs_data;
        hs_concat.visit([&](auto output) { hs_data.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1436

Shucai Xiao's avatar
Shucai Xiao committed
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
        std::vector<float> hs_data_gold{0.37780784,
                                        0.61055139,
                                        0.55168478,
                                        -0.5888475,
                                        -0.37144644,
                                        0.31708236,
                                        0.13104209,
                                        -0.18736027,
                                        0.03445704,
                                        0.19167931,
                                        -0.3946827,
                                        -0.30889652,
                                        -0.22276389,
                                        0.44193283,
                                        -0.16477929,
                                        -0.11893477};
1453
1454
1455
1456
1457
        EXPECT(migraphx::verify_range(hs_data, hs_data_gold));
    }

    {
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);
Shucai Xiao's avatar
Shucai Xiao committed
1458

1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
        migraphx::program p;
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, wf_data});
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, rf_data});
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, biasf_data});
Shucai Xiao's avatar
Shucai Xiao committed
1472
        auto und  = p.add_instruction(migraphx::op::undefined{});
1473

Shucai Xiao's avatar
Shucai Xiao committed
1474
        auto out_hs =
Shucai Xiao's avatar
Shucai Xiao committed
1475
            p.add_instruction(migraphx::op::rnn{hidden_size, {}, migraphx::op::rnn::forward, clip},
Shucai Xiao's avatar
Shucai Xiao committed
1476
1477
1478
1479
                              seq,
                              w,
                              r,
                              bias,
1480
                              und,
Shucai Xiao's avatar
Shucai Xiao committed
1481
                              ih);
1482
1483

        p.add_instruction(migraphx::op::rnn_last_output{}, out_hs);
Shucai Xiao's avatar
Shucai Xiao committed
1484
        p.compile(migraphx::cpu::target{});
1485
1486
1487
1488
1489

        auto last_output = p.eval({});
        std::vector<float> last_output_data;
        last_output.visit([&](auto out) { last_output_data.assign(out.begin(), out.end()); });

Shucai Xiao's avatar
Shucai Xiao committed
1490
1491
1492
1493
1494
1495
1496
1497
        std::vector<float> last_output_data_gold{0.03445704,
                                                 0.19167931,
                                                 -0.3946827,
                                                 -0.30889652,
                                                 -0.22276389,
                                                 0.44193283,
                                                 -0.16477929,
                                                 -0.11893477};
1498
        EXPECT(migraphx::verify_range(last_output_data, last_output_data_gold));
Shucai Xiao's avatar
Shucai Xiao committed
1499
    }
1500
}
Shucai Xiao's avatar
Shucai Xiao committed
1501

1502
1503
TEST_CASE(rnn_reverse)
{
Shucai Xiao's avatar
Shucai Xiao committed
1504
1505
    std::size_t batch_size  = 2;
    std::size_t seq_len     = 2;
1506
    std::size_t hidden_size = 4;
Shucai Xiao's avatar
Shucai Xiao committed
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
    std::size_t input_size  = 3;
    std::size_t num_dirct   = 1;
    std::vector<float> wr_data{-0.0296,
                               -0.1341,
                               0.1761,
                               -0.2325,
                               -0.0717,
                               0.1852,
                               0.2720,
                               0.1471,
                               -0.1097,
                               0.3363,
                               -0.0587,
                               -0.2302};
    std::vector<float> rr_data{0.2528,
                               -0.2333,
                               0.3973,
                               0.1593,
                               -0.0388,
                               0.1702,
                               0.3829,
                               -0.0712,
                               -0.1668,
                               0.3074,
                               -0.2854,
                               0.4049,
                               -0.3737,
                               -0.1051,
                               0.4482,
                               -0.2841};
    std::vector<float> biasr_data{-0.3188, 0.1341, -0.4446, 0.1389, 0.3117, 0.3664, 0.2352, 0.2552};
1538
1539
    std::vector<float> input(seq_len * batch_size * input_size, 0);
    input[0] = input[1] = 1.0;
Shucai Xiao's avatar
Shucai Xiao committed
1540
    float clip          = 0.0f;
Shucai Xiao's avatar
Shucai Xiao committed
1541
    {
1542
1543
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

Shucai Xiao's avatar
Shucai Xiao committed
1544
        migraphx::program p;
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, wr_data});
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, rr_data});
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, biasr_data});
Shucai Xiao's avatar
Shucai Xiao committed
1557
        auto und  = p.add_instruction(migraphx::op::undefined{});
1558
1559
1560
1561
1562

        p.add_instruction(migraphx::op::rnn{hidden_size,
                                            {migraphx::op::tanh{}, migraphx::op::tanh{}},
                                            migraphx::op::rnn::reverse,
                                            clip},
Shucai Xiao's avatar
Shucai Xiao committed
1563
1564
1565
1566
                          seq,
                          w,
                          r,
                          bias,
1567
                          und,
Shucai Xiao's avatar
Shucai Xiao committed
1568
                          ih);
1569
1570
1571
1572
        p.compile(migraphx::cpu::target{});
        auto hs_concat = p.eval({});
        std::vector<float> hs_data;
        hs_concat.visit([&](auto output) { hs_data.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1573

Shucai Xiao's avatar
Shucai Xiao committed
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
        std::vector<float> hs_data_gold{-0.29385301,
                                        0.16796815,
                                        0.51075965,
                                        0.40258689,
                                        -0.13818839,
                                        0.44124447,
                                        0.14365635,
                                        0.14803654,
                                        -0.0070999,
                                        0.46251031,
                                        -0.20639211,
                                        0.37488942,
                                        -0.0070999,
                                        0.46251031,
                                        -0.20639211,
                                        0.37488942};
1590
1591
        EXPECT(migraphx::verify_range(hs_data, hs_data_gold));
    }
Shucai Xiao's avatar
Shucai Xiao committed
1592

1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
    {
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

        migraphx::program p;
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, wr_data});
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, rr_data});
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, biasr_data});
Shucai Xiao's avatar
Shucai Xiao committed
1609
        auto und  = p.add_instruction(migraphx::op::undefined{});
1610

Shucai Xiao's avatar
Shucai Xiao committed
1611
        auto out_hs =
Shucai Xiao's avatar
Shucai Xiao committed
1612
            p.add_instruction(migraphx::op::rnn{hidden_size, {}, migraphx::op::rnn::reverse, clip},
Shucai Xiao's avatar
Shucai Xiao committed
1613
1614
1615
1616
                              seq,
                              w,
                              r,
                              bias,
1617
                              und,
Shucai Xiao's avatar
Shucai Xiao committed
1618
                              ih);
1619
1620

        p.add_instruction(migraphx::op::rnn_last_output{}, out_hs);
Shucai Xiao's avatar
Shucai Xiao committed
1621
        p.compile(migraphx::cpu::target{});
1622
1623
1624
1625
1626

        auto last_output = p.eval({});
        std::vector<float> last_output_data;
        last_output.visit([&](auto out) { last_output_data.assign(out.begin(), out.end()); });

Shucai Xiao's avatar
Shucai Xiao committed
1627
1628
1629
1630
1631
1632
1633
1634
        std::vector<float> last_output_data_gold{-0.29385301,
                                                 0.16796815,
                                                 0.51075965,
                                                 0.40258689,
                                                 -0.13818839,
                                                 0.44124447,
                                                 0.14365635,
                                                 0.14803654};
1635
1636
1637
1638
1639
1640
        EXPECT(migraphx::verify_range(last_output_data, last_output_data_gold));
    }
}

TEST_CASE(rnn_bidirectional)
{
Shucai Xiao's avatar
Shucai Xiao committed
1641
    std::size_t batch_size  = 2;
Shucai Xiao's avatar
Shucai Xiao committed
1642
    std::size_t seq_len     = 2;
1643
    std::size_t hidden_size = 4;
Shucai Xiao's avatar
Shucai Xiao committed
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
    std::size_t input_size  = 3;
    std::size_t num_dirct   = 2;
    std::vector<float> wf_data{0.4691,
                               0.3185,
                               -0.2227,
                               0.4423,
                               -0.0609,
                               -0.2803,
                               0.1744,
                               0.3146,
                               0.4049,
                               -0.3973,
                               -0.0890,
                               -0.1636};
    std::vector<float> wr_data{-0.0296,
                               -0.1341,
                               0.1761,
                               -0.2325,
                               -0.0717,
                               0.1852,
                               0.2720,
                               0.1471,
                               -0.1097,
                               0.3363,
                               -0.0587,
                               -0.2302};
    std::vector<float> rf_data{-0.0456,
                               0.1061,
                               0.1574,
                               -0.4928,
                               -0.4300,
                               -0.1909,
                               -0.0225,
                               -0.2668,
                               0.1840,
                               -0.4453,
                               -0.4896,
                               0.1302,
                               -0.0929,
                               0.3545,
                               -0.4981,
                               0.0616};
    std::vector<float> rr_data{0.2528,
                               -0.2333,
                               0.3973,
                               0.1593,
                               -0.0388,
                               0.1702,
                               0.3829,
                               -0.0712,
                               -0.1668,
                               0.3074,
                               -0.2854,
                               0.4049,
                               -0.3737,
                               -0.1051,
                               0.4482,
                               -0.2841};
    std::vector<float> biasf_data{
        -0.4938, 0.4355, -0.3186, 0.2094, 0.1037, -0.1071, 0.4504, -0.3990};
    std::vector<float> biasr_data{-0.3188, 0.1341, -0.4446, 0.1389, 0.3117, 0.3664, 0.2352, 0.2552};
1705
1706
    std::vector<float> input(seq_len * batch_size * input_size, 0);
    input[0] = input[1] = 1.0;
Shucai Xiao's avatar
Shucai Xiao committed
1707
    float clip          = 0.0f;
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
    {
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

        migraphx::program p;
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        auto w_data = wf_data;
        w_data.insert(w_data.end(), wr_data.begin(), wr_data.end());
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, w_data});

        auto r_data = rf_data;
        r_data.insert(r_data.end(), rr_data.begin(), rr_data.end());
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, r_data});

        auto bias_data = biasf_data;
        bias_data.insert(bias_data.end(), biasr_data.begin(), biasr_data.end());
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, bias_data});
Shucai Xiao's avatar
Shucai Xiao committed
1732
        auto und  = p.add_instruction(migraphx::op::undefined{});
1733

Shucai Xiao's avatar
Shucai Xiao committed
1734
1735
1736
1737
1738
1739
1740
1741
        p.add_instruction(
            migraphx::op::rnn{hidden_size, {}, migraphx::op::rnn::bidirectional, clip},
            seq,
            w,
            r,
            bias,
            und,
            ih);
1742
1743
1744
1745
1746
        p.compile(migraphx::cpu::target{});
        auto hs_concat = p.eval({});
        std::vector<float> hs_data;
        hs_concat.visit([&](auto output) { hs_data.assign(output.begin(), output.end()); });

Shucai Xiao's avatar
Shucai Xiao committed
1747
1748
1749
1750
1751
1752
1753
        std::vector<float> hs_data_gold{
            0.37780784,  0.61055139,  0.55168478,  -0.5888475, -0.37144644, 0.31708236,
            0.13104209,  -0.18736027, -0.29385301, 0.16796815, 0.51075965,  0.40258689,
            -0.13818839, 0.44124447,  0.14365635,  0.14803654, 0.03445704,  0.19167931,
            -0.3946827,  -0.30889652, -0.22276389, 0.44193283, -0.16477929, -0.11893477,
            -0.0070999,  0.46251031,  -0.20639211, 0.37488942, -0.0070999,  0.46251031,
            -0.20639211, 0.37488942};
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
        EXPECT(migraphx::verify_range(hs_data, hs_data_gold));
    }

    {
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

        migraphx::program p;
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        auto ih = p.add_literal(migraphx::literal{ih_shape, ih_data});

        auto w_data = wf_data;
        w_data.insert(w_data.end(), wr_data.begin(), wr_data.end());
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, w_data});

        auto r_data = rf_data;
        r_data.insert(r_data.end(), rr_data.begin(), rr_data.end());
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, r_data});

        auto bias_data = biasf_data;
        bias_data.insert(bias_data.end(), biasr_data.begin(), biasr_data.end());
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, bias_data});
Shucai Xiao's avatar
Shucai Xiao committed
1781
        auto und  = p.add_instruction(migraphx::op::undefined{});
1782

Shucai Xiao's avatar
Shucai Xiao committed
1783
1784
1785
1786
1787
1788
1789
1790
1791
        auto out_hs = p.add_instruction(
            migraphx::op::rnn{
                hidden_size, {migraphx::op::tanh{}}, migraphx::op::rnn::bidirectional, clip},
            seq,
            w,
            r,
            bias,
            und,
            ih);
1792
1793
1794
1795
1796
1797
1798
1799

        p.add_instruction(migraphx::op::rnn_last_output{}, out_hs);
        p.compile(migraphx::cpu::target{});

        auto last_output = p.eval({});
        std::vector<float> last_output_data;
        last_output.visit([&](auto out) { last_output_data.assign(out.begin(), out.end()); });

1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
        std::vector<float> last_output_data_gold{0.03445704,
                                                 0.19167931,
                                                 -0.3946827,
                                                 -0.30889652,
                                                 -0.22276389,
                                                 0.44193283,
                                                 -0.16477929,
                                                 -0.11893477,
                                                 -0.29385301,
                                                 0.16796815,
                                                 0.51075965,
                                                 0.40258689,
                                                 -0.13818839,
                                                 0.44124447,
                                                 0.14365635,
                                                 0.14803654};

        EXPECT(migraphx::verify_range(last_output_data, last_output_data_gold));
    }
    {
        std::vector<float> ih_data(num_dirct * batch_size * hidden_size, 0);

        migraphx::program p;
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        auto seq = p.add_literal(migraphx::literal{in_shape, input});

        auto w_data = wf_data;
        w_data.insert(w_data.end(), wr_data.begin(), wr_data.end());
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        auto w = p.add_literal(migraphx::literal{w_shape, w_data});

        auto r_data = rf_data;
        r_data.insert(r_data.end(), rr_data.begin(), rr_data.end());
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        auto r = p.add_literal(migraphx::literal{r_shape, r_data});

        auto bias_data = biasf_data;
        bias_data.insert(bias_data.end(), biasr_data.begin(), biasr_data.end());
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
        auto bias = p.add_literal(migraphx::literal{b_shape, bias_data});

        auto out_hs =
            p.add_instruction(migraphx::op::rnn{hidden_size,
                                                {migraphx::op::tanh{}, migraphx::op::tanh{}},
                                                migraphx::op::rnn::bidirectional,
                                                clip},
                              seq,
                              w,
                              r,
                              bias);

        p.add_instruction(migraphx::op::rnn_last_output{}, out_hs);
        p.compile(migraphx::cpu::target{});

        auto last_output = p.eval({});
        std::vector<float> last_output_data;
        last_output.visit([&](auto out) { last_output_data.assign(out.begin(), out.end()); });

Shucai Xiao's avatar
Shucai Xiao committed
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
        std::vector<float> last_output_data_gold{0.03445704,
                                                 0.19167931,
                                                 -0.3946827,
                                                 -0.30889652,
                                                 -0.22276389,
                                                 0.44193283,
                                                 -0.16477929,
                                                 -0.11893477,
                                                 -0.29385301,
                                                 0.16796815,
                                                 0.51075965,
                                                 0.40258689,
                                                 -0.13818839,
                                                 0.44124447,
                                                 0.14365635,
                                                 0.14803654};
1874
1875

        EXPECT(migraphx::verify_range(last_output_data, last_output_data_gold));
Shucai Xiao's avatar
Shucai Xiao committed
1876
1877
1878
    }
}

1879
1880
1881
TEST_CASE(pad_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
1882
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
1883
    auto l0 = p.add_literal(migraphx::literal{s, {1, 2, 3, 4}});
Khalique's avatar
Khalique committed
1884
    p.add_instruction(migraphx::op::pad{{1, 1, 1, 1}}, l0);
1885
1886
1887
1888
    p.compile(migraphx::cpu::target{});
    auto result = p.eval({});
    std::vector<float> results_vector(16);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Khalique's avatar
Khalique committed
1889
    std::vector<float> gold{0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0};
1890
    EXPECT(migraphx::verify_range(results_vector, gold));
1891
1892
}

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