onnx_test.cpp 348 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.
 */
Scott Thornton's avatar
Scott Thornton committed
24
#include <iostream>
Shucai Xiao's avatar
Shucai Xiao committed
25
#include <fstream>
Scott Thornton's avatar
Scott Thornton committed
26
#include <vector>
turneram's avatar
turneram committed
27
#include <random>
28
#include <migraphx/common.hpp>
29
#include <migraphx/apply_alpha_beta.hpp>
Paul's avatar
Paul committed
30
31
32
#include <migraphx/literal.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
33
#include <migraphx/instruction_ref.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
34
35
#include <migraphx/pass_manager.hpp>
#include <migraphx/dead_code_elimination.hpp>
turneram's avatar
turneram committed
36
#include <migraphx/rewrite_quantization.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
37
#include <migraphx/eliminate_identity.hpp>
Paul's avatar
Paul committed
38
#include <migraphx/onnx.hpp>
39
#include <migraphx/make_op.hpp>
turneram's avatar
turneram committed
40
41
42
43
44
#include <migraphx/op/convolution.hpp>
#include <migraphx/op/pooling.hpp>
#include <migraphx/op/lrn.hpp>
#include <migraphx/op/reshape.hpp>
#include <migraphx/op/unknown.hpp>
turneram's avatar
turneram committed
45
#include <migraphx/env.hpp>
46
47
48

#include <migraphx/serialize.hpp>

Scott Thornton's avatar
Scott Thornton committed
49
50
#include "test.hpp"

turneram's avatar
turneram committed
51
52
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_ENABLE_CK_WORKAROUNDS);

turneram's avatar
turneram committed
53
migraphx::program optimize_onnx(const std::string& name, bool run_passes = false)
Shucai Xiao's avatar
Shucai Xiao committed
54
{
55
56
57
    migraphx::onnx_options options;
    options.skip_unknown_operators = true;
    auto prog                      = migraphx::parse_onnx(name, options);
58
    auto* mm                       = prog.get_main_module();
turneram's avatar
turneram committed
59
60
61
    if(run_passes)
        migraphx::run_passes(*mm,
                             {migraphx::rewrite_quantization{}, migraphx::dead_code_elimination{}});
Shucai Xiao's avatar
Shucai Xiao committed
62
63

    // remove the last identity instruction
64
    auto last_ins = std::prev(mm->end());
65
    if(last_ins->name() == "@return")
Shucai Xiao's avatar
Shucai Xiao committed
66
    {
67
        mm->remove_instruction(last_ins);
Shucai Xiao's avatar
Shucai Xiao committed
68
69
70
71
72
    }

    return prog;
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
void add_celu_instruction(migraphx::module* mm, const migraphx::shape& s, float alpha)
{
    auto x                 = mm->add_parameter("x", s);
    const auto& input_lens = s.lens();
    const auto& input_type = s.type();
    auto zero_lit =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0.}}));
    auto one_lit =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1.}}));
    auto alpha_lit = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}}));
    auto linear_part = mm->add_instruction(migraphx::make_op("max"), zero_lit, x);
    auto divi        = mm->add_instruction(migraphx::make_op("div"), x, alpha_lit);
    auto expo        = mm->add_instruction(migraphx::make_op("exp"), divi);
    auto sub         = mm->add_instruction(migraphx::make_op("sub"), expo, one_lit);
    auto mul         = mm->add_instruction(migraphx::make_op("mul"), alpha_lit, sub);
    auto exp_part    = mm->add_instruction(migraphx::make_op("min"), zero_lit, mul);
    mm->add_instruction(migraphx::make_op("add"), linear_part, exp_part);
}

Charlie Lin's avatar
Charlie Lin committed
96
97
98
99
100
101
102
103
104
105
106
static std::vector<double> make_r_eyelike(size_t num_rows, size_t num_cols, size_t k)
{
    std::vector<double> eyelike_mat(num_rows * num_cols, 0);
    for(size_t i = 0; i < num_rows; ++i)
    {
        if(i + k < num_cols)
            eyelike_mat[(num_cols + 1) * i + k] = 1.;
    }
    return eyelike_mat;
}

Khalique's avatar
Khalique committed
107
TEST_CASE(acos_test)
Scott Thornton's avatar
Scott Thornton committed
108
{
Paul's avatar
Paul committed
109
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
110
111
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
112
    mm->add_instruction(migraphx::make_op("acos"), input);
Scott Thornton's avatar
Scott Thornton committed
113

Shucai Xiao's avatar
Shucai Xiao committed
114
    auto prog = optimize_onnx("acos_test.onnx");
Scott Thornton's avatar
Scott Thornton committed
115
116
117
118

    EXPECT(p == prog);
}

119
120
121
TEST_CASE(acosh_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
122
123
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
124
    mm->add_instruction(migraphx::make_op("acosh"), input);
125
126
127
128
129
130

    auto prog = optimize_onnx("acosh_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
131
TEST_CASE(add_bcast_test)
132
{
Paul's avatar
Paul committed
133
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
134
135
136
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 4}});
137
    auto l2  = mm->add_instruction(
138
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", l0->get_shape().lens()}}), l1);
139
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
140

Shucai Xiao's avatar
Shucai Xiao committed
141
    auto prog = optimize_onnx("add_bcast_test.onnx");
Paul's avatar
Paul committed
142

143
144
145
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
146
TEST_CASE(add_fp16_test)
Scott Thornton's avatar
Scott Thornton committed
147
{
Paul's avatar
Paul committed
148
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
149
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
150
    auto l0 =
Shucai Xiao's avatar
Shucai Xiao committed
151
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type, {1}}, {1.5}});
Khalique's avatar
Khalique committed
152
    auto l1 =
Shucai Xiao's avatar
Shucai Xiao committed
153
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type, {1}}, {2.5}});
154
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
155
    auto prog = optimize_onnx("add_fp16_test.onnx");
Scott Thornton's avatar
Scott Thornton committed
156
157
158
159

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
160
TEST_CASE(add_scalar_test)
161
{
Paul's avatar
Paul committed
162
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
163
164
165
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::uint8_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::uint8_type});
166
167
    auto m1 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
168
    auto r = mm->add_instruction(migraphx::make_op("add"), l0, m1);
Shucai Xiao's avatar
Shucai Xiao committed
169
    mm->add_return({r});
170
    auto prog = migraphx::parse_onnx("add_scalar_test.onnx");
171
172
173
174

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
175
TEST_CASE(argmax_test)
Khalique's avatar
Khalique committed
176
{
Paul's avatar
Paul committed
177
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
178
179
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
180
181
    auto ins = mm->add_instruction(migraphx::make_op("argmax", {{"axis", 2}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
182
    auto prog = optimize_onnx("argmax_test.onnx");
Khalique's avatar
Khalique committed
183
184
185
186

    EXPECT(p == prog);
}

187
188
189
190
191
192
193
194
195
196
197
198
199
TEST_CASE(argmax_select_last_index_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    auto ins = mm->add_instruction(
        migraphx::make_op("argmax", {{"axis", 2}, {"select_last_index", true}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
    auto prog = optimize_onnx("argmax_select_last_index_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
200
201
202
203
204
TEST_CASE(argmax_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
205
        "x", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {5, 5}, {6, 6}}});
Charlie Lin's avatar
Charlie Lin committed
206
207
208
209
210
    auto ins = mm->add_instruction(migraphx::make_op("argmax", {{"axis", 2}}), l0);
    auto ret = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
    mm->add_return({ret});

    migraphx::onnx_options options;
211
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
212
213
214
215
216
    auto prog                     = parse_onnx("argmax_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
217
TEST_CASE(argmin_test)
218
{
Paul's avatar
Paul committed
219
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
220
221
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
222
223
    auto ins = mm->add_instruction(migraphx::make_op("argmin", {{"axis", 3}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {3}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
224
    auto prog = optimize_onnx("argmin_test.onnx");
225
226
227
228

    EXPECT(p == prog);
}

229
230
231
232
233
234
235
236
237
238
239
240
241
TEST_CASE(argmin_select_last_index_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    auto ins = mm->add_instruction(
        migraphx::make_op("argmin", {{"axis", 3}, {"select_last_index", true}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {3}}}), ins);
    auto prog = optimize_onnx("argmin_select_last_index_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
242
TEST_CASE(asin_test)
243
{
Paul's avatar
Paul committed
244
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
245
246
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
247
    mm->add_instruction(migraphx::make_op("asin"), input);
248

Shucai Xiao's avatar
Shucai Xiao committed
249
    auto prog = optimize_onnx("asin_test.onnx");
250
251
252
253

    EXPECT(p == prog);
}

254
255
256
TEST_CASE(asinh_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
257
258
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
259
    mm->add_instruction(migraphx::make_op("asinh"), input);
260
261
262
263
264
265

    auto prog = optimize_onnx("asinh_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
266
TEST_CASE(atan_test)
Khalique's avatar
Khalique committed
267
{
Paul's avatar
Paul committed
268
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
269
270
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
271
    mm->add_instruction(migraphx::make_op("atan"), input);
Khalique's avatar
Khalique committed
272

Shucai Xiao's avatar
Shucai Xiao committed
273
    auto prog = optimize_onnx("atan_test.onnx");
Khalique's avatar
Khalique committed
274
275
276
277

    EXPECT(p == prog);
}

278
279
280
TEST_CASE(atanh_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
281
282
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
283
    mm->add_instruction(migraphx::make_op("atanh"), input);
284
285
286
287
288
289

    auto prog = optimize_onnx("atanh_test.onnx");

    EXPECT(p == prog);
}

290
291
292
TEST_CASE(averagepool_1d_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
293
294
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5}});
295
296
297
298
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::average},
                                           {"padding", {0, 0}},
                                           {"stride", {1}},
299
300
                                           {"lengths", {3}},
                                           {"dilations", {1}}}),
301
                        l0);
302
303
304
305
306

    auto prog = optimize_onnx("averagepool_1d_test.onnx");
    EXPECT(p == prog);
}

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
TEST_CASE(averagepool_dilate_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 4, 3}});
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::average},
                                           {"padding", {1, 1}},
                                           {"stride", {1}},
                                           {"lengths", {2}},
                                           {"dilations", {3}}}),
                        input);

    auto prog = optimize_onnx("averagepool_dilate_test.onnx");

    EXPECT(p == prog);
}

325
326
327
TEST_CASE(averagepool_3d_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
328
329
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5, 5, 5}});
330
    mm->add_instruction(migraphx::make_op("pooling",
331
                                          {{"mode", migraphx::op::pooling_mode::average},
kahmed10's avatar
kahmed10 committed
332
                                           {"padding", {0, 0, 0, 0, 0, 0}},
333
                                           {"stride", {1, 1, 1}},
334
335
                                           {"lengths", {3, 3, 3}},
                                           {"dilations", {1, 1, 1}}}),
336
                        l0);
337
338
339
340
341

    auto prog = optimize_onnx("averagepool_3d_test.onnx");
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
342
343
TEST_CASE(averagepool_dyn_test)
{
Brian Pickrell's avatar
Brian Pickrell committed
344
    // Pooling with dynamic input and no auto padding
Charlie Lin's avatar
Charlie Lin committed
345
346
347
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
348
        "0", {migraphx::shape::float_type, {{1, 4}, {3, 3}, {5, 5}, {5, 5}, {5, 5}}});
Brian Pickrell's avatar
Brian Pickrell committed
349
350
351
352
353
354
    auto ret =
        mm->add_instruction(migraphx::make_op("pooling",
                                              {
                                                  {"mode", migraphx::op::pooling_mode::average},
                                                  {"stride", {2, 2, 2}},
                                                  {"lengths", {3, 3, 3}},
355
                                                  {"dilations", {1, 1, 1}},
Brian Pickrell's avatar
Brian Pickrell committed
356
357
358
359
                                                  {"padding", {1, 1, 1, 1, 1, 1}},
                                                  {"padding_mode", 0},
                                              }),
                            l0);
Charlie Lin's avatar
Charlie Lin committed
360
361
362
    mm->add_return({ret});

    migraphx::onnx_options options;
363
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
364
365
366
367
    auto prog                     = migraphx::parse_onnx("averagepool_dyn_test.onnx", options);
    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
368
TEST_CASE(averagepool_dyn_autopad_test)
Charlie Lin's avatar
Charlie Lin committed
369
{
Brian Pickrell's avatar
Brian Pickrell committed
370
371
372
373
374
375
376
377
378
379
380
    // Pooling with dynamic input and auto padding. Default padding values will be overridden.
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
        "0", {migraphx::shape::float_type, {{1, 4}, {3, 3}, {5, 5}, {5, 5}, {5, 5}}});
    auto ret = mm->add_instruction(
        migraphx::make_op("pooling",
                          {
                              {"mode", migraphx::op::pooling_mode::average},
                              {"stride", {2, 2, 2}},
                              {"lengths", {3, 3, 3}},
381
                              {"dilations", {1, 1, 1}},
Brian Pickrell's avatar
Brian Pickrell committed
382
383
384
385
386
387
                              {"padding", {0, 0, 0, 0, 0, 0}},
                              {"padding_mode", migraphx::op::padding_mode_t::same_upper},
                          }),
        l0);
    mm->add_return({ret});

Charlie Lin's avatar
Charlie Lin committed
388
    migraphx::onnx_options options;
389
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
390
391
    auto prog = migraphx::parse_onnx("averagepool_dyn_autopad_test.onnx", options);
    EXPECT(p == prog);
Charlie Lin's avatar
Charlie Lin committed
392
393
394
395
396
}

TEST_CASE(averagepool_dyn_asym_padding_error_test)
{
    migraphx::onnx_options options;
397
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
398
399
400
401
402
403
404
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("averagepool_dyn_asym_padding_error_test.onnx", options); }));
}

TEST_CASE(averagepool_dyn_cip_error_test)
{
    migraphx::onnx_options options;
405
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
406
407
408
409
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("averagepool_dyn_cip_error_test.onnx", options); }));
}

410
TEST_CASE(averagepool_notset_test)
411
412
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
413
414
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
kahmed10's avatar
kahmed10 committed
415
    auto ins   = mm->add_instruction(migraphx::make_op("pooling",
416
417
418
                                                       {{"mode", migraphx::op::pooling_mode::average},
                                                        {"padding", {2, 2, 2, 2}},
                                                        {"stride", {2, 2}},
419
420
                                                        {"lengths", {6, 6}},
                                                        {"dilations", {1, 1}}}),
kahmed10's avatar
kahmed10 committed
421
422
                                   input);
    auto ret   = mm->add_instruction(
423
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {1, 1}}, {"ends", {2, 2}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
424
    mm->add_return({ret});
425
426
427
428
429
430
    auto prog = migraphx::parse_onnx("averagepool_notset_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(averagepool_nt_cip_test)
431
432
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
433
434
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
435
    std::vector<int64_t> pads = {0, 0, 0, 0, 0, 0, 1, 1};
436
    auto ins_pad = mm->add_instruction(migraphx::make_op("pad", {{"pads", pads}}), input);
437
438
439
440
    auto ret                  = mm->add_instruction(migraphx::make_op("pooling",
                                                                      {{"mode", migraphx::op::pooling_mode::average},
                                                                       {"padding", {0, 0, 0, 0}},
                                                                       {"stride", {2, 2}},
441
442
                                                                       {"lengths", {6, 6}},
                                                                       {"dilations", {1, 1}}}),
kahmed10's avatar
kahmed10 committed
443
                                   ins_pad);
Shucai Xiao's avatar
Shucai Xiao committed
444
    mm->add_return({ret});
445

446
    auto prog = migraphx::parse_onnx("averagepool_nt_cip_test.onnx");
447
448
449
450
    EXPECT(p == prog);
}

TEST_CASE(averagepool_same_lower_test)
451
{
Brian Pickrell's avatar
Brian Pickrell committed
452
453
    // auto_pad mode of SAME_LOWER with a static input shape is handled in parsing and
    // padding_mode is set to default_ when the operation is created
454
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
455
456
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
Brian Pickrell's avatar
Brian Pickrell committed
457
458
459
460
461
462
463
    auto ins   = mm->add_instruction(
        migraphx::make_op("pooling",
                            {
                              {"mode", migraphx::op::pooling_mode::average},
                              {"padding", {1, 1, 1, 1}},
                              {"stride", {1, 1}},
                              {"lengths", {2, 2}},
464
                              {"dilations", {1, 1}},
Brian Pickrell's avatar
Brian Pickrell committed
465
466
467
468
                              {"padding_mode", migraphx::op::padding_mode_t::default_},
                          }),
        input);
    auto ret = mm->add_instruction(
469
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {0, 0}}, {"ends", {5, 5}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
470
    mm->add_return({ret});
471
472
473
474
475
476
    auto prog = migraphx::parse_onnx("averagepool_same_lower_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(averagepool_sl_cip_test)
477
478
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
479
480
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
481
    std::vector<int64_t> pads = {0, 0, 1, 1, 0, 0, 0, 0};
482
    auto ins_pad = mm->add_instruction(migraphx::make_op("pad", {{"pads", pads}}), input);
483
484
485
486
    auto ret                  = mm->add_instruction(migraphx::make_op("pooling",
                                                                      {{"mode", migraphx::op::pooling_mode::average},
                                                                       {"padding", {0, 0, 0, 0}},
                                                                       {"stride", {1, 1}},
487
488
                                                                       {"lengths", {2, 2}},
                                                                       {"dilations", {1, 1}}}),
kahmed10's avatar
kahmed10 committed
489
                                   ins_pad);
Shucai Xiao's avatar
Shucai Xiao committed
490
    mm->add_return({ret});
491
    auto prog = migraphx::parse_onnx("averagepool_sl_cip_test.onnx");
492
493
494
495
496
497
498

    EXPECT(p == prog);
}

TEST_CASE(averagepool_same_upper_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
499
500
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
kahmed10's avatar
kahmed10 committed
501
    auto ins   = mm->add_instruction(migraphx::make_op("pooling",
502
503
504
                                                       {{"mode", migraphx::op::pooling_mode::average},
                                                        {"padding", {1, 1, 1, 1}},
                                                        {"stride", {1, 1}},
505
506
                                                        {"lengths", {2, 2}},
                                                        {"dilations", {1, 1}}}),
kahmed10's avatar
kahmed10 committed
507
508
                                   input);
    auto ret   = mm->add_instruction(
509
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {1, 1}}, {"ends", {6, 6}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
510
    mm->add_return({ret});
511
    auto prog = migraphx::parse_onnx("averagepool_same_upper_test.onnx");
512
513
514
515

    EXPECT(p == prog);
}

516
TEST_CASE(batch_norm_flat_test)
517
518
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
519
    auto* mm = p.get_main_module();
520

521
522
523
524
525
526
527
528
    auto x     = mm->add_parameter("x", {migraphx::shape::float_type, {10}});
    auto scale = mm->add_parameter("scale", {migraphx::shape::float_type, {1}});
    auto bias  = mm->add_parameter("bias", {migraphx::shape::float_type, {1}});
    auto mean  = mm->add_parameter("mean", {migraphx::shape::float_type, {1}});
    auto var   = mm->add_parameter("variance", {migraphx::shape::float_type, {1}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-6f}});

529
530
531
532
533
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), {var_eps});
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
534
535
536
    add_common_op(*mm, migraphx::make_op("add"), {r0, bias});

    auto prog = optimize_onnx("batch_norm_flat_test.onnx");
537
538
539
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
540
541
542
543
544
545
546
547
548
549
550
551
552
TEST_CASE(batch_norm_rank_2_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x     = mm->add_parameter("x", {migraphx::shape::float_type, {2, 5}});
    auto scale = mm->add_parameter("scale", {migraphx::shape::float_type, {5}});
    auto bias  = mm->add_parameter("bias", {migraphx::shape::float_type, {5}});
    auto mean  = mm->add_parameter("mean", {migraphx::shape::float_type, {5}});
    auto var   = mm->add_parameter("variance", {migraphx::shape::float_type, {5}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-6f}});

553
554
555
556
557
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), {var_eps});
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
Charlie Lin's avatar
Charlie Lin committed
558
559
560
561
562
563
    add_common_op(*mm, migraphx::make_op("add"), {r0, bias});

    auto prog = optimize_onnx("batch_norm_rank_2_test.onnx");
    EXPECT(p == prog);
}

564
TEST_CASE(batch_norm_1d_test)
565
566
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
567
    auto* mm = p.get_main_module();
568

569
570
571
572
573
574
575
576
577
578
579
580
581
    auto x     = mm->add_parameter("x", {migraphx::shape::half_type, {2, 3, 4}});
    auto scale = mm->add_parameter("scale", {migraphx::shape::float_type, {3}});
    auto bias  = mm->add_parameter("bias", {migraphx::shape::float_type, {3}});
    auto mean  = mm->add_parameter("mean", {migraphx::shape::float_type, {3}});
    auto var   = mm->add_parameter("variance", {migraphx::shape::float_type, {3}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::half_type, {1e-5f}});

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), scale);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), bias);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), mean);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), var);

582
583
584
585
586
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
587
588
589
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

    auto prog = optimize_onnx("batch_norm_1d_test.onnx");
590
591
592
    EXPECT(p == prog);
}

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
TEST_CASE(batch_norm_2d_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x     = mm->add_parameter("x", {migraphx::shape::float_type, {2, 3, 4, 4}});
    auto scale = mm->add_parameter("scale", {migraphx::shape::float_type, {3}});
    auto bias  = mm->add_parameter("bias", {migraphx::shape::float_type, {3}});
    auto mean  = mm->add_parameter("mean", {migraphx::shape::float_type, {3}});
    auto var   = mm->add_parameter("variance", {migraphx::shape::float_type, {3}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-5f}});

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), scale);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), bias);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), mean);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), var);

611
612
613
614
615
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

    auto prog = optimize_onnx("batch_norm_2d_test.onnx");
    EXPECT(p == prog);
}

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

    auto x     = mm->add_parameter("x", {migraphx::shape::half_type, {2, 2, 2, 2, 2}});
    auto scale = mm->add_parameter("scale", {migraphx::shape::half_type, {2}});
    auto bias  = mm->add_parameter("bias", {migraphx::shape::half_type, {2}});
    auto mean  = mm->add_parameter("mean", {migraphx::shape::half_type, {2}});
    auto var   = mm->add_parameter("variance", {migraphx::shape::half_type, {2}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::half_type, {1e-6f}});

    auto usq_scale =
        mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), scale);
    auto usq_bias =
        mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), bias);
    auto usq_mean =
        mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), mean);
    auto usq_var = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2, 3}}}), var);

643
644
645
646
647
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
648
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});
649

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    auto prog = optimize_onnx("batch_norm_3d_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(batch_norm_invalid_rank)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("batch_norm_invalid_rank.onnx"); }));
}

TEST_CASE(batch_norm_invalid_bias_rank)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("batch_norm_invalid_bias_rank.onnx"); }));
}

665
666
667
668
669
TEST_CASE(binary_dyn_brcst_prelu_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
670
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
671
672
673
674
675
676
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 5}});

    auto ret = add_common_op(*mm, migraphx::make_op("prelu"), {l0, l1});
    mm->add_return({ret});

    migraphx::onnx_options options;
677
    options.default_dyn_dim_value = {1, 4};
678
679
680
681
682
683
684
685
686
687
688
    auto prog = migraphx::parse_onnx("binary_dyn_brcst_prelu_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(binary_dyn_brcst_add_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::half_type, {4, 5}});
    auto l1  = mm->add_parameter(
689
        "1", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
690
691
692
693
694

    auto ret = add_common_op(*mm, migraphx::make_op("add"), {l0, l1});
    mm->add_return({ret});

    migraphx::onnx_options options;
695
    options.default_dyn_dim_value = {1, 4};
696
697
698
699
700
701
702
703
    auto prog                     = migraphx::parse_onnx("binary_dyn_brcst_add_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(binary_dyn_brcst_attr_error_test)
{
    migraphx::onnx_options options;
704
    options.default_dyn_dim_value = {1, 4};
705
706
707
708
709
710
711
712
713
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("binary_dyn_brcst_attr_error_test.onnx", options); }));
}

TEST_CASE(binary_dyn_brcst_mul_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
714
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
715
716
717
718
719
720
721
722
723
724
725
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 1}});

    auto bl1 = mm->add_instruction(
        migraphx::make_op("multibroadcast",
                          {{"out_dyn_dims", to_value(l0->get_shape().dyn_dims())}}),
        l1,
        l0);
    auto ret = mm->add_instruction(migraphx::make_op("mul"), l0, bl1);
    mm->add_return({ret});

    migraphx::onnx_options options;
726
    options.default_dyn_dim_value = {1, 4};
727
728
729
730
731
    auto prog                     = migraphx::parse_onnx("binary_dyn_brcst_mul_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
732
TEST_CASE(cast_test)
Khalique's avatar
Khalique committed
733
{
Paul's avatar
Paul committed
734
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
735
736
    auto* mm = p.get_main_module();
    auto l   = mm->add_parameter("x", migraphx::shape{migraphx::shape::half_type, {10}});
737
738
739
740
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l);
Khalique's avatar
Khalique committed
741

Shucai Xiao's avatar
Shucai Xiao committed
742
    auto prog = optimize_onnx("cast_test.onnx");
Khalique's avatar
Khalique committed
743
744
745
    EXPECT(p == prog);
}

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
TEST_CASE(castlike_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l   = mm->add_parameter("0", migraphx::shape{migraphx::shape::half_type, {10}});
    mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {10}});
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l);

    auto prog = optimize_onnx("castlike_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(castlike_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("castlike_error_test.onnx"); }));
}

Shucai Xiao's avatar
Shucai Xiao committed
766
767
768
TEST_CASE(ceil_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
769
770
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
771
    mm->add_instruction(migraphx::make_op("ceil"), input);
Shucai Xiao's avatar
Shucai Xiao committed
772

Shucai Xiao's avatar
Shucai Xiao committed
773
    auto prog = optimize_onnx("ceil_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
774
775
776
777

    EXPECT(p == prog);
}

778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
TEST_CASE(celu_alpha_test)
{
    migraphx::program p;
    auto* mm                            = p.get_main_module();
    std::vector<std::size_t> input_lens = {3};
    auto input_type                     = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    float alpha = 0.8;
    add_celu_instruction(mm, s, alpha);
    auto prog = optimize_onnx("celu_alpha_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(celu_default_test)
{
    migraphx::program p;
    auto* mm                            = p.get_main_module();
    std::vector<std::size_t> input_lens = {2, 3};
    auto input_type                     = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    float alpha = 1.0;
    add_celu_instruction(mm, s, alpha);
    auto prog = optimize_onnx("celu_default_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(celu_wrong_type_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("celu_wrong_type_test.onnx"); }));
}

TEST_CASE(celu_zero_alpha_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("celu_zero_alpha_test.onnx"); }));
}

Khalique's avatar
Khalique committed
814
TEST_CASE(clip_test)
Khalique's avatar
Khalique committed
815
816
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
817
818
819
820
    auto* mm     = p.get_main_module();
    auto l0      = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
821
    min_val =
822
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), min_val);
823
    max_val =
824
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), max_val);
825
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
Shucai Xiao's avatar
Shucai Xiao committed
826
    auto prog = optimize_onnx("clip_test.onnx");
Khalique's avatar
Khalique committed
827

828
829
830
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
831
832
833
TEST_CASE(clip_test_op11_max_only)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
834
835
836
    auto* mm     = p.get_main_module();
    auto max_val = mm->add_literal(0.0f);
    auto l0      = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
837
838
    mm->add_instruction(migraphx::make_op("undefined"));
    max_val =
839
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), max_val);
840
    auto r = mm->add_instruction(migraphx::make_op("min"), l0, max_val);
Shucai Xiao's avatar
Shucai Xiao committed
841
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
842
843
844
845
846
847

    auto prog = migraphx::parse_onnx("clip_test_op11_max_only.onnx");

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
848
849
850
TEST_CASE(clip_test_op11)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
851
852
853
854
    auto* mm     = p.get_main_module();
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
    auto l0      = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
855
    min_val =
856
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), min_val);
857
    max_val =
858
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), max_val);
859
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
kahmed10's avatar
kahmed10 committed
860
861
862
863
864
865
866
867
    auto prog = optimize_onnx("clip_test_op11.onnx");

    EXPECT(p == prog);
}

TEST_CASE(clip_test_op11_min_only)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
868
869
870
    auto* mm     = p.get_main_module();
    auto min_val = mm->add_literal(0.0f);
    auto l0      = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
871
    min_val =
872
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), min_val);
873
    mm->add_instruction(migraphx::make_op("max"), l0, min_val);
kahmed10's avatar
kahmed10 committed
874
875
876
877
878
879
880
881
    auto prog = optimize_onnx("clip_test_op11_min_only.onnx");

    EXPECT(p == prog);
}

TEST_CASE(clip_test_op11_no_args)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
882
883
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
884
    mm->add_instruction(migraphx::make_op("identity"), l0);
kahmed10's avatar
kahmed10 committed
885
886
887
888
889
    auto prog = optimize_onnx("clip_test_op11_no_args.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
890
891
892
TEST_CASE(clip_test_op11_no_args1)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
893
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
894

Shucai Xiao's avatar
Shucai Xiao committed
895
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
896
897
    mm->add_instruction(migraphx::make_op("undefined"));
    auto r = mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
898
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
899
900
901
902
903
    auto prog = migraphx::parse_onnx("clip_test_op11_no_args1.onnx");

    EXPECT(p == prog);
}

904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
TEST_CASE(clip_test_args_type_mismatch)
{
    migraphx::program p;
    auto* mm     = p.get_main_module();
    auto min_val = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {1, 3}}, {1.5, 2.5, 3.5}});
    auto max_val = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {3, 1}}, {2, 3, 4}});

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 3}});
    min_val =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), min_val);
    max_val =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 3}}}), max_val);
    max_val = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), max_val);
    auto r = mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
    mm->add_return({r});
    auto prog = migraphx::parse_onnx("clip_test_args_type_mismatch.onnx");
    EXPECT(p == prog);
}

926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
TEST_CASE(clip_dyn_min_max_test)
{
    migraphx::program p;
    auto* mm                                            = p.get_main_module();
    auto min_val                                        = mm->add_literal(0.0f);
    auto max_val                                        = mm->add_literal(6.0f);
    std::vector<migraphx::shape::dynamic_dimension> dds = {{2, 8, {3}}};
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, dds});
    min_val = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_dyn_dims", to_value(dds)}}), min_val, l0);
    max_val = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_dyn_dims", to_value(dds)}}), max_val, l0);
    auto ret = mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {2, 8, {3}};
    auto prog                     = parse_onnx("clip_dyn_min_max_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(clip_dyn_min_only_test)
{
    migraphx::program p;
    auto* mm                                            = p.get_main_module();
    auto min_val                                        = mm->add_literal(0.0f);
    std::vector<migraphx::shape::dynamic_dimension> dds = {{2, 8, {3}}};
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, dds});
    min_val = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_dyn_dims", to_value(dds)}}), min_val, l0);
    auto ret = mm->add_instruction(migraphx::make_op("max"), l0, min_val);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {2, 8, {3}};
    auto prog                     = parse_onnx("clip_dyn_min_only_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
967
TEST_CASE(concat_test)
Shucai Xiao's avatar
Shucai Xiao committed
968
969
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
970
971
972
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 4, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {7, 4, 3}});
973
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
974
    auto prog = optimize_onnx("concat_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
975
976
977
978

    EXPECT(p == prog);
}

979
980
981
982
983
TEST_CASE(concat_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
984
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {1, 4}, {3, 3}}});
985
    auto l1 = mm->add_parameter(
986
        "1", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {1, 4}, {3, 3}}});
987
988
989
990
991
    auto ret = mm->add_instruction(migraphx::make_op("concat"), l0, l1);

    mm->add_return({ret});

    migraphx::onnx_options options;
992
    options.default_dyn_dim_value = {1, 4};
993
994
995
996
997
    auto prog                     = parse_onnx("concat_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
998
TEST_CASE(constant_test)
Shucai Xiao's avatar
Shucai Xiao committed
999
1000
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1001
1002
1003
    auto* mm = p.get_main_module();
    mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {0, 1, 2}});
Shucai Xiao's avatar
Shucai Xiao committed
1004
    auto prog = optimize_onnx("constant_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
1005
1006
1007
1008

    EXPECT(p == prog);
}

1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
TEST_CASE(constant_value_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type, {1}}, {1.0f}});
    auto prog = optimize_onnx("constant_value_float_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(constant_value_floats_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {1.0f, 2.0f, 3.0f}});
    auto prog = optimize_onnx("constant_value_floats_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(constant_value_int_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {1}}, {1}});
    auto prog = optimize_onnx("constant_value_int_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(constant_value_ints_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {3}}, {1, 2, 3}});
    auto prog = optimize_onnx("constant_value_ints_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(constant_no_attributes_test)
{
    EXPECT(test::throws([&] { optimize_onnx("constant_no_attributes_test.onnx"); }));
}

TEST_CASE(constant_multiple_attributes_test)
{
    EXPECT(test::throws([&] { optimize_onnx("constant_multiple_attributes_test.onnx"); }));
}

Khalique's avatar
Khalique committed
1061
TEST_CASE(constant_fill_test)
Shucai Xiao's avatar
Shucai Xiao committed
1062
{
Khalique's avatar
Khalique committed
1063
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1064
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
1065
1066
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> value(s.elements(), 1.0);
Shucai Xiao's avatar
Shucai Xiao committed
1067
    mm->add_literal(migraphx::literal{s, value});
Shucai Xiao's avatar
Shucai Xiao committed
1068
    auto prog = optimize_onnx("constant_fill_test.onnx");
Khalique's avatar
Khalique committed
1069
1070
1071
1072
1073
1074
1075

    EXPECT(p == prog);
}

TEST_CASE(constant_fill_input_as_shape_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1076
1077
    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(migraphx::literal{{migraphx::shape::int32_type, {2}}, {2, 3}});
Khalique's avatar
Khalique committed
1078
1079
1080
1081
1082
    std::vector<std::size_t> dims(l0->get_shape().elements());
    migraphx::literal ls = l0->get_literal();
    ls.visit([&](auto s) { dims.assign(s.begin(), s.end()); });
    migraphx::shape s{migraphx::shape::float_type, dims};
    std::vector<float> value(s.elements(), 1.0);
Shucai Xiao's avatar
Shucai Xiao committed
1083
    mm->add_literal(migraphx::literal{s, value});
Shucai Xiao's avatar
Shucai Xiao committed
1084
    auto prog = optimize_onnx("constant_fill_input_as_shape_test.onnx");
Khalique's avatar
Khalique committed
1085
1086

    EXPECT(p == prog);
Shucai Xiao's avatar
Shucai Xiao committed
1087
1088
}

Khalique's avatar
Khalique committed
1089
TEST_CASE(constant_scalar_test)
1090
1091
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1092
1093
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {1}}, {1}});
Shucai Xiao's avatar
Shucai Xiao committed
1094
    auto prog = optimize_onnx("constant_scalar_test.onnx");
1095
1096
1097
1098

    EXPECT(p == prog);
}

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
TEST_CASE(constant_empty_scalar_int64_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
    auto prog = optimize_onnx("constant_empty_scalar_int64_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(constant_one_val_int64_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {1}}, {1}});
    auto prog = optimize_onnx("constant_one_val_int64_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
TEST_CASE(const_of_shape_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape output_dims_shape(migraphx::shape::int64_type, {3});
    mm->add_literal(migraphx::literal(output_dims_shape, {2, 3, 4}));
    migraphx::shape output_shape{migraphx::shape::float_type, {2, 3, 4}};
    std::vector<float> vec(output_shape.elements(), 0.0);
    mm->add_literal(migraphx::literal(output_shape, vec));

    auto prog = optimize_onnx("const_of_shape_default_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1133
TEST_CASE(const_of_shape_empty_input_test)
Shucai Xiao's avatar
Shucai Xiao committed
1134
1135
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1136
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
1137
    mm->add_literal(migraphx::literal(migraphx::shape::int64_type));
Khalique's avatar
Khalique committed
1138
1139
    migraphx::shape s(migraphx::shape::int64_type, {1}, {0});
    std::vector<int64_t> vec(s.elements(), 10);
Shucai Xiao's avatar
Shucai Xiao committed
1140
    mm->add_literal(migraphx::literal(s, vec));
Shucai Xiao's avatar
Shucai Xiao committed
1141

Shucai Xiao's avatar
Shucai Xiao committed
1142
    auto prog = optimize_onnx("const_of_shape_empty_input_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
1143
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
1144
1145
}

Khalique's avatar
Khalique committed
1146
TEST_CASE(const_of_shape_float_test)
1147
1148
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1149
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
1150
    migraphx::shape ss(migraphx::shape::int64_type, {3});
Shucai Xiao's avatar
Shucai Xiao committed
1151
    mm->add_literal(migraphx::literal(ss, {2, 3, 4}));
Khalique's avatar
Khalique committed
1152
1153
    migraphx::shape s(migraphx::shape::float_type, {2, 3, 4});
    std::vector<float> vec(s.elements(), 10.0f);
Shucai Xiao's avatar
Shucai Xiao committed
1154
    mm->add_literal(migraphx::literal(s, vec));
1155

Shucai Xiao's avatar
Shucai Xiao committed
1156
    auto prog = optimize_onnx("const_of_shape_float_test.onnx");
1157
1158
1159
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1160
TEST_CASE(const_of_shape_int64_test)
1161
1162
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1163
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
1164
1165
    // output_dims
    migraphx::shape ss(migraphx::shape::int64_type, {3});
Shucai Xiao's avatar
Shucai Xiao committed
1166
    mm->add_literal(migraphx::literal(ss, {2, 3, 4}));
Charlie Lin's avatar
Charlie Lin committed
1167
    // constant shape literal
Khalique's avatar
Khalique committed
1168
1169
    migraphx::shape s(migraphx::shape::int64_type, {2, 3, 4});
    std::vector<int64_t> vec(s.elements(), 10);
Shucai Xiao's avatar
Shucai Xiao committed
1170
    mm->add_literal(migraphx::literal(s, vec));
1171

Shucai Xiao's avatar
Shucai Xiao committed
1172
    auto prog = optimize_onnx("const_of_shape_int64_test.onnx");
1173
1174
1175
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1176
TEST_CASE(const_of_shape_no_value_attr_test)
1177
1178
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1179
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
1180
    migraphx::shape ss(migraphx::shape::int64_type, {3});
Shucai Xiao's avatar
Shucai Xiao committed
1181
    mm->add_literal(migraphx::literal(ss, {2, 3, 4}));
Khalique's avatar
Khalique committed
1182
1183
    migraphx::shape s(migraphx::shape::float_type, {2, 3, 4});
    std::vector<float> vec(s.elements(), 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
1184
    mm->add_literal(migraphx::literal(s, vec));
1185

Shucai Xiao's avatar
Shucai Xiao committed
1186
    auto prog = optimize_onnx("const_of_shape_no_value_attr_test.onnx");
1187
1188
1189
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
TEST_CASE(const_of_shape_dyn_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto od_param =
        mm->add_parameter("output_dims", migraphx::shape{migraphx::shape::int64_type, {3}});
    auto alloc_ins = mm->add_instruction(
        migraphx::make_op("allocate", {{"buf_type", migraphx::shape::float_type}}), od_param);
    migraphx::shape dv_shape(migraphx::shape::float_type, {1}, {0});
    auto dv_lit   = mm->add_literal(migraphx::literal(dv_shape, {10}));
    auto fill_ins = mm->add_instruction(migraphx::make_op("fill"), dv_lit, alloc_ins);
    mm->add_return({fill_ins});

    migraphx::onnx_options options;
    auto prog = parse_onnx("const_of_shape_dyn_float_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(const_of_shape_dyn_int64_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto od_param =
        mm->add_parameter("output_dims", migraphx::shape{migraphx::shape::int64_type, {3}});
    auto alloc_ins = mm->add_instruction(
        migraphx::make_op("allocate", {{"buf_type", migraphx::shape::int64_type}}), od_param);
    migraphx::shape dv_shape(migraphx::shape::int64_type, {1}, {0});
    auto dv_lit   = mm->add_literal(migraphx::literal(dv_shape, {10}));
    auto fill_ins = mm->add_instruction(migraphx::make_op("fill"), dv_lit, alloc_ins);
    mm->add_return({fill_ins});

    migraphx::onnx_options options;
    auto prog = parse_onnx("const_of_shape_dyn_int64_test.onnx", options);
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1226
TEST_CASE(conv_autopad_fail_test)
1227
{
Shucai Xiao's avatar
Shucai Xiao committed
1228
    EXPECT(test::throws([&] { optimize_onnx("conv_autopad_fail_test.onnx"); }));
Khalique's avatar
Khalique committed
1229
}
1230

1231
1232
1233
TEST_CASE(conv_1d_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1234
1235
1236
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3}});
1237
1238
1239
1240
    mm->add_instruction(
        migraphx::make_op("convolution", {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
        l0,
        l1);
1241
1242
1243
1244
1245
1246
1247
1248

    auto prog = optimize_onnx("conv_1d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(conv_3d_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1249
1250
1251
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5, 5, 5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3, 3}});
1252
1253
1254
1255
1256
    mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0, 0}}, {"stride", {1, 1, 1}}, {"dilation", {1, 1, 1}}}),
        l0,
        l1);
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266

    auto prog = optimize_onnx("conv_3d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(conv_attr_fail_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("conv_attr_fail_test.onnx"); }));
}

1267
1268
1269
TEST_CASE(conv_autopad_same_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1270
1271
1272
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 32, 32}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3}});
1273
    migraphx::op::convolution op;
