onnx_test.cpp 341 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);
}

Umang Yadav's avatar
Umang Yadav committed
1868
1869
1870
1871
1872
1873
1874
1875
1876
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);
1877
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 8, 5, 5}}}), tmp2);
Umang Yadav's avatar
Umang Yadav committed
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
    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);
1891
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 8, 2, 3}}}), tmp2);
Umang Yadav's avatar
Umang Yadav committed
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
    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"); }));
}

1906
1907
1908
1909
1910
1911
1912
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 =
1913
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
1914
    auto dequant = mm->add_instruction(
1915
        migraphx::make_op("convert",
turneram's avatar
turneram committed
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
                          {{"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 =
1932
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
1933
    auto l2_mbcast =
1934
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l2);
turneram's avatar
turneram committed
1935
    l2_mbcast = mm->add_instruction(
1936
        migraphx::make_op("convert",
turneram's avatar
turneram committed
1937
1938
1939
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l2_mbcast);
    l0 = mm->add_instruction(
1940
1941
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
turneram's avatar
turneram committed
1942
        l0);
1943

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

turneram's avatar
turneram committed
1947
    auto prog = optimize_onnx("dequantizelinear_zero_point_test.onnx", true);
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
    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(
1961
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l1);
1962
    auto l2_bcast = mm->add_instruction(
1963
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l2);
1964
1965
    l2_bcast = mm->add_instruction(
        migraphx::make_op("convert",
turneram's avatar
turneram committed
1966
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
1967
1968
1969
1970
        l2_bcast);
    l0 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
turneram's avatar
turneram committed
1971
1972
        l0);
    auto sub = mm->add_instruction(migraphx::make_op("sub"), l0, l2_bcast);
1973

turneram's avatar
turneram committed
1974
    mm->add_instruction(migraphx::make_op("mul"), sub, l1_bcast);
1975
1976
1977
1978
1979
1980
1981
    return p;
}

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

turneram's avatar
turneram committed
1982
    auto prog = optimize_onnx("dequantizelinear_axis_test.onnx", true);
1983
1984
1985
1986
1987
1988
1989
    EXPECT(p.sort() == prog.sort());
}

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

turneram's avatar
turneram committed
1990
    auto prog = optimize_onnx("dequantizelinear_neg_axis_test.onnx", true);
1991
1992
1993
    EXPECT(p.sort() == prog.sort());
}

Khalique's avatar
Khalique committed
1994
TEST_CASE(dropout_test)
Shucai Xiao's avatar
Shucai Xiao committed
1995
1996
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
1997
1998
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 2, 2}});
1999
    auto out   = mm->add_instruction(migraphx::make_op("identity"), input);
2000
2001
    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
2002
2003
    mm->add_literal(migraphx::literal(s, vec));
    mm->add_return({out});
Shucai Xiao's avatar
Shucai Xiao committed
2004

2005
    auto prog = migraphx::parse_onnx("dropout_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
2006
2007
2008
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2009
2010
2011
TEST_CASE(elu_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2012
2013
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3}});
2014
    mm->add_instruction(migraphx::make_op("elu", {{"alpha", 0.01}}), input);
Khalique's avatar
Khalique committed
2015

Shucai Xiao's avatar
Shucai Xiao committed
2016
    auto prog = optimize_onnx("elu_test.onnx");
Khalique's avatar
Khalique committed
2017
2018
2019
2020

    EXPECT(p == prog);
}

2021
2022
2023
TEST_CASE(embedding_bag_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2024
2025
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("weight", migraphx::shape{migraphx::shape::float_type, {4, 2}});
2026
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {3}}, {1, 0, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
2027
2028
    auto l1 = mm->add_literal(l);
    mm->add_literal(0);
2029
2030
2031
2032
2033
2034
    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
2035
    mm->add_return({r1, r2, r3});
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046

    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"); }));
}

2047
2048
2049
TEST_CASE(equal_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2050
    auto* mm = p.get_main_module();
2051
2052
2053
    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
2054
2055
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
2056
2057
2058
    auto eq     = mm->add_instruction(migraphx::make_op("equal"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
2059
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2060
        eq);
Shucai Xiao's avatar
Shucai Xiao committed
2061
    mm->add_return({ret});
2062
2063
2064
2065
2066
2067
2068
2069
2070

    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
2071
    auto* mm = p.get_main_module();
2072
2073
2074
    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
2075
2076
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
2077
2078
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
2079
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2080
2081
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("equal"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
2082
    mm->add_return({ret});
2083
2084
2085
2086
2087
2088

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2089
TEST_CASE(erf_test)
2090
2091
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2092
2093
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
2094
    mm->add_instruction(migraphx::make_op("erf"), input);
Khalique's avatar
Khalique committed
2095

Shucai Xiao's avatar
Shucai Xiao committed
2096
    auto prog = optimize_onnx("erf_test.onnx");
Khalique's avatar
Khalique committed
2097
2098
2099
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2100
TEST_CASE(exp_test)
2101
2102
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2103
2104
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
2105
    mm->add_instruction(migraphx::make_op("exp"), input);
Khalique's avatar
Khalique committed
2106

Shucai Xiao's avatar
Shucai Xiao committed
2107
    auto prog = optimize_onnx("exp_test.onnx");
Khalique's avatar
Khalique committed
2108
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
2109
2110
}

Khalique's avatar
Khalique committed
2111
TEST_CASE(expand_test)
2112
2113
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2114
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
2115
    migraphx::shape s(migraphx::shape::float_type, {3, 1, 1});
Shucai Xiao's avatar
Shucai Xiao committed
2116
    auto param = mm->add_parameter("x", s);
Khalique's avatar
Khalique committed
2117
    migraphx::shape ss(migraphx::shape::int32_type, {4});
Shucai Xiao's avatar
Shucai Xiao committed
2118
    mm->add_literal(migraphx::literal(ss, {2, 3, 4, 5}));
2119
    mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), param);
Khalique's avatar
Khalique committed
2120

Shucai Xiao's avatar
Shucai Xiao committed
2121
    auto prog = optimize_onnx("expand_test.onnx");
Khalique's avatar
Khalique committed
2122
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
2123
2124
}

2125
2126
2127
migraphx::program create_external_data_prog()
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2128
    auto* mm = p.get_main_module();
2129
2130
2131
2132
    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
2133
    auto bias = mm->add_literal(migraphx::literal({migraphx::shape::float_type, {10}}, bias_data));
kahmed10's avatar
kahmed10 committed
2134
2135
2136
2137
    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
2138
    auto bias_bcast = mm->add_instruction(
2139
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 10, 214, 214}}}), bias);
Shucai Xiao's avatar
Shucai Xiao committed
2140
    mm->add_instruction(migraphx::make_op("add"), conv, bias_bcast);
2141
2142
2143
    return p;
}

2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
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);
}

2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
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
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
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
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
2285
TEST_CASE(flatten_test)
2286
2287
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2288
2289
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
2290
2291
    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
2292
    auto prog = optimize_onnx("flatten_test.onnx");
2293
2294
2295
2296

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2297
2298
2299
2300
2301
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}});
2302
2303
2304
    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
2305
2306
2307
2308
2309
2310
2311
2312
    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
2313
2314
2315
2316
2317
TEST_CASE(flatten_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2318
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
Charlie Lin's avatar
Charlie Lin committed
2319
2320
2321
2322
2323
    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;
2324
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2325
2326
2327
2328
    auto prog                     = parse_onnx("flatten_dyn_test.onnx", options);
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2329
2330
2331
TEST_CASE(floor_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2332
2333
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
2334
    mm->add_instruction(migraphx::make_op("floor"), input);
Shucai Xiao's avatar
Shucai Xiao committed
2335

Shucai Xiao's avatar
Shucai Xiao committed
2336
    auto prog = optimize_onnx("floor_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
2337
2338
2339
2340

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2341
TEST_CASE(gather_test)
2342
2343
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2344
2345
2346
    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
2347
    int axis = 1;
2348
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
2349
    auto prog = optimize_onnx("gather_test.onnx");
2350
2351
2352
2353

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
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(
2374
        "data", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {5, 5}, {6, 6}}});
Brian Pickrell's avatar
Brian Pickrell committed
2375
    auto l1 = mm->add_parameter(
2376
        "indices", migraphx::shape{migraphx::shape::int32_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}});
Brian Pickrell's avatar
Brian Pickrell committed
2377
2378
2379
2380
2381
2382
2383
2384
2385
    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;
2386
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
2387
2388
2389
2390
2391
    auto prog                     = parse_onnx("gather_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2392
2393
2394
TEST_CASE(gather_elements_axis0_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2395
2396
2397
    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
2398
2399
2400
2401
    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
2402
        mm->add_literal(migraphx::literal{ind_s, ind_indices.begin(), ind_indices.end()});
Shucai Xiao's avatar
Shucai Xiao committed
2403
    auto l_ind_axis_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2404
2405
        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
2406

2407
2408
    auto rsp_data    = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), data);
    auto lbst_stride = mm->add_instruction(
2409
        migraphx::make_op("multibroadcast", {{"out_lens", ind_s.lens()}}), l_stride);
2410
2411
2412
2413
    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
2414
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
2415
2416
2417
2418
2419
2420
2421
2422
2423

    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
2424
2425
2426
    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
2427
2428
2429
2430
    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
2431
        mm->add_literal(migraphx::literal{ind_s, ind_indices.begin(), ind_indices.end()});
Shucai Xiao's avatar
Shucai Xiao committed
2432
    auto l_ind_axis_indices =
Shucai Xiao's avatar
Shucai Xiao committed
2433
2434
        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
2435

2436
2437
    auto rsp_data    = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {12}}}), data);
    auto lbst_stride = mm->add_instruction(
2438
        migraphx::make_op("multibroadcast", {{"out_lens", ind_s.lens()}}), l_stride);
2439
2440
2441
2442
    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
2443
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
2444
2445
2446
2447
2448
2449

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

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
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
2462
2463
2464
2465
TEST_CASE(gathernd_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
2466
2467
2468
    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
2469
                                migraphx::shape{migraphx::shape::int64_type, {{1, 3}, {2, 2}}});
2470
    auto r  = mm->add_instruction(migraphx::make_op("gathernd"), l0, l1);
Brian Pickrell's avatar
Brian Pickrell committed
2471
2472
2473
    mm->add_return({r});

    migraphx::onnx_options options;
2474
    options.map_dyn_input_dims["data"]    = {{2, 4, {2}}, {2, 4}};
