"src/targets/ref/target.cpp" did not exist on "7f9d85cc0323bff32b92ea5b3751ac8c2e6fb2f3"
ref_ops_test.cpp 378 KB
Newer Older
1
2
3
/*
 * The MIT License (MIT)
 *
4
 * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
#include <iostream>
#include <vector>
26
#include <cmath>
turneram's avatar
turneram committed
27
#include <random>
Charlie Lin's avatar
Charlie Lin committed
28
#include <limits>
Paul's avatar
Paul committed
29
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
30
#include <migraphx/op/pooling.hpp>
Paul's avatar
Paul committed
31
#include <migraphx/instruction.hpp>
32
#include <migraphx/quantization.hpp>
33
#include <migraphx/register_target.hpp>
Paul's avatar
Paul committed
34
#include <migraphx/verify.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
35
#include <migraphx/onnx.hpp>
36
37
38
39
#include <migraphx/make_op.hpp>

#include <migraphx/serialize.hpp>

40
#include "test.hpp"
41
#include <migraphx/half.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
42
#include <iomanip>
Scott Thornton's avatar
Scott Thornton committed
43

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

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

Shucai Xiao's avatar
Shucai Xiao committed
48
TEST_CASE(abs_test)
49
{
Shucai Xiao's avatar
Shucai Xiao committed
50
51
52
53
54
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = mm->add_literal(migraphx::literal{s, {-1, 2, -3, 4}});
    mm->add_instruction(migraphx::make_op("abs"), l);
55
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
56
57
58
59
60
61
    auto result = p.eval({}).back();
    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));
}
62

Charlie Lin's avatar
Charlie Lin committed
63
TEST_CASE(abs_dyn_test)
64
65
{
    migraphx::program p;
Charlie Lin's avatar
Charlie Lin committed
66
    auto* mm = p.get_main_module();
67
    migraphx::shape s{migraphx::shape::float_type, {{2, 8}, {2, 2}}};
68
69
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("abs"), input);
70
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
71
72

    std::vector<float> a = {-1, 2, -3, 4};
73
74
75
76
77
78
79
80
81
82
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 2}};
    params0["X"] = migraphx::argument(input_fixed_shape0, a.data());
    auto result  = p.eval(params0).back();
    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));
}

Shucai Xiao's avatar
Shucai Xiao committed
83
84
85
86
87
88
89
90
TEST_CASE(acos_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {3}};
    std::vector<float> data{-0.8f, 0.0f, 1.0f};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("acos"), l);
91
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
92
93
94
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
95
96
97
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return acosf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
98
99
    EXPECT(migraphx::verify_range(results_vector, gold));
}
100

Charlie Lin's avatar
Charlie Lin committed
101
TEST_CASE(acos_dyn_test)
102
103
104
{
    migraphx::program p;
    auto* mm = p.get_main_module();
105
    migraphx::shape::dynamic_dimension dd{3, 8};
106
107
108
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("acos"), input);
109
    p.compile(migraphx::make_target("ref"));
110

Charlie Lin's avatar
Charlie Lin committed
111
    std::vector<float> input_data{-0.8f, 0.0f, 1.0f};
112
113
114
115
116
117
118
119
120
121
122
123
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return acosf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
124
125
126
127
128
129
130
131
TEST_CASE(acosh_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {3}};
    std::vector<float> data{1.1f, 1.2f, 2.0f};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("acosh"), l);
132
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
133
134
135
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
136
137
138
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return acoshf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
139
    EXPECT(migraphx::verify_range(results_vector, gold));
140
141
}

Charlie Lin's avatar
Charlie Lin committed
142
TEST_CASE(acosh_dyn_test)
143
144
145
{
    migraphx::program p;
    auto* mm = p.get_main_module();
146
    migraphx::shape::dynamic_dimension dd{3, 8};
147
148
149
150
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    std::vector<float> input_data{1.1f, 1.2f, 2.0f};
    mm->add_instruction(migraphx::make_op("acosh"), input);
151
    p.compile(migraphx::make_target("ref"));
152
153
154
155
156
157
158
159
160
161
162
163
164

    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return acoshf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
165
TEST_CASE(add_broadcast_test)
166
167
168
{
    {
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
169
170
171
172
173
174
175
176
177
        auto* mm = p.get_main_module();
        migraphx::shape a_shape{migraphx::shape::float_type, {2, 2, 3}};
        std::vector<float> a_data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        migraphx::shape b_shape{migraphx::shape::float_type, {2, 2}};
        std::vector<float> b_data{0, -1, -2, -3};
        uint64_t axis = 0;
        auto l1       = mm->add_literal(migraphx::literal{a_shape, a_data});
        auto l2       = mm->add_literal(migraphx::literal{b_shape, b_data});
        auto l3       = mm->add_instruction(
178
179
            migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l1->get_shape().lens()}}),
            l2);
Shucai Xiao's avatar
Shucai Xiao committed
180
        mm->add_instruction(migraphx::make_op("add"), l1, l3);
181
        p.compile(migraphx::make_target("ref"));
182
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
183
184
        EXPECT(result.get_shape().packed());
        std::vector<float> results_vector(12);
185
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
186
        std::vector<float> gold = {0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8};
187
188
189
190
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
    {
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
191
192
193
194
195
196
197
        auto* mm = p.get_main_module();
        migraphx::shape a_shape{migraphx::shape::float_type, {2, 2, 3}};
        std::vector<float> a_data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        migraphx::shape b_shape{migraphx::shape::float_type, {2, 2, 1}};
        std::vector<float> b_data{0, -1, -2, -3};
        auto l1 = mm->add_literal(migraphx::literal{a_shape, a_data});
        auto l2 = mm->add_literal(migraphx::literal{b_shape, b_data});
198
199
200
201
        auto l3 =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 3}}}), l1);
        auto l4 =
            mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 3}}}), l2);
Shucai Xiao's avatar
Shucai Xiao committed
202
        mm->add_instruction(migraphx::make_op("add"), l3, l4);
203
        p.compile(migraphx::make_target("ref"));
204
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
205
206
        EXPECT(result.get_shape().packed());
        std::vector<float> results_vector(12);
207
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
208
        std::vector<float> gold = {0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8};
209
210
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
Shucai Xiao's avatar
Shucai Xiao committed
211
}
212

Shucai Xiao's avatar
Shucai Xiao committed
213
214
215
216
217
218
219
220
TEST_CASE(add_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = mm->add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = mm->add_literal(migraphx::literal{s, {1, 2, 3}});
    mm->add_instruction(migraphx::make_op("add"), l1, l2);
221
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
222
223
224
225
226
227
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0, 2, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}
Shucai Xiao's avatar
Shucai Xiao committed
228

229
230
231
232
TEST_CASE(add_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
233
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
234
235
236
237
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x = mm->add_parameter("x", s);
    auto y = mm->add_parameter("y", s);
    mm->add_instruction(migraphx::make_op("add"), x, y);
238
    p.compile(migraphx::make_target("ref"));
239
240
241
242
243
244
245
246
247
248
249
250
251
252

    std::vector<float> x_data{-1, 0, 1};
    std::vector<float> y_data{1, 2, 3};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0, 2, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
253
254
255
256
257
258
259
260
261
262
263
TEST_CASE(argmax_test_0)
{
    migraphx::program p;
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", 0}}), dl);
264
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
265
266
267
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
268

Shucai Xiao's avatar
Shucai Xiao committed
269
270
    EXPECT(migraphx::verify_range(result_vec, res_gold));
}
Shucai Xiao's avatar
Shucai Xiao committed
271

Shucai Xiao's avatar
Shucai Xiao committed
272
273
274
275
276
277
278
279
280
281
282
TEST_CASE(argmax_test_1)
{
    migraphx::program p;
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {0, 0, 2, 1, 2, 0, 0, 2};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", 1}}), dl);
283
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
284
285
286
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
287

Shucai Xiao's avatar
Shucai Xiao committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    EXPECT(migraphx::verify_range(result_vec, res_gold));
}

TEST_CASE(argmax_test_2)
{
    migraphx::program p;
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {1, 3, 2, 2, 2, 3};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", 2}}), dl);
302
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
303
304
305
306
307
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(result_vec, res_gold));
308
309
}

Shucai Xiao's avatar
Shucai Xiao committed
310
TEST_CASE(argmax_test_neg_2)
311
{
Paul's avatar
Paul committed
312
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
313
314
315
316
317
318
319
320
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {0, 0, 2, 1, 2, 0, 0, 2};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", -2}}), dl);
321
    p.compile(migraphx::make_target("ref"));
322
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
323
324
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
325

Shucai Xiao's avatar
Shucai Xiao committed
326
    EXPECT(migraphx::verify_range(result_vec, res_gold));
327
328
}

Charlie Lin's avatar
Charlie Lin committed
329
330
331
332
TEST_CASE(argmax_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
333
    migraphx::shape s{migraphx::shape::float_type, {{2, 2}, {3, 6}, {3, 6}}};
Charlie Lin's avatar
Charlie Lin committed
334
335
    auto dl = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", 0}}), dl);
336
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350

    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 3, 4}};
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
    std::vector<int64_t> res_gold = {0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1};
    EXPECT(migraphx::verify_range(result_vec, res_gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
351
TEST_CASE(argmin_test_0)
352
{
Paul's avatar
Paul committed
353
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
354
355
356
357
358
359
360
361
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmin", {{"axis", 0}}), dl);
362
    p.compile(migraphx::make_target("ref"));
363
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
364
365
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
366

Shucai Xiao's avatar
Shucai Xiao committed
367
    EXPECT(migraphx::verify_range(result_vec, res_gold));
368
369
}

Shucai Xiao's avatar
Shucai Xiao committed
370
TEST_CASE(argmin_test_1)
Scott Thornton's avatar
Scott Thornton committed
371
{
Shucai Xiao's avatar
Shucai Xiao committed
372
373
374
375
376
377
378
379
380
    migraphx::program p;
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {2, 2, 0, 2, 0, 1, 2, 0};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmin", {{"axis", 1}}), dl);
381
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
382
383
384
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
Scott Thornton's avatar
Scott Thornton committed
385

Shucai Xiao's avatar
Shucai Xiao committed
386
387
    EXPECT(migraphx::verify_range(result_vec, res_gold));
}
Scott Thornton's avatar
Scott Thornton committed
388

Shucai Xiao's avatar
Shucai Xiao committed
389
390
TEST_CASE(argmin_test_2)
{
Paul's avatar
Paul committed
391
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
392
393
394
395
396
397
398
399
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {2, 1, 0, 3, 3, 2};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmin", {{"axis", 2}}), dl);
400
    p.compile(migraphx::make_target("ref"));
401
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
402
403
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
Scott Thornton's avatar
Scott Thornton committed
404

Shucai Xiao's avatar
Shucai Xiao committed
405
    EXPECT(migraphx::verify_range(result_vec, res_gold));
Scott Thornton's avatar
Scott Thornton committed
406
407
}

Shucai Xiao's avatar
Shucai Xiao committed
408
TEST_CASE(argmin_test_neg_1)
Scott Thornton's avatar
Scott Thornton committed
409
{
Shucai Xiao's avatar
Shucai Xiao committed
410
411
412
413
414
415
416
417
418
    migraphx::program p;
    auto* mm                = p.get_main_module();
    std::vector<float> data = {1.2255,  1.6834,  -2.0305, -0.3221, 0.4701,  0.2583, 0.7545, 2.5758,
                               -1.6849, 0.0928,  0.9022,  -0.8765, -0.4090, 0.9301, 2.0724, -1.5706,
                               0.4867,  -0.1493, 0.6957,  -0.2179, 0.7142,  0.7177, 0.0183, 1.3497};
    std::vector<int64_t> res_gold = {2, 1, 0, 3, 3, 2};
    migraphx::shape data_shape{migraphx::shape::float_type, {2, 3, 4}};
    auto dl = mm->add_literal(migraphx::literal{data_shape, data});
    mm->add_instruction(migraphx::make_op("argmin", {{"axis", -1}}), dl);
419
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
420
421
422
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
Scott Thornton's avatar
Scott Thornton committed
423

Shucai Xiao's avatar
Shucai Xiao committed
424
425
    EXPECT(migraphx::verify_range(result_vec, res_gold));
}
Scott Thornton's avatar
Scott Thornton committed
426

Shucai Xiao's avatar
Shucai Xiao committed
427
428
TEST_CASE(asin_test)
{
Paul's avatar
Paul committed
429
    migraphx::program p;
430
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
431
432
433
434
    migraphx::shape s{migraphx::shape::float_type, {3}};
    std::vector<float> data{-0.5f, 0.0f, 0.9f};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("asin"), l);
435
    p.compile(migraphx::make_target("ref"));
436
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
437
    std::vector<float> results_vector(3);
Scott Thornton's avatar
Scott Thornton committed
438
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
439
440
441
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return asinf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
442
    EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
443
444
}

Charlie Lin's avatar
Charlie Lin committed
445
TEST_CASE(asin_dyn_test)
446
447
448
{
    migraphx::program p;
    auto* mm = p.get_main_module();
449
    migraphx::shape::dynamic_dimension dd{3, 8};
450
451
452
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("asin"), input);
453
    p.compile(migraphx::make_target("ref"));
454

Charlie Lin's avatar
Charlie Lin committed
455
    std::vector<float> input_data{-0.5f, 0.0f, 0.9f};
456
457
458
459
460
461
462
463
464
465
466
467
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return asinf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
468
TEST_CASE(asinh_test)
Scott Thornton's avatar
Scott Thornton committed
469
{
Paul's avatar
Paul committed
470
    migraphx::program p;
471
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
472
473
474
475
    migraphx::shape s{migraphx::shape::float_type, {3}};
    std::vector<float> data{-0.5f, 0.0f, 0.9f};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("asinh"), l);
476
    p.compile(migraphx::make_target("ref"));
477
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
478
    std::vector<float> results_vector(3);
Scott Thornton's avatar
Scott Thornton committed
479
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
480
481
482
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return asinhf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
483
    EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
484
485
}

Charlie Lin's avatar
Charlie Lin committed
486
TEST_CASE(asinh_dyn_test)
487
488
489
{
    migraphx::program p;
    auto* mm = p.get_main_module();
490
    migraphx::shape::dynamic_dimension dd{3, 8};
491
492
493
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("asinh"), input);
494
    p.compile(migraphx::make_target("ref"));
495

Charlie Lin's avatar
Charlie Lin committed
496
    std::vector<float> input_data{-0.5f, 0.0f, 0.9f};
497
498
499
500
501
502
503
504
505
506
507
508
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return asinhf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
509
TEST_CASE(atan_test)
Scott Thornton's avatar
Scott Thornton committed
510
{
Paul's avatar
Paul committed
511
    migraphx::program p;
512
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
513
    migraphx::shape s{migraphx::shape::double_type, {3}};
514
515
    std::vector<float> data{-1.0f, 0.0f, 1.0f};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
516
    mm->add_instruction(migraphx::make_op("atan"), l);
517
    p.compile(migraphx::make_target("ref"));
518
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
519
520
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
521
522
523
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return atanf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
524
525
    EXPECT(migraphx::verify_range(results_vector, gold));
}
Scott Thornton's avatar
Scott Thornton committed
526

Charlie Lin's avatar
Charlie Lin committed
527
TEST_CASE(atan_dyn_test)
528
529
530
{
    migraphx::program p;
    auto* mm = p.get_main_module();
531
    migraphx::shape::dynamic_dimension dd{3, 8};
532
533
534
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("atan"), input);
535
    p.compile(migraphx::make_target("ref"));
536

Charlie Lin's avatar
Charlie Lin committed
537
    std::vector<float> input_data{-1.0f, 0.0f, 1.0f};
538
539
540
541
542
543
544
545
546
547
548
549
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return atanf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
550
551
552
553
554
TEST_CASE(atanh_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {3}};
555
556
    std::vector<float> data{0.4435683f, 0.6223626f, 0.316958f};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
557
    mm->add_instruction(migraphx::make_op("atanh"), l);
558
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
559
560
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
Scott Thornton's avatar
Scott Thornton committed
561
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
562
563
564
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return atanhf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
565
    EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
566
567
}

Charlie Lin's avatar
Charlie Lin committed
568
TEST_CASE(atanh_dyn_test)
569
570
571
{
    migraphx::program p;
    auto* mm = p.get_main_module();
572
    migraphx::shape::dynamic_dimension dd{3, 8};
573
574
575
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("atanh"), input);
576
    p.compile(migraphx::make_target("ref"));
577

Charlie Lin's avatar
Charlie Lin committed
578
    std::vector<float> input_data{0.4435683f, 0.6223626f, 0.316958f};
579
580
581
582
583
584
585
586
587
588
589
590
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return atanhf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
591
TEST_CASE(avgpool_rank3_test)
Shucai Xiao's avatar
Shucai Xiao committed
592
{
Shucai Xiao's avatar
Shucai Xiao committed
593
    // 1D case 1, input is 3D
Charlie Lin's avatar
Charlie Lin committed
594
595
596
597
598
599
600
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 4}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
    op.lengths = {2};
    op.padding = {0};
    op.stride  = {1};
Shucai Xiao's avatar
Shucai Xiao committed
601

Charlie Lin's avatar
Charlie Lin committed
602
603
604
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
605
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
606
607
608
609
610
611
612
613
614
615
616
617
    auto result = p.eval({}).back();

    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.25, 0.3, 0.25, 0.65, 0.7, 0.5, 0.4, 0.4, 0.35};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(avgpool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
618
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
619
620
621
622
623
624
625
    auto x   = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::average},
                                           {"lengths", {2}},
                                           {"padding", {0}},
                                           {"stride", {1}}}),
                        x);
626
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
627
628
629
630
631
632
633
634
635
636
637

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 4}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.25, 0.3, 0.25, 0.65, 0.7, 0.5, 0.4, 0.4, 0.35};
    EXPECT(migraphx::verify_range(results_vector, gold));
}
Shucai Xiao's avatar
Shucai Xiao committed
638

639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
TEST_CASE(avgpool_dyn_pad_test)
{
    // pooling with dynamic input and padding, ceiling mode for output size
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 4}, {1, 3}, {2, 4}, {2, 4}}};
    auto x   = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::average},
                                           {"lengths", {2, 2}},
                                           {"padding", {1, 0}},
                                           {"ceil_mode", true},
                                           {"stride", {2, 2}}}),
                        x);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> data{1, 2, 3, 4, 5, 6};

    //      *  *  *
    //      1  2  3        padding will look like this
    //      4  5  6        The * are used when tiling the kernel
    //      *  *  *        but are ignored in averaging

    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 1, 2, 3}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1.5, 3.0, 4.5, 6.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(avgpool_dyn_pad_ceil_test)
{
    // pooling with dynamic input and padding
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 4}, {1, 3}, {2, 4}, {2, 4}}};
    auto x   = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::average},
                                           {"lengths", {2, 3}},
                                           {"padding", {1, 2}},
                                           {"ceil_mode", true},
                                           {"stride", {1, 1}}}),
                        x);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> data{1, 2, 3, 4};

    //  * *  *  * * *
    //  * *  1  2 * *      padded input will look like this
    //  * *  3  4 * *      but the * are ignored in averaging
    //  * *  *  * * *

    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 1, 2, 2}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    // clang-format off
    std::vector<float> gold{1.0, 1.5, 1.5, 2.0, 
                            2.0, 2.5, 2.5, 3.0, 
                            3.0, 3.5, 3.5, 4.0};
    // clang-format on
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
709
710
TEST_CASE(avgpool_rank3_stride2_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
711
    // 1D case 2, stride 2
Charlie Lin's avatar
Charlie Lin committed
712
713
714
715
716
717
718
719
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {2, 2, 4}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
    op.lengths = {2};
    op.padding = {1};
    op.stride  = {2};

720
721
722
723
724
725
    // clang-format off
    std::vector<float> data{1.6321, -2.4186, 0.2239, -1.4232, 
                            0.8158, 0.4103, -0.3149, -0.1361,
                            -0.3442, 2.007, 0.4331, 1.5295,
                            0.9965, 0.4766, 1.0942, -0.2915};
    // clang-format on
Charlie Lin's avatar
Charlie Lin committed
726
727
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
728
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
729
730
731
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
732
733
734
735
736
737
738

    // clang-format off
    std::vector<float> gold{1.6321, -1.09735, -1.4232,
                            0.8158, 0.0477, -0.1361, 
                            -0.3442, 1.22005, 1.5295,
                            0.9965, 0.7854, -0.2915};
    // clang-format on
Charlie Lin's avatar
Charlie Lin committed
739
740
    EXPECT(migraphx::verify_range(results_vector, gold));
}
Shucai Xiao's avatar
Shucai Xiao committed
741

Charlie Lin's avatar
Charlie Lin committed
742
743
TEST_CASE(avgpool_rank5_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
744
    // 3D, input is 5D
Charlie Lin's avatar
Charlie Lin committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {2, 2, 3, 3, 3}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
    op.lengths = {2, 2, 2};
    op.padding = {0, 0, 0};
    op.stride  = {1, 1, 1};

    std::vector<float> data{
        -0.179, -1.756, 0.651,  1.955,  1.87,   -0.604, 0.247,  0.449,  -0.137, 1.187,  1.593,
        0.424,  2.698,  -0.104, -0.069, -1.293, 0.538,  1.291,  0.974,  1.096,  0.74,   -0.669,
        -1.08,  -1.041, -1.407, 1.43,   -0.211, -0.017, 0.532,  1.276,  0.627,  0.236,  -0.396,
        -0.204, 0.501,  -0.599, -1.414, -0.615, -0.274, 0.168,  -0.144, 0.5,    1.42,   1.082,
        -0.952, -0.846, -1.244, 1.475,  1.246,  1.344,  -1.722, -1.24,  -0.851, 0.06,   0.507,
        0.762,  -0.007, -1.484, 1.028,  0.317,  1.077,  -1.289, 0.875,  -0.417, -0.673, 1.715,
        -0.307, 0.264,  -0.973, 1.412,  2.561,  -0.515, -0.201, 0.827,  -1.231, 1.958,  -0.552,
        0.036,  -0.993, -0.859, -1.458, -0.575, 0.048,  -0.779, -1.025, -1.135, 1.166,  -0.131,
        0.726,  0.52,   0.467,  -0.494, 0.675,  0.203,  -0.63,  -0.918, -0.5,   -1.395, 1.39,
        1.705,  0.444,  -0.835, -0.506, 0.101,  0.602,  0.543,  0.357,  1.042};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
766
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
767
768
769
770
771
772
773
774
775
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{
        0.908,     0.250625,  0.795,     0.40425, 0.711875,  0.194875,  0.014125,  0.09425,
        -0.078375, 0.139375,  0.46075,   0.0285,  -0.188125, -0.085,    0.378125,  -0.085375,
        -0.04,     0.304125,  0.40775,   0.2835,  0.112375,  -0.073375, 0.4355,    -0.187,
        -0.392625, -0.258375, -0.485875, -0.0345, 0.16125,   -0.131875, -0.228375, 0.068625};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
776
777
}

Shucai Xiao's avatar
Shucai Xiao committed
778
TEST_CASE(broadcast_test)
Shucai Xiao's avatar
Shucai Xiao committed
779
780
{
    migraphx::program p;
781
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
782
783
784
785
786
787
788
789
    migraphx::shape a_shape{migraphx::shape::int32_type, {2, 2}};
    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    uint64_t axis = 0;
    auto l1       = mm->add_literal(migraphx::literal{a_shape, a_data});
    auto l2       = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(
790
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l1->get_shape().lens()}}), l2);
791
    p.compile(migraphx::make_target("ref"));
792
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
793
794
795
796
797
    auto output = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -2);
    EXPECT(output(1, 0) == -3);
    EXPECT(output(1, 1) == -3);
Shucai Xiao's avatar
Shucai Xiao committed
798
799
}

800
801
802
803
804
805
806
807
808
809
810
811
TEST_CASE(broadcast_2in_static_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::int32_type, {2, 2}};
    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    uint64_t axis = 0;
    auto l1       = mm->add_literal(migraphx::literal{a_shape, a_data});
    auto l2       = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(migraphx::make_op("broadcast", {{"axis", axis}}), l2, l1);
812
    p.compile(migraphx::make_target("ref"));
813
814
815
816
817
818
819
820
821
822
823
824
    auto result = p.eval({}).back();
    auto output = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -2);
    EXPECT(output(1, 0) == -3);
    EXPECT(output(1, 1) == -3);
}

TEST_CASE(broadcast_2in_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
825
    migraphx::shape a_shape{migraphx::shape::int32_type, {{2, 2}, {2, 4}}};
826
827
828
829
830
831
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    uint64_t axis = 0;
    auto pa       = mm->add_parameter("a", a_shape);
    auto lb       = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(migraphx::make_op("broadcast", {{"axis", axis}}), lb, pa);
832
    p.compile(migraphx::make_target("ref"));
833
834
835
836
837
838
839
840
841
842
843
844
845

    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::shape input_fixed_shape0{migraphx::shape::int32_type, {2, 2}};
    migraphx::parameter_map params0;
    params0["a"] = migraphx::argument(input_fixed_shape0, a_data.data());
    auto result  = p.eval(params0).back();
    auto output  = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -2);
    EXPECT(output(1, 0) == -3);
    EXPECT(output(1, 1) == -3);
}

Shucai Xiao's avatar
Shucai Xiao committed
846
TEST_CASE(ceil_test)
847
848
{
    migraphx::program p;
849
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
850
    migraphx::shape s{migraphx::shape::float_type, {9}};
851
852
    std::vector<float> data = {1.1, 1.5, 1.6, -1.1, -1.5, -1.6, 0.0, 2.0, -2.0};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
853
    mm->add_instruction(migraphx::make_op("ceil"), l);
854
    p.compile(migraphx::make_target("ref"));
855
    auto result = p.eval({}).back();
856
857
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
858
859
860
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return std::ceil(n); });
861
862
863
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
864
TEST_CASE(ceil_dyn_test)
865
866
867
{
    migraphx::program p;
    auto* mm = p.get_main_module();
868
    migraphx::shape::dynamic_dimension dd{4, 12};
869
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
870
    auto input = mm->add_parameter("X", s);
871
    mm->add_instruction(migraphx::make_op("ceil"), input);
872
    p.compile(migraphx::make_target("ref"));
873

Charlie Lin's avatar
Charlie Lin committed
874
    std::vector<float> input_data = {1.1, 1.5, 1.6, -1.1, -1.5, -1.6, 0.0, 2.0, -2.0};
875
876
877
878
879
880
881
882
883
884
885
886
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return std::ceil(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
887
TEST_CASE(clip_test)
Shucai Xiao's avatar
Shucai Xiao committed
888
889
{
    migraphx::program p;
890
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
891
    migraphx::shape s{migraphx::shape::float_type, {3}};
Shucai Xiao's avatar
Shucai Xiao committed
892
893
894
895
    auto l       = mm->add_literal(migraphx::literal{s, {-1.0, 0.0, 10.0}});
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
    min_val =
896
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), min_val);
Shucai Xiao's avatar
Shucai Xiao committed
897
    max_val =
898
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), max_val);
Shucai Xiao's avatar
Shucai Xiao committed
899
    mm->add_instruction(migraphx::make_op("clip"), l, min_val, max_val);
900
    p.compile(migraphx::make_target("ref"));
901
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
902
903
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
904
    std::vector<float> gold = {0.0, 0.0, 6.0};
Shucai Xiao's avatar
Shucai Xiao committed
905
906
907
    EXPECT(migraphx::verify_range(results_vector, gold));
}

908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
TEST_CASE(clip_dyn_test)
{
    migraphx::program p;
    auto* mm                                            = p.get_main_module();
    std::vector<migraphx::shape::dynamic_dimension> dds = {{2, 8, {3}}};
    migraphx::shape s{migraphx::shape::float_type, dds};
    auto l       = mm->add_parameter("X", s);
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
    min_val      = mm->add_instruction(migraphx::make_op("multibroadcast"), min_val, l);
    max_val      = mm->add_instruction(migraphx::make_op("multibroadcast"), max_val, l);
    mm->add_instruction(migraphx::make_op("clip"), l, min_val, max_val);
    p.compile(migraphx::make_target("ref"));

    migraphx::shape static_shape{migraphx::shape::float_type, {3}};
    migraphx::parameter_map params;
    std::vector<float> data = {-1.0, 0.0, 10.0};
    params["X"]             = migraphx::argument(static_shape, data.data());
    auto result             = p.eval(params).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.0, 0.0, 6.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
TEST_CASE(concat_test)
{
    {
        migraphx::program p;
        auto* mm               = p.get_main_module();
        int 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};
        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 = mm->add_literal(migraphx::literal{s0, data0});
        auto l1 = mm->add_literal(migraphx::literal{s1, data1});
        auto l2 = mm->add_literal(migraphx::literal{s2, data2});
        mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1, l2);
949
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
        auto result           = p.eval({}).back();
        std::vector<int> gold = {0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9, 20};
        std::vector<int> results_vector(2 * 6);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({2, 6})));
        EXPECT(
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({6, 1})));
    }

    {
        migraphx::program p;
        auto* mm               = p.get_main_module();
        int 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};
        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 = mm->add_literal(migraphx::literal{s0, data0});
        auto l1 = mm->add_literal(migraphx::literal{s1, data1});
        auto l2 = mm->add_literal(migraphx::literal{s2, data2});
        mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1, l2);
974
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
        auto result           = p.eval({}).back();
        std::vector<int> gold = {0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9, 20};
        std::vector<int> results_vector(2 * 6);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({2, 6})));
        EXPECT(
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({6, 1})));
    }

    {
        migraphx::program p;
        auto* mm               = p.get_main_module();
        int 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};
        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 = mm->add_literal(migraphx::literal{s0, data0});
        auto l1 = mm->add_literal(migraphx::literal{s1, data1});
        auto l2 = mm->add_literal(migraphx::literal{s2, data2});
        mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1, l2);
999
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
        auto result           = p.eval({}).back();
        std::vector<int> gold = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        std::vector<int> results_vector(6 * 2);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({6, 2})));
        EXPECT(
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({2, 1})));
    }

    {
        migraphx::program p;
        auto* mm               = p.get_main_module();
        int axis               = -2;
        std::vector<int> data0 = {0, 1, 2, 3};
        std::vector<int> data1 = {4, 5, 6, 7, 8, 9};
        std::vector<int> data2 = {10, 11};
        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 = mm->add_literal(migraphx::literal{s0, data0});
        auto l1 = mm->add_literal(migraphx::literal{s1, data1});
        auto l2 = mm->add_literal(migraphx::literal{s2, data2});
        mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1, l2);
1024
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
        auto result           = p.eval({}).back();
        std::vector<int> gold = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        std::vector<int> results_vector(6 * 2);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({6, 2})));
        EXPECT(
            migraphx::verify_range(result.get_shape().strides(), std::vector<std::size_t>({2, 1})));
    }
}

1036
1037
1038
1039
1040
TEST_CASE(concat_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    int axis = 0;
1041
1042
1043
    migraphx::shape s0{migraphx::shape::int32_type, {{2, 4, {2}}, {2, 3, {2}}}};
    migraphx::shape s1{migraphx::shape::int32_type, {{3, 4, {4}}, {2, 3, {2}}}};
    migraphx::shape s2{migraphx::shape::int32_type, {{1, 5, {3}}, {2, 3, {2}}}};
1044
1045
1046
1047
1048

    auto input0 = mm->add_parameter("X", s0);
    auto input1 = mm->add_parameter("Y", s1);
    auto input2 = mm->add_parameter("Z", s2);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), input0, input1, input2);
1049
    p.compile(migraphx::make_target("ref"));
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070

    migraphx::shape static_shape0{migraphx::shape::int32_type, {2, 2}};
    migraphx::shape static_shape1{migraphx::shape::int32_type, {3, 2}};
    migraphx::shape static_shape2{migraphx::shape::int32_type, {1, 2}};
    std::vector<int> data0 = {0, 1, 2, 3};
    std::vector<int> data1 = {4, 5, 6, 7, 8, 9};
    std::vector<int> data2 = {10, 11};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(static_shape0, data0.data());
    params["Y"] = migraphx::argument(static_shape1, data1.data());
    params["Z"] = migraphx::argument(static_shape2, data2.data());
    auto result = p.eval(params).back();

    std::vector<int> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    std::vector<float> gold = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    EXPECT(migraphx::verify_range(results_vector, gold));
    EXPECT(migraphx::verify_range(result.get_shape().lens(), std::vector<std::size_t>({6, 2})));
}

Shucai Xiao's avatar
Shucai Xiao committed
1071
TEST_CASE(contiguous_test)
Shucai Xiao's avatar
Shucai Xiao committed
1072
{
Shucai Xiao's avatar
Shucai Xiao committed
1073
1074
1075
1076
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 3, 2, 2}, {12, 1, 6, 3}};
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);

Shucai Xiao's avatar
Shucai Xiao committed
1077
    migraphx::program p;
1078
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
1079
1080
    auto l   = mm->add_literal(migraphx::literal{a_shape, data});
    mm->add_instruction(migraphx::make_op("contiguous"), l);
1081
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
1082
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1083

1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
    std::vector<size_t> new_strides = {12, 4, 2, 1};
    EXPECT(result.get_shape().strides() == new_strides);

    std::vector<float> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    std::vector<float> gold = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(contiguous_param_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 3, 2, 2}, {12, 1, 6, 3}};
    auto a = mm->add_parameter("X", a_shape);
    mm->add_instruction(migraphx::make_op("contiguous"), a);
1101
    p.compile(migraphx::make_target("ref"));
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111

    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(a_shape, data.data());
    auto result = p.eval(params).back();

    std::vector<size_t> new_strides = {12, 4, 2, 1};
    EXPECT(result.get_shape().strides() == new_strides);

Shucai Xiao's avatar
Shucai Xiao committed
1112
    std::vector<float> results_vector(12);
Shucai Xiao's avatar
Shucai Xiao committed
1113
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
1114
1115
    std::vector<float> gold = {0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
1116
1117
}

Charlie Lin's avatar
Charlie Lin committed
1118
1119
1120
1121
TEST_CASE(contiguous_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1122
    migraphx::shape dyn_shape{migraphx::shape::float_type, {{1, 1}, {2, 6}, {2, 2}, {2, 2}}};
Charlie Lin's avatar
Charlie Lin committed
1123
1124
    auto input = mm->add_parameter("X", dyn_shape);
    mm->add_instruction(migraphx::make_op("contiguous"), input);
1125
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143

    migraphx::shape static_shape{migraphx::shape::float_type, {1, 3, 2, 2}, {12, 1, 6, 3}};
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(static_shape, data.data());
    auto result = p.eval(params).back();

    std::vector<size_t> new_strides = {12, 4, 2, 1};
    EXPECT(result.get_shape().strides() == new_strides);

    std::vector<float> results_vector(12);
    result.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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
1144
TEST_CASE(conv_dyn_batch_test)
1145
1146
1147
1148
1149
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape input_dyn_shape{migraphx::shape::float_type,
1150
                                    {{1, 100}, {3, 3}, {4, 4}, {4, 4}}};
1151
1152
1153
1154
1155
1156
1157
1158
    migraphx::shape weights_shape{migraphx::shape::float_type, {2, 3, 3, 3}};

    auto input   = mm->add_parameter("X", input_dyn_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}, {"stride", {2, 2}}}),
                        input,
                        weights);

1159
    p.compile(migraphx::make_target("ref"));
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216

    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> sol = {-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};

    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3, 4, 4}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_fixed_shape0, a.data());
    params0["W"] = migraphx::argument(weights_shape, c.data());

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(64);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, sol));
}

Charlie Lin's avatar
Charlie Lin committed
1217
TEST_CASE(conv_dyn_img_shape_test)
1218
1219
1220
1221
{
    migraphx::program p;
    auto* mm = p.get_main_module();

1222
    migraphx::shape input_dyn_shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {4, 6}, {4, 6}}};
1223
1224
1225
1226
1227
1228
1229
1230
    migraphx::shape weights_shape{migraphx::shape::float_type, {1, 3, 3, 3}};

    auto input   = mm->add_parameter("X", input_dyn_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {1, 1}}}),
                        input,
                        weights);

1231
    p.compile(migraphx::make_target("ref"));
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
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
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
1301
1302
1303
1304

    std::vector<float> a = {0.28007596, 0.46114671, 0.12171969, 0.52260835, 0.40916841, 0.07163955,
                            0.09896668, 0.98628836, 0.69406788, 0.44868846, 0.64017681, 0.27048886,
                            0.30187397, 0.07334207, 0.05258557, 0.80747513, 0.81330534, 0.00497161,
                            0.33005534, 0.08908686, 0.46794691, 0.61768946, 0.55104806, 0.13406187,
                            0.70244284, 0.61296941, 0.46742536, 0.29712714, 0.91839388, 0.0834397,
                            0.14476327, 0.37857075, 0.25922384, 0.61620963, 0.69455439, 0.70389431,
                            0.77388606, 0.1752363,  0.74631394, 0.24604889, 0.53600244, 0.22116457,
                            0.81217463, 0.10789447, 0.43083784, 0.63371852, 0.69742316, 0.09536905};

    std::vector<float> c = {0.98411968, 0.2899219,  0.44638833, 0.30390816, 0.03989896, 0.2445332,
                            0.32700131, 0.57517075, 0.06956476, 0.93079306, 0.19882314, 0.52940601,
                            0.35624753, 0.35938406, 0.9111428,  0.88923574, 0.61040283, 0.2797513,
                            0.15479768, 0.46534674, 0.16970931, 0.49704618, 0.07062198, 0.01678321,
                            0.53150934, 0.39244495, 0.9963813};

    std::vector<float> sol = {6.1329393, 4.3199925, 5.448438, 3.8497565};

    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {1, 3, 4, 4}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_fixed_shape0, a.data());
    params0["W"] = migraphx::argument(weights_shape, c.data());

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(72);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, sol));

    a = {0.95600171, 0.20768181, 0.82844489, 0.14928212, 0.51280462, 0.1359196,  0.68903648,
         0.84174772, 0.425509,   0.956926,   0.82533291, 0.33821531, 0.57576055, 0.75330186,
         0.82710394, 0.93343847, 0.14499469, 0.74558021, 0.13935139, 0.90652876, 0.22611443,
         0.85323975, 0.30631787, 0.96983037, 0.51783421, 0.32247456, 0.28243352, 0.605865,
         0.33376446, 0.67864877, 0.15442507, 0.24977552, 0.86989425, 0.60036782, 0.26198306,
         0.1494149,  0.13678915, 0.24892094, 0.38282467, 0.64907906, 0.83756376, 0.77603195,
         0.33951558, 0.14856874, 0.45701939, 0.43786436, 0.57421759, 0.37326922, 0.63382506,
         0.11464436, 0.23309047, 0.76724102, 0.98712427, 0.80800108, 0.84296564, 0.79568268,
         0.45684131, 0.73867068, 0.57845499, 0.45073557, 0.27102442, 0.86460315, 0.06865567,
         0.81673446, 0.881835,   0.42351639, 0.83322931, 0.34101671, 0.51979151, 0.54920645,
         0.19287718, 0.33321689, 0.27752456, 0.45755893, 0.67484562, 0.68383122, 0.52361312,
         0.46437257, 0.50862936, 0.32460429, 0.1726007,  0.29933345, 0.64856728, 0.06471591,
         0.63370843, 0.27900152, 0.18595992, 0.48904812, 0.35368508, 0.09620202};

    c = {0.709561,   0.7916206,  0.0443115,  0.62592275, 0.2498623,  0.42725624, 0.7905135,
         0.53160169, 0.01303743, 0.01987505, 0.39041803, 0.89530203, 0.23155373, 0.44435213,
         0.14407301, 0.80968594, 0.38216188, 0.35692557, 0.2568538,  0.83587388, 0.43654904,
         0.04974508, 0.80375029, 0.25350374, 0.1820275,  0.23369029, 0.54358755};

    sol = {6.305986,
           5.564665,
           6.122996,
           5.7262855,
           5.5546584,
           5.779489,
           5.798161,
           5.160476,
           6.702436,
           5.4851074,
           6.227567,
           5.2016754};
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {1, 3, 6, 5}};

    migraphx::parameter_map params1;
    params1["X"] = migraphx::argument(input_fixed_shape1, a.data());
    params1["W"] = migraphx::argument(weights_shape, c.data());

    result = p.eval(params1).back();
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, sol));
}

Charlie Lin's avatar
Charlie Lin committed
1305
TEST_CASE(conv_dyn_weights_shape_test)
1306
1307
1308
1309
1310
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape input_shape{migraphx::shape::float_type, {1, 3, 4, 4}};
1311
    migraphx::shape weights_shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 3}, {2, 3}}};
1312
1313
1314
1315
1316
1317
1318

    auto input   = mm->add_parameter("X", input_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {1, 1}}}),
                        input,
                        weights);

1319
    p.compile(migraphx::make_target("ref"));
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380

    std::vector<float> a = {0.28007596, 0.46114671, 0.12171969, 0.52260835, 0.40916841, 0.07163955,
                            0.09896668, 0.98628836, 0.69406788, 0.44868846, 0.64017681, 0.27048886,
                            0.30187397, 0.07334207, 0.05258557, 0.80747513, 0.81330534, 0.00497161,
                            0.33005534, 0.08908686, 0.46794691, 0.61768946, 0.55104806, 0.13406187,
                            0.70244284, 0.61296941, 0.46742536, 0.29712714, 0.91839388, 0.0834397,
                            0.14476327, 0.37857075, 0.25922384, 0.61620963, 0.69455439, 0.70389431,
                            0.77388606, 0.1752363,  0.74631394, 0.24604889, 0.53600244, 0.22116457,
                            0.81217463, 0.10789447, 0.43083784, 0.63371852, 0.69742316, 0.09536905};

    std::vector<float> c   = {0.98411968,
                            0.2899219,
                            0.44638833,
                            0.30390816,
                            0.03989896,
                            0.2445332,
                            0.32700131,
                            0.57517075,
                            0.06956476,
                            0.93079306,
                            0.19882314,
                            0.52940601};
    std::vector<float> sol = {1.9939406,
                              2.2703054,
                              1.8896171,
                              2.062202,
                              2.3035214,
                              1.629366,
                              2.1606991,
                              2.1917608,
                              1.6797699};

    migraphx::shape weight_fixed_shape0{migraphx::shape::float_type, {1, 3, 2, 2}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_shape, a.data());
    params0["W"] = migraphx::argument(weight_fixed_shape0, c.data());

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(72);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, sol));

    c   = {0.98411968, 0.2899219,  0.44638833, 0.30390816, 0.03989896, 0.2445332,  0.32700131,
         0.57517075, 0.06956476, 0.93079306, 0.19882314, 0.52940601, 0.35624753, 0.35938406,
         0.9111428,  0.88923574, 0.61040283, 0.2797513,  0.15479768, 0.46534674, 0.16970931,
         0.49704618, 0.07062198, 0.01678321, 0.53150934, 0.39244495, 0.9963813};
    sol = {6.1329393, 4.3199925, 5.448438, 3.8497565};
    migraphx::shape weights_fixed_shape1{migraphx::shape::float_type, {1, 3, 3, 3}};

    migraphx::parameter_map params1;
    params1["X"] = migraphx::argument(input_shape, a.data());
    params1["W"] = migraphx::argument(weights_fixed_shape1, c.data());

    result = p.eval(params1).back();
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, sol));
}

Charlie Lin's avatar
Charlie Lin committed
1381
TEST_CASE(conv_dyn_img_same_upper_test)
1382
1383
1384
1385
{
    migraphx::program p;
    auto* mm = p.get_main_module();

1386
    migraphx::shape input_dyn_shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {4, 6}, {4, 6}}};
1387
1388
1389
1390
1391
    migraphx::shape weights_shape{migraphx::shape::float_type, {1, 3, 3, 3}};

    auto input   = mm->add_parameter("X", input_dyn_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(
1392
1393
1394
        migraphx::make_op(
            "convolution",
            {{"stride", {1, 1}}, {"padding_mode", migraphx::op::padding_mode_t::same_upper}}),
1395
1396
1397
        input,
        weights);

1398
    p.compile(migraphx::make_target("ref"));
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450

    std::vector<float> a = {0.63321185, 0.6466339,  0.8515352,  0.44240063, 0.5018913,  0.5068494,
                            0.75330657, 0.7383877,  0.15870683, 0.8171611,  0.56118083, 0.87004256,
                            0.24401724, 0.8815178,  0.4222333,  0.27191755,

                            0.41633207, 0.2460619,  0.32004243, 0.6962248,  0.12284133, 0.2620491,
                            0.96931046, 0.6030955,  0.7623861,  0.2395751,  0.61440414, 0.577285,
                            0.80087787, 0.12776066, 0.26566318, 0.46569306,

                            0.96701574, 0.3850145,  0.14165345, 0.5887347,  0.7152134,  0.5295342,
                            0.6303507,  0.4037548,  0.18556239, 0.79416305, 0.29107493, 0.18770285,
                            0.6870904,  0.30701008, 0.314684,   0.91075855};

    std::vector<float> c = {
        2.8150102e-01, 3.3198616e-01, 9.5149356e-01, 7.4039467e-02, 9.6555042e-01,
        2.8815505e-01, 2.5100240e-01, 5.2186239e-01, 2.3850012e-01,

        8.2963020e-01, 3.0763101e-04, 6.7026985e-01, 1.4260857e-01, 9.7517288e-01,
        3.6847427e-02, 8.5804445e-01, 7.3440993e-01, 6.7948365e-01,

        7.9253986e-02, 7.3943835e-01, 1.7813577e-01, 1.0780835e-01, 4.2304707e-01,
        4.0084350e-01, 1.1114500e-01, 4.4846520e-01, 5.0109702e-01};

    std::vector<float> sol = {3.013387,
                              3.7111127,
                              4.2946506,
                              3.579301,
                              4.5306826,
                              6.1262493,
                              6.332169,
                              4.495293,
                              4.46013,
                              6.0938954,
                              5.848162,
                              4.514299,
                              2.9587686,
                              4.117671,
                              3.5187216,
                              2.3236327};

    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {1, 3, 4, 4}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_fixed_shape0, a.data());
    params0["W"] = migraphx::argument(weights_shape, c.data());

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(16);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, sol));
}

Charlie Lin's avatar
Charlie Lin committed
1451
TEST_CASE(conv_dyn_kernel_same_upper_test)
1452
1453
1454
1455
1456
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape input_shape{migraphx::shape::float_type, {1, 3, 4, 4}};
1457
    migraphx::shape weights_shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 3}, {2, 3}}};
1458
1459
1460
1461

    auto input   = mm->add_parameter("X", input_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(
1462
1463
1464
        migraphx::make_op(
            "convolution",
            {{"stride", {1, 1}}, {"padding_mode", migraphx::op::padding_mode_t::same_upper}}),
1465
1466
1467
        input,
        weights);

1468
    p.compile(migraphx::make_target("ref"));
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516

    std::vector<float> a   = {0.63321185, 0.6466339,  0.8515352,  0.44240063, 0.5018913,  0.5068494,
                            0.75330657, 0.7383877,  0.15870683, 0.8171611,  0.56118083, 0.87004256,
                            0.24401724, 0.8815178,  0.4222333,  0.27191755,

                            0.41633207, 0.2460619,  0.32004243, 0.6962248,  0.12284133, 0.2620491,
                            0.96931046, 0.6030955,  0.7623861,  0.2395751,  0.61440414, 0.577285,
                            0.80087787, 0.12776066, 0.26566318, 0.46569306,

                            0.96701574, 0.3850145,  0.14165345, 0.5887347,  0.7152134,  0.5295342,
                            0.6303507,  0.4037548,  0.18556239, 0.79416305, 0.29107493, 0.18770285,
                            0.6870904,  0.30701008, 0.314684,   0.91075855};
    std::vector<float> c   = {2.8150102e-01,
                            3.3198616e-01,
                            9.5149356e-01,
                            7.4039467e-02,

                            9.6555042e-01,
                            2.8815505e-01,
                            2.5100240e-01,
                            5.2186239e-01,

                            2.3850012e-01,
                            8.2963020e-01,
                            3.0763101e-04,
                            6.7026985e-01};
    std::vector<float> sol = {2.453681,
                              2.536207,
                              3.0187201,
                              1.7912633,
                              2.1738236,
                              2.9695358,
                              3.2319589,
                              1.859269,
                              2.5953722,
                              2.50734,
                              2.7736917,
                              1.2229807,
                              1.5900216,
                              0.9225286,
                              1.43048,
                              0.74341124};

    migraphx::shape weight_fixed_shape0{migraphx::shape::float_type, {1, 3, 2, 2}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_shape, a.data());
    params0["W"] = migraphx::argument(weight_fixed_shape0, c.data());
1517
1518
1519
1520
1521
1522
1523

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(16);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, sol));
}

Charlie Lin's avatar
Charlie Lin committed
1524
TEST_CASE(conv_dyn_kernel_same_lower_test)
1525
1526
1527
1528
1529
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape input_shape{migraphx::shape::float_type, {1, 3, 4, 4}};
1530
    migraphx::shape weights_shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 3}, {2, 3}}};
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540

    auto input   = mm->add_parameter("X", input_shape);
    auto weights = mm->add_parameter("W", weights_shape);
    mm->add_instruction(
        migraphx::make_op(
            "convolution",
            {{"stride", {1, 1}}, {"padding_mode", migraphx::op::padding_mode_t::same_lower}}),
        input,
        weights);

1541
    p.compile(migraphx::make_target("ref"));
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589

    std::vector<float> a   = {0.63321185, 0.6466339,  0.8515352,  0.44240063, 0.5018913,  0.5068494,
                            0.75330657, 0.7383877,  0.15870683, 0.8171611,  0.56118083, 0.87004256,
                            0.24401724, 0.8815178,  0.4222333,  0.27191755,

                            0.41633207, 0.2460619,  0.32004243, 0.6962248,  0.12284133, 0.2620491,
                            0.96931046, 0.6030955,  0.7623861,  0.2395751,  0.61440414, 0.577285,
                            0.80087787, 0.12776066, 0.26566318, 0.46569306,

                            0.96701574, 0.3850145,  0.14165345, 0.5887347,  0.7152134,  0.5295342,
                            0.6303507,  0.4037548,  0.18556239, 0.79416305, 0.29107493, 0.18770285,
                            0.6870904,  0.30701008, 0.314684,   0.91075855};
    std::vector<float> c   = {2.8150102e-01,
                            3.3198616e-01,
                            9.5149356e-01,
                            7.4039467e-02,

                            9.6555042e-01,
                            2.8815505e-01,
                            2.5100240e-01,
                            5.2186239e-01,

                            2.3850012e-01,
                            8.2963020e-01,
                            3.0763101e-04,
                            6.7026985e-01};
    std::vector<float> sol = {0.91231215,
                              1.1416453,
                              1.00216,
                              1.6813052,
                              1.7131033,
                              2.453681,
                              2.536207,
                              3.0187201,
                              1.3293691,
                              2.1738236,
                              2.9695358,
                              3.2319589,
                              1.3228729,
                              2.5953722,
                              2.50734,
                              2.7736917};

    migraphx::shape weight_fixed_shape0{migraphx::shape::float_type, {1, 3, 2, 2}};

    migraphx::parameter_map params0;
    params0["X"] = migraphx::argument(input_shape, a.data());
    params0["W"] = migraphx::argument(weight_fixed_shape0, c.data());
1590
1591
1592
1593
1594
1595
1596

    auto result = p.eval(params0).back();
    std::vector<float> results_vector(16);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, sol));
}

Shucai Xiao's avatar
Shucai Xiao committed
1597
TEST_CASE(conv2d_padding_stride_test)
Shucai Xiao's avatar
Shucai Xiao committed
1598
1599
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
    auto* mm             = p.get_main_module();
    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};

    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});

    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});

    mm->add_instruction(
        migraphx::make_op("convolution", {{"padding", {1, 1}}, {"stride", {2, 2}}}), al, cl);
1652
    p.compile(migraphx::make_target("ref"));
1653
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1654
1655

    std::vector<float> results_vector(16);
Shucai Xiao's avatar
Shucai Xiao committed
1656
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1657
    EXPECT(migraphx::verify_range(results_vector, s));
Shucai Xiao's avatar
Shucai Xiao committed
1658
1659
}

Shucai Xiao's avatar
Shucai Xiao committed
1660
TEST_CASE(conv2d_padding_test)
1661
{
Paul's avatar
Paul committed
1662
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
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
1705
1706
1707
1708
1709
    auto* mm             = p.get_main_module();
    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};

    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});

    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});

    mm->add_instruction(
        migraphx::make_op("convolution", {{"padding", {1, 1}}, {"stride", {1, 1}}}), al, cl);
1710
    p.compile(migraphx::make_target("ref"));
1711
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1712
1713

    std::vector<float> results_vector(64);
1714
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1715
    EXPECT(migraphx::verify_range(results_vector, s));
1716
1717
}

Shucai Xiao's avatar
Shucai Xiao committed
1718
TEST_CASE(conv2d_test)
1719
{
Paul's avatar
Paul committed
1720
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
    auto* mm             = p.get_main_module();
    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,
                            -0.46427044};
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});

    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});

    mm->add_instruction(migraphx::make_op("convolution"), al, cl);
1774
    p.compile(migraphx::make_target("ref"));
1775
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1776
1777

    std::vector<float> results_vector(16);
1778
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1779
    EXPECT(migraphx::verify_range(results_vector, s));
1780
1781
}

Shucai Xiao's avatar
Shucai Xiao committed
1782
TEST_CASE(conv3d_test)
1783
{
Paul's avatar
Paul committed
1784
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
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
    auto* mm             = p.get_main_module();
    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,
                            -0.46427044};
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 4, 4, 1}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});

    migraphx::shape c_shape{migraphx::shape::float_type, {2, 3, 3, 3, 1}};
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});

    mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0, 0}}, {"stride", {1, 1, 1}}, {"dilation", {1, 1, 1}}}),
        al,
        cl);
1842
    p.compile(migraphx::make_target("ref"));
1843
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1844
1845

    std::vector<float> results_vector(16);
1846
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
1847
    EXPECT(migraphx::verify_range(results_vector, s));
1848
1849
}

Shucai Xiao's avatar
Shucai Xiao committed
1850
TEST_CASE(cos_test)
1851
1852
{
    migraphx::program p;
1853
    auto* mm = p.get_main_module();
1854
    migraphx::shape s{migraphx::shape::float_type, {3}};
1855
1856
    std::vector<float> data{-1, 0, 1};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
1857
    mm->add_instruction(migraphx::make_op("cos"), l);
1858
    p.compile(migraphx::make_target("ref"));
1859
    auto result = p.eval({}).back();
1860
1861
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
1862
1863
1864
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return cosf(n); });
1865
1866
1867
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
1868
TEST_CASE(cos_dyn_test)
1869
1870
1871
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1872
    migraphx::shape::dynamic_dimension dd{3, 8};
1873
1874
1875
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("cos"), input);
1876
    p.compile(migraphx::make_target("ref"));
1877

Charlie Lin's avatar
Charlie Lin committed
1878
    std::vector<float> input_data{-1, 0, 1};
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return cosf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
1891
TEST_CASE(cosh_test)
1892
1893
{
    migraphx::program p;
1894
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
1895
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
1896
1897
    std::vector<float> data = {-1.0, 2.0, -3.0, 4.0};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
1898
    mm->add_instruction(migraphx::make_op("cosh"), l);
1899
    p.compile(migraphx::make_target("ref"));
1900
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
1901
    std::vector<float> results_vector(4);
1902
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
1903
1904
1905
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return coshf(n); });
1906
1907
1908
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
1909
TEST_CASE(cosh_dyn_test)
1910
1911
1912
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1913
    migraphx::shape::dynamic_dimension dd{3, 8};
1914
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
1915
    auto input = mm->add_parameter("X", s);
1916
    mm->add_instruction(migraphx::make_op("cosh"), input);
1917
    p.compile(migraphx::make_target("ref"));
1918

Charlie Lin's avatar
Charlie Lin committed
1919
    std::vector<float> input_data = {-1.0, 2.0, -3.0, 4.0};
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return coshf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
TEST_CASE(convert_nan_upcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::half_type, {2, 2}};
    std::vector<migraphx::half> data(4, std::numeric_limits<migraphx::half>::quiet_NaN());
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4, -1);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::all_of(
        results_vector.begin(), results_vector.end(), [](const auto& x) { return std::isnan(x); }));
}

TEST_CASE(convert_nan_downcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {2, 2}};
    std::vector<double> data(4, std::numeric_limits<double>::quiet_NaN());
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4, -1);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::all_of(
        results_vector.begin(), results_vector.end(), [](const auto& x) { return std::isnan(x); }));
}

TEST_CASE(convert_nan_double_convert_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {2, 2}};
    std::vector<double> data(4, std::numeric_limits<double>::quiet_NaN());
    auto l   = mm->add_literal(migraphx::literal{s, data});
    auto f_l = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l);
    mm->add_instruction(migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}),
                        f_l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<migraphx::half> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::all_of(
        results_vector.begin(), results_vector.end(), [](const auto& x) { return std::isnan(x); }));
}

TEST_CASE(convert_nan_convert_updown_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    std::vector<float> data(4, std::numeric_limits<float>::quiet_NaN());
    auto l   = mm->add_literal(migraphx::literal{s, data});
    auto f_l = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l);
    auto h_l = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), f_l);
    mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), h_l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::all_of(
        results_vector.begin(), results_vector.end(), [](const auto& x) { return std::isnan(x); }));
}

Shucai Xiao's avatar
Shucai Xiao committed
2006
TEST_CASE(deconv_1d_test)
2007
{
Shucai Xiao's avatar
Shucai Xiao committed
2008
2009
2010
2011
2012
2013
    migraphx::shape s{migraphx::shape::float_type, {1, 1, 3}};
    std::vector<float> x_data{0, 0.5, 1};
    std::vector<float> w_data{0.5, 0.5, 0.5};

    std::vector<float> gold{0, 0.25, 0.75, 0.75, 0.5};

2014
    migraphx::program p;
2015
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2016
2017
2018
2019
2020
2021
2022
    auto x   = mm->add_literal(migraphx::literal{s, x_data});
    auto w   = mm->add_literal(migraphx::literal{s, w_data});

    mm->add_instruction(
        migraphx::make_op("deconvolution", {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
        x,
        w);
2023
    p.compile(migraphx::make_target("ref"));
2024
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2025
2026

    std::vector<float> results_vector;
2027
2028
2029
2030
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2031
TEST_CASE(deconv_3d_test)
2032
{
Shucai Xiao's avatar
Shucai Xiao committed
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
    migraphx::shape s_1{migraphx::shape::float_type, {1, 1, 1, 2, 3}};
    migraphx::shape s_2{migraphx::shape::float_type, {1, 1, 3, 2, 3}};
    std::vector<float> x_data{0.8471, -0.4195, -2.2749, 1.2491, 0.1722, 0.3246};
    std::vector<float> w_data{0.6478,
                              -0.1985,
                              0.0633,
                              -0.3479,
                              2.7056,
                              -0.1440,
                              -1.1229,
                              -0.7507,
                              -1.3151,
                              0.8884,
                              -0.1859,
                              -0.3407,
                              -1.1544,
                              -1.5893,
                              1.6265,
                              -1.4624,
                              0.3812,
                              -1.5378};

    std::vector<float> gold{0.5488,  -0.4399, -1.3369, 0.4251,  -0.1439, 0.5145,  2.3015,  -0.2104,
                            -6.1482, 0.3482,  -0.4346, 3.3197,  0.1731,  0.8533,  -0.0467, -0.9512,
                            -0.1649, 1.7553,  2.2594,  2.9917,  -0.6500, -1.6612, -4.3680, 0.0957,
                            0.3482,  1.1097,  -0.0792, -0.1692, -0.1190, -0.1106, -0.9779, -0.8621,
                            4.6707,  2.9332,  -3.7001, -2.6808, -1.2476, 3.2475,  -0.4578, 4.0263,
                            -1.8267, 0.2243,  -2.3299, -0.1411, -0.4991};

2062
    migraphx::program p;
2063
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2064
2065
2066
2067
2068
2069
2070
2071
    auto x   = mm->add_literal(migraphx::literal{s_1, x_data});
    auto w   = mm->add_literal(migraphx::literal{s_2, w_data});

    mm->add_instruction(
        migraphx::make_op("deconvolution",
                          {{"padding", {0, 0, 0}}, {"stride", {1, 1, 1}}, {"dilation", {1, 1, 1}}}),
        x,
        w);
2072
    p.compile(migraphx::make_target("ref"));
2073
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2074
2075

    std::vector<float> results_vector;
2076
2077
2078
2079
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2080
TEST_CASE(deconv_test)
2081
{
Shucai Xiao's avatar
Shucai Xiao committed
2082
2083
2084
2085
2086
2087
2088
    migraphx::shape s{migraphx::shape::float_type, {1, 1, 3, 3}};
    std::vector<float> x_data{0, 1, 2, 3, 4, 5, 6, 7, 8};
    std::vector<float> w_data{1, 1, 1, 1, 1, 1, 1, 1, 1};

    std::vector<float> gold{0,  1,  3, 3,  2,  3,  8,  15, 12, 7,  9,  21, 36,
                            27, 15, 9, 20, 33, 24, 13, 6,  13, 21, 15, 8};

2089
    migraphx::program p;
2090
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2091
2092
2093
2094
    auto x   = mm->add_literal(migraphx::literal{s, x_data});
    auto w   = mm->add_literal(migraphx::literal{s, w_data});

    mm->add_instruction(migraphx::make_op("deconvolution"), x, w);
2095
    p.compile(migraphx::make_target("ref"));
2096
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2097
2098

    std::vector<float> results_vector;
2099
2100
2101
2102
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

turneram's avatar
turneram committed
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
TEST_CASE(dequantizelinear)
{
    { /*uint8*/
        migraphx::shape xs{migraphx::shape::uint8_type, {1, 3, 3}};
        std::vector<uint8_t> xv = {0, 1, 2, 5, 10, 50, 100, 150, 250};
        migraphx::shape ss{migraphx::shape::float_type, {1, 3, 3}};
        std::vector<float> sv = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        migraphx::shape zs{migraphx::shape::uint8_type, {1, 3, 3}};
        std::vector<uint8_t> zv = {0, 0, 0, 0, 0, 0, 0, 0, 0};
        auto create_program     = [&]() {
            migraphx::program p;
            auto* mm = p.get_main_module();
            auto x   = mm->add_literal(xs, xv);
            auto s   = mm->add_literal(ss, sv);
            auto z   = mm->add_literal(zs, zv);
            mm->add_instruction(migraphx::make_op("dequantizelinear"), x, s, z);
            return p;
        };

        migraphx::program p1 = create_program();