1274
    op.padding = {1, 1, 1, 1};
Shucai Xiao's avatar
Shucai Xiao committed
1275
    mm->add_instruction(op, l0, l1);
1276
1277
1278
1279
1280

    auto prog = optimize_onnx("conv_autopad_same_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1281
1282
1283
TEST_CASE(conv_bias_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1284
1285
1286
1287
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 32, 32}});
    auto l1       = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 5, 5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::float_type, {1}});
Khalique's avatar
Khalique committed
1288
    uint64_t axis = 1;
1289
1290
    auto l3       = mm->add_instruction(migraphx::make_op("convolution"), l0, l1);
    auto l4       = mm->add_instruction(
1291
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1292
    mm->add_instruction(migraphx::make_op("add"), l3, l4);
Khalique's avatar
Khalique committed
1293

Shucai Xiao's avatar
Shucai Xiao committed
1294
    auto prog = optimize_onnx("conv_bias_test.onnx");
Khalique's avatar
Khalique committed
1295
1296
1297
1298
1299
1300
    EXPECT(p == prog);
}

TEST_CASE(conv_bn_relu_maxpool_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1301
1302
1303
1304
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 32, 32}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 5, 5}});
    auto l2  = mm->add_parameter("2", {migraphx::shape::float_type, {1}});
Khalique's avatar
Khalique committed
1305

1306
1307
1308
1309
1310
1311
1312
    auto p3 = mm->add_parameter("3", {migraphx::shape::float_type, {1}});
    auto p4 = mm->add_parameter("4", {migraphx::shape::float_type, {1}});
    auto p5 = mm->add_parameter("5", {migraphx::shape::float_type, {1}});
    auto p6 = mm->add_parameter("6", {migraphx::shape::float_type, {1}});

    auto eps = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-5f}});

Khalique's avatar
Khalique committed
1313
    uint64_t axis = 1;
kahmed10's avatar
kahmed10 committed
1314
1315
1316
    auto l3 =
        mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0, 0, 0}}}), l0, l1);
    auto l4 = mm->add_instruction(
1317
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1318
    auto l5 = mm->add_instruction(migraphx::make_op("add"), l3, l4);
1319
1320
1321
1322
1323
1324

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), p3);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), p4);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), p5);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), p6);

1325
1326
1327
1328
1329
1330
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {l5, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
    auto l6         = add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});
1331

1332
    auto l7 = mm->add_instruction(migraphx::make_op("relu"), l6);
1333
1334
1335
1336
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 0, 0}},
                                           {"stride", {2, 2}},
1337
1338
                                           {"lengths", {2, 2}},
                                           {"dilations", {1, 1}}}),
1339
                        l7);
Khalique's avatar
Khalique committed
1340

Shucai Xiao's avatar
Shucai Xiao committed
1341
    auto prog = optimize_onnx("conv_bn_relu_maxpool_test.onnx");
Khalique's avatar
Khalique committed
1342
1343
1344
    EXPECT(p == prog);
}

1345
1346
1347
1348
TEST_CASE(conv_dynamic_batch_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1349
1350
    auto l0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 6}, {3, 3}, {5, 5}, {5, 5}}});
1351
1352
1353
1354
1355
1356
1357
1358
1359
    auto l1 = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3}});
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1360
    options.default_dyn_dim_value = {1, 6};
1361
1362
1363
1364
1365

    auto prog = migraphx::parse_onnx("conv_dynamic_batch_test.onnx", options);
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
1366
1367
1368
1369
TEST_CASE(conv_dynamic_bias_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1370
1371
    auto x0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 6}, {3, 3}, {32, 32}, {32, 32}}});
Charlie Lin's avatar
Charlie Lin committed
1372
1373
1374
1375
1376
1377
1378
1379
    auto x1 = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 5, 5}});
    auto x2 = mm->add_parameter("2", {migraphx::shape::float_type, {1}});
    auto x3 = mm->add_instruction(migraphx::make_op("convolution"), x0, x1);
    auto x4 = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), x2, x3);
    auto x5 = mm->add_instruction(migraphx::make_op("add"), x3, x4);
    mm->add_return({x5});

    migraphx::onnx_options options;
1380
    options.default_dyn_dim_value = {1, 6};
Charlie Lin's avatar
Charlie Lin committed
1381
1382
1383
1384
    auto prog                     = migraphx::parse_onnx("conv_dynamic_bias_test.onnx", options);
    EXPECT(p == prog);
}

1385
1386
1387
1388
TEST_CASE(conv_dynamic_img_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1389
1390
    auto l0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {5, 10}, {5, 10}}});
1391
1392
1393
1394
1395
1396
1397
1398
1399
    auto l1 = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3}});
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1400
    options.default_dyn_dim_value = {5, 10};
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410

    auto prog = migraphx::parse_onnx("conv_dynamic_img_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_dynamic_weights_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5, 5}});
1411
1412
    auto l1 =
        mm->add_parameter("1", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}});
1413
1414
1415
1416
1417
1418
1419
1420
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1421
    options.default_dyn_dim_value = {2, 4};
1422
1423
1424
1425
1426
1427
1428
1429
1430

    auto prog = migraphx::parse_onnx("conv_dynamic_weights_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_dynamic_img_and_weights_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1431
1432
1433
1434
    auto l0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {5, 10}, {5, 10}}});
    auto l1 =
        mm->add_parameter("1", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}});
1435
1436
1437
1438
1439
1440
1441
1442
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1443
1444
    options.default_dyn_dim_value   = {5, 10};
    options.map_dyn_input_dims["1"] = {{1, 1}, {3, 3}, {2, 4}, {2, 4}};
1445
1446
1447
1448
1449
1450
1451
1452
1453

    auto prog = migraphx::parse_onnx("conv_dynamic_img_and_weights_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_dynamic_batch_same_upper)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1454
1455
    auto l0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 10}, {3, 3}, {5, 5}, {5, 5}}});
1456
    auto l1 = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3}});
1457
1458
1459
1460
1461
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {1, 1, 1, 1}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
        l0,
        l1);
1462
1463
1464
    mm->add_return({c0});

    migraphx::onnx_options options;
1465
    options.default_dyn_dim_value = {1, 10};
1466
1467
1468
1469
1470
1471
1472
1473
1474

    auto prog = migraphx::parse_onnx("conv_dynamic_batch_same_upper_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_dynamic_img_same_upper)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
1475
1476
    auto l0 =
        mm->add_parameter("0", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {5, 10}, {5, 10}}});
1477
1478
1479
1480
1481
1482
    auto l1 = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 3, 3}});
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}},
                           {"stride", {1, 1}},
                           {"dilation", {1, 1}},
1483
                           {"padding_mode", migraphx::op::padding_mode_t::same_upper}}),
1484
1485
1486
1487
1488
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1489
    options.default_dyn_dim_value = {5, 10};
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499

    auto prog = migraphx::parse_onnx("conv_dynamic_img_same_upper_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_dynamic_kernel_same_lower)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 5, 5}});
1500
1501
    auto l1 =
        mm->add_parameter("1", {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}});
1502
1503
1504
1505
1506
    auto c0 = mm->add_instruction(
        migraphx::make_op("convolution",
                          {{"padding", {0, 0}},
                           {"stride", {1, 1}},
                           {"dilation", {1, 1}},
1507
                           {"padding_mode", migraphx::op::padding_mode_t::same_lower}}),
1508
1509
1510
1511
1512
        l0,
        l1);
    mm->add_return({c0});

    migraphx::onnx_options options;
1513
    options.default_dyn_dim_value = {2, 4};
1514
1515
1516
1517
    auto prog = migraphx::parse_onnx("conv_dynamic_kernel_same_lower_test.onnx", options);
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1518
1519
1520
TEST_CASE(conv_relu_maxpool_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1521
1522
1523
1524
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 32, 32}});
    auto l1       = mm->add_parameter("1", {migraphx::shape::float_type, {1, 3, 5, 5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::float_type, {1}});
Khalique's avatar
Khalique committed
1525
    uint64_t axis = 1;
kahmed10's avatar
kahmed10 committed
1526
1527
1528
    auto l3 =
        mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0, 0, 0}}}), l0, l1);
    auto l4 = mm->add_instruction(
1529
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1530
1531
    auto l5 = mm->add_instruction(migraphx::make_op("add"), l3, l4);
    auto l6 = mm->add_instruction(migraphx::make_op("relu"), l5);
1532
1533
1534
1535
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 0, 0}},
                                           {"stride", {2, 2}},
1536
1537
                                           {"lengths", {2, 2}},
                                           {"dilations", {1, 1}}}),
1538
                        l6);
Khalique's avatar
Khalique committed
1539

Shucai Xiao's avatar
Shucai Xiao committed
1540
    auto prog = optimize_onnx("conv_relu_maxpool_test.onnx");
Khalique's avatar
Khalique committed
1541
1542
1543
1544
1545
1546
    EXPECT(p == prog);
}

TEST_CASE(conv_relu_maxpool_x2_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1547
1548
1549
1550
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("0", {migraphx::shape::float_type, {1, 3, 32, 32}});
    auto l1       = mm->add_parameter("1", {migraphx::shape::float_type, {5, 3, 5, 5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::float_type, {5}});
Khalique's avatar
Khalique committed
1551
    uint64_t axis = 1;
kahmed10's avatar
kahmed10 committed
1552
1553
1554
    auto l3 =
        mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0, 0, 0}}}), l0, l1);
    auto l4 = mm->add_instruction(
1555
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1556
1557
    auto l5 = mm->add_instruction(migraphx::make_op("add"), l3, l4);
    auto l6 = mm->add_instruction(migraphx::make_op("relu"), l5);
1558
1559
1560
1561
    auto l7 = mm->add_instruction(migraphx::make_op("pooling",
                                                    {{"mode", migraphx::op::pooling_mode::max},
                                                     {"padding", {0, 0, 0, 0}},
                                                     {"stride", {2, 2}},
1562
1563
                                                     {"lengths", {2, 2}},
                                                     {"dilations", {1, 1}}}),
1564
                                  l6);
Shucai Xiao's avatar
Shucai Xiao committed
1565

kahmed10's avatar
kahmed10 committed
1566
1567
1568
1569
    auto l8 = mm->add_parameter("3", {migraphx::shape::float_type, {1, 5, 5, 5}});
    auto l9 = mm->add_parameter("4", {migraphx::shape::float_type, {1}});
    auto l10 =
        mm->add_instruction(migraphx::make_op("convolution", {{"padding", {0, 0, 0, 0}}}), l7, l8);
1570
    auto l11 = mm->add_instruction(
1571
1572
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l10->get_shape().lens()}}),
        l9);
1573
1574
    auto l12 = mm->add_instruction(migraphx::make_op("add"), l10, l11);
    auto l13 = mm->add_instruction(migraphx::make_op("relu"), l12);
1575
1576
1577
1578
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 0, 0}},
                                           {"stride", {2, 2}},
1579
1580
                                           {"lengths", {2, 2}},
                                           {"dilations", {1, 1}}}),
1581
                        l13);
Khalique's avatar
Khalique committed
1582

Shucai Xiao's avatar
Shucai Xiao committed
1583
    auto prog = optimize_onnx("conv_relu_maxpool_x2_test.onnx");
Khalique's avatar
Khalique committed
1584
1585
1586
1587

    EXPECT(p == prog);
}

1588
1589
1590
TEST_CASE(convinteger_bias_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1591
1592
1593
1594
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("0", {migraphx::shape::int8_type, {1, 3, 32, 32}});
    auto l1       = mm->add_parameter("1", {migraphx::shape::int8_type, {1, 3, 5, 5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::int32_type, {1}});
1595
    uint64_t axis = 1;
1596
1597
    auto l3       = mm->add_instruction(migraphx::make_op("quant_convolution"), l0, l1);
    auto l4       = mm->add_instruction(
1598
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1599
    mm->add_instruction(migraphx::make_op("add"), l3, l4);
1600
1601
1602
1603
1604

    auto prog = optimize_onnx("convinteger_bias_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1605
TEST_CASE(cos_test)
1606
1607
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1608
1609
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
1610
    mm->add_instruction(migraphx::make_op("cos"), input);
1611

Shucai Xiao's avatar
Shucai Xiao committed
1612
    auto prog = optimize_onnx("cos_test.onnx");
1613
1614
1615
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1616
TEST_CASE(cosh_test)
1617
1618
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1619
1620
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1}});
1621
    mm->add_instruction(migraphx::make_op("cosh"), input);
1622

Shucai Xiao's avatar
Shucai Xiao committed
1623
    auto prog = optimize_onnx("cosh_test.onnx");
1624
1625
1626
1627

    EXPECT(p == prog);
}

1628
TEST_CASE(conv_transpose_test)
kahmed10's avatar
kahmed10 committed
1629
1630
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1631
1632
1633
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 1, 3, 3}});
1634
    mm->add_instruction(migraphx::make_op("convolution_backwards"), l0, l1);
kahmed10's avatar
kahmed10 committed
1635

1636
    auto prog = optimize_onnx("conv_transpose_test.onnx");
kahmed10's avatar
kahmed10 committed
1637
1638
1639
    EXPECT(p == prog);
}

1640
TEST_CASE(conv_transpose_bias_test)
kahmed10's avatar
kahmed10 committed
1641
1642
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1643
1644
1645
1646
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1       = mm->add_parameter("w", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l2       = mm->add_parameter("b", {migraphx::shape::float_type, {1}});
kahmed10's avatar
kahmed10 committed
1647
    uint64_t axis = 1;
1648
    auto l3       = mm->add_instruction(migraphx::make_op("convolution_backwards"), l0, l1);
1649
    auto l4       = mm->add_instruction(
1650
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l3->get_shape().lens()}}), l2);
1651
    mm->add_instruction(migraphx::make_op("add"), l3, l4);
kahmed10's avatar
kahmed10 committed
1652

1653
    auto prog = optimize_onnx("conv_transpose_bias_test.onnx");
kahmed10's avatar
kahmed10 committed
1654
1655
1656
    EXPECT(p == prog);
}

1657
TEST_CASE(conv_transpose_input_pads_strides_test)
kahmed10's avatar
kahmed10 committed
1658
1659
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1660
1661
1662
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3}});
1663
    mm->add_instruction(
1664
1665
1666
        migraphx::make_op("convolution_backwards", {{"padding", {1, 1}}, {"stride", {3, 2}}}),
        l0,
        l1);
kahmed10's avatar
kahmed10 committed
1667

1668
    auto prog = optimize_onnx("conv_transpose_input_pads_strides_test.onnx");
kahmed10's avatar
kahmed10 committed
1669
1670
1671
    EXPECT(p == prog);
}

1672
TEST_CASE(conv_transpose_input_pads_asymm_test)
kahmed10's avatar
kahmed10 committed
1673
1674
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1675
1676
1677
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3}});
1678
    auto l2  = mm->add_instruction(
1679
1680
1681
        migraphx::make_op("convolution_backwards", {{"padding", {0, 0}}, {"stride", {3, 2}}}),
        l0,
        l1);
1682
1683
    mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {0, 0}}, {"ends", {8, 6}}}), l2);
kahmed10's avatar
kahmed10 committed
1684

1685
    auto prog = optimize_onnx("conv_transpose_input_pads_asymm_test.onnx");
kahmed10's avatar
kahmed10 committed
1686
1687
1688
    EXPECT(p == prog);
}

1689
TEST_CASE(conv_transpose_input_pads_asymm_1d_test)
kahmed10's avatar
kahmed10 committed
1690
1691
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1692
1693
1694
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3}});
1695
    auto l2  = mm->add_instruction(
1696
        migraphx::make_op("convolution_backwards",
1697
                           {{"padding", {0}}, {"stride", {2}}, {"dilation", {1}}}),
1698
1699
1700
1701
        l0,
        l1);
    mm->add_instruction(migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {6}}}),
                        l2);
kahmed10's avatar
kahmed10 committed
1702

1703
    auto prog = optimize_onnx("conv_transpose_input_pads_asymm_1d_test.onnx");
kahmed10's avatar
kahmed10 committed
1704
1705
1706
    EXPECT(p == prog);
}

1707
TEST_CASE(conv_transpose_output_padding_test)
kahmed10's avatar
kahmed10 committed
1708
1709
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1710
1711
1712
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3}});
1713
    auto l2  = mm->add_instruction(
1714
1715
1716
        migraphx::make_op("convolution_backwards", {{"padding", {0, 0}}, {"stride", {3, 2}}}),
        l0,
        l1);
1717
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 0, 0, 0, 0, 0, 1, 1}}}), l2);
kahmed10's avatar
kahmed10 committed
1718

1719
    auto prog = optimize_onnx("conv_transpose_output_padding_test.onnx");
kahmed10's avatar
kahmed10 committed
1720
1721
1722
    EXPECT(p == prog);
}

1723
TEST_CASE(conv_transpose_output_padding_3d_test)
kahmed10's avatar
kahmed10 committed
1724
1725
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1726
1727
1728
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3, 3}});
1729
    auto l2  = mm->add_instruction(
1730
        migraphx::make_op("convolution_backwards",
1731
                           {{"padding", {0, 0, 0}}, {"stride", {3, 2, 2}}, {"dilation", {1, 1, 1}}}),
1732
1733
1734
        l0,
        l1);
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 0, 0, 0, 0, 0, 0, 1, 1, 1}}}), l2);
kahmed10's avatar
kahmed10 committed
1735

1736
    auto prog = optimize_onnx("conv_transpose_output_padding_3d_test.onnx");
kahmed10's avatar
kahmed10 committed
1737
1738
1739
    EXPECT(p == prog);
}

1740
TEST_CASE(conv_transpose_output_shape_test)
kahmed10's avatar
kahmed10 committed
1741
1742
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1743
1744
1745
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3}});
1746
    auto l2  = mm->add_instruction(
1747
1748
1749
        migraphx::make_op("convolution_backwards", {{"padding", {0, 0}}, {"stride", {3, 2}}}),
        l0,
        l1);
1750
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 0, 0, 0, 0, 0, 1, 1}}}), l2);
kahmed10's avatar
kahmed10 committed
1751

1752
    auto prog = optimize_onnx("conv_transpose_output_shape_test.onnx");
kahmed10's avatar
kahmed10 committed
1753
1754
1755
    EXPECT(p == prog);
}

1756
TEST_CASE(conv_transpose_output_shape_3d_test)
kahmed10's avatar
kahmed10 committed
1757
1758
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1759
1760
1761
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 1, 3, 3, 3}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 2, 3, 3, 3}});
1762
    auto l2  = mm->add_instruction(
1763
        migraphx::make_op("convolution_backwards",
1764
                           {{"padding", {0, 0, 0}}, {"stride", {3, 2, 2}}, {"dilation", {1, 1, 1}}}),
1765
1766
1767
        l0,
        l1);
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 0, 0, 0, 0, 0, 0, 1, 1, 1}}}), l2);
kahmed10's avatar
kahmed10 committed
1768

1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
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
    auto prog = optimize_onnx("conv_transpose_output_shape_3d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(conv_transpose_auto_pad_error)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("conv_transpose_auto_pad_test.onnx"); }));
}

TEST_CASE(conv_transpose_dyn_asym_padding_error)
{
    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 4};
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("conv_transpose_dyn_asym_padding_test.onnx", options); }));
}

TEST_CASE(conv_transpose_dyn_output_shape_error)
{
    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 4};
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("conv_transpose_dyn_output_shape_test.onnx", options); }));
}

TEST_CASE(conv_transpose_dyn_batch_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 =
        mm->add_parameter("x", {migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {3, 3}}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto ret = mm->add_instruction(migraphx::make_op("convolution_backwards"), l0, l1);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 4};
    auto prog                     = parse_onnx("conv_transpose_dyn_batch_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(conv_transpose_dyn_img_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 =
        mm->add_parameter("x", {migraphx::shape::float_type, {{1, 1}, {1, 1}, {3, 6}, {3, 6}}});
    auto l1  = mm->add_parameter("w", {migraphx::shape::float_type, {1, 1, 3, 3}});
    auto ret = mm->add_instruction(migraphx::make_op("convolution_backwards"), l0, l1);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {3, 6};
    auto prog                     = parse_onnx("conv_transpose_dyn_img_test.onnx", options);
kahmed10's avatar
kahmed10 committed
1823
1824
1825
    EXPECT(p == prog);
}

1826
1827
1828
1829
1830
1831
1832
1833
1834
TEST_CASE(depthtospace_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {2, 8, 5, 5}});
    auto tmp1 =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 2, 2, 5, 5}}}), l0);
    auto tmp2 = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 3, 4, 1, 5, 2}}}), tmp1);
1835
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 10, 10}}}), tmp2);
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
    auto prog = optimize_onnx("depthtospace_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(depthtospace_crd_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {2, 8, 5, 5}});
    auto tmp1 =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 2, 2, 5, 5}}}), l0);
    auto tmp2 = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 1, 4, 2, 5, 3}}}), tmp1);
1849
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 10, 10}}}), tmp2);
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
    auto prog = optimize_onnx("depthtospace_crd_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(depthtospace_simple_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 8, 2, 3}});
    auto tmp1 =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 2, 2, 2, 3}}}), l0);
    auto tmp2 = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 3, 4, 1, 5, 2}}}), tmp1);
1863
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 4, 6}}}), tmp2);
1864
1865
1866
1867
    auto prog = optimize_onnx("depthtospace_simple_test.onnx");
    EXPECT(p == prog);
}

1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
TEST_CASE(dynamicquantizelinear_2d_test)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto x_dims = {3, 4};
    auto x_type = migraphx::shape::float_type;
    auto x      = mm->add_parameter("x", {x_type, x_dims});

    auto l0         = mm->add_literal({0.f});
    auto x_reshaped = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), x);
    x_reshaped = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x_reshaped, l0);

    auto q_range = mm->add_literal(
        migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::max()}});

    auto max_x = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {0}}}), x_reshaped);
    auto min_x = mm->add_instruction(migraphx::make_op("reduce_min", {{"axes", {0}}}), x_reshaped);

    auto sub0    = mm->add_instruction(migraphx::make_op("sub"), max_x, min_x);
    auto y_scale = mm->add_instruction(migraphx::make_op("div"), sub0, q_range);

    auto q_min = mm->add_literal(
        migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::min()}});
    auto q_max = mm->add_literal(
        migraphx::literal{migraphx::shape{x_type}, {std::numeric_limits<uint8_t>::max()}});
    auto sub1         = mm->add_instruction(migraphx::make_op("sub"), q_min, min_x);
    auto interm_zp    = mm->add_instruction(migraphx::make_op("div"), sub1, y_scale);
    auto saturate     = mm->add_instruction(migraphx::make_op("clip"), interm_zp, q_min, q_max);
    auto round        = mm->add_instruction(migraphx::make_op("nearbyint"), saturate);
    auto y_zero_point = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::uint8_type}}), round);

    auto scale_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", x_dims}}), y_scale);

    auto y_pt_c_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x_dims}}), y_zero_point);

    mm->add_instruction(migraphx::make_op("quantizelinear"), x, scale_y_bcast, y_pt_c_bcast);

    auto prog = optimize_onnx("dynamicquantizelinear_2d_test.onnx");
    EXPECT(p == prog);
}

Umang Yadav's avatar
Umang Yadav committed
1912
1913
1914
1915
1916
1917
1918
1919
1920
TEST_CASE(spacetodepth_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {2, 2, 10, 10}});
    auto tmp1 =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 2, 5, 2, 5, 2}}}), l0);
    auto tmp2 = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 3, 5, 1, 2, 4}}}), tmp1);
1921
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 8, 5, 5}}}), tmp2);
Umang Yadav's avatar
Umang Yadav committed
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
    auto prog = optimize_onnx("spacetodepth_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(spacetodepth_simple_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 2, 4, 6}});
    auto tmp1 =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 2, 2, 3, 2}}}), l0);
    auto tmp2 = mm->add_instruction(
        migraphx::make_op("transpose", {{"permutation", {0, 3, 5, 1, 2, 4}}}), tmp1);
1935
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 8, 2, 3}}}), tmp2);
Umang Yadav's avatar
Umang Yadav committed
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
    auto prog = optimize_onnx("spacetodepth_simple_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(spacetodepth_invalid_blocksize)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("spacetodepth_invalid_blocksize_test.onnx"); }));
}

TEST_CASE(spacetodepth_nondivisibility_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("spacetodepth_nondivisibility_test.onnx"); }));
}

1950
1951
1952
1953
1954
1955
1956
TEST_CASE(dequantizelinear_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::int8_type, {5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1}});
    auto l1_mbcast =
1957
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
1958
    auto dequant = mm->add_instruction(
1959
        migraphx::make_op("convert",
turneram's avatar
turneram committed
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l0);
    mm->add_instruction(migraphx::make_op("mul"), dequant, l1_mbcast);

    auto prog = optimize_onnx("dequantizelinear_test.onnx", true);
    EXPECT(p.sort() == prog.sort());
}

TEST_CASE(dequantizelinear_zero_point_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::int8_type, {5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1}});
    auto l2  = mm->add_parameter("2", {migraphx::shape::int8_type, {1}});
    auto l1_mbcast =
1976
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
1977
    auto l2_mbcast =
1978
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l2);
turneram's avatar
turneram committed
1979
    l2_mbcast = mm->add_instruction(
1980
        migraphx::make_op("convert",
turneram's avatar
turneram committed
1981
1982
1983
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l2_mbcast);
    l0 = mm->add_instruction(
1984
1985
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
turneram's avatar
turneram committed
1986
        l0);
1987

turneram's avatar
turneram committed
1988
1989
    auto sub = mm->add_instruction(migraphx::make_op("sub"), l0, l2_mbcast);
    mm->add_instruction(migraphx::make_op("mul"), sub, l1_mbcast);
1990

turneram's avatar
turneram committed
1991
    auto prog = optimize_onnx("dequantizelinear_zero_point_test.onnx", true);
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
    EXPECT(p.sort() == prog.sort());
}

migraphx::program make_dequantizelinear_axis_prog()
{
    migraphx::program p;
    std::vector<size_t> input_lens{1, 1, 5, 1};
    int axis      = 2;
    auto* mm      = p.get_main_module();
    auto l0       = mm->add_parameter("0", {migraphx::shape::int8_type, input_lens});
    auto l1       = mm->add_parameter("1", {migraphx::shape::float_type, {5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::int8_type, {5}});
    auto l1_bcast = mm->add_instruction(
2005
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l1);
2006
    auto l2_bcast = mm->add_instruction(
2007
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l2);
2008
2009
    l2_bcast = mm->add_instruction(
        migraphx::make_op("convert",
turneram's avatar
turneram committed
2010
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
2011
2012
2013
2014
        l2_bcast);
    l0 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
turneram's avatar
turneram committed
2015
2016
        l0);
    auto sub = mm->add_instruction(migraphx::make_op("sub"), l0, l2_bcast);
2017

turneram's avatar
turneram committed
2018
    mm->add_instruction(migraphx::make_op("mul"), sub, l1_bcast);
2019
2020
2021
2022
2023
2024
2025
    return p;
}

TEST_CASE(dequantizelinear_axis_test)
{
    migraphx::program p = make_dequantizelinear_axis_prog();

turneram's avatar
turneram committed
2026
    auto prog = optimize_onnx("dequantizelinear_axis_test.onnx", true);
2027
2028
2029
2030
2031
2032
2033
    EXPECT(p.sort() == prog.sort());
}

TEST_CASE(dequantizelinear_neg_axis_test)
{
    migraphx::program p = make_dequantizelinear_axis_prog();

turneram's avatar
turneram committed
2034
    auto prog = optimize_onnx("dequantizelinear_neg_axis_test.onnx", true);
2035
2036
2037
    EXPECT(p.sort() == prog.sort());
}

Khalique's avatar
Khalique committed
2038
TEST_CASE(dropout_test)
Shucai Xiao's avatar
Shucai Xiao committed
2039
2040
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2041
2042
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}});
2043
    auto out   = mm->add_instruction(migraphx::make_op("identity"), input);
2044
2045
    migraphx::shape s{migraphx::shape::bool_type, {1, 3, 2, 2}};
    std::vector<int8_t> vec(s.elements(), 1);
Shucai Xiao's avatar
Shucai Xiao committed
2046
2047
    mm->add_literal(migraphx::literal(s, vec));
    mm->add_return({out});
Shucai Xiao's avatar
Shucai Xiao committed
2048

2049
    auto prog = migraphx::parse_onnx("dropout_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
2050
2051
2052
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2053
2054
2055
TEST_CASE(elu_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2056
2057
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
2058
    mm->add_instruction(migraphx::make_op("elu", {{"alpha", 0.01}}), input);
Khalique's avatar
Khalique committed
2059

Shucai Xiao's avatar
Shucai Xiao committed
2060
    auto prog = optimize_onnx("elu_test.onnx");
Khalique's avatar
Khalique committed
2061
2062
2063
2064

    EXPECT(p == prog);
}

2065
2066
2067
TEST_CASE(embedding_bag_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2068
2069
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("weight", migraphx::shape{migraphx::shape::float_type, {4, 2}});
2070
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {3}}, {1, 0, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
2071
2072
    auto l1 = mm->add_literal(l);
    mm->add_literal(0);
2073
2074
2075
2076
2077
2078
    auto l4 = mm->add_instruction(migraphx::make_op("gather"), l0, l1);
    auto r1 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0}}}), l4);
    auto l5 = mm->add_instruction(migraphx::make_op("gather"), l0, l1);
    auto r2 = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0}}}), l5);
    auto l6 = mm->add_instruction(migraphx::make_op("gather"), l0, l1);
    auto r3 = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {0}}}), l6);
Shucai Xiao's avatar
Shucai Xiao committed
2079
    mm->add_return({r1, r2, r3});
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090

    auto prog = migraphx::parse_onnx("embedding_bag_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(embedding_bag_offset_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("embedding_bag_offset_test.onnx"); }));
}

2091
2092
2093
TEST_CASE(equal_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2094
    auto* mm = p.get_main_module();
2095
2096
2097
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};

Shucai Xiao's avatar
Shucai Xiao committed
2098
2099
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
2100
2101
2102
    auto eq     = mm->add_instruction(migraphx::make_op("equal"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
2103
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2104
        eq);
Shucai Xiao's avatar
Shucai Xiao committed
2105
    mm->add_return({ret});
2106
2107
2108
2109
2110
2111
2112
2113
2114

    auto prog = migraphx::parse_onnx("equal_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(equal_bool_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2115
    auto* mm = p.get_main_module();
2116
2117
2118
    migraphx::shape sf{migraphx::shape::float_type, {2, 3}};
    migraphx::shape sb{migraphx::shape::bool_type, {2, 3}};

Shucai Xiao's avatar
Shucai Xiao committed
2119
2120
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
2121
2122
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
2123
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2124
2125
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("equal"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
2126
    mm->add_return({ret});
2127
2128
2129
2130
2131
2132

    auto prog = migraphx::parse_onnx("equal_bool_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2133
TEST_CASE(erf_test)
2134
2135
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2136
2137
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
2138
    mm->add_instruction(migraphx::make_op("erf"), input);
Khalique's avatar
Khalique committed
2139

Shucai Xiao's avatar
Shucai Xiao committed
2140
    auto prog = optimize_onnx("erf_test.onnx");
Khalique's avatar
Khalique committed
2141
2142
2143
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2144
TEST_CASE(exp_test)
2145
2146
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2147
2148
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
2149
    mm->add_instruction(migraphx::make_op("exp"), input);
Khalique's avatar
Khalique committed
2150

Shucai Xiao's avatar
Shucai Xiao committed
2151
    auto prog = optimize_onnx("exp_test.onnx");
Khalique's avatar
Khalique committed
2152
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
2153
2154
}

Khalique's avatar
Khalique committed
2155
TEST_CASE(expand_test)
2156
2157
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2158
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
2159
    migraphx::shape s(migraphx::shape::float_type, {3, 1, 1});
Shucai Xiao's avatar
Shucai Xiao committed
2160
    auto param = mm->add_parameter("x", s);
Khalique's avatar
Khalique committed
2161
    migraphx::shape ss(migraphx::shape::int32_type, {4});
Shucai Xiao's avatar
Shucai Xiao committed
2162
    mm->add_literal(migraphx::literal(ss, {2, 3, 4, 5}));
2163
    mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), param);
Khalique's avatar
Khalique committed
2164

Shucai Xiao's avatar
Shucai Xiao committed
2165
    auto prog = optimize_onnx("expand_test.onnx");
Khalique's avatar
Khalique committed
2166
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
2167
2168
}

2169
2170
2171
migraphx::program create_external_data_prog()
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2172
    auto* mm = p.get_main_module();