Brian Pickrell's avatar
Brian Pickrell committed
2475
2476
2477
2478
2479
    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
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
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
2493
TEST_CASE(gemm_test)
2494
2495
{
    migraphx::program p;
2496
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2497
2498
2499
2500
2501
    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;
2502
2503
    auto a_l   = mm->add_literal(alpha);
    auto t_a   = add_common_op(*mm, migraphx::make_op("mul"), {a_l, l0});
2504
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
Charlie Lin's avatar
Charlie Lin committed
2505
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
2506
2507
    auto b_l = mm->add_literal(beta);
    auto b_b = mm->add_instruction(
Charlie Lin's avatar
Charlie Lin committed
2508
2509
2510
        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
2511

Shucai Xiao's avatar
Shucai Xiao committed
2512
    auto prog = optimize_onnx("gemm_test.onnx");
2513
2514
2515
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2516
TEST_CASE(gemm_no_C_test)
2517
2518
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2519
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2520
2521
2522
2523
2524
    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;
2525
2526
    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
2527
2528
2529
    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);
2530
    auto b_l = mm->add_literal(beta);
Charlie Lin's avatar
Charlie Lin committed
2531
2532
    auto l2_b =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {7, 11}}}), l2);
2533
    auto b_b = mm->add_instruction(
Charlie Lin's avatar
Charlie Lin committed
2534
2535
2536
        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);
2537

Charlie Lin's avatar
Charlie Lin committed
2538
    auto prog = optimize_onnx("gemm_no_C_test.onnx");
2539
2540
2541
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2542
TEST_CASE(gemm_brcst_C_test)
2543
2544
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2545
    auto* mm = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2546
2547
2548
2549
    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
2550
2551
    auto alpha = 0.5f;
    auto beta  = 0.8f;
2552
2553
    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
2554
    t_a      = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
2555
    auto dot = migraphx::add_apply_alpha_beta(*mm, {t_a, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
2556
2557
    auto b_l = mm->add_literal(beta);
    auto l2_b =
2558
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", out_lens}}), l2);
2559
    auto b_b = mm->add_instruction(
2560
        migraphx::make_op("multibroadcast", {{"out_lens", l2_b->get_shape().lens()}}), b_l);
2561
    auto l2_bb = mm->add_instruction(migraphx::make_op("mul"), l2_b, b_b);
turneram's avatar
turneram committed
2562
    mm->add_instruction(migraphx::make_op("add"), dot, l2_bb);
2563

Charlie Lin's avatar
Charlie Lin committed
2564
    auto prog = optimize_onnx("gemm_brcst_C_test.onnx");
2565
2566
2567
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
2568
2569
2570
2571
TEST_CASE(gemm_half_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
Charlie Lin's avatar
Charlie Lin committed
2572
2573
2574
    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
2575
2576
2577
2578
2579
2580
    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
2581
2582
    t_a = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), t_a);
    std::vector<std::size_t> lens = {6, 7};
2583
2584
2585
    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
2586
        migraphx::make_op("convert", {{"target_type", migraphx::shape::float_type}}), l2);
2587
2588
    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
2589
2590
2591
    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
2592
    mm->add_instruction(migraphx::make_op("add"), dot, l2_b);
Shucai Xiao's avatar
Shucai Xiao committed
2593
2594
2595
2596
2597

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

Charlie Lin's avatar
Charlie Lin committed
2598
2599
2600
2601
2602
TEST_CASE(gemm_dyn_inner_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
2603
        "A", migraphx::shape{migraphx::shape::float_type, {{1, 10, {8}}, {6, 6}}});
Charlie Lin's avatar
Charlie Lin committed
2604
    auto l1 = mm->add_parameter(
2605
        "B", migraphx::shape{migraphx::shape::float_type, {{1, 10, {8}}, {7, 7}}});
Charlie Lin's avatar
Charlie Lin committed
2606
2607
2608
2609
2610
2611
2612
2613
    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;
2614
    options.default_dyn_dim_value = {1, 10, {8}};
Charlie Lin's avatar
Charlie Lin committed
2615
2616
2617
2618
2619
2620
2621
2622
2623
    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(
2624
        "A", migraphx::shape{migraphx::shape::float_type, {{5, 5}, {5, 10, {7}}}});
Charlie Lin's avatar
Charlie Lin committed
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
    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;
2635
    options.default_dyn_dim_value = {5, 10, {7}};
Charlie Lin's avatar
Charlie Lin committed
2636
2637
2638
2639
    auto prog                     = migraphx::parse_onnx("gemm_dyn_outer_test.onnx", options);
    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2640
TEST_CASE(gemm_dyn_bias_test)
Charlie Lin's avatar
Charlie Lin committed
2641
{
Charlie Lin's avatar
Charlie Lin committed
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
    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
2654
    migraphx::onnx_options options;
Charlie Lin's avatar
Charlie Lin committed
2655
2656
2657
    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
2658
2659
2660
2661
2662
2663
2664
}

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

Khalique's avatar
Khalique committed
2665
TEST_CASE(globalavgpool_test)
2666
2667
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2668
2669
2670
    auto* mm = p.get_main_module();
    auto input =
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
2671
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::average};
Khalique's avatar
Khalique committed
2672
2673
    auto lens  = input->get_shape().lens();
    op.lengths = {lens[2], lens[3]};
kahmed10's avatar
kahmed10 committed
2674
    op.padding = {0, 0, 0, 0};
Shucai Xiao's avatar
Shucai Xiao committed
2675
    mm->add_instruction(op, input);
Khalique's avatar
Khalique committed
2676

Shucai Xiao's avatar
Shucai Xiao committed
2677
    auto prog = optimize_onnx("globalavgpool_test.onnx");
2678
2679
2680
2681

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2682
2683
2684
TEST_CASE(globalavgpool_dyn_test)
{
    migraphx::program p;
2685
2686
2687
    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
2688
2689
2690
2691
2692
2693
2694
2695
    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;
2696
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2697
2698
2699
2700
2701
    auto prog                     = parse_onnx("globalavgpool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
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
2719
2720
2721
TEST_CASE(globallppool_dyn_test)
{
    migraphx::program p;
2722
2723
2724
    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
2725
2726
2727
2728
2729
2730
2731
2732
2733
    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;
2734
    options.default_dyn_dim_value = {16, 32};
Charlie Lin's avatar
Charlie Lin committed
2735
2736
2737
2738
2739
    auto prog                     = migraphx::parse_onnx("globallppool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
2740
TEST_CASE(globalmaxpool_test)
Khalique's avatar
Khalique committed
2741
2742
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2743
2744
2745
    auto* mm = p.get_main_module();
    auto input =
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
2746
    auto op    = migraphx::op::pooling{migraphx::op::pooling_mode::max};
Khalique's avatar
Khalique committed
2747
2748
    auto lens  = input->get_shape().lens();
    op.lengths = {lens[2], lens[3]};
kahmed10's avatar
kahmed10 committed
2749
    op.padding = {0, 0, 0, 0};
Shucai Xiao's avatar
Shucai Xiao committed
2750
    mm->add_instruction(op, input);
Khalique's avatar
Khalique committed
2751

Shucai Xiao's avatar
Shucai Xiao committed
2752
    auto prog = optimize_onnx("globalmaxpool_test.onnx");
Khalique's avatar
Khalique committed
2753
2754
2755
2756

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
2757
2758
2759
TEST_CASE(globalmaxpool_dyn_test)
{
    migraphx::program p;
2760
2761
2762
    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
2763
2764
2765
2766
2767
2768
2769
2770
    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;
2771
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
2772
2773
2774
2775
2776
    auto prog                     = parse_onnx("globalmaxpool_dyn_test.onnx", options);

    EXPECT(p == prog);
}

2777
2778
2779
TEST_CASE(greater_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2780
    auto* mm = p.get_main_module();
2781
2782
2783
    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
2784
2785
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
2786
2787
2788
    auto gr     = mm->add_instruction(migraphx::make_op("greater"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
2789
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2790
        gr);
Shucai Xiao's avatar
Shucai Xiao committed
2791
    mm->add_return({ret});
2792
2793
2794
2795
2796
2797
2798
2799

    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
2800
    auto* mm = p.get_main_module();
2801
2802
2803
    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
2804
2805
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
2806
2807
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
2808
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
2809
2810
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("greater"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
2811
    mm->add_return({ret});
2812
2813
2814
2815
2816

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

turneram's avatar
turneram committed
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
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
2835
TEST_CASE(group_conv_test)
2836
2837
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
2838
2839
2840
    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
2841
2842
    migraphx::op::convolution op;
    op.group = 4;
Shucai Xiao's avatar
Shucai Xiao committed
2843
    mm->add_instruction(op, l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
2844
    auto prog = optimize_onnx("group_conv_test.onnx");
2845
2846
2847
2848

    EXPECT(p == prog);
}

2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
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
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}});

    auto x_reshaped =
        mm->add_instruction(migraphx::make_op("reshape", {{"dims", reshape_dims}}), x);
    auto mean =
        mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", reduce_axes}}), x_reshaped);
    auto x_sub_mean    = add_common_op(*mm, migraphx::make_op("sub"), {x_reshaped, mean});
    auto x_sqdiff_mean = add_common_op(*mm, migraphx::make_op("sqdiff"), {x_reshaped, 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});
    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
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
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
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);
}

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
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
3122
3123
3124
3125
3126
3127
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}};
3128

Shucai Xiao's avatar
Shucai Xiao committed
3129
    std::vector<float> ones(s.elements(), 1.0f);
3130
3131
3132
3133
3134
3135
3136
    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
3137
3138
3139
3140

    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
3141

Shucai Xiao's avatar
Shucai Xiao committed
3142
3143
3144
    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
3145

Shucai Xiao's avatar
Shucai Xiao committed
3146
3147
    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
3148
3149
    mm->add_return({r});

3150
3151
3152
3153
3154
3155
3156
3157
    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
3158

3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
    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
3176
3177
3178
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
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
3191
    then_mod->add_literal({});
Shucai Xiao's avatar
Shucai Xiao committed
3192
3193
3194
3195
3196
    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
3197
    else_mod->add_literal({});
Shucai Xiao's avatar
Shucai Xiao committed
3198
3199
3200
    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
3201
3202
    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
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240

    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
3241
3242
    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
3243
3244
3245
3246
3247

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

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
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
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
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
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
3339
3340
3341
    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
3342
3343
3344
3345
3346

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

Shucai Xiao's avatar
Shucai Xiao committed
3347
3348
3349
3350
3351
3352
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}};
3353

Shucai Xiao's avatar
Shucai Xiao committed
3354
    std::vector<float> ones(s.elements(), 1.0f);
3355
3356
3357
3358
3359
3360
3361
    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
3362

Shucai Xiao's avatar
Shucai Xiao committed
3363
3364
3365
3366
3367
3368
3369
    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
3370

Shucai Xiao's avatar
Shucai Xiao committed
3371
3372
    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
3373
3374
3375
    mm->add_return({r});

    auto prog = migraphx::parse_onnx("if_then_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
3376
3377
3378
    EXPECT(p == prog);
}

3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
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
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
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");
3421
3422
    auto m1 =
        then_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
3423
    auto add0 = then_mod->add_instruction(migraphx::make_op("add"), x, m1);
3424
3425
    auto m2 =
        then_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l2);
Shucai Xiao's avatar
Shucai Xiao committed
3426
3427
3428
3429
    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");
3430
3431
    auto me1 =
        else_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 4}}}), l3);
Shucai Xiao's avatar
Shucai Xiao committed
3432
    auto mul1 = else_mod->add_instruction(migraphx::make_op("mul"), x, me1);