2123
        p1.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
        auto result = p1.eval({}).back();
        std::vector<float> results_vector(9);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{0, 2, 4, 10, 20, 100, 200, 300, 500};
        EXPECT(results_vector == gold);
    }

    { /*int8*/
        migraphx::shape xs{migraphx::shape::int8_type, {1, 3, 3}};
        std::vector<int8_t> xv = {-128, -100, -50, -1, 0, 1, 50, 100, 127};
        migraphx::shape ss{migraphx::shape::float_type, {1, 3, 3}};
        std::vector<float> sv = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        auto create_program   = [&]() {
            migraphx::program p;
            auto* mm = p.get_main_module();
            auto x   = mm->add_literal(xs, xv);
            auto s   = mm->add_literal(ss, sv);
            mm->add_instruction(migraphx::make_op("dequantizelinear"), x, s);
            return p;
        };

        migraphx::program p1 = create_program();
2146
        p1.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2147
2148
2149
2150
2151
2152
2153
2154
        auto result = p1.eval({}).back();
        std::vector<float> results_vector(9);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{-256, -200, -100, -2, 0, 2, 100, 200, 254};
        EXPECT(results_vector == gold);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
2155
TEST_CASE(div_test)
2156
2157
{
    migraphx::program p;
2158
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2159
    migraphx::shape s{migraphx::shape::float_type, {3}};
2160
2161
2162
2163
    std::vector<float> data1 = {-1.0f, 0.5f, 1.0f};
    std::vector<float> data2 = {1.0f, 2.0f, 4.0f};
    auto l1                  = mm->add_literal(migraphx::literal{s, data1});
    auto l2                  = mm->add_literal(migraphx::literal{s, data2});
Shucai Xiao's avatar
Shucai Xiao committed
2164
    mm->add_instruction(migraphx::make_op("div"), l1, l2);
2165
    p.compile(migraphx::make_target("ref"));
2166
    auto result = p.eval({}).back();
2167
2168
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
2169
2170
    std::vector<float> gold(data1.size());
    std::transform(data1.begin(), data1.end(), data2.begin(), gold.begin(), std::divides<float>());
2171
2172
2173
    EXPECT(migraphx::verify_range(results_vector, gold));
}

2174
2175
2176
2177
TEST_CASE(div_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2178
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6, {3}}};
2179
2180
2181
2182
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x = mm->add_parameter("x", s);
    auto y = mm->add_parameter("y", s);
    mm->add_instruction(migraphx::make_op("div"), x, y);
2183
    p.compile(migraphx::make_target("ref"));
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199

    std::vector<float> x_data{-1.0f, 0.5f, 1.0f};
    std::vector<float> y_data{1.0f, 2.0f, 4.0f};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold(x_data.size());
    std::transform(
        x_data.begin(), x_data.end(), y_data.begin(), gold.begin(), std::divides<float>());
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2200
TEST_CASE(elu_test)
2201
{
Paul's avatar
Paul committed
2202
    migraphx::program p;
2203
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2204
2205
2206
2207
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l      = mm->add_literal(migraphx::literal{s, {-1.0, 2.0, -3.0, 4.0}});
    float alpha = 0.5;
    mm->add_instruction(migraphx::make_op("elu", {{"alpha", alpha}}), l);
2208
    p.compile(migraphx::make_target("ref"));
2209
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2210
    std::vector<float> results_vector(4);
2211
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
2212
    std::vector<float> gold{elu(alpha, -1), elu(alpha, 2), elu(alpha, -3), elu(alpha, 4)};
Paul's avatar
Paul committed
2213
    EXPECT(migraphx::verify_range(results_vector, gold));
2214
2215
}

Charlie Lin's avatar
Charlie Lin committed
2216
TEST_CASE(elu_dyn_test)
2217
2218
2219
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2220
    migraphx::shape::dynamic_dimension dd{3, 8};
2221
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
2222
    auto input  = mm->add_parameter("X", s);
2223
2224
    float alpha = 0.5;
    mm->add_instruction(migraphx::make_op("elu", {{"alpha", alpha}}), input);
2225
    p.compile(migraphx::make_target("ref"));
2226

Charlie Lin's avatar
Charlie Lin committed
2227
    std::vector<float> input_data{-1.0, 2.0, -3.0, 4.0};
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{elu(alpha, -1), elu(alpha, 2), elu(alpha, -3), elu(alpha, 4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2238
TEST_CASE(equal_brcst_test)
2239
{
Paul's avatar
Paul committed
2240
    migraphx::program p;
2241
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2242
2243
2244
2245
    migraphx::shape s0{migraphx::shape::float_type, {3, 3}};
    auto l0 =
        mm->add_literal(migraphx::literal{s0, {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0}});
    migraphx::shape s1{migraphx::shape::float_type, {3, 1}};
2246
2247
2248
2249
    auto l1  = mm->add_literal(migraphx::literal{s1, {1.1, -1.5, 0.0}});
    auto bl1 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), l1);
    auto eq  = mm->add_instruction(migraphx::make_op("equal"), l0, bl1);
    auto r   = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
2250
2251
2252
2253
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        eq);
    mm->add_return({r});
2254

2255
    p.compile(migraphx::make_target("ref"));
2256
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2257
    std::vector<bool> results_vector;
2258
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
2259
2260
    std::vector<bool> gold = {true, false, false, false, true, false, true, false, false};
    EXPECT(results_vector == gold);
2261
2262
}

Shucai Xiao's avatar
Shucai Xiao committed
2263
TEST_CASE(equal_test)
2264
{
Paul's avatar
Paul committed
2265
    migraphx::program p;
2266
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
    migraphx::shape s{migraphx::shape::float_type, {9}};
    auto l0 =
        mm->add_literal(migraphx::literal{s, {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0}});
    auto l1 =
        mm->add_literal(migraphx::literal{s, {1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1}});
    auto eq = mm->add_instruction(migraphx::make_op("equal"), l0, l1);
    auto r  = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        eq);
    mm->add_return({r});