2173
2174
2175
2176
    migraphx::shape s(migraphx::shape::float_type, {1, 1, 224, 224});
    migraphx::shape s2(migraphx::shape::float_type, {10, 1, 11, 11});
    std::vector<float> weight_data(1210, 1);
    std::vector<float> bias_data(10, 1);
Shucai Xiao's avatar
Shucai Xiao committed
2177
    auto bias = mm->add_literal(migraphx::literal({migraphx::shape::float_type, {10}}, bias_data));
kahmed10's avatar
kahmed10 committed
2178
2179
2180
2181
    auto weights = mm->add_literal(migraphx::literal(s2, weight_data));
    auto param   = mm->add_parameter("input", s);
    auto conv    = mm->add_instruction(
        migraphx::make_op("convolution", {{"padding", {0, 0, 0, 0}}}), param, weights);
Shucai Xiao's avatar
Shucai Xiao committed
2182
    auto bias_bcast = mm->add_instruction(
2183
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 10, 214, 214}}}), bias);
Shucai Xiao's avatar
Shucai Xiao committed
2184
    mm->add_instruction(migraphx::make_op("add"), conv, bias_bcast);
2185
2186
2187
    return p;
}

2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
TEST_CASE(external_constant_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{{migraphx::shape::int64_type, {3}}, {0, 1, 2}});

    auto prog = optimize_onnx("external_constant_test.onnx");
    EXPECT(p == prog);
}

2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
TEST_CASE(external_data_test)
{
    migraphx::program p = create_external_data_prog();

    auto prog = optimize_onnx("external_data_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(external_data_diff_path_test)
{
    migraphx::program p = create_external_data_prog();

    auto prog = optimize_onnx("ext_path/external_data_test.onnx");
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
TEST_CASE(eyelike_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{3, 4};
    const size_t k   = 0;
    auto num_rows    = input_lens.front();
    auto num_cols    = input_lens.back();
    auto input_type  = migraphx::shape::float_type;
    auto output_type = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    mm->add_parameter("T1", s);

    auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k);
    mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat});

    auto prog = optimize_onnx("eyelike_default_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(eyelike_double_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{6, 15};
    const size_t k   = 0;
    auto num_rows    = input_lens.front();
    auto num_cols    = input_lens.back();
    auto input_type  = migraphx::shape::double_type;
    auto output_type = migraphx::shape::double_type;
    migraphx::shape s{input_type, input_lens};
    mm->add_parameter("T1", s);

    auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k);
    mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat});

    auto prog = optimize_onnx("eyelike_double_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(eyelike_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{8, 8};
    const size_t k   = 0;
    auto num_rows    = input_lens.front();
    auto num_cols    = input_lens.back();
    auto input_type  = migraphx::shape::half_type;
    auto output_type = migraphx::shape::half_type;
    migraphx::shape s{input_type, input_lens};
    mm->add_parameter("T1", s);

    auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k);
    mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat});

    auto prog = optimize_onnx("eyelike_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(eyelike_k_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{3, 4};
    const size_t k   = 1;
    auto num_rows    = input_lens.front();
    auto num_cols    = input_lens.back();
    auto input_type  = migraphx::shape::float_type;
    auto output_type = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    mm->add_parameter("T1", s);

    auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k);
    mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat});

    auto prog = optimize_onnx("eyelike_k_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(eyelike_k_outofbounds_neg_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("eyelike_k_outofbounds_neg_test.onnx"); }));
}

TEST_CASE(eyelike_k_outofbounds_pos_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("eyelike_k_outofbounds_pos_test.onnx"); }));
}

TEST_CASE(eyelike_not_rank2_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("eyelike_not_rank2_test.onnx"); }));
}

TEST_CASE(eyelike_set_dtype_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{3, 4};
    const size_t k   = 0;
    auto num_rows    = input_lens.front();
    auto num_cols    = input_lens.back();
    auto input_type  = migraphx::shape::float_type;
    auto output_type = migraphx::shape::double_type;
    migraphx::shape s{input_type, input_lens};
    mm->add_parameter("T1", s);

    auto eyelike_mat = make_r_eyelike(num_rows, num_cols, k);
    mm->add_literal(migraphx::literal{migraphx::shape{output_type, input_lens}, eyelike_mat});

    auto prog = optimize_onnx("eyelike_set_dtype_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2329
TEST_CASE(flatten_test)
2330
2331
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2332
2333
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
2334
2335
    mm->add_instruction(migraphx::make_op("flatten", {{"axis", 2}}), l0);
    mm->add_instruction(migraphx::make_op("flatten", {{"axis", 1}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
2336
    auto prog = optimize_onnx("flatten_test.onnx");
2337
2338
2339
2340

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2341
2342
2343
2344
2345
TEST_CASE(flatten_nonstd_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 5, 4}});
2346
2347
2348
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l0);
    auto l2 = mm->add_instruction(migraphx::make_op("contiguous"), l1);
Khalique's avatar
Khalique committed
2349
2350
2351
2352
2353
2354
2355
2356
    mm->add_instruction(migraphx::make_op("flatten", {{"axis", 2}}), l2);
    auto l3 = mm->add_instruction(migraphx::make_op("contiguous"), l1);
    mm->add_instruction(migraphx::make_op("flatten", {{"axis", 1}}), l3);
    auto prog = optimize_onnx("flatten_nonstd_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2357
2358
2359
2360
2361
TEST_CASE(flatten_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2362
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
Charlie Lin's avatar
Charlie Lin committed
2363
2364
2365
2366
2367
    auto c0  = mm->add_instruction(migraphx::make_op("contiguous"), l0);
    auto ret = mm->add_instruction(migraphx::make_op("flatten", {{"axis", 2}}), c0);
    mm->add_return({ret});

    migraphx::onnx_options options;
2368
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2369
2370
2371
2372
    auto prog                     = parse_onnx("flatten_dyn_test.onnx", options);
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2373
2374
2375
TEST_CASE(floor_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2376
2377
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
2378
    mm->add_instruction(migraphx::make_op("floor"), input);
Shucai Xiao's avatar
Shucai Xiao committed
2379

Shucai Xiao's avatar
Shucai Xiao committed
2380
    auto prog = optimize_onnx("floor_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
2381
2382
2383
2384

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2385
TEST_CASE(gather_test)
2386
2387
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2388
2389
2390
    auto* mm = p.get_main_module();
    auto l0 = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    auto l1 = mm->add_parameter("indices", migraphx::shape{migraphx::shape::int32_type, {2, 3}});
Khalique's avatar
Khalique committed
2391
    int axis = 1;
2392
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
2393
    auto prog = optimize_onnx("gather_test.onnx");
2394
2395
2396
2397

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
TEST_CASE(gather_scalar_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    std::vector<size_t> idims{1};
    auto l1 =
        mm->add_parameter("indices", migraphx::shape{migraphx::shape::int32_type, idims, {0}});
    int axis = 1;
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l0, l1);
    auto prog = optimize_onnx("gather_scalar_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(gather_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2418
        "data", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {5, 5}, {6, 6}}});
Brian Pickrell's avatar
Brian Pickrell committed
2419
    auto l1 = mm->add_parameter(
2420
        "indices", migraphx::shape{migraphx::shape::int32_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
Brian Pickrell's avatar
Brian Pickrell committed
2421
2422
2423
2424
2425
2426
2427
2428
2429
    auto cont_l0 = mm->add_instruction(migraphx::make_op("contiguous"), l0);
    auto cont_l1 = mm->add_instruction(migraphx::make_op("contiguous"), l1);

    int axis       = 1;
    auto gather_op = migraphx::make_op("gather", {{"axis", axis}});
    auto ret       = mm->add_instruction(gather_op, cont_l0, cont_l1);
    mm->add_return({ret});

    migraphx::onnx_options options;
2430
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
2431
2432
2433
2434
2435
    auto prog                     = parse_onnx("gather_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2436
2437
2438
TEST_CASE(gather_elements_axis0_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2439
2440
2441
    auto* mm     = p.get_main_module();
    auto data    = mm->add_parameter("data", {migraphx::shape::float_type, {3, 4}});
    auto indices = mm->add_parameter("indices", {migraphx::shape::int32_type, {2, 3}});
Shucai Xiao's avatar
Shucai Xiao committed
2442
2443
2444
2445
    std::vector<int> ind_indices{0, 1, 2, 4, 5, 6};
    std::vector<int> ind_axis_indices{0, 0, 0, 1, 1, 1};
    migraphx::shape ind_s{migraphx::shape::int32_type, {2, 3}};
    auto l_data_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2446
        mm->add_literal(migraphx::literal{ind_s, ind_indices.begin(), ind_indices.end()});
Shucai Xiao's avatar
Shucai Xiao committed
2447
    auto l_ind_axis_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2448
2449
        mm->add_literal(migraphx::literal{ind_s, ind_axis_indices.begin(), ind_axis_indices.end()});
    auto l_stride = mm->add_literal(migraphx::literal{{migraphx::shape::int32_type, {1}}, {4}});
Shucai Xiao's avatar
Shucai Xiao committed
2450

2451
2452
    auto rsp_data    = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), data);
    auto lbst_stride = mm->add_instruction(
2453
        migraphx::make_op("multibroadcast", {{"out_lens", ind_s.lens()}}), l_stride);
2454
2455
2456
2457
    auto axis_delta = mm->add_instruction(migraphx::make_op("sub"), indices, l_ind_axis_indices);
    auto mul_delta  = mm->add_instruction(migraphx::make_op("mul"), axis_delta, lbst_stride);
    auto ind        = mm->add_instruction(migraphx::make_op("add"), l_data_indices, mul_delta);
    auto ret = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp_data, ind);
Shucai Xiao's avatar
Shucai Xiao committed
2458
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
2459
2460
2461
2462
2463
2464
2465
2466
2467

    auto prog = migraphx::parse_onnx("gather_elements_axis0_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(gather_elements_axis1_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2468
2469
2470
    auto* mm     = p.get_main_module();
    auto data    = mm->add_parameter("data", {migraphx::shape::float_type, {3, 4}});
    auto indices = mm->add_parameter("indices", {migraphx::shape::int32_type, {2, 3}});
Shucai Xiao's avatar
Shucai Xiao committed
2471
2472
2473
2474
    std::vector<int> ind_indices{0, 1, 2, 4, 5, 6};
    std::vector<int> ind_axis_indices{0, 1, 2, 0, 1, 2};
    migraphx::shape ind_s{migraphx::shape::int32_type, {2, 3}};
    auto l_data_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2475
        mm->add_literal(migraphx::literal{ind_s, ind_indices.begin(), ind_indices.end()});
Shucai Xiao's avatar
Shucai Xiao committed
2476
    auto l_ind_axis_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2477
2478
        mm->add_literal(migraphx::literal{ind_s, ind_axis_indices.begin(), ind_axis_indices.end()});
    auto l_stride = mm->add_literal(migraphx::literal{{migraphx::shape::int32_type, {1}}, {1}});
Shucai Xiao's avatar
Shucai Xiao committed
2479

2480
2481
    auto rsp_data    = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), data);
    auto lbst_stride = mm->add_instruction(
2482
        migraphx::make_op("multibroadcast", {{"out_lens", ind_s.lens()}}), l_stride);
2483
2484
2485
2486
    auto axis_delta = mm->add_instruction(migraphx::make_op("sub"), indices, l_ind_axis_indices);
    auto mul_delta  = mm->add_instruction(migraphx::make_op("mul"), axis_delta, lbst_stride);
    auto ind        = mm->add_instruction(migraphx::make_op("add"), l_data_indices, mul_delta);
    auto ret = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp_data, ind);
Shucai Xiao's avatar
Shucai Xiao committed
2487
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
2488
2489
2490
2491
2492
2493

    auto prog = migraphx::parse_onnx("gather_elements_axis1_test.onnx");

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
TEST_CASE(gathernd_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {2, 2}});
    auto l1  = mm->add_parameter("indices", migraphx::shape{migraphx::shape::int64_type, {2, 2}});
    mm->add_instruction(migraphx::make_op("gathernd"), l0, l1);
    auto prog = optimize_onnx("gathernd_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
2506
2507
2508
2509
TEST_CASE(gathernd_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2510
2511
2512
    auto l0  = mm->add_parameter(
        "data", migraphx::shape{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4}}});
    auto l1 = mm->add_parameter("indices",
Brian Pickrell's avatar
Brian Pickrell committed
2513
                                migraphx::shape{migraphx::shape::int64_type, {{1, 3}, {2, 2}}});
2514
    auto r  = mm->add_instruction(migraphx::make_op("gathernd"), l0, l1);
Brian Pickrell's avatar
Brian Pickrell committed
2515
2516
2517
    mm->add_return({r});

    migraphx::onnx_options options;
2518
    options.map_dyn_input_dims["data"]    = {{2, 4, {2}}, {2, 4}};
Brian Pickrell's avatar
Brian Pickrell committed
2519
2520
2521
2522
2523
    options.map_dyn_input_dims["indices"] = {{1, 3}, {2, 2}};
    auto prog                             = migraphx::parse_onnx("gathernd_dyn_test.onnx", options);
    EXPECT(p == prog);
}

turneram's avatar
turneram committed
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
TEST_CASE(gathernd_batch_dims_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {2, 2, 2}});
    auto l1  = mm->add_parameter("indices", migraphx::shape{migraphx::shape::int64_type, {2, 1}});
    int batch_dims = 1;
    mm->add_instruction(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), l0, l1);
    auto prog = optimize_onnx("gathernd_batch_dims_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2537
TEST_CASE(gemm_test)
2538
2539
{
    migraphx::program p;
2540
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2541
2542
2543
2544
2545
    auto l0    = mm->add_parameter("A", migraphx::shape{migraphx::shape::float_type, {8, 6}});
    auto l1    = mm->add_parameter("B", migraphx::shape{migraphx::shape::float_type, {8, 7}});
    auto l2    = mm->add_parameter("C", migraphx::shape{migraphx::shape::float_type, {6, 7}});
    auto alpha = 0.5f;
    auto beta  = 0.8f;
2546
2547
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
2548
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
Charlie Lin's avatar
Charlie Lin committed
2549
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
2550
2551
    auto b_l = mm->add_literal(beta);
    auto b_b = mm->add_instruction(
Charlie Lin's avatar
Charlie Lin committed
2552
2553
2554
        migraphx::make_op("multibroadcast", {{"out_lens", l2->get_shape().lens()}}), b_l);
    auto l2_b = mm->add_instruction(migraphx::make_op("mul"), l2, b_b);
    mm->add_instruction(migraphx::make_op("add"), dot, l2_b);
turneram's avatar
turneram committed
2555

Shucai Xiao's avatar
Shucai Xiao committed
2556
    auto prog = optimize_onnx("gemm_test.onnx");
2557
2558
2559
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2560
TEST_CASE(gemm_no_C_test)
2561
2562
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2563
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2564
2565
2566
2567
2568
    auto l0    = mm->add_parameter("A", migraphx::shape{migraphx::shape::float_type, {5, 7}});
    auto l1    = mm->add_parameter("B", migraphx::shape{migraphx::shape::float_type, {11, 5}});
    auto l2    = mm->add_parameter("C", migraphx::shape{migraphx::shape::float_type});
    auto alpha = 2.f;
    auto beta  = 2.0f;
2569
2570
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
Charlie Lin's avatar
Charlie Lin committed
2571
2572
2573
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
    auto t1  = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l1);
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, t1}, migraphx::make_op("dot"), 1.0f, 0.0f);
2574
    auto b_l = mm->add_literal(beta);
Charlie Lin's avatar
Charlie Lin committed
2575
2576
    auto l2_b =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {7, 11}}}), l2);
2577
    auto b_b = mm->add_instruction(
Charlie Lin's avatar
Charlie Lin committed
2578
2579
2580
        migraphx::make_op("multibroadcast", {{"out_lens", l2_b->get_shape().lens()}}), b_l);
    auto l2_bb = mm->add_instruction(migraphx::make_op("mul"), l2_b, b_b);
    mm->add_instruction(migraphx::make_op("add"), dot, l2_bb);
2581

Charlie Lin's avatar
Charlie Lin committed
2582
    auto prog = optimize_onnx("gemm_no_C_test.onnx");
2583
2584
2585
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2586
TEST_CASE(gemm_brcst_C_test)
2587
2588
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2589
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2590
2591
2592
2593
    auto l0  = mm->add_parameter("A", migraphx::shape{migraphx::shape::float_type, {5, 6}});
    auto l1  = mm->add_parameter("B", migraphx::shape{migraphx::shape::float_type, {5, 7}});
    auto l2  = mm->add_parameter("C", migraphx::shape{migraphx::shape::float_type, {6, 1}});
    std::vector<std::size_t> out_lens{6, 7};
Khalique's avatar
Khalique committed
2594
2595
    auto alpha = 0.5f;
    auto beta  = 0.8f;
2596
2597
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
Charlie Lin's avatar
Charlie Lin committed
2598
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
2599
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
2600
2601
    auto b_l = mm->add_literal(beta);
    auto l2_b =
2602
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", out_lens}}), l2);
2603
    auto b_b = mm->add_instruction(
2604
        migraphx::make_op("multibroadcast", {{"out_lens", l2_b->get_shape().lens()}}), b_l);
2605
    auto l2_bb = mm->add_instruction(migraphx::make_op("mul"), l2_b, b_b);
turneram's avatar
turneram committed
2606
    mm->add_instruction(migraphx::make_op("add"), dot, l2_bb);
2607

Charlie Lin's avatar
Charlie Lin committed
2608
    auto prog = optimize_onnx("gemm_brcst_C_test.onnx");
2609
2610
2611
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2612
2613
2614
2615
TEST_CASE(gemm_half_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2616
2617
2618
    auto l0    = mm->add_parameter("A", migraphx::shape{migraphx::shape::half_type, {8, 6}});
    auto l1    = mm->add_parameter("B", migraphx::shape{migraphx::shape::half_type, {8, 7}});
    auto l2    = mm->add_parameter("C", migraphx::shape{migraphx::shape::half_type, {6, 1}});
Shucai Xiao's avatar
Shucai Xiao committed
2619
2620
2621
2622
2623
2624
    auto alpha = 0.5f;
    auto beta  = 0.8f;
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
    t_a        = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), t_a);
Charlie Lin's avatar
Charlie Lin committed
2625
2626
    t_a = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
    std::vector<std::size_t> lens = {6, 7};
2627
2628
2629
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    l2       = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), l2);
    l2       = mm->add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
2630
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l2);
2631
2632
    auto b_l  = mm->add_literal(beta);
    auto b_b  = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), b_l);
Shucai Xiao's avatar
Shucai Xiao committed
2633
2634
2635
    auto l2_b = mm->add_instruction(migraphx::make_op("mul"), l2, b_b);
    l2_b      = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), l2_b);
turneram's avatar
turneram committed
2636
    mm->add_instruction(migraphx::make_op("add"), dot, l2_b);
Shucai Xiao's avatar
Shucai Xiao committed
2637
2638
2639
2640
2641

    auto prog = optimize_onnx("gemm_half_test.onnx");
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2642
2643
2644
2645
2646
TEST_CASE(gemm_dyn_inner_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2647
        "A", migraphx::shape{migraphx::shape::float_type, {{1, 10, {8}}, {6, 6}}});
Charlie Lin's avatar
Charlie Lin committed
2648
    auto l1 = mm->add_parameter(
2649
        "B", migraphx::shape{migraphx::shape::float_type, {{1, 10, {8}}, {7, 7}}});
Charlie Lin's avatar
Charlie Lin committed
2650
2651
2652
2653
2654
2655
2656
2657
    auto alpha = 0.5f;
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    mm->add_return({dot});

    migraphx::onnx_options options;
2658
    options.default_dyn_dim_value = {1, 10, {8}};
Charlie Lin's avatar
Charlie Lin committed
2659
2660
2661
2662
2663
2664
2665
2666
2667
    auto prog                     = migraphx::parse_onnx("gemm_dyn_inner_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(gemm_dyn_outer_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2668
        "A", migraphx::shape{migraphx::shape::float_type, {{5, 5}, {5, 10, {7}}}});
Charlie Lin's avatar
Charlie Lin committed
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
    auto l1    = mm->add_parameter("B", migraphx::shape{migraphx::shape::float_type, {11, 5}});
    auto alpha = 2.f;
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
    auto t1  = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l1);
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, t1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    mm->add_return({dot});

    migraphx::onnx_options options;
2679
    options.default_dyn_dim_value = {5, 10, {7}};
Charlie Lin's avatar
Charlie Lin committed
2680
2681
2682
2683
    auto prog                     = migraphx::parse_onnx("gemm_dyn_outer_test.onnx", options);
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2684
TEST_CASE(gemm_dyn_bias_test)
Charlie Lin's avatar
Charlie Lin committed
2685
{
Charlie Lin's avatar
Charlie Lin committed
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x0 =
        mm->add_parameter("A", migraphx::shape{migraphx::shape::float_type, {{8, 8}, {1, 10}}});
    auto x1   = mm->add_parameter("B", migraphx::shape{migraphx::shape::float_type, {8, 7}});
    auto x2   = mm->add_parameter("C", migraphx::shape{migraphx::shape::float_type, {1, 7}});
    auto x0_t = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), x0);
    auto dot  = mm->add_instruction(migraphx::make_op("dot"), x0_t, x1);
    auto x2_b = mm->add_instruction(migraphx::make_op("multibroadcast"), x2, dot);
    auto ret  = mm->add_instruction(migraphx::make_op("add"), dot, x2_b);
    mm->add_return({ret});

Charlie Lin's avatar
Charlie Lin committed
2698
    migraphx::onnx_options options;
Charlie Lin's avatar
Charlie Lin committed
2699
2700
2701
    options.default_dyn_dim_value = {1, 10};
    auto prog                     = parse_onnx("gemm_dyn_bias_test.onnx", options);
    EXPECT(p == prog);
Charlie Lin's avatar
Charlie Lin committed
2702
2703
2704
2705
2706
2707
2708
}

TEST_CASE(gemm_rank_error)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("gemm_rank_error.onnx"); }));
}

Khalique's avatar
Khalique committed
2709
TEST_CASE(globalavgpool_test)
2710
2711
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2712
2713
2714
    auto* mm = p.get_main_module();
    auto input =
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
2715
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
Khalique's avatar
Khalique committed
2716
2717
    auto lens  = input->get_shape().lens();
    op.lengths = {lens[2], lens[3]};
kahmed10's avatar
kahmed10 committed
2718
    op.padding = {0, 0, 0, 0};
Shucai Xiao's avatar
Shucai Xiao committed
2719
    mm->add_instruction(op, input);
Khalique's avatar
Khalique committed
2720

Shucai Xiao's avatar
Shucai Xiao committed
2721
    auto prog = optimize_onnx("globalavgpool_test.onnx");
2722
2723
2724
2725

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2726
2727
2728
TEST_CASE(globalavgpool_dyn_test)
{
    migraphx::program p;
2729
2730
2731
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {16, 16}, {16, 16}}});
Charlie Lin's avatar
Charlie Lin committed
2732
2733
2734
2735
2736
2737
2738
2739
    auto ret = mm->add_instruction(migraphx::make_op("pooling",
                                                     {{"mode", migraphx::op::pooling_mode::average},
                                                      {"lengths", {16, 16}},
                                                      {"padding", {0, 0, 0, 0}}}),
                                   input);
    mm->add_return({ret});

    migraphx::onnx_options options;
2740
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2741
2742
2743
2744
2745
    auto prog                     = parse_onnx("globalavgpool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
TEST_CASE(globallppool_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto input =
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::lpnorm};
    auto lens  = input->get_shape().lens();
    op.lengths = {lens[2], lens[3]};
    op.padding = {0, 0, 0, 0};
    mm->add_instruction(op, input);

    auto prog = optimize_onnx("globallppool_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2763
2764
2765
TEST_CASE(globallppool_dyn_test)
{
    migraphx::program p;
2766
2767
2768
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 1}, {3, 3}, {16, 32}, {16, 32}}});
Charlie Lin's avatar
Charlie Lin committed
2769
2770
2771
2772
2773
2774
2775
2776
2777
    auto ret = mm->add_instruction(migraphx::make_op("pooling",
                                                     {{"mode", migraphx::op::pooling_mode::lpnorm},
                                                      {"dyn_global", true},
                                                      {"padding", {0, 0, 0, 0}},
                                                      {"lengths", {}}}),
                                   input);
    mm->add_return({ret});

    migraphx::onnx_options options;
2778
    options.default_dyn_dim_value = {16, 32};
Charlie Lin's avatar
Charlie Lin committed
2779
2780
2781
2782
2783
    auto prog                     = migraphx::parse_onnx("globallppool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2784
TEST_CASE(globalmaxpool_test)
Khalique's avatar
Khalique committed
2785
2786
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2787
2788
2789
    auto* mm = p.get_main_module();
    auto input =
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
2790
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
Khalique's avatar
Khalique committed
2791
2792
    auto lens  = input->get_shape().lens();
    op.lengths = {lens[2], lens[3]};
kahmed10's avatar
kahmed10 committed
2793
    op.padding = {0, 0, 0, 0};
Shucai Xiao's avatar
Shucai Xiao committed
2794
    mm->add_instruction(op, input);
Khalique's avatar
Khalique committed
2795

Shucai Xiao's avatar
Shucai Xiao committed
2796
    auto prog = optimize_onnx("globalmaxpool_test.onnx");
Khalique's avatar
Khalique committed
2797
2798
2799
2800

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2801
2802
2803
TEST_CASE(globalmaxpool_dyn_test)
{
    migraphx::program p;
2804
2805
2806
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {32, 32}, {32, 32}}});
Charlie Lin's avatar
Charlie Lin committed
2807
2808
2809
2810
2811
2812
2813
2814
    auto ret = mm->add_instruction(migraphx::make_op("pooling",
                                                     {{"mode", migraphx::op::pooling_mode::max},
                                                      {"lengths", {32, 32}},
                                                      {"padding", {0, 0, 0, 0}}}),
                                   input);
    mm->add_return({ret});

    migraphx::onnx_options options;
2815
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2816
2817
2818
2819
2820
    auto prog                     = parse_onnx("globalmaxpool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

2821
2822
2823
TEST_CASE(greater_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2824
    auto* mm = p.get_main_module();
2825
2826
2827
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};

Shucai Xiao's avatar
Shucai Xiao committed
2828
2829
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
2830
2831
2832
    auto gr     = mm->add_instruction(migraphx::make_op("greater"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
2833
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2834
        gr);
Shucai Xiao's avatar
Shucai Xiao committed
2835
    mm->add_return({ret});
2836
2837
2838
2839
2840
2841
2842
2843

    auto prog = migraphx::parse_onnx("greater_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(greater_bool_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2844
    auto* mm = p.get_main_module();
2845
2846
2847
    migraphx::shape sf{migraphx::shape::float_type, {2, 3}};
    migraphx::shape sb{migraphx::shape::bool_type, {2, 3}};

Shucai Xiao's avatar
Shucai Xiao committed
2848
2849
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
2850
2851
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
2852
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2853
2854
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("greater"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
2855
    mm->add_return({ret});
2856
2857
2858
2859
2860

    auto prog = migraphx::parse_onnx("greater_bool_test.onnx");
    EXPECT(p == prog);
}

turneram's avatar
turneram committed
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
TEST_CASE(greaterorequal_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto input1 = mm->add_parameter("x1", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {3}});
    auto temp   = mm->add_instruction(migraphx::make_op("less"), input1, input2);
    auto bt     = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), temp);
    auto ge = mm->add_instruction(migraphx::make_op("not"), bt);

    mm->add_return({ge});

    auto prog = migraphx::parse_onnx("greaterorequal_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2879
TEST_CASE(group_conv_test)
2880
2881
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2882
2883
2884
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 4, 16, 16}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 1, 3, 3}});
Khalique's avatar
Khalique committed
2885
2886
    migraphx::op::convolution op;
    op.group = 4;
Shucai Xiao's avatar
Shucai Xiao committed
2887
    mm->add_instruction(op, l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
2888
    auto prog = optimize_onnx("group_conv_test.onnx");
2889
2890
2891
2892

    EXPECT(p == prog);
}

2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
migraphx::program make_group_norm(const std::vector<int64_t>& input_dims,
                                  const std::vector<int64_t>& scale_dims,
                                  const std::vector<int64_t>& bias_dims,
                                  const std::vector<int64_t>& reshape_dims,
                                  const std::vector<int64_t>& reduce_axes,
                                  const float eps_value               = 1e-5f,
                                  const migraphx::shape::type_t dtype = migraphx::shape::float_type)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x     = mm->add_parameter("x", {dtype, input_dims});
    auto scale = mm->add_parameter("scale", {dtype, scale_dims});
    auto bias  = mm->add_parameter("bias", {dtype, bias_dims});

    auto eps = mm->add_literal(migraphx::literal{dtype, {eps_value}});

2910
    auto x_reshapedd =
2911
2912
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", reshape_dims}}), x);
    auto mean =
2913
2914
2915
        mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", reduce_axes}}), x_reshapedd);
    auto x_sub_mean    = add_common_op(*mm, migraphx::make_op("sub"), {x_reshapedd, mean});
    auto x_sqdiff_mean = add_common_op(*mm, migraphx::make_op("sqdiff"), {x_reshapedd, mean});
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
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
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
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
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
    auto var     = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", reduce_axes}}),
                                   x_sqdiff_mean);
    auto var_eps = add_common_op(*mm, migraphx::make_op("add"), {var, eps});
    auto rsqrt   = mm->add_instruction(migraphx::make_op("rsqrt"), {var_eps});
    auto result  = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, rsqrt});
    auto scale_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", reshape_dims}}), scale);
    auto bias_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", reshape_dims}}), bias);
    auto scaled = mm->add_instruction(migraphx::make_op("mul"), {result, scale_bcast});
    auto y      = mm->add_instruction(migraphx::make_op("add"), {scaled, bias_bcast});
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", input_dims}}), y);

    return p;
}

TEST_CASE(group_norm_3d_test)
{
    migraphx::program p = make_group_norm(
        {1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-5f, migraphx::shape::float_type);
    auto prog = optimize_onnx("group_norm_3d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_3d_half_test)
{
    migraphx::program p = make_group_norm(
        {1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-5f, migraphx::shape::half_type);
    auto prog = optimize_onnx("group_norm_3d_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_4d_test)
{
    migraphx::program p = make_group_norm(
        {1, 4, 3, 3}, {2}, {2}, {1, 2, 2, 3, 3}, {2, 3, 4}, 1e-5f, migraphx::shape::float_type);
    auto prog = optimize_onnx("group_norm_4d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_4d_half_test)
{
    migraphx::program p = make_group_norm(
        {1, 4, 3, 3}, {2}, {2}, {1, 2, 2, 3, 3}, {2, 3, 4}, 1e-5f, migraphx::shape::half_type);
    auto prog = optimize_onnx("group_norm_4d_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_5d_test)
{
    migraphx::program p = make_group_norm({3, 3, 3, 3, 3},
                                          {1},
                                          {1},
                                          {3, 1, 3, 3, 3, 3},
                                          {2, 3, 4, 5},
                                          1e-5f,
                                          migraphx::shape::float_type);
    auto prog           = optimize_onnx("group_norm_5d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_5d_half_test)
{
    migraphx::program p = make_group_norm({3, 3, 3, 3, 3},
                                          {1},
                                          {1},
                                          {3, 1, 3, 3, 3, 3},
                                          {2, 3, 4, 5},
                                          1e-5f,
                                          migraphx::shape::half_type);
    auto prog           = optimize_onnx("group_norm_5d_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_small_eps_half_test)
{
    migraphx::program p = make_group_norm(
        {1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-7f, migraphx::shape::half_type);
    auto prog = optimize_onnx("group_norm_small_eps_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(group_norm_invalid_num_groups_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("group_norm_invalid_num_groups_error_test.onnx"); }));
}

TEST_CASE(group_norm_missing_attribute_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("group_norm_missing_attribute_error_test.onnx"); }));
}

TEST_CASE(group_norm_invalid_input_count_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("group_norm_invalid_input_count_error_test.onnx"); }));
}

TEST_CASE(group_norm_invalid_input_shape_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("group_norm_invalid_input_shape_error_test.onnx"); }));
}

TEST_CASE(group_norm_invalid_scale_shape_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("group_norm_invalid_scale_shape_test.onnx"); }));
}

TEST_CASE(group_norm_invalid_bias_shape_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("group_norm_invalid_bias_shape_test.onnx"); }));
}

turneram's avatar
turneram committed
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
TEST_CASE(hardsigmoid_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{1, 3, 4, 5};
    auto input_type = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    auto x = mm->add_parameter("x", s);

    float alpha = 0.2;
    float beta  = 0.5;

    auto mb_alpha = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}}));
    auto mb_beta = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {beta}}));
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0}}));
    auto mb_one =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));

    auto mul = mm->add_instruction(migraphx::make_op("mul"), mb_alpha, x);
    auto add = mm->add_instruction(migraphx::make_op("add"), mb_beta, mul);
    mm->add_instruction(migraphx::make_op("clip"), add, mb_zero, mb_one);

    auto prog = optimize_onnx("hardsigmoid_default_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(hardsigmoid_double_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{1, 3, 4, 5};
    auto input_type = migraphx::shape::double_type;
    migraphx::shape s{input_type, input_lens};
    auto x = mm->add_parameter("x", s);

    float alpha = 0.3;
    float beta  = 0.7;

    auto mb_alpha = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}}));
    auto mb_beta = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {beta}}));
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0}}));
    auto mb_one =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));

    auto mul = mm->add_instruction(migraphx::make_op("mul"), mb_alpha, x);
    auto add = mm->add_instruction(migraphx::make_op("add"), mb_beta, mul);
    mm->add_instruction(migraphx::make_op("clip"), add, mb_zero, mb_one);

    auto prog = optimize_onnx("hardsigmoid_double_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(hardsigmoid_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{1, 3, 4, 5};
    auto input_type = migraphx::shape::half_type;
    migraphx::shape s{input_type, input_lens};
    auto x = mm->add_parameter("x", s);

    float alpha = 0.2;
    float beta  = 0.5;

    auto mb_alpha = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}}));
    auto mb_beta = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {beta}}));
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0}}));
    auto mb_one =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));

    auto mul = mm->add_instruction(migraphx::make_op("mul"), mb_alpha, x);
    auto add = mm->add_instruction(migraphx::make_op("add"), mb_beta, mul);
    mm->add_instruction(migraphx::make_op("clip"), add, mb_zero, mb_one);

    auto prog = optimize_onnx("hardsigmoid_half_test.onnx");
    EXPECT(p == prog);
}