3433
3434
    auto me2 =
        else_mod->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4}}}), l3);
Shucai Xiao's avatar
Shucai Xiao committed
3435
3436
3437
3438
3439
3440
3441
    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
3442

Shucai Xiao's avatar
Shucai Xiao committed
3443
    auto prog = migraphx::parse_onnx("if_tuple_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
3444
3445
3446
    EXPECT(p == prog);
}

3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
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
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
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
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
3549
TEST_CASE(imagescaler_test)
3550
3551
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3552
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
3553
    migraphx::shape s{migraphx::shape::float_type, {1, 3, 16, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
3554
3555
3556
    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
3557
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {0.01, 0.02, 0.03}});
3558
3559
3560
3561
    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(
3562
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals);
3563
    mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
Khalique's avatar
Khalique committed
3564

Shucai Xiao's avatar
Shucai Xiao committed
3565
    auto prog = optimize_onnx("imagescaler_test.onnx");
3566
3567
3568
3569

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
3570
3571
3572
TEST_CASE(imagescaler_half_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3573
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
3574
    migraphx::shape s{migraphx::shape::half_type, {1, 3, 16, 16}};
Shucai Xiao's avatar
Shucai Xiao committed
3575
    auto l0 = mm->add_parameter("0", s);
Shucai Xiao's avatar
Shucai Xiao committed
3576
    auto scale_val =
Shucai Xiao's avatar
Shucai Xiao committed
3577
3578
        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
3579
        migraphx::literal{migraphx::shape{migraphx::shape::half_type, {3}}, {0.01, 0.02, 0.03}});
3580
3581
3582
3583
    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(
3584
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", s.lens()}}), bias_vals);
3585
    mm->add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
Shucai Xiao's avatar
Shucai Xiao committed
3586
3587
3588
3589
3590
3591

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3592
TEST_CASE(implicit_add_bcast_test)
3593
3594
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3595
3596
3597
    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}});
3598
3599
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3600
    mm->add_instruction(migraphx::make_op("add"), l0, l3);
Khalique's avatar
Khalique committed
3601

Shucai Xiao's avatar
Shucai Xiao committed
3602
    auto prog = optimize_onnx("implicit_add_bcast_test.onnx");
3603
3604
3605
3606

    EXPECT(p == prog);
}

3607
3608
3609
TEST_CASE(implicit_add_bcast_user_input_shape_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3610
3611
3612
    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}});
3613
3614
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 4, 5, 6}}}), l1);
3615
    auto r = mm->add_instruction(migraphx::make_op("add"), l0, l3);
Shucai Xiao's avatar
Shucai Xiao committed
3616
    mm->add_return({r});
3617
3618
3619
3620
3621
3622
3623
3624
3625

    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
3626
TEST_CASE(implicit_pow_bcast_test)
3627
3628
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3629
3630
3631
    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}});
3632
3633
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3634
    mm->add_instruction(migraphx::make_op("pow"), l0, l3);
Khalique's avatar
Khalique committed
3635

Shucai Xiao's avatar
Shucai Xiao committed
3636
    auto prog = optimize_onnx("implicit_pow_bcast_test.onnx");
3637
3638
3639
3640

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
3641
TEST_CASE(implicit_sub_bcast_test)
3642
3643
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3644
3645
3646
    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}});
3647
3648
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
3649
    mm->add_instruction(migraphx::make_op("sub"), l0, l3);
Khalique's avatar
Khalique committed
3650

Shucai Xiao's avatar
Shucai Xiao committed
3651
    auto prog = optimize_onnx("implicit_sub_bcast_test.onnx");
3652
3653
3654
3655

    EXPECT(p == prog);
}

3656
3657
3658
TEST_CASE(initializer_not_an_input)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3659
    auto* mm             = p.get_main_module();
3660
    std::vector<float> w = {1, 2, 3, 4, 5, 6, 7, 8};
Shucai Xiao's avatar
Shucai Xiao committed
3661
3662
    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}});
3663
    migraphx::add_apply_alpha_beta(*mm, {l0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
3664
    auto prog = optimize_onnx("initializer_not_an_input.onnx");
kahmed10's avatar
kahmed10 committed
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675

    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
3676
3677
3678
3679
3680
    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);

3681
    auto mean = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), x);
3682
3683
3684
3685
3686
    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);

3687
3688
    auto epsilon_literal =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
3689
3690
3691
3692
3693
    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});

3694
3695
3696
3697
    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);
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
    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
3727

3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
    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);
3740
3741
3742
3743

    EXPECT(p == prog);
}

3744
3745
3746
3747
3748
3749
3750
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;
3751
3752
3753
3754
3755
    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);

3756
    // conversion of half type to float is enabled by default
3757
3758
3759
3760
3761
3762
    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);
3763
3764

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

3768
    auto variance = mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2, 3}}}), l1);
3769
3770
    // type of epsilon_literal is same as 0'th input; convert instruction will be added by
    // add_common_op
3771
    auto epsilon_literal =
3772
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {1e-5}});
3773
3774
3775
3776
3777
    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});

3778
3779
3780
3781
    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);
3782
3783
3784
3785
    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);
3786
3787
3788
3789
3790
    auto prog = optimize_onnx("instance_norm_half_test.onnx");

    EXPECT(p == prog);
}

3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
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);
}

3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
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
3855
TEST_CASE(leaky_relu_test)
3856
3857
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3858
    auto* mm    = p.get_main_module();
Khalique's avatar
Khalique committed
3859
    float alpha = 0.01f;
Shucai Xiao's avatar
Shucai Xiao committed
3860
    auto l0     = mm->add_parameter("0", {migraphx::shape::float_type, {3}});
3861
    mm->add_instruction(migraphx::make_op("leaky_relu", {{"alpha", alpha}}), l0);
Khalique's avatar
Khalique committed
3862

Shucai Xiao's avatar
Shucai Xiao committed
3863
    auto prog = optimize_onnx("leaky_relu_test.onnx");
3864
3865
3866
3867

    EXPECT(p == prog);
}

3868
3869
3870
TEST_CASE(less_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
3871
    auto* mm = p.get_main_module();
3872
3873
3874
    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
3875
3876
    auto input1 = mm->add_literal(migraphx::literal(s, data));
    auto input2 = mm->add_parameter("x2", migraphx::shape{migraphx::shape::float_type, {2, 3}});
3877
3878
3879
    auto le     = mm->add_instruction(migraphx::make_op("less"), input1, input2);
    auto ret    = mm->add_instruction(
        migraphx::make_op("convert",
3880
                             {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
3881
        le);
Shucai Xiao's avatar
Shucai Xiao committed
3882
    mm->add_return({ret});
3883
3884
3885
3886
3887
3888
3889
3890

    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
3891
    auto* mm = p.get_main_module();
3892
3893
3894
    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
3895
3896
    auto input1 = mm->add_parameter("x1", sf);
    auto input2 = mm->add_parameter("x2", sb);
3897
3898
    auto cin1   = mm->add_instruction(
        migraphx::make_op("convert",
3899
                            {{"target_type", migraphx::to_value(migraphx::shape::bool_type)}}),
3900
3901
        input1);
    auto ret = mm->add_instruction(migraphx::make_op("less"), cin1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
3902
    mm->add_return({ret});
3903
3904
3905
3906
3907

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

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
3908
3909
3910
3911
3912
3913
3914
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
3915
    auto temp   = mm->add_instruction(migraphx::make_op("greater"), input1, input2);
Shucai Xiao's avatar
Shucai Xiao committed
3916
3917
3918
    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
3919

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
3920
3921
3922
3923
3924
3925
    mm->add_return({le});

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

3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
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
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
4069
TEST_CASE(log_test)
4070
4071
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4072
4073
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
4074
    mm->add_instruction(migraphx::make_op("log"), input);
4075

Shucai Xiao's avatar
Shucai Xiao committed
4076
    auto prog = optimize_onnx("log_test.onnx");
4077
4078
4079
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
4080
4081
4082
4083
4084
4085
4086
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(
4087
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
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
4113
4114
4115
4116
    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(
4117
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
Shucai Xiao's avatar
Shucai Xiao committed
4118
4119
4120
4121
4122
4123
4124
4125
    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
4126
TEST_CASE(logsoftmax_test)
4127
4128
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4129
4130
    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
4131
    int axis = 1;
4132
    mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", axis}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
4133
    auto prog = optimize_onnx("logsoftmax_test.onnx");
4134
4135
4136
4137

    EXPECT(p == prog);
}

4138
4139
4140
4141
4142
4143
4144
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
4145
    auto l2 = mm->add_instruction(migraphx::make_op("logsoftmax", {{"axis", -1}}), l1);
4146
4147
4148
4149
4150
4151
4152
    mm->add_return({l2});

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

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
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
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
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
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"); }));
}

4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
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}},
4279
                                           {"dilations", {1}},
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
                                           {"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}},
4296
                                           {"dilations", {1}},
4297
4298
4299
4300
4301
4302
                                           {"lp_order", 2}}),
                        l0);
    auto prog = optimize_onnx("lppool_l2_test.onnx");
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4303
TEST_CASE(lrn_test)
4304
4305
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4306
4307
    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
4308
4309
4310
4311
4312
    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
4313
    mm->add_instruction(op, l0);
Shucai Xiao's avatar
Shucai Xiao committed
4314
    auto prog = optimize_onnx("lrn_test.onnx");
4315
4316
4317
4318

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4319
TEST_CASE(matmul_bmbm_test)
4320
4321
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4322
4323
4324
    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}});
4325
    auto bl0 = mm->add_instruction(
4326
        migraphx::make_op("multibroadcast", {{"out_lens", {5, 2, 3, 6, 7}}}), l0);
4327
    auto bl1 = mm->add_instruction(
4328
        migraphx::make_op("multibroadcast", {{"out_lens", {5, 2, 3, 7, 8}}}), l1);
4329
    migraphx::add_apply_alpha_beta(*mm, {bl0, bl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
Shucai Xiao's avatar
Shucai Xiao committed
4330
    auto prog = optimize_onnx("matmul_bmbm_test.onnx");
4331
4332
4333
4334

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4335
TEST_CASE(matmul_bmv_test)
4336
{
Khalique's avatar
Khalique committed
4337
    migraphx::program p;
4338
4339
4340
4341
4342
    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 =
4343
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {3, 7, 1}}}), sl1);
4344
    auto res =
4345
        migraphx::add_apply_alpha_beta(*mm, {l0, bsl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4346
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), res);
4347

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

Khalique's avatar
Khalique committed
4350
    EXPECT(p == prog);
4351
4352
}

Khalique's avatar
Khalique committed
4353
TEST_CASE(matmul_mv_test)
4354
4355
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4356
4357
4358
    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}});
4359
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
4360
    auto res = migraphx::add_apply_alpha_beta(*mm, {l0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4361
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), res);
Khalique's avatar
Khalique committed
4362

Shucai Xiao's avatar
Shucai Xiao committed
4363
    auto prog = optimize_onnx("matmul_mv_test.onnx");