2279
    p.compile(migraphx::make_target("ref"));
2280
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2281
    std::vector<bool> results_vector;
2282
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
2283
2284
    std::vector<bool> gold = {true, false, false, false, true, false, true, false, false};
    EXPECT(results_vector == gold);
2285
2286
}

2287
2288
2289
2290
TEST_CASE(equal_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2291
    std::vector<migraphx::shape::dynamic_dimension> dd{{6, 12, {9}}};
2292
2293
2294
2295
2296
2297
2298
2299
2300
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto p0 = mm->add_parameter("l", s);
    auto p1 = mm->add_parameter("r", s);
    auto eq = mm->add_instruction(migraphx::make_op("equal"), p0, p1);
    auto r  = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        eq);
    mm->add_return({r});
2301
    p.compile(migraphx::make_target("ref"));
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315

    std::vector<float> l_data{1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
    std::vector<float> r_data{1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["l"] = migraphx::argument(input_fixed_shape0, l_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, r_data.data());
    auto result  = p.eval(params0).back();
    std::vector<bool> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold = {true, false, false, false, true, false, true, false, false};
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
2316
TEST_CASE(erf_test)
2317
{
Paul's avatar
Paul committed
2318
    migraphx::program p;
2319
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2320
    migraphx::shape s{migraphx::shape::float_type, {4}};
2321
2322
    std::vector<float> data = {0.73785057, 1.58165966, -0.43597795, -0.01677432};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
2323
    mm->add_instruction(migraphx::make_op("erf"), l);
2324
    p.compile(migraphx::make_target("ref"));
2325
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2326
    std::vector<float> results_vector;
2327
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
2328
2329
2330
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return erff(n); });
Paul's avatar
Paul committed
2331
    EXPECT(migraphx::verify_range(results_vector, gold));
2332
2333
}

Charlie Lin's avatar
Charlie Lin committed
2334
TEST_CASE(erf_dyn_test)
2335
2336
2337
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2338
    migraphx::shape::dynamic_dimension dd{3, 8};
2339
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
2340
    auto input = mm->add_parameter("X", s);
2341
    mm->add_instruction(migraphx::make_op("erf"), input);
2342
    p.compile(migraphx::make_target("ref"));
2343

Charlie Lin's avatar
Charlie Lin committed
2344
    std::vector<float> input_data = {0.73785057, 1.58165966, -0.43597795, -0.01677432};
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return erff(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2357
TEST_CASE(exp_test)
Khalique's avatar
Khalique committed
2358
{
Paul's avatar
Paul committed
2359
    migraphx::program p;
2360
    auto* mm = p.get_main_module();
2361
    std::vector<float> data{-1, 0, 1};
Paul's avatar
Paul committed
2362
    migraphx::shape s{migraphx::shape::float_type, {3}};
2363
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
2364
    mm->add_instruction(migraphx::make_op("exp"), l);
2365
    p.compile(migraphx::make_target("ref"));
2366
    auto result = p.eval({}).back();
Khalique's avatar
Khalique committed
2367
2368
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
2369
2370
2371
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return expf(n); });
Paul's avatar
Paul committed
2372
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
2373
2374
}

Charlie Lin's avatar
Charlie Lin committed
2375
TEST_CASE(exp_dyn_test)
2376
2377
2378
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2379
    migraphx::shape::dynamic_dimension dd{3, 8};
2380
2381
2382
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("exp"), input);
2383
    p.compile(migraphx::make_target("ref"));
2384

Charlie Lin's avatar
Charlie Lin committed
2385
    std::vector<float> input_data{-1, 0, 1};
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return expf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2398
TEST_CASE(floor_test)
Khalique's avatar
Khalique committed
2399
{
Paul's avatar
Paul committed
2400
    migraphx::program p;
2401
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2402
    migraphx::shape s{migraphx::shape::float_type, {9}};
2403
2404
    std::vector<float> data = {1.1, 1.5, 0.6, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
2405
    mm->add_instruction(migraphx::make_op("floor"), l);
2406
    p.compile(migraphx::make_target("ref"));
2407
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2408
    std::vector<float> results_vector;
Khalique's avatar
Khalique committed
2409
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
2410
2411
2412
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return floor(n); });
Paul's avatar
Paul committed
2413
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
2414
2415
}

Charlie Lin's avatar
Charlie Lin committed
2416
TEST_CASE(floor_dyn_test)
2417
2418
2419
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2420
    migraphx::shape::dynamic_dimension dd{5, 12};
2421
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
2422
    auto input = mm->add_parameter("X", s);
2423
    mm->add_instruction(migraphx::make_op("floor"), input);
2424
    p.compile(migraphx::make_target("ref"));
2425

Charlie Lin's avatar
Charlie Lin committed
2426
    std::vector<float> input_data = {1.1, 1.5, 0.6, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return floor(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
2439
TEST_CASE(fp16_test)
Khalique's avatar
Khalique committed
2440
{
Khalique's avatar
Khalique committed
2441
    migraphx::program p;
2442
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
2443
2444
2445
2446
2447
2448
2449
    migraphx::shape s{migraphx::shape::half_type, {1}};
    migraphx::half a{1.5};
    migraphx::half b{2.5};
    migraphx::half c{4.0};
    auto l0 = mm->add_literal(migraphx::literal{s, {a}});
    auto l1 = mm->add_literal(migraphx::literal{s, {b}});
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
2450
    p.compile(migraphx::make_target("ref"));
2451
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2452
    std::vector<migraphx::half> results_vector(1);
Khalique's avatar
Khalique committed
2453
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
2454
    std::vector<migraphx::half> gold{c};
Khalique's avatar
Khalique committed
2455
    EXPECT(migraphx::verify_range(results_vector, gold));
Khalique's avatar
Khalique committed
2456
2457
}

Shucai Xiao's avatar
Shucai Xiao committed
2458
TEST_CASE(fp32_fp16_test)
Khalique's avatar
Khalique committed
2459
{
Shucai Xiao's avatar
Shucai Xiao committed
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
    auto create_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        std::vector<float> data(2 * 3);
        std::iota(data.begin(), data.end(), 1.0f);
        auto l1 = mm->add_literal(migraphx::literal(s, data));
        auto l2 = mm->add_literal(migraphx::literal(s, data));
        mm->add_instruction(migraphx::make_op("add"), l1, l2);
        return p;
    };
Khalique's avatar
Khalique committed
2471

Shucai Xiao's avatar
Shucai Xiao committed
2472
2473
2474
2475
    auto test_case = [&](std::vector<std::string>&& op_names) {
        std::vector<float> gold_res = {2.0, 4.0, 6.0, 8.0, 10.0, 12.0};
        auto p                      = create_program();
        migraphx::quantize_fp16(p, op_names);
2476
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
2477
2478
2479
2480
2481
        auto result = p.eval({}).back();
        std::vector<float> res;
        result.visit([&](auto output) { res.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res, gold_res));
    };
Khalique's avatar
Khalique committed
2482

Shucai Xiao's avatar
Shucai Xiao committed
2483
2484
    test_case({"all"});
    test_case({"add"});
Khalique's avatar
Khalique committed
2485
2486
}

2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
TEST_CASE(gather_non_std_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        std::vector<float> data = {0.5f, 3.5f, 6.5f, 1.5f, 4.5f, 7.5f, 2.5f, 2.5f, 8.5f};
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto d = mm->add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {2, 2}};
        std::vector<int> indices{-3, -3, -1, -1};
        auto ind = mm->add_literal(migraphx::literal{s_indices, indices});
        auto td = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), d);
        auto tind =
            mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), ind);

        mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), td, tind);
        auto result               = p.eval({}).back();
        std::vector<float> golden = {
            0.5f, 1.5f, 2.5f, 6.5f, 7.5f, 8.5f, 0.5f, 1.5f, 2.5f, 6.5f, 7.5f, 8.5f};
        std::vector<float> res_data;
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
2513
TEST_CASE(gather_test)
2514
{
2515
    {
Paul's avatar
Paul committed
2516
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = 0;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2528
        p.compile(migraphx::make_target("ref"));
2529
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2530
2531
2532
2533
        std::vector<float> res_data(4 * 5);
        std::vector<float> golden = {0.5f, 1.5f, 2.5f, 6.5f, 7.5f, 8.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
2534
    }
Shucai Xiao's avatar
Shucai Xiao committed
2535

2536
    {
Paul's avatar
Paul committed
2537
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{-3, -1};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = 0;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2549
        p.compile(migraphx::make_target("ref"));
2550
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
        std::vector<float> res_data(4 * 5);
        std::vector<float> golden = {0.5f, 1.5f, 2.5f, 6.5f, 7.5f, 8.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = 1;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2570
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
        auto result = p.eval({}).back();
        std::vector<float> res_data(4 * 5);
        std::vector<float> golden = {0.5f, 2.5f, 3.5f, 5.5f, 6.5f, 8.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        migraphx::shape s_indices{migraphx::shape::int32_type, {1, 2}};
        std::vector<int> indices{0, 2};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = -1;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2591
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
        auto result = p.eval({}).back();
        std::vector<float> res_data(4 * 5);
        std::vector<float> golden = {0.5f, 2.5f, 3.5f, 5.5f, 6.5f, 8.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        // scalar index
        migraphx::shape s_indices{migraphx::shape::int32_type};
        std::vector<int> indices{0};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = -1;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2613
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> golden = {0.5f, 3.5f, 6.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        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 = mm->add_literal(migraphx::literal{s, data});
        // scalar index
        migraphx::shape s_indices{migraphx::shape::int32_type};
        std::vector<int> indices{-3};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = -1;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2635
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
2636
2637
2638
2639
2640
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> golden = {0.5f, 3.5f, 6.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
2641
    }
Shucai Xiao's avatar
Shucai Xiao committed
2642

2643
    {
Paul's avatar
Paul committed
2644
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
        auto* mm = p.get_main_module();

        std::vector<float> data(3);
        std::iota(data.begin(), data.end(), 0.5);
        migraphx::shape s{migraphx::shape::float_type, {3}};
        auto a0 = mm->add_literal(migraphx::literal{s, data});
        // scalar index
        migraphx::shape s_indices{migraphx::shape::int32_type};
        std::vector<int> indices{0};
        auto a1  = mm->add_literal(migraphx::literal{s_indices, indices});
        int axis = -1;
        mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), a0, a1);
2657
        p.compile(migraphx::make_target("ref"));
2658
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
2659
2660
2661
2662
        std::vector<float> res_data{};
        std::vector<float> golden = {0.5f};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
        EXPECT(migraphx::verify_range(res_data, golden));
2663
2664
2665
    }
}

Brian Pickrell's avatar
Brian Pickrell committed
2666
2667
2668
2669
2670
TEST_CASE(gather_dyn_test0)
{
    // Dynamic data, static indices
    migraphx::program p;
    auto* mm = p.get_main_module();
2671
    migraphx::shape s{migraphx::shape::int32_type, {{2, 5}, {3, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
2672
2673
2674
2675
2676
2677
2678
2679

    auto x = mm->add_parameter("x", s);
    std::vector<int> indices{1, 2};

    migraphx::shape s_ind{migraphx::shape::int32_type, {1, 2}};
    auto ind = mm->add_parameter("indices", s_ind);
    mm->add_instruction(migraphx::make_op("gather", {{"axis", 1}}), x, ind);

2680
    migraphx::shape sresult{migraphx::shape::int32_type, {{2, 5}, {1, 1}, {2, 2}}};
Brian Pickrell's avatar
Brian Pickrell committed
2681
    EXPECT(p.get_output_shapes().back() == sresult);
2682
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705

    migraphx::shape input_fixed_shape{migraphx::shape::int32_type, {2, 3}};
    migraphx::shape input_indices{migraphx::shape::int32_type, {1, 2}};
    migraphx::parameter_map params;
    std::vector<int> data(2 * 3);
    std::iota(data.begin(), data.end(), 0);
    params["x"]       = migraphx::argument(input_fixed_shape, data.data());
    params["indices"] = migraphx::argument(input_indices, indices.data());
    auto result       = p.eval(params).back();

    std::vector<int> gold = {1, 2, 4, 5};
    std::vector<int> results_vector(2 * 1 * 2);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, gold));
    migraphx::shape sfinal{migraphx::shape::int32_type, {2, 1, 2}};
    EXPECT(result.get_shape() == sfinal);
}

TEST_CASE(gather_dyn_test1)
{
    // Dynamic data, dynamic indices
    migraphx::program p;
    auto* mm = p.get_main_module();
2706
    migraphx::shape s{migraphx::shape::int32_type, {{2, 5}, {4, 4}}};
Brian Pickrell's avatar
Brian Pickrell committed
2707
2708
2709

    auto x = mm->add_parameter("x", s);

2710
    migraphx::shape s_ind{migraphx::shape::int32_type, {{1, 8, {7}}, {2, 3, {3}}}};
Brian Pickrell's avatar
Brian Pickrell committed
2711
2712
2713
    auto ind = mm->add_parameter("indices", s_ind);
    mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), x, ind);

2714
    migraphx::shape sresult{migraphx::shape::int32_type, {{1, 8, {7}}, {2, 3, {3}}, {4, 4}}};
Brian Pickrell's avatar
Brian Pickrell committed
2715
    EXPECT(p.get_output_shapes().back() == sresult);
2716
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737

    migraphx::shape input_fixed_shape{migraphx::shape::int32_type, {3, 4}};
    migraphx::shape input_indices_shape{migraphx::shape::int32_type, {1, 2}};
    std::vector<int> indices{2, 0};
    migraphx::parameter_map params;

    std::vector<int> data(3 * 4);
    std::iota(data.begin(), data.end(), 0);
    params["x"]       = migraphx::argument(input_fixed_shape, data.data());
    params["indices"] = migraphx::argument(input_indices_shape, indices.data());
    auto result       = p.eval(params).back();

    std::vector<int> gold = {8, 9, 10, 11, 0, 1, 2, 3};
    std::vector<int> results_vector(1 * 2 * 4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, gold));
    migraphx::shape sfinal{migraphx::shape::int32_type, {1, 2, 4}};
    EXPECT(result.get_shape() == sfinal);
}

turneram's avatar
turneram committed
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
TEST_CASE(gathernd_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 2}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 2}};

        std::vector<float> data_vec(2 * 2);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{0, 0, 1, 1};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(migraphx::make_op("gathernd"), data, indices);
2755
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{0, 3};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 2}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 1}};

        std::vector<float> data_vec(2 * 2);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{1, 0};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(migraphx::make_op("gathernd"), data, indices);
2779
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{2, 3, 0, 1};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 3, 1}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 2, 1}};

        std::vector<float> data_vec(2 * 3 * 1);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{1, 0, 0, 1};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(migraphx::make_op("gathernd"), data, indices);
2803
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{3, 4, 5, 0, 1, 2, 0, 1, 2, 3, 4, 5};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 3, 2, 3}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 2, 2}};

        std::vector<float> data_vec(2 * 3 * 2 * 3);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{0, 0, 0, 1, 0, 0, 0, 1};
        const int batch_dims = 1;

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(
            migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), data, indices);
2829
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{0, 1, 2, 3, 4, 5, 18, 19, 20, 21, 22, 23};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 3, 1, 3}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 3, 2}};

        std::vector<float> data_vec(2 * 3 * 1 * 3);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{0, 0, 0, 1, 0, 2, 0, 2, 0, 1, 0, 0};
        const int batch_dims = 2;

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(
            migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), data, indices);

2856
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{0, 4, 8, 11, 13, 15};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        // k > r - batch_dims
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 3, 1, 3}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 3, 3}};

        std::vector<float> data_vec(2 * 3 * 1 * 3);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec(2 * 3 * 3, 0);
        const int batch_dims = 2;

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        EXPECT(test::throws([&] {
            mm->add_instruction(
                migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), data, indices);
        }));
    }
}

Brian Pickrell's avatar
Brian Pickrell committed
2888
2889
2890
2891
2892
2893
TEST_CASE(gathernd_dynamic0)
{
    // dynamic data, all dimensions fixed
    migraphx::program p;
    auto* mm = p.get_main_module();

2894
    migraphx::shape ds{migraphx::shape::float_type, {{2, 2, {2}}, {3, 3}, {1, 1}}};
Brian Pickrell's avatar
Brian Pickrell committed
2895
2896
2897
2898
2899
2900
2901
2902
2903
    migraphx::shape is{migraphx::shape::int64_type, {2, 2, 1}};

    auto xdata  = mm->add_parameter("X", ds);
    auto xindex = mm->add_parameter("I", is);

    auto gathernd_op = migraphx::make_op("gathernd");
    auto gathernd    = mm->add_instruction(gathernd_op, xdata, xindex);

    mm->add_return({gathernd});
2904
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3, 1}}; // data
    migraphx::shape input_fixed_shape1{migraphx::shape::int64_type, {2, 2, 1}}; // index

    std::vector<float> data_vec(2 * 3 * 1);
    std::iota(data_vec.begin(), data_vec.end(), 0);
    std::vector<int64_t> indices_vec{1, 0, 0, 1};

    params["X"] = migraphx::argument(input_fixed_shape0, data_vec.data());
    params["I"] = migraphx::argument(input_fixed_shape1, indices_vec.data());

    auto result = p.eval(params).back();
    std::vector<float> res_data{};
    std::vector<float> gold{3, 4, 5, 0, 1, 2, 0, 1, 2, 3, 4, 5};
    result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(res_data, gold));
}

TEST_CASE(gathernd_dynamic1)
{
    // dynamic data, dims not fixed
    migraphx::program p;
    auto* mm = p.get_main_module();

2931
    migraphx::shape ds{migraphx::shape::float_type, {{2, 5, {2}}, {1, 5}, {1, 5}}};
Brian Pickrell's avatar
Brian Pickrell committed
2932
2933
2934
2935
2936
2937
2938
2939
2940
    migraphx::shape is{migraphx::shape::int64_type, {2, 2, 1}};

    auto xdata  = mm->add_parameter("X", ds);
    auto xindex = mm->add_parameter("I", is);

    auto gathernd_op = migraphx::make_op("gathernd");
    auto gathernd    = mm->add_instruction(gathernd_op, xdata, xindex);

    mm->add_return({gathernd});
2941
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3, 1}}; // data
    migraphx::shape input_fixed_shape1{migraphx::shape::int64_type, {2, 2, 1}}; // index

    std::vector<float> data_vec(2 * 3 * 1);
    std::iota(data_vec.begin(), data_vec.end(), 0);
    std::vector<int64_t> indices_vec{1, 0, 0, 1};
    params["X"] = migraphx::argument(input_fixed_shape0, data_vec.data());
    params["I"] = migraphx::argument(input_fixed_shape1, indices_vec.data());

    auto result = p.eval(params).back();
    std::vector<float> res_data{};
    std::vector<float> gold{3, 4, 5, 0, 1, 2, 0, 1, 2, 3, 4, 5};
    result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(res_data, gold));
}

TEST_CASE(gathernd_dynamic2)
{
    // dynamic both index and data
    migraphx::program p;
    auto* mm = p.get_main_module();

2967
2968
    migraphx::shape ds{migraphx::shape::float_type, {{2, 5, {2}}, {1, 5}, {1, 5}}};
    migraphx::shape is{migraphx::shape::int64_type, {{2, 5, {3}}, {2, 3, {3}}, {1, 1}}};
Brian Pickrell's avatar
Brian Pickrell committed
2969
2970
2971
2972
2973
2974
2975
2976

    auto xdata  = mm->add_parameter("X", ds);
    auto xindex = mm->add_parameter("I", is);

    auto gathernd_op = migraphx::make_op("gathernd");
    auto gathernd    = mm->add_instruction(gathernd_op, xdata, xindex);

    mm->add_return({gathernd});
2977
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3, 1}}; // data
    migraphx::shape input_fixed_shape1{migraphx::shape::int64_type, {2, 2, 1}}; // index

    std::vector<float> data_vec(2 * 3 * 1);
    std::iota(data_vec.begin(), data_vec.end(), 0);
    std::vector<int64_t> indices_vec{1, 0, 0, 1};
    params["X"] = migraphx::argument(input_fixed_shape0, data_vec.data());
    params["I"] = migraphx::argument(input_fixed_shape1, indices_vec.data());

    auto result = p.eval(params).back();
    std::vector<float> res_data{};
    std::vector<float> gold{3, 4, 5, 0, 1, 2, 0, 1, 2, 3, 4, 5};
    result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(res_data, gold));
}

TEST_CASE(gathernd_dynamic3)
{
    // dynamic index, static data and a batch_dims input
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape ds{migraphx::shape::float_type, {2, 3, 1}};
3004
    migraphx::shape is{migraphx::shape::int64_type, {{2, 5, {3}}, {2, 3, {3}}, {1, 1}}};
Brian Pickrell's avatar
Brian Pickrell committed
3005
3006
3007
3008
3009
3010
3011
3012
3013

    auto xdata  = mm->add_parameter("X", ds);
    auto xindex = mm->add_parameter("I", is);

    int batch_dims{1};
    auto gathernd_op = migraphx::make_op("gathernd", {{"batch_dims", batch_dims}});
    auto gathernd    = mm->add_instruction(gathernd_op, xdata, xindex);

    mm->add_return({gathernd});
3014
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3, 1}}; // data
    migraphx::shape input_fixed_shape1{migraphx::shape::int64_type, {2, 2, 1}}; // index

    std::vector<float> data_vec(2 * 3 * 1);
    std::iota(data_vec.begin(), data_vec.end(), 0);
    std::vector<int64_t> indices_vec{1, 0, 0, 1};
    params["X"] = migraphx::argument(input_fixed_shape0, data_vec.data());
    params["I"] = migraphx::argument(input_fixed_shape1, indices_vec.data());

    auto result = p.eval(params).back();
    std::vector<float> res_data{};
    std::vector<float> gold{1, 0, 3, 4};
    result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(res_data, gold));
}

TEST_CASE(gathernd_dynamic4)
{
    // int(q) + r - k - batch_dims - 1 = 0 => returns a scalar
    migraphx::program p;
    auto* mm = p.get_main_module();

3039
    migraphx::shape ds{migraphx::shape::float_type, {migraphx::shape::dynamic_dimension({2, 2})}};
Brian Pickrell's avatar
Brian Pickrell committed
3040
3041
3042
3043
3044
3045
3046
3047
3048
    migraphx::shape is{migraphx::shape::int64_type, {1}};

    auto xdata  = mm->add_parameter("X", ds);
    auto xindex = mm->add_parameter("I", is);

    auto gathernd_op = migraphx::make_op("gathernd");
    auto gathernd    = mm->add_instruction(gathernd_op, xdata, xindex);

    mm->add_return({gathernd});
3049
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2}}; // data
    migraphx::shape input_fixed_shape1{migraphx::shape::int64_type, {1}}; // index

    std::vector<float> data_vec(2);
    std::iota(data_vec.begin(), data_vec.end(), 4);
    std::vector<int64_t> indices_vec{1};
    params["X"] = migraphx::argument(input_fixed_shape0, data_vec.data());
    params["I"] = migraphx::argument(input_fixed_shape1, indices_vec.data());

    auto result = p.eval(params).back();
    std::vector<float> res_data{};
    std::vector<float> gold{5};
    result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(res_data, gold));
}

turneram's avatar
turneram committed
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
TEST_CASE(gathernd_negative_index_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 2}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 1, 1}};

        std::vector<float> data_vec(2 * 2);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{-1, 0};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(migraphx::make_op("gathernd"), data, indices);
3085
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
        auto result = p.eval({}).back();
        std::vector<float> res_data{};
        std::vector<float> gold{2, 3, 0, 1};
        result.visit([&](auto output) { res_data.assign(output.begin(), output.end()); });

        EXPECT(migraphx::verify_range(res_data, gold));
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();

        migraphx::shape ds{migraphx::shape::float_type, {2, 2}};
        migraphx::shape is{migraphx::shape::int64_type, {2, 1, 1}};

        std::vector<float> data_vec(2 * 2);
        std::iota(data_vec.begin(), data_vec.end(), 0);
        std::vector<int64_t> indices_vec{-3, 0};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, indices_vec});

        mm->add_instruction(migraphx::make_op("gathernd"), data, indices);
3109
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
3110
3111
3112
3113
3114

        EXPECT(test::throws([&] { p.eval({}); }));
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
3115
TEST_CASE(globalavgpool_test)
3116
{
Paul's avatar
Paul committed
3117
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3118
3119
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}};
3120
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
Shucai Xiao's avatar
Shucai Xiao committed
3121
3122
    auto lens  = s.lens();
    op.lengths = {lens[2], lens[3]};
3123

Shucai Xiao's avatar
Shucai Xiao committed
3124
3125
3126
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
3127
    p.compile(migraphx::make_target("ref"));
3128
    auto result = p.eval({}).back();
3129

Shucai Xiao's avatar
Shucai Xiao committed
3130
    std::vector<float> results_vector(3);
3131
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3132
3133
    std::vector<float> gold{0.25, 0.575, 0.375};
    EXPECT(migraphx::verify_range(results_vector, gold));
Scott Thornton's avatar
Scott Thornton committed
3134
3135
}

Charlie Lin's avatar
Charlie Lin committed
3136
3137
3138
3139
TEST_CASE(globalavgpool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3140
3141
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 6}, {2, 6, {2}}}};
    auto x   = mm->add_parameter("X", s);
Charlie Lin's avatar
Charlie Lin committed
3142
3143
3144
3145
    mm->add_instruction(
        migraphx::make_op("pooling",
                          {{"mode", migraphx::op::pooling_mode::average}, {"dyn_global", true}}),
        x);
3146
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
TEST_CASE(globallppool_test)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto s      = migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    auto op     = migraphx::op::pooling{migraphx::op::pooling_mode::lpnorm};
    auto lens   = s.lens();
    op.lengths  = {lens[2], lens[3]};
    op.lp_order = 2;

    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
3172
    p.compile(migraphx::make_target("ref"));
3173
3174
3175
3176
3177
3178
3179
3180
    auto result = p.eval({}).back();

    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.5477225575051662, 1.307669683062202, 0.9327379053088815};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
3181
3182
3183
3184
3185
TEST_CASE(globallppool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s =
3186
        migraphx::shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 6, {2}}, {2, 6, {2}}}};
Charlie Lin's avatar
Charlie Lin committed
3187
3188
3189
3190
3191
    auto x = mm->add_parameter("X", s);
    mm->add_instruction(
        migraphx::make_op("pooling",
                          {{"mode", migraphx::op::pooling_mode::lpnorm}, {"dyn_global", true}}),
        x);
3192
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.5477225575051662, 1.307669683062202, 0.9327379053088815};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
3205
TEST_CASE(globalmaxpool_test)
3206
3207
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3208
3209
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}};
3210
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
Shucai Xiao's avatar
Shucai Xiao committed
3211
3212
    auto lens  = s.lens();
    op.lengths = {lens[2], lens[3]};
3213

Shucai Xiao's avatar
Shucai Xiao committed
3214
3215
3216
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
3217
    p.compile(migraphx::make_target("ref"));
3218
    auto result = p.eval({}).back();
3219

Shucai Xiao's avatar
Shucai Xiao committed
3220
    std::vector<float> results_vector(3);
3221
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3222
3223
3224
3225
    std::vector<float> gold{0.4, 0.9, 0.7};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
3226
3227
3228
3229
3230
TEST_CASE(globalmaxpool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s =
3231
        migraphx::shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 6, {2}}, {2, 6, {2}}}};
Charlie Lin's avatar
Charlie Lin committed
3232
3233
3234
3235
3236
    auto x = mm->add_parameter("X", s);
    mm->add_instruction(
        migraphx::make_op("pooling",
                          {{"mode", migraphx::op::pooling_mode::max}, {"dyn_global", true}}),
        x);
3237
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 2, 2}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
3250
3251
3252
3253
3254
3255
3256
3257
TEST_CASE(greater_brcst_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s0{migraphx::shape::float_type, {3, 3}};
    auto l0 =
        mm->add_literal(migraphx::literal{s0, {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0}});
    migraphx::shape s1{migraphx::shape::float_type, {3, 1}};
3258
3259
3260
3261
    auto l1  = mm->add_literal(migraphx::literal{s1, {1.1, -1.5, 0.0}});
    auto bl1 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), l1);
    auto gr  = mm->add_instruction(migraphx::make_op("greater"), l0, bl1);
    auto r   = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
3262
3263
3264
3265
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        gr);
    mm->add_return({r});
3266

3267
    p.compile(migraphx::make_target("ref"));
3268
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3269
    std::vector<bool> results_vector;
3270
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3271
3272
    std::vector<bool> gold = {false, true, false, true, false, true, false, true, false};
    EXPECT(results_vector == gold);
3273
3274
}

Shucai Xiao's avatar
Shucai Xiao committed
3275
TEST_CASE(greater_test)
3276
3277
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {9}};
    auto l0 =
        mm->add_literal(migraphx::literal{s, {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0}});
    auto l1 =
        mm->add_literal(migraphx::literal{s, {1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1}});
    auto gr = mm->add_instruction(migraphx::make_op("greater"), l0, l1);
    auto r  = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        gr);
    mm->add_return({r});
3290

3291
    p.compile(migraphx::make_target("ref"));
3292
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3293
    std::vector<bool> results_vector;
3294
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3295
3296
    std::vector<bool> gold = {false, false, true, true, false, true, false, false, true};
    EXPECT(results_vector == gold);
3297
}
Shucai Xiao's avatar
Shucai Xiao committed
3298

3299
3300
3301
3302
TEST_CASE(greater_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3303
    std::vector<migraphx::shape::dynamic_dimension> dd{{8, 10, {9}}};
3304
3305
3306
3307
3308
3309
3310
3311
3312
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto left  = mm->add_parameter("l", s);
    auto right = mm->add_parameter("r", s);
    auto gr    = mm->add_instruction(migraphx::make_op("greater"), left, right);
    auto r     = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        gr);
    mm->add_return({r});
3313
    p.compile(migraphx::make_target("ref"));
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327

    std::vector<float> left_data{1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
    std::vector<float> right_data{1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["l"] = migraphx::argument(input_fixed_shape0, left_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, right_data.data());
    auto result  = p.eval(params0).back();
    std::vector<bool> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold = {false, false, true, true, false, true, false, false, true};
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
3328
TEST_CASE(identity_test)
Shucai Xiao's avatar
Shucai Xiao committed
3329
3330
3331
{
    migraphx::program p;
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3332
3333
3334
3335
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    std::vector<int> data{1, 2, 3, 4};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("identity"), l);
3336
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3337
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3338
    std::vector<int> results_vector(4);
Shucai Xiao's avatar
Shucai Xiao committed
3339
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3340
    EXPECT(std::equal(data.begin(), data.end(), results_vector.begin()));
Shucai Xiao's avatar
Shucai Xiao committed
3341
3342
}

Charlie Lin's avatar
Charlie Lin committed
3343
TEST_CASE(identity_dyn_test)
3344
3345
3346
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3347
    migraphx::shape s{migraphx::shape::float_type, {{2, 4}, {2, 4}}};
3348
3349
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("identity"), input);
3350
    p.compile(migraphx::make_target("ref"));
3351

Charlie Lin's avatar
Charlie Lin committed
3352
    std::vector<int> input_data{1, 2, 3, 4};
3353
3354
3355
3356
3357
3358
3359
3360
3361
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::int32_type, {2, 2}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<int> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(std::equal(input_data.begin(), input_data.end(), results_vector.begin()));
}