3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
TEST_CASE(hardswish_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{2, 5};
    auto input_type = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    auto x = mm->add_parameter("x", s);

    float alpha = 1.0 / 6.0;
    float beta  = 0.5;

    auto mb_alpha = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {alpha}}));
    auto mb_beta = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
        mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {beta}}));
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0}}));
    auto mb_one =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));

    auto mul         = mm->add_instruction(migraphx::make_op("mul"), mb_alpha, x);
    auto add         = mm->add_instruction(migraphx::make_op("add"), mb_beta, mul);
    auto hardsigmoid = mm->add_instruction(migraphx::make_op("clip"), add, mb_zero, mb_one);
    mm->add_instruction(migraphx::make_op("mul"), x, hardsigmoid);

    auto prog = optimize_onnx("hardswish_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3166
3167
3168
3169
3170
3171
TEST_CASE(if_else_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
3172

Shucai Xiao's avatar
Shucai Xiao committed
3173
    std::vector<float> ones(s.elements(), 1.0f);
3174
3175
3176
3177
3178
3179
3180
    std::vector<float> rand = {1.3865, -0.494756, -0.283504, 0.200491, -0.490031, 1.32388};

    auto l1   = mm->add_literal(s, ones);
    auto l2   = mm->add_literal(s, rand);
    auto x    = mm->add_parameter("x", s);
    auto y    = mm->add_parameter("y", s);
    auto cond = mm->add_parameter("cond", sc);
Shucai Xiao's avatar
Shucai Xiao committed
3181
3182
3183
3184

    auto* then_mod = p.create_module("If_5_if");
    auto rt        = then_mod->add_instruction(migraphx::make_op("add"), x, l1);
    then_mod->add_return({rt});
Shucai Xiao's avatar
Shucai Xiao committed
3185

Shucai Xiao's avatar
Shucai Xiao committed
3186
3187
3188
    auto* else_mod = p.create_module("If_5_else");
    auto re        = else_mod->add_instruction(migraphx::make_op("mul"), y, l2);
    else_mod->add_return({re});
Shucai Xiao's avatar
Shucai Xiao committed
3189

Shucai Xiao's avatar
Shucai Xiao committed
3190
3191
    auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
    auto r   = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
Shucai Xiao's avatar
Shucai Xiao committed
3192
3193
    mm->add_return({r});

3194
3195
3196
3197
3198
3199
3200
3201
    auto prog = migraphx::parse_onnx("if_else_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(if_else_test_inlined)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3202

3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    mm->add_literal(migraphx::literal(sc, {0}));

    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> ones(s.elements(), 1.0f);
    mm->add_literal(s, ones);

    std::vector<float> rand = {0.811412, -0.949771, -0.169276, 0.36552, -0.14801, 2.07061};
    auto l2                 = mm->add_literal(s, rand);

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

    auto y  = mm->add_parameter("y", s);
    auto re = mm->add_instruction(migraphx::make_op("mul"), y, l2);
    mm->add_return({re});

    auto prog = migraphx::parse_onnx("if_else_test_inlined.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
3220
3221
3222
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
TEST_CASE(if_literal_test)
{
    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_1_if");
    std::vector<float> data1 = {1, 2, 3, 4, 5};
    auto l1                  = then_mod->add_literal(migraphx::literal(s, data1));
Shucai Xiao's avatar
Shucai Xiao committed
3235
    then_mod->add_literal({});
Shucai Xiao's avatar
Shucai Xiao committed
3236
3237
3238
3239
3240
    then_mod->add_return({l1});

    auto* else_mod           = p.create_module("If_1_else");
    std::vector<float> data2 = {5, 4, 3, 2, 1};
    auto l2                  = else_mod->add_literal(migraphx::literal(s, data2));
Shucai Xiao's avatar
Shucai Xiao committed
3241
    else_mod->add_literal({});
Shucai Xiao's avatar
Shucai Xiao committed
3242
3243
3244
    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
3245
3246
    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
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284

    auto prog = migraphx::parse_onnx("if_literal_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(if_param_excp_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("if_param_excp_test.onnx"); }));
}

TEST_CASE(if_param_excp1_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("if_param_excp1_test.onnx"); }));
}

TEST_CASE(if_param_test)
{
    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 ds{migraphx::shape::float_type, {2, 3}};
    auto x = mm->add_parameter("x", ds);
    auto y = mm->add_parameter("y", ds);

    auto* then_mod           = p.create_module("If_3_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 a1                  = then_mod->add_instruction(migraphx::make_op("add"), x, l1);
    then_mod->add_return({a1});

    auto* else_mod           = p.create_module("If_3_else");
    std::vector<float> data2 = {-0.258047, 0.360394, 0.536804, -0.577762, 1.0217, 1.02442};
    auto l2                  = else_mod->add_literal(migraphx::literal(ds, data2));
    auto a2                  = else_mod->add_instruction(migraphx::make_op("mul"), y, l2);
    else_mod->add_return({a2});

    auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
Shucai Xiao's avatar
Shucai Xiao committed
3285
3286
    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
3287
3288
3289
3290
3291

    auto prog = migraphx::parse_onnx("if_param_test.onnx");
    EXPECT(p == prog);
}

3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
TEST_CASE(if_then_else_multi_output_shapes_inlined_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    mm->add_literal(migraphx::literal(sc, {1}));

    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    migraphx::shape s_trail{migraphx::shape::float_type, {2, 3, 1}};
    std::vector<float> ones(s.elements(), 1.0f);

    auto l1                 = mm->add_literal(s_trail, ones);
    std::vector<float> rand = {-1.01837, -0.305541, -0.254105, 0.892955, 1.38714, -0.584205};
    mm->add_literal(s, rand);

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

    auto rt  = mm->add_instruction(migraphx::make_op("add"), x, l1);
    auto rt2 = mm->add_instruction(migraphx::make_op("add"), x, x);

    mm->add_return({rt, rt2});

    auto prog = migraphx::parse_onnx("if_then_else_multi_output_shapes_inlined_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(if_then_else_multi_output_shapes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sc{migraphx::shape::bool_type, {1}};

    migraphx::shape s{migraphx::shape::float_type, {2, 3, 1}};
    migraphx::shape s_trail{migraphx::shape::float_type, {2, 3, 1}};
    std::vector<float> ones(s.elements(), 1.0f);

    auto l1                 = mm->add_literal(s_trail, ones);
    std::vector<float> rand = {-0.753997, 0.707831, -0.865795, 2.49574, 0.464937, -0.168745};
    auto l2                 = mm->add_literal(s, rand);
    auto x                  = mm->add_parameter("x", s_trail);
    auto y                  = mm->add_parameter("y", s);
    auto cond               = mm->add_parameter("cond", sc);

    auto* then_mod = p.create_module("If_5_if");
    auto rt        = then_mod->add_instruction(migraphx::make_op("add"), x, l1);
    auto rt2       = then_mod->add_instruction(migraphx::make_op("add"), x, x);
    then_mod->add_return({rt, rt2});

    auto* else_mod = p.create_module("If_5_else");
    auto re        = else_mod->add_instruction(migraphx::make_op("mul"), y, l2);
    auto re2       = else_mod->add_instruction(migraphx::make_op("sub"), y, l2);
    else_mod->add_return({re, re2});

    auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
    auto r1  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
    auto r2  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), ret);
    mm->add_return({r1, r2});

    auto prog = migraphx::parse_onnx("if_then_else_multi_output_shapes_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
TEST_CASE(if_pl_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape cond_s{migraphx::shape::bool_type};
    migraphx::shape xs{migraphx::shape::float_type, {2, 3}};
    migraphx::shape ys{migraphx::shape::float_type, {3, 3}};
    std::vector<float> datax = {1, 2, 3, 4, 5, 6};
    std::vector<float> datay = {8, 7, 6, 5, 4, 3, 2, 1, 0};

    auto lx   = mm->add_literal(migraphx::literal(xs, datax));
    auto ly   = mm->add_literal(migraphx::literal(ys, datay));
    auto cond = mm->add_parameter("cond", cond_s);
    auto x    = mm->add_parameter("x", xs);
    auto y    = mm->add_parameter("y", ys);

    auto* then_mod = p.create_module("If_5_if");
    auto l1        = then_mod->add_literal(migraphx::literal(ys, datay));
    auto a1        = then_mod->add_instruction(migraphx::make_op("add"), x, lx);
    then_mod->add_return({a1, l1});

    auto* else_mod = p.create_module("If_5_else");
    auto l2        = else_mod->add_literal(migraphx::literal(xs, datax));
    auto a2        = else_mod->add_instruction(migraphx::make_op("mul"), y, ly);
    else_mod->add_return({l2, a2});

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

    auto prog = migraphx::parse_onnx("if_pl_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3391
3392
3393
3394
3395
3396
TEST_CASE(if_then_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
3397

Shucai Xiao's avatar
Shucai Xiao committed
3398
    std::vector<float> ones(s.elements(), 1.0f);
3399
3400
3401
3402
3403
3404
3405
    std::vector<float> rand = {-0.266913, -0.180328, -0.124268, -1.23768, 0.312334, 1.18475};

    auto l1   = mm->add_literal(s, ones);
    auto l2   = mm->add_literal(s, rand);
    auto x    = mm->add_parameter("x", s);
    auto y    = mm->add_parameter("y", s);
    auto cond = mm->add_parameter("cond", sc);
Shucai Xiao's avatar
Shucai Xiao committed
3406

Shucai Xiao's avatar
Shucai Xiao committed
3407
3408
3409
3410
3411
3412
3413
    auto* then_mod = p.create_module("If_5_if");
    auto rt        = then_mod->add_instruction(migraphx::make_op("add"), x, l1);
    then_mod->add_return({rt});

    auto* else_mod = p.create_module("If_5_else");
    auto re        = else_mod->add_instruction(migraphx::make_op("mul"), y, l2);
    else_mod->add_return({re});
Shucai Xiao's avatar
Shucai Xiao committed
3414

Shucai Xiao's avatar
Shucai Xiao committed
3415
3416
    auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
    auto r   = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
Shucai Xiao's avatar
Shucai Xiao committed
3417
3418
3419
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("if_then_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
3420
3421
3422
    EXPECT(p == prog);
}

3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
TEST_CASE(if_then_test_inlined)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    mm->add_literal(migraphx::literal(sc, {1}));

    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> ones(s.elements(), 1.0f);

    auto l1                 = mm->add_literal(s, ones);
    std::vector<float> rand = {-1.26487, -2.42279, 0.990835, 1.63072, 0.812238, -0.174946};

    mm->add_literal(s, rand);

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

    auto rt = mm->add_instruction(migraphx::make_op("add"), x, l1);
    mm->add_return({rt});

    auto prog = migraphx::parse_onnx("if_then_test_inlined.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
TEST_CASE(if_tuple_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sd{migraphx::shape::float_type, {1}};
    auto l1 = mm->add_literal(migraphx::literal(sd, {1}));
    auto l2 = mm->add_literal(migraphx::literal(sd, {2}));
    auto l3 = mm->add_literal(migraphx::literal(sd, {3}));
    migraphx::shape sx{migraphx::shape::float_type, {1, 4}};
    migraphx::shape sy{migraphx::shape::float_type, {3, 4}};
    migraphx::shape sc{migraphx::shape::bool_type};
    auto cond = mm->add_parameter("cond", sc);
    auto x    = mm->add_parameter("x", sx);
    auto y    = mm->add_parameter("y", sy);

    auto* then_mod = p.create_module("If_6_if");
3465
3466
    auto m1 =
        then_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
3467
    auto add0 = then_mod->add_instruction(migraphx::make_op("add"), x, m1);
3468
3469
    auto m2 =
        then_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l2);
Shucai Xiao's avatar
Shucai Xiao committed
3470
3471
3472
3473
    auto mul0 = then_mod->add_instruction(migraphx::make_op("mul"), y, m2);
    then_mod->add_return({add0, mul0});

    auto* else_mod = p.create_module("If_6_else");
3474
3475
    auto me1 =
        else_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l3);
Shucai Xiao's avatar
Shucai Xiao committed
3476
    auto mul1 = else_mod->add_instruction(migraphx::make_op("mul"), x, me1);
3477
3478
    auto me2 =
        else_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l3);
Shucai Xiao's avatar
Shucai Xiao committed
3479
3480
3481
3482
3483
3484
3485
    auto add1 = else_mod->add_instruction(migraphx::make_op("add"), y, me2);
    else_mod->add_return({mul1, add1});

    auto ret = mm->add_instruction(migraphx::make_op("if"), {cond}, {then_mod, else_mod});
    auto r0  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), ret);
    auto r1  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), ret);
    mm->add_return({r0, r1});
Shucai Xiao's avatar
Shucai Xiao committed
3486

Shucai Xiao's avatar
Shucai Xiao committed
3487
    auto prog = migraphx::parse_onnx("if_tuple_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
3488
3489
3490
    EXPECT(p == prog);
}

3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
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
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
TEST_CASE(isinf_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::half_type, {2, 3}};
    auto t1  = mm->add_parameter("t1", s);
    auto ret = mm->add_instruction(migraphx::make_op("isinf"), t1);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isinf_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(isinf_neg_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    auto t1     = mm->add_parameter("t1", s);
    auto is_inf = mm->add_instruction(migraphx::make_op("isinf"), t1);
    auto zero_l = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0}});
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), zero_l);

    auto is_neg = mm->add_instruction(migraphx::make_op("less"), t1, mb_zero);
    if(is_neg->get_shape().type() != migraphx::shape::bool_type)
    {
        is_neg = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), is_neg);
    }
    auto ret = mm->add_instruction(migraphx::make_op("logical_and"), is_inf, is_neg);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isinf_neg_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(isinf_double_pos_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::double_type, {2, 3}};
    auto t1     = mm->add_parameter("t1", s);
    auto is_inf = mm->add_instruction(migraphx::make_op("isinf"), t1);
    auto zero_l = mm->add_literal(migraphx::literal{migraphx::shape::double_type, {0}});
    auto mb_zero =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), zero_l);

    auto is_neg = mm->add_instruction(migraphx::make_op("greater"), t1, mb_zero);
    if(is_neg->get_shape().type() != migraphx::shape::bool_type)
    {
        is_neg = mm->add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), is_neg);
    }
    auto ret = mm->add_instruction(migraphx::make_op("logical_and"), is_inf, is_neg);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isinf_double_pos_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(isinf_no_detect_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    mm->add_parameter("t1", s);
    auto ret = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}),
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::bool_type}, {false}}));
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isinf_no_detect_test.onnx");
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
TEST_CASE(isnan_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    auto t1  = mm->add_parameter("t1", s);
    auto ret = mm->add_instruction(migraphx::make_op("isnan"), t1);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isnan_float_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(isnan_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::half_type, {2, 3}};
    auto t1  = mm->add_parameter("t1", s);
    auto ret = mm->add_instruction(migraphx::make_op("isnan"), t1);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("isnan_half_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3593
TEST_CASE(imagescaler_test)
3594
3595
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3596
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
3597
    migraphx::shape s{migraphx::shape::float_type, {1, 3, 16, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
3598
3599
3600
    auto l0        = mm->add_parameter("0", s);
    auto scale_val = mm->add_literal(0.5f);
    auto bias_vals = mm->add_literal(
Khalique's avatar
Khalique committed
3601
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {0.01, 0.02, 0.03}});
3602
3603
3604
3605
    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"), l0, scaled_tensor);
    auto bias_bcast = mm->add_instruction(
3606
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals);
3607
    mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
Khalique's avatar
Khalique committed
3608

Shucai Xiao's avatar
Shucai Xiao committed
3609
    auto prog = optimize_onnx("imagescaler_test.onnx");
3610
3611
3612
3613

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3614
3615
3616
TEST_CASE(imagescaler_half_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3617
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3618
    migraphx::shape s{migraphx::shape::half_type, {1, 3, 16, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
3619
    auto l0 = mm->add_parameter("0", s);
Shucai Xiao's avatar
Shucai Xiao committed
3620
    auto scale_val =
Shucai Xiao's avatar
Shucai Xiao committed
3621
3622
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type}, {0.5f}});
    auto bias_vals = mm->add_literal(
Shucai Xiao's avatar
Shucai Xiao committed
3623
        migraphx::literal{migraphx::shape{migraphx::shape::half_type, {3}}, {0.01, 0.02, 0.03}});
3624
3625
3626
3627
    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"), l0, scaled_tensor);
    auto bias_bcast = mm->add_instruction(
3628
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals);
3629
    mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
Shucai Xiao's avatar
Shucai Xiao committed
3630
3631
3632
3633
3634
3635

    auto prog = optimize_onnx("imagescaler_half_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3636
TEST_CASE(implicit_add_bcast_test)
3637
3638
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3639
3640
3641
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 4, 1}});
3642
3643
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3644
    mm->add_instruction(migraphx::make_op("add"), l0, l3);
Khalique's avatar
Khalique committed
3645

Shucai Xiao's avatar
Shucai Xiao committed
3646
    auto prog = optimize_onnx("implicit_add_bcast_test.onnx");
3647
3648
3649
3650

    EXPECT(p == prog);
}

3651
3652
3653
TEST_CASE(implicit_add_bcast_user_input_shape_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3654
3655
3656
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 5, 1}});
3657
3658
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4, 5, 6}}}), l1);
3659
    auto r = mm->add_instruction(migraphx::make_op("add"), l0, l3);
Shucai Xiao's avatar
Shucai Xiao committed
3660
    mm->add_return({r});
3661
3662
3663
3664
3665
3666
3667
3668
3669

    migraphx::onnx_options options;
    options.map_input_dims["0"] = {3, 4, 5, 6};
    options.map_input_dims["1"] = {4, 5, 1};
    auto prog                   = migraphx::parse_onnx("implicit_add_bcast_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3670
TEST_CASE(implicit_pow_bcast_test)
3671
3672
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3673
3674
3675
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 4, 1}});
3676
3677
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3678
    mm->add_instruction(migraphx::make_op("pow"), l0, l3);
Khalique's avatar
Khalique committed
3679

Shucai Xiao's avatar
Shucai Xiao committed
3680
    auto prog = optimize_onnx("implicit_pow_bcast_test.onnx");
3681
3682
3683
3684

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3685
TEST_CASE(implicit_sub_bcast_test)
3686
3687
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3688
3689
3690
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::uint64_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::uint64_type, {4, 5}});
3691
3692
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3693
    mm->add_instruction(migraphx::make_op("sub"), l0, l3);
Khalique's avatar
Khalique committed
3694

Shucai Xiao's avatar
Shucai Xiao committed
3695
    auto prog = optimize_onnx("implicit_sub_bcast_test.onnx");
3696
3697
3698
3699

    EXPECT(p == prog);
}

3700
3701
3702
TEST_CASE(initializer_not_an_input)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3703
    auto* mm             = p.get_main_module();
3704
    std::vector<float> w = {1, 2, 3, 4, 5, 6, 7, 8};
Shucai Xiao's avatar
Shucai Xiao committed
3705
3706
    auto l1 = mm->add_literal(migraphx::literal({migraphx::shape::float_type, {2, 4}}, w));
    auto l0 = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {5, 2}});
3707
    migraphx::add_apply_alpha_beta(*mm, {l0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
3708
    auto prog = optimize_onnx("initializer_not_an_input.onnx");
kahmed10's avatar
kahmed10 committed
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719

    EXPECT(p == prog);
}

TEST_CASE(instance_norm_test)
{
    std::vector<size_t> dims{1, 2, 3, 3};
    migraphx::shape s1{migraphx::shape::float_type, dims};
    migraphx::shape s2{migraphx::shape::float_type, {2}};

    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3720
3721
3722
3723
3724
    auto* mm   = p.get_main_module();
    auto x     = mm->add_parameter("0", s1);
    auto scale = mm->add_parameter("1", s2);
    auto bias  = mm->add_parameter("2", s2);

3725
    auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x);
3726
3727
3728
3729
3730
    auto l1   = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto l0   = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean});

    auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l0);

3731
3732
    auto epsilon_literal =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
3733
3734
3735
3736
3737
    auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal});

    auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2);
    auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l1, l3});

3738
3739
3740
3741
    auto scale_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), scale);
    auto bias_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), bias);
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
    auto l5  = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast);
    auto ret = mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast);
    mm->add_return({ret});

    migraphx::onnx_options options;
    auto prog = migraphx::parse_onnx("instance_norm_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(instance_norm_dyn_batch_test)
{
    // instancenorm with dynamic input in the 0'th (batch) dimension
    migraphx::shape s1{migraphx::shape::float_type, {{1, 2, {2}}, {2, 2}, {3, 3}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {2}};

    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto x     = mm->add_parameter("0", s1);
    auto scale = mm->add_parameter("1", s2);
    auto bias  = mm->add_parameter("2", s2);

    auto mean     = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x);
    auto l1       = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto l0       = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean});
    auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l0);
    auto epsilon_literal =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
    auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal});
kahmed10's avatar
kahmed10 committed
3771

3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
    auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2);
    auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l1, l3});

    auto scale_bcast = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), scale, x);
    auto bias_bcast  = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), bias, x);
    auto l5          = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast);
    auto ret         = mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 2, {2}};
    auto prog = migraphx::parse_onnx("instance_norm_dyn_batch_test.onnx", options);
3784
3785
3786
3787

    EXPECT(p == prog);
}

3788
3789
3790
3791
3792
3793
3794
TEST_CASE(instance_norm_half_test)
{
    std::vector<size_t> dims{1, 2, 3, 3};
    migraphx::shape s1{migraphx::shape::half_type, dims};
    migraphx::shape s2{migraphx::shape::half_type, {2}};

    migraphx::program p;
3795
3796
3797
3798
3799
    auto* mm        = p.get_main_module();
    auto x_fp16     = mm->add_parameter("0", s1);
    auto scale_fp16 = mm->add_parameter("1", s2);
    auto bias_fp16  = mm->add_parameter("2", s2);

3800
    // conversion of half type to float is enabled by default
3801
3802
3803
3804
3805
3806
    auto x = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), x_fp16);
    auto scale = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), scale_fp16);
    auto bias = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), bias_fp16);
3807
3808

    auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x);
3809
3810
3811
    auto l0   = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto l1   = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean});

3812
    auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l1);
3813
3814
    // type of epsilon_literal is same as 0'th input; convert instruction will be added by
    // add_common_op
3815
    auto epsilon_literal =
3816
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
3817
3818
3819
3820
3821
    auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal});

    auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2);
    auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l0, l3});

3822
3823
3824
3825
    auto scale_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), scale);
    auto bias_bcast = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), bias);
3826
3827
3828
3829
    auto l5                 = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast);
    auto instance_norm_fp32 = mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast);
    mm->add_instruction(migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}),
                        instance_norm_fp32);
3830
3831
3832
3833
3834
    auto prog = optimize_onnx("instance_norm_half_test.onnx");

    EXPECT(p == prog);
}

3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
TEST_CASE(instance_norm_dyn_batch_half_test)
{
    // instancenorm with half type, dynamic input in the 0'th (batch) dimension
    migraphx::shape s1{migraphx::shape::half_type, {{1, 2, {2}}, {2, 2}, {3, 3}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::half_type, {2}};

    migraphx::program p;
    auto* mm        = p.get_main_module();
    auto x_fp16     = mm->add_parameter("0", s1);
    auto scale_fp16 = mm->add_parameter("1", s2);
    auto bias_fp16  = mm->add_parameter("2", s2);

    // conversion of half type to float is enabled by default
    auto x = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), x_fp16);
    auto scale = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), scale_fp16);
    auto bias = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), bias_fp16);

    auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x);
    auto l0   = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto l1   = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean});

    auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l1);
    // type of epsilon_literal is same as 0'th input; convert instruction will be added by
    // add_common_op
    auto epsilon_literal =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
    auto l2 = add_common_op(*mm, migraphx::make_op("add"), {variance, epsilon_literal});

    auto l3 = mm->add_instruction(migraphx::make_op("rsqrt"), l2);
    auto l4 = add_common_op(*mm, migraphx::make_op("mul"), {l0, l3});

    auto scale_bcast = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), scale, x);
    auto bias_bcast  = mm->add_instruction(migraphx::make_op("broadcast", {{"axis", 1}}), bias, x);
    auto l5          = mm->add_instruction(migraphx::make_op("mul"), l4, scale_bcast);
    auto instance_norm_fp32 = mm->add_instruction(migraphx::make_op("add"), l5, bias_bcast);
    auto ret                = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}),
        instance_norm_fp32);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 2, {2}};
    auto prog = migraphx::parse_onnx("instance_norm_dyn_batch_half_test.onnx", options);
    EXPECT(p == prog);
}

3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
TEST_CASE(instance_norm_type_mismatch_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("instance_norm_type_mismatch_test.onnx"); }));
}

TEST_CASE(instance_norm_invalid_type_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("instance_norm_invalid_type_test.onnx"); }));
}

TEST_CASE(instance_norm_nonbroadcastable_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("instance_norm_nonbroadcastable_test.onnx"); }));
}

Khalique's avatar
Khalique committed
3899
TEST_CASE(leaky_relu_test)
3900
3901
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3902
    auto* mm    = p.get_main_module();
Khalique's avatar
Khalique committed
3903
    float alpha = 0.01f;
Shucai Xiao's avatar
Shucai Xiao committed
3904
    auto l0     = mm->add_parameter("0", {migraphx::shape::float_type, {3}});
3905
    mm->add_instruction(migraphx::make_op("leaky_relu", {{"alpha", alpha}}), l0);
Khalique's avatar
Khalique committed
3906

Shucai Xiao's avatar
Shucai Xiao committed
3907
    auto prog = optimize_onnx("leaky_relu_test.onnx");
3908
3909
3910
3911

    EXPECT(p == prog);
}

3912
3913
3914
TEST_CASE(less_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3915
    auto* mm = p.get_main_module();
3916
3917
3918
    migraphx::shape s{migraphx::shape::float_type, {2, 3}};
    std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};

Shucai Xiao's avatar
Shucai Xiao committed
3919
3920
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
3921
3922
3923
    auto le     = mm->add_instruction(migraphx::make_op("less"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
3924
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
3925
        le);
Shucai Xiao's avatar
Shucai Xiao committed
3926
    mm->add_return({ret});
3927
3928
3929
3930
3931
3932
3933
3934

    auto prog = migraphx::parse_onnx("less_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(less_bool_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3935
    auto* mm = p.get_main_module();
3936
3937
3938
    migraphx::shape sf{migraphx::shape::float_type, {2, 3}};
    migraphx::shape sb{migraphx::shape::bool_type, {2, 3}};

Shucai Xiao's avatar
Shucai Xiao committed
3939
3940
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
3941
3942
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
3943
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
3944
3945
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("less"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
3946
    mm->add_return({ret});
3947
3948
3949
3950
3951

    auto prog = migraphx::parse_onnx("less_bool_test.onnx");
    EXPECT(p == prog);
}

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
3952
3953
3954
3955
3956
3957
3958
TEST_CASE(lessorequal_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto input1 = mm->add_parameter("x1", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {3}});
kahmed10's avatar
kahmed10 committed
3959
    auto temp   = mm->add_instruction(migraphx::make_op("greater"), input1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
3960
3961
3962
    auto bt     = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), temp);
    auto le = mm->add_instruction(migraphx::make_op("not"), bt);
kahmed10's avatar
kahmed10 committed
3963

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
3964
3965
3966
3967
3968
3969
    mm->add_return({le});

    auto prog = migraphx::parse_onnx("lessorequal_test.onnx");
    EXPECT(p == prog);
}

3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
migraphx::program make_layer_norm(const std::vector<int64_t>& input_shape,
                                  const std::vector<int64_t>& scale_bias_shape,
                                  const std::vector<int64_t>& reduce_axes,
                                  size_t skipped_axis,
                                  bool skip_bias                      = false,
                                  const float eps_value               = 1e-5f,
                                  const migraphx::shape::type_t dtype = migraphx::shape::float_type)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto x     = mm->add_parameter("x", {dtype, input_shape});
    auto scale = mm->add_parameter("scale", {dtype, scale_bias_shape});
    migraphx::instruction_ref bias;
    if(not skip_bias)
    {
        bias = mm->add_parameter("bias", {dtype, scale_bias_shape});
    }

    auto eps = mm->add_literal(migraphx::literal{dtype, {eps_value}});

    auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", reduce_axes}}), x);
    auto x_sub_mean    = add_common_op(*mm, migraphx::make_op("sub"), {x, mean});
    auto x_sqdiff_mean = add_common_op(*mm, migraphx::make_op("sqdiff"), {x, mean});
    auto var     = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", reduce_axes}}),
                                   x_sqdiff_mean);
    auto var_eps = add_common_op(*mm, migraphx::make_op("add"), {var, eps});
    auto rsqrt   = mm->add_instruction(migraphx::make_op("rsqrt"), {var_eps});
    auto result  = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, rsqrt});
    migraphx::instruction_ref scale_bcast = scale;
    migraphx::instruction_ref bias_bcast  = bias;
    if(skipped_axis > 0)
    {
        scale_bcast = mm->add_instruction(
            migraphx::make_op("broadcast", {{"axis", skipped_axis}, {"out_lens", input_shape}}),
            scale);
        if(not skip_bias)
        {
            bias_bcast = mm->add_instruction(
                migraphx::make_op("broadcast", {{"axis", skipped_axis}, {"out_lens", input_shape}}),
                bias);
        }
    }
    auto scaled = mm->add_instruction(migraphx::make_op("mul"), {result, scale_bcast});
    if(not skip_bias)
    {
        mm->add_instruction(migraphx::make_op("add"), {scaled, bias_bcast});
    }

    return p;
}

TEST_CASE(layer_norm_2d_axis_zero_test)
{
    migraphx::program p = make_layer_norm({3, 4}, {3, 4}, {0, 1}, 0);

    auto prog = optimize_onnx("layer_norm_2d_axis_zero_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_2d_axis_one_test)
{
    migraphx::program p = make_layer_norm({3, 4}, {4}, {1}, 1);

    auto prog = optimize_onnx("layer_norm_2d_axis_one_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_2d_axis_minus_one_test)
{
    migraphx::program p = make_layer_norm({3, 4}, {4}, {1}, 1);

    auto prog = optimize_onnx("layer_norm_2d_axis_one_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_3d_test)
{
    migraphx::program p = make_layer_norm({1, 4, 2}, {2}, {2}, 2);

    auto prog = optimize_onnx("layer_norm_3d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_3d_half_test)
{
    migraphx::program p =
        make_layer_norm({1, 4, 2}, {2}, {2}, 2, false, 1e-5f, migraphx::shape::half_type);

    auto prog = optimize_onnx("layer_norm_3d_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_4d_test)
{
    migraphx::program p = make_layer_norm({3, 3, 3, 3}, {3}, {3}, 3);

    auto prog = optimize_onnx("layer_norm_4d_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_4d_half_test)
{
    migraphx::program p =
        make_layer_norm({3, 3, 3, 3}, {3}, {3}, 3, false, 1e-5f, migraphx::shape::half_type);

    auto prog = optimize_onnx("layer_norm_4d_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_invalid_axis_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("layer_norm_invalid_axis_error_test.onnx"); }));
}

TEST_CASE(layer_norm_invalid_minus_axis_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("layer_norm_invalid_minus_axis_error_test.onnx"); }));
}

TEST_CASE(layer_norm_invalid_input_count_error_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("layer_norm_invalid_input_count_error_test.onnx"); }));
}

TEST_CASE(layer_norm_without_bias_test)
{
    migraphx::program p = make_layer_norm({1, 2}, {2}, {1}, 1, true);

    auto prog = optimize_onnx("layer_norm_without_bias_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(layer_norm_small_eps_half_test)
{
    migraphx::program p =
        make_layer_norm({1, 2}, {2}, {1}, 1, true, 1e-7, migraphx::shape::half_type);

    auto prog = optimize_onnx("layer_norm_small_eps_half_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4113
TEST_CASE(log_test)
4114
4115
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4116
4117
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
4118
    mm->add_instruction(migraphx::make_op("log"), input);
4119

Shucai Xiao's avatar
Shucai Xiao committed
4120
    auto prog = optimize_onnx("log_test.onnx");
4121
4122
4123
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
4124
4125
4126
4127
4128
4129
4130
TEST_CASE(logical_and_bcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::bool_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::bool_type, {4, 5}});
    auto l2  = mm->add_instruction(
4131
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
    auto ret = mm->add_instruction(migraphx::make_op("logical_and"), l0, l2);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("logical_and_bcast_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(logical_or_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::bool_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::bool_type, {2, 3, 4, 5}});
    auto ret = mm->add_instruction(migraphx::make_op("logical_or"), l0, l1);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("logical_or_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(logical_xor_bcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::bool_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::bool_type, {4, 1}});
    auto l2  = mm->add_instruction(
4161
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
4162
4163
4164
4165
4166
4167
4168
4169
    auto ret = mm->add_instruction(migraphx::make_op("logical_xor"), l0, l2);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("logical_xor_bcast_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4170
TEST_CASE(logsoftmax_test)
4171
4172
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4173
4174
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
Khalique's avatar
Khalique committed
4175
    int axis = 1;
4176
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
4177
    auto prog = optimize_onnx("logsoftmax_test.onnx");
4178
4179
4180
4181

    EXPECT(p == prog);
}

4182
4183
4184
4185
4186
4187
4188
TEST_CASE(logsoftmax_nonstd_input_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {6, 9}});
    auto l1  = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {4, 4}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
4189
    auto l2 = mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", -1}}), l1);
4190
4191
4192
4193
4194
4195
4196
    mm->add_return({l2});

    auto prog = migraphx::parse_onnx("logsoftmax_nonstd_input_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
TEST_CASE(loop_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape su{migraphx::shape::float_type};
    auto a = mm->add_parameter("a", su);
    auto b = mm->add_parameter("b", su);
    migraphx::shape si{migraphx::shape::int64_type};
    auto max_iter = mm->add_literal(migraphx::literal(si, {10}));
    migraphx::shape sc{migraphx::shape::bool_type};
    auto icond = mm->add_literal(migraphx::literal(sc, {1}));
    mm->add_instruction(migraphx::make_op("undefined"));

    auto* body = p.create_module("Loop_3_loop");
    body->add_parameter("iteration_num", {migraphx::shape::int64_type});
    body->add_parameter("keep_going_inp", {migraphx::shape::bool_type});
    auto var = body->add_parameter("b_in", su);

    auto ad = body->add_instruction(migraphx::make_op("add"), a, var);
    auto sb = body->add_instruction(migraphx::make_op("sub"), a, var);
    auto gt = body->add_instruction(migraphx::make_op("greater"), ad, sb);
    auto cv = body->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), gt);
    auto ad1 = body->add_instruction(migraphx::make_op("add"), sb, sb);
    body->add_return({cv, sb, ad, ad1});

    auto lp = mm->add_instruction(
        migraphx::make_op("loop", {{"max_iterations", 10}}), {max_iter, icond, b}, {body});
    auto r0 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), lp);
    mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), lp);
    auto r2 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), lp);
    mm->add_return({r0, r2});

    auto prog = migraphx::parse_onnx("loop_default_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(loop_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape si{migraphx::shape::int64_type, {1}};
    auto max_iter = mm->add_parameter("max_trip_count", si);
    migraphx::shape sc{migraphx::shape::bool_type, {1}};
    auto icond = mm->add_parameter("keep_going_cond", sc);
    migraphx::shape su{migraphx::shape::float_type, {1}};
    auto a = mm->add_parameter("a", su);
    auto b = mm->add_parameter("b", su);

    auto* body = p.create_module("Loop_4_loop");
    body->add_parameter("iteration_num", si);
    body->add_parameter("keep_going_inp", sc);
    auto var = body->add_parameter("b_in", su);

    auto ad = body->add_instruction(migraphx::make_op("add"), a, var);
    auto sb = body->add_instruction(migraphx::make_op("sub"), a, var);
    auto gt = body->add_instruction(migraphx::make_op("greater"), ad, sb);
    auto cv = body->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::bool_type}}), gt);
    auto ad1 = body->add_instruction(migraphx::make_op("add"), sb, sb);
    body->add_return({cv, sb, ad, ad1});

    auto lp = mm->add_instruction(
        migraphx::make_op("loop", {{"max_iterations", 10}}), {max_iter, icond, b}, {body});
    auto r0 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), lp);
    mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), lp);
    auto r2 = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), lp);
    mm->add_return({r0, r2});

    auto prog = migraphx::parse_onnx("loop_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
TEST_CASE(lpnormalization_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<std::size_t> input_lens{3, 4};
    auto input_type = migraphx::shape::float_type;
    migraphx::shape s{input_type, input_lens};
    auto x = mm->add_parameter("x", s);

    std::ptrdiff_t axis = 0;
    auto p_val          = mm->add_instruction(migraphx::make_op("mul"), x, x);
    auto norms = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {axis}}}), p_val);
    norms      = mm->add_instruction(migraphx::make_op("sqrt"), norms);
    norms =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}), norms);
    auto zero_mb =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {0.}}));
    auto one_mb =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1.}}));
    auto is_zero = mm->add_instruction(migraphx::make_op("equal"), norms, zero_mb);
    auto norms_zeros_to_one =
        mm->add_instruction(migraphx::make_op("where"), is_zero, one_mb, norms);
    mm->add_instruction(migraphx::make_op("div"), x, norms_zeros_to_one);

    auto prog = optimize_onnx("lpnormalization_default_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(lpnormalization_axis_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("lpnormalization_axis_error_test.onnx"); }));
}

TEST_CASE(lpnormalization_p_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("lpnormalization_p_error_test.onnx"); }));
}

4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
TEST_CASE(lppool_l1_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 3, 5}});
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::lpnorm},
                                           {"padding", {0, 0}},
                                           {"stride", {1}},
                                           {"lengths", {3}},
4323
                                           {"dilations", {1}},
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
                                           {"lp_order", 1}}),
                        l0);
    auto prog = optimize_onnx("lppool_l1_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(lppool_l2_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", {migraphx::shape::float_type, {1, 3, 5}});
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::lpnorm},
                                           {"padding", {0, 0}},
                                           {"stride", {1}},
                                           {"lengths", {3}},
4340
                                           {"dilations", {1}},
4341
4342
4343
4344
4345
4346
                                           {"lp_order", 2}}),
                        l0);
    auto prog = optimize_onnx("lppool_l2_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4347
TEST_CASE(lrn_test)
4348
4349
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4350
4351
    auto* mm = p.get_main_module();
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 28, 24, 24}});
Khalique's avatar
Khalique committed
4352
4353
4354
4355
4356
    migraphx::op::lrn op;
    op.size  = 5;
    op.alpha = 0.0001;
    op.beta  = 0.75;
    op.bias  = 1.0;
Shucai Xiao's avatar
Shucai Xiao committed
4357
    mm->add_instruction(op, l0);
Shucai Xiao's avatar
Shucai Xiao committed
4358
    auto prog = optimize_onnx("lrn_test.onnx");
4359
4360
4361
4362

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4363
TEST_CASE(matmul_bmbm_test)
4364
4365
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4366
4367
4368
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 6, 7}});
    auto l1 = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {5, 2, 1, 7, 8}});
4369
    auto bl0 = mm->add_instruction(
4370
        migraphx::make_op("multibroadcast", {{"out_lens", {5, 2, 3, 6, 7}}}), l0);
4371
    auto bl1 = mm->add_instruction(
4372
        migraphx::make_op("multibroadcast", {{"out_lens", {5, 2, 3, 7, 8}}}), l1);