4364
4365
4366
4367

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4368
TEST_CASE(matmul_vbm_test)
4369
4370
{
    migraphx::program p;
4371
4372
4373
4374
4375
    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 =
4376
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5, 1, 7}}}), sl0);
4377
    auto res =
4378
        migraphx::add_apply_alpha_beta(*mm, {bsl0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4379
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1}}}), res);
Khalique's avatar
Khalique committed
4380

Shucai Xiao's avatar
Shucai Xiao committed
4381
    auto prog = optimize_onnx("matmul_vbm_test.onnx");
4382
4383
4384
4385

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4386
TEST_CASE(matmul_vm_test)
4387
4388
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4389
4390
4391
    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}});
4392
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
4393
    auto res = migraphx::add_apply_alpha_beta(*mm, {sl0, l1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4394
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
Khalique's avatar
Khalique committed
4395

Shucai Xiao's avatar
Shucai Xiao committed
4396
    auto prog = optimize_onnx("matmul_vm_test.onnx");
4397
4398
4399
4400

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4401
TEST_CASE(matmul_vv_test)
4402
4403
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4404
4405
4406
    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}});
4407
4408
    auto sl0 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {0}}}), l0);
    auto sl1 = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l1);
4409
    auto res =
4410
        migraphx::add_apply_alpha_beta(*mm, {sl0, sl1}, migraphx::make_op("dot"), 1.0f, 0.0f);
4411
4412
    auto sr0 = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), res);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0}}}), sr0);
4413

Shucai Xiao's avatar
Shucai Xiao committed
4414
    auto prog = optimize_onnx("matmul_vv_test.onnx");
4415
4416
4417
4418

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
4419
4420
4421
4422
TEST_CASE(matmul_dyn_mm_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
4423
4424
4425
4426
    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
4427
4428
4429
4430
    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;
4431
4432
    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
4433
4434
4435
4436
4437
4438
4439
4440
4441
    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();
4442
4443
    auto l0 =
        mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {{4, 8, {6}}, {7, 7}}});
Charlie Lin's avatar
Charlie Lin committed
4444
4445
4446
4447
4448
4449
4450
    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;
4451
    options.map_dyn_input_dims["1"] = {{4, 8, {6}}, {7, 7}};
Charlie Lin's avatar
Charlie Lin committed
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
    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(
4463
        "2", migraphx::shape{migraphx::shape::float_type, {{7, 7}, {4, 10, {8}}}});
Charlie Lin's avatar
Charlie Lin committed
4464
4465
4466
4467
4468
4469
    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;
4470
    options.map_dyn_input_dims["2"] = {{7, 7}, {4, 10, {8}}};
Charlie Lin's avatar
Charlie Lin committed
4471
4472
4473
4474
4475
4476
4477
4478
4479
    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();
4480
    migraphx::shape::dynamic_dimension dd{5, 8, {7}};
Charlie Lin's avatar
Charlie Lin committed
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
    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;
4501
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
4502
4503
4504
    EXPECT(test::throws([&] { migraphx::parse_onnx("matmul_dyn_broadcast_error.onnx", options); }));
}

4505
4506
4507
TEST_CASE(matmulinteger_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4508
4509
4510
    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}});
4511
    mm->add_instruction(migraphx::make_op("quant_dot"), l0, l1);
4512
4513
4514
4515
4516
4517

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

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
4518
4519
4520
TEST_CASE(matmulinteger_dyn_error)
{
    migraphx::onnx_options options;
4521
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
4522
4523
4524
    EXPECT(test::throws([&] { migraphx::parse_onnx("matmulinteger_dyn_error.onnx", options); }));
}

Khalique's avatar
Khalique committed
4525
TEST_CASE(max_test)
4526
4527
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4528
4529
4530
4531
    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}});
4532
4533
    auto l0     = mm->add_instruction(migraphx::make_op("max"), input0, input1);
    mm->add_instruction(migraphx::make_op("max"), l0, input2);
4534

4535
4536
4537
    auto prog = optimize_onnx("max_test.onnx");

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
4538
}
4539

4540
4541
4542
TEST_CASE(maxpool_notset_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4543
4544
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
4545
4546
4547
4548
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 1, 1}},
                                           {"stride", {2, 2}},
4549
4550
                                           {"lengths", {6, 6}},
                                           {"dilations", {1, 1}}}),
4551
                        input);
4552
4553
4554
4555
4556
4557

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

    EXPECT(p == prog);
}

4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
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);
}

4576
4577
4578
TEST_CASE(maxpool_same_upper_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4579
4580
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1, 1, 5, 5}});
4581
4582
4583
4584
    mm->add_instruction(migraphx::make_op("pooling",
                                          {{"mode", migraphx::op::pooling_mode::max},
                                           {"padding", {0, 0, 1, 1}},
                                           {"stride", {1, 1}},
4585
4586
                                           {"lengths", {2, 2}},
                                           {"dilations", {1, 1}}}),
4587
                        input);
4588
4589
4590
4591
4592
4593

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

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
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);
}

4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
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);
}

4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
void mvn_n_rank_test(std::vector<int64_t> axes,
                     std::vector<size_t> input_shape,
                     const std::string& test_file)
{
    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});

    auto prog = optimize_onnx(test_file);

    EXPECT(p == prog);
}

TEST_CASE(mvn_default_axes_test)
{
    mvn_n_rank_test({0, 2, 3}, {2, 2, 2, 2}, "mvn_default_axes_test.onnx");
}

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"); }));
}

TEST_CASE(mvn_rank_2_test) { mvn_n_rank_test({1}, {2, 2}, "mvn_rank_2_test.onnx"); }

TEST_CASE(mvn_rank_3_test) { mvn_n_rank_test({0, 1}, {2, 2, 2}, "mvn_rank_3_test.onnx"); }

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
4722
4723
4724
TEST_CASE(min_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4725
4726
4727
4728
    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}});
4729
4730
    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
4731

4732
4733
4734
    auto prog = optimize_onnx("min_test.onnx");

    EXPECT(p == prog);
4735
4736
}

4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
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
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
4807
4808
4809
4810
TEST_CASE(multinomial_test)
{
    migraphx::program p;
    auto* mm           = p.get_main_module();
Brian Pickrell's avatar
Brian Pickrell committed
4811
4812
4813
4814
    size_t sample_size = 13;
    size_t batch_size  = 3;
    size_t categories  = 10;
    float seed         = 0;
turneram's avatar
turneram committed
4815

Brian Pickrell's avatar
Brian Pickrell committed
4816
4817
4818
4819
4820
    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
4821
4822
4823
4824
4825
    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
4826
4827
4828
4829
4830
    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));
    auto rand_dummy =
        mm->add_literal(migraphx::literal{migraphx::shape::float_type, {batch_size * sample_size}});
turneram's avatar
turneram committed
4831

Brian Pickrell's avatar
Brian Pickrell committed
4832
4833
    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
4834
4835
4836
4837
4838
    auto prog = optimize_onnx("multinomial_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
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
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
4945
4946
4947
4948
4949
TEST_CASE(multinomial_dtype_error_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("multinomial_dtype_error_test.onnx"); }));
}

4950
4951
TEST_CASE(multinomial_generated_seed_test)
{
Brian Pickrell's avatar
Brian Pickrell committed
4952
    // multinomial op. no longer generates its own randoms
4953
4954
4955
    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
4956
    EXPECT(p1 == p2);
4957
4958
}

turneram's avatar
turneram committed
4959
4960
4961
4962
4963
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
4964
4965
    float seed                    = 1.0;
    uint32_t batch_size           = 1;
turneram's avatar
turneram committed
4966
4967
4968
4969
    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
4970
4971

    auto cdf = add_common_op(*mm, migraphx::make_op("sub"), {input, maxes});
turneram's avatar
turneram committed
4972
4973
4974
4975
    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
4976
4977
4978
    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
4979

Brian Pickrell's avatar
Brian Pickrell committed
4980
4981
4982
4983
4984
    // static size
    auto rand_dummy =
        mm->add_literal(migraphx::literal{migraphx::shape::float_type, {batch_size * sample_size}});
    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
4985
4986
4987
4988
4989
    auto prog = optimize_onnx("multinomial_int64_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
4990
TEST_CASE(no_pad_test)
4991
4992
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
4993
4994
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
4995
    mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
4996
    auto prog = optimize_onnx("no_pad_test.onnx");
4997

Khalique's avatar
Khalique committed
4998
4999
5000
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5001
5002
5003
TEST_CASE(neg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5004
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5005
    migraphx::shape s{migraphx::shape::int64_type, {2, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
5006
    auto input = mm->add_parameter("0", s);
5007
    auto ret   = mm->add_instruction(migraphx::make_op("neg"), input);
Shucai Xiao's avatar
Shucai Xiao committed
5008
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
5009
5010
5011
5012
5013
5014

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

    EXPECT(p == prog);
}

5015
5016
5017
5018
TEST_CASE(neg_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5019
    migraphx::shape s{migraphx::shape::int64_type, {{1, 10}, {3, 3}}};
5020
5021
5022
5023
5024
    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;
5025
    options.default_dyn_dim_value = {1, 10};
5026
5027
5028
5029
    auto prog                     = migraphx::parse_onnx("neg_dynamic_test.onnx", options);
    EXPECT(p == prog);
}

5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
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
5050
        migraphx::make_op("nonmaxsuppression", {{"center_point_box", true}}), b, s, mo, iou, st);
5051
5052
5053
5054
5055
5056
    mm->add_return({ret});

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

Charlie Lin's avatar
Charlie Lin committed
5057
5058
5059
5060
TEST_CASE(nms_dynamic_batch_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
5061
    migraphx::shape sb{migraphx::shape::float_type, {{1, 10}, {6, 6}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5062
    auto b = mm->add_parameter("boxes", sb);
5063
    migraphx::shape ss{migraphx::shape::float_type, {{1, 10}, {1, 1}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
    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;
5082
    options.default_dyn_dim_value = {1, 10};
Charlie Lin's avatar
Charlie Lin committed
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
    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();
5093
    migraphx::shape sb{migraphx::shape::float_type, {{1, 1}, {6, 20}, {4, 4}}};
Charlie Lin's avatar
Charlie Lin committed
5094
    auto b = mm->add_parameter("boxes", sb);
5095
    migraphx::shape ss{migraphx::shape::float_type, {{1, 1}, {1, 1}, {6, 20}}};
Charlie Lin's avatar
Charlie Lin committed
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
    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;
5108
    options.default_dyn_dim_value = {6, 20};
Charlie Lin's avatar
Charlie Lin committed
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
    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);
5121
    migraphx::shape ss{migraphx::shape::float_type, {{1, 1}, {1, 10}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
    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;
5134
    options.default_dyn_dim_value = {1, 10};
Charlie Lin's avatar
Charlie Lin committed
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
    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
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
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
5184
5185
5186
TEST_CASE(nonzero_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5187
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5188
5189
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
    std::vector<float> data = {1, 0, 1, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5190
    mm->add_literal(migraphx::literal(s, data));
Shucai Xiao's avatar
Shucai Xiao committed
5191
5192
5193

    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
5194
5195
    auto r                       = mm->add_literal(migraphx::literal(si, indices));
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5196
5197
5198
5199
5200
5201
5202
5203

    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
5204
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5205
    migraphx::shape s{migraphx::shape::int16_type, {2, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
5206
    std::vector<int> data = {1, 1, 0, 1, 0, 1};
Shucai Xiao's avatar
Shucai Xiao committed
5207
    mm->add_literal(migraphx::literal(s, data.begin(), data.end()));
Shucai Xiao's avatar
Shucai Xiao committed
5208
5209
5210

    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
5211
5212
    auto r                       = mm->add_literal(migraphx::literal(si, indices));
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5213
5214
5215
5216
5217

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

5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
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
5244
5245
5246
TEST_CASE(onehot_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5247
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
5248
5249
    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
5250
5251
5252
    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
5253
5254
    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
5255
    auto l_dep      = mm->add_literal(migraphx::literal(s_dep, data_dep));
5256
    auto gather_out = mm->add_instruction(migraphx::make_op("gather", {{"axis", 0}}), l_dep, l_ind);
5257
5258
    auto tr_out  = mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {2, 0, 1}}}),
                                      gather_out);
5259
5260
5261
5262
5263
5264
    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(
5265
5266
5267
        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);
5268
5269
    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
5270
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
5271
5272

    auto prog = migraphx::parse_onnx("onehot_test.onnx");
kahmed10's avatar
kahmed10 committed
5273
5274
5275
5276

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
5277
5278
5279
TEST_CASE(pad_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5280
5281
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 2}});
5282
    mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
5283
    auto prog = optimize_onnx("pad_test.onnx");
5284
5285
5286
5287

    EXPECT(p == prog);
}

5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
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"); }));
}

5304
5305
5306
TEST_CASE(pad_3arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5307
5308
5309
5310
    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}});
5311
5312
    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
5313
    mm->add_return({r});
5314
5315
5316
5317
5318
5319

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

    EXPECT(p == prog);
}

5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
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
5365
5366
5367
5368
5369
TEST_CASE(pad_attr_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto x   = mm->add_parameter(
5370
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4, {2}}}});
Charlie Lin's avatar
Charlie Lin committed
5371
5372
5373
5374
    auto ret = mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), x);
    mm->add_return({ret});

    migraphx::onnx_options options;