Shucai Xiao's avatar
Shucai Xiao committed
3362
TEST_CASE(if_literal_test)
3363
{
Shucai Xiao's avatar
Shucai Xiao committed
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
    auto create_program = [] {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape cond_s{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", cond_s);

        migraphx::shape s{migraphx::shape::float_type, {5}};

        auto* then_mod           = p.create_module("If_0_if");
        std::vector<float> data1 = {1, 2, 3, 4, 5};
        auto l1                  = then_mod->add_literal(migraphx::literal(s, data1));
        then_mod->add_return({l1});

        auto* else_mod           = p.create_module("If_0_else");
        std::vector<float> data2 = {5, 4, 3, 2, 1};
        auto l2                  = else_mod->add_literal(migraphx::literal(s, data2));
        else_mod->add_return({l2});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
Shucai Xiao's avatar
Shucai Xiao committed
3383
3384
        auto r   = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
        mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
3385
3386
3387
3388
3389
3390

        return p;
    };

    auto run_prog = [&](bool cond) {
        auto p = create_program();
3391
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
        std::vector<char> c_data = {static_cast<char>(cond)};
        migraphx::shape cs{migraphx::shape::bool_type};
        migraphx::parameter_map m;
        m["cond"] = migraphx::argument(cs, c_data.data());

        auto res = p.eval(m).back();
        std::vector<float> ret;
        res.visit([&](auto v) { ret.assign(v.begin(), v.end()); });

        return ret;
    };

    // then branch
    {
        std::vector<float> gold_ret = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
        auto ret                    = run_prog(true);
        EXPECT(gold_ret == ret);
    }

    // else branch
3412
    {
Shucai Xiao's avatar
Shucai Xiao committed
3413
3414
3415
3416
3417
3418
3419
3420
3421
        std::vector<float> gold_ret = {5.0f, 4.0f, 3.0f, 2.0f, 1.0f};
        auto ret                    = run_prog(false);
        EXPECT(gold_ret == ret);
    }
}

TEST_CASE(if_param_test)
{
    auto create_program = [] {
3422
3423
        migraphx::program p;
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
        migraphx::shape cond_s{migraphx::shape::bool_type};
        auto cond = mm->add_parameter("cond", cond_s);
        migraphx::shape ds{migraphx::shape::float_type, {2, 3}};
        auto x                   = mm->add_parameter("x", ds);
        auto y                   = mm->add_parameter("y", ds);
        std::vector<float> data2 = {-0.258047, 0.360394, 0.536804, -0.577762, 1.0217, 1.02442};
        auto l2                  = mm->add_literal(migraphx::literal(ds, data2));
        auto sum                 = mm->add_instruction(migraphx::make_op("add"), x, l2);

        auto* then_mod           = p.create_module("If_0_if");
        std::vector<float> data1 = {0.384804, -1.77948, -0.453775, 0.477438, -1.06333, -1.12893};
        auto l1                  = then_mod->add_literal(migraphx::literal(ds, data1));
        auto tx                  = then_mod->add_parameter("x", ds);
        auto a1                  = then_mod->add_instruction(migraphx::make_op("add"), tx, l1);
        then_mod->add_return({a1});

        auto* else_mod = p.create_module("If_0_else");
        auto ey        = else_mod->add_parameter("y", ds);
        auto a2        = else_mod->add_instruction(migraphx::make_op("mul"), ey, sum);
        else_mod->add_return({a2});

        auto ret = mm->add_instruction(migraphx::make_op("if"), {cond, x, y}, {then_mod, else_mod});
Shucai Xiao's avatar
Shucai Xiao committed
3446
3447
        auto r   = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
        mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
3448
3449
3450
3451
3452
3453

        return p;
    };

    auto run_prog = [&](bool cond) {
        auto p = create_program();
3454
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
        std::vector<char> c_data = {static_cast<char>(cond)};
        migraphx::shape cs{migraphx::shape::bool_type};
        migraphx::parameter_map m;
        m["cond"] = migraphx::argument(cs, c_data.data());
        migraphx::shape ds{migraphx::shape::float_type, {2, 3}};
        std::vector<float> data_x(ds.elements(), 1);
        m["x"] = migraphx::argument(ds, data_x.data());
        std::vector<float> data_y(ds.elements(), 2);
        m["y"] = migraphx::argument(ds, data_y.data());

        auto res = p.eval(m).back();
        std::vector<float> ret;
        res.visit([&](auto v) { ret.assign(v.begin(), v.end()); });
        return ret;
    };

    // then branch
    {
        std::vector<float> gold_ret = {
            1.384804, -0.77947998, 0.54622501, 1.477438, -0.063330054, -0.12892997};
        auto ret = run_prog(true);
        EXPECT(gold_ret == ret);
3477
3478
    }

Shucai Xiao's avatar
Shucai Xiao committed
3479
    // else branch
3480
    {
Shucai Xiao's avatar
Shucai Xiao committed
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
        std::vector<float> gold_ret = {
            1.483906, 2.720788, 3.0736079, 0.84447598, 4.0433998, 4.04884};
        auto ret = run_prog(false);
        EXPECT(gold_ret == ret);
    }
}

TEST_CASE(if_pl_test)
{
    auto create_program = [] {
3491
3492
        migraphx::program p;
        auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
        migraphx::shape cond_s{migraphx::shape::bool_type};
        migraphx::shape s{migraphx::shape::float_type, {5}};
        auto cond = mm->add_parameter("cond", cond_s);
        auto x    = mm->add_parameter("x", s);

        auto* then_mod           = p.create_module("If_0_if");
        std::vector<float> data1 = {1, 2, 3, 4, 5};
        auto l1                  = then_mod->add_literal(migraphx::literal(s, data1));
        then_mod->add_return({l1, x});

        auto* else_mod           = p.create_module("If_0_else");
        std::vector<float> data2 = {5, 4, 3, 2, 1};
        auto l2                  = else_mod->add_literal(migraphx::literal(s, data2));
        auto s2                  = else_mod->add_instruction(migraphx::make_op("add"), x, l2);
        else_mod->add_return({s2, l2});

        auto ret     = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
        auto outline = mm->add_outline(s);
Shucai Xiao's avatar
Shucai Xiao committed
3511
3512
        auto r = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
        mm->add_return({outline, r});
Shucai Xiao's avatar
Shucai Xiao committed
3513
3514
3515
3516
3517
3518

        return p;
    };

    auto run_prog = [&](bool cond) {
        auto p = create_program();
3519
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
        std::vector<char> c_data = {static_cast<char>(cond)};
        migraphx::shape cs{migraphx::shape::bool_type};
        migraphx::parameter_map m;
        m["cond"] = migraphx::argument(cs, c_data.data());
        migraphx::shape ds{migraphx::shape::float_type, {5}};
        std::vector<float> data(ds.elements(), 1);
        m["x"] = migraphx::argument(ds, data.data());

        auto res = p.eval(m).back();
        std::vector<float> ret;
        res.visit([&](auto v) { ret.assign(v.begin(), v.end()); });

        return ret;
    };

    // then branch
    {
        std::vector<float> gold_ret = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
        auto ret                    = run_prog(true);
        EXPECT(gold_ret == ret);
    }

    // else branch
    {
        std::vector<float> gold_ret = {6.0f, 5.0f, 4.0f, 3.0f, 2.0f};
        auto ret                    = run_prog(false);
        EXPECT(gold_ret == ret);
3547
3548
3549
    }
}

Charlie Lin's avatar
Charlie Lin committed
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
TEST_CASE(isnan_test)
{
    // float test
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3}};
        auto nan_val             = std::numeric_limits<float>::quiet_NaN();
        std::vector<float> data0 = {1.2, 5.2, nan_val, nan_val, 0., 100.};
        auto l1                  = mm->add_literal(migraphx::literal{s, data0});
        mm->add_instruction(migraphx::make_op("isnan"), l1);
3561
        p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> correct = {0, 0, 1, 1, 0, 0};
        EXPECT(migraphx::verify_range(results_vector, correct));
    }

    // half test
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::half_type, {2, 3}};
        auto nan_val = std::numeric_limits<migraphx::half>::quiet_NaN();
        migraphx::half a{1.2};
        migraphx::half b{5.2};
        std::vector<migraphx::half> data0 = {a, b, nan_val, nan_val, b, a};
        auto l1                           = mm->add_literal(migraphx::literal{s, data0});
        mm->add_instruction(migraphx::make_op("isnan"), l1);
3580
        p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
3581
3582
3583
3584
3585
3586
3587
3588
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> correct = {0, 0, 1, 1, 0, 0};
        EXPECT(migraphx::verify_range(results_vector, correct));
    }
}

Charlie Lin's avatar
Charlie Lin committed
3589
TEST_CASE(isnan_dyn_test)
3590
3591
3592
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3593
    migraphx::shape s{migraphx::shape::float_type, {{2, 2}, {3, 8}}};
Charlie Lin's avatar
Charlie Lin committed
3594
3595
    auto input   = mm->add_parameter("X", s);
    auto nan_val = std::numeric_limits<float>::quiet_NaN();
3596
    mm->add_instruction(migraphx::make_op("isnan"), input);
3597
    p.compile(migraphx::make_target("ref"));
3598

Charlie Lin's avatar
Charlie Lin committed
3599
    std::vector<float> input_data = {1.2, 5.2, nan_val, nan_val, 0., 100.};
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> correct = {0, 0, 1, 1, 0, 0};
    EXPECT(migraphx::verify_range(results_vector, correct));
}

Shucai Xiao's avatar
Shucai Xiao committed
3610
TEST_CASE(im2col_3x3_no_pad_identity_test)
Shucai Xiao's avatar
Shucai Xiao committed
3611
{
Shucai Xiao's avatar
Shucai Xiao committed
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {3, 3};
    std::vector<std::size_t> padding{0, 0};
    std::vector<std::size_t> stride{1, 1};
    std::vector<std::size_t> 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);

Shucai Xiao's avatar
Shucai Xiao committed
3623
3624
    migraphx::program p;
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3625
3626
3627
3628
3629
3630
3631
3632
3633
    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   = mm->add_literal(migraphx::literal{s_image, input});
    auto l_weights = mm->add_literal(migraphx::literal{s_weights, weights});
    mm->add_instruction(
        migraphx::make_op("im2col",
                          {{"padding", padding}, {"stride", stride}, {"dilation", dilation}}),
        l_image,
        l_weights);
3634
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3635
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667

    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()); });
    EXPECT(migraphx::verify_range(results_vector, input));
}

TEST_CASE(im2col_3x3_no_pad_test)
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {4, 4};
    std::vector<std::size_t> padding{0, 0};
    std::vector<std::size_t> stride{1, 1};
    std::vector<std::size_t> 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);

    migraphx::program p;
    auto* mm = p.get_main_module();
    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   = mm->add_literal(migraphx::literal{s_image, input});
    auto l_weights = mm->add_literal(migraphx::literal{s_weights, weights});
    mm->add_instruction(
        migraphx::make_op("im2col",
                          {{"padding", padding}, {"stride", stride}, {"dilation", dilation}}),
        l_image,
        l_weights);
3668
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
    auto result = p.eval({}).back();

    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()); });
    EXPECT(migraphx::verify_range(results_vector, correct));
}

TEST_CASE(im2col_3x3_stride_2_no_pad_test)
{
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {6, 6};
    std::vector<std::size_t> padding{0, 0};
    std::vector<std::size_t> stride{2, 2};
    std::vector<std::size_t> 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);

    migraphx::program p;
    auto* mm = p.get_main_module();
    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   = mm->add_literal(migraphx::literal{s_image, input});
    auto l_weights = mm->add_literal(migraphx::literal{s_weights, weights});
    mm->add_instruction(
        migraphx::make_op("im2col",
                          {{"padding", padding}, {"stride", stride}, {"dilation", dilation}}),
        l_image,
        l_weights);
3705
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3706
3707
3708
3709
3710
3711
3712
3713
3714
    auto result = p.eval({}).back();

    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);
Shucai Xiao's avatar
Shucai Xiao committed
3715
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3716
    EXPECT(migraphx::verify_range(results_vector, correct));
Shucai Xiao's avatar
Shucai Xiao committed
3717
3718
}

Shucai Xiao's avatar
Shucai Xiao committed
3719
TEST_CASE(im2col_3x3_with_channels_identity_test)
Shucai Xiao's avatar
Shucai Xiao committed
3720
{
Shucai Xiao's avatar
Shucai Xiao committed
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {3, 3};
    std::vector<std::size_t> padding{0, 0};
    std::vector<std::size_t> stride{1, 1};
    std::vector<std::size_t> 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);

Shucai Xiao's avatar
Shucai Xiao committed
3732
3733
    migraphx::program p;
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3734
3735
3736
3737
3738
3739
3740
3741
3742
    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   = mm->add_literal(migraphx::literal{s_image, input});
    auto l_weights = mm->add_literal(migraphx::literal{s_weights, weights});
    mm->add_instruction(
        migraphx::make_op("im2col",
                          {{"padding", padding}, {"stride", stride}, {"dilation", dilation}}),
        l_image,
        l_weights);
3743
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
3744
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3745
3746
3747
3748

    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);
Shucai Xiao's avatar
Shucai Xiao committed
3749
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
3750
    EXPECT(migraphx::verify_range(results_vector, input));
Shucai Xiao's avatar
Shucai Xiao committed
3751
}
3752

Shucai Xiao's avatar
Shucai Xiao committed
3753
TEST_CASE(im2col_3x3_with_padding_test)
3754
{
Shucai Xiao's avatar
Shucai Xiao committed
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
    std::size_t f[2]    = {3, 3};
    std::size_t size[2] = {2, 2};
    std::vector<std::size_t> padding{1, 1};
    std::vector<std::size_t> stride{1, 1};
    std::vector<std::size_t> 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);

3766
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
    auto* mm = p.get_main_module();
    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   = mm->add_literal(migraphx::literal{s_image, input});
    auto l_weights = mm->add_literal(migraphx::literal{s_weights, weights});
    mm->add_instruction(
        migraphx::make_op("im2col",
                          {{"padding", padding}, {"stride", stride}, {"dilation", dilation}}),
        l_image,
        l_weights);
3777
    p.compile(migraphx::make_target("ref"));
3778
    auto result = p.eval({}).back();
3779

Shucai Xiao's avatar
Shucai Xiao committed
3780
3781
3782
3783
3784
3785
3786
3787
    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()); });
    EXPECT(migraphx::verify_range(results_vector, correct));
3788
3789
}

Shucai Xiao's avatar
Shucai Xiao committed
3790
TEST_CASE(imagescaler_test)
3791
3792
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {1, 3, 2, 2}};
    auto img           = mm->add_literal(migraphx::literal{s,
                                                 {0.2,
                                                  0.3,
                                                  0.5,
                                                  0.4,

                                                  0.7,
                                                  0.8,
                                                  0.1,
                                                  0.9,

                                                  0.15,
                                                  0.25,
                                                  0.35,
                                                  0.45}});
    auto scale_val     = mm->add_literal(2.f);
    auto scaled_tensor = mm->add_instruction(
        migraphx::make_op("scalar", {{"scalar_bcst_dims", s.lens()}}), scale_val);
    auto img_scaled = mm->add_instruction(migraphx::make_op("mul"), img, scaled_tensor);
    auto bias_vals  = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {0.01, 0.02, 0.03}});
    auto bias_bcast = mm->add_instruction(
3817
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals);
Shucai Xiao's avatar
Shucai Xiao committed
3818
    mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
3819
    p.compile(migraphx::make_target("ref"));
3820
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3821
3822
3823
3824
3825
3826
    std::vector<float> results_vector(12);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.41,
                               0.61,
                               1.01,
                               0.81,
3827

Shucai Xiao's avatar
Shucai Xiao committed
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
                               1.42,
                               1.62,
                               0.22,
                               1.82,

                               0.33,
                               0.53,
                               0.73,
                               0.93};
    EXPECT(migraphx::verify_range(results_vector, gold));
3838
3839
}

Shucai Xiao's avatar
Shucai Xiao committed
3840
TEST_CASE(leaky_relu_test)
3841
3842
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3843
3844
3845
3846
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = mm->add_literal(migraphx::literal{s, {-1.f, 0.f, 1.f}});
    mm->add_instruction(migraphx::make_op("leaky_relu", {{"alpha", 0.01}}), l);
3847
    p.compile(migraphx::make_target("ref"));
3848
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3849
3850
3851
3852
    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};
    EXPECT(migraphx::verify_range(results_vector, gold));
3853
3854
}

Shucai Xiao's avatar
Shucai Xiao committed
3855
TEST_CASE(less_brcst_test)
3856
3857
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3858
3859
3860
3861
3862
    auto* mm = p.get_main_module();
    migraphx::shape s0{migraphx::shape::float_type, {3, 3}};
    auto l0 =
        mm->add_literal(migraphx::literal{s0, {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0}});
    migraphx::shape s1{migraphx::shape::float_type, {3, 1}};
3863
3864
3865
3866
    auto l1  = mm->add_literal(migraphx::literal{s1, {1.1, -1.5, 0.0}});
    auto bl1 = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), l1);
    auto le  = mm->add_instruction(migraphx::make_op("less"), l0, bl1);
    auto r   = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
3867
3868
3869
3870
3871
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        le);
    mm->add_return({r});

3872
    p.compile(migraphx::make_target("ref"));
3873
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3874
3875
3876
3877
    std::vector<bool> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold = {false, false, true, false, false, false, false, false, true};
    EXPECT(results_vector == gold);
3878
3879
}

Shucai Xiao's avatar
Shucai Xiao committed
3880
TEST_CASE(less_test)
3881
3882
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3883
3884
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {9}};
3885
3886
3887
3888
3889
3890
    std::vector<float> data1 = {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
    std::vector<float> data2 = {1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1};
    auto l0                  = mm->add_literal(migraphx::literal{s, data1});
    auto l1                  = mm->add_literal(migraphx::literal{s, data2});
    auto le                  = mm->add_instruction(migraphx::make_op("less"), l0, l1);
    auto r                   = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
3891
3892
3893
3894
3895
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        le);
    mm->add_return({r});

3896
    p.compile(migraphx::make_target("ref"));
3897
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3898
3899
    std::vector<bool> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
3900
3901
3902
3903
3904
    std::vector<bool> gold(data1.size());
    std::transform(
        data1.begin(), data1.end(), data2.begin(), gold.begin(), [](float n1, float n2) -> bool {
            return n1 < n2;
        });
Shucai Xiao's avatar
Shucai Xiao committed
3905
    EXPECT(results_vector == gold);
3906
3907
}

3908
3909
3910
3911
TEST_CASE(less_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3912
    std::vector<migraphx::shape::dynamic_dimension> dd{{8, 10, {9}}};
3913
3914
3915
3916
3917
3918
3919
3920
3921
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto left  = mm->add_parameter("l", s);
    auto right = mm->add_parameter("r", s);
    auto le    = mm->add_instruction(migraphx::make_op("less"), left, right);
    auto r     = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
        le);
    mm->add_return({r});
3922
    p.compile(migraphx::make_target("ref"));
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941

    std::vector<float> left_data  = {1.1, 1.5, 0.1, -1.1, -1.5, -0.6, 0.0, 2.0, -2.0};
    std::vector<float> right_data = {1.1, 1.6, -0.1, -1.2, -1.5, -0.7, 0.0, 2.3, -2.1};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["l"] = migraphx::argument(input_fixed_shape0, left_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, right_data.data());
    auto result  = p.eval(params0).back();
    std::vector<bool> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold(left_data.size());
    std::transform(left_data.begin(),
                   left_data.end(),
                   right_data.begin(),
                   gold.begin(),
                   [](float n1, float n2) -> bool { return n1 < n2; });
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
3942
TEST_CASE(log_test)
3943
3944
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3945
3946
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
3947
3948
    std::vector<float> data = {1, 2, 3};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
3949
    mm->add_instruction(migraphx::make_op("log"), l);
3950
    p.compile(migraphx::make_target("ref"));
3951
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3952
3953
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
3954
3955
3956
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return logf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
3957
    EXPECT(migraphx::verify_range(results_vector, gold));
3958
3959
}

Charlie Lin's avatar
Charlie Lin committed
3960
TEST_CASE(log_dyn_test)
3961
3962
3963
{
    migraphx::program p;
    auto* mm = p.get_main_module();
3964
    migraphx::shape::dynamic_dimension dd{3, 8};
3965
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
3966
    auto input = mm->add_parameter("X", s);
3967
    mm->add_instruction(migraphx::make_op("log"), input);
3968
    p.compile(migraphx::make_target("ref"));
3969

Charlie Lin's avatar
Charlie Lin committed
3970
    std::vector<float> input_data = {1, 2, 3};
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return logf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
3983
TEST_CASE(logical_and_test)
3984
3985
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3986
3987
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::bool_type, {4}};
3988
3989
3990
3991
    std::vector<bool> data1{true, false, true, false};
    std::vector<bool> data2{true, true, false, false};
    auto l1 = mm->add_literal(migraphx::literal{s, data1});
    auto l2 = mm->add_literal(migraphx::literal{s, data2});
Shucai Xiao's avatar
Shucai Xiao committed
3992
    mm->add_instruction(migraphx::make_op("logical_and"), l1, l2);
3993
    p.compile(migraphx::make_target("ref"));
3994
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
3995
3996
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
3997
3998
3999
4000
4001
    std::vector<bool> gold(data2.size());
    std::transform(
        data1.begin(), data1.end(), data2.begin(), gold.begin(), [](bool n1, bool n2) -> bool {
            return n1 and n2;
        });
Shucai Xiao's avatar
Shucai Xiao committed
4002
    EXPECT(migraphx::verify_range(results_vector, gold));
4003
4004
}

4005
4006
4007
4008
TEST_CASE(logical_and_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4009
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6, {4}}};
4010
4011
4012
4013
    migraphx::shape s{migraphx::shape::bool_type, dd};
    auto left  = mm->add_parameter("l", s);
    auto right = mm->add_parameter("r", s);
    mm->add_instruction(migraphx::make_op("logical_and"), left, right);
4014
    p.compile(migraphx::make_target("ref"));
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033

    std::vector<char> left_data{1, 0, 1, 0};
    std::vector<char> right_data{1, 1, 0, 0};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::bool_type, {4}};
    params0["l"] = migraphx::argument(input_fixed_shape0, left_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, right_data.data());
    auto result  = p.eval(params0).back();
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold(left_data.size());
    std::transform(left_data.begin(),
                   left_data.end(),
                   right_data.begin(),
                   gold.begin(),
                   [](bool n1, bool n2) -> bool { return n1 and n2; });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
4034
TEST_CASE(logical_or_test)
4035
4036
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4037
4038
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::bool_type, {4}};
4039
4040
4041
4042
    std::vector<bool> data1{true, false, true, false};
    std::vector<bool> data2{true, true, false, false};
    auto l1 = mm->add_literal(migraphx::literal{s, data1});
    auto l2 = mm->add_literal(migraphx::literal{s, data2});
Shucai Xiao's avatar
Shucai Xiao committed
4043
    mm->add_instruction(migraphx::make_op("logical_or"), l1, l2);
4044
    p.compile(migraphx::make_target("ref"));
4045
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4046
4047
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
4048
4049
4050
4051
4052
    std::vector<bool> gold(data1.size());
    std::transform(
        data1.begin(), data1.end(), data2.begin(), gold.begin(), [](bool n1, bool n2) -> bool {
            return n1 or n2;
        });
Shucai Xiao's avatar
Shucai Xiao committed
4053
    EXPECT(migraphx::verify_range(results_vector, gold));
4054
4055
}

4056
4057
4058
4059
TEST_CASE(logical_or_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4060
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6, {4}}};
4061
4062
4063
4064
    migraphx::shape s{migraphx::shape::bool_type, dd};
    auto left  = mm->add_parameter("l", s);
    auto right = mm->add_parameter("r", s);
    mm->add_instruction(migraphx::make_op("logical_or"), left, right);
4065
    p.compile(migraphx::make_target("ref"));
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084

    std::vector<char> left_data{1, 0, 1, 0};
    std::vector<char> right_data{1, 1, 0, 0};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::bool_type, {4}};
    params0["l"] = migraphx::argument(input_fixed_shape0, left_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, right_data.data());
    auto result  = p.eval(params0).back();
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold(left_data.size());
    std::transform(left_data.begin(),
                   left_data.end(),
                   right_data.begin(),
                   gold.begin(),
                   [](bool n1, bool n2) -> bool { return n1 or n2; });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
4085
TEST_CASE(logical_xor_test)
Shucai Xiao's avatar
Shucai Xiao committed
4086
4087
{
    migraphx::program p;
4088
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
4089
    migraphx::shape s{migraphx::shape::bool_type, {4}};
4090
4091
4092
4093
    std::vector<bool> data1{true, false, true, false};
    std::vector<bool> data2{true, true, false, false};
    auto l1 = mm->add_literal(migraphx::literal{s, data1});
    auto l2 = mm->add_literal(migraphx::literal{s, data2});
Shucai Xiao's avatar
Shucai Xiao committed
4094
    mm->add_instruction(migraphx::make_op("logical_xor"), l1, l2);
4095
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
4096
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4097
4098
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
4099
4100
4101
4102
4103
    std::vector<bool> gold = {false, true, true, false};
    std::transform(
        data1.begin(), data1.end(), data2.begin(), gold.begin(), [](bool n1, bool n2) -> bool {
            return n1 ^ n2;
        });
Shucai Xiao's avatar
Shucai Xiao committed
4104
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
4105
4106
}

4107
4108
4109
4110
TEST_CASE(logical_xor_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4111
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6, {4}}};
4112
4113
4114
4115
    migraphx::shape s{migraphx::shape::bool_type, dd};
    auto left  = mm->add_parameter("l", s);
    auto right = mm->add_parameter("r", s);
    mm->add_instruction(migraphx::make_op("logical_xor"), left, right);
4116
    p.compile(migraphx::make_target("ref"));
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135

    std::vector<char> left_data{1, 0, 1, 0};
    std::vector<char> right_data{1, 1, 0, 0};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::bool_type, {4}};
    params0["l"] = migraphx::argument(input_fixed_shape0, left_data.data());
    params0["r"] = migraphx::argument(input_fixed_shape0, right_data.data());
    auto result  = p.eval(params0).back();
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<bool> gold = {false, true, true, false};
    std::transform(left_data.begin(),
                   left_data.end(),
                   right_data.begin(),
                   gold.begin(),
                   [](bool n1, bool n2) -> bool { return n1 ^ n2; });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
4136
TEST_CASE(logsoftmax_test_axis_0)
4137
{
Paul's avatar
Paul committed
4138
    migraphx::program p;
4139
    auto* mm             = p.get_main_module();
4140
    std::vector<float> a = {
Shucai Xiao's avatar
Shucai Xiao committed
4141
4142
4143
4144
4145
4146
4147
4148
        1.93885877,  -1.20006269, 0.90960855,  0.42108916,  -1.50797544, -1.31047913, 1.07816336,
        -1.13288733, -0.86411064, 0.97800238,  0.76631385,  2.07962834,  -0.8940665,  -1.62855592,
        -0.53763057, -1.48165117, -0.64154112, 0.42486547,  0.89330917,  -2.42022666, 0.192611,
        -0.01257413, -1.5326607,  0.53137897,  -1.52383859, 0.46994381,  0.00453619,  0.0066996,
        1.58394908,  0.84216752,  -0.04137941, -0.88580789, 1.44055158,  -0.17621241, -1.98917923,
        -0.08610038, 0.79020567,  -0.67714548, 0.42774631,  0.1376574,   2.23569227,  1.16681234,
        -1.21191456, -0.28411502, -0.18688975, 1.67552548,  2.48357974,  0.95891282,  -0.06616535,
        -0.99628491, 1.04314606,  -1.22943315, 0.76930403,  0.31106618};
4149

Shucai Xiao's avatar
Shucai Xiao committed
4150
4151
4152
4153
4154
4155
4156
4157
    std::vector<float> s = {
        -0.135261, -2.843968, -0.659995, -0.488413, -1.051857, -2.812936, -0.250956, -0.353985,
        -1.155980, -0.603651, -0.211969, -0.175371, -1.336552, -3.885010, -1.871544, -0.837083,
        -0.887745, -0.433338, -1.158864, -4.911197, -1.147972, -0.666711, -0.996874, -0.981418,
        -0.851145, -0.853988, -0.858112, -2.067420, -0.059956, -0.727436, -0.950881, -0.429689,
        -0.061906, -1.505332, -1.210277, -0.377970, -0.791448, -1.655428, -1.827253, -0.304828,
        -0.020762, -0.167101, -0.567346, -0.530319, -1.045094, -0.376648, -0.007391, -0.381670,
        -0.720302, -0.460499, -0.469651, -0.556740, -0.554628, -0.551582};
4158

Shucai Xiao's avatar
Shucai Xiao committed
4159
4160
4161
4162
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto al  = mm->add_literal(migraphx::literal{a_shape, a});
    int axis = 0;
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), al);
4163
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
4164
4165
4166
4167
4168
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, s));
}
Scott Thornton's avatar
Scott Thornton committed
4169

Shucai Xiao's avatar
Shucai Xiao committed
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
TEST_CASE(logsoftmax_test_axis_1)
{
    migraphx::program p;
    auto* mm             = p.get_main_module();
    std::vector<float> a = {
        1.93885877,  -1.20006269, 0.90960855,  0.42108916,  -1.50797544, -1.31047913, 1.07816336,
        -1.13288733, -0.86411064, 0.97800238,  0.76631385,  2.07962834,  -0.8940665,  -1.62855592,
        -0.53763057, -1.48165117, -0.64154112, 0.42486547,  0.89330917,  -2.42022666, 0.192611,
        -0.01257413, -1.5326607,  0.53137897,  -1.52383859, 0.46994381,  0.00453619,  0.0066996,
        1.58394908,  0.84216752,  -0.04137941, -0.88580789, 1.44055158,  -0.17621241, -1.98917923,
        -0.08610038, 0.79020567,  -0.67714548, 0.42774631,  0.1376574,   2.23569227,  1.16681234,
        -1.21191456, -0.28411502, -0.18688975, 1.67552548,  2.48357974,  0.95891282,  -0.06616535,
        -0.99628491, 1.04314606,  -1.22943315, 0.76930403,  0.31106618};
Scott Thornton's avatar
Scott Thornton committed
4183

Shucai Xiao's avatar
Shucai Xiao committed
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
    std::vector<float> s = {
        -0.550468, -2.132973, -1.549746, -0.650533, -1.051529, -2.248570, -0.141017, -2.028357,
        -1.947730, -1.511324, -0.166597, -0.379726, -1.965689, -1.172109, -1.475721, -2.700831,
        -1.537011, -0.658754, -1.596017, -3.353137, -2.266743, -1.084197, -1.076214, -0.406712,
        -2.743019, -0.425526, -1.079083, -2.139486, -1.270584, -1.024088, -1.154231, -3.201762,
        -0.888957, -0.532855, -3.103583, -1.221339, -1.355980, -3.531678, -1.438510, -0.975194,
        -0.080261, -1.162697, -1.568557, -1.398519, -1.322129, -0.470660, -0.370953, -0.907343,
        -1.179017, -3.312239, -1.286363, -1.586076, -0.345100, -0.824173};

    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto al  = mm->add_literal(migraphx::literal{a_shape, a});
    int axis = 1;
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), al);
4197
    p.compile(migraphx::make_target("ref"));
4198
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4199
    std::vector<float> results_vector;
4200
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Paul's avatar
Paul committed
4201
    EXPECT(migraphx::verify_range(results_vector, s));
Scott Thornton's avatar
Scott Thornton committed
4202
4203
}

Shucai Xiao's avatar
Shucai Xiao committed
4204
TEST_CASE(logsoftmax_test_axis_2)
Paul Fultz II's avatar
Paul Fultz II committed
4205
4206
{
    migraphx::program p;
4207
    auto* mm             = p.get_main_module();
Paul Fultz II's avatar
Paul Fultz II committed
4208
    std::vector<float> a = {
Shucai Xiao's avatar
Shucai Xiao committed
4209
4210
4211
4212
4213
4214
4215
4216
        1.93885877,  -1.20006269, 0.90960855,  0.42108916,  -1.50797544, -1.31047913, 1.07816336,
        -1.13288733, -0.86411064, 0.97800238,  0.76631385,  2.07962834,  -0.8940665,  -1.62855592,
        -0.53763057, -1.48165117, -0.64154112, 0.42486547,  0.89330917,  -2.42022666, 0.192611,
        -0.01257413, -1.5326607,  0.53137897,  -1.52383859, 0.46994381,  0.00453619,  0.0066996,
        1.58394908,  0.84216752,  -0.04137941, -0.88580789, 1.44055158,  -0.17621241, -1.98917923,
        -0.08610038, 0.79020567,  -0.67714548, 0.42774631,  0.1376574,   2.23569227,  1.16681234,
        -1.21191456, -0.28411502, -0.18688975, 1.67552548,  2.48357974,  0.95891282,  -0.06616535,
        -0.99628491, 1.04314606,  -1.22943315, 0.76930403,  0.31106618};
Paul Fultz II's avatar
Paul Fultz II committed
4217

Shucai Xiao's avatar
Shucai Xiao committed
4218
4219
4220
4221
4222
4223
4224
4225
    std::vector<float> s = {
        -0.495957, -1.031212, -0.245531, -2.013726, -1.339125, -2.465619, -1.356652, -0.964037,
        -2.019250, -0.214522, -0.289569, -0.234392, -2.086591, -2.684439, -2.851651, -2.674176,
        -1.697424, -1.889155, -0.401029, -3.064586, -1.173030, -1.306912, -2.177020, -0.834262,
        -2.818177, -0.174415, -1.361105, -1.024571, -0.106766, -1.167645, -1.072650, -2.576522,
        -0.569261, -1.207483, -3.679894, -2.095913, -0.504264, -3.039291, -1.290559, -1.156812,
        -0.126453, -0.551493, -2.506384, -2.646261, -1.905195, -0.206994, -0.191369, -0.959754,
        -1.948685, -3.671233, -0.875521, -3.111952, -1.905644, -1.6076011};
Paul Fultz II's avatar
Paul Fultz II committed
4226

Shucai Xiao's avatar
Shucai Xiao committed
4227
4228
4229
4230
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto al  = mm->add_literal(migraphx::literal{a_shape, a});
    int axis = 2;
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), al);
4231
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
4232
4233
4234
4235
4236
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, s));
}
Paul Fultz II's avatar
Paul Fultz II committed
4237

Shucai Xiao's avatar
Shucai Xiao committed
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
TEST_CASE(logsoftmax_test_axis_3)
{
    migraphx::program p;
    auto* mm             = p.get_main_module();
    std::vector<float> a = {
        1.93885877,  -1.20006269, 0.90960855,  0.42108916,  -1.50797544, -1.31047913, 1.07816336,
        -1.13288733, -0.86411064, 0.97800238,  0.76631385,  2.07962834,  -0.8940665,  -1.62855592,
        -0.53763057, -1.48165117, -0.64154112, 0.42486547,  0.89330917,  -2.42022666, 0.192611,
        -0.01257413, -1.5326607,  0.53137897,  -1.52383859, 0.46994381,  0.00453619,  0.0066996,
        1.58394908,  0.84216752,  -0.04137941, -0.88580789, 1.44055158,  -0.17621241, -1.98917923,
        -0.08610038, 0.79020567,  -0.67714548, 0.42774631,  0.1376574,   2.23569227,  1.16681234,
        -1.21191456, -0.28411502, -0.18688975, 1.67552548,  2.48357974,  0.95891282,  -0.06616535,
        -0.99628491, 1.04314606,  -1.22943315, 0.76930403,  0.31106618};
Paul Fultz II's avatar
Paul Fultz II committed
4251

Shucai Xiao's avatar
Shucai Xiao committed
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
    std::vector<float> s = {
        -0.336904, -3.475825, -1.366154, -0.279366, -2.208430, -2.010934, -0.225511, -2.436562,
        -2.167785, -1.572415, -1.784104, -0.470789, -1.067459, -1.801948, -0.711023, -2.307197,
        -1.467087, -0.400681, -0.426983, -3.740518, -1.127681, -1.078919, -2.599005, -0.534965,
        -2.561400, -0.567617, -1.033025, -2.097713, -0.520463, -1.262245, -1.763230, -2.607658,
        -0.281299, -0.814243, -2.627210, -0.724131, -0.655704, -2.123055, -1.018163, -2.480634,
        -0.382599, -1.451479, -1.843102, -0.915303, -0.818078, -1.316929, -0.508875, -2.033541,
        -1.487672, -2.417791, -0.378360, -2.568531, -0.569794, -1.028032};

    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 3, 3}};
    auto al  = mm->add_literal(migraphx::literal{a_shape, a});
    int axis = 3;
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), al);
4265
    p.compile(migraphx::make_target("ref"));
Paul Fultz II's avatar
Paul Fultz II committed
4266
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4267
    std::vector<float> results_vector;
Paul Fultz II's avatar
Paul Fultz II committed
4268
4269
4270
4271
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, s));
}