4373
    migraphx::add_apply_alpha_beta(*mm, {bl0, bl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
4374
    auto prog = optimize_onnx("matmul_bmbm_test.onnx");
4375
4376
4377
4378

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4379
TEST_CASE(matmul_bmv_test)
4380
{
Khalique's avatar
Khalique committed
4381
    migraphx::program p;
4382
4383
4384
4385
4386
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 6, 7}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {7}});
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
    auto bsl1 =
4387
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 7, 1}}}), sl1);
4388
    auto res =
4389
        migraphx::add_apply_alpha_beta(*mm, {l0, bsl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4390
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), res);
4391

Shucai Xiao's avatar
Shucai Xiao committed
4392
    auto prog = optimize_onnx("matmul_bmv_test.onnx");
4393

Khalique's avatar
Khalique committed
4394
    EXPECT(p == prog);
4395
4396
}

Khalique's avatar
Khalique committed
4397
TEST_CASE(matmul_mv_test)
4398
4399
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4400
4401
4402
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {6, 7}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {7}});
4403
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
4404
    auto res = migraphx::add_apply_alpha_beta(*mm, {l0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4405
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), res);
Khalique's avatar
Khalique committed
4406

Shucai Xiao's avatar
Shucai Xiao committed
4407
    auto prog = optimize_onnx("matmul_mv_test.onnx");
4408
4409
4410
4411

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4412
TEST_CASE(matmul_vbm_test)
4413
4414
{
    migraphx::program p;
4415
4416
4417
4418
4419
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {7}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {5, 7, 8}});
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
    auto bsl0 =
4420
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5, 1, 7}}}), sl0);
4421
    auto res =
4422
        migraphx::add_apply_alpha_beta(*mm, {bsl0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4423
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), res);
Khalique's avatar
Khalique committed
4424

Shucai Xiao's avatar
Shucai Xiao committed
4425
    auto prog = optimize_onnx("matmul_vbm_test.onnx");
4426
4427
4428
4429

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4430
TEST_CASE(matmul_vm_test)
4431
4432
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4433
4434
4435
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {7}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {7, 8}});
4436
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
4437
    auto res = migraphx::add_apply_alpha_beta(*mm, {sl0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4438
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
Khalique's avatar
Khalique committed
4439

Shucai Xiao's avatar
Shucai Xiao committed
4440
    auto prog = optimize_onnx("matmul_vm_test.onnx");
4441
4442
4443
4444

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4445
TEST_CASE(matmul_vv_test)
4446
4447
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4448
4449
4450
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {7}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {7}});
4451
4452
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
4453
    auto res =
4454
        migraphx::add_apply_alpha_beta(*mm, {sl0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4455
4456
    auto sr0 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), sr0);
4457

Shucai Xiao's avatar
Shucai Xiao committed
4458
    auto prog = optimize_onnx("matmul_vv_test.onnx");
4459
4460
4461
4462

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
4463
4464
4465
4466
TEST_CASE(matmul_dyn_mm_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4467
4468
4469
4470
    auto l0 =
        mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {{4, 8, {6}}, {7, 7}}});
    auto l1 =
        mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {{7, 7}, {1, 5, {3}}}});
Charlie Lin's avatar
Charlie Lin committed
4471
4472
4473
4474
    auto ret = migraphx::add_apply_alpha_beta(*mm, {l0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    mm->add_return({ret});

    migraphx::onnx_options options;
4475
4476
    options.map_dyn_input_dims["1"] = {{4, 8, {6}}, {7, 7}};
    options.map_dyn_input_dims["2"] = {{7, 7}, {1, 5, {3}}};
Charlie Lin's avatar
Charlie Lin committed
4477
4478
4479
4480
4481
4482
4483
4484
4485
    auto prog                       = parse_onnx("matmul_dyn_mm_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(matmul_dyn_mv_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4486
4487
    auto l0 =
        mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {{4, 8, {6}}, {7, 7}}});
Charlie Lin's avatar
Charlie Lin committed
4488
4489
4490
4491
4492
4493
4494
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {7}});
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
    auto res = migraphx::add_apply_alpha_beta(*mm, {l0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    auto ret = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), res);
    mm->add_return({ret});

    migraphx::onnx_options options;
4495
    options.map_dyn_input_dims["1"] = {{4, 8, {6}}, {7, 7}};
Charlie Lin's avatar
Charlie Lin committed
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
    auto prog                       = parse_onnx("matmul_dyn_mv_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(matmul_dyn_vm_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {7}});
    auto l1  = mm->add_parameter(
4507
        "2", migraphx::shape{migraphx::shape::float_type, {{7, 7}, {4, 10, {8}}}});
Charlie Lin's avatar
Charlie Lin committed
4508
4509
4510
4511
4512
4513
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
    auto res = migraphx::add_apply_alpha_beta(*mm, {sl0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    auto ret = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
    mm->add_return({ret});

    migraphx::onnx_options options;
4514
    options.map_dyn_input_dims["2"] = {{7, 7}, {4, 10, {8}}};
Charlie Lin's avatar
Charlie Lin committed
4515
4516
4517
4518
4519
4520
4521
4522
4523
    auto prog                       = parse_onnx("matmul_dyn_vm_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(matmul_dyn_vv_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4524
    migraphx::shape::dynamic_dimension dd{5, 8, {7}};
Charlie Lin's avatar
Charlie Lin committed
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {dd}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {dd}});
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
    auto res =
        migraphx::add_apply_alpha_beta(*mm, {sl0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
    auto sr0 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
    auto ret = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), sr0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = dd;
    auto prog                     = parse_onnx("matmul_dyn_vv_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(matmul_dyn_broadcast_error)
{
    migraphx::onnx_options options;
4545
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
4546
4547
4548
    EXPECT(test::throws([&] { migraphx::parse_onnx("matmul_dyn_broadcast_error.onnx", options); }));
}

4549
4550
4551
TEST_CASE(matmulinteger_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4552
4553
4554
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("1", migraphx::shape{migraphx::shape::int8_type, {3, 6, 16}});
    auto l1  = mm->add_parameter("2", migraphx::shape{migraphx::shape::int8_type, {3, 16, 8}});
4555
    mm->add_instruction(migraphx::make_op("quant_dot"), l0, l1);
4556
4557
4558
4559
4560
4561

    auto prog = optimize_onnx("matmulinteger_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
4562
4563
4564
TEST_CASE(matmulinteger_dyn_error)
{
    migraphx::onnx_options options;
4565
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
4566
4567
4568
    EXPECT(test::throws([&] { migraphx::parse_onnx("matmulinteger_dyn_error.onnx", options); }));
}

Khalique's avatar
Khalique committed
4569
TEST_CASE(max_test)
4570
4571
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4572
4573
4574
4575
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input2 = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {3}});
4576
4577
    auto l0     = mm->add_instruction(migraphx::make_op("max"), input0, input1);
    mm->add_instruction(migraphx::make_op("max"), l0, input2);
4578

4579
4580
4581
    auto prog = optimize_onnx("max_test.onnx");

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
4582
}
4583

4584
4585
4586
TEST_CASE(maxpool_notset_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4587
4588
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
4589
4590
4591
4592
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 1, 1}},
                                           {"stride", {2, 2}},
4593
4594
                                           {"lengths", {6, 6}},
                                           {"dilations", {1, 1}}}),
4595
                        input);
4596
4597
4598
4599
4600
4601

    auto prog = optimize_onnx("maxpool_notset_test.onnx");

    EXPECT(p == prog);
}

4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
TEST_CASE(maxpool_dilate_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 4, 3}});
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {1, 1}},
                                           {"stride", {1}},
                                           {"lengths", {2}},
                                           {"dilations", {3}}}),
                        input);

    auto prog = optimize_onnx("maxpool_dilate_test.onnx");

    EXPECT(p == prog);
}

4620
4621
4622
TEST_CASE(maxpool_same_upper_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4623
4624
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
4625
4626
4627
4628
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 1, 1}},
                                           {"stride", {1, 1}},
4629
4630
                                           {"lengths", {2, 2}},
                                           {"dilations", {1, 1}}}),
4631
                        input);
4632
4633
4634
4635
4636
4637

    auto prog = optimize_onnx("maxpool_same_upper_test.onnx");

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
TEST_CASE(mean_invalid_broadcast_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("mean_invalid_broadcast_test.onnx"); }));
}

TEST_CASE(mean_single_input_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto data0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 3}});
    mm->add_return({data0});

    auto prog = migraphx::parse_onnx("mean_single_input_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(mean_test)
{
    const std::size_t num_data = 3;
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::half_type, {1, 2, 3}};
    auto data0   = mm->add_parameter("0", s);
    auto data1   = mm->add_parameter("1", s);
    auto data2   = mm->add_parameter("2", s);
    auto div_lit = mm->add_literal(migraphx::literal{migraphx::shape{s.type()}, {num_data}});
    auto divisor =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), div_lit);
    auto mean = mm->add_instruction(migraphx::make_op("div"), data0, divisor);
    divisor =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), div_lit);
    data1 = mm->add_instruction(migraphx::make_op("div"), data1, divisor);
    mean  = mm->add_instruction(migraphx::make_op("add"), mean, data1);
    divisor =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), div_lit);
    data2 = mm->add_instruction(migraphx::make_op("div"), data2, divisor);
    mean  = mm->add_instruction(migraphx::make_op("add"), mean, data2);

    auto prog = optimize_onnx("mean_fp16_test.onnx");

    EXPECT(p == prog);
}

4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
TEST_CASE(mean_integral_test)
{
    const std::size_t num_data = 10;
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::int32_type, {2, 2, 2}};

    auto mean = mm->add_parameter("0", s);
    for(std::size_t i = 1; i < num_data; ++i)
    {
        auto data = mm->add_parameter(std::to_string(i), s);
        mean      = mm->add_instruction(migraphx::make_op("add"), mean, data);
    }

    auto div_lit = mm->add_literal(migraphx::literal{migraphx::shape{s.type()}, {num_data}});
    auto divisor =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s.lens()}}), div_lit);
    mean = mm->add_instruction(migraphx::make_op("div"), mean, divisor);

    auto prog = optimize_onnx("mean_integral_test.onnx");

    EXPECT(p == prog);
}

4706
4707
void mvn_n_rank_test(std::vector<int64_t> axes,
                     std::vector<size_t> input_shape,
Paul's avatar
Paul committed
4708
                     const migraphx::program& prog)
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
{
    using migraphx::make_op;

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

    auto data = mm->add_parameter("data", {migraphx::shape::float_type, std::move(input_shape)});
    auto data_mean         = mm->add_instruction(make_op("reduce_mean", {{"axes", axes}}), data);
    auto data_mean_squared = add_common_op(*mm, make_op("mul"), {data_mean, data_mean});

    auto data_squared = add_common_op(*mm, make_op("mul"), {data, data});
    auto data_squared_mean =
        mm->add_instruction(make_op("reduce_mean", {{"axes", axes}}), data_squared);

    auto mean_sub = add_common_op(*mm, make_op("sub"), {data_squared_mean, data_mean_squared});
    auto std      = add_common_op(*mm, make_op("sqrt"), {mean_sub});

    auto dividend = add_common_op(*mm, make_op("sub"), {data, data_mean});
    auto epsilon  = mm->add_literal({migraphx::shape::float_type, {1e-9}});
    auto divisor  = add_common_op(*mm, make_op("add"), {std, epsilon});
    add_common_op(*mm, make_op("div"), {dividend, divisor});

    EXPECT(p == prog);
}

TEST_CASE(mvn_default_axes_test)
{
Paul's avatar
Paul committed
4736
    mvn_n_rank_test({0, 2, 3}, {2, 2, 2, 2}, optimize_onnx("mvn_default_axes_test.onnx"));
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
}

TEST_CASE(mvn_default_axes_rank_too_small_test)
{
    EXPECT(
        test::throws([&] { migraphx::parse_onnx("mvn_default_axes_rank_too_small_test.onnx"); }));
}

TEST_CASE(mvn_default_axes_rank_too_big_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("mvn_default_axes_rank_too_big_test.onnx"); }));
}

Paul's avatar
Paul committed
4750
TEST_CASE(mvn_rank_2_test) { mvn_n_rank_test({1}, {2, 2}, optimize_onnx("mvn_rank_2_test.onnx")); }
4751

Paul's avatar
Format  
Paul committed
4752
4753
4754
4755
TEST_CASE(mvn_rank_3_test)
{
    mvn_n_rank_test({0, 1}, {2, 2, 2}, optimize_onnx("mvn_rank_3_test.onnx"));
}
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766

TEST_CASE(mvn_axes_rank_too_small_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("mvn_axes_rank_too_small_test.onnx"); }));
}

TEST_CASE(mvn_axes_rank_too_big_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("mvn_axes_rank_too_big_test.onnx"); }));
}

Khalique's avatar
Khalique committed
4767
4768
4769
TEST_CASE(min_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4770
4771
4772
4773
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input2 = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {3}});
4774
4775
    auto l0     = mm->add_instruction(migraphx::make_op("min"), input0, input1);
    mm->add_instruction(migraphx::make_op("min"), l0, input2);
Khalique's avatar
Khalique committed
4776

4777
4778
4779
    auto prog = optimize_onnx("min_test.onnx");

    EXPECT(p == prog);
4780
4781
}

4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
TEST_CASE(mod_test)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::int32_type, {3, 3, 3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {3, 3, 3}});
    mm->add_instruction(migraphx::make_op("mod"), input0, input1);

    auto prog = optimize_onnx("mod_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(mod_test_half)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("mod_test_half.onnx"); }));
}

TEST_CASE(mod_test_different_dtypes)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::int16_type, {3, 3, 3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {3, 3, 3}});
    add_common_op(*mm, migraphx::make_op("mod"), {input0, input1});

    auto prog = optimize_onnx("mod_test_different_dtypes.onnx");

    EXPECT(p == prog);
}

TEST_CASE(mod_test_fmod)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 3, 3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 3, 3}});
    mm->add_instruction(migraphx::make_op("fmod"), input0, input1);

    auto prog = optimize_onnx("mod_test_fmod.onnx");

    EXPECT(p == prog);
}

TEST_CASE(mod_test_fmod_half)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::half_type, {3, 3, 3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::half_type, {3, 3, 3}});
    mm->add_instruction(migraphx::make_op("fmod"), input0, input1);

    auto prog = optimize_onnx("mod_test_fmod_half.onnx");

    EXPECT(p == prog);
}

TEST_CASE(mod_test_fmod_different_dtypes)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 3, 3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::int32_type, {3, 3, 3}});
    add_common_op(*mm, migraphx::make_op("fmod"), {input0, input1});

    auto prog = optimize_onnx("mod_test_fmod_different_dtypes.onnx");

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
4852
4853
4854
4855
TEST_CASE(multinomial_test)
{
    migraphx::program p;
    auto* mm           = p.get_main_module();
Brian Pickrell's avatar
Brian Pickrell committed
4856
4857
4858
4859
    size_t sample_size = 13;
    size_t batch_size  = 3;
    size_t categories  = 10;
    float seed         = 0;
turneram's avatar
turneram committed
4860

Brian Pickrell's avatar
Brian Pickrell committed
4861
4862
4863
4864
4865
    auto input = mm->add_parameter(
        "input", migraphx::shape{migraphx::shape::float_type, {batch_size, categories}});
    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", {batch_size, 10}}}), maxes);
turneram's avatar
turneram committed
4866
4867
4868
4869
4870
    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);

Brian Pickrell's avatar
Brian Pickrell committed
4871
4872
4873
    migraphx::shape s{migraphx::shape::float_type, {1}};
    std::vector<float> seed_data = {seed};
    auto seed_input              = mm->add_literal(migraphx::literal(s, seed_data));
4874
4875
4876
    auto rand_dummy              = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {batch_size, sample_size}},
                          std::vector<float>(batch_size * sample_size)});
turneram's avatar
turneram committed
4877

Brian Pickrell's avatar
Brian Pickrell committed
4878
4879
    auto randoms = mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, rand_dummy);
    mm->add_instruction(migraphx::make_op("multinomial"), cdf, randoms);
turneram's avatar
turneram committed
4880
4881
4882
4883
4884
    auto prog = optimize_onnx("multinomial_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
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
TEST_CASE(multinomial_dyn_test)
{
    // compile-time random seed
    migraphx::program p;
    auto* mm           = p.get_main_module();
    size_t sample_size = 100000;
    size_t categories  = 5;
    float seed         = 1.3f;

    auto input = mm->add_parameter(
        "input",
        migraphx::shape{migraphx::shape::float_type, {{1, categories}, {categories, categories}}});

    auto maxes = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {1}}}), input);

    auto cdf = add_common_op(*mm, migraphx::make_op("sub"), {input, 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);

    migraphx::shape s{migraphx::shape::float_type, {1}};
    std::vector<float> seed_data = {seed};
    auto seed_input              = mm->add_literal(migraphx::literal(s, seed_data));

    // dynamic input only:  must calculate alloc_shape as (batch_size, sample_size)
    //                read the runtime input dimensions
    auto dim_of = mm->add_instruction(migraphx::make_op("dimensions_of", {{"end", 2}}), input);
    // make an argument of (1, 0)
    migraphx::shape lit_shape(migraphx::shape::int64_type, {2});
    std::vector<int64_t> data1{1, 0};
    auto l1        = mm->add_literal(lit_shape, data1);
    auto batch_arg = mm->add_instruction(migraphx::make_op("mul"), dim_of, l1);
    std::vector<int64_t> data2(2, 0);
    // make an argument of (0, sample_size)
    data2[1]         = sample_size;
    auto l2          = mm->add_literal(lit_shape, data2);
    auto alloc_shape = mm->add_instruction(migraphx::make_op("add"), batch_arg, l2);
    migraphx::shape compile_shape =
        migraphx::shape(migraphx::shape::float_type,
                        {input->get_shape().dyn_dims().front(), {sample_size, sample_size}});

    auto alloc = mm->add_instruction(
        migraphx::make_op("allocate", {{"shape", to_value(compile_shape)}}), alloc_shape);

    auto randoms = mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, alloc);
    auto ret     = mm->add_instruction(
        migraphx::make_op("multinomial", {{"dtype", migraphx::shape::float_type}}), cdf, randoms);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value  = {1, categories};
    options.print_program_on_error = true;
    auto prog                      = migraphx::parse_onnx("multinomial_dyn_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(multinomial_autoseed_dyn_test)
{
    // runtime random seed
    migraphx::program p;
    auto* mm           = p.get_main_module();
    size_t sample_size = 12;
    size_t categories  = 10;

    auto input = mm->add_parameter(
        "input", migraphx::shape{migraphx::shape::float_type, {{1, 10}, {10, 10}}});

    auto maxes = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {1}}}), input);

    auto cdf = add_common_op(*mm, migraphx::make_op("sub"), {input, 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);
    auto seed_input = mm->add_instruction(migraphx::make_op("random_seed"));

    // dynamic input only:  must calculate alloc_shape as (batch_size, sample_size)
    //                read the runtime input dimensions
    auto dim_of = mm->add_instruction(migraphx::make_op("dimensions_of", {{"end", 2}}), input);
    // make an argument of (1, 0)
    migraphx::shape lit_shape(migraphx::shape::int64_type, {2});
    std::vector<int64_t> data1{1, 0};
    auto l1        = mm->add_literal(lit_shape, data1);
    auto batch_arg = mm->add_instruction(migraphx::make_op("mul"), dim_of, l1);
    std::vector<int64_t> data2(2, 0);
    // make an argument of (0, sample_size)
    data2[1]         = sample_size;
    auto l2          = mm->add_literal(lit_shape, data2);
    auto alloc_shape = mm->add_instruction(migraphx::make_op("add"), batch_arg, l2);
    migraphx::shape compile_shape =
        migraphx::shape(migraphx::shape::float_type,
                        {input->get_shape().dyn_dims().front(), {sample_size, sample_size}});

    auto alloc = mm->add_instruction(
        migraphx::make_op("allocate", {{"shape", to_value(compile_shape)}}), alloc_shape);

    auto randoms = mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, alloc);
    auto ret     = mm->add_instruction(migraphx::make_op("multinomial"), cdf, randoms);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value  = {1, categories};
    options.print_program_on_error = true;
    auto prog = migraphx::parse_onnx("multinomial_autoseed_dyn_test.onnx", options);
    EXPECT(p == prog);
}

turneram's avatar
turneram committed
4991
4992
4993
4994
4995
TEST_CASE(multinomial_dtype_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("multinomial_dtype_error_test.onnx"); }));
}

4996
4997
TEST_CASE(multinomial_generated_seed_test)
{
Brian Pickrell's avatar
Brian Pickrell committed
4998
    // multinomial op. no longer generates its own randoms
4999
5000
5001
    auto p1 = optimize_onnx("multinomial_generated_seed_test.onnx");
    auto p2 = optimize_onnx("multinomial_generated_seed_test.onnx");

Brian Pickrell's avatar
Brian Pickrell committed
5002
    EXPECT(p1 == p2);
5003
5004
}

turneram's avatar
turneram committed
5005
5006
5007
5008
5009
TEST_CASE(multinomial_int64_test)
{
    migraphx::program p;
    auto* mm                      = p.get_main_module();
    size_t sample_size            = 10;
Brian Pickrell's avatar
Brian Pickrell committed
5010
5011
    float seed                    = 1.0;
    uint32_t batch_size           = 1;
turneram's avatar
turneram committed
5012
5013
5014
5015
    migraphx::shape::type_t dtype = migraphx::shape::type_t::int64_type;

    auto input = mm->add_parameter("input", migraphx::shape{migraphx::shape::float_type, {1, 10}});
    auto maxes = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {1}}}), input);
Brian Pickrell's avatar
Brian Pickrell committed
5016
5017

    auto cdf = add_common_op(*mm, migraphx::make_op("sub"), {input, maxes});
turneram's avatar
turneram committed
5018
5019
5020
5021
    cdf      = mm->add_instruction(migraphx::make_op("exp"), cdf);
    cdf      = mm->add_instruction(
        migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", false}}), cdf);

Brian Pickrell's avatar
Brian Pickrell committed
5022
5023
5024
    migraphx::shape s{migraphx::shape::float_type, {1}};
    std::vector<float> data = {seed};
    auto seed_input         = mm->add_literal(migraphx::literal(s, data));
turneram's avatar
turneram committed
5025

Brian Pickrell's avatar
Brian Pickrell committed
5026
    // static size
5027
5028
5029
    auto rand_dummy = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {batch_size, sample_size}},
                          std::vector<float>(batch_size * sample_size)});
Brian Pickrell's avatar
Brian Pickrell committed
5030
5031
    auto randoms = mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, rand_dummy);
    mm->add_instruction(migraphx::make_op("multinomial", {{"dtype", dtype}}), cdf, randoms);
turneram's avatar
turneram committed
5032
5033
5034
5035
5036
    auto prog = optimize_onnx("multinomial_int64_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
5037
TEST_CASE(no_pad_test)
5038
5039
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5040
5041
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
5042
    mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
5043
    auto prog = optimize_onnx("no_pad_test.onnx");
5044

Khalique's avatar
Khalique committed
5045
5046
5047
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5048
5049
5050
TEST_CASE(neg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5051
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5052
    migraphx::shape s{migraphx::shape::int64_type, {2, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
5053
    auto input = mm->add_parameter("0", s);
5054
    auto ret   = mm->add_instruction(migraphx::make_op("neg"), input);
Shucai Xiao's avatar
Shucai Xiao committed
5055
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
5056
5057
5058
5059
5060
5061

    auto prog = migraphx::parse_onnx("neg_test.onnx");

    EXPECT(p == prog);
}

5062
5063
5064
5065
TEST_CASE(neg_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5066
    migraphx::shape s{migraphx::shape::int64_type, {{1, 10}, {3, 3}}};
5067
5068
5069
5070
5071
    auto input = mm->add_parameter("0", s);
    auto ret   = mm->add_instruction(migraphx::make_op("neg"), input);
    mm->add_return({ret});

    migraphx::onnx_options options;
5072
    options.default_dyn_dim_value = {1, 10};
5073
5074
5075
5076
    auto prog                     = migraphx::parse_onnx("neg_dynamic_test.onnx", options);
    EXPECT(p == prog);
}

5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
TEST_CASE(nms_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sb{migraphx::shape::float_type, {1, 6, 4}};
    auto b = mm->add_parameter("boxes", sb);

    migraphx::shape ss{migraphx::shape::float_type, {1, 1, 6}};
    auto s = mm->add_parameter("scores", ss);

    migraphx::shape smo{migraphx::shape::int64_type, {1}};
    auto mo = mm->add_parameter("max_output_boxes_per_class", smo);

    migraphx::shape siou{migraphx::shape::float_type, {1}};
    auto iou = mm->add_parameter("iou_threshold", siou);

    migraphx::shape sst{migraphx::shape::float_type, {1}};
    auto st = mm->add_parameter("score_threshold", sst);

    auto ret = mm->add_instruction(
Charlie Lin's avatar
Charlie Lin committed
5097
        migraphx::make_op("nonmaxsuppression", {{"center_point_box", true}}), b, s, mo, iou, st);
5098
5099
5100
5101
5102
5103
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("nms_test.onnx");
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
5104
5105
5106
5107
TEST_CASE(nms_dynamic_batch_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5108
    migraphx::shape sb{migraphx::shape::float_type, {{1, 10}, {6, 6}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5109
    auto b = mm->add_parameter("boxes", sb);
5110
    migraphx::shape ss{migraphx::shape::float_type, {{1, 10}, {1, 1}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
    auto s = mm->add_parameter("scores", ss);
    migraphx::shape smo{migraphx::shape::int64_type, {1}};
    auto mo = mm->add_parameter("max_output_boxes_per_class", smo);
    migraphx::shape siou{migraphx::shape::float_type, {1}};
    auto iou = mm->add_parameter("iou_threshold", siou);
    migraphx::shape sst{migraphx::shape::float_type, {1}};
    auto st  = mm->add_parameter("score_threshold", sst);
    auto ret = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression",
                          {{"center_point_box", true}, {"use_dyn_output", true}}),
        b,
        s,
        mo,
        iou,
        st);
    mm->add_return({ret});

    migraphx::onnx_options options;
5129
    options.default_dyn_dim_value = {1, 10};
Charlie Lin's avatar
Charlie Lin committed
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
    options.use_dyn_output        = true;

    auto prog = migraphx::parse_onnx("nms_dynamic_batch_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(nms_dynamic_boxes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5140
    migraphx::shape sb{migraphx::shape::float_type, {{1, 1}, {6, 20}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5141
    auto b = mm->add_parameter("boxes", sb);
5142
    migraphx::shape ss{migraphx::shape::float_type, {{1, 1}, {1, 1}, {6, 20}}};
Charlie Lin's avatar
Charlie Lin committed
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
    auto s = mm->add_parameter("scores", ss);
    migraphx::shape smo{migraphx::shape::int64_type, {1}};
    auto mo = mm->add_parameter("max_output_boxes_per_class", smo);
    migraphx::shape siou{migraphx::shape::float_type, {1}};
    auto iou = mm->add_parameter("iou_threshold", siou);
    migraphx::shape sst{migraphx::shape::float_type, {1}};
    auto st  = mm->add_parameter("score_threshold", sst);
    auto ret = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression", {{"use_dyn_output", true}}), b, s, mo, iou, st);
    mm->add_return({ret});

    migraphx::onnx_options options;
5155
    options.default_dyn_dim_value = {6, 20};
Charlie Lin's avatar
Charlie Lin committed
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
    options.use_dyn_output        = true;

    auto prog = migraphx::parse_onnx("nms_dynamic_boxes_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(nms_dynamic_classes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sb{migraphx::shape::float_type, {1, 6, 4}};
    auto b = mm->add_parameter("boxes", sb);
5168
    migraphx::shape ss{migraphx::shape::float_type, {{1, 1}, {1, 10}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
    auto s = mm->add_parameter("scores", ss);
    migraphx::shape smo{migraphx::shape::int64_type, {1}};
    auto mo = mm->add_parameter("max_output_boxes_per_class", smo);
    migraphx::shape siou{migraphx::shape::float_type, {1}};
    auto iou = mm->add_parameter("iou_threshold", siou);
    migraphx::shape sst{migraphx::shape::float_type, {1}};
    auto st  = mm->add_parameter("score_threshold", sst);
    auto ret = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression", {{"use_dyn_output", true}}), b, s, mo, iou, st);
    mm->add_return({ret});

    migraphx::onnx_options options;
5181
    options.default_dyn_dim_value = {1, 10};
Charlie Lin's avatar
Charlie Lin committed
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
    options.use_dyn_output        = true;

    auto prog = migraphx::parse_onnx("nms_dynamic_classes_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(nms_overwrite_use_dyn_output_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sb{migraphx::shape::float_type, {1, 6, 4}};
    auto b = mm->add_parameter("boxes", sb);

    migraphx::shape ss{migraphx::shape::float_type, {1, 1, 6}};
    auto s = mm->add_parameter("scores", ss);

    migraphx::shape smo{migraphx::shape::int64_type, {1}};
    auto mo = mm->add_parameter("max_output_boxes_per_class", smo);

    migraphx::shape siou{migraphx::shape::float_type, {1}};
    auto iou = mm->add_parameter("iou_threshold", siou);

    migraphx::shape sst{migraphx::shape::float_type, {1}};
    auto st = mm->add_parameter("score_threshold", sst);

    auto ret = mm->add_instruction(
        migraphx::make_op("nonmaxsuppression", {{"use_dyn_output", true}}), b, s, mo, iou, st);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.use_dyn_output = true;

    auto prog = migraphx::parse_onnx("nms_use_dyn_output_false_test.onnx", options);
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
TEST_CASE(nonzero_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::bool_type, {2, 2}};
    auto data = mm->add_parameter("data", s);
    auto r    = mm->add_instruction(migraphx::make_op("nonzero"), data);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("nonzero_dynamic_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5231
5232
5233
TEST_CASE(nonzero_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5234
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5235
5236
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    std::vector<float> data = {1, 0, 1, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5237
    mm->add_literal(migraphx::literal(s, data));
Shucai Xiao's avatar
Shucai Xiao committed
5238
5239
5240

    migraphx::shape si{migraphx::shape::int64_type, {2, 3}};
    std::vector<int64_t> indices = {0, 1, 1, 0, 0, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5241
5242
    auto r                       = mm->add_literal(migraphx::literal(si, indices));
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5243
5244
5245
5246
5247
5248
5249
5250

    auto prog = migraphx::parse_onnx("nonzero_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(nonzero_int_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5251
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5252
    migraphx::shape s{migraphx::shape::int16_type, {2, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
5253
    std::vector<int> data = {1, 1, 0, 1, 0, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5254
    mm->add_literal(migraphx::literal(s, data.begin(), data.end()));
Shucai Xiao's avatar
Shucai Xiao committed
5255
5256
5257

    migraphx::shape si{migraphx::shape::int64_type, {2, 4}};
    std::vector<int64_t> indices = {0, 0, 1, 1, 0, 1, 0, 2};
Shucai Xiao's avatar
Shucai Xiao committed
5258
5259
    auto r                       = mm->add_literal(migraphx::literal(si, indices));
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5260
5261
5262
5263
5264

    auto prog = migraphx::parse_onnx("nonzero_int_test.onnx");
    EXPECT(p == prog);
}

5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
TEST_CASE(not_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::int32_type, {4}});
    auto ret = mm->add_instruction(migraphx::make_op("not"), l0);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("not_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(not_bool_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::bool_type, {4}});
    auto ret = mm->add_instruction(migraphx::make_op("not"), l0);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("not_bool_test.onnx");

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
5291
5292
5293
TEST_CASE(onehot_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5294
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5295
5296
    migraphx::shape s_ind{migraphx::shape::int32_type, {5, 2}};
    migraphx::shape s_val{migraphx::shape::half_type, {2}};
Shucai Xiao's avatar
Shucai Xiao committed
5297
5298
5299
    mm->add_literal(3);
    auto l_ind = mm->add_parameter("indices", s_ind);
    auto l_val = mm->add_parameter("values", s_val);
Shucai Xiao's avatar
Shucai Xiao committed
5300
5301
    migraphx::shape s_dep{migraphx::shape::half_type, {3, 3}};
    std::vector<float> data_dep{1, 0, 0, 0, 1, 0, 0, 0, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5302
    auto l_dep      = mm->add_literal(migraphx::literal(s_dep, data_dep));
5303
    auto gather_out = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), l_dep, l_ind);
5304
5305
    auto tr_out  = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {2, 0, 1}}}),
                                      gather_out);
5306
5307
5308
5309
5310
5311
    auto off_val = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), l_val);
    auto on_val = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), l_val);
    auto diff       = mm->add_instruction(migraphx::make_op("sub"), on_val, off_val);
    auto mb_off_val = mm->add_instruction(
5312
5313
5314
        migraphx::make_op("multibroadcast", {{"out_lens", {3, 5, 2}}}), off_val);
    auto mb_diff =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 5, 2}}}), diff);
5315
5316
    auto mul = mm->add_instruction(migraphx::make_op("mul"), tr_out, mb_diff);
    auto r   = mm->add_instruction(migraphx::make_op("add"), mul, mb_off_val);
Shucai Xiao's avatar
Shucai Xiao committed
5317
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5318
5319

    auto prog = migraphx::parse_onnx("onehot_test.onnx");
kahmed10's avatar
kahmed10 committed
5320
5321
5322
5323

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
5324
5325
5326
TEST_CASE(pad_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5327
5328
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
5329
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
5330
    auto prog = optimize_onnx("pad_test.onnx");
5331
5332
5333
5334

    EXPECT(p == prog);
}

5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
TEST_CASE(pad_asym_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 5}});
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 1, 0, 3, 0, 2, 0, 4}}}), l0);
    auto prog = optimize_onnx("pad_asym_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(pad_asym_invalid_pads_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("pad_asym_invalid_pads_error_test.onnx"); }));
}

5351
5352
5353
TEST_CASE(pad_3arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5354
5355
5356
5357
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
    mm->add_literal({migraphx::shape{migraphx::shape::float_type}, {1.0f}});
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {1, 1, 2, 2}});
5358
5359
    auto r = mm->add_instruction(
        migraphx::make_op("pad", {{"pads", {1, 1, 2, 2}}, {"value", 1.0f}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
5360
    mm->add_return({r});
5361
5362
5363
5364
5365
5366

    auto prog = migraphx::parse_onnx("pad_3arg_test.onnx");

    EXPECT(p == prog);
}

5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
TEST_CASE(pad_4arg_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 5}});
    // axes=[1,3]
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 3}});
    // constant_value=1
    mm->add_literal({migraphx::shape{migraphx::shape::float_type}, {1.0f}});
    // pads=[1,3,2,4]
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {1, 3, 2, 4}});
    auto r = mm->add_instruction(
        migraphx::make_op("pad", {{"pads", {0, 1, 0, 3, 0, 2, 0, 4}}, {"value", 1.0f}}), l0);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("pad_4arg_axes_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(pad_4arg_invalid_axes_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("pad_4arg_invalid_axes_error_test.onnx"); }));
}

TEST_CASE(pad_4arg_neg_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 5}});
    // axes=[-3,-1]
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {2}}, {-3, -1}});
    // constant_value=1
    mm->add_literal({migraphx::shape{migraphx::shape::float_type}, {1.0f}});
    // pads=[1,3,2,4]
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {1, 3, 2, 4}});
    auto r = mm->add_instruction(
        migraphx::make_op("pad", {{"pads", {0, 1, 0, 3, 0, 2, 0, 4}}, {"value", 1.0f}}), l0);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("pad_4arg_neg_axes_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
5412
5413
5414
5415
5416
TEST_CASE(pad_attr_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter(
5417
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4, {2}}}});
Charlie Lin's avatar
Charlie Lin committed
5418
5419
5420
5421
    auto ret = mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), x);
    mm->add_return({ret});

    migraphx::onnx_options options;
5422
    options.map_dyn_input_dims["0"] = {{2, 4, {2}}, {2, 4, {2}}};
Charlie Lin's avatar
Charlie Lin committed
5423
5424
5425
5426
5427
5428
5429
5430
5431
    auto prog                       = parse_onnx("pad_attr_dyn_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(pad_cnst_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter(
5432
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4, {2}}}});
Charlie Lin's avatar
Charlie Lin committed
5433
5434
5435
5436
5437
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {0, 2, 0, 1}});
    auto ret = mm->add_instruction(migraphx::make_op("pad", {{"pads", {0, 2, 0, 1}}}), x);
    mm->add_return({ret});

    migraphx::onnx_options options;
5438
    options.map_dyn_input_dims["0"] = {{2, 4, {2}}, {2, 4, {2}}};
Charlie Lin's avatar
Charlie Lin committed
5439
5440
5441
5442
5443
5444
5445
    auto prog                       = parse_onnx("pad_cnst_dyn_test.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(pad_dyn_reflect_error)
{
    migraphx::onnx_options options;
5446
    options.default_dyn_dim_value = {2, 4, {2}};
Charlie Lin's avatar
Charlie Lin committed
5447
5448
5449
    EXPECT(test::throws([&] { migraphx::parse_onnx("pad_dyn_reflect_error.onnx", options); }));
}

kahmed10's avatar
kahmed10 committed
5450
5451
5452
TEST_CASE(pad_reflect_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5453
5454
5455
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {0, 2, 0, 1}});
5456
5457
5458
5459
5460
5461
5462
    auto l1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 1}}, {"ends", {2, 2}}}), l0);
    auto l2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {2, 1}}}), l0);
    auto l3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {2, 1}}}), l0);
    auto r = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l1, l0, l3);