5375
    options.map_dyn_input_dims["0"] = {{2, 4, {2}}, {2, 4, {2}}};
Charlie Lin's avatar
Charlie Lin committed
5376
5377
5378
5379
5380
5381
5382
5383
5384
    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(
5385
        "0", migraphx::shape{migraphx::shape::float_type, {{2, 4, {2}}, {2, 4, {2}}}});
Charlie Lin's avatar
Charlie Lin committed
5386
5387
5388
5389
5390
    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;
5391
    options.map_dyn_input_dims["0"] = {{2, 4, {2}}, {2, 4, {2}}};
Charlie Lin's avatar
Charlie Lin committed
5392
5393
5394
5395
5396
5397
5398
    auto prog                       = parse_onnx("pad_cnst_dyn_test.onnx", options);
    EXPECT(p == prog);
}

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

kahmed10's avatar
kahmed10 committed
5403
5404
5405
TEST_CASE(pad_reflect_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5406
5407
5408
    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}});
5409
5410
5411
5412
5413
5414
5415
    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
5416
    mm->add_return({r});
kahmed10's avatar
kahmed10 committed
5417
5418
5419
5420
5421
5422

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

    EXPECT(p == prog);
}

5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
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
5444
5445
5446
TEST_CASE(pad_reflect_multiaxis_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5447
5448
5449
    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}});
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
    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
5460
    mm->add_return({r});
kahmed10's avatar
kahmed10 committed
5461
5462
5463
5464
5465
5466

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
5467
TEST_CASE(pow_test)
5468
5469
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5470
5471
5472
    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}});
5473
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
5474

Shucai Xiao's avatar
Shucai Xiao committed
5475
    auto prog = optimize_onnx("pow_test.onnx");
5476
5477
5478
5479

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
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
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
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
5529
5530
5531
TEST_CASE(prelu_brcst_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
5532
5533
5534
    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}});
5535
    auto bl1 = mm->add_instruction(
5536
        migraphx::make_op("multibroadcast", {{"out_lens", l0->get_shape().lens()}}), l1);
5537
    auto ret = mm->add_instruction(migraphx::make_op("prelu"), l0, bl1);
Shucai Xiao's avatar
Shucai Xiao committed
5538
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
5539
5540
5541
5542
5543
5544

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

    EXPECT(p == prog);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
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
5595
5596
5597
    EXPECT(p.sort() == prog.sort());
}

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
5642
5643
5644
5645
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);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
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
5693
5694
5695
5696
5697
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");

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
    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
5743
5744
5745
    EXPECT(p.sort() == prog.sort());
}

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
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
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
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
5843
5844
5845
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
5886
5887
5888
5889
5890
5891
5892
5893
5894
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
5895
5896
5897
    EXPECT(p.sort() == prog.sort());
}

Zakor Gyula's avatar
Zakor Gyula committed
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
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());
}

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
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
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
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});
}

6016
6017
6018
TEST_CASE(quantizelinear_test)
{
    migraphx::program p;
turneram's avatar
turneram committed
6019
6020
6021
    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}});
6022
    auto l1_mbcast =
6023
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6024
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6025
6026
6027
    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
6028
6029
6030
6031
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::uint8_type)}}),
        clip);
6032

turneram's avatar
turneram committed
6033
6034
6035
    auto prog = optimize_onnx("quantizelinear_test.onnx", true);
    EXPECT(p.sort() == prog.sort());
}
6036

turneram's avatar
turneram committed
6037
6038
6039
6040
6041
6042
6043
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 =
6044
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6045
6046
6047
6048
    l0 = mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l0);
turneram's avatar
turneram committed
6049
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6050
6051
6052
    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
6053
    mm->add_instruction(
6054
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
                          {{"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 =
6070
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l1);
turneram's avatar
turneram committed
6071
    auto div   = mm->add_instruction(migraphx::make_op("div"), l0, l1_mbcast);
6072
    auto round = mm->add_instruction(migraphx::make_op("nearbyint"), div);
6073
    auto l2_mbcast =
6074
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {5}}}), l2);
turneram's avatar
turneram committed
6075
    l2_mbcast = mm->add_instruction(
6076
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6077
6078
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
        l2_mbcast);
turneram's avatar
turneram committed
6079
6080
6081
    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);
6082
6083
6084
6085
6086
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int8_type)}}),
        clip);

turneram's avatar
turneram committed
6087
    auto prog = optimize_onnx("quantizelinear_zero_point_test.onnx", true);
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
    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
6098
6099
6100
    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}});
6101
    auto l1_bcast = mm->add_instruction(
6102
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l1);
6103
6104

    auto div      = mm->add_instruction(migraphx::make_op("div"), l0, l1_bcast);
6105
    auto round    = mm->add_instruction(migraphx::make_op("nearbyint"), div);
6106
    auto l2_bcast = mm->add_instruction(
6107
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", input_lens}}), l2);
6108
6109
    l2_bcast = mm->add_instruction(
        migraphx::make_op("convert",
turneram's avatar
turneram committed
6110
                          {{"target_type", migraphx::to_value(migraphx::shape::float_type)}}),
6111
        l2_bcast);
turneram's avatar
turneram committed
6112
6113
6114
    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);
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
    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
6126
    auto prog = optimize_onnx("quantizelinear_axis_test.onnx", true);
6127
6128
6129
6130
6131
6132
6133
    EXPECT(p.sort() == prog.sort());
}

TEST_CASE(quantizelinear_neg_axis_test)
{
    migraphx::program p = make_quantizelinear_axis_prog();

turneram's avatar
turneram committed
6134
    auto prog = optimize_onnx("quantizelinear_neg_axis_test.onnx", true);
6135
6136
6137
    EXPECT(p.sort() == prog.sort());
}

6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
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"); }));
}

6166
6167
6168
6169
6170
6171
6172
6173
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);
}

6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
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"); }));
}

6236
6237
6238
6239
6240
6241
6242
6243
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);
}

6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
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
6278
6279
6280
TEST_CASE(range_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6281
6282
6283
6284
6285
    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
6286
6287
6288
6289
6290
6291
6292
6293
6294

    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
6295
6296
6297
6298
6299
    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
6300
6301
6302
6303
6304
6305

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
6306
6307
6308
TEST_CASE(recip_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6309
6310
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3}});
6311
    mm->add_instruction(migraphx::make_op("recip"), input);
kahmed10's avatar
kahmed10 committed
6312
6313
6314
6315
6316
6317

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

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6318
6319
6320
TEST_CASE(reducel1_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6321
6322
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6323
6324
6325
    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
6326
6327
6328
6329
6330
    auto prog = optimize_onnx("reducel1_test.onnx");

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
6331
6332
6333
6334
6335
6336
6337
6338
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,
6339
                                                         {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}}});
Brian Pickrell's avatar
Brian Pickrell committed
6340
6341
6342
6343
6344
6345
6346
        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;
6347
        options.map_dyn_input_dims["x"] = {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}};
Brian Pickrell's avatar
Brian Pickrell committed
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
        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,
6359
                                                         {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}}});
Brian Pickrell's avatar
Brian Pickrell committed
6360
6361
6362
6363
6364
6365
6366
6367
        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;
6368
        options.map_dyn_input_dims["x"] = {{3, 3}, {3, 5}, {4, 6, {5}}, {5, 7, {6}}};
Brian Pickrell's avatar
Brian Pickrell committed
6369
6370
6371
6372
6373
6374
        auto prog = migraphx::parse_onnx("reducel1_dyn_noaxes_test.onnx", options);

        EXPECT(p == prog);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