Charlie Lin's avatar
Charlie Lin committed
4272
TEST_CASE(lppool_l1_norm_test)
4273
4274
{
    // L1 norm test
Charlie Lin's avatar
Charlie Lin committed
4275
4276
4277
4278
4279
4280
4281
4282
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto s      = migraphx::shape{migraphx::shape::float_type, {1, 3, 4}};
    auto op     = migraphx::op::pooling{migraphx::op::pooling_mode::lpnorm};
    op.lengths  = {2};
    op.padding  = {0};
    op.stride   = {1};
    op.lp_order = 1;
4283

Charlie Lin's avatar
Charlie Lin committed
4284
4285
4286
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4287
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4288
    auto result = p.eval({}).back();
4289

Charlie Lin's avatar
Charlie Lin committed
4290
4291
4292
4293
4294
4295
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.5, 0.6, 0.5, 1.3, 1.4, 1.0, 0.8, 0.8, 0.7};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
// TODO: this tests compliance with a oneDNN rule and a feature that's commented out
// in pooling.hpp
// TEST_CASE(lppool_l1_norm_err_test)
// {
//     // padding too large for kernel size
//     migraphx::program p;
//     auto* mm    = p.get_main_module();
//     auto s      = migraphx::shape{migraphx::shape::float_type, {1, 2, 5}};
//     auto op     = migraphx::op::pooling{migraphx::op::pooling_mode::lpnorm};
//     op.lengths  = {3};
//     op.padding  = {2};
//     op.stride   = {1};
//     op.lp_order = 1;

//     std::vector<float> data{0.3, 0.2, 0.4, 0.1, 0.8, 0.5, 0.9, 0.1, 0.1, 0.7};
//     auto l0 = mm->add_literal(migraphx::literal{s, data});
//     EXPECT(test::throws([&] {
//             mm->add_instruction(op, l0);
//         }));
// }

Charlie Lin's avatar
Charlie Lin committed
4317
4318
TEST_CASE(lppool_l2_norm_test)
{
4319
    // L2 norm test
Charlie Lin's avatar
Charlie Lin committed
4320
4321
4322
4323
4324
4325
4326
4327
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto s      = migraphx::shape{migraphx::shape::float_type, {1, 3, 4}};
    auto op     = migraphx::op::pooling{migraphx::op::pooling_mode::lpnorm};
    op.lengths  = {2};
    op.padding  = {0};
    op.stride   = {1};
    op.lp_order = 2;
4328

Charlie Lin's avatar
Charlie Lin committed
4329
4330
4331
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4332
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
    auto result = p.eval({}).back();

    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.36055512754639896,
                            0.447213595499958,
                            0.4123105625617661,
                            0.9433981132056605,
                            1.0295630140987,
                            0.9055385138137417,
                            0.7071067811865475,
                            0.7071067811865475,
                            0.6082762530298219};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(lppool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4353
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
4354
4355
4356
4357
4358
4359
4360
    auto x   = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::lpnorm},
                                           {"lengths", {2}},
                                           {"padding", {0}},
                                           {"stride", {1}}}),
                        x);
4361
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 4}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.36055512754639896,
                            0.447213595499958,
                            0.4123105625617661,
                            0.9433981132056605,
                            1.0295630140987,
                            0.9055385138137417,
                            0.7071067811865475,
                            0.7071067811865475,
                            0.6082762530298219};
    EXPECT(migraphx::verify_range(results_vector, gold));
4380
4381
}

Shucai Xiao's avatar
Shucai Xiao committed
4382
TEST_CASE(lrn_test)
4383
{
Paul's avatar
Paul committed
4384
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4385
4386
4387
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {1, 5, 1, 1}};
    auto l = mm->add_literal(migraphx::literal{s, {-2.0f, 1.0f, 0.f, 1.0f, 2.0f}});
4388
    mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
4389
        migraphx::make_op("lrn", {{"alpha", 0.0001}, {"beta", 0.75}, {"bias", 1}, {"size", 5}}), l);
4390
    p.compile(migraphx::make_target("ref"));
4391
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4392
4393
4394
4395
4396
    std::vector<float> results_vector(5);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-2 / 1.000075, 1 / 1.00009, 0 / 1.000145, 1 / 1.00009, 2 / 1.000075};
    EXPECT(migraphx::verify_range(results_vector, gold));
}
Scott Thornton's avatar
Scott Thornton committed
4397

Shucai Xiao's avatar
Shucai Xiao committed
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
TEST_CASE(max_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {1, 4, 3}});
    auto l1       = mm->add_literal(migraphx::literal{s, {2, 8, 6}});
    auto l2       = mm->add_literal(migraphx::literal{s, {7, 5, 9}});
    auto curr_max = mm->add_instruction(migraphx::make_op("max"), l0, l1);
    mm->add_instruction(migraphx::make_op("max"), curr_max, l2);
4408
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
4409
4410
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
4411
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
4412
4413
    std::vector<float> gold{7, 8, 9};
    EXPECT(migraphx::verify_range(results_vector, gold));
4414
4415
}

4416
4417
4418
4419
TEST_CASE(max_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4420
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
4421
4422
4423
4424
4425
4426
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x        = mm->add_parameter("x", s);
    auto y        = mm->add_parameter("y", s);
    auto z        = mm->add_parameter("z", s);
    auto curr_max = mm->add_instruction(migraphx::make_op("max"), x, y);
    mm->add_instruction(migraphx::make_op("max"), curr_max, z);
4427
    p.compile(migraphx::make_target("ref"));
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443

    std::vector<float> x_data{1, 4, 3};
    std::vector<float> y_data{2, 8, 6};
    std::vector<float> z_data{7, 5, 9};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    params0["z"] = migraphx::argument(input_fixed_shape0, z_data.data());
    auto result  = p.eval(params0).back();
    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));
}

Shucai Xiao's avatar
Shucai Xiao committed
4444
TEST_CASE(maxpool_test)
4445
{
Paul's avatar
Paul committed
4446
    migraphx::program p;
4447
    auto* mm             = p.get_main_module();
4448
    std::vector<float> a = {
Shucai Xiao's avatar
Shucai Xiao committed
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
        -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};
    migraphx::shape a_shape{migraphx::shape::float_type, {2, 3, 6, 6}};
4487
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
4488
4489
4490
4491
4492
4493
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0}},
                                           {"stride", {2, 2}},
                                           {"lengths", {3, 2}}}),
                        al);
4494
    p.compile(migraphx::make_target("ref"));
4495
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4496
    std::vector<float> results_vector(36);
4497
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
4498
    EXPECT(migraphx::verify_range(results_vector, c));
Scott Thornton's avatar
Scott Thornton committed
4499
}
4500

4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
TEST_CASE(maxpool_pad_test)
{
    migraphx::program p;
    auto* mm             = p.get_main_module();
    std::vector<float> a = {-6, -5, -4, -3, -5, -1, 0, 1, 2, 3, 4, 5};
    std::vector<float> c = {-4, -3, -4, -1, 2, 3, 4, 5};
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 2, 3, 2}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {1, 1}},
                                           {"stride", {2, 2}},
                                           {"lengths", {3, 2}}}),
                        al);

    //   * *  *  *                                           * *  *  *
    //   * -6 -5 *                                           * 0  1  *
    //   * -4 -3 *      padding will look like this          * 2  3  *
    //   * -5 -1 *                  and this                 * 4  5  *
    //   * *  *  *      The * values are actually -INF       * *  *  *

    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector(8);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, c));
}

Charlie Lin's avatar
Charlie Lin committed
4530
TEST_CASE(maxpool_rank3_test0)
4531
{
Shucai Xiao's avatar
Shucai Xiao committed
4532
    // 1D case 1, input is 3D
Charlie Lin's avatar
Charlie Lin committed
4533
4534
4535
4536
4537
4538
4539
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {1, 3, 4}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
    op.lengths = {2};
    op.padding = {0};
    op.stride  = {1};
4540

Charlie Lin's avatar
Charlie Lin committed
4541
4542
4543
    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};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4544
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4545
    auto result = p.eval({}).back();
4546

Charlie Lin's avatar
Charlie Lin committed
4547
4548
4549
4550
4551
4552
4553
4554
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.3, 0.4, 0.4, 0.8, 0.9, 0.9, 0.7, 0.7, 0.6};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(maxpool_rank3_test1)
{
Shucai Xiao's avatar
Shucai Xiao committed
4555
    // 1D case 2, input is 3D
Charlie Lin's avatar
Charlie Lin committed
4556
4557
4558
4559
4560
4561
4562
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {2, 2, 5}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
    op.lengths = {2};
    op.padding = {0};
    op.stride  = {2};
Shucai Xiao's avatar
Shucai Xiao committed
4563

Charlie Lin's avatar
Charlie Lin committed
4564
4565
4566
4567
4568
    std::vector<float> data{0.4975, -0.1226, -0.0405, -0.2861, -0.1227, -0.6186, -0.9618,
                            0.6022, -0.1912, 1.1925,  0.5493,  0.1692,  -0.8039, -1.0281,
                            0.9907, 0.477,   1.5001,  -1.1603, -1.361,  1.2556};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4569
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4570
4571
4572
4573
4574
4575
4576
    auto result = p.eval({}).back();

    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.4975, -0.0405, -0.6186, 0.6022, 0.5493, -0.8039, 1.5001, -1.1603};
    EXPECT(migraphx::verify_range(results_vector, gold));
}
4577

Charlie Lin's avatar
Charlie Lin committed
4578
4579
TEST_CASE(maxpool_rank3_ceil_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
4580
    // 1D case 2, input is 3D, ceil mode
Charlie Lin's avatar
Charlie Lin committed
4581
4582
4583
4584
4585
4586
4587
4588
4589
    migraphx::program p;
    auto* mm     = p.get_main_module();
    auto s       = migraphx::shape{migraphx::shape::float_type, {2, 2, 5}};
    auto op      = migraphx::op::pooling{migraphx::op::pooling_mode::max};
    op.lengths   = {2};
    op.padding   = {0};
    op.stride    = {2};
    op.ceil_mode = true;

4590
4591
4592
4593
4594
4595
    // clang-format off
    std::vector<float> data{0.4975, -0.1226, -0.0405, -0.2861, -0.1227, 
                        -0.6186, -0.9618, 0.6022, -0.1912, 1.1925,
                        0.5493,  0.1692,  -0.8039, -1.0281, 0.9907, 
                        0.477,   1.5001,  -1.1603, -1.361,  1.2556};
    // clang-format on
Charlie Lin's avatar
Charlie Lin committed
4596
4597
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4598
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4599
    auto result = p.eval({}).back();
4600

Charlie Lin's avatar
Charlie Lin committed
4601
4602
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
4603
4604
4605
4606
4607
4608

    // clang-format off
    std::vector<float> gold{0.4975, -0.0405, -0.1227, -0.6186,
                            0.6022, 1.1925, 0.5493, -0.8039,
                            0.9907, 1.5001, -1.1603, 1.2556};
    // clang-format on
Charlie Lin's avatar
Charlie Lin committed
4609
4610
    EXPECT(migraphx::verify_range(results_vector, gold));
}
4611

Charlie Lin's avatar
Charlie Lin committed
4612
4613
TEST_CASE(maxpool_rank5_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
4614
    // 3D, input is 5D
Charlie Lin's avatar
Charlie Lin committed
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto s     = migraphx::shape{migraphx::shape::float_type, {2, 2, 3, 3, 3}};
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
    op.lengths = {2, 2, 2};
    op.padding = {0, 0, 0};
    op.stride  = {2, 2, 2};

    std::vector<float> data{
        -2.8029, 0.5861,  0.7015,  0.1297,  -1.44,   -1.9472, 0.7812,  2.408,   -0.3145, 0.3405,
        -0.9146, 0.0624,  1.5064,  -0.8345, 1.7977,  1.8949,  1.0073,  -0.2102, -0.042,  -0.7146,
        0.6227,  -0.5263, -2.2598, 0.1713,  0.449,   0.5303,  -0.8622, -0.5691, 0.907,   -0.0569,
        -1.5348, -0.4109, -0.1461, -0.5445, 0.4266,  0.2282,  1.3655,  -2.1519, 0.6068,  -0.2001,
        -0.4702, 0.3864,  1.7083,  0.9096,  0.4286,  -1.8866, 0.7034,  0.0293,  1.4587,  0.7672,
        -2.8614, 0.8124,  -0.053,  1.0449,  0.845,   -0.0131, 0.1139,  -0.859,  -1.2681, -0.6337,
        -0.4644, 0.1938,  0.2889,  0.9035,  0.7118,  -0.5767, 0.4577,  -0.0549, 0.2237,  0.5756,
        0.0677,  -0.0223, -0.329,  0.2364,  2.7666,  -0.7417, -1.3196, -0.2655, 0.1698,  -0.1777,
        -0.9427, 2.6859,  -0.7501, 0.5175,  1.0029,  -2.6436, -0.4388, -1.2348, -0.1539, -0.6229,
        -0.4136, 0.5085,  0.4136,  -0.6439, -1.1953, -0.406,  -0.0195, 0.1869,  -0.8664, 1.1364,
        0.5041,  0.0647,  0.1941,  -1.0819, -0.4629, -0.5107, 0.3612,  -0.3583};
    auto l0 = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(op, l0);
4637
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1.5064, 1.3655, 0.9035, 2.6859};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(maxpool_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4649
    auto s   = migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
4650
4651
4652
4653
4654
4655
4656
    auto x   = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"lengths", {2}},
                                           {"padding", {0}},
                                           {"stride", {1}}}),
                        x);
4657
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667

    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};
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 3, 4}};
    migraphx::parameter_map params;
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0.3, 0.4, 0.4, 0.8, 0.9, 0.9, 0.7, 0.7, 0.6};
    EXPECT(migraphx::verify_range(results_vector, gold));
4668
4669
}

Shucai Xiao's avatar
Shucai Xiao committed
4670
TEST_CASE(min_test)
kahmed10's avatar
kahmed10 committed
4671
4672
{
    migraphx::program p;
4673
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
4674
4675
4676
4677
4678
4679
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {1, 4, 3}});
    auto l1       = mm->add_literal(migraphx::literal{s, {2, 8, 6}});
    auto l2       = mm->add_literal(migraphx::literal{s, {7, 5, 9}});
    auto curr_min = mm->add_instruction(migraphx::make_op("min"), l0, l1);
    mm->add_instruction(migraphx::make_op("min"), curr_min, l2);
4680
    p.compile(migraphx::make_target("ref"));
4681
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4682
    std::vector<float> results_vector(4);
kahmed10's avatar
kahmed10 committed
4683
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
4684
    std::vector<float> gold{1, 4, 3};
kahmed10's avatar
kahmed10 committed
4685
4686
4687
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4688
4689
4690
4691
TEST_CASE(min_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4692
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
4693
4694
4695
4696
4697
4698
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x        = mm->add_parameter("x", s);
    auto y        = mm->add_parameter("y", s);
    auto z        = mm->add_parameter("z", s);
    auto curr_min = mm->add_instruction(migraphx::make_op("min"), x, y);
    mm->add_instruction(migraphx::make_op("min"), curr_min, z);
4699
    p.compile(migraphx::make_target("ref"));
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715

    std::vector<float> x_data{1, 4, 3};
    std::vector<float> y_data{2, 8, 6};
    std::vector<float> z_data{7, 5, 9};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    params0["z"] = migraphx::argument(input_fixed_shape0, z_data.data());
    auto result  = p.eval(params0).back();
    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));
}

4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
TEST_CASE(fmod_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::int32_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {-7, 8, -3}});
    auto l1       = mm->add_literal(migraphx::literal{s, {2, 4, 6}});
    auto l2       = mm->add_literal(migraphx::literal{s, {7, 5, 9}});
    auto curr_mod = mm->add_instruction(migraphx::make_op("fmod"), l0, l1);
    mm->add_instruction(migraphx::make_op("fmod"), curr_mod, l2);
4726
    p.compile(migraphx::make_target("ref"));
4727
4728
4729
4730
4731
4732
4733
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{-1, 0, -3};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
4734
TEST_CASE(fmod_dyn_test)
4735
4736
4737
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4738
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
4739
4740
4741
4742
4743
4744
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x        = mm->add_parameter("x", s);
    auto y        = mm->add_parameter("y", s);
    auto z        = mm->add_parameter("z", s);
    auto curr_mod = mm->add_instruction(migraphx::make_op("fmod"), x, y);
    mm->add_instruction(migraphx::make_op("fmod"), curr_mod, z);
4745
    p.compile(migraphx::make_target("ref"));
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761

    std::vector<float> x_data{-7, 8, -3};
    std::vector<float> y_data{2, 4, 6};
    std::vector<float> z_data{7, 5, 9};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    params0["z"] = migraphx::argument(input_fixed_shape0, z_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{-1, 0, -3};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
TEST_CASE(fmod_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {-7.2f, 8.5f, -3.3f}});
    auto l1       = mm->add_literal(migraphx::literal{s, {2.0f, 4.0f, 6.0f}});
    auto l2       = mm->add_literal(migraphx::literal{s, {7.0f, 5.0f, 9.0f}});
    auto curr_mod = mm->add_instruction(migraphx::make_op("fmod"), l0, l1);
    mm->add_instruction(migraphx::make_op("fmod"), curr_mod, l2);

4773
    p.compile(migraphx::make_target("ref"));
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{-1.2f, 0.5f, -3.3f};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(mod_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::int32_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {-3, 8, -7}});
    auto l1       = mm->add_literal(migraphx::literal{s, {3, 3, 3}});
    auto l2       = mm->add_literal(migraphx::literal{s, {10, 2, 9}});
    auto curr_mod = mm->add_instruction(migraphx::make_op("mod"), l0, l1);
    mm->add_instruction(migraphx::make_op("mod"), curr_mod, l2);
4791
    p.compile(migraphx::make_target("ref"));
4792
4793
4794
4795
4796
4797
4798
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0, 0, 2};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4799
4800
4801
4802
TEST_CASE(mod_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4803
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
4804
4805
4806
4807
4808
4809
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x        = mm->add_parameter("x", s);
    auto y        = mm->add_parameter("y", s);
    auto z        = mm->add_parameter("z", s);
    auto curr_mod = mm->add_instruction(migraphx::make_op("mod"), x, y);
    mm->add_instruction(migraphx::make_op("mod"), curr_mod, z);
4810
    p.compile(migraphx::make_target("ref"));
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826

    std::vector<float> x_data{-3, 8, -7};
    std::vector<float> y_data{3, 3, 3};
    std::vector<float> z_data{10, 2, 9};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    params0["z"] = migraphx::argument(input_fixed_shape0, z_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0, 0, 2};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
TEST_CASE(mod_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l0       = mm->add_literal(migraphx::literal{s, {-3.0f, 8.5f, -7.0f}});
    auto l1       = mm->add_literal(migraphx::literal{s, {2.0f, 3.0f, 3.0f}});
    auto l2       = mm->add_literal(migraphx::literal{s, {3.0f, 3.0f, 4.0f}});
    auto curr_mod = mm->add_instruction(migraphx::make_op("mod"), l0, l1);
    mm->add_instruction(migraphx::make_op("mod"), curr_mod, l2);

4838
    p.compile(migraphx::make_target("ref"));
4839
4840
4841
4842
4843
4844
4845
    auto result = p.eval({}).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1.0f, 2.5f, 2.0f};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
4846
TEST_CASE(mul_test)
kahmed10's avatar
kahmed10 committed
4847
4848
{
    migraphx::program p;
4849
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
4850
    migraphx::shape s{migraphx::shape::float_type, {3}};
4851
4852
    std::vector<float> data1{-1, 0, 1};
    std::vector<float> data2{1, 2, 3};
Shucai Xiao's avatar
Shucai Xiao committed
4853
4854
4855
    auto l1 = mm->add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = mm->add_literal(migraphx::literal{s, {1, 2, 3}});
    mm->add_instruction(migraphx::make_op("mul"), l1, l2);
4856
    p.compile(migraphx::make_target("ref"));
kahmed10's avatar
kahmed10 committed
4857
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
4858
    std::vector<float> results_vector(3);
kahmed10's avatar
kahmed10 committed
4859
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
4860
4861
4862
4863
4864
    std::vector<float> gold(data1.size());
    std::transform(
        data1.begin(), data1.end(), data2.begin(), gold.begin(), [](float n1, float n2) -> float {
            return n1 * n2;
        });
kahmed10's avatar
kahmed10 committed
4865
4866
4867
    EXPECT(migraphx::verify_range(results_vector, gold));
}

4868
4869
4870
4871
TEST_CASE(mul_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4872
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
4873
4874
4875
4876
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x = mm->add_parameter("x", s);
    auto y = mm->add_parameter("y", s);
    mm->add_instruction(migraphx::make_op("mul"), x, y);
4877
    p.compile(migraphx::make_target("ref"));
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908

    std::vector<float> x_data{-1, 0, 1};
    std::vector<float> y_data{1, 2, 3};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold(x_data.size());
    std::transform(x_data.begin(),
                   x_data.end(),
                   y_data.begin(),
                   gold.begin(),
                   [](float n1, float n2) -> float { return n1 * n2; });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(multibroadcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::int32_type, {2, 2}};
    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    auto l1 = mm->add_literal(migraphx::literal{a_shape, a_data});
    auto l2 = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", l1->get_shape().lens()}}),
                        l2);
4909
    p.compile(migraphx::make_target("ref"));
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
    auto result = p.eval({}).back();
    auto output = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -3);
    EXPECT(output(1, 0) == -2);
    EXPECT(output(1, 1) == -3);
}

TEST_CASE(multibroadcast_2in_static_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::int32_type, {2, 2}};
    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    auto l1 = mm->add_literal(migraphx::literal{a_shape, a_data});
    auto l2 = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(migraphx::make_op("multibroadcast"), l2, l1);
4929
    p.compile(migraphx::make_target("ref"));
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
    auto result = p.eval({}).back();
    auto output = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -3);
    EXPECT(output(1, 0) == -2);
    EXPECT(output(1, 1) == -3);
}

TEST_CASE(multibroadcast_2in_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4942
    migraphx::shape a_shape{migraphx::shape::int32_type, {{2, 4}, {2, 2}}};
4943
4944
4945
4946
4947
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    std::vector<int32_t> b_data{-2, -3};
    auto l1 = mm->add_parameter("a", a_shape);
    auto l2 = mm->add_literal(migraphx::literal{b_shape, b_data});
    mm->add_instruction(migraphx::make_op("multibroadcast"), l2, l1);
4948
    p.compile(migraphx::make_target("ref"));
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961

    std::vector<int32_t> a_data{0, 0, 0, 0};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 2}};
    params0["a"] = migraphx::argument(input_fixed_shape0, a_data.data());
    auto result  = p.eval(params0).back();
    auto output  = result.get<int32_t>();
    EXPECT(output(0, 0) == -2);
    EXPECT(output(0, 1) == -3);
    EXPECT(output(1, 0) == -2);
    EXPECT(output(1, 1) == -3);
}

4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
TEST_CASE(multibroadcast_3in_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::int32_type, {{2, 4}, {2, 2}}};
    migraphx::shape b_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape c_shape{migraphx::shape::int32_type, {{1, 4, {2, 4}}, {2, 4}, {2, 2}}};
    auto l1 = mm->add_parameter("a", a_shape);
    std::vector<int32_t> b_data{-2, -3};
    auto l2 = mm->add_literal(migraphx::literal{b_shape, b_data});
    auto l3 = mm->add_parameter("c", c_shape);
    mm->add_instruction(migraphx::make_op("multibroadcast"), l2, l1, l3);
    p.compile(migraphx::make_target("ref"));

    std::vector<int32_t> a_data(4, 0);
    std::vector<int32_t> c_data(8, 0);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape_a{migraphx::shape::float_type, {2, 2}};
    migraphx::shape input_fixed_shape_c{migraphx::shape::float_type, {2, 2, 2}};
    params["a"] = migraphx::argument(input_fixed_shape_a, a_data.data());
    params["c"] = migraphx::argument(input_fixed_shape_c, c_data.data());
    auto result = p.eval(params).back();
    auto output = result.get<int32_t>();
    EXPECT(output(0, 0, 0) == -2);
    EXPECT(output(0, 0, 1) == -3);
    EXPECT(output(0, 1, 0) == -2);
    EXPECT(output(0, 1, 1) == -3);
    EXPECT(output(1, 0, 0) == -2);
    EXPECT(output(1, 0, 1) == -3);
    EXPECT(output(1, 1, 0) == -2);
    EXPECT(output(1, 1, 1) == -3);
}

turneram's avatar
turneram committed
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
TEST_CASE(multinomial_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    size_t sample_size = 100000;
    float seed         = 0.0f;
    std::mt19937 gen(seed);
    std::uniform_real_distribution<> dis(0.0, 1.0);
    std::vector<float> rand_samples(sample_size);
    std::generate(rand_samples.begin(), rand_samples.end(), [&]() { return dis(gen); });
    migraphx::shape rs{migraphx::shape::float_type, {1, sample_size}};
    auto rs_lit = mm->add_literal(migraphx::literal{rs, rand_samples});

    migraphx::shape s{migraphx::shape::float_type, {1, 5}};
    std::vector<int> dist{15, 25, 15, 25, 20};
    std::vector<float> data(5);
    std::transform(dist.begin(), dist.end(), data.begin(), [&](auto d) { return std::log(d); });
    auto input = mm->add_literal(migraphx::literal(s, data));

    auto maxes = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {1}}}), input);
    auto mb_maxes =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 5}}}), maxes);
    auto cdf = mm->add_instruction(migraphx::make_op("sub"), input, mb_maxes);
    cdf      = mm->add_instruction(migraphx::make_op("exp"), cdf);
    cdf      = mm->add_instruction(
        migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", false}}), cdf);

    mm->add_instruction(migraphx::make_op("multinomial"), cdf, rs_lit);
5024
    p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5025
5026
5027
5028
5029
    auto result = p.eval({}).back();
    std::vector<int32_t> result_vec(sample_size);
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });

    std::vector<int> res_dist(5, 0);
5030
    for(const auto& r : result_vec)
turneram's avatar
turneram committed
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
        res_dist[r]++;
    auto dist_sum     = std::accumulate(dist.begin(), dist.end(), 0);
    auto res_dist_sum = std::accumulate(res_dist.begin(), res_dist.end(), 0);
    std::vector<float> norm(5);
    std::vector<float> res_norm(5);
    std::transform(dist.begin(), dist.end(), norm.begin(), [&](auto n) {
        return static_cast<double>(n) / dist_sum;
    });
    std::transform(res_dist.begin(), res_dist.end(), res_norm.begin(), [&](auto n) {
        return static_cast<double>(n) / res_dist_sum;
    });
    EXPECT(migraphx::verify_range(norm, res_norm, 100000));
}

Shucai Xiao's avatar
Shucai Xiao committed
5045
TEST_CASE(neg_test)
kahmed10's avatar
kahmed10 committed
5046
5047
{
    migraphx::program p;
5048
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5049
5050
5051
5052
5053
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> data = {1.0f, 1.3f, -1.2f, 0.0f, -100.f, 200.f};
    auto input              = mm->add_literal(migraphx::literal(s, data));
    auto ret                = mm->add_instruction(migraphx::make_op("neg"), input);
    mm->add_return({ret});
5054
    p.compile(migraphx::make_target("ref"));
kahmed10's avatar
kahmed10 committed
5055
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
5056
5057
    std::vector<float> result_vector;
    result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });
5058
5059
    std::vector<float> gold = data;
    std::transform(gold.begin(), gold.end(), gold.begin(), std::negate<float>());
Shucai Xiao's avatar
Shucai Xiao committed
5060
    EXPECT(migraphx::verify_range(result_vector, gold));
kahmed10's avatar
kahmed10 committed
5061
5062
}

Charlie Lin's avatar
Charlie Lin committed
5063
TEST_CASE(neg_dyn_test)
5064
5065
5066
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5067
    migraphx::shape s{migraphx::shape::float_type, {{2, 4}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
5068
5069
    auto input = mm->add_parameter("X", s);
    auto ret   = mm->add_instruction(migraphx::make_op("neg"), input);
5070
    mm->add_return({ret});
5071
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5072
5073

    std::vector<float> a = {1.0f, 1.3f, -1.2f, 0.0f, -100.f, 200.f};
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, a.data());
    auto result  = p.eval(params0).back();
    std::vector<float> result_vector;
    result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = a;
    std::transform(gold.begin(), gold.end(), gold.begin(), std::negate<float>());
    EXPECT(migraphx::verify_range(result_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
5085
TEST_CASE(nms_dyn_out_test)
Charlie Lin's avatar
Charlie Lin committed
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape boxes_s{migraphx::shape::float_type, {1, 6, 4}};
    std::vector<float> boxes_vec = {0.5, 0.5,  1.0, 1.0, 0.5, 0.6,  1.0, 1.0, 0.5, 0.4,   1.0, 1.0,
                                    0.5, 10.5, 1.0, 1.0, 0.5, 10.6, 1.0, 1.0, 0.5, 100.5, 1.0, 1.0};

    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    auto boxes_l         = mm->add_literal(migraphx::literal(boxes_s, boxes_vec));
    auto scores_l        = mm->add_literal(migraphx::literal(scores_s, scores_vec));
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto r = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression",
                          {{"center_point_box", true}, {"use_dyn_output", true}}),
        boxes_l,
        scores_l,
        max_out_l,
        iou_threshold,
        score_threshold);
    mm->add_return({r});

5112
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5113
5114
5115
5116
5117
5118
5119
    auto output = p.eval({}).back();
    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5};
    EXPECT(migraphx::verify_range(result, gold));
}

Charlie Lin's avatar
Charlie Lin committed
5120
TEST_CASE(nms_dyn_batch_test)
Charlie Lin's avatar
Charlie Lin committed
5121
5122
5123
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5124
    migraphx::shape boxes_s{migraphx::shape::float_type, {{1, 3}, {6, 6}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5125

5126
    migraphx::shape scores_s{migraphx::shape::float_type, {{1, 3}, {1, 1}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143

    auto boxes_p         = mm->add_parameter("boxes", boxes_s);
    auto scores_p        = mm->add_parameter("scores", scores_s);
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto r = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression",
                          {{"center_point_box", true}, {"use_dyn_output", true}}),
        boxes_p,
        scores_p,
        max_out_l,
        iou_threshold,
        score_threshold);
    mm->add_return({r});

5144
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165

    std::vector<float> boxes_vec  = {0.5, 0.5,  1.0, 1.0, 0.5, 0.6,  1.0, 1.0, 0.5, 0.4,   1.0, 1.0,
                                    0.5, 10.5, 1.0, 1.0, 0.5, 10.6, 1.0, 1.0, 0.5, 100.5, 1.0, 1.0,
                                    0.5, 0.5,  1.0, 1.0, 0.5, 0.6,  1.0, 1.0, 0.5, 0.4,   1.0, 1.0,
                                    0.5, 10.5, 1.0, 1.0, 0.5, 10.6, 1.0, 1.0, 0.5, 100.5, 1.0, 1.0};
    std::vector<float> scores_vec = {
        0.9, 0.75, 0.6, 0.95, 0.5, 0.3, 0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 6, 4}};
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {2, 1, 6}};
    migraphx::parameter_map params0;
    params0["boxes"]  = migraphx::argument(input_fixed_shape0, boxes_vec.data());
    params0["scores"] = migraphx::argument(input_fixed_shape1, scores_vec.data());
    auto output       = p.eval(params0).back();

    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5, 1, 0, 3, 1, 0, 0, 1, 0, 5};
    EXPECT(migraphx::verify_range(result, gold));
}

Charlie Lin's avatar
Charlie Lin committed
5166
TEST_CASE(nms_dyn_boxes_test)
Charlie Lin's avatar
Charlie Lin committed
5167
5168
5169
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5170
    migraphx::shape boxes_s{migraphx::shape::float_type, {{1, 1}, {4, 20}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5171

5172
    migraphx::shape scores_s{migraphx::shape::float_type, {{1, 1}, {1, 1}, {4, 20}}};
Charlie Lin's avatar
Charlie Lin committed
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189

    auto boxes_p         = mm->add_parameter("boxes", boxes_s);
    auto scores_p        = mm->add_parameter("scores", scores_s);
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto r = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression",
                          {{"center_point_box", true}, {"use_dyn_output", true}}),
        boxes_p,
        scores_p,
        max_out_l,
        iou_threshold,
        score_threshold);
    mm->add_return({r});

5190
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208

    std::vector<float> boxes_vec  = {0.5, 0.5,  1.0, 1.0, 0.5, 0.6,  1.0, 1.0, 0.5, 0.4,   1.0, 1.0,
                                    0.5, 10.5, 1.0, 1.0, 0.5, 10.6, 1.0, 1.0, 0.5, 100.5, 1.0, 1.0};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {1, 6, 4}};
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {1, 1, 6}};
    migraphx::parameter_map params0;
    params0["boxes"]  = migraphx::argument(input_fixed_shape0, boxes_vec.data());
    params0["scores"] = migraphx::argument(input_fixed_shape1, scores_vec.data());
    auto output       = p.eval(params0).back();

    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5};
    EXPECT(migraphx::verify_range(result, gold));
}

Charlie Lin's avatar
Charlie Lin committed
5209
TEST_CASE(nms_dyn_classes_test)
Charlie Lin's avatar
Charlie Lin committed
5210
5211
5212
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5213
    migraphx::shape boxes_s{migraphx::shape::float_type, {{1, 1}, {6, 6}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5214

5215
    migraphx::shape scores_s{migraphx::shape::float_type, {{1, 1}, {1, 3}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232

    auto boxes_p         = mm->add_parameter("boxes", boxes_s);
    auto scores_p        = mm->add_parameter("scores", scores_s);
    auto max_out_l       = mm->add_literal(int64_t{2});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto r = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression",
                          {{"center_point_box", true}, {"use_dyn_output", true}}),
        boxes_p,
        scores_p,
        max_out_l,
        iou_threshold,
        score_threshold);
    mm->add_return({r});

5233
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252

    std::vector<float> boxes_vec  = {0.0, 0.0,  1.0, 1.0,  0.0, 0.1,   1.0, 1.1,
                                    0.0, -0.1, 1.0, 0.9,  0.0, 10.0,  1.0, 11.0,
                                    0.0, 10.1, 1.0, 11.1, 0.0, 100.0, 1.0, 101.0};
    std::vector<float> scores_vec = {
        0.9, 0.75, 0.6, 0.95, 0.5, 0.3, 0.9, 0.75, 0.6, 0.95, 0.5, 0.3};
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {1, 6, 4}};
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {1, 2, 6}};
    migraphx::parameter_map params0;
    params0["boxes"]  = migraphx::argument(input_fixed_shape0, boxes_vec.data());
    params0["scores"] = migraphx::argument(input_fixed_shape1, scores_vec.data());
    auto output       = p.eval(params0).back();

    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 1, 3, 0, 1, 0};
    EXPECT(migraphx::verify_range(result, gold));
}