Shucai Xiao's avatar
Shucai Xiao committed
5463
    mm->add_return({r});
kahmed10's avatar
kahmed10 committed
5464
5465
5466
5467
5468
5469

    auto prog = migraphx::parse_onnx("pad_reflect_test.onnx");

    EXPECT(p == prog);
}

5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
TEST_CASE(pad_reflect_with_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {1}}, {1}});
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {2}}, {2, 1}});
    auto l1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 1}}, {"ends", {2, 2}}}), l0);
    auto l2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {2, 1}}}), l0);
    auto l3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {2, 1}}}), l0);
    auto r = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l1, l0, l3);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("pad_reflect_with_axes_test.onnx");

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
5491
5492
5493
TEST_CASE(pad_reflect_multiaxis_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5494
5495
5496
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3}});
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {4}}, {0, 2, 2, 0}});
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
    auto l1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 1}}, {"ends", {2, 2}}}), l0);
    auto l2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 2}}, {"ends", {2, 3}}}), l0);
    auto l3 = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l1, l0);
    auto l4 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {1, 5}}}), l3);
    auto l5 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {2, 5}}}), l3);
    auto r = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), l3, l4, l5);
Shucai Xiao's avatar
Shucai Xiao committed
5507
    mm->add_return({r});
kahmed10's avatar
kahmed10 committed
5508
5509
5510
5511
5512
5513

    auto prog = migraphx::parse_onnx("pad_reflect_multiaxis_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
5514
TEST_CASE(pow_test)
5515
5516
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5517
5518
5519
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
5520
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
5521

Shucai Xiao's avatar
Shucai Xiao committed
5522
    auto prog = optimize_onnx("pow_test.onnx");
5523
5524
5525
5526

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
TEST_CASE(pow_fp32_i64_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::int64_type, {2, 3, 4, 5}});
    auto l1f = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l1);
    auto ret = mm->add_instruction(migraphx::make_op("pow"), l0, l1f);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("pow_fp32_i64_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(pow_i64_fp32_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::int64_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l0f = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l0);
    auto fr = mm->add_instruction(migraphx::make_op("pow"), l0f, l1);
    auto ir = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::int64_type}}), fr);
    mm->add_return({ir});

    auto prog = migraphx::parse_onnx("pow_i64_fp32_test.onnx");

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
TEST_CASE(prefix_scan_sum)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal({migraphx::shape{migraphx::shape::int32_type, {1}, {1}}, {0}});
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2, 2}});
    auto ret = mm->add_instruction(
        migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", true}, {"reverse", true}}),
        l0);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("prefix_scan_sum_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5576
5577
5578
TEST_CASE(prelu_brcst_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5579
5580
5581
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 5}});
5582
    auto bl1 = mm->add_instruction(
5583
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
5584
    auto ret = mm->add_instruction(migraphx::make_op("prelu"), l0, bl1);
Shucai Xiao's avatar
Shucai Xiao committed
5585
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
5586
5587
5588
5589
5590
5591

    auto prog = migraphx::parse_onnx("prelu_brcst_test.onnx");

    EXPECT(p == prog);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
TEST_CASE(qlinearadd_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto a = mm->add_parameter("A", {migraphx::shape::uint8_type, {64}});
    auto b = mm->add_parameter("B", {migraphx::shape::uint8_type, {64}});

    auto sc_a   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_a = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {0}});

    auto sc_b   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_b = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {128}});

    auto sc_c   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_c = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {64}});

    auto scale_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_a);

    auto z_pt_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_a);

    auto fp_a =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), a, scale_a_bcast, z_pt_a_bcast);

    auto scale_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_b);

    auto z_pt_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_b);

    auto fp_b =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), b, scale_b_bcast, z_pt_b_bcast);

    auto fp_c = mm->add_instruction(migraphx::make_op("add"), fp_a, fp_b);

    auto scale_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_c);

    auto z_pt_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_c);

    auto c =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_c, scale_c_bcast, z_pt_c_bcast);

    mm->add_return({c});

    auto prog = migraphx::parse_onnx("qlinearadd_test.onnx");

Lakhinder Walia's avatar
Lakhinder Walia committed
5642
5643
5644
    EXPECT(p.sort() == prog.sort());
}

5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
TEST_CASE(qlinearaveragepool_notset_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto sc_x   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    auto z_pt_x = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {0}});

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {10}});

    auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::int8_type, {1, 1, 5, 5}});

    auto scale_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 5, 5}}}), sc_x);

    auto z_pt_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 5, 5}}}), z_pt_x);

    auto fp_x =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), x, scale_x_bcast, z_pt_x_bcast);

    auto fp_y =
        mm->add_instruction(migraphx::make_op("pooling",
                                              {{"mode", migraphx::op::pooling_mode::average},
                                               {"padding", {2, 2, 2, 2}},
                                               {"stride", {2, 2}},
                                               {"lengths", {6, 6}}}),
                            fp_x);

    fp_y = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {1, 1}}, {"ends", {2, 2}}}), fp_y);

    auto scale_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 1, 1}}}), sc_y);

    auto z_pt_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 1, 1}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});
    auto prog = migraphx::parse_onnx("qlinearaveragepool_notset_test.onnx");

    EXPECT(p == prog);
}

Zakor Gyula's avatar
Zakor Gyula committed
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
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
TEST_CASE(qlinearconcat_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {2}});

    auto sc_0   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    auto z_pt_0 = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {1}});

    auto sc_1   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.25}});
    auto z_pt_1 = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {0}});

    auto t0 = mm->add_parameter("t0", {migraphx::shape::int8_type, {2}});
    auto t1 = mm->add_parameter("t1", {migraphx::shape::int8_type, {3}});

    auto scale_0_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2}}}), sc_0);

    auto z_pt_0_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2}}}), z_pt_0);

    auto fp_0 =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), t0, scale_0_bcast, z_pt_0_bcast);

    auto scale_1_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), sc_1);

    auto z_pt_1_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3}}}), z_pt_1);

    auto fp_1 =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), t1, scale_1_bcast, z_pt_1_bcast);

    auto fp_y = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), fp_0, fp_1);

    auto scale_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), sc_y);

    auto z_pt_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});

    auto prog = migraphx::parse_onnx("qlinearconcat_test.onnx");

    EXPECT(p == prog);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
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
5791
5792
5793
5794
5795
5796
5797
TEST_CASE(qlinearconv_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x = mm->add_parameter("X", {migraphx::shape::uint8_type, {1, 1, 7, 7}});

    auto sc_x   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.00369204697}});
    auto z_pt_x = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {132}});

    auto w = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::uint8_type, {1, 1, 1, 1}}, {0}});

    auto sc_w   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.00172794575}});
    auto z_pt_w = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {255}});

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.00162681262}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {123}});

    auto scale_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 7, 7}}}), sc_x);

    auto z_pt_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 7, 7}}}), z_pt_x);

    auto fp_x =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), x, scale_x_bcast, z_pt_x_bcast);

    auto scale_w_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 1, 1}}}), sc_w);

    auto z_pt_w_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 1, 1}}}), z_pt_w);

    auto fp_w =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), w, scale_w_bcast, z_pt_w_bcast);

    auto fp_y = mm->add_instruction(migraphx::make_op("convolution"), fp_x, fp_w);

    auto scale_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 7, 7}}}), sc_y);

    auto z_pt_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 1, 7, 7}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});

    auto prog = migraphx::parse_onnx("qlinearconv_test.onnx");

5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
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
    EXPECT(p.sort() == prog.sort());
}

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

    auto x = mm->add_parameter("X", {migraphx::shape::uint8_type, {1, 3, 4, 4}});

    auto sc_x   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_x = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {128}});

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.025}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {64}});

    auto scale_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 3, 4, 4}}}), sc_x);

    auto z_pt_x_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 3, 4, 4}}}), z_pt_x);

    auto fp_x =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), x, scale_x_bcast, z_pt_x_bcast);

    auto fp_y =
        mm->add_instruction(migraphx::make_op("pooling",
                                              {{"mode", migraphx::op::pooling_mode::average},
                                               {"padding", {0, 0, 0, 0}},
                                               {"lengths", {4, 4}}}),
                            fp_x);

    auto scale_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 3, 1, 1}}}), sc_y);

    auto z_pt_y_bcast = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 3, 1, 1}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});

    auto prog = migraphx::parse_onnx("qlinearglobalavgpool_test.onnx");

Lakhinder Walia's avatar
Lakhinder Walia committed
5843
5844
5845
    EXPECT(p.sort() == prog.sort());
}

5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
TEST_CASE(qlinearleakyrelu_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x = mm->add_parameter("X", {migraphx::shape::int8_type, {64}});

    auto sc_x   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_x = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {0}});

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {10}});

    auto scale_x_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_x);

    auto z_pt_x_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_x);

    auto fp_x =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), x, scale_x_bcast, z_pt_x_bcast);

    auto fp_y = mm->add_instruction(migraphx::make_op("leaky_relu", {{"alpha", 1.1}}), fp_x);

    auto scale_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_y);

    auto z_pt_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});

    auto prog = migraphx::parse_onnx("qlinearleakyrelu_test.onnx");

    EXPECT(p.sort() == prog.sort());
}

Lakhinder Walia's avatar
Lakhinder Walia committed
5886
5887
5888
5889
5890
5891
5892
5893
5894
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
5927
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
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
TEST_CASE(qlinearmatmul_1D_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto a = mm->add_parameter("A", {migraphx::shape::uint8_type, {8}});
    auto b = mm->add_parameter("B", {migraphx::shape::uint8_type, {8}});

    auto sc_a   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_a = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {0}});

    auto sc_b   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_b = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {128}});

    auto sc_c   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_c = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {64}});

    auto scale_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8}}}), sc_a);

    auto z_pt_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8}}}), z_pt_a);

    auto fp_a =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), a, scale_a_bcast, z_pt_a_bcast);

    auto scale_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8}}}), sc_b);

    auto z_pt_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8}}}), z_pt_b);

    auto fp_b =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), b, scale_b_bcast, z_pt_b_bcast);

    auto sq_a = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), fp_a);

    auto sq_b = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), fp_b);

    auto fp_c = mm->add_instruction(migraphx::make_op("dot"), sq_a, sq_b);

    auto sq_c = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), fp_c);

    auto scale_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1}}}), sc_c);

    auto z_pt_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1}}}), z_pt_c);

    auto c =
        mm->add_instruction(migraphx::make_op("quantizelinear"), sq_c, scale_c_bcast, z_pt_c_bcast);

    mm->add_return({c});

    auto prog = migraphx::parse_onnx("qlinearmatmul_1D_test.onnx");

    EXPECT(p.sort() == prog.sort());
}

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

    auto a = mm->add_parameter("A", {migraphx::shape::uint8_type, {1, 8}});
    auto b = mm->add_parameter("B", {migraphx::shape::uint8_type, {8, 1}});

    auto sc_a   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_a = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {0}});

    auto sc_b   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_b = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {128}});

    auto sc_c   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_c = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {64}});

    auto scale_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 8}}}), sc_a);

    auto z_pt_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 8}}}), z_pt_a);

    auto fp_a =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), a, scale_a_bcast, z_pt_a_bcast);

    auto scale_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8, 1}}}), sc_b);

    auto z_pt_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {8, 1}}}), z_pt_b);

    auto fp_b =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), b, scale_b_bcast, z_pt_b_bcast);

    auto fp_c = mm->add_instruction(migraphx::make_op("dot"), fp_a, fp_b);

    auto scale_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 1}}}), sc_c);

    auto z_pt_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 1}}}), z_pt_c);

    auto c =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_c, scale_c_bcast, z_pt_c_bcast);

    mm->add_return({c});

    auto prog = migraphx::parse_onnx("qlinearmatmul_2D_test.onnx");

Lakhinder Walia's avatar
Lakhinder Walia committed
5995
5996
5997
    EXPECT(p.sort() == prog.sort());
}

Zakor Gyula's avatar
Zakor Gyula committed
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
TEST_CASE(qlinearmul_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto a = mm->add_parameter("A", {migraphx::shape::uint8_type, {64}});
    auto b = mm->add_parameter("B", {migraphx::shape::uint8_type, {64}});

    auto sc_a   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_a = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {0}});

    auto sc_b   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_b = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {16}});

    auto sc_c   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_c = mm->add_literal(migraphx::literal{migraphx::shape::uint8_type, {100}});

    auto scale_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_a);

    auto z_pt_a_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_a);

    auto fp_a =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), a, scale_a_bcast, z_pt_a_bcast);

    auto scale_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_b);

    auto z_pt_b_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_b);

    auto fp_b =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), b, scale_b_bcast, z_pt_b_bcast);

    auto fp_c = mm->add_instruction(migraphx::make_op("mul"), fp_a, fp_b);

    auto scale_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_c);

    auto z_pt_c_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_c);

    auto c =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_c, scale_c_bcast, z_pt_c_bcast);

    mm->add_return({c});

    auto prog = migraphx::parse_onnx("qlinearmul_test.onnx");

    EXPECT(p.sort() == prog.sort());
}

6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
TEST_CASE(qlinearsigmoid_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto x = mm->add_parameter("X", {migraphx::shape::int8_type, {64}});

    auto sc_x   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.05}});
    auto z_pt_x = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {0}});

    auto sc_y   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.0035}});
    auto z_pt_y = mm->add_literal(migraphx::literal{migraphx::shape::int8_type, {-128}});

    auto scale_x_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_x);

    auto z_pt_x_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_x);

    auto fp_x =
        mm->add_instruction(migraphx::make_op("dequantizelinear"), x, scale_x_bcast, z_pt_x_bcast);

    auto fp_y = mm->add_instruction(migraphx::make_op("sigmoid"), fp_x);

    auto scale_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), sc_y);

    auto z_pt_y_bcast =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {64}}}), z_pt_y);

    auto y =
        mm->add_instruction(migraphx::make_op("quantizelinear"), fp_y, scale_y_bcast, z_pt_y_bcast);

    mm->add_return({y});

    auto prog = migraphx::parse_onnx("qlinearsigmoid_test.onnx");

    EXPECT(p.sort() == prog.sort());
}

turneram's avatar
turneram committed
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
migraphx::instruction_ref insert_quantizelinear_clip(migraphx::module& m,
                                                     const migraphx::instruction_ref ins,
                                                     const migraphx::instruction_ref round,
                                                     const migraphx::shape s,
                                                     const int64_t min_quant,
                                                     const int64_t max_quant)
{
    migraphx::instruction_ref min_arg;
    migraphx::instruction_ref max_arg;
    if(migraphx::enabled(MIGRAPHX_ENABLE_CK_WORKAROUNDS{}))
    {
        std::vector<int> min_data(s.elements(), min_quant);
        std::vector<int> max_data(s.elements(), max_quant);
        min_arg = m.add_literal(migraphx::literal(s, min_data));
        max_arg = m.add_literal(migraphx::literal(s, max_data));
    }
    else
    {
        min_arg = m.add_literal(migraphx::literal{migraphx::shape{s.type()}, {min_quant}});
        max_arg = m.add_literal(migraphx::literal{migraphx::shape{s.type()}, {max_quant}});
    }

    return migraphx::insert_common_op(m, ins, migraphx::make_op("clip"), {round, min_arg, max_arg});
}

6116
6117
6118
TEST_CASE(quantizelinear_test)
{
    migraphx::program p;
turneram's avatar
turneram committed
6119
6120
6121
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1}});
6122
    auto l1_mbcast =
6123
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6124
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6125
6126
6127
    auto nearbyint = mm->add_instruction(migraphx::make_op("nearbyint"), div);
    auto s         = nearbyint->get_shape();
    auto clip      = insert_quantizelinear_clip(*mm, div, nearbyint, s, 0, 255);
turneram's avatar
turneram committed
6128
6129
6130
6131
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::uint8_type)}}),
        clip);
6132

turneram's avatar
turneram committed
6133
6134
6135
    auto prog = optimize_onnx("quantizelinear_test.onnx", true);
    EXPECT(p.sort() == prog.sort());
}
6136

turneram's avatar
turneram committed
6137
6138
6139
6140
6141
6142
6143
TEST_CASE(quantizelinear_int32_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::int32_type, {5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1}});
    auto l1_mbcast =
6144
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6145
6146
6147
6148
    l0 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l0);
turneram's avatar
turneram committed
6149
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6150
6151
6152
    auto nearbyint = mm->add_instruction(migraphx::make_op("nearbyint"), div);
    auto s         = nearbyint->get_shape();
    auto clip      = insert_quantizelinear_clip(*mm, div, nearbyint, s, 0, 255);
turneram's avatar
turneram committed
6153
    mm->add_instruction(
6154
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
                          {{"target_type", migraphx::to_value(migraphx::shape::uint8_type)}}),
        clip);

    auto prog = optimize_onnx("quantizelinear_int32_test.onnx", true);
    EXPECT(p.sort() == prog.sort());
}

TEST_CASE(quantizelinear_zero_point_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", {migraphx::shape::float_type, {5}});
    auto l1  = mm->add_parameter("1", {migraphx::shape::float_type, {1}});
    auto l2  = mm->add_parameter("2", {migraphx::shape::int8_type, {1}});
    auto l1_mbcast =
6170
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6171
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6172
    auto round = mm->add_instruction(migraphx::make_op("nearbyint"), div);
6173
    auto l2_mbcast =
6174
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l2);
turneram's avatar
turneram committed
6175
    l2_mbcast = mm->add_instruction(
6176
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6177
6178
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l2_mbcast);
turneram's avatar
turneram committed
6179
6180
6181
    auto add  = mm->add_instruction(migraphx::make_op("add"), round, l2_mbcast);
    auto s    = round->get_shape();
    auto clip = insert_quantizelinear_clip(*mm, div, add, s, -128, 127);
6182
6183
6184
6185
6186
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int8_type)}}),
        clip);

turneram's avatar
turneram committed
6187
    auto prog = optimize_onnx("quantizelinear_zero_point_test.onnx", true);
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
    EXPECT(p.sort() == prog.sort());
}

migraphx::program make_quantizelinear_axis_prog()
{
    migraphx::program p;
    std::vector<size_t> input_lens{1, 1, 5, 1};
    int axis = 2;
    auto* mm = p.get_main_module();

turneram's avatar
turneram committed
6198
6199
6200
    auto l0       = mm->add_parameter("0", {migraphx::shape::float_type, input_lens});
    auto l1       = mm->add_parameter("1", {migraphx::shape::float_type, {5}});
    auto l2       = mm->add_parameter("2", {migraphx::shape::int8_type, {5}});
6201
    auto l1_bcast = mm->add_instruction(
6202
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l1);
6203
6204

    auto div      = mm->add_instruction(migraphx::make_op("div"), l0, l1_bcast);
6205
    auto round    = mm->add_instruction(migraphx::make_op("nearbyint"), div);
6206
    auto l2_bcast = mm->add_instruction(
6207
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l2);
6208
6209
    l2_bcast = mm->add_instruction(
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6210
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
6211
        l2_bcast);
turneram's avatar
turneram committed
6212
6213
6214
    auto add  = mm->add_instruction(migraphx::make_op("add"), round, l2_bcast);
    auto s    = round->get_shape();
    auto clip = insert_quantizelinear_clip(*mm, div, add, s, -128, 127);
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int8_type)}}),
        clip);
    return p;
}

TEST_CASE(quantizelinear_axis_test)
{
    migraphx::program p = make_quantizelinear_axis_prog();

turneram's avatar
turneram committed
6226
    auto prog = optimize_onnx("quantizelinear_axis_test.onnx", true);
6227
6228
6229
6230
6231
6232
6233
    EXPECT(p.sort() == prog.sort());
}

TEST_CASE(quantizelinear_neg_axis_test)
{
    migraphx::program p = make_quantizelinear_axis_prog();

turneram's avatar
turneram committed
6234
    auto prog = optimize_onnx("quantizelinear_neg_axis_test.onnx", true);
6235
6236
6237
    EXPECT(p.sort() == prog.sort());
}

6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
TEST_CASE(randomnormal_test)
{
    float mean  = 10.0;
    float scale = 1.5;
    float seed  = 0.0;
    std::vector<int> shape_attr{2, 3, 4};

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

    migraphx::shape s{migraphx::shape::double_type, shape_attr};
    std::vector<double> rand_vals(s.elements());
    std::mt19937 gen(seed);
    std::normal_distribution<> d(mean, scale);
    std::generate(rand_vals.begin(), rand_vals.end(), [&]() { return d(gen); });

    mm->add_literal(migraphx::literal{s, rand_vals});

    auto prog = optimize_onnx("randomnormal_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(randomnormal_dtype_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomnormal_dtype_error_test.onnx"); }));
}

6266
6267
6268
6269
6270
6271
6272
6273
TEST_CASE(randomnormal_generated_seed_test)
{
    auto p1 = optimize_onnx("randomnormal_generated_seed_test.onnx");
    auto p2 = optimize_onnx("randomnormal_generated_seed_test.onnx");

    EXPECT(p1 != p2);
}

6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
TEST_CASE(randomnormal_shape_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomnormal_shape_error_test.onnx"); }));
}

TEST_CASE(randomnormallike_test)
{
    float mean  = 10.0;
    float scale = 1.5;
    float seed  = 0.0;
    std::vector<int> shape_attr{2, 3, 4};

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

    migraphx::shape s{migraphx::shape::half_type, shape_attr};
    std::vector<double> rand_vals(s.elements());
    std::mt19937 gen(seed);
    std::normal_distribution<> d(mean, scale);
    std::generate(rand_vals.begin(), rand_vals.end(), [&]() { return d(gen); });

    mm->add_parameter("input", s);
    mm->add_literal(migraphx::literal{s, rand_vals});

    auto prog = optimize_onnx("randomnormallike_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(randomnormallike_type_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomnormallike_type_error_test.onnx"); }));
}

TEST_CASE(randomuniform_test)
{
    float high = 1.0;
    float low  = 0.0;
    float seed = 0.0;
    std::vector<int> shape_attr{2, 3, 4};

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

    migraphx::shape s{migraphx::shape::double_type, shape_attr};
    std::vector<double> rand_vals(s.elements());
    std::mt19937 gen(seed);
    std::uniform_real_distribution<> d(low, high);
    std::generate(rand_vals.begin(), rand_vals.end(), [&]() { return d(gen); });

    mm->add_literal(migraphx::literal{s, rand_vals});

    auto prog = optimize_onnx("randomuniform_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(randomuniform_dtype_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomuniform_dtype_error_test.onnx"); }));
}

6336
6337
6338
6339
6340
6341
6342
6343
TEST_CASE(randomuniform_generated_seed_test)
{
    auto p1 = optimize_onnx("randomuniform_generated_seed_test.onnx");
    auto p2 = optimize_onnx("randomuniform_generated_seed_test.onnx");

    EXPECT(p1 != p2);
}

6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
TEST_CASE(randomuniform_shape_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomuniform_shape_error_test.onnx"); }));
}

TEST_CASE(randomuniformlike_test)
{
    float high = 10.0;
    float low  = 1.0;
    float seed = 0.0;
    std::vector<int> shape_attr{2, 3, 4};

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

    migraphx::shape s{migraphx::shape::half_type, shape_attr};
    std::vector<double> rand_vals(s.elements());
    std::mt19937 gen(seed);
    std::uniform_real_distribution<> d(low, high);
    std::generate(rand_vals.begin(), rand_vals.end(), [&]() { return d(gen); });

    mm->add_parameter("input", s);
    mm->add_literal(migraphx::literal{s, rand_vals});

    auto prog = optimize_onnx("randomuniformlike_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(randomuniformlike_type_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("randomuniformlike_type_error_test.onnx"); }));
}

kahmed10's avatar
kahmed10 committed
6378
6379
6380
TEST_CASE(range_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6381
6382
6383
6384
6385
    auto* mm = p.get_main_module();
    mm->add_literal(int64_t{10});
    mm->add_literal(int64_t{6});
    mm->add_literal(int64_t{-3});
    mm->add_literal(migraphx::literal{{migraphx::shape::int64_type, {2}}, {10, 7}});
kahmed10's avatar
kahmed10 committed
6386
6387
6388
6389
6390
6391
6392
6393
6394

    auto prog = optimize_onnx("range_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(range_float_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6395
6396
6397
6398
6399
    auto* mm = p.get_main_module();
    mm->add_literal(float{2});
    mm->add_literal(float{11});
    mm->add_literal(float{2});
    mm->add_literal(migraphx::literal{{migraphx::shape::float_type, {5}}, {2, 4, 6, 8, 10}});
kahmed10's avatar
kahmed10 committed
6400
6401
6402
6403
6404
6405

    auto prog = optimize_onnx("range_float_test.onnx");

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
6406
6407
6408
TEST_CASE(recip_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6409
6410
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3}});
6411
    mm->add_instruction(migraphx::make_op("recip"), input);
kahmed10's avatar
kahmed10 committed
6412
6413
6414
6415
6416
6417

    auto prog = optimize_onnx("recip_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6418
6419
6420
TEST_CASE(reducel1_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6421
6422
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6423
6424
6425
    auto abs_l0 = mm->add_instruction(migraphx::make_op("abs"), l0);
    auto sum_l0 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-2}}}), abs_l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {-2}}}), sum_l0);
Shucai Xiao's avatar
Shucai Xiao committed
6426
6427
6428
6429
6430
    auto prog = optimize_onnx("reducel1_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
6431
6432
6433
6434
6435
6436
6437
6438
TEST_CASE(reducel1_dyn_test)
{
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        // a shape with 4 dynamic dimensions
        auto l0      = mm->add_parameter("x",
                                    migraphx::shape{migraphx::shape::float_type,
6439
                                                         {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}}});
Brian Pickrell's avatar
Brian Pickrell committed
6440
6441
6442
6443
6444
6445
6446
        auto abs_ins = mm->add_instruction(migraphx::make_op("abs"), l0);
        auto sum_ins =
            mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-2}}}), abs_ins);
        auto sq_ins = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {-2}}}), sum_ins);
        mm->add_return({sq_ins});

        migraphx::onnx_options options;
6447
        options.map_dyn_input_dims["x"] = {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}};
Brian Pickrell's avatar
Brian Pickrell committed
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
        auto prog                       = migraphx::parse_onnx("reducel1_dyn_test.onnx", options);

        EXPECT(p == prog);
    }

    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        // No axes given in the onnx file.  Parser should default to all axes.
        auto l0      = mm->add_parameter("x",
                                    migraphx::shape{migraphx::shape::float_type,
6459
                                                         {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}}});
Brian Pickrell's avatar
Brian Pickrell committed
6460
6461
6462
6463
6464
6465
6466
6467
        auto abs_ins = mm->add_instruction(migraphx::make_op("abs"), l0);
        auto sum_ins =
            mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2, 3}}}), abs_ins);
        auto sq_ins =
            mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0, 1, 2, 3}}}), sum_ins);
        mm->add_return({sq_ins});

        migraphx::onnx_options options;
6468
        options.map_dyn_input_dims["x"] = {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}};
Brian Pickrell's avatar
Brian Pickrell committed
6469
6470
6471
6472
6473
6474
        auto prog = migraphx::parse_onnx("reducel1_dyn_noaxes_test.onnx", options);

        EXPECT(p == prog);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
6475
6476
6477
TEST_CASE(reducel2_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6478
6479
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6480
6481
6482
6483
    auto square_l0 = mm->add_instruction(migraphx::make_op("mul"), l0, l0);
    auto sum_l0 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-1}}}), square_l0);
    auto squ_l0 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {-1}}}), sum_l0);
    mm->add_instruction(migraphx::make_op("sqrt"), squ_l0);
Shucai Xiao's avatar
Shucai Xiao committed
6484
6485
6486
6487
6488
6489
6490
6491
    auto prog = optimize_onnx("reducel2_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(reduce_log_sum_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6492
6493
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6494
6495
    auto sum_l0 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-3}}}), l0);
    mm->add_instruction(migraphx::make_op("log"), sum_l0);
Shucai Xiao's avatar
Shucai Xiao committed
6496
6497
6498
6499
6500
6501
6502
6503
    auto prog = optimize_onnx("reduce_log_sum_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(reduce_log_sum_exp_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6504
6505
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6506
6507
6508
    auto exp_l0 = mm->add_instruction(migraphx::make_op("exp"), l0);
    auto sum_l0 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-4}}}), exp_l0);
    mm->add_instruction(migraphx::make_op("log"), sum_l0);
Shucai Xiao's avatar
Shucai Xiao committed
6509
6510
6511
6512
6513
    auto prog = optimize_onnx("reduce_log_sum_exp_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6514
6515
6516
TEST_CASE(reducemax_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6517
6518
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6519
    mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6520
    auto prog = optimize_onnx("reducemax_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6521
6522
6523
6524

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
6525
6526
6527
6528
6529
6530
TEST_CASE(reducemax_dyn_test)
{
    // input shape with 4 dynamic dimensions
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
6531
        "x", migraphx::shape{migraphx::shape::float_type, {{3, 5}, {4, 4}, {5, 5}, {6, 6}}});
Brian Pickrell's avatar
Brian Pickrell committed
6532
6533
6534
6535
6536
    auto r0 = mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {2}}}), l0);
    auto r1 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), r0);
    mm->add_return({r1});

    migraphx::onnx_options options;
6537
    options.map_dyn_input_dims["x"] = {{3, 5}, {4, 4}, {5, 5}, {6, 6}};
Brian Pickrell's avatar
Brian Pickrell committed
6538
6539
6540
6541
6542
    auto prog                       = migraphx::parse_onnx("reducemax_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6543
TEST_CASE(reducemean_test)
6544
6545
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6546
6547
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6548
6549
    auto l1  = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
6550
    auto prog = optimize_onnx("reducemean_test.onnx");
6551
6552
6553
6554

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6555
TEST_CASE(reducemean_keepdims_test)
6556
6557
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6558
6559
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6560
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6561
    auto prog = optimize_onnx("reducemean_keepdims_test.onnx");
6562
6563
6564
6565

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6566
6567
6568
TEST_CASE(reducemin_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6569
6570
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6571
6572
    auto l1  = mm->add_instruction(migraphx::make_op("reduce_min", {{"axes", {2, 3}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
6573
    auto prog = optimize_onnx("reducemin_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6574
6575
6576
6577

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6578
6579
6580
TEST_CASE(reduceprod_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6581
6582
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6583
    mm->add_instruction(migraphx::make_op("reduce_prod", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6584
6585
6586
6587
6588
    auto prog = optimize_onnx("reduceprod_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6589
TEST_CASE(reducesum_test)
Khalique's avatar
Khalique committed
6590
6591
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6592
6593
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6594
6595
    auto l1  = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {2}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
6596
    auto prog = optimize_onnx("reducesum_test.onnx");
6597
6598

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6599
6600
}

Shucai Xiao's avatar
Shucai Xiao committed
6601
6602
6603
6604
TEST_CASE(reducesum_empty_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6605
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
    auto x  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    auto l1 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1, 2, 3}}}), x);
    auto r  = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0, 1, 2, 3}}}), l1);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("reducesum_empty_axes_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(reducesum_noop_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6620
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
6621
6622
6623
6624
6625
6626
6627
    auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    mm->add_return({x});
    auto prog = migraphx::parse_onnx("reducesum_noop_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6628
TEST_CASE(reducesum_multiaxis_test)
Khalique's avatar
Khalique committed
6629
6630
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6631
6632
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6633
6634
    auto l1  = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {2, 3}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
6635
    auto prog = optimize_onnx("reducesum_multiaxis_test.onnx");
6636
6637

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6638
6639
}

Khalique's avatar
Khalique committed
6640
TEST_CASE(reducesum_keepdims_test)
Khalique's avatar
Khalique committed
6641
6642
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6643
6644
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6645
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {2, 3}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6646
    auto prog = optimize_onnx("reducesum_keepdims_test.onnx");
6647
6648
6649
6650

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6651
6652
6653
TEST_CASE(reducesum_square_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6654
6655
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6656
6657
6658
    auto squ_l0 = mm->add_instruction(migraphx::make_op("mul"), l0, l0);
    auto sum_l0 = mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {-2}}}), squ_l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {-2}}}), sum_l0);
Shucai Xiao's avatar
Shucai Xiao committed
6659
6660
6661
6662
6663
    auto prog = optimize_onnx("reducesum_square_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6664
TEST_CASE(reshape_test)
6665
{
Khalique's avatar
Khalique committed
6666
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6667
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
6668
6669
    migraphx::op::reshape op;
    std::vector<int64_t> reshape_dims{3, 8};
Shucai Xiao's avatar
Shucai Xiao committed
6670
    mm->add_literal(
Khalique's avatar
Khalique committed
6671
        migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {2}}, reshape_dims});
Shucai Xiao's avatar
Shucai Xiao committed
6672
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 2, 3}});
Khalique's avatar
Khalique committed
6673
    op.dims = reshape_dims;
6674
6675
    mm->add_instruction(op, l0);
    mm->add_instruction(op, l0);
Shucai Xiao's avatar
Shucai Xiao committed
6676
    auto prog = optimize_onnx("reshape_test.onnx");
6677
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6678
6679
}

Khalique's avatar
Khalique committed
6680
TEST_CASE(reshape_non_standard_test)
6681
6682
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6683
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
6684
6685
    migraphx::op::reshape op;
    migraphx::shape s{migraphx::shape::float_type, {2, 3, 4}};
6686
6687
6688
    auto x = mm->add_parameter("x", s);
    auto tran_x =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), x);
6689
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4, 3, 2}}}), tran_x);
Shucai Xiao's avatar
Shucai Xiao committed
6690
    auto prog = optimize_onnx("reshape_non_standard_test.onnx");
6691
6692
6693
6694

    EXPECT(p == prog);
}

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
TEST_CASE(reshape_variable_input_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto p0    = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 2, 3}});
    auto p1    = mm->add_parameter("1", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto alloc = mm->add_instruction(
        migraphx::make_op("allocate", {{"buf_type", migraphx::shape::float_type}}), p1);
    mm->add_instruction(migraphx::make_op("reshape"), p0, alloc);

    auto prog = optimize_onnx("reshape_variable_input_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(reshape_variable_input_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto p0  = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {3, 3}}});
    auto p1    = mm->add_parameter("1", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto alloc = mm->add_instruction(
        migraphx::make_op("allocate", {{"buf_type", migraphx::shape::float_type}}), p1);
    auto reshape = mm->add_instruction(migraphx::make_op("reshape"), p0, alloc);
    mm->add_return({reshape});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {1, 4};
    auto prog                     = parse_onnx("reshape_variable_input_dyn_test.onnx", options);
    EXPECT(p == prog);
}

6727
TEST_CASE(resize_downsample_c_test)
Shucai Xiao's avatar
Shucai Xiao committed
6728
6729
{
    migraphx::program p;
6730
6731
    auto* mm = p.get_main_module();

Shucai Xiao's avatar
Shucai Xiao committed
6732
6733
    std::vector<float> ds = {1.0f, 1.0f, 0.6f, 0.6f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
6734
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
6735
6736

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
6737
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6738

6739
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6740
6741

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 1, 2}};
6742
    std::vector<int> ind = {0, 2};
6743
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6744

6745
6746
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), inx);
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
6747
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6748

6749
    auto prog = migraphx::parse_onnx("resize_downsample_c_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6750
6751
6752
6753

    EXPECT(p == prog);
}

6754
TEST_CASE(resize_downsample_f_test)
Shucai Xiao's avatar
Shucai Xiao committed
6755
6756
{
    migraphx::program p;
6757
    auto* mm              = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6758
6759
    std::vector<float> ds = {1.0f, 1.0f, 0.6f, 0.6f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
6760
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
6761
6762

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
6763
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6764

6765
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6766
6767

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 1, 2}};
6768
    std::vector<int> ind = {0, 3};
6769
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6770

6771
6772
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), inx);
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
6773
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6774