6375
6376
6377
TEST_CASE(reducel2_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6378
6379
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6380
6381
6382
6383
    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
6384
6385
6386
6387
6388
6389
6390
6391
    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
6392
6393
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6394
6395
    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
6396
6397
6398
6399
6400
6401
6402
6403
    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
6404
6405
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6406
6407
6408
    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
6409
6410
6411
6412
6413
    auto prog = optimize_onnx("reduce_log_sum_exp_test.onnx");

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6414
6415
6416
TEST_CASE(reducemax_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6417
6418
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6419
    mm->add_instruction(migraphx::make_op("reduce_max", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6420
    auto prog = optimize_onnx("reducemax_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6421
6422
6423
6424

    EXPECT(p == prog);
}

Brian Pickrell's avatar
Brian Pickrell committed
6425
6426
6427
6428
6429
6430
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(
6431
        "x", migraphx::shape{migraphx::shape::float_type, {{3, 5}, {4, 4}, {5, 5}, {6, 6}}});
Brian Pickrell's avatar
Brian Pickrell committed
6432
6433
6434
6435
6436
    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;
6437
    options.map_dyn_input_dims["x"] = {{3, 5}, {4, 4}, {5, 5}, {6, 6}};
Brian Pickrell's avatar
Brian Pickrell committed
6438
6439
6440
6441
6442
    auto prog                       = migraphx::parse_onnx("reducemax_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6443
TEST_CASE(reducemean_test)
6444
6445
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6446
6447
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6448
6449
    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
6450
    auto prog = optimize_onnx("reducemean_test.onnx");
6451
6452
6453
6454

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6455
TEST_CASE(reducemean_keepdims_test)
6456
6457
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6458
6459
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6460
    mm->add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6461
    auto prog = optimize_onnx("reducemean_keepdims_test.onnx");
6462
6463
6464
6465

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6466
6467
6468
TEST_CASE(reducemin_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6469
6470
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6471
6472
    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
6473
    auto prog = optimize_onnx("reducemin_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6474
6475
6476
6477

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6478
6479
6480
TEST_CASE(reduceprod_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6481
6482
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6483
    mm->add_instruction(migraphx::make_op("reduce_prod", {{"axes", {2}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6484
6485
6486
6487
6488
    auto prog = optimize_onnx("reduceprod_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6489
TEST_CASE(reducesum_test)
Khalique's avatar
Khalique committed
6490
6491
{
    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 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
6496
    auto prog = optimize_onnx("reducesum_test.onnx");
6497
6498

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6499
6500
}

Shucai Xiao's avatar
Shucai Xiao committed
6501
6502
6503
6504
TEST_CASE(reducesum_empty_axes_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
6505
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
    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();
6520
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
6521
6522
6523
6524
6525
6526
6527
    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
6528
TEST_CASE(reducesum_multiaxis_test)
Khalique's avatar
Khalique committed
6529
6530
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6531
6532
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6533
6534
    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
6535
    auto prog = optimize_onnx("reducesum_multiaxis_test.onnx");
6536
6537

    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6538
6539
}

Khalique's avatar
Khalique committed
6540
TEST_CASE(reducesum_keepdims_test)
Khalique's avatar
Khalique committed
6541
6542
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6543
6544
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6545
    mm->add_instruction(migraphx::make_op("reduce_sum", {{"axes", {2, 3}}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
6546
    auto prog = optimize_onnx("reducesum_keepdims_test.onnx");
6547
6548
6549
6550

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
6551
6552
6553
TEST_CASE(reducesum_square_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6554
6555
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
6556
6557
6558
    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
6559
6560
6561
6562
6563
    auto prog = optimize_onnx("reducesum_square_test.onnx");

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
6564
TEST_CASE(reshape_test)
6565
{
Khalique's avatar
Khalique committed
6566
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6567
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
6568
6569
    migraphx::op::reshape op;
    std::vector<int64_t> reshape_dims{3, 8};
Shucai Xiao's avatar
Shucai Xiao committed
6570
    mm->add_literal(
Khalique's avatar
Khalique committed
6571
        migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {2}}, reshape_dims});
Shucai Xiao's avatar
Shucai Xiao committed
6572
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 2, 3}});
Khalique's avatar
Khalique committed
6573
    op.dims = reshape_dims;
6574
6575
    mm->add_instruction(op, l0);
    mm->add_instruction(op, l0);
Shucai Xiao's avatar
Shucai Xiao committed
6576
    auto prog = optimize_onnx("reshape_test.onnx");
6577
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
6578
6579
}

Khalique's avatar
Khalique committed
6580
TEST_CASE(reshape_non_standard_test)
6581
6582
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
6583
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
6584
6585
    migraphx::op::reshape op;
    migraphx::shape s{migraphx::shape::float_type, {2, 3, 4}};
6586
6587
6588
    auto x = mm->add_parameter("x", s);
    auto tran_x =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), x);
6589
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {4, 3, 2}}}), tran_x);
Shucai Xiao's avatar
Shucai Xiao committed
6590
    auto prog = optimize_onnx("reshape_non_standard_test.onnx");
6591
6592
6593
6594

    EXPECT(p == prog);
}

6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
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);
}

6627
TEST_CASE(resize_downsample_c_test)
Shucai Xiao's avatar
Shucai Xiao committed
6628
6629
{
    migraphx::program p;
6630
6631
    auto* mm = p.get_main_module();

Shucai Xiao's avatar
Shucai Xiao committed
6632
6633
    std::vector<float> ds = {1.0f, 1.0f, 0.6f, 0.6f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
6634
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
6635
6636

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
6637
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6638

6639
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6640
6641

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 1, 2}};
6642
    std::vector<int> ind = {0, 2};
6643
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6644

6645
6646
    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);
6647
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6648

6649
    auto prog = migraphx::parse_onnx("resize_downsample_c_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
6650
6651
6652
6653

    EXPECT(p == prog);
}

6654
TEST_CASE(resize_downsample_f_test)
Shucai Xiao's avatar
Shucai Xiao committed
6655
6656
{
    migraphx::program p;
6657
    auto* mm              = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6658
6659
    std::vector<float> ds = {1.0f, 1.0f, 0.6f, 0.6f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
6660
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
6661
6662

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
6663
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6664

6665
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6666
6667

    migraphx::shape si{migraphx::shape::int32_type, {1, 1, 1, 2}};
6668
    std::vector<int> ind = {0, 3};
6669
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6670

6671
6672
    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);
6673
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6674

6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
    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
6706

6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
    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
6745
6746
6747
6748
6749
6750
    EXPECT(p == prog);
}

TEST_CASE(resize_outsize_test)
{
    migraphx::program p;
6751
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
6752
6753
6754

    std::vector<int64_t> out_len = {1, 1, 4, 6};
    migraphx::shape so{migraphx::shape::int64_type, {4}};
6755
    mm->add_literal(migraphx::literal(so, out_len));
Shucai Xiao's avatar
Shucai Xiao committed
6756
6757

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
6758
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
6759

6760
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
6761
6762
6763

    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};
6764
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
6765

6766
6767
    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);
6768
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
6769
6770
6771
6772
6773
6774

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

    EXPECT(p == prog);
}

6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
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));

6791
6792
    auto tx =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), inx);
6793
6794
    mm->add_instruction(migraphx::make_op("undefined"));

6795
    auto lrsp = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {8}}}), tx);
6796
6797
6798
6799
6800
6801
6802
6803
    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
6804
static auto create_upsample_linear_prog()
6805
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
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
{
    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
6895
6896
6897
6898
6899
6900
    return p;
}

TEST_CASE(resize_upsample_linear_ac_test)
{
    auto p    = create_upsample_linear_prog();
6901
6902
6903
6904
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
6995
6996
6997
6998
6999
    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
7000
7001
7002
TEST_CASE(resize_upsample_pc_test)
{
    migraphx::program p;
7003
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7004
7005
7006

    std::vector<float> ds = {1.0f, 1.0f, 2.0f, 1.5f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
7007
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
7008
7009

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 4}};
7010
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
7011

7012
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
7013
7014
7015

    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};
7016
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
7017

7018
7019
    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);
7020
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7021
7022
7023
7024
7025
7026
7027
7028
7029

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

    EXPECT(p == prog);
}

TEST_CASE(resize_upsample_pf_test)
{
    migraphx::program p;
7030
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7031
7032
7033

    std::vector<float> ds = {1.0f, 1.0f, 2.0f, 3.0f};
    migraphx::shape ss{migraphx::shape::float_type, {4}};
7034
    mm->add_literal(migraphx::literal{ss, ds});
Shucai Xiao's avatar
Shucai Xiao committed
7035
7036

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
7037
    auto inx = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
7038

7039
    mm->add_instruction(migraphx::make_op("undefined"));
Shucai Xiao's avatar
Shucai Xiao committed
7040
7041
7042

    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};
7043
    auto li              = mm->add_literal(migraphx::literal(si, ind));
Shucai Xiao's avatar
Shucai Xiao committed
7044

7045
7046
    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);
7047
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7048
7049
7050
7051
7052
7053

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

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
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
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
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
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
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);

7186
7187
7188
7189
7190
7191
7192
    // 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
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
    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
7229
7230
7231
TEST_CASE(round_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7232
7233
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {10, 5}});
7234
    mm->add_instruction(migraphx::make_op("nearbyint"), input);
Khalique's avatar
Khalique committed
7235

Shucai Xiao's avatar
Shucai Xiao committed
7236
    auto prog = optimize_onnx("round_test.onnx");
Khalique's avatar
Khalique committed
7237
7238
7239
    EXPECT(p == prog);
}

7240
7241
// 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
7242
7243
7244
7245
{
    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
7246
7247
7248
7249
    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}});
7250
    auto r = mm->add_instruction(migraphx::make_op(scatter_mode, {{"axis", axis}}), l0, l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
7251
    mm->add_return({r});
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
    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
7274
7275
7276
7277

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
7278
7279
TEST_CASE(scatternd_test)
{
7280
7281
7282
7283
7284
7285
7286
7287
    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}});
    auto r  = mm->add_instruction(migraphx::make_op("scatternd_none"), l0, l1, l2);
    mm->add_return({r});
    auto prog = migraphx::parse_onnx("scatternd_test.onnx");
turneram's avatar
turneram committed
7288

7289
7290
    EXPECT(p == prog);
}
turneram's avatar
turneram committed
7291

7292
7293
7294
7295
7296
7297
7298
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(
7299
        "data", migraphx::shape{migraphx::shape::float_type, {{1, 3, {2}}, {2, 2}, {2, 2}}});
7300
    auto l1 = mm->add_parameter(
7301
        "indices", migraphx::shape{migraphx::shape::int64_type, {{2, 1, {2}}, {1, 1}, {2, 2}}});
7302
    auto l2 = mm->add_parameter(
7303
        "updates", migraphx::shape{migraphx::shape::float_type, {{2, 1, {2}}, {1, 1}, {2, 2}}});
7304
7305
7306
    auto r = mm->add_instruction(migraphx::make_op("scatternd_none"), l0, l1, l2);
    mm->add_return({r});
    migraphx::onnx_options options;
7307
7308
7309
    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}};
7310
    auto prog = migraphx::parse_onnx("scatternd_dyn_test.onnx", options);
turneram's avatar
turneram committed
7311

7312
7313
    EXPECT(p == prog);
}
turneram's avatar
turneram committed
7314

7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
TEST_CASE(scatternd_add_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, 2}});
    auto l2 = mm->add_parameter("updates", migraphx::shape{migraphx::shape::float_type, {2, 1, 2}});
    auto r  = mm->add_instruction(migraphx::make_op("scatternd_add"), l0, l1, l2);
    mm->add_return({r});
    auto prog = migraphx::parse_onnx("scatternd_add_test.onnx");
turneram's avatar
turneram committed
7325

7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
    EXPECT(p == prog);
}

TEST_CASE(scatternd_mul_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, 2}});
    auto l2 = mm->add_parameter("updates", migraphx::shape{migraphx::shape::float_type, {2, 1, 2}});
    auto r  = mm->add_instruction(migraphx::make_op("scatternd_mul"), l0, l1, l2);
    mm->add_return({r});
    auto prog = migraphx::parse_onnx("scatternd_mul_test.onnx");

    EXPECT(p == prog);