5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
TEST_CASE(nms_not_center_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape boxes_s{migraphx::shape::float_type, {1, 6, 4}};
    std::vector<float> boxes_vec = {1.0, 1.0,  0.0, 0.0,  0.0, 0.1,   1.0, 1.1,
                                    0.0, 0.9,  1.0, -0.1, 0.0, 10.0,  1.0, 11.0,
                                    1.0, 10.1, 0.0, 11.1, 1.0, 101.0, 0.0, 100.0};

    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    auto boxes_l         = mm->add_literal(migraphx::literal(boxes_s, boxes_vec));
    auto scores_l        = mm->add_literal(migraphx::literal(scores_s, scores_vec));
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

Charlie Lin's avatar
Charlie Lin committed
5271
5272
5273
5274
5275
5276
5277
5278
    // set use_dyn_output back to false in operator map
    auto r =
        mm->add_instruction(migraphx::make_op("nonmaxsuppression", {{"use_dyn_output", false}}),
                            boxes_l,
                            scores_l,
                            max_out_l,
                            iou_threshold,
                            score_threshold);
5279
5280
    mm->add_return({r});

5281
    p.compile(migraphx::make_target("ref"));
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
    auto output = p.eval({}).back();
    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(result, gold));
}

TEST_CASE(nms_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape boxes_s{migraphx::shape::float_type, {1, 6, 4}};
    std::vector<float> boxes_vec = {0.5, 0.5,  1.0, 1.0, 0.5, 0.6,  1.0, 1.0, 0.5, 0.4,   1.0, 1.0,
                                    0.5, 10.5, 1.0, 1.0, 0.5, 10.6, 1.0, 1.0, 0.5, 100.5, 1.0, 1.0};

    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    auto boxes_l         = mm->add_literal(migraphx::literal(boxes_s, boxes_vec));
    auto scores_l        = mm->add_literal(migraphx::literal(scores_s, scores_vec));
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

Charlie Lin's avatar
Charlie Lin committed
5306
5307
5308
5309
5310
5311
5312
    auto r =
        mm->add_instruction(migraphx::make_op("nonmaxsuppression", {{"center_point_box", true}}),
                            boxes_l,
                            scores_l,
                            max_out_l,
                            iou_threshold,
                            score_threshold);
5313
5314
    mm->add_return({r});

5315
    p.compile(migraphx::make_target("ref"));
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
    auto output = p.eval({}).back();
    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(result, gold));
}

TEST_CASE(nms_transpose1_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape boxes_s{migraphx::shape::float_type, {1, 4, 6}};
    std::vector<float> boxes_vec = {
        0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.6, 0.4, 10.5, 10.6, 100.5,
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,  1.0,  1.0,
    };

    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    auto t_boxes_l       = mm->add_literal(migraphx::literal(boxes_s, boxes_vec));
    auto scores_l        = mm->add_literal(migraphx::literal(scores_s, scores_vec));
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto transpose_boxes = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), t_boxes_l);
Charlie Lin's avatar
Charlie Lin committed
5344
5345
5346
5347
5348
5349
5350
    auto r =
        mm->add_instruction(migraphx::make_op("nonmaxsuppression", {{"center_point_box", true}}),
                            transpose_boxes,
                            scores_l,
                            max_out_l,
                            iou_threshold,
                            score_threshold);
5351
5352
    mm->add_return({r});

5353
    p.compile(migraphx::make_target("ref"));
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
    auto output = p.eval({}).back();
    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(result, gold));
}

TEST_CASE(nms_transpose2_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape boxes_s{migraphx::shape::float_type, {4, 1, 6}};
    std::vector<float> boxes_vec = {
        0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.6, 0.4, 10.5, 10.6, 100.5,
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,  1.0,  1.0,
    };

    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    std::vector<float> scores_vec = {0.9, 0.75, 0.6, 0.95, 0.5, 0.3};

    auto t_boxes_l       = mm->add_literal(migraphx::literal(boxes_s, boxes_vec));
    auto scores_l        = mm->add_literal(migraphx::literal(scores_s, scores_vec));
    auto max_out_l       = mm->add_literal(int64_t{4});
    auto iou_threshold   = mm->add_literal(0.5f);
    auto score_threshold = mm->add_literal(0.0f);

    auto transpose_boxes = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {1, 2, 0}}}), t_boxes_l);
Charlie Lin's avatar
Charlie Lin committed
5382
5383
5384
5385
5386
5387
5388
    auto r =
        mm->add_instruction(migraphx::make_op("nonmaxsuppression", {{"center_point_box", true}}),
                            transpose_boxes,
                            scores_l,
                            max_out_l,
                            iou_threshold,
                            score_threshold);
5389
5390
    mm->add_return({r});

5391
    p.compile(migraphx::make_target("ref"));
5392
5393
5394
5395
5396
5397
5398
    auto output = p.eval({}).back();
    std::vector<int64_t> result;
    output.visit([&](auto out) { result.assign(out.begin(), out.end()); });
    std::vector<int64_t> gold = {0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(result, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
TEST_CASE(nonzero_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}};
    std::vector<float> data = {
        1.0f, 1.3f, 0.0f, -1.2f, 0.0f, -100.f, 200.f, 0.0f, 0.1f, 0.2f, 0.0f, 0.5f};
    auto input = mm->add_literal(migraphx::literal(s, data));
    auto ret   = mm->add_instruction(migraphx::make_op("nonzero"), input);
    mm->add_return({ret});
5409
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
5410
5411
5412
5413
5414
5415
5416
5417
    auto result = p.eval({}).back();
    std::vector<int64_t> result_vector;
    result.visit([&](auto output) { result_vector.assign(output.begin(), output.end()); });
    std::vector<int64_t> gold = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
                                 1, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(result_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5418
TEST_CASE(not_test)
5419
{
Shucai Xiao's avatar
Shucai Xiao committed
5420
    // int32
5421
    {
Paul's avatar
Paul committed
5422
        migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5423
5424
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::int32_type, {4}};
5425
5426
        std::vector<float> data{0, 8, 1, -32};
        auto l1 = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
5427
        mm->add_instruction(migraphx::make_op("not"), l1);
5428
        p.compile(migraphx::make_target("ref"));
5429
        auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
5430
5431
        std::vector<char> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
5432
        std::vector<char> gold{1, 0, 0, 0};
Shucai Xiao's avatar
Shucai Xiao committed
5433
        EXPECT(migraphx::verify_range(results_vector, gold));
5434
    }
Shucai Xiao's avatar
Shucai Xiao committed
5435
5436

    // bool
5437
    {
Shucai Xiao's avatar
Shucai Xiao committed
5438
5439
5440
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::bool_type, {4}};
5441
        std::vector<bool> data{false, false, true, true};
Shucai Xiao's avatar
Shucai Xiao committed
5442
5443
        auto l1 = mm->add_literal(migraphx::literal{s, {0, 0, 1, 1}});
        mm->add_instruction(migraphx::make_op("not"), l1);
5444
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
5445
5446
5447
        auto result = p.eval({}).back();
        std::vector<char> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
5448
        std::vector<bool> gold(data.size());
5449
5450
        std::transform(
            data.begin(), data.end(), gold.begin(), [](bool n) -> bool { return not n; });
Paul's avatar
Paul committed
5451
        EXPECT(migraphx::verify_range(results_vector, gold));
5452
    }
5453
5454
}

Charlie Lin's avatar
Charlie Lin committed
5455
TEST_CASE(not_dyn_test)
5456
5457
5458
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5459
    migraphx::shape::dynamic_dimension dd{3, 8};
5460
5461
5462
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("not"), input);
5463
    p.compile(migraphx::make_target("ref"));
5464

Charlie Lin's avatar
Charlie Lin committed
5465
    std::vector<float> input_data{0, 8, 1, -32};
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<char> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<char> gold{1, 0, 0, 0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5476
TEST_CASE(pad_test)
Khalique's avatar
Khalique committed
5477
5478
{
    migraphx::program p;
5479
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
5480
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
5481
5482
    auto l0 = mm->add_literal(migraphx::literal{s, {1, 2, 3, 4}});
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), l0);
5483
    p.compile(migraphx::make_target("ref"));
5484
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
5485
    std::vector<float> results_vector(16);
Khalique's avatar
Khalique committed
5486
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
5487
    std::vector<float> gold{0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0};
Khalique's avatar
Khalique committed
5488
5489
5490
    EXPECT(migraphx::verify_range(results_vector, gold));
}

kahmed10's avatar
kahmed10 committed
5491
5492
5493
5494
5495
5496
5497
TEST_CASE(pad_test_asym)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l0 = mm->add_literal(migraphx::literal{s, {1, 2, 3, 4}});
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 0, 1, 1}}}), l0);
5498
    p.compile(migraphx::make_target("ref"));
kahmed10's avatar
kahmed10 committed
5499
5500
5501
5502
5503
5504
5505
    auto result = p.eval({}).back();
    std::vector<float> results_vector(9);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1, 2, 0, 3, 4, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5506
TEST_CASE(pad_test_highest_half)
5507
5508
{
    migraphx::program p;
5509
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5510
5511
5512
5513
5514
5515
    migraphx::shape s{migraphx::shape::half_type, {2, 2}};
    auto l0 = mm->add_literal(migraphx::literal{s, {1, 2, 3, 4}});
    mm->add_instruction(
        migraphx::make_op("pad",
                          {{"pads", {1, 1, 1, 1}}, {"value", std::numeric_limits<float>::max()}}),
        l0);
5516
    p.compile(migraphx::make_target("ref"));
5517
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
5518
    std::vector<float> results_vector(16);
5519
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
5520
5521
    const float x = std::numeric_limits<migraphx::half>::max();
    std::vector<float> gold{x, x, x, x, x, 1, 2, x, x, 3, 4, x, x, x, x, x};
5522
5523
5524
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5525
TEST_CASE(pad_test_lowest_half)
5526
5527
{
    migraphx::program p;
5528
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5529
5530
5531
5532
5533
5534
    migraphx::shape s{migraphx::shape::half_type, {2, 2}};
    auto l0 = mm->add_literal(migraphx::literal{s, {1, 2, 3, 4}});
    mm->add_instruction(
        migraphx::make_op(
            "pad", {{"pads", {1, 1, 1, 1}}, {"value", std::numeric_limits<float>::lowest()}}),
        l0);
5535
    p.compile(migraphx::make_target("ref"));
5536
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
5537
    std::vector<float> results_vector(16);
5538
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
5539
5540
    const float x = std::numeric_limits<migraphx::half>::lowest();
    std::vector<float> gold{x, x, x, x, x, 1, 2, x, x, 3, 4, x, x, x, x, x};
5541
5542
5543
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
5544
5545
5546
5547
TEST_CASE(pad_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5548
    migraphx::shape s{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4, {2}}}};
Charlie Lin's avatar
Charlie Lin committed
5549
5550
    auto x = mm->add_parameter("x", s);
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), x);
5551
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563

    std::vector<float> data = {1, 2, 3, 4};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 2}};
    params["x"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector(16);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
TEST_CASE(pointwise_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1  = mm->add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2  = mm->add_literal(migraphx::literal{s, {1, 2, 3}});
    auto* pm = p.create_module("pointwise");
    auto x1  = pm->add_parameter("x1", {migraphx::shape::float_type});
    auto x2  = pm->add_parameter("x2", {migraphx::shape::float_type});
    pm->add_instruction(migraphx::make_op("add"), x1, x2);
    mm->add_instruction(migraphx::make_op("pointwise"), {l1, l2}, {pm});
5576
    p.compile(migraphx::make_target("ref"));
5577
5578
5579
5580
5581
5582
5583
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0, 2, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
5584
TEST_CASE(pow_test)
Khalique's avatar
Khalique committed
5585
5586
{
    migraphx::program p;
5587
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5588
    migraphx::shape s{migraphx::shape::float_type, {3}};
5589
5590
5591
    std::vector<float> data = {1, 2, 3};
    auto b                  = mm->add_literal(migraphx::literal{s, data});
    auto e                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
5592
    mm->add_instruction(migraphx::make_op("pow"), b, e);
5593
    p.compile(migraphx::make_target("ref"));
5594
    auto result = p.eval({}).back();
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return std::pow(n, n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(pow_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto b = mm->add_parameter("b", s);
    auto e = mm->add_parameter("e", s);
    mm->add_instruction(migraphx::make_op("pow"), b, e);
5611
    p.compile(migraphx::make_target("ref"));
5612
5613
5614
5615
5616
5617
5618
5619

    std::vector<float> data = {1, 2, 3};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["b"] = migraphx::argument(input_fixed_shape0, data.data());
    params0["e"] = migraphx::argument(input_fixed_shape0, data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
Khalique's avatar
Khalique committed
5620
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
5621
5622
5623
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return std::pow(n, n); });
Khalique's avatar
Khalique committed
5624
5625
5626
    EXPECT(migraphx::verify_range(results_vector, gold));
}

turneram's avatar
turneram committed
5627
5628
5629
5630
5631
5632
5633
5634
5635
TEST_CASE(prefix_scan_sum_1d)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {6}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", false}}),
                        l0);
5636
    p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1.0, 3.0, 6.0, 10.0, 15.0, 21.0};
    EXPECT(results_vector == gold);
}

TEST_CASE(prefix_scan_sum_2d)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", false}}), l0);
5654
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 3.0, 6.0, 9.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", false}}), l0);
5670
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0, 3.0, 6.0, 1.0, 3.0, 6.0, 1.0, 3.0, 6.0};
        EXPECT(results_vector == gold);
    }
}

TEST_CASE(prefix_scan_sum_3d)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", false}}), l0);
5689
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                2.0,
                                3.0,
                                1.0,
                                2.0,
                                3.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                2.0,
                                4.0,
                                6.0,
                                2.0,
                                4.0,
                                6.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", false}}), l0);
5722
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                3.0,
                                6.0,
                                9.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                3.0,
                                6.0,
                                9.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 2}, {"exclusive", false}}), l0);
5755
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0};
        EXPECT(results_vector == gold);
    }
}

TEST_CASE(prefix_scan_sum_exclusive)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {8}};
        auto input = migraphx::literal{s, {1, 2, 3, 4, 1, 2, 3, 4}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", true}}), l0);
5791
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{0.0, 1.0, 3.0, 6.0, 10.0, 11.0, 13.0, 16.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", true}}), l0);
5807
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{0.0,
                                0.0,
                                0.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                0.0,
                                0.0,
                                0.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0};
        EXPECT(results_vector == gold);
    }
}

TEST_CASE(prefix_scan_sum_exclusive_reverse)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {6}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(
        migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", true}, {"reverse", true}}),
        l0);
5843
    p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{20.0, 18.0, 15.0, 11.0, 6.0, 0.0};
    EXPECT(results_vector == gold);
}

TEST_CASE(prefix_scan_sum_negative_axis)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", -3}, {"exclusive", false}}), l0);
5861
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                2.0,
                                3.0,
                                1.0,
                                2.0,
                                3.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                2.0,
                                4.0,
                                6.0,
                                2.0,
                                4.0,
                                6.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", -2}, {"exclusive", false}}), l0);
5894
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                3.0,
                                6.0,
                                9.0,
                                1.0,
                                2.0,
                                3.0,
                                2.0,
                                4.0,
                                6.0,
                                3.0,
                                6.0,
                                9.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 3, 3}};
        auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum", {{"axis", -1}, {"exclusive", false}}), l0);
5927
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0,
                                1.0,
                                3.0,
                                6.0};
        EXPECT(results_vector == gold);
    }
}

TEST_CASE(prefix_scan_sum_reverse)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {8}};
        auto input = migraphx::literal{s, {1, 2, 3, 4, 1, 2, 3, 4}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum",
                              {{"axis", 0}, {"exclusive", false}, {"reverse", true}}),
            l0);
5965
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{20.0, 19.0, 17.0, 14.0, 10.0, 9.0, 7.0, 4.0};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {2, 2, 2}};
        auto input = migraphx::literal{s, {1, 2, 3, 4, 1, 2, 3, 4}};
        auto l0    = mm->add_literal(input);
        mm->add_instruction(
            migraphx::make_op("prefix_scan_sum",
                              {{"axis", 0}, {"exclusive", false}, {"reverse", true}}),
            l0);
5983
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
5984
5985
5986
5987
5988
5989
5990
5991
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{2.0, 4.0, 6.0, 8.0, 1.0, 2.0, 3.0, 4.0};
        EXPECT(results_vector == gold);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
5992
TEST_CASE(prelu_test)
Khalique's avatar
Khalique committed
5993
5994
{
    migraphx::program p;
5995
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5996
5997
5998
5999
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto x     = mm->add_literal(migraphx::literal{s, {-1, 0, 2}});
    auto slope = mm->add_literal(migraphx::literal{s, {2, 1, 2}});
    mm->add_instruction(migraphx::make_op("prelu"), x, slope);
6000
    p.compile(migraphx::make_target("ref"));
6001
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6002
    std::vector<float> results_vector;
Khalique's avatar
Khalique committed
6003
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6004
    std::vector<float> gold = {-2.0f, 0.0f, 2.0f};
Khalique's avatar
Khalique committed
6005
6006
6007
    EXPECT(migraphx::verify_range(results_vector, gold));
}

6008
6009
6010
6011
TEST_CASE(prelu_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6012
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
6013
6014
6015
6016
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x     = mm->add_parameter("x", s);
    auto slope = mm->add_parameter("slope", s);
    mm->add_instruction(migraphx::make_op("prelu"), x, slope);
6017
    p.compile(migraphx::make_target("ref"));
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031

    std::vector<float> x_data{-1, 0, 2};
    std::vector<float> slope_data{2, 1, 2};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"]     = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["slope"] = migraphx::argument(input_fixed_shape0, slope_data.data());
    auto result      = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-2.0f, 0.0f, 2.0f};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
6032
TEST_CASE(quant_conv2d_padding_stride_test)
Khalique's avatar
Khalique committed
6033
6034
{
    migraphx::program p;
6035
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
    migraphx::shape a_shape{migraphx::shape::int8_type, {2, 3, 4, 4}};
    std::vector<int8_t> a(2 * 3 * 4 * 4);
    std::iota(a.begin(), a.end(), 0);
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
    migraphx::shape c_shape{migraphx::shape::int8_type, {2, 3, 3, 3}};
    std::vector<int8_t> c(2 * 3 * 3 * 3);
    std::iota(c.begin(), c.end(), 0);
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});
    mm->add_instruction(
        migraphx::make_op("quant_convolution", {{"padding", {1, 1}}, {"stride", {2, 2}}}), al, cl);
6046
    p.compile(migraphx::make_target("ref"));
6047
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065

    std::vector<int32_t> s = {4521,
                              7014,
                              7830,
                              11952,
                              10515,
                              16734,
                              19737,
                              30906,
                              13161,
                              19542,
                              19494,
                              28800,
                              34707,
                              52590,
                              54729,
                              82746};
    std::vector<int32_t> results_vector;
Khalique's avatar
Khalique committed
6066
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6067
    EXPECT(migraphx::verify_range(results_vector, s));
Khalique's avatar
Khalique committed
6068
6069
}

Shucai Xiao's avatar
Shucai Xiao committed
6070
TEST_CASE(quant_conv2d_padding_test)
Khalique's avatar
Khalique committed
6071
6072
{
    migraphx::program p;
6073
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
    migraphx::shape a_shape{migraphx::shape::int8_type, {2, 3, 4, 4}};
    std::vector<int8_t> a(2 * 3 * 4 * 4);
    std::iota(a.begin(), a.end(), 0);
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
    migraphx::shape c_shape{migraphx::shape::int8_type, {2, 3, 3, 3}};
    std::vector<int8_t> c(2 * 3 * 3 * 3);
    std::iota(c.begin(), c.end(), 0);
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});
    mm->add_instruction(
        migraphx::make_op("quant_convolution", {{"padding", {1, 1}}, {"stride", {1, 1}}}), al, cl);
6084
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6085
6086
6087
6088
6089
6090
6091
6092
6093
    auto result            = p.eval({}).back();
    std::vector<int32_t> s = {
        4521,  6753,  7014,  4635,  6858,  10197, 10548, 6939,  7830,  11601, 11952, 7839,  5007,
        7383,  7590,  4953,  10515, 15987, 16734, 11277, 16821, 25506, 26586, 17874, 19737, 29826,
        30906, 20718, 13593, 20505, 21198, 14187, 13161, 19281, 19542, 12699, 18522, 27045, 27396,
        17739, 19494, 28449, 28800, 18639, 11919, 17319, 17526, 11289, 34707, 51843, 52590, 34893,
        51813, 77346, 78426, 52002, 54729, 81666, 82746, 54846, 36057, 53769, 54462, 36075};

    std::vector<int32_t> results_vector;
Khalique's avatar
Khalique committed
6094
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6095
    EXPECT(migraphx::verify_range(results_vector, s));
Khalique's avatar
Khalique committed
6096
6097
}

Shucai Xiao's avatar
Shucai Xiao committed
6098
TEST_CASE(quant_conv2d_test)
6099
6100
{
    migraphx::program p;
6101
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
    migraphx::shape a_shape{migraphx::shape::int8_type, {2, 3, 4, 4}};
    std::vector<int8_t> a(2 * 3 * 4 * 4);
    std::iota(a.begin(), a.end(), 0);
    auto al = mm->add_literal(migraphx::literal{a_shape, a});

    migraphx::shape c_shape{migraphx::shape::int8_type, {2, 3, 3, 3}};
    std::vector<int8_t> c(2 * 3 * 3 * 3);
    std::iota(c.begin(), c.end(), 0);
    auto cl = mm->add_literal(migraphx::literal{c_shape, c});

    mm->add_instruction(migraphx::make_op("quant_convolution"), al, cl);
6113
    p.compile(migraphx::make_target("ref"));
6114
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133

    std::vector<int32_t> s = {10197,
                              10548,
                              11601,
                              11952,
                              25506,
                              26586,
                              29826,
                              30906,
                              27045,
                              27396,
                              28449,
                              28800,
                              77346,
                              78426,
                              81666,
                              82746};

    std::vector<int32_t> results_vector;
6134
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6135
    EXPECT(migraphx::verify_range(results_vector, s));
6136
6137
}

turneram's avatar
turneram committed
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
TEST_CASE(quantizelinear)
{
    {
        migraphx::shape xs{migraphx::shape::float_type, {2, 3, 3}};
        std::vector<float> xv = {
            -300, 600, 129, -1000, 4, 3, -6, 600, 550, -300, 600, 129, -1000, 4, 3, -6, 600, 550};
        migraphx::shape ss{migraphx::shape::float_type, {2, 3, 3}};
        std::vector<float> sv = {2, 2, 2, 4, 4, 4, 6, 6, 6, 2, 2, 2, 4, 4, 4, 6, 6, 6};
        migraphx::shape zs{migraphx::shape::int8_type, {2, 3, 3}};
        std::vector<uint8_t> zv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        auto create_program     = [&]() {
            migraphx::program p;
            auto* mm = p.get_main_module();
            auto x   = mm->add_literal(xs, xv);
            auto s   = mm->add_literal(ss, sv);
            auto z   = mm->add_literal(zs, zv);
            mm->add_instruction(migraphx::make_op("quantizelinear"), x, s, z);
            return p;
        };

        migraphx::program p1 = create_program();
6159
        p1.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
        auto result = p1.eval({}).back();
        std::vector<float> results_vector(18);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{
            -128, 127, 65, -128, 1, 1, -1, 100, 92, -128, 127, 65, -128, 1, 1, -1, 100, 92};
        EXPECT(results_vector == gold);
    }

    {
        migraphx::shape xs{migraphx::shape::float_type, {2, 3, 3}};
        std::vector<float> xv = {
            -300, 600, 129, -1000, 4, 3, -6, 600, 550, -300, 600, 129, -1000, 4, 3, -6, 600, 550};
        migraphx::shape ss{migraphx::shape::float_type, {2, 3, 3}};
        std::vector<float> sv = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
        auto create_program   = [&]() {
            migraphx::program p;
            auto* mm = p.get_main_module();
            auto x   = mm->add_literal(xs, xv);
            auto s   = mm->add_literal(ss, sv);
            mm->add_instruction(migraphx::make_op("quantizelinear"), x, s);
            return p;
        };

        migraphx::program p1 = create_program();
6184
        p1.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
6185
6186
6187
6188
6189
6190
6191
6192
        auto result = p1.eval({}).back();
        std::vector<float> results_vector(18);
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{0, 255, 65, 0, 2, 2, 0, 255, 255, 0, 255, 65, 0, 2, 2, 0, 255, 255};
        EXPECT(results_vector == gold);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
6193
TEST_CASE(recip_test)
6194
6195
{
    migraphx::program p;
6196
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6197
6198
6199
6200
    migraphx::shape s{migraphx::shape::double_type, {3}};
    std::vector<float> data{-0.5f, 0.1f, 0.5f};
    auto l = mm->add_literal(migraphx::literal{s, data});
    mm->add_instruction(migraphx::make_op("recip"), l);
6201
    p.compile(migraphx::make_target("ref"));
6202
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6203
    std::vector<float> results_vector(3);
6204
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6205
    std::vector<float> gold = {-2.0f, 10.0f, 2.0f};
6206
6207
6208
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
6209
TEST_CASE(recip_dyn_test)
6210
6211
6212
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6213
    migraphx::shape::dynamic_dimension dd{3, 8};
6214
6215
6216
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("recip"), input);
6217
    p.compile(migraphx::make_target("ref"));
6218

Charlie Lin's avatar
Charlie Lin committed
6219
    std::vector<float> input_data{-0.5f, 0.1f, 0.5f};
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-2.0f, 10.0f, 2.0f};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
6230
TEST_CASE(reduce_max_axis0)
6231
6232
{
    migraphx::program p;
6233
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6234
6235
6236
6237
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {0}}}), l0);
6238
    p.compile(migraphx::make_target("ref"));
6239
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6240
    std::vector<float> results_vector;
6241
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6242
6243
    std::vector<float> gold{9, 10, 11, 12};
    EXPECT(results_vector == gold);
6244
6245
}

Brian Pickrell's avatar
Brian Pickrell committed
6246
6247
6248
6249
TEST_CASE(reduce_max_dynamic_axis0)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6250
    migraphx::shape s{migraphx::shape::float_type, {{2, 4, {2}}, {3, 5, {3}}}};
Brian Pickrell's avatar
Brian Pickrell committed
6251
6252
6253
    auto input         = mm->add_parameter("X", s);
    auto reduce_max_op = migraphx::make_op("reduce_max", {{"axes", {0}}});
    mm->add_instruction(reduce_max_op, input);
6254
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 5}};
    std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    params["X"] = migraphx::argument(input_fixed_shape, input_data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {6, 7, 8, 9, 10};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
6267
TEST_CASE(reduce_max_axis01)
6268
6269
{
    migraphx::program p;
6270
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6271
6272
6273
6274
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {0, 1}}}), l0);
6275
    p.compile(migraphx::make_target("ref"));
6276
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6277
    std::vector<float> results_vector;
6278
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6279
6280
    std::vector<float> gold{11, 12};
    EXPECT(results_vector == gold);
6281
6282
}

Shucai Xiao's avatar
Shucai Xiao committed
6283
TEST_CASE(reduce_max_axis02)
Khalique's avatar
Khalique committed
6284
6285
{
    migraphx::program p;
6286
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6287
6288
6289
6290
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {0, 2}}}), l0);
6291
    p.compile(migraphx::make_target("ref"));
6292
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6293
    std::vector<float> results_vector;
Khalique's avatar
Khalique committed
6294
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6295
6296
    std::vector<float> gold{10, 12};
    EXPECT(results_vector == gold);
Khalique's avatar
Khalique committed
6297
6298
}

Shucai Xiao's avatar
Shucai Xiao committed
6299
TEST_CASE(reduce_mean_axis02)
Shucai Xiao's avatar
Shucai Xiao committed
6300
6301
{
    migraphx::program p;
6302
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6303
6304
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6305
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6306
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 2}}}), l0);
6307
    p.compile(migraphx::make_target("ref"));
6308
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6309
6310
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6311
    std::vector<float> gold{5.5, 7.5};
Shucai Xiao's avatar
Shucai Xiao committed
6312
6313
6314
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6315
TEST_CASE(reduce_mean_axis1)
Paul's avatar
Paul committed
6316
6317
{
    migraphx::program p;
6318
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
6319
6320
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6321
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6322
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {1}}}), l0);
6323
    p.compile(migraphx::make_target("ref"));
6324
    auto result = p.eval({}).back();
Paul's avatar
Paul committed
6325
6326
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6327
    std::vector<float> gold{2, 3, 6, 7, 10, 11};
Paul's avatar
Paul committed
6328
6329
6330
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6331
TEST_CASE(reduce_mean_axis12)
Paul's avatar
Paul committed
6332
6333
{
    migraphx::program p;
6334
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
6335
6336
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6337
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6338
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {1, 2}}}), l0);
6339
    p.compile(migraphx::make_target("ref"));
6340
    auto result = p.eval({}).back();
Paul's avatar
Paul committed
6341
6342
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6343
    std::vector<float> gold{2.5f, 6.5f, 10.5f};
Paul's avatar
Paul committed
6344
6345
6346
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6347
TEST_CASE(reduce_mean_axis2)
Paul's avatar
Paul committed
6348
6349
{
    migraphx::program p;
6350
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
6351
6352
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6353
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6354
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2}}}), l0);
6355
    p.compile(migraphx::make_target("ref"));
6356
    auto result = p.eval({}).back();
Paul's avatar
Paul committed
6357
6358
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6359
    std::vector<float> gold{1.5f, 3.5f, 5.5f, 7.5f, 9.5f, 11.5f};
Paul's avatar
Paul committed
6360
6361
6362
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6363
TEST_CASE(reduce_mean_int)
Paul's avatar
Paul committed
6364
6365
{
    migraphx::program p;
6366
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6367
    migraphx::shape s{migraphx::shape::int32_type, {3, 2, 2}};
Paul's avatar
Paul committed
6368
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6369
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6370
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {1, 2}}}), l0);
6371
    p.compile(migraphx::make_target("ref"));
6372
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6373
    std::vector<int> results_vector;
Paul's avatar
Paul committed
6374
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6375
    std::vector<int> gold{2, 6, 10};
Paul's avatar
Paul committed
6376
6377
6378
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6379
TEST_CASE(reduce_min_axis02)
Paul's avatar
Paul committed
6380
6381
{
    migraphx::program p;
6382
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
6383
6384
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6385
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6386
    mm->add_instruction(migraphx::make_op("reduce_min", {{"axes", {0, 2}}}), l0);
6387
    p.compile(migraphx::make_target("ref"));
6388
    auto result = p.eval({}).back();
Paul's avatar
Paul committed
6389
6390
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6391
    std::vector<float> gold{1, 3};
Paul's avatar
Paul committed
6392
6393
6394
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6395
TEST_CASE(reduce_min_axis1)
Khalique's avatar
Khalique committed
6396
6397
{
    migraphx::program p;
6398
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6399
6400
6401
6402
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
    auto l0    = mm->add_literal(input);
    mm->add_instruction(migraphx::make_op("reduce_min", {{"axes", {1}}}), l0);
6403
    p.compile(migraphx::make_target("ref"));
6404
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6405
    std::vector<float> results_vector;
Khalique's avatar
Khalique committed
6406
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6407
6408
    std::vector<float> gold{1, 2, 5, 6, 9, 10};
    EXPECT(results_vector == gold);
Khalique's avatar
Khalique committed
6409
6410
}

Shucai Xiao's avatar
Shucai Xiao committed
6411
TEST_CASE(reduce_min_axis12)
Shucai Xiao's avatar
Shucai Xiao committed
6412
6413
{
    migraphx::program p;
6414
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6415
6416
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6417
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6418
    mm->add_instruction(migraphx::make_op("reduce_min", {{"axes", {1, 2}}}), l0);
6419
    p.compile(migraphx::make_target("ref"));
6420
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6421
6422
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6423
    std::vector<float> gold{1, 5, 9};
Shucai Xiao's avatar
Shucai Xiao committed
6424
6425
6426
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6427
TEST_CASE(reduce_prod_axis0)
Shucai Xiao's avatar
Shucai Xiao committed
6428
6429
{
    migraphx::program p;
6430
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6431
6432
    migraphx::shape s{migraphx::shape::float_type, {4, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3, 2, 3}};
6433
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6434
    mm->add_instruction(migraphx::make_op("reduce_prod", {{"axes", {0}}}), l0);
6435
    p.compile(migraphx::make_target("ref"));
6436
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6437
6438
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6439
    std::vector<float> gold{6, 18, 12, 18};
Shucai Xiao's avatar
Shucai Xiao committed
6440
6441
6442
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6443
TEST_CASE(reduce_sum_axis0)
Shucai Xiao's avatar
Shucai Xiao committed
6444
6445
{
    migraphx::program p;
6446
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6447
6448
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6449
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6450
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0}}}), l0);
6451
    p.compile(migraphx::make_target("ref"));
6452
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6453
6454
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6455
    std::vector<float> gold{15, 18, 21, 24};
Shucai Xiao's avatar
Shucai Xiao committed
6456
6457
6458
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6459
TEST_CASE(reduce_sum_axis02)
Shucai Xiao's avatar
Shucai Xiao committed
6460
6461
{
    migraphx::program p;
6462
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6463
6464
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6465
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6466
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), l0);
6467
    p.compile(migraphx::make_target("ref"));
6468
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6469
6470
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6471
    std::vector<float> gold{33, 45};
Shucai Xiao's avatar
Shucai Xiao committed
6472
6473
6474
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6475
TEST_CASE(reduce_sum_axis1)
Shucai Xiao's avatar
Shucai Xiao committed
6476
6477
{
    migraphx::program p;
6478
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6479
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
6480
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6481
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6482
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {1}}}), l0);
6483
    p.compile(migraphx::make_target("ref"));
6484
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6485
    std::vector<float> results_vector;
Shucai Xiao's avatar
Shucai Xiao committed
6486
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6487
    std::vector<float> gold{4, 6, 12, 14, 20, 22};