6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
    auto prog = migraphx::parse_onnx("resize_downsample_f_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(resize_downsample_linear_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape ss{migraphx::shape::float_type, {4}};
    std::vector<float> ds = {1, 1, 0.6, 0.5};
    mm->add_literal(migraphx::literal(ss, ds));

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
    auto x = mm->add_parameter("X", sx);
    migraphx::shape s_ind{migraphx::shape::int32_type, {16, 1, 1, 2}};
    std::vector<int> d_ind = {0, 2, 0, 2, 0, 2, 0, 2, 4, 6, 4, 6, 4, 6, 4, 6,
                              1, 3, 1, 3, 1, 3, 1, 3, 5, 7, 5, 7, 5, 7, 5, 7};
    auto l_ind             = mm->add_literal(migraphx::literal(s_ind, d_ind));

    migraphx::shape s8{migraphx::shape::float_type, {8, 1, 1, 2}};
    std::vector<float> d8(16, 0.5f);
    auto l8 = mm->add_literal(migraphx::literal(s8, d8));

    migraphx::shape s4{migraphx::shape::float_type, {4, 1, 1, 2}};
    std::vector<float> d4(8, 1.0f / 3.0f);
    auto l4 = mm->add_literal(migraphx::literal(s4, d4));

    migraphx::shape s2{migraphx::shape::float_type, {2, 1, 1, 2}};
    std::vector<float> d2(4, 0);
    auto l2 = mm->add_literal(migraphx::literal(s2, d2));
Shucai Xiao's avatar
Shucai Xiao committed
6806

6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
    migraphx::shape s1{migraphx::shape::float_type, {1, 1, 1, 2}};
    std::vector<float> d1(2, 0.0f);
    auto l1 = mm->add_literal(migraphx::literal(s1, d1));

    mm->add_instruction(migraphx::make_op("undefined"));
    auto rsp   = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), x);
    auto data  = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp, l_ind);
    auto slc80 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {8}}}), data);
    auto slc81 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {8}}, {"ends", {16}}}), data);
    auto diff8 = mm->add_instruction(migraphx::make_op("sub"), slc81, slc80);
    auto mul8  = mm->add_instruction(migraphx::make_op("mul"), diff8, l8);
    auto add8  = mm->add_instruction(migraphx::make_op("add"), mul8, slc80);
    auto slc40 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {4}}}), add8);
    auto slc41 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {4}}, {"ends", {8}}}), add8);
    auto diff4 = mm->add_instruction(migraphx::make_op("sub"), slc41, slc40);
    auto mul4  = mm->add_instruction(migraphx::make_op("mul"), diff4, l4);
    auto add4  = mm->add_instruction(migraphx::make_op("add"), mul4, slc40);
    auto slc20 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), add4);
    auto slc21 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), add4);
    auto diff2 = mm->add_instruction(migraphx::make_op("sub"), slc21, slc20);
    auto mul2  = mm->add_instruction(migraphx::make_op("mul"), diff2, l2);
    auto add2  = mm->add_instruction(migraphx::make_op("add"), mul2, slc20);
    auto slc10 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), add2);
    auto slc11 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), add2);
    auto diff1 = mm->add_instruction(migraphx::make_op("sub"), slc11, slc10);
    auto mul1  = mm->add_instruction(migraphx::make_op("mul"), diff1, l1);
    auto add1  = mm->add_instruction(migraphx::make_op("add"), mul1, slc10);
    mm->add_return({add1});

    auto prog = migraphx::parse_onnx("resize_downsample_linear_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6845
6846
6847
6848
6849
6850
    EXPECT(p == prog);
}

TEST_CASE(resize_outsize_test)
{
    migraphx::program p;
6851
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6852
6853
6854

    std::vector<int64_t> out_len = {1, 1, 4, 6};
    migraphx::shape so{migraphx::shape::int64_type, {4}};
6855
    mm->add_literal(migraphx::literal(so, out_len));
Shucai Xiao's avatar
Shucai Xiao committed
6856
6857

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
6858
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6859

6860
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6861
6862
6863

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 4, 6}};
    std::vector<int> ind = {0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3};
6864
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6865

6866
6867
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), inx);
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
6868
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6869
6870
6871
6872
6873
6874

    auto prog = migraphx::parse_onnx("resize_outsize_test.onnx");

    EXPECT(p == prog);
}

6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
TEST_CASE(resize_nonstd_input_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    std::vector<float> ds = {1.0f, 1.0f, 0.6f, 0.6f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
    mm->add_literal(migraphx::literal{ss, ds});

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 4, 2}};
    auto inx = mm->add_parameter("X", sx);

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 1, 2}};
    std::vector<int> ind = {0, 4};
    auto li              = mm->add_literal(migraphx::literal(si, ind));

6891
6892
    auto tx =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), inx);
6893
6894
    mm->add_instruction(migraphx::make_op("undefined"));

6895
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), tx);
6896
6897
6898
6899
6900
6901
6902
6903
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("resize_nonstd_input_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6904
static auto create_upsample_linear_prog()
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape ss{migraphx::shape::float_type, {4}};
    std::vector<float> ds = {1, 1, 2, 2};
    mm->add_literal(migraphx::literal(ss, ds));

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
    auto x = mm->add_parameter("X", sx);
    migraphx::shape s_ind{migraphx::shape::int32_type, {16, 1, 4, 4}};
    std::vector<int> d_ind = {
        0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2,
        2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
        0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2,
        3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1,
        2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0,
        1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3,
        3, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3,
        3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3,
        2, 3, 3, 3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3};
    auto l_ind = mm->add_literal(migraphx::literal(s_ind, d_ind));

    migraphx::shape s8{migraphx::shape::float_type, {8, 1, 4, 4}};
    std::vector<float> d8 = {
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0};
    auto l8 = mm->add_literal(migraphx::literal(s8, d8));

    migraphx::shape s4{migraphx::shape::float_type, {4, 1, 4, 4}};
    std::vector<float> d4 = {
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0};
    auto l4 = mm->add_literal(migraphx::literal(s4, d4));

    migraphx::shape s2{migraphx::shape::float_type, {2, 1, 4, 4}};
    std::vector<float> d2(32, 0);
    auto l2 = mm->add_literal(migraphx::literal(s2, d2));

    migraphx::shape s1{migraphx::shape::float_type, {1, 1, 4, 4}};
    std::vector<float> d1(16, 0.0f);
    auto l1 = mm->add_literal(migraphx::literal(s1, d1));

    mm->add_instruction(migraphx::make_op("undefined"));
    auto rsp   = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), x);
    auto data  = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp, l_ind);
    auto slc80 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {8}}}), data);
    auto slc81 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {8}}, {"ends", {16}}}), data);
    auto diff8 = mm->add_instruction(migraphx::make_op("sub"), slc81, slc80);
    auto mul8  = mm->add_instruction(migraphx::make_op("mul"), diff8, l8);
    auto add8  = mm->add_instruction(migraphx::make_op("add"), mul8, slc80);
    auto slc40 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {4}}}), add8);
    auto slc41 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {4}}, {"ends", {8}}}), add8);
    auto diff4 = mm->add_instruction(migraphx::make_op("sub"), slc41, slc40);
    auto mul4  = mm->add_instruction(migraphx::make_op("mul"), diff4, l4);
    auto add4  = mm->add_instruction(migraphx::make_op("add"), mul4, slc40);
    auto slc20 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), add4);
    auto slc21 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), add4);
    auto diff2 = mm->add_instruction(migraphx::make_op("sub"), slc21, slc20);
    auto mul2  = mm->add_instruction(migraphx::make_op("mul"), diff2, l2);
    auto add2  = mm->add_instruction(migraphx::make_op("add"), mul2, slc20);
    auto slc10 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), add2);
    auto slc11 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), add2);
    auto diff1 = mm->add_instruction(migraphx::make_op("sub"), slc11, slc10);
    auto mul1  = mm->add_instruction(migraphx::make_op("mul"), diff1, l1);
    auto add1  = mm->add_instruction(migraphx::make_op("add"), mul1, slc10);
    mm->add_return({add1});

Shucai Xiao's avatar
Shucai Xiao committed
6995
6996
6997
6998
6999
7000
    return p;
}

TEST_CASE(resize_upsample_linear_ac_test)
{
    auto p    = create_upsample_linear_prog();
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
    auto prog = migraphx::parse_onnx("resize_upsample_linear_ac_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(resize_upsample_linear_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape ss{migraphx::shape::float_type, {4}};
    std::vector<float> ds = {1, 1, 2, 2};
    mm->add_literal(migraphx::literal(ss, ds));

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
    auto x = mm->add_parameter("X", sx);
    migraphx::shape s_ind{migraphx::shape::int32_type, {16, 1, 4, 4}};
    std::vector<int> d_ind = {
        0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2,
        2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
        0, 1, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2,
        3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 0, 0, 1,
        2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0,
        1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3,
        3, 3, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3,
        3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3,
        2, 3, 3, 3, 2, 3, 3, 3, 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3};
    auto l_ind = mm->add_literal(migraphx::literal(s_ind, d_ind));

    migraphx::shape s8{migraphx::shape::float_type, {8, 1, 4, 4}};
    std::vector<float> d8 = {
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0,
        0, 1.0f / 3, 2.0f / 3, 0, 0, 1.0f / 3, 2.0f / 3, 0};
    auto l8 = mm->add_literal(migraphx::literal(s8, d8));

    migraphx::shape s4{migraphx::shape::float_type, {4, 1, 4, 4}};
    std::vector<float> d4 = {
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0,
        0,        0,        0,        0,        1.0f / 3, 1.0f / 3, 1.0f / 3, 1.0f / 3,
        2.0f / 3, 2.0f / 3, 2.0f / 3, 2.0f / 3, 0,        0,        0,        0};
    auto l4 = mm->add_literal(migraphx::literal(s4, d4));

    migraphx::shape s2{migraphx::shape::float_type, {2, 1, 4, 4}};
    std::vector<float> d2(32, 0);
    auto l2 = mm->add_literal(migraphx::literal(s2, d2));

    migraphx::shape s1{migraphx::shape::float_type, {1, 1, 4, 4}};
    std::vector<float> d1(16, 0.0f);
    auto l1 = mm->add_literal(migraphx::literal(s1, d1));

    mm->add_instruction(migraphx::make_op("undefined"));
    auto rsp   = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), x);
    auto data  = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp, l_ind);
    auto slc80 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {8}}}), data);
    auto slc81 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {8}}, {"ends", {16}}}), data);
    auto diff8 = mm->add_instruction(migraphx::make_op("sub"), slc81, slc80);
    auto mul8  = mm->add_instruction(migraphx::make_op("mul"), diff8, l8);
    auto add8  = mm->add_instruction(migraphx::make_op("add"), mul8, slc80);
    auto slc40 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {4}}}), add8);
    auto slc41 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {4}}, {"ends", {8}}}), add8);
    auto diff4 = mm->add_instruction(migraphx::make_op("sub"), slc41, slc40);
    auto mul4  = mm->add_instruction(migraphx::make_op("mul"), diff4, l4);
    auto add4  = mm->add_instruction(migraphx::make_op("add"), mul4, slc40);
    auto slc20 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), add4);
    auto slc21 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), add4);
    auto diff2 = mm->add_instruction(migraphx::make_op("sub"), slc21, slc20);
    auto mul2  = mm->add_instruction(migraphx::make_op("mul"), diff2, l2);
    auto add2  = mm->add_instruction(migraphx::make_op("add"), mul2, slc20);
    auto slc10 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), add2);
    auto slc11 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), add2);
    auto diff1 = mm->add_instruction(migraphx::make_op("sub"), slc11, slc10);
    auto mul1  = mm->add_instruction(migraphx::make_op("mul"), diff1, l1);
    auto add1  = mm->add_instruction(migraphx::make_op("add"), mul1, slc10);
    mm->add_return({add1});

    auto prog = migraphx::parse_onnx("resize_upsample_linear_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
7100
7101
7102
TEST_CASE(resize_upsample_pc_test)
{
    migraphx::program p;
7103
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7104
7105
7106

    std::vector<float> ds = {1.0f, 1.0f, 2.0f, 1.5f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
7107
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
7108
7109

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
7110
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
7111

7112
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
7113
7114
7115

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 4, 6}};
    std::vector<int> ind = {0, 1, 1, 2, 3, 3, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 4, 5, 5, 6, 7, 7};
7116
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
7117

7118
7119
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), inx);
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
7120
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7121
7122
7123
7124
7125
7126
7127
7128
7129

    auto prog = migraphx::parse_onnx("resize_upsample_pc_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(resize_upsample_pf_test)
{
    migraphx::program p;
7130
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7131
7132
7133

    std::vector<float> ds = {1.0f, 1.0f, 2.0f, 3.0f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
7134
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
7135
7136

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
7137
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
7138

7139
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
7140
7141
7142

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 4, 6}};
    std::vector<int> ind = {0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 2, 2, 2, 3, 3, 3};
7143
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
7144

7145
7146
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), inx);
    auto r    = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), lrsp, li);
7147
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7148
7149
7150
7151
7152
7153

    auto prog = migraphx::parse_onnx("resize_upsample_pf_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
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
7205
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
7236
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
7268
7269
7270
7271
7272
7273
TEST_CASE(reversesequence_batch_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    int batch_axis = 0;
    int time_axis  = 1;

    migraphx::shape sx{migraphx::shape::float_type, {4, 4}};
    auto input = mm->add_parameter("x", sx);

    std::vector<int64_t> sequence_lens = {1, 2, 3, 4};
    mm->add_literal({{migraphx::shape::int64_type, {4}}, sequence_lens});

    int batch_size = sx.lens()[batch_axis];
    int time_size  = sx.lens()[time_axis];

    auto add_slice =
        [&mm, &input, batch_axis, time_axis](int b_start, int b_end, int t_start, int t_end) {
            return mm->add_instruction(migraphx::make_op("slice",
                                                         {{"axes", {batch_axis, time_axis}},
                                                          {"starts", {b_start, t_start}},
                                                          {"ends", {b_end, t_end}}}),
                                       input);
        };
    auto ret = add_slice(0, 1, 0, time_size);
    for(int b = 1; b < batch_size; ++b)
    {
        auto s0 = add_slice(b, b + 1, 0, sequence_lens[b]);
        s0      = mm->add_instruction(migraphx::make_op("reverse", {{"axes", {time_axis}}}), s0);
        if(sequence_lens[b] < time_size)
        {
            auto s1 = add_slice(b, b + 1, sequence_lens[b], time_size);
            s0 = mm->add_instruction(migraphx::make_op("concat", {{"axis", time_axis}}), s0, s1);
        }
        ret = mm->add_instruction(migraphx::make_op("concat", {{"axis", batch_axis}}), ret, s0);
    }
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("reversesequence_batch_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(reversesequence_batch_axis_err_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("reversesequence_batch_axis_err_test.onnx"); }));
}

TEST_CASE(reversesequence_rank_err_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("reversesequence_rank_err_test.onnx"); }));
}

TEST_CASE(reversesequence_sequence_lens_shape_err_test)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("reversesequence_sequence_lens_shape_err_test.onnx"); }));
}

TEST_CASE(reversesequence_same_axis_err_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("reversesequence_same_axis_err_test.onnx"); }));
}

TEST_CASE(reversesequence_time_axis_err_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("reversesequence_time_axis_err_test.onnx"); }));
}

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

    int batch_axis = 1;
    int time_axis  = 0;

    migraphx::shape sx{migraphx::shape::float_type, {4, 4}};
    auto input = mm->add_parameter("x", sx);

    int batch_size                     = sx.lens()[batch_axis];
    int time_size                      = sx.lens()[time_axis];
    std::vector<int64_t> sequence_lens = {4, 3, 2, 1};

    auto add_slice =
        [&mm, &input, batch_axis, time_axis](int b_start, int b_end, int t_start, int t_end) {
            return mm->add_instruction(migraphx::make_op("slice",
                                                         {{"axes", {batch_axis, time_axis}},
                                                          {"starts", {b_start, t_start}},
                                                          {"ends", {b_end, t_end}}}),
                                       input);
        };

    migraphx::instruction_ref ret;
    for(int b = 0; b < batch_size - 1; ++b)
    {
        auto s0 = add_slice(b, b + 1, 0, sequence_lens[b]);
        s0      = mm->add_instruction(migraphx::make_op("reverse", {{"axes", {time_axis}}}), s0);
        if(sequence_lens[b] < time_size)
        {
            auto s1 = add_slice(b, b + 1, sequence_lens[b], time_size);
            s0 = mm->add_instruction(migraphx::make_op("concat", {{"axis", time_axis}}), s0, s1);
        }
        if(b == 0)
        {
            ret = s0;
        }
        else
        {
            ret = mm->add_instruction(migraphx::make_op("concat", {{"axis", batch_axis}}), ret, s0);
        }
    }
    auto s0 = add_slice(batch_size - 1, batch_size, 0, time_size);
    ret     = mm->add_instruction(migraphx::make_op("concat", {{"axis", batch_axis}}), ret, s0);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("reversesequence_time_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
TEST_CASE(roialign_default_test)
{
    migraphx::shape sx{migraphx::shape::float_type, {10, 4, 7, 8}};
    migraphx::shape srois{migraphx::shape::float_type, {8, 4}};
    migraphx::shape sbi{migraphx::shape::int64_type, {8}};

    migraphx::program p;
    auto* mm  = p.get_main_module();
    auto x    = mm->add_parameter("x", sx);
    auto rois = mm->add_parameter("rois", srois);
    auto bi   = mm->add_parameter("batch_ind", sbi);

7286
7287
7288
7289
7290
7291
7292
    // Due to the onnx model using opset 12, the coordinate_transformation_mode should be set to
    // output_half_pixel
    auto r = mm->add_instruction(
        migraphx::make_op("roialign", {{"coordinate_transformation_mode", "output_half_pixel"}}),
        x,
        rois,
        bi);
Shucai Xiao's avatar
Shucai Xiao committed
7293
7294
7295
7296
7297
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
7326
7327
7328
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("roialign_default_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(roialign_test)
{
    migraphx::shape sx{migraphx::shape::float_type, {10, 5, 4, 7}};
    migraphx::shape srois{migraphx::shape::float_type, {8, 4}};
    migraphx::shape sbi{migraphx::shape::int64_type, {8}};

    migraphx::program p;
    auto* mm  = p.get_main_module();
    auto x    = mm->add_parameter("x", sx);
    auto rois = mm->add_parameter("rois", srois);
    auto bi   = mm->add_parameter("batch_ind", sbi);

    auto r = mm->add_instruction(
        migraphx::make_op("roialign",
                          {{"coordinate_transformation_mode", "output_half_pixel"},
                           {"spatial_scale", 2.0f},
                           {"output_height", 5},
                           {"output_width", 5},
                           {"sampling_ratio", 3}}),
        x,
        rois,
        bi);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("roialign_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7329
7330
7331
TEST_CASE(round_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7332
7333
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {10, 5}});
7334
    mm->add_instruction(migraphx::make_op("nearbyint"), input);
Khalique's avatar
Khalique committed
7335

Shucai Xiao's avatar
Shucai Xiao committed
7336
    auto prog = optimize_onnx("round_test.onnx");
Khalique's avatar
Khalique committed
7337
7338
7339
    EXPECT(p == prog);
}

7340
7341
// the ScatterElements op has 3 reduction modes, which map to separate reference ops
migraphx::program create_scatter_program(const std::string& scatter_mode, int axis)
Shucai Xiao's avatar
Shucai Xiao committed
7342
7343
7344
7345
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
Shucai Xiao's avatar
Shucai Xiao committed
7346
7347
7348
7349
    auto l1 =
        mm->add_parameter("indices", migraphx::shape{migraphx::shape::int32_type, {2, 3, 4, 5}});
    auto l2 =
        mm->add_parameter("update", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
7350
    auto r = mm->add_instruction(migraphx::make_op(scatter_mode, {{"axis", axis}}), l0, l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
7351
    mm->add_return({r});
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
    return p;
}

TEST_CASE(scatter_add_test)
{
    migraphx::program p = create_scatter_program("scatter_add", -2);
    auto prog           = migraphx::parse_onnx("scatter_add_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(scatter_mul_test)
{
    migraphx::program p = create_scatter_program("scatter_mul", -2);
    auto prog           = migraphx::parse_onnx("scatter_mul_test.onnx");

    EXPECT(p == prog);
}
TEST_CASE(scatter_none_test)
{
    migraphx::program p = create_scatter_program("scatter_none", -2);
    auto prog           = migraphx::parse_onnx("scatter_none_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
7374
7375
7376
7377

    EXPECT(p == prog);
}

7378
void scatternd_test_base(const std::string& reduction, const std::string& onnx_file)
turneram's avatar
turneram committed
7379
{
7380
7381
7382
7383
7384
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {2, 2, 2}});
    auto l1 = mm->add_parameter("indices", migraphx::shape{migraphx::shape::int64_type, {2, 1, 2}});
    auto l2 = mm->add_parameter("updates", migraphx::shape{migraphx::shape::float_type, {2, 1, 2}});
7385
    auto r   = mm->add_instruction(migraphx::make_op("scatternd_" + reduction), l0, l1, l2);
7386
    mm->add_return({r});
7387
    auto prog = migraphx::parse_onnx(onnx_file);
turneram's avatar
turneram committed
7388

7389
7390
    EXPECT(p == prog);
}
turneram's avatar
turneram committed
7391

7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
TEST_CASE(scatternd_test) { scatternd_test_base("none", "scatternd_test.onnx"); }

TEST_CASE(scatternd_add_test) { scatternd_test_base("add", "scatternd_add_test.onnx"); }

TEST_CASE(scatternd_mul_test) { scatternd_test_base("mul", "scatternd_mul_test.onnx"); }

TEST_CASE(scatternd_max_test) { scatternd_test_base("max", "scatternd_max_test.onnx"); }

TEST_CASE(scatternd_min_test) { scatternd_test_base("min", "scatternd_min_test.onnx"); }

TEST_CASE(scatternd_invalid_reduction_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("scatternd_invalid_reduction_test.onnx"); }));
}

7407
7408
7409
7410
7411
7412
7413
TEST_CASE(scatternd_dyn_test)
{
    // dynamic input.
    migraphx::program p;
    auto* mm = p.get_main_module();
    // parameters with dynamic dimensions
    auto l0 = mm->add_parameter(
7414
        "data", migraphx::shape{migraphx::shape::float_type, {{1, 3, {2}}, {2, 2}, {2, 2}}});
7415
    auto l1 = mm->add_parameter(
7416
        "indices", migraphx::shape{migraphx::shape::int64_type, {{2, 1, {2}}, {1, 1}, {2, 2}}});
7417
    auto l2 = mm->add_parameter(
7418
        "updates", migraphx::shape{migraphx::shape::float_type, {{2, 1, {2}}, {1, 1}, {2, 2}}});
7419
7420
7421
    auto r = mm->add_instruction(migraphx::make_op("scatternd_none"), l0, l1, l2);
    mm->add_return({r});
    migraphx::onnx_options options;
7422
7423
7424
    options.map_dyn_input_dims["data"]    = {{1, 3, {2}}, {2, 2}, {2, 2}};
    options.map_dyn_input_dims["indices"] = {{2, 1, {2}}, {1, 1}, {2, 2}};
    options.map_dyn_input_dims["updates"] = {{2, 1, {2}}, {1, 1}, {2, 2}};
7425
    auto prog = migraphx::parse_onnx("scatternd_dyn_test.onnx", options);
turneram's avatar
turneram committed
7426

7427
7428
    EXPECT(p == prog);
}
turneram's avatar
turneram committed
7429

Shucai Xiao's avatar
Shucai Xiao committed
7430
7431
7432
TEST_CASE(selu_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7433
    auto* mm                      = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7434
7435
    std::vector<std::size_t> lens = {2, 3};
    migraphx::shape s{migraphx::shape::double_type, lens};
Shucai Xiao's avatar
Shucai Xiao committed
7436
    auto x = mm->add_parameter("x", s);
Shucai Xiao's avatar
Shucai Xiao committed
7437
7438

    migraphx::shape ls{migraphx::shape::double_type, {1}};
7439
7440
7441
7442
    auto la   = mm->add_literal({ls, {0.3}});
    auto lg   = mm->add_literal({ls, {0.25}});
    auto mbla = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), la);
    auto mblg = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), lg);
Shucai Xiao's avatar
Shucai Xiao committed
7443

7444
7445
    auto sign_x = mm->add_instruction(migraphx::make_op("sign"), x);
    auto exp_x  = mm->add_instruction(migraphx::make_op("exp"), x);
Shucai Xiao's avatar
Shucai Xiao committed
7446

7447
7448
    auto mlax  = mm->add_instruction(migraphx::make_op("mul"), mbla, exp_x);
    auto smlax = mm->add_instruction(migraphx::make_op("sub"), mlax, mbla);
Shucai Xiao's avatar
Shucai Xiao committed
7449

7450
7451
    auto item1 = mm->add_instruction(migraphx::make_op("add"), smlax, x);
    auto item2 = mm->add_instruction(migraphx::make_op("sub"), smlax, x);
Shucai Xiao's avatar
Shucai Xiao committed
7452

7453
7454
7455
    auto sitem2 = mm->add_instruction(migraphx::make_op("mul"), sign_x, item2);
    auto item12 = mm->add_instruction(migraphx::make_op("sub"), item1, sitem2);
    auto r      = mm->add_instruction(migraphx::make_op("mul"), item12, mblg);
Shucai Xiao's avatar
Shucai Xiao committed
7456
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7457
7458
7459
7460
7461
7462

    auto prog = migraphx::parse_onnx("selu_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7463
TEST_CASE(shape_test)
7464
7465
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7466
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
7467
    migraphx::shape s{migraphx::shape::float_type, {3, 4, 5, 6}};
Shucai Xiao's avatar
Shucai Xiao committed
7468
    auto l0 = mm->add_parameter("x", s);
Khalique's avatar
Khalique committed
7469
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
Shucai Xiao's avatar
Shucai Xiao committed
7470
    mm->add_literal(s_shape, l0->get_shape().lens());
Shucai Xiao's avatar
Shucai Xiao committed
7471
    auto prog = optimize_onnx("shape_test.onnx");
7472
7473
7474
7475

    EXPECT(p == prog);
}

7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
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
7542
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
TEST_CASE(shape_dyn_test0)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret = mm->add_instruction(migraphx::make_op("dimensions_of", {{"end", 4}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = parse_onnx("shape_dyn_test0.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_dyn_test1)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret =
        mm->add_instruction(migraphx::make_op("dimensions_of", {{"start", 2}, {"end", 4}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = parse_onnx("shape_dyn_test1.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_dyn_test2)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret =
        mm->add_instruction(migraphx::make_op("dimensions_of", {{"start", 2}, {"end", 4}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = parse_onnx("shape_dyn_test2.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_dyn_test3)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret =
        mm->add_instruction(migraphx::make_op("dimensions_of", {{"start", 1}, {"end", 2}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = parse_onnx("shape_dyn_test3.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_end_oob_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret = mm->add_instruction(migraphx::make_op("dimensions_of", {{"end", 4}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = migraphx::parse_onnx("shape_end_oob_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_start_oob_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}}};
    auto p0 = mm->add_parameter("x", s);
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
    auto ret = mm->add_instruction(migraphx::make_op("dimensions_of", {{"end", 4}}), p0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    auto prog                       = migraphx::parse_onnx("shape_start_oob_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(shape_end_less_start_error)
{
    migraphx::onnx_options options;
    options.map_dyn_input_dims["x"] = {{1, 4, {1, 4}}, {4, 4}, {2, 4}, {2, 4}};
    EXPECT(test::throws([&] { migraphx::parse_onnx("shape_end_less_start_error.onnx", options); }));
}

Khalique's avatar
Khalique committed
7588
TEST_CASE(shape_gather_test)
7589
7590
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7591
7592
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {7, 3, 10}});
7593
    migraphx::shape const_shape{migraphx::shape::int32_type, {1}};
Shucai Xiao's avatar
Shucai Xiao committed
7594
    auto l2 = mm->add_literal(migraphx::literal{const_shape, {1}});
Khalique's avatar
Khalique committed
7595
    auto l1 =
Shucai Xiao's avatar
Shucai Xiao committed
7596
        mm->add_literal(migraphx::shape{migraphx::shape::int64_type, {3}}, l0->get_shape().lens());
Khalique's avatar
Khalique committed
7597
    int axis = 0;
7598
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
7599
    auto prog = optimize_onnx("shape_gather_test.onnx");
7600
7601
7602
7603

    EXPECT(p == prog);
}

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
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
TEST_CASE(shrink_hard_test)
{
    migraphx::program p;
    float bias  = 0.0;
    float lambd = 1.5;
    std::vector<size_t> lens{5};
    auto* mm           = p.get_main_module();
    auto x             = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, lens});
    auto lit_bias      = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {bias}});
    auto lit_neg_lambd = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {-lambd}});
    auto lit_lambd     = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {lambd}});

    auto x_plus_bias = add_common_op(*mm, migraphx::make_op("add"), {x, lit_bias});
    auto x_min_bias  = add_common_op(*mm, migraphx::make_op("sub"), {x, lit_bias});

    auto cond1   = add_common_op(*mm, migraphx::make_op("less"), {x, lit_neg_lambd});
    auto cond2_a = add_common_op(*mm, migraphx::make_op("not"), {cond1});
    auto cond2_b = add_common_op(*mm, migraphx::make_op("greater"), {x, lit_lambd});
    auto cond2   = add_common_op(*mm, migraphx::make_op("logical_and"), {cond2_a, cond2_b});

    auto mul1 = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), cond1);
    auto mul2 = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), cond2);

    auto first  = add_common_op(*mm, migraphx::make_op("mul"), {mul1, x_plus_bias});
    auto second = add_common_op(*mm, migraphx::make_op("mul"), {mul2, x_min_bias});
    add_common_op(*mm, migraphx::make_op("add"), {first, second});
    auto prog = optimize_onnx("shrink_hard_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(shrink_int8_test)
{
    migraphx::program p;
    float bias  = 1.5;
    float lambd = 1.5;
    std::vector<size_t> lens{3, 3};
    auto* mm           = p.get_main_module();
    auto x             = mm->add_parameter("x", migraphx::shape{migraphx::shape::int8_type, lens});
    auto lit_bias      = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {bias}});
    auto lit_neg_lambd = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {-lambd}});
    auto lit_lambd     = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {lambd}});

    auto x_plus_bias = add_common_op(*mm, migraphx::make_op("add"), {x, lit_bias});
    auto x_min_bias  = add_common_op(*mm, migraphx::make_op("sub"), {x, lit_bias});

    auto cond1   = add_common_op(*mm, migraphx::make_op("less"), {x, lit_neg_lambd});
    auto cond2_a = add_common_op(*mm, migraphx::make_op("not"), {cond1});
    auto cond2_b = add_common_op(*mm, migraphx::make_op("greater"), {x, lit_lambd});
    auto cond2   = add_common_op(*mm, migraphx::make_op("logical_and"), {cond2_a, cond2_b});

    auto mul1 = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::int8_type}}), cond1);
    auto mul2 = mm->add_instruction(
        migraphx::make_op("convert", {{"target_type", migraphx::shape::int8_type}}), cond2);

    auto first  = add_common_op(*mm, migraphx::make_op("mul"), {mul1, x_plus_bias});
    auto second = add_common_op(*mm, migraphx::make_op("mul"), {mul2, x_min_bias});
    auto ret    = add_common_op(*mm, migraphx::make_op("add"), {first, second});
    mm->add_instruction(migraphx::make_op("convert", {{"target_type", migraphx::shape::int8_type}}),
                        ret);
    auto prog = optimize_onnx("shrink_int8_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7671
TEST_CASE(sign_test)
Khalique's avatar
Khalique committed
7672
7673
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7674
7675
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {10, 5}});
7676
    mm->add_instruction(migraphx::make_op("sign"), input);
Khalique's avatar
Khalique committed
7677

Shucai Xiao's avatar
Shucai Xiao committed
7678
    auto prog = optimize_onnx("sign_test.onnx");
Khalique's avatar
Khalique committed
7679
7680
7681
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7682
TEST_CASE(sin_test)
7683
7684
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7685
7686
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
7687
    mm->add_instruction(migraphx::make_op("sin"), input);
7688

Shucai Xiao's avatar
Shucai Xiao committed
7689
    auto prog = optimize_onnx("sin_test.onnx");
7690
7691
7692
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7693
TEST_CASE(sinh_test)
7694
7695
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7696
7697
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
7698
    mm->add_instruction(migraphx::make_op("sinh"), input);
Khalique's avatar
Khalique committed
7699

Shucai Xiao's avatar
Shucai Xiao committed
7700
    auto prog = optimize_onnx("sinh_test.onnx");
7701
7702
7703
7704

    EXPECT(p == prog);
}

7705
7706
7707
7708
TEST_CASE(sinh_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7709
    migraphx::shape::dynamic_dimension dd{1, 10};
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
    std::vector<migraphx::shape::dynamic_dimension> dyn_dims;
    dyn_dims.push_back(dd);
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dyn_dims});
    auto ret   = mm->add_instruction(migraphx::make_op("sinh"), input);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = dd;
    auto prog                     = parse_onnx("sinh_dynamic_test.onnx", options);

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
TEST_CASE(size_float_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s   = migraphx::shape{migraphx::shape::float_type, {2, 3, 4}};
    mm->add_parameter("x", s);
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type, {s.elements()}});

    auto prog = optimize_onnx("size_float_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(size_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s   = migraphx::shape{migraphx::shape::half_type, {3, 1}};
    mm->add_parameter("x", s);
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type, {s.elements()}});
    auto prog = optimize_onnx("size_half_test.onnx");
    EXPECT(p == prog);
}

TEST_CASE(size_int_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto s   = migraphx::shape{migraphx::shape::int32_type, {8, 2, 3}};
    mm->add_parameter("x", s);
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type, {s.elements()}});
    auto prog = optimize_onnx("size_int_test.onnx");
    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
7757
7758
7759
TEST_CASE(slice_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7760
7761
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 2}});
7762
7763
    mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {2, 2}}}), l0);
kahmed10's avatar
kahmed10 committed
7764
7765
7766
7767
7768
    auto prog = optimize_onnx("slice_test.onnx");

    EXPECT(p == prog);
}

7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
TEST_CASE(slice_constant_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(migraphx::literal{
        migraphx::shape{migraphx::shape::float_type, {3, 2}}, {0, 1, 2, 3, 4, 5}});
    mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {2, 2}}}), l0);
    auto prog = optimize_onnx("slice_constant_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
7782
7783
7784
7785
7786
7787
TEST_CASE(slice_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter(
7788
        "0", migraphx::shape{migraphx::shape::float_type, {{3, 3}, {1, 3}, {2, 2}}});
Brian Pickrell's avatar
Brian Pickrell committed
7789
7790
7791
7792
7793
7794
7795
    auto ret = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), l0);
    mm->add_return({ret});

    migraphx::onnx_options options;
    // Parser converts the dynamic input shape to static unless there is at least one non-fixed
    // dynamic dimension. Slicing is not allowed along the non-fixed axis 1.
7796
    options.map_dyn_input_dims["0"] = {{3, 3}, {1, 3}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
    auto prog                       = migraphx::parse_onnx("slice_dyn_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(slice_step_dyn_test)
{
    // A slice command with non-default steps will have a "Step" instruction added in parsing.
    // At the time of writing, Step doesn't support dynamic shape input.
    migraphx::onnx_options options;
7807
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
7808
7809
7810
7811
7812
7813
7814
7815
    EXPECT(test::throws([&] { migraphx::parse_onnx("slice_step_dyn_test.onnx", options); }));
}

TEST_CASE(slice_reverse_dyn_test)
{
    // A slice command with negative step on any axis will have a "Reverse" instruction added in
    // parsing. At the time of writing, Reverse doesn't support dynamic shape input.
    migraphx::onnx_options options;
7816
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
7817
7818
7819
    EXPECT(test::throws([&] { migraphx::parse_onnx("slice_reverse_dyn_test.onnx", options); }));
}

kahmed10's avatar
kahmed10 committed
7820
7821
7822
TEST_CASE(slice_3arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7823
7824
7825
7826
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 5}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {0, 0}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {2, 5}});
7827
7828
    auto ret = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {0, 0}}, {"ends", {2, 5}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
7829
    mm->add_return({ret});
kahmed10's avatar
kahmed10 committed
7830
7831
7832
7833
7834
7835

    auto prog = migraphx::parse_onnx("slice_3arg_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
7836
7837
7838
TEST_CASE(slice_5arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7839
7840
7841
7842
7843
7844
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 5}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {1, 1}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -2}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -1}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-5, -3}});
7845
7846
7847
    auto ret = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {-1, -2}}, {"starts", {-5, -3}}, {"ends", {-1, -1}}}),
        l0);
Shucai Xiao's avatar
Shucai Xiao committed
7848
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
7849
7850
7851
7852
7853
7854

    auto prog = migraphx::parse_onnx("slice_5arg_test.onnx");

    EXPECT(p == prog);
}

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
7855
7856
7857
7858
7859
7860
7861
TEST_CASE(slice_5arg_reverse_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 5}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, 1}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -2}});
7862
7863
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-5, -1}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -3}});
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
7864
    auto slice_out = mm->add_instruction(
7865
7866
        migraphx::make_op("slice",
                          {{"axes", {-1, -2}}, {"starts", {-4, -3}}, {"ends", {2147483647, -1}}}),
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
7867
7868
7869
7870
7871
7872
7873
7874
7875
        l0);
    auto ret = mm->add_instruction(migraphx::make_op("reverse", {{"axes", {-1}}}), slice_out);
    mm->add_return({ret});

    auto prog = migraphx::parse_onnx("slice_5arg_reverse_test.onnx");

    EXPECT(p == prog);
}