turneram's avatar
turneram committed
7341
7342
}

Shucai Xiao's avatar
Shucai Xiao committed
7343
7344
7345
TEST_CASE(selu_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7346
    auto* mm                      = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
7347
7348
    std::vector<std::size_t> lens = {2, 3};
    migraphx::shape s{migraphx::shape::double_type, lens};
Shucai Xiao's avatar
Shucai Xiao committed
7349
    auto x = mm->add_parameter("x", s);
Shucai Xiao's avatar
Shucai Xiao committed
7350
7351

    migraphx::shape ls{migraphx::shape::double_type, {1}};
7352
7353
7354
7355
    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
7356

7357
7358
    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
7359

7360
7361
    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
7362

7363
7364
    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
7365

7366
7367
7368
    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
7369
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
7370
7371
7372
7373
7374
7375

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7376
TEST_CASE(shape_test)
7377
7378
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7379
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
7380
    migraphx::shape s{migraphx::shape::float_type, {3, 4, 5, 6}};
Shucai Xiao's avatar
Shucai Xiao committed
7381
    auto l0 = mm->add_parameter("x", s);
Khalique's avatar
Khalique committed
7382
    migraphx::shape s_shape{migraphx::shape::int64_type, {4}};
Shucai Xiao's avatar
Shucai Xiao committed
7383
    mm->add_literal(s_shape, l0->get_shape().lens());
Shucai Xiao's avatar
Shucai Xiao committed
7384
    auto prog = optimize_onnx("shape_test.onnx");
7385
7386
7387
7388

    EXPECT(p == prog);
}

7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
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
7501
TEST_CASE(shape_gather_test)
7502
7503
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7504
7505
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {7, 3, 10}});
7506
    migraphx::shape const_shape{migraphx::shape::int32_type, {1}};
Shucai Xiao's avatar
Shucai Xiao committed
7507
    auto l2 = mm->add_literal(migraphx::literal{const_shape, {1}});
Khalique's avatar
Khalique committed
7508
    auto l1 =
Shucai Xiao's avatar
Shucai Xiao committed
7509
        mm->add_literal(migraphx::shape{migraphx::shape::int64_type, {3}}, l0->get_shape().lens());
Khalique's avatar
Khalique committed
7510
    int axis = 0;
7511
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l1, l2);
Shucai Xiao's avatar
Shucai Xiao committed
7512
    auto prog = optimize_onnx("shape_gather_test.onnx");
7513
7514
7515
7516

    EXPECT(p == prog);
}

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
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
7584
TEST_CASE(sign_test)
Khalique's avatar
Khalique committed
7585
7586
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7587
7588
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {10, 5}});
7589
    mm->add_instruction(migraphx::make_op("sign"), input);
Khalique's avatar
Khalique committed
7590

Shucai Xiao's avatar
Shucai Xiao committed
7591
    auto prog = optimize_onnx("sign_test.onnx");
Khalique's avatar
Khalique committed
7592
7593
7594
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7595
TEST_CASE(sin_test)
7596
7597
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7598
7599
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
7600
    mm->add_instruction(migraphx::make_op("sin"), input);
7601

Shucai Xiao's avatar
Shucai Xiao committed
7602
    auto prog = optimize_onnx("sin_test.onnx");
7603
7604
7605
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
7606
TEST_CASE(sinh_test)
7607
7608
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7609
7610
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
7611
    mm->add_instruction(migraphx::make_op("sinh"), input);
Khalique's avatar
Khalique committed
7612

Shucai Xiao's avatar
Shucai Xiao committed
7613
    auto prog = optimize_onnx("sinh_test.onnx");
7614
7615
7616
7617

    EXPECT(p == prog);
}

7618
7619
7620
7621
TEST_CASE(sinh_dynamic_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
7622
    migraphx::shape::dynamic_dimension dd{1, 10};
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
    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
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
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
7670
7671
7672
TEST_CASE(slice_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7673
7674
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 2}});
7675
7676
    mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 0}}, {"ends", {2, 2}}}), l0);
kahmed10's avatar
kahmed10 committed
7677
7678
7679
7680
7681
    auto prog = optimize_onnx("slice_test.onnx");

    EXPECT(p == prog);
}

7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
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
7695
7696
7697
7698
7699
7700
TEST_CASE(slice_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter(
7701
        "0", migraphx::shape{migraphx::shape::float_type, {{3, 3}, {1, 3}, {2, 2}}});
Brian Pickrell's avatar
Brian Pickrell committed
7702
7703
7704
7705
7706
7707
7708
    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.
7709
    options.map_dyn_input_dims["0"] = {{3, 3}, {1, 3}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
    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;
7720
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
7721
7722
7723
7724
7725
7726
7727
7728
    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;
7729
    options.default_dyn_dim_value = {1, 4};
Brian Pickrell's avatar
Brian Pickrell committed
7730
7731
7732
    EXPECT(test::throws([&] { migraphx::parse_onnx("slice_reverse_dyn_test.onnx", options); }));
}

kahmed10's avatar
kahmed10 committed
7733
7734
7735
TEST_CASE(slice_3arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7736
7737
7738
7739
    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}});
7740
7741
    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
7742
    mm->add_return({ret});
kahmed10's avatar
kahmed10 committed
7743
7744
7745
7746
7747
7748

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

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
7749
7750
7751
TEST_CASE(slice_5arg_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7752
7753
7754
7755
7756
7757
    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}});
7758
7759
7760
    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
7761
    mm->add_return({ret});
Shucai Xiao's avatar
Shucai Xiao committed
7762
7763
7764
7765
7766
7767

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

    EXPECT(p == prog);
}

Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
7768
7769
7770
7771
7772
7773
7774
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}});
7775
7776
    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
7777
    auto slice_out = mm->add_instruction(
7778
7779
        migraphx::make_op("slice",
                          {{"axes", {-1, -2}}, {"starts", {-4, -3}}, {"ends", {2147483647, -1}}}),
Cagri Eryilmaz's avatar
Cagri Eryilmaz committed
7780
7781
7782
7783
7784
7785
7786
7787
7788
        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);
}

7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
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);
}

7813
7814
7815
TEST_CASE(slice_max_end_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7816
7817
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {10, 20}});
7818
7819
7820
7821
    mm->add_instruction(
        migraphx::make_op("slice",
                          {{"axes", {0, 1}}, {"starts", {1, 2}}, {"ends", {3000000000, -1}}}),
        l0);
7822
7823
7824
7825
7826
    auto prog = optimize_onnx("slice_max_end_test.onnx");

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
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);
}

7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
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
7909
7910
7911
7912
7913
TEST_CASE(slice_var_input_steps_error)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("slice_var_input_steps_error.onnx"); }));
}

Khalique's avatar
Khalique committed
7914
TEST_CASE(softmax_test)
Shucai Xiao's avatar
Shucai Xiao committed
7915
7916
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
7917
7918
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
7919
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Shucai Xiao's avatar
Shucai Xiao committed
7920
    auto prog = optimize_onnx("softmax_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
7921
7922
7923
7924

    EXPECT(p == prog);
}

7925
7926
7927
7928
7929
7930
7931
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
7932
    auto l2 = mm->add_instruction(migraphx::make_op("softmax", {{"axis", -1}}), l1);
7933
7934
7935
7936
7937
7938
7939
    mm->add_return({l2});

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

    EXPECT(p == prog);
}

7940
7941
7942
7943
7944
TEST_CASE(softmax_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter(
7945
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {4, 4}}});
7946
7947
7948
7949
    auto ret = mm->add_instruction(migraphx::make_op("softmax", {{"axis", -1}}), l0);
    mm->add_return({ret});

    migraphx::onnx_options options;
7950
    options.default_dyn_dim_value = {1, 4};
7951
7952
7953
7954
7955
    auto prog                     = migraphx::parse_onnx("softmax_dyn_test.onnx", options);

    EXPECT(p == prog);
}

turneram's avatar
turneram committed
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
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
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
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
8036
8037
8038
TEST_CASE(split_minus_axis_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8039
8040
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8041
8042
8043
8044
8045
8046
    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
8047
    mm->add_return({r1, r2, r3});
Shucai Xiao's avatar
Shucai Xiao committed
8048
8049
8050
8051
8052
8053

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

    EXPECT(p == prog);
}

8054
8055
8056
TEST_CASE(split_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8057
8058
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8059
8060
8061
8062
8063
8064
    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
8065
    mm->add_return({r1, r2, r3});
8066
8067
8068
8069
8070

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

8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
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);
}

8096
8097
8098
TEST_CASE(split_test_default)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8099
8100
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8101
8102
8103
8104
    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
8105
    mm->add_return({r1, r2});
8106
8107
8108
8109
8110

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

8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
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);
}

8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
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"); }));
}

8168
8169
8170
8171
8172
TEST_CASE(split_test_invalid_num_outputs)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("split_test_invalid_num_outputs.onnx"); }));
}

Khalique's avatar
Khalique committed
8173
TEST_CASE(sqrt_test)
Shucai Xiao's avatar
Shucai Xiao committed
8174
8175
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8176
8177
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10, 15}});
8178
    mm->add_instruction(migraphx::make_op("sqrt"), input);
Shucai Xiao's avatar
Shucai Xiao committed
8179

Shucai Xiao's avatar
Shucai Xiao committed
8180
    auto prog = optimize_onnx("sqrt_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8181
8182
8183
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8184
TEST_CASE(squeeze_unsqueeze_test)
Khalique's avatar
Khalique committed
8185
8186
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8187
    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
8188
8189
8190
    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
8191
        mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 1, 1, 2, 1}});
8192
8193
    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
8194
    auto prog = optimize_onnx("squeeze_unsqueeze_test.onnx");
Khalique's avatar
Khalique committed
8195

8196
8197
8198
8199
8200
8201
8202
8203
8204
    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};
8205
8206
    auto l0  = mm->add_parameter("0",
                                migraphx::shape{migraphx::shape::float_type,
8207
                                                 {{1, 1}, {1, 4}, {1, 1}, {1, 1}, {1, 4}, {1, 1}}});
8208
8209
8210
8211
8212
8213
8214
    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;
8215
    options.default_dyn_dim_value = {1, 4};
8216
8217
    auto prog                     = parse_onnx("squeeze_unsqueeze_dyn_test.onnx", options);

Khalique's avatar
Khalique committed
8218
8219
8220
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
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();
8239
    mm->add_literal(migraphx::literal{migraphx::shape::int64_type});
Shucai Xiao's avatar
Shucai Xiao committed
8240
8241
8242
8243
8244
8245
8246
8247
8248
    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
8249
TEST_CASE(sub_bcast_test)
Shucai Xiao's avatar
Shucai Xiao committed
8250
8251
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8252
8253
8254
    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}});
8255
    auto l2  = mm->add_instruction(
8256
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", l0->get_shape().lens()}}), l1);
8257
    mm->add_instruction(migraphx::make_op("sub"), l0, l2);
Shucai Xiao's avatar
Shucai Xiao committed
8258

Shucai Xiao's avatar
Shucai Xiao committed
8259
    auto prog = optimize_onnx("sub_bcast_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8260
8261
8262
8263

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8264
TEST_CASE(sub_scalar_test)
Shucai Xiao's avatar
Shucai Xiao committed
8265
8266
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8267
8268
8269
    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}});