Shucai Xiao's avatar
Shucai Xiao committed
6488
6489
6490
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6491
TEST_CASE(reduce_sum_axis12)
Shucai Xiao's avatar
Shucai Xiao committed
6492
6493
{
    migraphx::program p;
6494
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6495
6496
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6497
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6498
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {1, 2}}}), l0);
6499
    p.compile(migraphx::make_target("ref"));
6500
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6501
6502
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6503
    std::vector<float> gold{10, 26, 42};
Shucai Xiao's avatar
Shucai Xiao committed
6504
6505
6506
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6507
TEST_CASE(reduce_sum_axis2)
Shucai Xiao's avatar
Shucai Xiao committed
6508
6509
{
    migraphx::program p;
6510
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6511
6512
    migraphx::shape s{migraphx::shape::float_type, {3, 2, 2}};
    auto input = migraphx::literal{s, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};
6513
    auto l0    = mm->add_literal(input);
Shucai Xiao's avatar
Shucai Xiao committed
6514
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {2}}}), l0);
6515
    p.compile(migraphx::make_target("ref"));
6516
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6517
6518
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6519
    std::vector<float> gold{3, 7, 11, 15, 19, 23};
Shucai Xiao's avatar
Shucai Xiao committed
6520
6521
6522
    EXPECT(results_vector == gold);
}

Shucai Xiao's avatar
Shucai Xiao committed
6523
6524
6525
6526
6527
6528
6529
TEST_CASE(relu_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = mm->add_literal(migraphx::literal{s, {-1.f, 0.f, 1.f}});
    mm->add_instruction(migraphx::make_op("relu"), l);
6530
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6531
6532
6533
6534
6535
6536
6537
    auto result = p.eval({}).back();
    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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
6538
TEST_CASE(relu_dyn_test)
6539
6540
6541
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6542
    migraphx::shape::dynamic_dimension dd{3, 8};
6543
6544
6545
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("relu"), input);
6546
    p.compile(migraphx::make_target("ref"));
6547

Charlie Lin's avatar
Charlie Lin committed
6548
    std::vector<float> input_data{-1.f, 0.f, 1.f};
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

6559
TEST_CASE(reshape_test0)
Shucai Xiao's avatar
Shucai Xiao committed
6560
6561
6562
6563
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
6564
6565
6566
6567
6568
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {8, 3, 1, 1};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
6569
    p.compile(migraphx::make_target("ref"));
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, data));
}

TEST_CASE(reshape_test1)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {1, 3, 4, 2};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
6586
    p.compile(migraphx::make_target("ref"));
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, data));
}

TEST_CASE(reshape_test2)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {1, 2, 3, 4};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
6603
    p.compile(migraphx::make_target("ref"));
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, data));
}

TEST_CASE(reshape_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6614
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
6615
6616
6617
    std::vector<int64_t> new_shape = {0, 8, 3, 1};
    auto input                     = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
6618
    p.compile(migraphx::make_target("ref"));
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628

    std::vector<float> data(48);
    std::iota(data.begin(), data.end(), -3);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 24, 1, 1}};
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, data));
Shucai Xiao's avatar
Shucai Xiao committed
6629
6630
}

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
TEST_CASE(reverse_test_axis0)
{
    migraphx::shape in_shape{migraphx::shape::float_type, {2, 16}};
    std::vector<float> data(32);
    std::iota(data.begin(), data.end(), 1);
    migraphx::program p;
    auto* mm              = p.get_main_module();
    auto l                = mm->add_literal(migraphx::literal{in_shape, data});
    std::vector<int> axes = {0};
    mm->add_instruction(migraphx::make_op("reverse", {{"axes", axes}}), l);
6641
    p.compile(migraphx::make_target("ref"));
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> target_data = data;
    std::swap_ranges(target_data.begin(), target_data.begin() + 16, target_data.begin() + 16);
    EXPECT(migraphx::verify_range(results_vector, target_data));
}

TEST_CASE(reverse_test_axis1)
{
    migraphx::shape in_shape{migraphx::shape::float_type, {2, 16}};
    std::vector<float> data(32);
    std::iota(data.begin(), data.end(), 1);
    migraphx::program p;
    auto* mm              = p.get_main_module();
    auto l                = mm->add_literal(migraphx::literal{in_shape, data});
    std::vector<int> axes = {1};
    mm->add_instruction(migraphx::make_op("reverse", {{"axes", axes}}), l);
6660
    p.compile(migraphx::make_target("ref"));
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> target_data = data;
    std::reverse(target_data.begin(), target_data.begin() + 16);
    std::reverse(target_data.end() - 16, target_data.end());
    EXPECT(migraphx::verify_range(results_vector, target_data));
}

TEST_CASE(reverse_test_axis10)
{
    migraphx::shape in_shape{migraphx::shape::float_type, {2, 16}};
    std::vector<float> data(32);
    std::iota(data.begin(), data.end(), 1);
    migraphx::program p;
    auto* mm              = p.get_main_module();
    auto l                = mm->add_literal(migraphx::literal{in_shape, data});
    std::vector<int> axes = {1, 0};
    mm->add_instruction(migraphx::make_op("reverse", {{"axes", axes}}), l);
6680
    p.compile(migraphx::make_target("ref"));
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> target_data = data;
    std::reverse(target_data.begin(), target_data.begin() + 16);
    std::reverse(target_data.end() - 16, target_data.end());
    std::swap_ranges(target_data.begin(), target_data.begin() + 16, target_data.begin() + 16);
    EXPECT(migraphx::verify_range(results_vector, target_data));
}

Shucai Xiao's avatar
Shucai Xiao committed
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
TEST_CASE(roialign_out_of_bound_test)
{
    auto create_program = [](const std::string& trans_mode = "half_pixel") {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape x_s{migraphx::shape::float_type, {1, 1, 10, 10}};
        std::vector<float> x_vec = {
            0.2764, 0.7150, 0.1958, 0.3416, 0.4638, 0.0259, 0.2963, 0.6518, 0.4856, 0.7250,
            0.9637, 0.0895, 0.2919, 0.6753, 0.0234, 0.6132, 0.8085, 0.5324, 0.8992, 0.4467,
            0.3265, 0.8479, 0.9698, 0.2471, 0.9336, 0.1878, 0.4766, 0.4308, 0.3400, 0.2162,
            0.0206, 0.1720, 0.2155, 0.4394, 0.0653, 0.3406, 0.7724, 0.3921, 0.2541, 0.5799,
            0.4062, 0.2194, 0.4473, 0.4687, 0.7109, 0.9327, 0.9815, 0.6320, 0.1728, 0.6119,
            0.3097, 0.1283, 0.4984, 0.5068, 0.4279, 0.0173, 0.4388, 0.0430, 0.4671, 0.7119,
            0.1011, 0.8477, 0.4726, 0.1777, 0.9923, 0.4042, 0.1869, 0.7795, 0.9946, 0.9689,
            0.1366, 0.3671, 0.7011, 0.6234, 0.9867, 0.5585, 0.6985, 0.5609, 0.8788, 0.9928,
            0.5697, 0.8511, 0.6711, 0.9406, 0.8751, 0.7496, 0.1650, 0.1049, 0.1559, 0.2514,
            0.7012, 0.4056, 0.7879, 0.3461, 0.0415, 0.2998, 0.5094, 0.3727, 0.5482, 0.0502};

        migraphx::shape roi_s{migraphx::shape::float_type, {3, 4}};
        std::vector<float> roi_vec = {0, 0, 9.99, 9.99, 0, 5, 4, 9, 5, 5, 9.9, 9.9};

        migraphx::shape ind_s{migraphx::shape::int64_type, {3}};
        std::vector<int64_t> ind_vec = {0, 0, 0};

        auto x   = mm->add_literal(migraphx::literal(x_s, x_vec));
        auto roi = mm->add_literal(migraphx::literal(roi_s, roi_vec));
        auto ind = mm->add_literal(migraphx::literal(ind_s, ind_vec));
        auto r =
            mm->add_instruction(migraphx::make_op("roialign",
                                                  {{"coordinate_transformation_mode", trans_mode},
                                                   {"spatial_scale", 5.0},
                                                   {"output_height", 1},
                                                   {"output_width", 1},
                                                   {"sampling_ratio", 1}}),
                                x,
                                roi,
                                ind);
        mm->add_return({r});
        return p;
    };

    {
        auto p = create_program("output_half_pixel");
6734
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {0.0f, 0.0f, 0.0f};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

TEST_CASE(roialign_test)
{
6746
6747
6748
6749
    auto create_program = [](const std::string& trans_mode = "half_pixel",
                             const migraphx::op::pooling_mode pooling_mode =
                                 migraphx::op::pooling_mode::average,
                             int64_t sampling_ratio = 2) {
Shucai Xiao's avatar
Shucai Xiao committed
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape x_s{migraphx::shape::float_type, {1, 1, 10, 10}};
        std::vector<float> x_vec = {
            0.2764, 0.7150, 0.1958, 0.3416, 0.4638, 0.0259, 0.2963, 0.6518, 0.4856, 0.7250,
            0.9637, 0.0895, 0.2919, 0.6753, 0.0234, 0.6132, 0.8085, 0.5324, 0.8992, 0.4467,
            0.3265, 0.8479, 0.9698, 0.2471, 0.9336, 0.1878, 0.4766, 0.4308, 0.3400, 0.2162,
            0.0206, 0.1720, 0.2155, 0.4394, 0.0653, 0.3406, 0.7724, 0.3921, 0.2541, 0.5799,
            0.4062, 0.2194, 0.4473, 0.4687, 0.7109, 0.9327, 0.9815, 0.6320, 0.1728, 0.6119,
            0.3097, 0.1283, 0.4984, 0.5068, 0.4279, 0.0173, 0.4388, 0.0430, 0.4671, 0.7119,
            0.1011, 0.8477, 0.4726, 0.1777, 0.9923, 0.4042, 0.1869, 0.7795, 0.9946, 0.9689,
            0.1366, 0.3671, 0.7011, 0.6234, 0.9867, 0.5585, 0.6985, 0.5609, 0.8788, 0.9928,
            0.5697, 0.8511, 0.6711, 0.9406, 0.8751, 0.7496, 0.1650, 0.1049, 0.1559, 0.2514,
            0.7012, 0.4056, 0.7879, 0.3461, 0.0415, 0.2998, 0.5094, 0.3727, 0.5482, 0.0502};

        migraphx::shape roi_s{migraphx::shape::float_type, {3, 4}};
        std::vector<float> roi_vec = {0, 0, 9, 9, 0, 5, 4, 9, 5, 5, 9, 9};

        migraphx::shape ind_s{migraphx::shape::int64_type, {3}};
        std::vector<int64_t> ind_vec = {0, 0, 0};

        auto x   = mm->add_literal(migraphx::literal(x_s, x_vec));
        auto roi = mm->add_literal(migraphx::literal(roi_s, roi_vec));
        auto ind = mm->add_literal(migraphx::literal(ind_s, ind_vec));
        auto r =
            mm->add_instruction(migraphx::make_op("roialign",
                                                  {{"coordinate_transformation_mode", trans_mode},
                                                   {"spatial_scale", 1.0},
                                                   {"output_height", 5},
                                                   {"output_width", 5},
                                                   {"sampling_ratio", sampling_ratio},
                                                   {"mode", pooling_mode}}),
                                x,
                                roi,
                                ind);
        mm->add_return({r});
        return p;
    };

    {
        auto p = create_program();
6791
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {
            0.466421425, 0.446552634, 0.340521216, 0.568848491, 0.606780827, 0.371379346,
            0.429571986, 0.383519977, 0.556241512, 0.351050019, 0.27680251,  0.488286227,
            0.522200167, 0.552770197, 0.417057365, 0.471240699, 0.4844096,   0.690457463,
            0.492039412, 0.877398551, 0.623889625, 0.712461948, 0.628926516, 0.335504025,
            0.349469036, 0.302179992, 0.43046391,  0.469585985, 0.39774403,  0.542259991,
            0.365552008, 0.704923987, 0.516481996, 0.317131996, 0.701444089, 0.291239977,
            0.505897999, 0.647610962, 0.623489916, 0.829879999, 0.591567993, 0.738860011,
            0.704825997, 0.837148011, 0.889315963, 0.622680008, 0.615276039, 0.709713995,
            0.615356028, 0.458524048, 0.238451958, 0.337952018, 0.371693879, 0.609999895,
            0.760059953, 0.376724035, 0.378532052, 0.71468991,  0.924308002, 0.972783983,
            0.574903965, 0.582623959, 0.570936024, 0.761904061, 0.876998067, 0.535508037,
            0.256580025, 0.214098021, 0.279604018, 0.360000014, 0.436488032, 0.350427985,
            0.288755983, 0.366139978, 0.234920025};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        auto p = create_program("output_half_pixel");
6815
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {
            0.517783, 0.343411, 0.322905, 0.447362, 0.634375, 0.40308,  0.536647, 0.442791,
            0.486144, 0.402313, 0.251194, 0.400154, 0.515524, 0.695369, 0.346537, 0.33504,
            0.460099, 0.588069, 0.343863, 0.684932, 0.49319,  0.714058, 0.821744, 0.471935,
            0.403946, 0.306955, 0.218678, 0.33369,  0.488001, 0.486962, 0.18709,  0.49142,
            0.55611,  0.419167, 0.368608, 0.143278, 0.460835, 0.597125, 0.53096,  0.498207,
            0.278818, 0.438569, 0.6022,   0.700038, 0.752436, 0.577385, 0.702383, 0.725097,
            0.733754, 0.816304, 0.23933,  0.407514, 0.337893, 0.252521, 0.474335, 0.367075,
            0.270168, 0.41051,  0.64189,  0.830777, 0.55564,  0.454295, 0.55645,  0.75015,
            0.929997, 0.66257,  0.561664, 0.481275, 0.495449, 0.666306, 0.663573, 0.372107,
            0.205603, 0.192776, 0.247849};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
6835
        auto p = create_program("output_half_pixel", migraphx::op::pooling_mode::max, 0);
6836
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {
            0.819145, 0.373103, 0.258302,  0.515419, 0.726104, 0.540536, 0.545512,  0.38511,
            0.376545, 0.274635, 0.22341,   0.184511, 0.230843, 0.404869, 0.29546,   0.540409,
            0.265838, 0.409324, 0.213915,  0.708654, 0.687264, 0.580821, 0.461283,  0.462879,
            0.709632, 0.27873,  0.083619,  0.22428,  0.313992, 0.410508, 0.0929099, 0.415373,
            0.296695, 0.231574, 0.136836,  0.0683,   0.296695, 0.211925, 0.245385,  0.28053,
            0.17091,  0.179879, 0.245385,  0.343539, 0.392742, 0.51273,  0.536193,  0.382995,
            0.422793, 0.761886, 0.0839429, 0.276444, 0.19746,  0.126117, 0.378351,  0.254646,
            0.092148, 0.272825, 0.381955,  0.626599, 0.251325, 0.244475, 0.194875,  0.272825,
            0.44757,  0.351855, 0.342265,  0.244475, 0.274841, 0.553644, 0.607176,  0.202392,
            0.07425,  0.066087, 0.126279};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
6856
TEST_CASE(round_test)
Shucai Xiao's avatar
Shucai Xiao committed
6857
6858
{
    migraphx::program p;
6859
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6860
6861
6862
6863
    migraphx::shape s{migraphx::shape::float_type, {9}};
    auto l =
        mm->add_literal(migraphx::literal{s, {1.1, 1.5, 1.6, -1.1, -1.5, -1.6, 0.0, 2.0, -2.0}});
    mm->add_instruction(migraphx::make_op("round"), l);
6864
    p.compile(migraphx::make_target("ref"));
6865
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6866
6867
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6868
6869
    std::vector<float> gold = {1.0, 2.0, 2.0, -1.0, -2.0, -2.0, 0.0, 2.0, -2.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
6870
6871
}

Charlie Lin's avatar
Charlie Lin committed
6872
TEST_CASE(round_dyn_test)
6873
6874
6875
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6876
    migraphx::shape::dynamic_dimension dd{4, 10};
6877
6878
6879
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("round"), input);
6880
    p.compile(migraphx::make_target("ref"));
6881

Charlie Lin's avatar
Charlie Lin committed
6882
    std::vector<float> input_data{1.1, 1.5, 1.6, -1.1, -1.5, -1.6, 0.0, 2.0, -2.0};
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {9}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {1.0, 2.0, 2.0, -1.0, -2.0, -2.0, 0.0, 2.0, -2.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
6893
TEST_CASE(rsqrt_test)
Shucai Xiao's avatar
Shucai Xiao committed
6894
6895
{
    migraphx::program p;
6896
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6897
6898
6899
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l = mm->add_literal(migraphx::literal{s, {4.0, 16.0, 64.0}});
    mm->add_instruction(migraphx::make_op("rsqrt"), l);
6900
    p.compile(migraphx::make_target("ref"));
6901
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
6902
    std::vector<float> results_vector(3);
Shucai Xiao's avatar
Shucai Xiao committed
6903
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
6904
6905
    std::vector<float> gold = {0.5, 0.25, 0.125};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
6906
6907
}

Charlie Lin's avatar
Charlie Lin committed
6908
TEST_CASE(rsqrt_dyn_test)
6909
6910
6911
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6912
    migraphx::shape::dynamic_dimension dd{3, 8};
6913
6914
6915
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("rsqrt"), input);
6916
    p.compile(migraphx::make_target("ref"));
6917

Charlie Lin's avatar
Charlie Lin committed
6918
    std::vector<float> input_data{4.0, 16.0, 64.0};
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {0.5, 0.25, 0.125};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

6929
6930
// reduction_mode: "scatter_none", "scatter_add", "scatter_mul"
migraphx::program create_scatter_program(const std::string& reduction_mode, int axis)
Shucai Xiao's avatar
Shucai Xiao committed
6931
{
6932
6933
6934
6935
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sd{migraphx::shape::float_type, {3, 3}};
    std::vector<float> vd(sd.elements(), 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
6936

6937
6938
    migraphx::shape si{migraphx::shape::int32_type, {2, 3}};
    std::vector<int> vi = {1, 0, 2, 0, 2, 1};
Shucai Xiao's avatar
Shucai Xiao committed
6939

6940
6941
    migraphx::shape su{migraphx::shape::float_type, {2, 3}};
    std::vector<float> vu = {1.0, 1.1, 1.2, 2.0, 2.1, 2.2};
Shucai Xiao's avatar
Shucai Xiao committed
6942

6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
    auto ld = mm->add_literal(migraphx::literal{sd, vd});
    auto li = mm->add_literal(migraphx::literal{si, vi});
    auto lu = mm->add_literal(migraphx::literal{su, vu});
    // scatter_none, formerly the scatter op
    auto r = mm->add_instruction(migraphx::make_op(reduction_mode, {{"axis", axis}}), ld, li, lu);
    mm->add_return({r});
    return p;
}

TEST_CASE(scatter_ax0_test)
{
    // this tests what used to be the only scatter op, now changed to 3 sub-ops
    // which have their own test case
    {
        migraphx::program p = create_scatter_program("scatter_none", 0);
6958
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6959
6960
6961
6962
6963
6964
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {2.0, 1.1, 0.0, 1.0, 0.0, 2.2, 0.0, 2.1, 1.2};
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
6965
}
Shucai Xiao's avatar
Shucai Xiao committed
6966

6967
6968
TEST_CASE(scatter_ax_neg_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
6969
    {
6970
        migraphx::program p = create_scatter_program("scatter_none", -2);
Shucai Xiao's avatar
Shucai Xiao committed
6971

6972
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
6973
6974
6975
6976
6977
6978
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {2.0, 1.1, 0.0, 1.0, 0.0, 2.2, 0.0, 2.1, 1.2};
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
6979
6980
6981
6982
6983
6984
}

TEST_CASE(scatter_ax1_test)
{
    {
        migraphx::program p = create_scatter_program("scatter_none", 1);
6985
        p.compile(migraphx::make_target("ref"));
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold = {1.1, 1.0, 1.2, 2.0, 2.2, 2.1, 0.0, 0.0, 0.0};
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

// similar to create_scatter_program but with different tensor values
// reduction_mode: "scatter_none", "scatter_add", "scatter_mul"
migraphx::program create_scatter_program2(const std::string& reduction_mode, int axis)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sd{migraphx::shape::float_type, {1, 5}};
    std::vector<float> vd({1., 2., 3., 4., 5.});

    migraphx::shape si{migraphx::shape::int32_type, {1, 2}};
    std::vector<int> vi = {1, 3};

    migraphx::shape su{migraphx::shape::float_type, {1, 2}};
    std::vector<float> vu = {1.1, 2.1};

    auto ld = mm->add_literal(migraphx::literal{sd, vd});
    auto li = mm->add_literal(migraphx::literal{si, vi});
    auto lu = mm->add_literal(migraphx::literal{su, vu});
    auto r  = mm->add_instruction(migraphx::make_op(reduction_mode, {{"axis", axis}}), ld, li, lu);
    mm->add_return({r});
    return p;
}
TEST_CASE(scatter_reduction1_test)
{
    {
        // Test sub-ops for the three reduction values scatter_none, scatter_add, scatter_mul
        migraphx::program p = create_scatter_program2("scatter_none", 1);

7022
        p.compile(migraphx::make_target("ref"));
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_none = {1.0, 1.1, 3.0, 2.1, 5.0};
        EXPECT(migraphx::verify_range(results_vector, gold_none));
    }
}

TEST_CASE(scatter_reduction2_test)
{
    {
        migraphx::program p = create_scatter_program2("scatter_mul", 1);
7035
        p.compile(migraphx::make_target("ref"));
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_mul = {1.0, 2.2, 3.0, 8.4, 5.0};

        EXPECT(migraphx::verify_range(results_vector, gold_mul));
    }
}
TEST_CASE(scatter_reduction3_test)
{
    {
        migraphx::program p = create_scatter_program2("scatter_add", 1);
7048
        p.compile(migraphx::make_target("ref"));
7049
7050
7051
7052
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_add = {1.0, 3.1, 3.0, 6.1, 5.0};
Shucai Xiao's avatar
Shucai Xiao committed
7053

7054
7055
7056
7057
7058
7059
        EXPECT(migraphx::verify_range(results_vector, gold_add));
    }
}

TEST_CASE(scatter_reduction_3x3_test)
{
Shucai Xiao's avatar
Shucai Xiao committed
7060
7061
7062
7063
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape sd{migraphx::shape::float_type, {3, 3}};
7064
        std::vector<float> vd(sd.elements(), 3.0f);
Shucai Xiao's avatar
Shucai Xiao committed
7065
7066
7067
7068
7069

        migraphx::shape si{migraphx::shape::int32_type, {2, 3}};
        std::vector<int> vi = {1, 0, 2, 0, 2, 1};

        migraphx::shape su{migraphx::shape::float_type, {2, 3}};
7070
        std::vector<float> vu = {1.0, 1.1, 1.2, 7.0, 7.1, 7.2};
Shucai Xiao's avatar
Shucai Xiao committed
7071
7072
7073
7074

        auto ld = mm->add_literal(migraphx::literal{sd, vd});
        auto li = mm->add_literal(migraphx::literal{si, vi});
        auto lu = mm->add_literal(migraphx::literal{su, vu});
7075
        auto r  = mm->add_instruction(migraphx::make_op("scatter_add", {{"axis", 1}}), ld, li, lu);
Shucai Xiao's avatar
Shucai Xiao committed
7076
        mm->add_return({r});
7077
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
7078
7079
7080
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
        std::vector<float> gold_a2 = {4.1, 4.0, 4.2, 10.0, 10.2, 10.1, 3.0, 3.0, 3.0};

        EXPECT(migraphx::verify_range(results_vector, gold_a2));
    }
}

// create a test scatter program with a 3x3 tensor;
//  su and si are transposed from previous case
migraphx::program create_scatter_program_3x3(const std::string& reduction_mode, int axis)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sd{migraphx::shape::float_type, {3, 3}};
    std::vector<float> vd(sd.elements(), 3.0f);

    migraphx::shape si{migraphx::shape::int32_type, {3, 2}};
    std::vector<int> vi = {1, 0, 0, 2, 2, 1};

    migraphx::shape su{migraphx::shape::float_type, {3, 2}};
    std::vector<float> vu = {1.0, 7.0, 1.1, 7.1, 1.2, 7.2};

    auto ld = mm->add_literal(migraphx::literal{sd, vd});
    auto li = mm->add_literal(migraphx::literal{si, vi});
    auto lu = mm->add_literal(migraphx::literal{su, vu});
    auto r  = mm->add_instruction(migraphx::make_op(reduction_mode, {{"axis", axis}}), ld, li, lu);
    mm->add_return({r});
    return p;
}

TEST_CASE(scatter_reduction_3x3_xpose1_test)
{
    // test on vertical (0) axis. su and si are transposed from previous case
    {
        migraphx::program p = create_scatter_program_3x3("scatter_none", 0);
7115
        p.compile(migraphx::make_target("ref"));
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_none2 = {1.1, 7.0, 3.0, 1.0, 7.2, 3.0, 1.2, 7.1, 3.0};
        EXPECT(migraphx::verify_range(results_vector, gold_none2));
    }
}

TEST_CASE(scatter_reduction_3x3_xpose2_test)
{
    // test on vertical (0) axis.
    {
        migraphx::program p = create_scatter_program_3x3("scatter_add", 0);
7129
        p.compile(migraphx::make_target("ref"));
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_a3 = {4.1, 10.0, 3.0, 4.0, 10.2, 3.0, 4.2, 10.1, 3.0};

        EXPECT(migraphx::verify_range(results_vector, gold_a3));
    }
}

TEST_CASE(scatter_reduction_3x3_xpose3_test)
{
    {
        migraphx::program p = create_scatter_program_3x3("scatter_mul", 0);
7143
        p.compile(migraphx::make_target("ref"));
7144
7145
7146
7147
7148
7149
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold_mul2 = {3.3, 21.0, 3.0, 3.0, 21.6, 3.0, 3.6, 21.3, 3.0};

        EXPECT(migraphx::verify_range(results_vector, gold_mul2));
Shucai Xiao's avatar
Shucai Xiao committed
7150
7151
7152
    }
}

turneram's avatar
turneram committed
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
TEST_CASE(scatternd_shapes_test)
{
    {
        // broadcasted input
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {4, 1}};
        migraphx::shape us{dtype, {4}};

        std::vector<int64_t> ind_vec{4, 3, 1, 7};
        std::vector<float> upd_vec{9, 10, 11, 12};

        auto data    = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8}}}),
                                        mm->add_literal(migraphx::literal{0.0f}));
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7174
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{0, 11, 0, 10, 9, 0, 0, 12};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // non-standard shape input
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {2, 2}};
        migraphx::shape is{itype, {2, 2}};
        migraphx::shape us{dtype, {2}};

        std::vector<float> data_vec{1, 2, 3, 4};
        std::vector<int64_t> ind_vec{0, 0, 0, 1};
        std::vector<float> upd_vec{5, 6};

        auto data = mm->add_literal(migraphx::literal{ds, data_vec});
        auto td =
            mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), data);
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), td, indices, updates);
        mm->add_return({scatternd});
7205
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{5, 6, 2, 4};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // non-standard updates shape
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {2, 2, 2}};
        migraphx::shape is{itype, {2, 1, 3}};
        migraphx::shape us{dtype, {1, 2}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{0, 0, 0, 1, 1, 1};
        std::vector<float> upd_vec{9, 10};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto tu =
            mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), updates);
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, tu);
        mm->add_return({scatternd});
7236
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{9, 2, 3, 4, 5, 6, 7, 10};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

TEST_CASE(scatternd_test)
{
    {
        // r=1, q=2, k=1
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {8}};
        migraphx::shape is{itype, {4, 1}};
        migraphx::shape us{dtype, {4}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{4, 3, 1, 7};
        std::vector<float> upd_vec{9, 10, 11, 12};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7268
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1, 11, 3, 10, 9, 6, 7, 12};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // r=2, q=2, k=2
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {2, 2}};
        migraphx::shape is{itype, {2, 2}};
        migraphx::shape us{dtype, {2}};

        std::vector<float> data_vec{1, 2, 3, 4};
        std::vector<int64_t> ind_vec{0, 0, 0, 1};
        std::vector<float> upd_vec{5, 6};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7297
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{5, 6, 3, 4};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // r=3, q=3, k=3
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {2, 2, 2}};
        migraphx::shape is{itype, {2, 1, 3}};
        migraphx::shape us{dtype, {2, 1}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{0, 0, 0, 1, 1, 1};
        std::vector<float> upd_vec{9, 10};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7326
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{9, 2, 3, 4, 5, 6, 7, 10};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // r=3, q=2, k=1
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {4, 4, 4}};
        migraphx::shape is{itype, {2, 1}};
        migraphx::shape us{dtype, {2, 4, 4}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1,
                                    1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1,
                                    8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8,
                                    8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{0, 2};
        std::vector<float> upd_vec{5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
                                   1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7359
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 1, 2, 3, 4, 5, 6,
                                7, 8, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
                                4, 4, 4, 4, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // r=5, q=1, k=1
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {2, 2, 2, 2, 2}};
        migraphx::shape is{itype, {1}};
        migraphx::shape us{dtype, {2, 2, 2, 2}};

        std::vector<float> data_vec(32, 1);
        std::vector<int64_t> ind_vec{1};
        std::vector<float> upd_vec(16, 0);

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_none"), data, indices, updates);
        mm->add_return({scatternd});
7390
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold(32, 0);
        std::copy(data_vec.begin(), data_vec.begin() + 16, gold.begin());

        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

TEST_CASE(scatternd_reduction_test)
{
    {
        // reduction = add
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {8}};
        migraphx::shape is{itype, {8, 1}};
        migraphx::shape us{dtype, {8}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{4, 3, 1, 7, 4, 3, 1, 7};
        std::vector<float> upd_vec{9, 10, 11, 12, -8, -9, -10, -11};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_add"), data, indices, updates);
        mm->add_return({scatternd});
7423
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1, 3, 3, 5, 6, 6, 7, 9};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }

    {
        // reduction = mul
        migraphx::program p;
        auto* mm   = p.get_main_module();
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape ds{dtype, {8}};
        migraphx::shape is{itype, {4, 1}};
        migraphx::shape us{dtype, {4}};

        std::vector<float> data_vec{1, 2, 3, 4, 5, 6, 7, 8};
        std::vector<int64_t> ind_vec{4, 3, 1, 7};
        std::vector<float> upd_vec{9, 10, 11, 12};

        auto data    = mm->add_literal(migraphx::literal{ds, data_vec});
        auto indices = mm->add_literal(migraphx::literal{is, ind_vec});
        auto updates = mm->add_literal(migraphx::literal{us, upd_vec});
        auto scatternd =
            mm->add_instruction(migraphx::make_op("scatternd_mul"), data, indices, updates);
        mm->add_return({scatternd});
7452
        p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
7453
7454
7455
7456
7457
7458
7459
7460
7461
        auto result = p.eval({}).back();
        std::vector<float> results_vector;
        result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
        std::vector<float> gold{1, 22, 3, 40, 45, 6, 7, 96};

        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

Charlie Lin's avatar
Charlie Lin committed
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
TEST_CASE(select_module_add_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape lit_s{migraphx::shape{migraphx::shape::float_type, {1}}};
    auto literal_ins = mm->add_literal(migraphx::literal{lit_s, {6}});

    // create batch submodules
    auto create_submodule = [&](std::size_t batch_size, const std::string& module_name) {
        auto* submod = p.create_module(module_name);
        migraphx::shape sm_shape{migraphx::shape::float_type, {batch_size, 4}};
        auto sm_input = submod->add_parameter("data", sm_shape);
        auto broadcast_lit =
            submod->add_instruction(migraphx::make_op("multibroadcast"), literal_ins, sm_input);
        auto add_ins = submod->add_instruction(migraphx::make_op("add"), sm_input, broadcast_lit);
        submod->add_return({add_ins});
        return submod;
    };
    auto* batch1 = create_submodule(1, "batch_1");
    auto* batch2 = create_submodule(2, "batch_2");
    auto* batch3 = create_submodule(3, "batch_3");
    auto* batch4 = create_submodule(4, "batch_4");

    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {4, 4}}};
    auto input                              = mm->add_parameter("data", s);
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}}});
    migraphx::shape out_attr = migraphx::shape{sub_shapes};
    auto sm_ins              = mm->add_instruction(
        migraphx::make_op("select_module", {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
        {input},
        {batch1, batch2, batch3, batch4});
    auto ret = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), sm_ins);
    mm->add_return({ret});
7496
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541

    std::vector<float> input_data{-4, 8, -1, 4, -1, 8, 8, -4};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 4}};
    params["data"] = migraphx::argument(input_fixed_shape, input_data.data());
    auto result    = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{2, 14, 5, 10, 5, 14, 14, 2};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(select_module_reduce_test0)
{
    migraphx::program p;

    // create batch submodules
    auto create_submodule = [&](std::size_t batch_size, const std::string& module_name) {
        auto* submod = p.create_module(module_name);
        migraphx::shape sm_shape{migraphx::shape::float_type, {batch_size, 2, 2}};
        auto sm_input = submod->add_parameter("data", sm_shape);
        auto reduce_ins =
            submod->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {1}}}), sm_input);
        auto squeeze_ins =
            submod->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), reduce_ins);
        submod->add_return({squeeze_ins});
        return submod;
    };
    auto* batch1 = create_submodule(1, "batch_1");
    auto* batch2 = create_submodule(2, "batch_2");
    auto* batch3 = create_submodule(3, "batch_3");
    auto* batch4 = create_submodule(4, "batch_4");

    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}};
    auto input                              = mm->add_parameter("data", s);
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}}});
    migraphx::shape out_attr = migraphx::shape{sub_shapes};
    auto sm_ins              = mm->add_instruction(
        migraphx::make_op("select_module", {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
        {input},
        {batch1, batch2, batch3, batch4});
    auto ret = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), sm_ins);
    mm->add_return({ret});