7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
TEST_CASE(slice_5arg_step_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 5}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-2, 2}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -2}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-5, -1}});
    mm->add_literal({{migraphx::shape::int32_type, {2}}, {-1, -3}});
    auto slice_out = mm->add_instruction(
        migraphx::make_op("slice",
                          {{"axes", {-1, -2}}, {"starts", {-4, -3}}, {"ends", {2147483647, -1}}}),
        l0);
    auto reverse_out =
        mm->add_instruction(migraphx::make_op("reverse", {{"axes", {-1}}}), slice_out);
    auto step_out = mm->add_instruction(
        migraphx::make_op("step", {{"axes", {-1, -2}}, {"steps", {2, 2}}}), reverse_out);
    mm->add_return({step_out});

    auto prog = migraphx::parse_onnx("slice_5arg_step_test.onnx");

    EXPECT(p == prog);
}

7900
7901
7902
TEST_CASE(slice_max_end_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7903
7904
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {10, 20}});
7905
7906
7907
7908
    mm->add_instruction(
        migraphx::make_op("slice",
                          {{"axes", {0, 1}}, {"starts", {1, 2}}, {"ends", {3000000000, -1}}}),
        l0);
7909
7910
7911
7912
7913
    auto prog = optimize_onnx("slice_max_end_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
TEST_CASE(slice_var_input_static0)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto data   = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 2}});
    auto starts = mm->add_parameter("starts", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto ends   = mm->add_parameter("ends", migraphx::shape{migraphx::shape::int32_type, {2}});
    mm->add_instruction(migraphx::make_op("slice", {{"axes", {0, 1}}}), data, starts, ends);
    auto prog = optimize_onnx("slice_var_input_static0.onnx");

    EXPECT(p == prog);
}

TEST_CASE(slice_var_input_static1)
{
    migraphx::program p;
    auto* mm    = p.get_main_module();
    auto data   = mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 2}});
    auto starts = mm->add_parameter("starts", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto ends   = mm->add_parameter("ends", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto axes   = mm->add_parameter("axes", migraphx::shape{migraphx::shape::int64_type, {2}});
    mm->add_instruction(migraphx::make_op("slice"), data, starts, ends, axes);
    auto prog = optimize_onnx("slice_var_input_static1.onnx");

    EXPECT(p == prog);
}

TEST_CASE(slice_var_input_dyn0)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto data =
        mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {{3, 8}, {2, 2}}});
    auto starts = mm->add_parameter("starts", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto ends   = mm->add_parameter("ends", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto ret =
        mm->add_instruction(migraphx::make_op("slice", {{"axes", {0, 1}}}), data, starts, ends);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {3, 8};
    auto prog                     = parse_onnx("slice_var_input_dyn0.onnx", options);
    EXPECT(p == prog);
}

TEST_CASE(slice_var_input_dyn1)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto data =
        mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {{3, 8}, {2, 2}}});
    auto starts = mm->add_parameter("starts", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto ends   = mm->add_parameter("ends", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto axes   = mm->add_parameter("axes", migraphx::shape{migraphx::shape::int32_type, {2}});
    auto ret    = mm->add_instruction(migraphx::make_op("slice"), data, starts, ends, axes);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {3, 8};
    auto prog                     = parse_onnx("slice_var_input_dyn1.onnx", options);
    EXPECT(p == prog);
}

7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
TEST_CASE(slice_var_input_default_steps)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto data =
        mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {{3, 8}, {2, 2}}});
    auto starts = mm->add_parameter("starts", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto ends   = mm->add_parameter("ends", migraphx::shape{migraphx::shape::int64_type, {2}});
    auto axes   = mm->add_parameter("axes", migraphx::shape{migraphx::shape::int64_type, {2}});
    mm->add_literal({{migraphx::shape::int64_type, {2}}, {1, 1}});
    auto ret = mm->add_instruction(migraphx::make_op("slice"), data, starts, ends, axes);
    mm->add_return({ret});

    migraphx::onnx_options options;
    options.default_dyn_dim_value = {3, 8};
    auto prog                     = parse_onnx("slice_var_input_default_steps.onnx", options);
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
7996
7997
7998
7999
8000
TEST_CASE(slice_var_input_steps_error)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("slice_var_input_steps_error.onnx"); }));
}

Khalique's avatar
Khalique committed
8001
TEST_CASE(softmax_test)
Shucai Xiao's avatar
Shucai Xiao committed
8002
8003
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8004
8005
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
8006
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8007
    auto prog = optimize_onnx("softmax_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8008
8009
8010
8011

    EXPECT(p == prog);
}

8012
8013
8014
8015
8016
8017
8018
TEST_CASE(softmax_nonstd_input_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {6, 8}});
    auto l1  = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {4, 4}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8019
    auto l2 = mm->add_instruction(migraphx::make_op("softmax", {{"axis", -1}}), l1);
8020
8021
8022
8023
8024
8025
8026
    mm->add_return({l2});

    auto prog = migraphx::parse_onnx("softmax_nonstd_input_test.onnx");

    EXPECT(p == prog);
}

8027
8028
8029
8030
8031
TEST_CASE(softmax_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
8032
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {4, 4}}});
8033
8034
8035
8036
    auto ret = mm->add_instruction(migraphx::make_op("softmax", {{"axis", -1}}), l0);
    mm->add_return({ret});

    migraphx::onnx_options options;
8037
    options.default_dyn_dim_value = {1, 4};
8038
8039
8040
8041
8042
    auto prog                     = migraphx::parse_onnx("softmax_dyn_test.onnx", options);

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
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
TEST_CASE(softplus_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    std::vector<std::size_t> input_lens{5};
    auto input_type = migraphx::shape::float_type;

    auto x = mm->add_parameter("x", migraphx::shape{input_type, input_lens});
    auto mb_ones =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));
    auto exp = mm->add_instruction(migraphx::make_op("exp"), x);
    auto add = mm->add_instruction(migraphx::make_op("add"), exp, mb_ones);
    mm->add_instruction(migraphx::make_op("log"), add);

    auto prog = optimize_onnx("softplus_test.onnx");
    EXPECT(p == prog);
}

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

    std::vector<std::size_t> input_lens{3, 4, 5};
    auto input_type = migraphx::shape::half_type;

    auto x = mm->add_parameter("x", migraphx::shape{input_type, input_lens});
    auto mb_ones =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));
    auto exp = mm->add_instruction(migraphx::make_op("exp"), x);
    auto add = mm->add_instruction(migraphx::make_op("add"), exp, mb_ones);
    mm->add_instruction(migraphx::make_op("log"), add);

    auto prog = optimize_onnx("softplus_nd_test.onnx");
    EXPECT(p == prog);
}

turneram's avatar
turneram committed
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
TEST_CASE(softsign_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    std::vector<std::size_t> input_lens{5};
    auto input_type = migraphx::shape::float_type;

    auto x = mm->add_parameter("x", migraphx::shape{input_type, input_lens});
    auto mb_ones =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));
    auto abs = mm->add_instruction(migraphx::make_op("abs"), x);
    auto add = mm->add_instruction(migraphx::make_op("add"), abs, mb_ones);
    mm->add_instruction(migraphx::make_op("div"), x, add);

    auto prog = optimize_onnx("softsign_test.onnx");
    EXPECT(p == prog);
}

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

    std::vector<std::size_t> input_lens{3, 4, 5};
    auto input_type = migraphx::shape::half_type;

    auto x = mm->add_parameter("x", migraphx::shape{input_type, input_lens});
    auto mb_ones =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                            mm->add_literal(migraphx::literal{migraphx::shape{input_type}, {1}}));
    auto abs = mm->add_instruction(migraphx::make_op("abs"), x);
    auto add = mm->add_instruction(migraphx::make_op("add"), abs, mb_ones);
    mm->add_instruction(migraphx::make_op("div"), x, add);

    auto prog = optimize_onnx("softsign_nd_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8123
8124
8125
TEST_CASE(split_minus_axis_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8126
8127
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8128
8129
8130
8131
8132
8133
    auto r1    = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {-1}}, {"starts", {0}}, {"ends", {5}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {-1}}, {"starts", {5}}, {"ends", {10}}}), input);
    auto r3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {-1}}, {"starts", {10}}, {"ends", {15}}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
8134
    mm->add_return({r1, r2, r3});
Shucai Xiao's avatar
Shucai Xiao committed
8135
8136
8137
8138
8139
8140

    auto prog = migraphx::parse_onnx("split_minus_axis_test.onnx");

    EXPECT(p == prog);
}

8141
8142
8143
TEST_CASE(split_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8144
8145
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8146
8147
8148
8149
8150
8151
    auto r1    = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {7}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {1}}, {"starts", {7}}, {"ends", {11}}}), input);
    auto r3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {1}}, {"starts", {11}}, {"ends", {15}}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
8152
    mm->add_return({r1, r2, r3});
8153
8154
8155
8156
8157

    auto prog = migraphx::parse_onnx("split_test.onnx");
    EXPECT(p == prog);
}

8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
TEST_CASE(split_test_no_attribute)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape si{migraphx::shape::int64_type, {4}, {1}};
    std::vector<int> ind = {75, 75, 75, 75};

    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {300, 15}});
    mm->add_literal(migraphx::literal(si, ind));
    auto r1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {75}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {75}}, {"ends", {150}}}), input);
    auto r3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {150}}, {"ends", {225}}}), input);
    auto r4 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {225}}, {"ends", {300}}}), input);

    mm->add_return({r1, r2, r3, r4});

    auto prog = migraphx::parse_onnx("split_test_no_attribute.onnx");
    EXPECT(p == prog);
}

8183
8184
8185
TEST_CASE(split_test_default)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8186
8187
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8188
8189
8190
8191
    auto r1    = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {5}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {5}}, {"ends", {10}}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
8192
    mm->add_return({r1, r2});
8193
8194
8195
8196
8197

    auto prog = migraphx::parse_onnx("split_test_default.onnx");
    EXPECT(p == prog);
}

8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
TEST_CASE(split_test_uneven)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {12, 15}});
    auto r1    = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {3}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {3}}, {"ends", {6}}}), input);
    auto r3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {6}}, {"ends", {9}}}), input);
    auto r4 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {9}}, {"ends", {12}}}), input);
    auto r5 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {12}}, {"ends", {12}}}), input);
    mm->add_return({r1, r2, r3, r4, r5});

    auto prog = migraphx::parse_onnx("split_test_uneven.onnx");
    EXPECT(p == prog);
}

TEST_CASE(split_test_uneven_num_outputs)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {11, 15}});
    auto r1    = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {3}}}), input);
    auto r2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {3}}, {"ends", {6}}}), input);
    auto r3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {6}}, {"ends", {9}}}), input);
    auto r4 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {9}}, {"ends", {11}}}), input);
    mm->add_return({r1, r2, r3, r4});

    auto prog = migraphx::parse_onnx("split_test_uneven_num_outputs.onnx");
    EXPECT(p == prog);
}

8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
TEST_CASE(split_test_no_attribute_invalid_split)
{
    EXPECT(
        test::throws([&] { migraphx::parse_onnx("split_test_no_attribute_invalid_split.onnx"); }));
}

TEST_CASE(split_test_invalid_split)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("split_test_invalid_split.onnx"); }));
}

TEST_CASE(split_test_no_attribute_invalid_input_split)
{
    EXPECT(test::throws(
        [&] { migraphx::parse_onnx("split_test_no_attribute_invalid_input_split.onnx"); }));
}

8255
8256
8257
8258
8259
TEST_CASE(split_test_invalid_num_outputs)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("split_test_invalid_num_outputs.onnx"); }));
}

Khalique's avatar
Khalique committed
8260
TEST_CASE(sqrt_test)
Shucai Xiao's avatar
Shucai Xiao committed
8261
8262
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8263
8264
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8265
    mm->add_instruction(migraphx::make_op("sqrt"), input);
Shucai Xiao's avatar
Shucai Xiao committed
8266

Shucai Xiao's avatar
Shucai Xiao committed
8267
    auto prog = optimize_onnx("sqrt_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8268
8269
8270
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8271
TEST_CASE(squeeze_unsqueeze_test)
Khalique's avatar
Khalique committed
8272
8273
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8274
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
8275
8276
8277
    std::vector<int64_t> squeeze_axes{0, 2, 3, 5};
    std::vector<int64_t> unsqueeze_axes{0, 1, 3, 5};
    auto l0 =
Shucai Xiao's avatar
Shucai Xiao committed
8278
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 1, 1, 2, 1}});
8279
8280
    auto l1 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", squeeze_axes}}), l0);
    mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", unsqueeze_axes}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
8281
    auto prog = optimize_onnx("squeeze_unsqueeze_test.onnx");
Khalique's avatar
Khalique committed
8282

8283
8284
8285
8286
8287
8288
8289
8290
8291
    EXPECT(p == prog);
}

TEST_CASE(squeeze_unsqueeze_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    std::vector<int64_t> squeeze_axes{0, 2, 3, 5};
    std::vector<int64_t> unsqueeze_axes{0, 1, 3, 5};
8292
8293
    auto l0  = mm->add_parameter("0",
                                migraphx::shape{migraphx::shape::float_type,
8294
                                                 {{1, 1}, {1, 4}, {1, 1}, {1, 1}, {1, 4}, {1, 1}}});
8295
8296
8297
8298
8299
8300
8301
    auto c0  = mm->add_instruction(migraphx::make_op("contiguous"), l0);
    auto l1  = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", squeeze_axes}}), c0);
    auto c1  = mm->add_instruction(migraphx::make_op("contiguous"), l1);
    auto ret = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", unsqueeze_axes}}), c1);
    mm->add_return({ret});

    migraphx::onnx_options options;
8302
    options.default_dyn_dim_value = {1, 4};
8303
8304
    auto prog                     = parse_onnx("squeeze_unsqueeze_dyn_test.onnx", options);

Khalique's avatar
Khalique committed
8305
8306
8307
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
TEST_CASE(squeeze_axes_input_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal({migraphx::shape::int64_type, {2}}, {1, 3}));
    auto l0 = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 1, 5, 1}});
    auto l1 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1, 3}}}), l0);
    mm->add_return({l1});

    auto prog = migraphx::parse_onnx("squeeze_axes_input_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(squeeze_empty_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8326
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
8327
8328
8329
8330
8331
8332
8333
8334
8335
    auto l0 = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 1, 5, 1}});
    auto l1 = mm->add_instruction(migraphx::make_op("squeeze"), l0);
    mm->add_return({l1});

    auto prog = migraphx::parse_onnx("squeeze_empty_axes_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8336
TEST_CASE(sub_bcast_test)
Shucai Xiao's avatar
Shucai Xiao committed
8337
8338
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8339
8340
8341
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 4}});
8342
    auto l2  = mm->add_instruction(
8343
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", l0->get_shape().lens()}}), l1);
8344
    mm->add_instruction(migraphx::make_op("sub"), l0, l2);
Shucai Xiao's avatar
Shucai Xiao committed
8345

Shucai Xiao's avatar
Shucai Xiao committed
8346
    auto prog = optimize_onnx("sub_bcast_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8347
8348
8349
8350

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8351
TEST_CASE(sub_scalar_test)
Shucai Xiao's avatar
Shucai Xiao committed
8352
8353
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8354
8355
8356
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1 = mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1}});
8357
8358
    auto m1 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
8359
    mm->add_instruction(migraphx::make_op("sub"), l0, m1);
Shucai Xiao's avatar
Shucai Xiao committed
8360
    auto prog = optimize_onnx("sub_scalar_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8361
8362
8363
8364

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8365
8366
8367
TEST_CASE(sum_int_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8368
8369
8370
8371
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::int16_type, {3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::uint16_type, {3}});
    auto input2 = mm->add_parameter("2", migraphx::shape{migraphx::shape::uint32_type, {3}});
8372
8373
    auto cin0   = mm->add_instruction(
        migraphx::make_op("convert",
8374
                            {{"target_type", migraphx::to_value(migraphx::shape::uint32_type)}}),
8375
8376
8377
8378
8379
8380
8381
        input0);
    auto cin1 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::uint32_type)}}),
        input1);
    auto l0 = mm->add_instruction(migraphx::make_op("add"), cin0, cin1);
    mm->add_instruction(migraphx::make_op("add"), l0, input2);
Shucai Xiao's avatar
Shucai Xiao committed
8382
8383
8384
8385
8386

    auto prog = optimize_onnx("sum_int_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8387
TEST_CASE(sum_test)
8388
8389
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8390
8391
8392
8393
    auto* mm    = p.get_main_module();
    auto input0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3}});
    auto input2 = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {3}});
8394
8395
    auto l0     = mm->add_instruction(migraphx::make_op("add"), input0, input1);
    mm->add_instruction(migraphx::make_op("add"), l0, input2);
8396

Shucai Xiao's avatar
Shucai Xiao committed
8397
    auto prog = optimize_onnx("sum_test.onnx");
8398
8399
8400
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8401
8402
8403
TEST_CASE(sum_type_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8404
8405
8406
8407
8408
8409
8410
8411
8412
    auto* mm      = p.get_main_module();
    auto l_bool   = mm->add_literal({migraphx::shape{migraphx::shape::bool_type, {2}}, {1, 0}});
    auto l_int8   = mm->add_literal({migraphx::shape{migraphx::shape::int8_type, {2}}, {1, 1}});
    auto l_uint8  = mm->add_literal({migraphx::shape{migraphx::shape::uint8_type, {2}}, {1, 1}});
    auto l_uint16 = mm->add_literal({migraphx::shape{migraphx::shape::uint16_type, {2}}, {1, 1}});
    auto l_uint32 = mm->add_literal({migraphx::shape{migraphx::shape::uint32_type, {2}}, {1, 1}});
    auto l_uint64 = mm->add_literal({migraphx::shape{migraphx::shape::uint64_type, {2}}, {1, 1}});
    auto l_double = mm->add_literal({migraphx::shape{migraphx::shape::double_type, {2}}, {1, 1}});
    auto l_raw  = mm->add_literal({migraphx::shape{migraphx::shape::double_type, {2}}, {1.5, 2.0}});
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
8441
8442
8443
    auto o_bool = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_bool);
    auto o_int8 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_int8);
    auto o_uint8 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_uint8);
    auto o_uint16 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_uint16);
    auto o_uint32 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_uint32);
    auto o_uint64 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::double_type)}}),
        l_uint64);
    auto s0 = mm->add_instruction(migraphx::make_op("add"), o_bool, o_int8);
    auto s1 = mm->add_instruction(migraphx::make_op("add"), s0, o_uint8);
    auto s2 = mm->add_instruction(migraphx::make_op("add"), s1, o_uint16);
    auto s3 = mm->add_instruction(migraphx::make_op("add"), s2, o_uint32);
    auto s4 = mm->add_instruction(migraphx::make_op("add"), s3, o_uint64);
    auto s5 = mm->add_instruction(migraphx::make_op("add"), s4, l_double);
    auto s6 = mm->add_instruction(migraphx::make_op("add"), s5, l_raw);
Shucai Xiao's avatar
Shucai Xiao committed
8444
    mm->add_return({s6});
Shucai Xiao's avatar
Shucai Xiao committed
8445
8446
8447
8448
8449
8450

    auto prog = migraphx::parse_onnx("sum_type_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8451
TEST_CASE(tan_test)
8452
8453
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8454
8455
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
8456
    mm->add_instruction(migraphx::make_op("tan"), input);
8457

Shucai Xiao's avatar
Shucai Xiao committed
8458
    auto prog = optimize_onnx("tan_test.onnx");
8459
8460
8461
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8462
TEST_CASE(tanh_test)
8463
8464
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8465
8466
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1}});
8467
    mm->add_instruction(migraphx::make_op("tanh"), input);
8468

Shucai Xiao's avatar
Shucai Xiao committed
8469
    auto prog = optimize_onnx("tanh_test.onnx");
8470
8471
8472
8473

    EXPECT(p == prog);
}

8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
TEST_CASE(thresholdedrelu_default_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2, 3}});
    auto lz  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {0}});
    auto la  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {1.0f}});
    auto mbz = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), lz);
    auto mba = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), la);
    auto condition = mm->add_instruction(migraphx::make_op("greater"), x, mba);
    mm->add_instruction(migraphx::make_op("where"), condition, x, mbz);

    auto prog = optimize_onnx("thresholdedrelu_default_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(thresholdedrelu_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2, 3}});
    auto lz  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {0}});
    auto la  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {3.0f}});
    auto mbz = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), lz);
    auto mba = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), la);
    auto condition = mm->add_instruction(migraphx::make_op("greater"), x, mba);
    mm->add_instruction(migraphx::make_op("where"), condition, x, mbz);

    auto prog = optimize_onnx("thresholdedrelu_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(thresholdedrelu_int_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter("x", migraphx::shape{migraphx::shape::int32_type, {2, 2, 3}});
    auto lz  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {0}});
    auto la  = mm->add_literal(migraphx::literal{migraphx::shape{x->get_shape().type()}, {3}});
    auto mbz = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), lz);
    auto mba = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", x->get_shape().lens()}}), la);
    auto condition = mm->add_instruction(migraphx::make_op("greater"), x, mba);
    mm->add_instruction(migraphx::make_op("where"), condition, x, mbz);

    auto prog = optimize_onnx("thresholdedrelu_int_test.onnx");

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
8531
8532
8533
TEST_CASE(tile_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8534
8535
8536
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {2}}, {1, 2}});
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
8537
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), input, input);
kahmed10's avatar
kahmed10 committed
8538
8539
8540
8541
8542
8543
8544
8545
8546

    auto prog = optimize_onnx("tile_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(tile_test_3x2)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8547
8548
8549
    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {2}}, {3, 2}});
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
8550
8551
8552
    auto l0    = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), input, input);
    auto l1    = mm->add_instruction(migraphx::make_op("concat", {{"axis", 0}}), l0, input);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l1, l1);
kahmed10's avatar
kahmed10 committed
8553
8554
8555
8556
8557
8558

    auto prog = optimize_onnx("tile_test_3x2.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
TEST_CASE(transpose_default_perm_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 5, 2, 3}});
    std::vector<int64_t> perm{3, 2, 1, 0};
    auto r = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("transpose_default_perm_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(transpose_invalid_perm_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("transpose_invalid_perm_test.onnx"); }));
}

Khalique's avatar
Khalique committed
8578
TEST_CASE(transpose_test)
8579
8580
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8581
8582
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
Khalique's avatar
Khalique committed
8583
    std::vector<int64_t> perm{0, 3, 1, 2};
8584
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
Khalique's avatar
Khalique committed
8585

Shucai Xiao's avatar
Shucai Xiao committed
8586
    auto prog = optimize_onnx("transpose_test.onnx");
8587
8588
8589
8590

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
8591
8592
8593
8594
8595
TEST_CASE(transpose_dyn_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter(
8596
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}, {3, 3}}});
Charlie Lin's avatar
Charlie Lin committed
8597
8598
8599
8600
8601
    std::vector<int64_t> perm{0, 3, 1, 2};
    auto t0 = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
    mm->add_return({t0});

    migraphx::onnx_options options;
8602
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
8603
8604
8605
8606
8607
    auto prog                     = migraphx::parse_onnx("transpose_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
TEST_CASE(topk_attrk_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {2, 5, 3, 2}};
    auto data = mm->add_parameter("data", s);
    auto out  = mm->add_instruction(migraphx::make_op("topk", {{"k", 2}, {"axis", -1}}), data);
    auto val  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto ind  = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    mm->add_return({val, ind});

    auto prog = migraphx::parse_onnx("topk_attrk_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(topk_neg_axis_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sk{migraphx::shape::int64_type, {1}};
    mm->add_literal(migraphx::literal(sk, {3}));
    migraphx::shape s{migraphx::shape::float_type, {3, 4, 5, 6}};
    auto data = mm->add_parameter("data", s);
    auto out  = mm->add_instruction(
        migraphx::make_op("topk", {{"k", 3}, {"axis", -2}, {"largest", 1}}), data);
    auto val = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    mm->add_return({val, ind});

    auto prog = migraphx::parse_onnx("topk_neg_axis_test.onnx");

    EXPECT(p == prog);
}

TEST_CASE(topk_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape sk{migraphx::shape::int64_type, {1}};
    mm->add_literal(migraphx::literal(sk, {4}));
    migraphx::shape s{migraphx::shape::float_type, {2, 5, 3, 2}};
    auto data = mm->add_parameter("data", s);
    auto out  = mm->add_instruction(
        migraphx::make_op("topk", {{"k", 4}, {"axis", 1}, {"largest", 0}}), data);
    auto val = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    mm->add_return({val, ind});

    auto prog = migraphx::parse_onnx("topk_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8662
TEST_CASE(transpose_gather_test)
8663
8664
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8665
8666
    auto* mm             = p.get_main_module();
    auto make_contiguous = [&mm](migraphx::instruction_ref ins) {
Khalique's avatar
Khalique committed
8667
8668
8669
8670
8671
        if(ins->get_shape().standard())
        {
            return ins;
        }

8672
        return mm->add_instruction(migraphx::make_op("contiguous"), ins);
Khalique's avatar
Khalique committed
8673
8674
    };

Shucai Xiao's avatar
Shucai Xiao committed
8675
8676
    auto data =
        mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 5, 4, 6}});
Khalique's avatar
Khalique committed
8677
    auto ind =
Shucai Xiao's avatar
Shucai Xiao committed
8678
        mm->add_parameter("indices", migraphx::shape{migraphx::shape::int32_type, {2, 4, 3, 5}});
8679
    auto tr_data =
8680
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1, 3}}}), data);
8681
    auto tr_ind =
8682
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1, 3}}}), ind);
8683
8684
8685
8686
    int axis = 1;
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}),
                        make_contiguous(tr_data),
                        make_contiguous(tr_ind));
Khalique's avatar
Khalique committed
8687

Shucai Xiao's avatar
Shucai Xiao committed
8688
    auto prog = optimize_onnx("transpose_gather_test.onnx");
8689

8690
    EXPECT(p.sort() == prog.sort());
8691
8692
}

8693
8694
8695
TEST_CASE(undefined_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8696
8697
    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
8698
8699
    auto l1 = mm->add_instruction(migraphx::make_op("undefined"));
    auto l2 = mm->add_instruction(migraphx::make_op("identity"), l1);
Shucai Xiao's avatar
Shucai Xiao committed
8700
    mm->add_return({l2});
8701
8702
8703
8704
8705
8706

    auto prog = migraphx::parse_onnx("undefined_test.onnx");

    EXPECT(p == prog);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
TEST_CASE(unique_dynamic_sorted_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape s{migraphx::shape::float_type, {6}};
    auto x = mm->add_parameter("X", s);

    auto out   = mm->add_instruction(migraphx::make_op("unique", {{"sorted", 1}, {"axis", 0}}), x);
    auto y     = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto y_ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    auto x_ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), out);
    auto count = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 3}}), out);

    mm->add_return({y, y_ind, x_ind, count});
    auto prog = migraphx::parse_onnx("unique_dynamic_sorted_test.onnx");

    EXPECT(p == prog);
}

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

    migraphx::shape s{migraphx::shape::int64_type, {4, 4, 4}};
    auto x = mm->add_parameter("X", s);

    auto out   = mm->add_instruction(migraphx::make_op("unique", {{"sorted", 1}}), x);
    auto y     = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto y_ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    auto x_ind = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), out);
    auto count = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 3}}), out);

    mm->add_return({y, y_ind, x_ind, count});
    auto prog = migraphx::parse_onnx("unique_dynamic_sorted_3D_test.onnx");

    EXPECT(p == prog);
}

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

    migraphx::shape s_x{migraphx::shape::float_type, {6}};
    std::vector<float> x_data = {2, 1, 1, 3, 4, 3};
    auto x                    = mm->add_literal(migraphx::literal(s_x, x_data));

    auto out   = mm->add_instruction(migraphx::make_op("unique", {{"sorted", 1}, {"axis", 0}}), x);
    auto y     = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto y_idx = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    auto x_idx = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), out);
    auto count = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 3}}), out);
    mm->add_return({y, y_idx, x_idx, count});
    auto prog = migraphx::parse_onnx("unique_sorted_test.onnx");

    EXPECT(p == prog);
}

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

    migraphx::shape s_x{migraphx::shape::float_type, {6}};
    std::vector<float> x_data = {2, 1, 1, 3, 4, 3};
    auto x                    = mm->add_literal(migraphx::literal(s_x, x_data));

    auto out   = mm->add_instruction(migraphx::make_op("unique", {{"sorted", 0}, {"axis", 0}}), x);
    auto y     = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 0}}), out);
    auto y_idx = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 1}}), out);
    auto x_idx = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 2}}), out);
    auto count = mm->add_instruction(migraphx::make_op("get_tuple_elem", {{"index", 3}}), out);
    mm->add_return({y, y_idx, x_idx, count});
    auto prog = migraphx::parse_onnx("unique_unsorted_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8787
TEST_CASE(unknown_test)
8788
8789
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8790
8791
8792
8793
8794
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {3, 4}});
    auto l2  = mm->add_instruction(migraphx::op::unknown{"Unknown"}, l0, l1);
    mm->add_instruction(migraphx::op::unknown{"Unknown"}, l2);
Shucai Xiao's avatar
Shucai Xiao committed
8795
    auto prog = optimize_onnx("unknown_test.onnx");
8796
8797
8798
8799

    EXPECT(p == prog);
}

8800
8801
8802
8803
8804
TEST_CASE(unknown_aten_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("unknown_aten_test.onnx"); }));
}

8805
8806
8807
8808
8809
TEST_CASE(unknown_test_throw)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("unknown_test.onnx"); }));
}

Shucai Xiao's avatar
Shucai Xiao committed
8810
8811
8812
8813
8814
8815
8816
TEST_CASE(upsample_linear_test)
{
    auto p    = create_upsample_linear_prog();
    auto prog = migraphx::parse_onnx("upsample_linear_test.onnx");
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8817
8818
8819
TEST_CASE(upsample_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8820
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8821
    migraphx::shape ss{migraphx::shape::float_type, {4}};
Shucai Xiao's avatar
Shucai Xiao committed
8822
    mm->add_literal(migraphx::literal(ss, {1.0f, 1.0f, 2.0f, 3.0f}));
Shucai Xiao's avatar
Shucai Xiao committed
8823
8824

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
8825
    auto ix = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
8826
8827
8828
8829

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

Shucai Xiao's avatar
Shucai Xiao committed
8830
    auto li  = mm->add_literal(migraphx::literal(si, ind));
8831
8832
    auto rsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), ix);
    auto r   = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp, li);
Shucai Xiao's avatar
Shucai Xiao committed
8833
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
8834
8835
8836
8837
8838
8839

    auto prog = migraphx::parse_onnx("upsample_test.onnx");

    EXPECT(p == prog);
}

8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
TEST_CASE(upsample_ver7_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
    auto ix = mm->add_parameter("X", sx);

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

    auto li  = mm->add_literal(migraphx::literal(si, ind));
    auto rsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4}}}), ix);
    auto r   = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), rsp, li);
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("upsample_ver7_test.onnx");

    EXPECT(p == prog);
}

8861
8862
8863
8864
8865
8866
8867
TEST_CASE(unknown_test_throw_print_error)
{
    migraphx::onnx_options options;
    options.print_program_on_error = true;
    EXPECT(test::throws([&] { migraphx::parse_onnx("unknown_test.onnx", options); }));
}

8868
8869
8870
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8871
8872
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
8873
    mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8874
    auto prog = optimize_onnx("variable_batch_test.onnx");
8875
8876
8877
8878

    EXPECT(p == prog);
}

8879
8880
8881
8882
8883
8884
8885
8886
8887
TEST_CASE(variable_batch_user_input_test1)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 16, 16}});
    auto r   = mm->add_instruction(migraphx::make_op("identity"), l0);
    mm->add_return({r});

    migraphx::onnx_options options;
8888
    options.default_dyn_dim_value = {2, 2};
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898

    auto prog = migraphx::parse_onnx("variable_batch_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(variable_batch_user_input_test2)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8899
8900
8901
    auto l0  = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 5}, {3, 3}, {16, 16}, {16, 16}}});
    auto r = mm->add_instruction(migraphx::make_op("identity"), l0);
8902
8903
8904
    mm->add_return({r});

    migraphx::onnx_options options;
8905
    options.default_dyn_dim_value = {2, 5};
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915

    auto prog = migraphx::parse_onnx("variable_batch_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(variable_batch_user_input_test3)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
8916
8917
8918
    auto l0  = mm->add_parameter(
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 5}, {3, 3}, {16, 16}, {16, 16}}});
    auto r = mm->add_instruction(migraphx::make_op("identity"), l0);
8919
8920
8921
    mm->add_return({r});

    migraphx::onnx_options options;
8922
    options.map_dyn_input_dims["0"] = {{2, 5}, {3, 3}, {16, 16}, {16, 16}};
8923
8924
8925
8926
8927
8928
8929

    auto prog = migraphx::parse_onnx("variable_batch_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(variable_batch_user_input_test4)
8930
8931
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8932
8933
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 16, 16}});
8934
    auto r   = mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8935
    mm->add_return({r});
8936
8937
8938
8939
8940
8941
8942
8943
8944

    migraphx::onnx_options options;
    options.default_dim_value = 2;

    auto prog = migraphx::parse_onnx("variable_batch_test.onnx", options);

    EXPECT(p == prog);
}

8945
8946
8947
8948
8949
TEST_CASE(variable_batch_user_input_test5)
{
    // Error using default_dim_value and default_dyn_dim_value
    migraphx::onnx_options options;
    options.default_dim_value     = 2;
8950
    options.default_dyn_dim_value = {1, 2};
8951
8952
8953
8954
8955
8956
8957
8958

    EXPECT(test::throws([&] { migraphx::parse_onnx("variable_batch_test.onnx", options); }));
}

TEST_CASE(variable_batch_user_input_test6)
{
    // Error using both map_dyn_input_dims and map_input_dims
    migraphx::onnx_options options;
8959
    options.map_dyn_input_dims["0"] = {{2, 5}, {3, 3}, {16, 16}, {16, 16}};
8960
8961
8962
8963
8964
    options.map_input_dims["0"]     = {2, 3, 16, 16};

    EXPECT(test::throws([&] { migraphx::parse_onnx("variable_batch_test.onnx", options); }));
}

8965
8966
TEST_CASE(variable_batch_user_input_test7)
{
8967
8968
    // if entry in map_dyn_input_dims is all fixed dynamic_dimensions, convert it to a static
    // shape
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 16, 16}});
    auto r   = mm->add_instruction(migraphx::make_op("identity"), l0);
    mm->add_return({r});

    migraphx::onnx_options options;
    options.map_dyn_input_dims["0"] = {{2, 2, {2}}, {3, 3}, {16, 16}, {16, 16}};

    auto prog = migraphx::parse_onnx("variable_batch_test.onnx", options);

    EXPECT(p == prog);
}

8983
8984
8985
TEST_CASE(variable_batch_leq_zero_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8986
8987
8988
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
8989
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
8990
    auto prog = optimize_onnx("variable_batch_leq_zero_test.onnx");
8991
8992
8993
8994

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8995
8996
8997
TEST_CASE(where_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8998
8999
9000
9001
9002
    auto* mm = p.get_main_module();
    auto lc  = mm->add_parameter("c", migraphx::shape{migraphx::shape::bool_type, {2}});
    auto lx  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2, 2}});
    auto ly  = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {2, 1, 2, 2}});

turneram's avatar
turneram committed
9003
9004
    auto lccm =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 2, 2}}}), lc);
9005
9006
9007
9008
    auto lxm =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 2, 2}}}), lx);
    auto lym =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 2, 2}}}), ly);
9009

turneram's avatar
turneram committed
9010
    auto r = mm->add_instruction(migraphx::make_op("where"), lccm, lxm, lym);
Shucai Xiao's avatar
Shucai Xiao committed
9011
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
9012
9013
9014
9015
9016
9017

    auto prog = migraphx::parse_onnx("where_test.onnx");

    EXPECT(p == prog);
}

9018
9019
9020
9021
9022
9023
9024
TEST_CASE(where_dyn_test)
{
    // TODO: broadcasting for dynamic shapes isn't implemented at time of writing.
    // Update this test case to use shapes that require broadcasting, when available.
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto lc  = mm->add_parameter(
9025
        "c", migraphx::shape{migraphx::shape::bool_type, {{1, 4}, {2, 2}, {2, 2}}});
9026
    auto lx = mm->add_parameter(
9027
        "x", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}});
9028
    auto ly = mm->add_parameter(
9029
        "y", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}});
9030
9031
9032
9033
9034

    auto r = mm->add_instruction(migraphx::make_op("where"), lc, lx, ly);
    mm->add_return({r});

    migraphx::onnx_options options;
9035
    options.default_dyn_dim_value = {1, 4};
9036
9037
9038
9039
9040
9041
9042
9043
9044
    auto prog                     = parse_onnx("where_dyn_test.onnx", options);

    EXPECT(p == prog);
}

TEST_CASE(where_mixed_test)
{
    //  mixture of static and dynamic input shapes is not supported
    migraphx::onnx_options options;
9045
    options.default_dyn_dim_value = {1, 4};
9046
9047
9048
    EXPECT(test::throws([&] { migraphx::parse_onnx("where_mixed_test.onnx", options); }));
}

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