8270
8271
    auto m1 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 3, 4, 5}}}), l1);
8272
    mm->add_instruction(migraphx::make_op("sub"), l0, m1);
Shucai Xiao's avatar
Shucai Xiao committed
8273
    auto prog = optimize_onnx("sub_scalar_test.onnx");
Shucai Xiao's avatar
Shucai Xiao committed
8274
8275
8276
8277

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8278
8279
8280
TEST_CASE(sum_int_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8281
8282
8283
8284
    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}});
8285
8286
    auto cin0   = mm->add_instruction(
        migraphx::make_op("convert",
8287
                            {{"target_type", migraphx::to_value(migraphx::shape::uint32_type)}}),
8288
8289
8290
8291
8292
8293
8294
        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
8295
8296
8297
8298
8299

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

Khalique's avatar
Khalique committed
8300
TEST_CASE(sum_test)
8301
8302
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8303
8304
8305
8306
    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}});
8307
8308
    auto l0     = mm->add_instruction(migraphx::make_op("add"), input0, input1);
    mm->add_instruction(migraphx::make_op("add"), l0, input2);
8309

Shucai Xiao's avatar
Shucai Xiao committed
8310
    auto prog = optimize_onnx("sum_test.onnx");
8311
8312
8313
    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8314
8315
8316
TEST_CASE(sum_type_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8317
8318
8319
8320
8321
8322
8323
8324
8325
    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}});
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
    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
8357
    mm->add_return({s6});
Shucai Xiao's avatar
Shucai Xiao committed
8358
8359
8360
8361
8362
8363

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8364
TEST_CASE(tan_test)
8365
8366
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8367
8368
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {10}});
8369
    mm->add_instruction(migraphx::make_op("tan"), input);
8370

Shucai Xiao's avatar
Shucai Xiao committed
8371
    auto prog = optimize_onnx("tan_test.onnx");
8372
8373
8374
    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8375
TEST_CASE(tanh_test)
8376
8377
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8378
8379
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {1}});
8380
    mm->add_instruction(migraphx::make_op("tanh"), input);
8381

Shucai Xiao's avatar
Shucai Xiao committed
8382
    auto prog = optimize_onnx("tanh_test.onnx");
8383
8384
8385
8386

    EXPECT(p == prog);
}

8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
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
8444
8445
8446
TEST_CASE(tile_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8447
8448
8449
    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}});
8450
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), input, input);
kahmed10's avatar
kahmed10 committed
8451
8452
8453
8454
8455
8456
8457
8458
8459

    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
8460
8461
8462
    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}});
8463
8464
8465
    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
8466
8467
8468
8469
8470
8471

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

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
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
8491
TEST_CASE(transpose_test)
8492
8493
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8494
8495
    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
8496
    std::vector<int64_t> perm{0, 3, 1, 2};
8497
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
Khalique's avatar
Khalique committed
8498

Shucai Xiao's avatar
Shucai Xiao committed
8499
    auto prog = optimize_onnx("transpose_test.onnx");
8500
8501
8502
8503

    EXPECT(p == prog);
}

Charlie Lin's avatar
Charlie Lin committed
8504
8505
8506
8507
8508
TEST_CASE(transpose_dyn_test)
{
    migraphx::program p;
    auto* mm   = p.get_main_module();
    auto input = mm->add_parameter(
8509
        "0", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}, {3, 3}}});
Charlie Lin's avatar
Charlie Lin committed
8510
8511
8512
8513
8514
    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;
8515
    options.default_dyn_dim_value = {1, 4};
Charlie Lin's avatar
Charlie Lin committed
8516
8517
8518
8519
8520
    auto prog                     = migraphx::parse_onnx("transpose_dyn_test.onnx", options);

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
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
8575
TEST_CASE(transpose_gather_test)
8576
8577
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8578
8579
    auto* mm             = p.get_main_module();
    auto make_contiguous = [&mm](migraphx::instruction_ref ins) {
Khalique's avatar
Khalique committed
8580
8581
8582
8583
8584
        if(ins->get_shape().standard())
        {
            return ins;
        }

8585
        return mm->add_instruction(migraphx::make_op("contiguous"), ins);
Khalique's avatar
Khalique committed
8586
8587
    };

Shucai Xiao's avatar
Shucai Xiao committed
8588
8589
    auto data =
        mm->add_parameter("data", migraphx::shape{migraphx::shape::float_type, {3, 5, 4, 6}});
Khalique's avatar
Khalique committed
8590
    auto ind =
Shucai Xiao's avatar
Shucai Xiao committed
8591
        mm->add_parameter("indices", migraphx::shape{migraphx::shape::int32_type, {2, 4, 3, 5}});
8592
    auto tr_data =
8593
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1, 3}}}), data);
8594
    auto tr_ind =
8595
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1, 3}}}), ind);
8596
8597
8598
8599
    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
8600

Shucai Xiao's avatar
Shucai Xiao committed
8601
    auto prog = optimize_onnx("transpose_gather_test.onnx");
8602

8603
    EXPECT(p.sort() == prog.sort());
8604
8605
}

8606
8607
8608
TEST_CASE(undefined_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8609
8610
    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}});
8611
8612
    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
8613
    mm->add_return({l2});
8614
8615
8616
8617
8618
8619

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
8620
TEST_CASE(unknown_test)
8621
8622
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8623
8624
8625
8626
8627
    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
8628
    auto prog = optimize_onnx("unknown_test.onnx");
8629
8630
8631
8632

    EXPECT(p == prog);
}

8633
8634
8635
8636
8637
TEST_CASE(unknown_aten_test)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("unknown_aten_test.onnx"); }));
}

8638
8639
8640
8641
8642
TEST_CASE(unknown_test_throw)
{
    EXPECT(test::throws([&] { migraphx::parse_onnx("unknown_test.onnx"); }));
}

Shucai Xiao's avatar
Shucai Xiao committed
8643
8644
8645
8646
8647
8648
8649
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
8650
8651
8652
TEST_CASE(upsample_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8653
    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
8654
    migraphx::shape ss{migraphx::shape::float_type, {4}};
Shucai Xiao's avatar
Shucai Xiao committed
8655
    mm->add_literal(migraphx::literal(ss, {1.0f, 1.0f, 2.0f, 3.0f}));
Shucai Xiao's avatar
Shucai Xiao committed
8656
8657

    migraphx::shape sx{migraphx::shape::float_type, {1, 1, 2, 2}};
Shucai Xiao's avatar
Shucai Xiao committed
8658
    auto ix = mm->add_parameter("X", sx);
Shucai Xiao's avatar
Shucai Xiao committed
8659
8660
8661
8662

    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
8663
    auto li  = mm->add_literal(migraphx::literal(si, ind));
8664
8665
    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
8666
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
8667
8668
8669
8670
8671
8672

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

    EXPECT(p == prog);
}

8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
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);
}

8694
8695
8696
8697
8698
8699
8700
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); }));
}

8701
8702
8703
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8704
8705
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
8706
    mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8707
    auto prog = optimize_onnx("variable_batch_test.onnx");
8708
8709
8710
8711

    EXPECT(p == prog);
}

8712
8713
8714
8715
8716
8717
8718
8719
8720
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;
8721
    options.default_dyn_dim_value = {2, 2};
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731

    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();
8732
8733
8734
    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);
8735
8736
8737
    mm->add_return({r});

    migraphx::onnx_options options;
8738
    options.default_dyn_dim_value = {2, 5};
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748

    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();
8749
8750
8751
    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);
8752
8753
8754
    mm->add_return({r});

    migraphx::onnx_options options;
8755
    options.map_dyn_input_dims["0"] = {{2, 5}, {3, 3}, {16, 16}, {16, 16}};
8756
8757
8758
8759
8760
8761
8762

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

    EXPECT(p == prog);
}

TEST_CASE(variable_batch_user_input_test4)
8763
8764
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8765
8766
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 16, 16}});
8767
    auto r   = mm->add_instruction(migraphx::make_op("identity"), l0);
Shucai Xiao's avatar
Shucai Xiao committed
8768
    mm->add_return({r});
8769
8770
8771
8772
8773
8774
8775
8776
8777

    migraphx::onnx_options options;
    options.default_dim_value = 2;

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

    EXPECT(p == prog);
}

8778
8779
8780
8781
8782
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;
8783
    options.default_dyn_dim_value = {1, 2};
8784
8785
8786
8787
8788
8789
8790
8791

    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;
8792
    options.map_dyn_input_dims["0"] = {{2, 5}, {3, 3}, {16, 16}, {16, 16}};
8793
8794
8795
8796
8797
    options.map_input_dims["0"]     = {2, 3, 16, 16};

    EXPECT(test::throws([&] { migraphx::parse_onnx("variable_batch_test.onnx", options); }));
}

8798
8799
TEST_CASE(variable_batch_user_input_test7)
{
8800
8801
    // if entry in map_dyn_input_dims is all fixed dynamic_dimensions, convert it to a static
    // shape
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
    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);
}

8816
8817
8818
TEST_CASE(variable_batch_leq_zero_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8819
8820
8821
    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}});
8822
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
Shucai Xiao's avatar
Shucai Xiao committed
8823
    auto prog = optimize_onnx("variable_batch_leq_zero_test.onnx");
8824
8825
8826
8827

    EXPECT(p == prog);
}

Shucai Xiao's avatar
Shucai Xiao committed
8828
8829
8830
TEST_CASE(where_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
8831
8832
8833
8834
8835
    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
8836
8837
    auto lccm =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {2, 2, 2, 2}}}), lc);
8838
8839
8840
8841
    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);
8842

turneram's avatar
turneram committed
8843
    auto r = mm->add_instruction(migraphx::make_op("where"), lccm, lxm, lym);
Shucai Xiao's avatar
Shucai Xiao committed
8844
    mm->add_return({r});
Shucai Xiao's avatar
Shucai Xiao committed
8845
8846
8847
8848
8849
8850

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

    EXPECT(p == prog);
}

8851
8852
8853
8854
8855
8856
8857
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(
8858
        "c", migraphx::shape{migraphx::shape::bool_type, {{1, 4}, {2, 2}, {2, 2}}});
8859
    auto lx = mm->add_parameter(
8860
        "x", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}});
8861
    auto ly = mm->add_parameter(
8862
        "y", migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 2}, {2, 2}}});
8863
8864
8865
8866
8867

    auto r = mm->add_instruction(migraphx::make_op("where"), lc, lx, ly);
    mm->add_return({r});

    migraphx::onnx_options options;
8868
    options.default_dyn_dim_value = {1, 4};
8869
8870
8871
8872
8873
8874
8875
8876
8877
    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;
8878
    options.default_dyn_dim_value = {1, 4};
8879
8880
8881
    EXPECT(test::throws([&] { migraphx::parse_onnx("where_mixed_test.onnx", options); }));
}

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