7542
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587

    std::vector<float> input_data{-4, 8, -1, 4, -1, 8, 8, -4};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 2, 2}};
    params["data"] = migraphx::argument(input_fixed_shape, input_data.data());
    auto result    = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{-5, 12, 7, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(select_module_reduce_test1)
{
    migraphx::program p;

    // create batch submodules
    auto create_submodule = [&](std::size_t batch_size, const std::string& module_name) {
        auto* submod = p.create_module(module_name);
        migraphx::shape sm_shape{migraphx::shape::float_type, {batch_size, 2, 2}};
        auto sm_input = submod->add_parameter("data", sm_shape);
        auto reduce_ins =
            submod->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {1}}}), sm_input);
        auto squeeze_ins =
            submod->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), reduce_ins);
        submod->add_return({squeeze_ins});
        return submod;
    };
    auto* batch1 = create_submodule(1, "batch_1");
    auto* batch2 = create_submodule(2, "batch_2");
    auto* batch3 = create_submodule(3, "batch_3");
    auto* batch4 = create_submodule(4, "batch_4");

    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}};
    auto input                              = mm->add_parameter("data", s);
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}}});
    migraphx::shape out_attr = migraphx::shape{sub_shapes};
    auto sm_ins              = mm->add_instruction(
        migraphx::make_op("select_module", {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
        {input},
        {batch1, batch2, batch3, batch4});
    auto ret = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), sm_ins);
    mm->add_return({ret});
7588
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633

    std::vector<float> input_data{-4, 8, -1, 4, -1, 8, 8, -4, -4, 8, -1, 4, -1, 8, 8, -4};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {4, 2, 2}};
    params["data"] = migraphx::argument(input_fixed_shape, input_data.data());
    auto result    = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{-5, 12, 7, 4, -5, 12, 7, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(select_module_not_found_error)
{
    migraphx::program p;

    // create batch submodules
    auto create_submodule = [&](std::size_t batch_size, const std::string& module_name) {
        auto* submod = p.create_module(module_name);
        migraphx::shape sm_shape{migraphx::shape::float_type, {batch_size, 2, 2}};
        auto sm_input = submod->add_parameter("data", sm_shape);
        auto reduce_ins =
            submod->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {1}}}), sm_input);
        auto squeeze_ins =
            submod->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), reduce_ins);
        submod->add_return({squeeze_ins});
        return submod;
    };
    auto* batch1 = create_submodule(1, "batch_1");
    auto* batch2 = create_submodule(2, "batch_2");
    auto* batch3 = create_submodule(3, "batch_3");
    auto* batch4 = create_submodule(4, "batch_4");

    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}};
    auto input                              = mm->add_parameter("data", s);
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}}});
    migraphx::shape out_attr = migraphx::shape{sub_shapes};
    auto sm_ins              = mm->add_instruction(
        migraphx::make_op("select_module", {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
        {input},
        {batch1, batch2, batch3, batch4});
    auto ret = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), sm_ins);
    mm->add_return({ret});
7634
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
7635
7636
7637
7638
7639
7640

    std::vector<float> input_data{-4, 8, -1, 4, -1, 8,  8,  -4, -4, 8,
                                  -1, 4, -1, 8, 8,  -4, -1, 8,  8,  -4};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {5, 2, 2}};
    params["data"] = migraphx::argument(input_fixed_shape, input_data.data());
7641
    EXPECT(test::throws([&] { std::ignore = p.eval(params).back(); }));
Charlie Lin's avatar
Charlie Lin committed
7642
7643
}

7644
7645
7646
7647
7648
7649
7650
TEST_CASE(scatternd_reduction_dyn_test)
{
    // reduction = add, with dynamic input shapes
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
7651
    migraphx::shape::dynamic_dimension dd{3, 6};
7652
7653
    migraphx::shape ds{migraphx::shape::float_type, {dd, dd, dd}};
    migraphx::shape is{itype, {2, 1}};
7654
    migraphx::shape us{dtype, {{2, 2}, dd, dd}};
7655
7656
7657
7658
7659
7660
7661
7662

    auto xdata    = mm->add_parameter("X", ds);
    auto xindex   = mm->add_parameter("I", is);
    auto xupdates = mm->add_parameter("U", us);

    auto scatternd_add_op = migraphx::make_op("scatternd_add");
    auto scatternd        = mm->add_instruction(scatternd_add_op, xdata, xindex, xupdates);
    mm->add_return({scatternd});
7663
    p.compile(migraphx::make_target("ref"));
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688

    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4, 4, 4}}; // data
    std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6,
                                  7, 8, 8, 7, 6, 5, 4, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4,
                                  5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8};
    std::vector<uint64_t> input_index{0, 2};
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {2, 4, 4}}; // updates
    std::vector<float> input_updates{5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
                                     1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};

    params["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    params["I"] = migraphx::argument(is, input_index.data());
    params["U"] = migraphx::argument(input_fixed_shape1, input_updates.data());

    auto result = p.eval(params).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{6, 7, 8, 9, 11, 12, 13, 14, 15, 14, 13, 12, 12, 11, 10, 9,
                            1, 2, 3, 4, 5,  6,  7,  8,  8,  7,  6,  5,  4,  3,  2,  1,
                            9, 8, 7, 6, 6,  5,  4,  3,  4,  5,  6,  7,  9,  10, 11, 12,
                            8, 7, 6, 5, 4,  3,  2,  1,  1,  2,  3,  4,  5,  6,  7,  8};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
7689
TEST_CASE(sigmoid_test)
Shucai Xiao's avatar
Shucai Xiao committed
7690
7691
{
    migraphx::program p;
7692
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7693
7694
7695
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    auto l = mm->add_literal(migraphx::literal{s, {-1, 2, -3, 4}});
    mm->add_instruction(migraphx::make_op("sigmoid"), l);
7696
    p.compile(migraphx::make_target("ref"));
7697
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
7698
    std::vector<float> results_vector(4);
Shucai Xiao's avatar
Shucai Xiao committed
7699
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
7700
7701
    std::vector<float> gold{sigmoid(-1), sigmoid(2), sigmoid(-3), sigmoid(4)};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
7702
7703
}

Charlie Lin's avatar
Charlie Lin committed
7704
TEST_CASE(sigmoid_dyn_test)
7705
7706
7707
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7708
    migraphx::shape s{migraphx::shape::float_type, {{2, 4}, {2, 2}}};
7709
7710
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("sigmoid"), input);
7711
    p.compile(migraphx::make_target("ref"));
7712

Charlie Lin's avatar
Charlie Lin committed
7713
    std::vector<float> input_data{-1, 2, -3, 4};
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {2, 2}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    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));
}

Shucai Xiao's avatar
Shucai Xiao committed
7724
TEST_CASE(sign_test)
Shucai Xiao's avatar
Shucai Xiao committed
7725
7726
{
    migraphx::program p;
7727
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7728
7729
7730
7731
    migraphx::shape s{migraphx::shape::float_type, {5}};
    auto l = mm->add_literal(
        migraphx::literal{s, {1.02481645, 0.85643062, -0.03404123, -0.92791926, 0.0}});
    mm->add_instruction(migraphx::make_op("sign"), l);
7732
    p.compile(migraphx::make_target("ref"));
7733
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
7734
7735
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
7736
7737
    std::vector<float> gold = {1.0, 1.0, -1.0, -1.0, 0.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
Shucai Xiao's avatar
Shucai Xiao committed
7738
7739
}

Charlie Lin's avatar
Charlie Lin committed
7740
TEST_CASE(sign_dyn_test)
7741
7742
7743
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7744
    migraphx::shape::dynamic_dimension dd{3, 8};
7745
7746
7747
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("sign"), input);
7748
    p.compile(migraphx::make_target("ref"));
7749

Charlie Lin's avatar
Charlie Lin committed
7750
    std::vector<float> input_data{1.02481645, 0.85643062, -0.03404123, -0.92791926, 0.0};
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {5}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {1.0, 1.0, -1.0, -1.0, 0.0};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
7761
TEST_CASE(sin_test)
Khalique's avatar
Khalique committed
7762
7763
{
    migraphx::program p;
7764
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
7765
    migraphx::shape s{migraphx::shape::float_type, {3}};
7766
7767
    std::vector<float> data = {-1, 0, 1};
    auto l                  = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
7768
    mm->add_instruction(migraphx::make_op("sin"), l);
7769
    p.compile(migraphx::make_target("ref"));
7770
    auto result = p.eval({}).back();
Khalique's avatar
Khalique committed
7771
7772
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
7773
7774
7775
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sinf(n); });
Khalique's avatar
Khalique committed
7776
7777
7778
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Charlie Lin's avatar
Charlie Lin committed
7779
TEST_CASE(sin_dyn_test)
7780
7781
7782
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7783
    migraphx::shape::dynamic_dimension dd{3, 8};
7784
    migraphx::shape s{migraphx::shape::float_type, {dd}};
Charlie Lin's avatar
Charlie Lin committed
7785
    auto input = mm->add_parameter("X", s);
7786
    mm->add_instruction(migraphx::make_op("sin"), input);
7787
    p.compile(migraphx::make_target("ref"));
7788

Charlie Lin's avatar
Charlie Lin committed
7789
    std::vector<float> input_data = {-1, 0, 1};
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sinf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
7802
TEST_CASE(sinh_test)
7803
7804
{
    migraphx::program p;
7805
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7806
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
7807
7808
    std::vector<float> data{-1.0, 2.0, -3.0, 4.0};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
7809
    mm->add_instruction(migraphx::make_op("sinh"), l);
7810
    p.compile(migraphx::make_target("ref"));
7811
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
7812
    std::vector<float> results_vector(4);
7813
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
7814
7815
7816
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sinhf(n); });
7817
7818
7819
    EXPECT(migraphx::verify_range(results_vector, gold));
}

7820
7821
7822
7823
TEST_CASE(sinh_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7824
    migraphx::shape s{migraphx::shape::float_type, {{2, 4}, {2, 4}}};
7825
7826
7827
    auto input = mm->add_parameter("X", s);
    std::vector<float> input_data{-1.0, 2.0, -3.0, 4.0};
    mm->add_instruction(migraphx::make_op("sinh"), input);
7828
    p.compile(migraphx::make_target("ref"));
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841

    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sinhf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
7842
TEST_CASE(slice_test)
Shucai Xiao's avatar
Shucai Xiao committed
7843
{
Shucai Xiao's avatar
Shucai Xiao committed
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<int> data(2 * 2 * 3);
        std::iota(data.begin(), data.end(), 0);
        migraphx::shape s{migraphx::shape::int32_type, {2, 2, 3}};
        auto l0 = mm->add_literal(migraphx::literal{s, data});
        mm->add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1}}, {"ends", {3}}}), l0);
        migraphx::shape s2{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}};
        EXPECT(p.get_output_shapes().back() == s2);
7855
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
        migraphx::shape sresult{migraphx::shape::int32_type, {2, 2, 2}, {4, 2, 1}};
        auto result           = p.eval({}).back();
        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()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(result.get_shape() == sresult);
    }
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<int> data(2 * 2 * 3);
        std::iota(data.begin(), data.end(), 0);
        migraphx::shape s{migraphx::shape::int32_type, {2, 2, 3}};
        auto l0 = mm->add_literal(migraphx::literal{s, data});
        mm->add_instruction(
            migraphx::make_op("slice",
                              {{"axes", {0, 1, 2}}, {"starts", {0, 0, 0}}, {"ends", {2, 2, 2}}}),
            l0);
        migraphx::shape s2{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}};
        EXPECT(p.get_output_shapes().back() == s2);
7877
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
7878
7879
7880
7881
7882
7883
7884
7885
        migraphx::shape sresult{migraphx::shape::int32_type, {2, 2, 2}, {4, 2, 1}};
        auto result           = p.eval({}).back();
        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()); });
        EXPECT(migraphx::verify_range(results_vector, gold));
        EXPECT(result.get_shape() == sresult);
    }
Shucai Xiao's avatar
Shucai Xiao committed
7886
7887
}

Brian Pickrell's avatar
Brian Pickrell committed
7888
7889
TEST_CASE(slice_dyn_test0)
{
7890
7891
    // Slice a single dynamic dimension. ax1 slice limits are smaller than min; ax2 "ends" is
    // too large
Brian Pickrell's avatar
Brian Pickrell committed
7892
7893
    migraphx::program p;
    auto* mm = p.get_main_module();
7894
    migraphx::shape s{migraphx::shape::int32_type, {{2, 3}, {2, 2}, {3, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
7895
7896
7897
    auto x = mm->add_parameter("x", s);
    mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {1, 2}}, {"starts", {0, 1}}, {"ends", {1, 6}}}), x);
7898
    migraphx::shape s2{migraphx::shape::int32_type, {{2, 3}, {1, 1}, {2, 2}}};
Brian Pickrell's avatar
Brian Pickrell committed
7899
    EXPECT(p.get_output_shapes().back() == s2);
7900
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924

    //  the strides of sresult are those of the original shape, not
    // reduced to sliced size.
    migraphx::shape sresult{migraphx::shape::int32_type, {2, 1, 2}, {6, 3, 1}};
    migraphx::shape input_fixed_shape{migraphx::shape::int32_type, {2, 2, 3}};
    migraphx::parameter_map params;
    std::vector<int> data(2 * 2 * 3);
    std::iota(data.begin(), data.end(), 0);
    params["x"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();

    std::vector<int> gold = {1, 2, 7, 8};
    std::vector<int> results_vector(2 * 1 * 2);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });

    EXPECT(migraphx::verify_range(results_vector, gold));
    EXPECT(result.get_shape() == sresult);
}

TEST_CASE(slice_dyn_test1)
{
    // Slice all three dynamic dimensions
    migraphx::program p;
    auto* mm = p.get_main_module();
7925
    migraphx::shape s{migraphx::shape::int32_type, {{2, 2}, {2, 2}, {3, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
7926
7927
7928
7929
7930
7931
    auto x = mm->add_parameter("x", s);
    mm->add_instruction(
        migraphx::make_op("slice",
                          {{"axes", {0, 1, 2}}, {"starts", {0, 0, 0}}, {"ends", {2, 2, 2}}}),
        x);

7932
    migraphx::shape s2{migraphx::shape::int32_type, {{2, 2}, {2, 2}, {2, 2}}};
Brian Pickrell's avatar
Brian Pickrell committed
7933
    EXPECT(p.get_output_shapes().back() == s2);
7934
    p.compile(migraphx::make_target("ref"));
Brian Pickrell's avatar
Brian Pickrell committed
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
    migraphx::shape sresult{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}};

    migraphx::shape input_fixed_shape{migraphx::shape::int32_type, {2, 2, 3}};
    migraphx::parameter_map params;
    std::vector<int> data(2 * 2 * 3);
    std::iota(data.begin(), data.end(), 0);
    params["x"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();

    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()); });
    EXPECT(migraphx::verify_range(results_vector, gold));
    EXPECT(result.get_shape() == sresult);
}

Shucai Xiao's avatar
Shucai Xiao committed
7951
TEST_CASE(softmax_simple_test)
Shucai Xiao's avatar
Shucai Xiao committed
7952
7953
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7954
7955
7956
7957
7958
7959
    auto* mm             = p.get_main_module();
    std::vector<float> a = {0.25, 0.75};
    std::vector<float> s = {0.377541, 0.622459};
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 2}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), al);
7960
    p.compile(migraphx::make_target("ref"));
7961
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
7962
    std::vector<float> results_vector(2);
Shucai Xiao's avatar
Shucai Xiao committed
7963
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
7964
    EXPECT(migraphx::verify_range(results_vector, s));
Shucai Xiao's avatar
Shucai Xiao committed
7965
7966
}

Shucai Xiao's avatar
Shucai Xiao committed
7967
TEST_CASE(softmax_test)
7968
7969
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
    auto* mm             = p.get_main_module();
    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};
7996

Shucai Xiao's avatar
Shucai Xiao committed
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
    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};
8016

Shucai Xiao's avatar
Shucai Xiao committed
8017
8018
    migraphx::shape a_shape{migraphx::shape::float_type, {5, 3, 4, 2}};
    auto al = mm->add_literal(migraphx::literal{a_shape, a});
Shucai Xiao's avatar
Shucai Xiao committed
8019
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), al);
8020
    p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8021
8022
8023
8024
    auto result = p.eval({}).back();
    std::vector<float> results_vector(120);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(results_vector, s));
8025
}
8026

8027
8028
8029
8030
8031
TEST_CASE(softmax_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape a_shape{migraphx::shape::float_type,
8032
                            {{1, 10}, {1, 3, {3}}, {4, 4}, {2, 2, {2}}}};
8033
8034
    auto al = mm->add_parameter("a", a_shape);
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), al);
8035
    p.compile(migraphx::make_target("ref"));
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089

    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};
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {5, 3, 4, 2}};
    params["a"] = migraphx::argument(input_fixed_shape, a.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector(120);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    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};
    EXPECT(migraphx::verify_range(results_vector, s));
}

Shucai Xiao's avatar
Shucai Xiao committed
8090
TEST_CASE(sqdiff_test)
kahmed10's avatar
kahmed10 committed
8091
8092
{
    migraphx::program p;
8093
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8094
8095
8096
8097
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = mm->add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = mm->add_literal(migraphx::literal{s, {1, 2, 3}});
    mm->add_instruction(migraphx::make_op("sqdiff"), l1, l2);
8098
    p.compile(migraphx::make_target("ref"));
kahmed10's avatar
kahmed10 committed
8099
8100
8101
    auto result = p.eval({}).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
8102
    std::vector<float> gold = {4, 4, 4};
kahmed10's avatar
kahmed10 committed
8103
8104
8105
    EXPECT(migraphx::verify_range(results_vector, gold));
}

8106
8107
8108
8109
TEST_CASE(sqdiff_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8110
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
8111
8112
8113
8114
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x = mm->add_parameter("x", s);
    auto y = mm->add_parameter("y", s);
    mm->add_instruction(migraphx::make_op("sqdiff"), x, y);
8115
    p.compile(migraphx::make_target("ref"));
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129

    std::vector<float> x_data{-1, 0, 1};
    std::vector<float> y_data{1, 2, 3};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {4, 4, 4};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
8130
TEST_CASE(sqrt_test)
8131
8132
{
    migraphx::program p;
8133
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8134
    migraphx::shape s{migraphx::shape::float_type, {5}};
8135
8136
    std::vector<float> data{1.02481645, 0.85643062, 0.03404123, 0.92791926, 0.10569184};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
8137
    mm->add_instruction(migraphx::make_op("sqrt"), l);
8138
    p.compile(migraphx::make_target("ref"));
8139
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
8140
    std::vector<float> results_vector;
8141
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
8142
8143
8144
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sqrtf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
8145
    EXPECT(migraphx::verify_range(results_vector, gold));
8146
8147
}

8148
8149
8150
8151
TEST_CASE(sqrt_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8152
    migraphx::shape::dynamic_dimension dd{3, 8};
8153
8154
8155
8156
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    std::vector<float> input_data{1.02481645, 0.85643062, 0.03404123, 0.92791926, 0.10569184};
    mm->add_instruction(migraphx::make_op("sqrt"), input);
8157
    p.compile(migraphx::make_target("ref"));
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170

    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {5}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return sqrtf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
8171
TEST_CASE(squeeze_test)
8172
{
Shucai Xiao's avatar
Shucai Xiao committed
8173
8174
8175
8176
8177
8178
8179
8180
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(4 * 3 * 3);
        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 = mm->add_literal(migraphx::literal{s1, data});
        mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), l0);
8181
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
        auto result = p.eval({}).back();
        EXPECT(result.get_shape() == s2);
    }
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(4 * 3 * 3);
        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 = mm->add_literal(migraphx::literal{s1, data});
        mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {3}}}), l0);
8193
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8194
8195
8196
        auto result = p.eval({}).back();
        EXPECT(result.get_shape() == s2);
    }
8197

Shucai Xiao's avatar
Shucai Xiao committed
8198
8199
8200
8201
8202
8203
8204
8205
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(4 * 3 * 3);
        migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 3, 3}};
        auto l0 = mm->add_literal(migraphx::literal{s1, data});
        mm->add_instruction(migraphx::make_op("squeeze"), l0);
8206
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8207
8208
8209
        auto result = p.eval({}).back();
        EXPECT(result.get_shape() == s2);
    }
8210
8211
}

8212
8213
8214
8215
TEST_CASE(squeeze_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8216
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {1, 1}, {3, 3}}};
8217
8218
    auto p0 = mm->add_parameter("x", s1);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), p0);
8219
    p.compile(migraphx::make_target("ref"));
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229

    std::vector<float> input_data(4 * 3 * 3);
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    migraphx::shape s2{migraphx::shape::float_type, {4, 3, 1, 3}};
    EXPECT(result.get_shape() == s2);
}

Shucai Xiao's avatar
Shucai Xiao committed
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
TEST_CASE(step_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(2 * 4 * 6);
        std::iota(data.begin(), data.end(), 2);
        migraphx::shape s1{migraphx::shape::float_type, {2, 1, 4, 6}};
        auto l0 = mm->add_literal(migraphx::literal{s1, data});
        auto r  = mm->add_instruction(
            migraphx::make_op("step", {{"axes", {0, 2, 3}}, {"steps", {2, 2, 3}}}), l0);
        mm->add_return({r});
8242
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
        auto result = p.eval({}).back();
        migraphx::shape s2{migraphx::shape::float_type, {1, 1, 2, 2}};
        EXPECT(result.get_shape() == s2);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(2 * 4 * 6);
        std::iota(data.begin(), data.end(), 2);
        migraphx::shape s1{migraphx::shape::float_type, {2, 1, 4, 6}};
        auto l0 = mm->add_literal(migraphx::literal{s1, data});
8255
8256
8257
        auto tl = mm->add_instruction(
            migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
        auto r = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
8258
8259
            migraphx::make_op("step", {{"axes", {0, 1, 2}}, {"steps", {2, 2, 3}}}), tl);
        mm->add_return({r});
8260
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8261
8262
8263
8264
8265
8266
        auto result = p.eval({}).back();
        migraphx::shape s2{migraphx::shape::float_type, {1, 2, 2, 1}};
        EXPECT(result.get_shape() == s2);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
8267
TEST_CASE(sub_test)
8268
8269
{
    migraphx::program p;
8270
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8271
8272
8273
8274
    migraphx::shape s{migraphx::shape::float_type, {3}};
    auto l1 = mm->add_literal(migraphx::literal{s, {-1, 0, 1}});
    auto l2 = mm->add_literal(migraphx::literal{s, {1, 2, 3}});
    mm->add_instruction(migraphx::make_op("sub"), l1, l2);
8275
    p.compile(migraphx::make_target("ref"));
8276
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
8277
    std::vector<float> results_vector(3);
8278
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
Shucai Xiao's avatar
Shucai Xiao committed
8279
8280
    std::vector<float> gold = {-2, -2, -2};
    EXPECT(migraphx::verify_range(results_vector, gold));
8281
8282
}

8283
8284
8285
8286
TEST_CASE(sub_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8287
    std::vector<migraphx::shape::dynamic_dimension> dd{{2, 6}};
8288
8289
8290
8291
    migraphx::shape s{migraphx::shape::float_type, dd};
    auto x = mm->add_parameter("x", s);
    auto y = mm->add_parameter("y", s);
    mm->add_instruction(migraphx::make_op("sub"), x, y);
8292
    p.compile(migraphx::make_target("ref"));
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306

    std::vector<float> x_data{-1, 0, 1};
    std::vector<float> y_data{1, 2, 3};
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, x_data.data());
    params0["y"] = migraphx::argument(input_fixed_shape0, y_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = {-2, -2, -2};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
8307
TEST_CASE(tan_test)
8308
8309
{
    migraphx::program p;
8310
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8311
    migraphx::shape s{migraphx::shape::float_type, {3}};
8312
8313
    std::vector<float> data{-1, 0, 1};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
8314
    mm->add_instruction(migraphx::make_op("tan"), l);
8315
    p.compile(migraphx::make_target("ref"));
8316
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
8317
    std::vector<float> results_vector(3);
8318
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
8319
8320
8321
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return tanf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
8322
    EXPECT(migraphx::verify_range(results_vector, gold));
8323
8324
}

8325
8326
8327
8328
TEST_CASE(tan_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8329
    migraphx::shape::dynamic_dimension dd{3, 8};
8330
8331
8332
8333
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    std::vector<float> input_data{-1, 0, 1};
    mm->add_instruction(migraphx::make_op("tan"), input);
8334
    p.compile(migraphx::make_target("ref"));
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347

    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return tanf(n); });
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
8348
TEST_CASE(tanh_test)
8349
8350
{
    migraphx::program p;
8351
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8352
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
8353
8354
    std::vector<float> data{-1.0, 2.0, -3.0, 4.0};
    auto l = mm->add_literal(migraphx::literal{s, data});
Shucai Xiao's avatar
Shucai Xiao committed
8355
    mm->add_instruction(migraphx::make_op("tanh"), l);
8356
    p.compile(migraphx::make_target("ref"));
8357
    auto result = p.eval({}).back();
Shucai Xiao's avatar
Shucai Xiao committed
8358
    std::vector<float> results_vector(4);
8359
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
8360
8361
8362
    std::vector<float> gold = data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return tanhf(n); });
8363
8364
8365
8366
8367
8368
8369
    EXPECT(migraphx::verify_range(results_vector, gold));
}

TEST_CASE(tanh_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8370
    migraphx::shape::dynamic_dimension dd{3, 8};
8371
8372
8373
8374
    migraphx::shape s{migraphx::shape::float_type, {dd}};
    auto input = mm->add_parameter("X", s);
    std::vector<float> input_data{-1.0, 2.0, -3.0, 4.0};
    mm->add_instruction(migraphx::make_op("tanh"), input);
8375
    p.compile(migraphx::make_target("ref"));
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385

    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4}};
    params0["X"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    std::vector<float> results_vector(4);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold = input_data;
    std::transform(
        gold.begin(), gold.end(), gold.begin(), [](float n) -> float { return tanhf(n); });
Shucai Xiao's avatar
Shucai Xiao committed
8386
    EXPECT(migraphx::verify_range(results_vector, gold));
8387
8388
}

Shucai Xiao's avatar
Shucai Xiao committed
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
TEST_CASE(topk_test)
{
    auto create_program = [](int64_t k, int64_t axis, int largest) {
        migraphx::program p;
        auto* mm = p.get_main_module();
        migraphx::shape s{migraphx::shape::float_type, {3, 5}};
        auto data = mm->add_parameter("data", s);
        auto r    = mm->add_instruction(
            migraphx::make_op("topk", {{"axis", axis}, {"k", k}, {"largest", largest}}), data);
        auto r0 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), r);
        auto r1 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), r);
        mm->add_return({r0, r1});

        return p;
    };

    auto run_program = [&](int64_t k, int64_t axis, int largest) {
        auto p = create_program(k, axis, largest);
8407
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
        std::vector<float> data = {
            2.1, 2.3, 2.0, 2.5, 1.9, 3.3, 0.2, 4.5, 0.1, 0.8, 1.0, 4.5, 2.1, 0.8, 1.5};
        migraphx::shape s{migraphx::shape::float_type, {3, 5}};
        migraphx::parameter_map pp;
        pp["data"] = migraphx::argument(s, data.data());
        auto rets  = p.eval(pp);
        std::vector<float> ret_val;
        rets.front().visit([&](auto v) { ret_val.assign(v.begin(), v.end()); });
        std::vector<int64_t> ret_ind;
        rets.back().visit([&](auto v) { ret_ind.assign(v.begin(), v.end()); });

        return std::make_pair(ret_val, ret_ind);
    };

    // case 1
    {
        auto results                = run_program(4, 1, 1);
        std::vector<float> gold_val = {2.5, 2.3, 2.1, 2, 4.5, 3.3, 0.8, 0.2, 4.5, 2.1, 1.5, 1};
        EXPECT(results.first == gold_val);
        std::vector<int64_t> gold_ind = {3, 1, 0, 2, 2, 0, 4, 1, 1, 2, 4, 0};
        EXPECT(results.second == gold_ind);
    }

    // case 2
    {
        auto results                = run_program(4, 1, 0);
        std::vector<float> gold_val = {1.9, 2, 2.1, 2.3, 0.1, 0.2, 0.8, 3.3, 0.8, 1, 1.5, 2.1};
        EXPECT(results.first == gold_val);
        std::vector<int64_t> gold_ind = {4, 2, 0, 1, 3, 1, 4, 0, 3, 0, 4, 2};
        EXPECT(results.second == gold_ind);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
8441
TEST_CASE(transpose_test)
8442
{
Shucai Xiao's avatar
Shucai Xiao committed
8443
8444
8445
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 2, 2, 3}};
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);
8446

Shucai Xiao's avatar
Shucai Xiao committed
8447
8448
8449
8450
8451
    {
        migraphx::program p;
        auto* mm                  = p.get_main_module();
        auto l                    = mm->add_literal(migraphx::literal{a_shape, data});
        std::vector<int64_t> perm = {0, 3, 1, 2};
8452
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), l);
8453
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8454
8455
8456
8457
8458
8459
8460
        auto result = p.eval({}).back();
    }
    {
        migraphx::program p;
        auto* mm                  = p.get_main_module();
        auto l                    = mm->add_literal(migraphx::literal{a_shape, data});
        std::vector<int64_t> perm = {0, 3, 1, 2};
8461
8462
        auto result =
            mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), l);
Shucai Xiao's avatar
Shucai Xiao committed
8463
        mm->add_instruction(migraphx::make_op("contiguous"), result);
8464
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8465
8466
8467
8468
8469
8470
8471
8472
8473
        auto result2 = p.eval({}).back();

        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};
        EXPECT(migraphx::verify_range(results_vector, gold));
    }
}

Charlie Lin's avatar
Charlie Lin committed
8474
8475
8476
8477
TEST_CASE(transpose_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8478
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
8479
8480
8481
    auto l                    = mm->add_parameter("X", s);
    std::vector<int64_t> perm = {0, 3, 1, 2};
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), l);
8482
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499

    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {1, 2, 2, 3}};
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();

    std::vector<size_t> new_lens = {1, 3, 2, 2};
    EXPECT(result.get_shape().lens() == new_lens);

    std::vector<float> results_vector(12);
    result.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};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

Shucai Xiao's avatar
Shucai Xiao committed
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
TEST_CASE(unsqueeze_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(4 * 3 * 3);
        migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
        auto l0 = mm->add_literal(migraphx::literal{s1, data});
        mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l0);
8510
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
        auto result = p.eval({}).back();
        EXPECT(result.get_shape() == s2);
    }
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        std::vector<float> data(4 * 3 * 3);
        migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {4, 3, 1, 3}};
        auto l0 = mm->add_literal(migraphx::literal{s1, data});
        mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}}), l0);
8522
        p.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
8523
8524
8525
        auto result = p.eval({}).back();
        EXPECT(result.get_shape() == s2);
    }
8526
8527
}

8528
8529
8530
8531
8532
TEST_CASE(unsqueeze_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

8533
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4}, {3, 3}, {3, 3}}};
8534
8535
    auto p0 = mm->add_parameter("x", s1);
    mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), p0);
8536
    p.compile(migraphx::make_target("ref"));
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546

    std::vector<float> input_data(4 * 3 * 3);
    migraphx::parameter_map params0;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {4, 3, 3}};
    params0["x"] = migraphx::argument(input_fixed_shape0, input_data.data());
    auto result  = p.eval(params0).back();
    migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
    EXPECT(result.get_shape() == s2);
}

turneram's avatar
turneram committed
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
TEST_CASE(where_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sb{migraphx::shape::bool_type, {3, 3}};
    migraphx::shape sx{migraphx::shape::float_type, {3, 3}};

    std::vector<bool> b{true, true, true, false, false, false, true, false, true};
    std::vector<float> x(9, 1.0);
    std::vector<float> y(9, 2.0);

    auto lb = mm->add_literal(migraphx::literal{sb, b});
    auto lx = mm->add_literal(migraphx::literal{sx, x});
    auto ly = mm->add_literal(migraphx::literal{sx, y});
    auto w  = mm->add_instruction(migraphx::make_op("where"), lb, lx, ly);
    mm->add_return({w});
8563
    p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
    auto result = p.eval({}).back();
    std::vector<float> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
    std::vector<float> gold(9);
    for(int i = 0; i < gold.size(); ++i)
        gold[i] = b[i] ? x[i] : y[i];

    EXPECT(migraphx::verify_range(result_vec, gold));
}

8574
8575
8576
8577
TEST_CASE(where_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8578
8579
    migraphx::shape sb{migraphx::shape::bool_type, {{2, 3}, {2, 3}}};
    migraphx::shape sx{migraphx::shape::float_type, {{2, 3}, {2, 3}}};
8580
8581
8582
8583
8584

    auto lb = mm->add_parameter("predicate", sb);
    auto lx = mm->add_parameter("X", sx);
    auto ly = mm->add_parameter("Y", sx);
    mm->add_instruction(migraphx::make_op("where"), lb, lx, ly);
8585
    p.compile(migraphx::make_target("ref"));
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604

    std::vector<char> b{1, 1, 1, 0, 0, 0, 1, 0, 1};
    std::vector<float> x(9, 1.0);
    std::vector<float> y(9, 2.0);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape0{migraphx::shape::float_type, {3, 3}};
    migraphx::shape input_fixed_shape1{migraphx::shape::uint8_type, {3, 3}};
    params["X"] = migraphx::argument(input_fixed_shape0, x.data());
    params["Y"] = migraphx::argument(input_fixed_shape0, y.data());

    params["predicate"] = migraphx::argument(input_fixed_shape1, b.data());

    auto result = p.eval(params).back();
    std::vector<float> results_vector(3 * 3);
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> gold{1, 1, 1, 2, 2, 2, 1, 2, 1};
    EXPECT(migraphx::verify_range(results_vector, gold));
}

turneram's avatar
turneram committed
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
TEST_CASE(where_broadcasted_inputs_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sb{migraphx::shape::bool_type, {3, 3}};

    std::vector<bool> b{true, true, true, false, false, false, true, false, true};

    auto lb  = mm->add_literal(migraphx::literal{sb, b});
    auto lx  = mm->add_literal(migraphx::literal(1.0f));
    auto ly  = mm->add_literal(migraphx::literal(2.0f));
    auto mbx = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), lx);
    auto mby = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), ly);
    auto w   = mm->add_instruction(migraphx::make_op("where"), lb, mbx, mby);
    mm->add_return({w});
8620
    p.compile(migraphx::make_target("ref"));
turneram's avatar
turneram committed
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
    auto result = p.eval({}).back();
    std::vector<float> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
    std::vector<float> gold(9);
    std::vector<float> x(9, 1.0);
    std::vector<float> y(9, 2.0);
    for(int i = 0; i < gold.size(); ++i)
        gold[i] = b[i] ? x[i] : y[i];

    EXPECT(migraphx::verify_range(result_vec, gold));
}

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