op_shape_test.cpp 179 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.
 */
Paul's avatar
Paul committed
24
25
26
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
27
28
#include <migraphx/permutation.hpp>
#include <migraphx/op/common.hpp>
29
#include <sstream>
30
31
32
33
#include <migraphx/make_op.hpp>

#include <migraphx/serialize.hpp>

34
35
#include "test.hpp"

Paul's avatar
Paul committed
36
template <class... Ts>
Paul's avatar
Paul committed
37
void expect_shape(const migraphx::shape& expected, const migraphx::operation& op, Ts... xs)
38
{
Paul's avatar
Paul committed
39
    migraphx::program p;
40
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
41
42
    std::vector<migraphx::shape> shapes{xs...};
    std::vector<migraphx::instruction_ref> args(shapes.size());
Paul's avatar
Paul committed
43
    std::transform(
44
45
        shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return mm->add_outline(s); });
    mm->add_instruction(op, args);
46
    if(p.get_output_shapes().back() != expected)
Paul's avatar
Paul committed
47
    {
48
        std::cout << "FAILED: Incorrect shape for " << op << ": ";
49
        std::cout << expected << " != " << p.get_output_shapes().back() << std::endl;
Paul's avatar
Paul committed
50
        for(auto&& s : shapes)
51
52
53
54
            std::cout << "    " << s << std::endl;
    }
}

Paul's avatar
Paul committed
55
template <class... Ts>
Paul's avatar
Paul committed
56
void throws_shape(const migraphx::operation& op, Ts... xs)
57
{
Paul's avatar
Paul committed
58
    migraphx::program p;
59
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
60
61
    std::vector<migraphx::shape> shapes{xs...};
    std::vector<migraphx::instruction_ref> args(shapes.size());
Paul's avatar
Paul committed
62
    std::transform(
63
64
        shapes.begin(), shapes.end(), args.begin(), [&](auto&& s) { return mm->add_outline(s); });
    bool thrown = test::throws([&] { mm->add_instruction(op, args); });
Paul's avatar
Paul committed
65
66
    if(not thrown)
    {
67
        std::cout << "FAILED: No error found for " << op.name() << ": ";
Paul's avatar
Paul committed
68
        for(auto&& s : shapes)
69
70
71
72
            std::cout << "    " << s << std::endl;
    }
}

Paul's avatar
Paul committed
73
74
75
76
template <class...>
struct always_false : std::false_type
{
};
77

Paul's avatar
Paul committed
78
template <class... Ts>
Paul's avatar
Paul committed
79
void throws_shape(const migraphx::shape&, Ts...)
80
{
Paul's avatar
Paul committed
81
82
    static_assert(always_false<Ts...>{},
                  "An expected shape should not be passed to throws_shape function");
83
84
}

Charlie Lin's avatar
Charlie Lin committed
85
86
87
88
89
90
TEST_CASE(allocate_static)
{
    migraphx::shape out_shape{migraphx::shape::float_type, {2, 3, 4}};
    expect_shape(out_shape, migraphx::make_op("allocate", {{"shape", to_value(out_shape)}}));
}

91
TEST_CASE(allocate_static_input)
92
93
94
95
96
97
{
    migraphx::shape input{migraphx::shape::int64_type, {3}};
    migraphx::shape out_shape{migraphx::shape::float_type, {2, 3, 4}};
    expect_shape(out_shape, migraphx::make_op("allocate", {{"shape", to_value(out_shape)}}), input);
}

Charlie Lin's avatar
Charlie Lin committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
TEST_CASE(allocate_dyn)
{
    migraphx::shape input{migraphx::shape::int64_type, {2}};
    auto max_val = std::numeric_limits<std::size_t>::max();
    std::vector<migraphx::shape::dynamic_dimension> dyn_dims(
        2, migraphx::shape::dynamic_dimension{0, max_val});
    expect_shape(migraphx::shape{migraphx::shape::float_type, dyn_dims},
                 migraphx::make_op("allocate", {{"buf_type", migraphx::shape::float_type}}),
                 input);
}

TEST_CASE(allocate_dyn_with_shape_attr)
{
    migraphx::shape input{migraphx::shape::int64_type, {4}};
    migraphx::shape shape_attr{migraphx::shape::float_type,
                               {{1, 4}, {3, 3}, {4, 8, {4, 6}}, {4, 8}, {4, 6}}};
    expect_shape(shape_attr,
                 migraphx::make_op("allocate", {{"shape", migraphx::to_value(shape_attr)}}),
                 input);
}

119
TEST_CASE(allocate_dyn_no_input)
120
121
122
{
    migraphx::shape shape_attr{migraphx::shape::float_type,
                               {{1, 4}, {3, 3}, {4, 8, {4, 6}}, {4, 8}, {4, 6}}};
123
124
    expect_shape(shape_attr,
                 migraphx::make_op("allocate", {{"shape", migraphx::to_value(shape_attr)}}));
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}

TEST_CASE(allocate_shape_and_buf_type_error)
{
    migraphx::shape shape_attr{migraphx::shape::float_type,
                               {{1, 4}, {3, 3}, {4, 8, {4, 6}}, {4, 8}, {4, 6}}};
    throws_shape(migraphx::make_op(
        "allocate",
        {{"shape", migraphx::to_value(shape_attr)}, {"buf_type", migraphx::shape::half_type}}));
}

TEST_CASE(allocate_no_attr_error)
{
    migraphx::shape input{migraphx::shape::int64_type, {4}};
    throws_shape(migraphx::make_op("allocate"), input);
140
141
}

Charlie Lin's avatar
Charlie Lin committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
TEST_CASE(argmax_axis0)
{
    migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {1, 3, 4, 5}},
                 migraphx::make_op("argmax", {{"axis", 0}}),
                 input);
}

TEST_CASE(argmax_axis1)
{
    migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 1, 4, 5}},
                 migraphx::make_op("argmax", {{"axis", 1}}),
                 input);
}

TEST_CASE(argmax_axis2)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 1, 5}},
                 migraphx::make_op("argmax", {{"axis", 2}}),
                 input);
}

TEST_CASE(argmax_axis_neg)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 4, 1}},
                 migraphx::make_op("argmax", {{"axis", -1}}),
                 input);
}

TEST_CASE(argmax_axis_outofbounds)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
    throws_shape(migraphx::make_op("argmax", {{"axis", 4}}), input);
}

TEST_CASE(argmax_dyn0)
{
182
183
184
185
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {{1, 4}, {1, 1}, {4, 4}, {5, 5}}},
                 migraphx::make_op("argmax", {{"axis", 1}}),
                 input);
Charlie Lin's avatar
Charlie Lin committed
186
187
188
189
}

TEST_CASE(argmax_dyn1)
{
190
191
192
193
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 6}, {4, 6}}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {{1, 4}, {3, 3}, {1, 1}, {4, 6}}},
                 migraphx::make_op("argmax", {{"axis", 2}}),
                 input);
Charlie Lin's avatar
Charlie Lin committed
194
195
}

196
197
198
TEST_CASE(binary_dyn_static_error)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 4, 4}};
199
    std::vector<migraphx::shape::dynamic_dimension> b{{1, 1}, {4, 4, {4}}, {4, 4}};
200
201
202
203
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("add"), a_shape, b_shape);
}

Shucai Xiao's avatar
Shucai Xiao committed
204
205
206
207
208
209
TEST_CASE(broadcast)
{
    {
        std::vector<std::size_t> lens{1, 1};
        migraphx::shape input{migraphx::shape::float_type, {1}, {0}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 1}, {0, 0}},
210
                     migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
211
212
213
214
215
216
                     input);
    }

    {
        std::vector<std::size_t> lens{1, 1};
        migraphx::shape input{migraphx::shape::float_type, {2}};
217
        throws_shape(migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", lens}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
218
219
220
221
222
    }

    {
        std::vector<std::size_t> lens{2, 2};
        migraphx::shape input{migraphx::shape::float_type, {1, 2}};
223
        throws_shape(migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", lens}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
224
225
226
227
228
229
    }

    {
        std::vector<std::size_t> lens{3, 2, 4, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 3}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 2, 4, 3}, {0, 0, 3, 1}},
230
                     migraphx::make_op("broadcast", {{"axis", 2}, {"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
231
232
233
234
235
236
                     input);
    }

    {
        std::vector<std::size_t> lens{3, 2, 4, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 4}};
237
        throws_shape(migraphx::make_op("broadcast", {{"axis", 2}, {"out_lens", lens}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
238
239
240
    }
}

241
242
243
244
245
246
247
TEST_CASE(broadcast_axis_out_of_range_error)
{
    std::vector<std::size_t> lens{1, 1};
    migraphx::shape input{migraphx::shape::float_type, {1}, {0}};
    throws_shape(migraphx::make_op("broadcast", {{"axis", 4}, {"out_lens", lens}}), input);
}

248
249
250
251
252
253
254
255
TEST_CASE(broadcast_1in_dyn_error)
{
    // broadcast doesn't support single dynamic shape input
    std::vector<std::size_t> lens{3, 2, 4, 3};
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {2, 2}}};
    throws_shape(migraphx::make_op("broadcast", {{"axis", 2}, {"out_lens", lens}}), input);
}

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
TEST_CASE(broadcast_2in_static_static)
{
    migraphx::shape a_input{migraphx::shape::float_type, {4}, {1}};
    migraphx::shape b_input{migraphx::shape::float_type, {4, 4}, {4, 1}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 4}, {1, 0}},
                 migraphx::make_op("broadcast", {{"axis", 0}}),
                 a_input,
                 b_input);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 4}, {0, 1}},
                 migraphx::make_op("broadcast", {{"axis", 1}}),
                 a_input,
                 b_input);
    throws_shape(migraphx::make_op("broadcast", {{"axis", 2}}), a_input, b_input);
}

TEST_CASE(broadcast_2in_not_matching_error)
{
    migraphx::shape a_input{migraphx::shape::float_type, {4}, {1}};
    migraphx::shape b_input{migraphx::shape::float_type, {2, 2}, {2, 1}};
    throws_shape(migraphx::make_op("broadcast", {{"axis", 1}}), a_input, b_input);
}

TEST_CASE(broadcast_2in_dynamic_s0_error1)
{
    migraphx::shape a_input{migraphx::shape::float_type, {4, 2}, {2, 1}};
281
    migraphx::shape b_input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {2, 2}}};
282
283
284
285
286
    throws_shape(migraphx::make_op("broadcast", {{"axis", 0}}), b_input, a_input);
}

TEST_CASE(broadcast_2in_dynamic_s0_error2)
{
287
    std::vector<migraphx::shape::dynamic_dimension> dd{{4, 4}};
288
289
290
291
292
293
294
295
    migraphx::shape a_input{migraphx::shape::float_type, dd};
    migraphx::shape b_input{migraphx::shape::float_type, {4, 4}, {4, 1}};
    throws_shape(migraphx::make_op("broadcast", {{"axis", 0}}), a_input, b_input);
}

TEST_CASE(broadcast_2in_static_dyn)
{
    migraphx::shape a_input{migraphx::shape::float_type, {4}, {1}};
296
    migraphx::shape b_input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {2, 2}}};
297
    throws_shape(migraphx::make_op("broadcast", {{"axis", 0}}), a_input, b_input);
298
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {2, 2}}},
299
300
301
302
303
304
305
306
307
                 migraphx::make_op("broadcast", {{"axis", 1}}),
                 a_input,
                 b_input);
    throws_shape(migraphx::make_op("broadcast", {{"axis", 2}}), a_input, b_input);
}

TEST_CASE(broadcast_2in_dyn_s0_ndim_greater_than_1_error)
{
    migraphx::shape a_input{migraphx::shape::float_type, {4, 2}};
308
    migraphx::shape b_input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {2, 2}}};
309
310
311
    throws_shape(migraphx::make_op("broadcast", {{"axis", 0}}), a_input, b_input);
}

312
TEST_CASE(conv_2d_0)
313
{
Paul's avatar
Paul committed
314
315
316
    migraphx::shape output{migraphx::shape::float_type, {4, 4, 1, 1}};
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
317
318
319
320
321
    expect_shape(output, migraphx::make_op("convolution"), input, weights);
    throws_shape(migraphx::make_op("convolution"), input);
    throws_shape(
        migraphx::make_op("convolution", {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
        input);
322
}
Paul's avatar
Paul committed
323

324
325
326
TEST_CASE(conv_2d_1)
{
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
Paul's avatar
Paul committed
327
328
    migraphx::shape input2{migraphx::shape::float_type, {3, 3}};
    migraphx::shape weights2{migraphx::shape::float_type, {3, 3}};
329
330
    throws_shape(migraphx::make_op("convolution"), input2, weights2);
    throws_shape(migraphx::make_op("convolution"), input2, weights);
331
}
332

333
334
TEST_CASE(conv_1d)
{
335
336
337
    migraphx::shape output_1d{migraphx::shape::float_type, {4, 4, 1}};
    migraphx::shape input_1d{migraphx::shape::float_type, {4, 3, 3}};
    migraphx::shape weights_1d{migraphx::shape::float_type, {4, 3, 3}};
338
339
340
341
342
    expect_shape(
        output_1d,
        migraphx::make_op("convolution", {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
        input_1d,
        weights_1d);
343
}
344

345
346
347
348
TEST_CASE(conv_channel_mismatch)
{
    migraphx::shape input_1d{migraphx::shape::float_type, {4, 3, 3}};
    migraphx::shape weights_1d = {migraphx::shape::float_type, {4, 8, 3}};
349
    throws_shape(migraphx::make_op("convolution"), input_1d, weights_1d);
350
}
351

352
353
TEST_CASE(conv_3D)
{
354
355
356
    migraphx::shape output_3d{migraphx::shape::float_type, {4, 4, 1, 1, 1}};
    migraphx::shape input_3d{migraphx::shape::float_type, {4, 3, 3, 3, 3}};
    migraphx::shape weights_3d{migraphx::shape::float_type, {4, 3, 3, 3, 3}};
357
358
359
360
361
362
363
364
    expect_shape(
        output_3d,
        migraphx::make_op("convolution",
                          {{"padding", {0, 0, 0}}, {"stride", {1, 1, 1}}, {"dilation", {1, 1, 1}}}),
        input_3d,
        weights_3d);

    throws_shape(migraphx::make_op("convolution"), input_3d, weights_3d);
365
}
366

367
368
TEST_CASE(conv_dyn_batch)
{
369
    migraphx::shape input_dyn_shape{migraphx::shape::float_type,
370
                                    {{1, 100}, {3, 3}, {5, 5}, {5, 5}}};
371
372
    migraphx::shape weights_shape{migraphx::shape::float_type, {1, 3, 3, 3}};
    migraphx::shape output_dyn_shape{migraphx::shape::float_type,
373
                                     {{1, 100}, {1, 1}, {3, 3}, {3, 3}}};
374
375
376
377
378
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input_dyn_shape,
                 weights_shape);
379
}
380

381
382
383
TEST_CASE(conv_dyn_img)
{
    migraphx::shape input_dyn_shape  = {migraphx::shape::float_type,
Lakhinder Walia's avatar
Lakhinder Walia committed
384
                                        {{1, 1}, {3, 3}, {5, 20}, {5, 20}}};
385
386
387
    migraphx::shape weights_shape    = {migraphx::shape::float_type, {1, 3, 3, 3}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 1}, {1, 1}, {3, 18}, {3, 18}}};
388
389
390
391
392
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input_dyn_shape,
                 weights_shape);
393
}
394

395
396
397
398
399
400
TEST_CASE(conv_dyn_weights)
{
    migraphx::shape input_dyn_shape = {migraphx::shape::float_type, {1, 3, 10, 10}};
    migraphx::shape weights_shape = {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 1}, {1, 1}, {7, 9}, {7, 9}}};
401
402
403
404
405
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input_dyn_shape,
                 weights_shape);
406
}
407

408
409
410
411
412
413
414
TEST_CASE(conv_dyn_img_weights)
{
    migraphx::shape input_dyn_shape = {migraphx::shape::float_type,
                                       {{1, 1}, {3, 3}, {5, 20}, {5, 20}}};
    migraphx::shape weights_shape = {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 1}, {1, 1}, {2, 19}, {2, 19}}};
415
416
417
418
419
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input_dyn_shape,
                 weights_shape);
420
}
421

422
423
424
425
426
TEST_CASE(conv_attr_shape_mismatch)
{
    migraphx::shape input_dyn_shape = {migraphx::shape::float_type,
                                       {{1, 100}, {3, 3}, {5, 5}, {5, 5}, {5, 5}}};
    migraphx::shape weights_shape   = {migraphx::shape::float_type, {1, 3, 3, 3, 3}};
427
428
429
430
    throws_shape(migraphx::make_op("convolution",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input_dyn_shape,
                 weights_shape);
431
}
432

433
434
TEST_CASE(conv_autopad_dyn_batch)
{
435
    // auto_pad dynamic batch
436
    migraphx::shape input_dyn_shape  = {migraphx::shape::float_type,
Lakhinder Walia's avatar
Lakhinder Walia committed
437
                                        {{1, 10}, {3, 3}, {5, 5}, {5, 5}}};
438
439
440
    migraphx::shape weights_shape    = {migraphx::shape::float_type, {1, 3, 3, 3}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 10}, {1, 1}, {5, 5}, {5, 5}}};
441
442
443
444
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"stride", {1, 1}},
                                    {"dilation", {1, 1}},
445
                                    {"padding_mode", migraphx::op::padding_mode_t::same_upper}}),
446
447
                 input_dyn_shape,
                 weights_shape);
448
}
449

450
451
TEST_CASE(conv_autopad_dyn_img)
{
452
    // auto_pad dynamic img
453
    migraphx::shape input_dyn_shape  = {migraphx::shape::float_type,
Lakhinder Walia's avatar
Lakhinder Walia committed
454
                                        {{1, 1}, {3, 3}, {5, 10}, {5, 10}}};
455
456
457
    migraphx::shape weights_shape    = {migraphx::shape::float_type, {1, 3, 3, 3}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 1}, {1, 1}, {5, 10}, {5, 10}}};
458
459
460
461
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"stride", {1, 1}},
                                    {"dilation", {1, 1}},
462
                                    {"padding_mode", migraphx::op::padding_mode_t::same_upper}}),
463
464
                 input_dyn_shape,
                 weights_shape);
465
}
466

467
468
469
470
471
472
473
TEST_CASE(conv_autopad_dyn_kernel)
{
    migraphx::shape input_dyn_shape = {migraphx::shape::float_type,
                                       {{1, 1}, {3, 3}, {10, 10}, {10, 10}}};
    migraphx::shape weights_shape = {migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 4}, {2, 4}}};
    migraphx::shape output_dyn_shape = {migraphx::shape::float_type,
                                        {{1, 1}, {1, 1}, {10, 10}, {10, 10}}};
474
475
476
477
    expect_shape(output_dyn_shape,
                 migraphx::make_op("convolution",
                                   {{"stride", {1, 1}},
                                    {"dilation", {1, 1}},
478
                                    {"padding_mode", migraphx::op::padding_mode_t::same_lower}}),
479
480
                 input_dyn_shape,
                 weights_shape);
481
482
}

Shucai Xiao's avatar
Shucai Xiao committed
483
484
485
486
487
488
489
490
491
492
493
TEST_CASE(contiguous_shape)
{
    migraphx::shape output{migraphx::shape::float_type, {2, 2}};
    migraphx::shape input{migraphx::shape::float_type, {2, 2}, {1, 2}};
    expect_shape(output, migraphx::make_op("contiguous"), input);
    throws_shape(migraphx::make_op("contiguous"), input, input);

    migraphx::shape single{migraphx::shape::float_type, {2}};
    expect_shape(single, migraphx::make_op("contiguous"), single);
}

Charlie Lin's avatar
Charlie Lin committed
494
495
TEST_CASE(contiguous_dyn_shape)
{
496
    migraphx::shape s0{migraphx::shape::float_type, {{1, 4}, {2, 2, {2}}}};
Charlie Lin's avatar
Charlie Lin committed
497
498
499
    expect_shape(s0, migraphx::make_op("contiguous"), s0);
}

Shucai Xiao's avatar
Shucai Xiao committed
500
501
TEST_CASE(contiguous_shape_scalar)
{
502
    migraphx::shape output{migraphx::shape::float_type, {1}};
Shucai Xiao's avatar
Shucai Xiao committed
503
504
505
506
    migraphx::shape input{migraphx::shape::float_type};
    expect_shape(output, migraphx::make_op("contiguous"), input);
}

507
508
509
510
511
512
513
TEST_CASE(contiguous_shape_singleton_dim)
{
    migraphx::shape output{migraphx::shape::float_type, {5, 1, 8}, {8, 8, 1}};
    migraphx::shape input{migraphx::shape::float_type, {5, 1, 8}, {8, 4, 1}};
    expect_shape(output, migraphx::make_op("contiguous"), input);
}

514
515
516
517
518
519
520
521
522
523
524
525
526
TEST_CASE(convolution_backwards_1d)
{
    migraphx::shape input_1d{migraphx::shape::float_type, {4, 4, 1}};
    migraphx::shape weights_1d{migraphx::shape::float_type, {4, 3, 3}};
    migraphx::shape output_1d{migraphx::shape::float_type, {4, 3, 3}};
    expect_shape(output_1d,
                 migraphx::make_op("convolution_backwards",
                                   {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
                 input_1d,
                 weights_1d);
}

TEST_CASE(convolution_backwards_2d)
kahmed10's avatar
kahmed10 committed
527
528
{
    migraphx::shape input{migraphx::shape::float_type, {4, 4, 1, 1}};
529
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
kahmed10's avatar
kahmed10 committed
530
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 3, 3}};
531
532
533
534
535
536
537
538
539
540
    expect_shape(output, migraphx::make_op("convolution_backwards"), input, weights);
    throws_shape(migraphx::make_op("convolution_backwards"), input);
    throws_shape(migraphx::make_op("convolution_backwards",
                                   {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
                 input);
}

TEST_CASE(convolution_backwards_1padding)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 4, 1, 1}};
kahmed10's avatar
kahmed10 committed
541
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
542
543
544
545
546
547
548
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 1, 1}};
    expect_shape(output,
                 migraphx::make_op("convolution_backwards",
                                   {{"padding", {1, 1}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input,
                 weights);
}
kahmed10's avatar
kahmed10 committed
549

550
551
552
553
554
555
556
557
558
559
560
TEST_CASE(convolution_backwards_2stride)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 4, 4, 4}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 9, 9}};
    expect_shape(output,
                 migraphx::make_op("convolution_backwards",
                                   {{"padding", {0, 0}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
                 input,
                 weights);
}
kahmed10's avatar
kahmed10 committed
561

562
563
564
565
566
567
568
569
570
571
572
573
574
575
TEST_CASE(convolution_backwards_2dilation)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 4, 4, 4}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 8, 8}};
    expect_shape(output,
                 migraphx::make_op("convolution_backwards",
                                   {{"padding", {0, 0}}, {"stride", {1, 1}}, {"dilation", {2, 2}}}),
                 input,
                 weights);
}

TEST_CASE(convolution_backwards_3d)
{
kahmed10's avatar
kahmed10 committed
576
577
578
    migraphx::shape input_3d{migraphx::shape::float_type, {4, 4, 1, 1, 1}};
    migraphx::shape output_3d{migraphx::shape::float_type, {4, 3, 3, 3, 3}};
    migraphx::shape weights_3d{migraphx::shape::float_type, {4, 3, 3, 3, 3}};
579
580
    expect_shape(
        output_3d,
581
        migraphx::make_op("convolution_backwards",
582
583
584
                          {{"padding", {0, 0, 0}}, {"stride", {1, 1, 1}}, {"dilation", {1, 1, 1}}}),
        input_3d,
        weights_3d);
kahmed10's avatar
kahmed10 committed
585
586
}

587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
TEST_CASE(convolution_backwards_channel_mismatch)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 4, 1, 1}};
    migraphx::shape weights{migraphx::shape::float_type, {3, 3, 3, 3}};
    throws_shape(migraphx::make_op("convolution_backwards"), input, weights);
}

TEST_CASE(convolution_backwards_dyn_batch_2d)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {1, 1}, {1, 1}}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 4}, {3, 3}, {3, 3}, {3, 3}}};
    expect_shape(output, migraphx::make_op("convolution_backwards"), input, weights);
}

TEST_CASE(convolution_backwards_dyn_img_2d)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 1}, {4, 4}, {1, 5}, {1, 5}}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 1}, {3, 3}, {3, 7}, {3, 7}}};
    expect_shape(output, migraphx::make_op("convolution_backwards"), input, weights);
}

TEST_CASE(convolution_backwards_dyn_kernel_2d)
{
    migraphx::shape input{migraphx::shape::float_type, {1, 4, 1, 1}};
    migraphx::shape weights{migraphx::shape::float_type, {{4, 4}, {3, 3}, {2, 6}, {2, 6}}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 1}, {3, 3}, {2, 6}, {2, 6}}};
    expect_shape(output, migraphx::make_op("convolution_backwards"), input, weights);
}

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
643
644
645
646
647
648
649
650
TEST_CASE(dimensions_of0)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 2, 1}};
    migraphx::shape output{migraphx::shape::int64_type, {4}};
    expect_shape(output, migraphx::make_op("dimensions_of", {{"end", 4}}), input);
}

TEST_CASE(dimensions_of1)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 2, 1}};
    migraphx::shape output{migraphx::shape::int64_type, {2}};
    expect_shape(output, migraphx::make_op("dimensions_of", {{"start", 1}, {"end", 3}}), input);
}

TEST_CASE(dimensions_of2)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4, {2}}, {2, 4}, {2, 4}, {1, 6, {2}}}};
    migraphx::shape output{migraphx::shape::int64_type, {2}};
    expect_shape(output, migraphx::make_op("dimensions_of", {{"start", 1}, {"end", 3}}), input);
}

TEST_CASE(dimensions_of_error0)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4, {2}}, {2, 4}}};
    throws_shape(migraphx::make_op("dimensions_of", {{"start", 3}, {"end", 3}}), input);
}

TEST_CASE(dimensions_of_error1)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4, {2}}, {2, 4}}};
    throws_shape(migraphx::make_op("dimensions_of", {{"start", 3}, {"end", 0}}), input);
}

Charlie Lin's avatar
Charlie Lin committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
TEST_CASE(dot_ndim_error0)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_ndim_error1)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 2}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_ndim_error2)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_ndim_error3)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {6, 5, 4}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_ndim_error4)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 1, 5, 7}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_mismatch_inner_error0)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {10, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_mismatch_inner_error1)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {4, 6}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_mismatch_inner_error2)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {4, 4}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_mismatch_inner_error3)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 1, 4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 2, 5, 7}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_mismatch_outer_error)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 4, 6}};
    migraphx::shape s_m2{migraphx::shape::float_type, {2, 5, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_2D_test0)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 8}};
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {4, 8}}, migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_2D_test1)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 4}};
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {1, 4}}, migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_2D_test2)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 8}};
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {4, 8}}, migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_2D_test3)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 1}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 1}};
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {1, 1}}, migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_3D_test0)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 5, 8}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 4, 8}},
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_3D_test_1)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {6, 1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {6, 5, 4}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {6, 1, 4}},
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_3D_test2)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 4, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 5, 7}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 4, 7}},
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_4D_test)
{
    migraphx::shape s_m1{migraphx::shape::float_type, {1, 6, 1, 5}};
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 6, 5, 4}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 6, 1, 4}},
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_dyn_static_test0)
{
795
    migraphx::shape s_m1{migraphx::shape::float_type, {{1, 4}, {5, 5}}};
Charlie Lin's avatar
Charlie Lin committed
796
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 8}};
797
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {8, 8}}},
Charlie Lin's avatar
Charlie Lin committed
798
799
800
801
802
803
804
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_dyn_static_mismatch_error)
{
805
    migraphx::shape s_m1{migraphx::shape::float_type, {{1, 4}, {3, 3}, {5, 5}, {5, 5}}};
Charlie Lin's avatar
Charlie Lin committed
806
807
808
809
810
811
    migraphx::shape s_m2{migraphx::shape::float_type, {5, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_dyn_dyn_test0)
{
812
813
814
    migraphx::shape s_m1{migraphx::shape::float_type, {{1, 4}, {5, 5}}};
    migraphx::shape s_m2{migraphx::shape::float_type, {{5, 5}, {6, 8, {8}}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {6, 8, {8}}}},
Charlie Lin's avatar
Charlie Lin committed
815
816
817
818
819
820
821
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_dyn_dyn_test1)
{
822
823
824
    migraphx::shape s_m1{migraphx::shape::float_type, {{1, 4}, {4, 5, {5}}}};
    migraphx::shape s_m2{migraphx::shape::float_type, {{4, 5, {5}}, {6, 8, {8}}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {6, 8, {8}}}},
Charlie Lin's avatar
Charlie Lin committed
825
826
827
828
829
830
831
                 migraphx::make_op("dot"),
                 s_m1,
                 s_m2);
}

TEST_CASE(dot_dyn_mismatch_test0)
{
832
    migraphx::shape s_m1{migraphx::shape::float_type, {{1, 4}, {5, 5}, {5, 5}}};
Charlie Lin's avatar
Charlie Lin committed
833
834
835
836
837
838
    migraphx::shape s_m2{migraphx::shape::float_type, {1, 5, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

TEST_CASE(dot_dyn_mismatch_test1)
{
839
    migraphx::shape s_m1{migraphx::shape::float_type, {{4, 4}, {5, 5}, {2, 5}}};
Charlie Lin's avatar
Charlie Lin committed
840
841
842
843
    migraphx::shape s_m2{migraphx::shape::float_type, {4, 5, 8}};
    throws_shape(migraphx::make_op("dot"), s_m1, s_m2);
}

Paul's avatar
Paul committed
844
TEST_CASE(flatten_shape)
845
{
Paul's avatar
Paul committed
846
847
    migraphx::shape input{migraphx::shape::float_type, {2, 4, 6, 8}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 2 * 4 * 6 * 8}},
848
                 migraphx::make_op("flatten", {{"axis", 0}}),
Scott Thornton's avatar
Scott Thornton committed
849
                 input);
850
    expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 2 * 4 * 6 * 8}},
851
                 migraphx::make_op("flatten", {{"axis", -4}}),
852
                 input);
Paul's avatar
Paul committed
853
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 4 * 6 * 8}},
854
                 migraphx::make_op("flatten", {{"axis", 1}}),
Paul's avatar
Paul committed
855
                 input);
856
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 4 * 6 * 8}},
857
                 migraphx::make_op("flatten", {{"axis", -3}}),
858
                 input);
Paul's avatar
Paul committed
859
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2 * 4, 6 * 8}},
860
                 migraphx::make_op("flatten", {{"axis", 2}}),
Paul's avatar
Paul committed
861
862
                 input);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2 * 4 * 6, 8}},
863
                 migraphx::make_op("flatten", {{"axis", 3}}),
Paul's avatar
Paul committed
864
                 input);
Paul's avatar
Paul committed
865
    expect_shape(migraphx::shape{migraphx::shape::float_type, {2 * 4 * 6 * 8, 1}},
866
                 migraphx::make_op("flatten", {{"axis", 4}}),
Scott Thornton's avatar
Scott Thornton committed
867
                 input);
868
869
    throws_shape(migraphx::make_op("flatten", {{"axis", 5}}), input);
    throws_shape(migraphx::make_op("flatten", {{"axis", -5}}), input);
870
871
}

Charlie Lin's avatar
Charlie Lin committed
872
873
TEST_CASE(flatten_dyn_axis0)
{
874
875
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {6, 6}, {8, 8}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 1}, {192, 768}}},
Charlie Lin's avatar
Charlie Lin committed
876
877
                 migraphx::make_op("flatten", {{"axis", 0}}),
                 input);
878
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 1}, {192, 768}}},
Charlie Lin's avatar
Charlie Lin committed
879
880
881
882
883
884
885
                 migraphx::make_op("flatten", {{"axis", -4}}),
                 input);
}

TEST_CASE(flatten_dyn_axis1)
{
    migraphx::shape input{migraphx::shape::float_type,
886
                          {{2, 2, {2}}, {4, 4}, {4, 6, {5}}, {4, 6, {5}}}};
Charlie Lin's avatar
Charlie Lin committed
887
    expect_shape(
888
        migraphx::shape{migraphx::shape::float_type, {{2, 2, {2}}, {4 * 4 * 4, 4 * 6 * 6}}},
Charlie Lin's avatar
Charlie Lin committed
889
890
891
        migraphx::make_op("flatten", {{"axis", 1}}),
        input);
    expect_shape(
892
        migraphx::shape{migraphx::shape::float_type, {{2, 2, {2}}, {4 * 4 * 4, 4 * 6 * 6}}},
Charlie Lin's avatar
Charlie Lin committed
893
894
895
896
897
898
899
        migraphx::make_op("flatten", {{"axis", -3}}),
        input);
}

TEST_CASE(flatten_dyn_axis2)
{
    migraphx::shape input{migraphx::shape::float_type,
900
901
902
903
                          {{2, 2, {2}}, {4, 4}, {4, 6, {5}}, {4, 6, {5}}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{2 * 4, 2 * 4}, {4 * 4, 6 * 6}}},
                 migraphx::make_op("flatten", {{"axis", 2}}),
                 input);
Charlie Lin's avatar
Charlie Lin committed
904
905
906
907
}

TEST_CASE(flatten_dyn_axis3)
{
908
909
910
911
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {6, 6}, {8, 8}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1 * 4 * 6, 4 * 4 * 6}, {8, 8}}},
                 migraphx::make_op("flatten", {{"axis", 3}}),
                 input);
Charlie Lin's avatar
Charlie Lin committed
912
913
914
915
}

TEST_CASE(flatten_dyn_axis4)
{
916
917
918
919
920
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {6, 6}, {8, 8}}};
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {{1 * 4 * 6 * 8, 4 * 4 * 6 * 8}, {1, 1}}},
        migraphx::make_op("flatten", {{"axis", 4}}),
        input);
Charlie Lin's avatar
Charlie Lin committed
921
922
}

Charlie Lin's avatar
Charlie Lin committed
923
924
925
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(fill_static_int)
{
    migraphx::shape default_value{migraphx::shape::int64_type, {1}, {0}};
    migraphx::shape data{migraphx::shape::int64_type, {3, 4, 4}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type, {3, 4, 4}},
                 migraphx::make_op("fill"),
                 default_value,
                 data);
}

TEST_CASE(fill_static_float)
{
    migraphx::shape default_value{migraphx::shape::float_type, {1}, {0}};
    migraphx::shape data{migraphx::shape::float_type, {4, 8}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 8}},
                 migraphx::make_op("fill"),
                 default_value,
                 data);
}

TEST_CASE(fill_dyn_int)
{
    migraphx::shape default_value{migraphx::shape::int64_type, {1}, {0}};
    migraphx::shape data{migraphx::shape::int64_type,
                         {{1, 4}, {4, 8, {4, 6, 8}}, {4, 8, {4, 6, 8}}}};
    expect_shape(migraphx::shape{migraphx::shape::int64_type,
                                 {{1, 4}, {4, 8, {4, 6, 8}}, {4, 8, {4, 6, 8}}}},
                 migraphx::make_op("fill"),
                 default_value,
                 data);
}

TEST_CASE(fill_dyn_float)
{
    migraphx::shape default_value{migraphx::shape::float_type, {1}, {0}};
    migraphx::shape data{migraphx::shape::float_type,
                         {{1, 4}, {4, 8, {4, 6, 8}}, {4, 8, {4, 6, 8}}}};
    expect_shape(migraphx::shape{migraphx::shape::float_type,
                                 {{1, 4}, {4, 8, {4, 6, 8}}, {4, 8, {4, 6, 8}}}},
                 migraphx::make_op("fill"),
                 default_value,
                 data);
}

Shucai Xiao's avatar
Shucai Xiao committed
967
TEST_CASE(gather)
Scott Thornton's avatar
Scott Thornton committed
968
{
Shucai Xiao's avatar
Shucai Xiao committed
969
970
971
972
973
974
975
976
977
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
        int axis = 1;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 2, 3, 4, 5}},
                     migraphx::make_op("gather", {{"axis", axis}}),
                     input,
                     indices);
    }
Scott Thornton's avatar
Scott Thornton committed
978
979

    {
Shucai Xiao's avatar
Shucai Xiao committed
980
981
982
983
984
985
986
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
        int axis = -4;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 3, 4, 5}},
                     migraphx::make_op("gather", {{"axis", axis}}),
                     input,
                     indices);
Scott Thornton's avatar
Scott Thornton committed
987
    }
Shucai Xiao's avatar
Shucai Xiao committed
988

Scott Thornton's avatar
Scott Thornton committed
989
    {
Shucai Xiao's avatar
Shucai Xiao committed
990
991
992
993
994
995
996
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {1}};
        int axis = -4;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 5}},
                     migraphx::make_op("gather", {{"axis", axis}}),
                     input,
                     indices);
Scott Thornton's avatar
Scott Thornton committed
997
    }
Shucai Xiao's avatar
Shucai Xiao committed
998

Scott Thornton's avatar
Scott Thornton committed
999
    {
Shucai Xiao's avatar
Shucai Xiao committed
1000
1001
1002
1003
1004
1005
1006
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type};
        int axis = -4;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}},
                     migraphx::make_op("gather", {{"axis", axis}}),
                     input,
                     indices);
Scott Thornton's avatar
Scott Thornton committed
1007
    }
Shucai Xiao's avatar
Shucai Xiao committed
1008

1009
1010
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
1011
        migraphx::shape indices{migraphx::shape::int32_type};
1012
1013
        int axis = 3;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4}},
1014
                     migraphx::make_op("gather", {{"axis", axis}}),
1015
1016
1017
1018
1019
1020
                     input,
                     indices);
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {3}};
1021
        migraphx::shape indices{migraphx::shape::int32_type};
1022
        int axis = 0;
1023
        expect_shape(migraphx::shape{migraphx::shape::float_type},
1024
                     migraphx::make_op("gather", {{"axis", axis}}),
1025
1026
1027
1028
1029
1030
1031
1032
1033
                     input,
                     indices);
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {3}};
        migraphx::shape indices{migraphx::shape::int32_type, {1}};
        int axis = 0;
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1}},
1034
                     migraphx::make_op("gather", {{"axis", axis}}),
1035
1036
1037
1038
                     input,
                     indices);
    }

1039
1040
1041
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
1042
        int axis = 4;
1043
        throws_shape(migraphx::make_op("gather", {{"axis", axis}}), input, indices);
1044
    }
1045
1046
1047
1048
1049

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
        int axis = -5;
1050
        throws_shape(migraphx::make_op("gather", {{"axis", axis}}), input, indices);
1051
    }
1052
1053
}

Brian Pickrell's avatar
Brian Pickrell committed
1054
1055
1056
1057
TEST_CASE(gather_dyn0)
{
    // Insert dynamic index into dynamic shape
    migraphx::shape input{migraphx::shape::float_type,
1058
1059
                          {{2, 3, {2}}, {3, 4, {3}}, {6, 9, {7}}, {12, 14, {13}}}};
    migraphx::shape indices{migraphx::shape::int32_type, {{2, 7, {3}}, {3, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
1060
1061
    int axis = 1;
    expect_shape(migraphx::shape{migraphx::shape::float_type,
1062
                                 {{2, 3, {2}}, {2, 7, {3}}, {3, 3}, {6, 9, {7}}, {12, 14, {13}}}},
Brian Pickrell's avatar
Brian Pickrell committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
                 migraphx::make_op("gather", {{"axis", axis}}),
                 input,
                 indices);
}

TEST_CASE(gather_dyn1)
{
    // Insert static index into dynamic shape
    migraphx::shape input{migraphx::shape::float_type,
1072
                          {{2, 3, {2}}, {3, 4, {3}}, {6, 9, {7}}, {12, 14, {13}}}};
Brian Pickrell's avatar
Brian Pickrell committed
1073
1074
1075
    migraphx::shape indices{migraphx::shape::int32_type, {2, 3}};
    int axis = 1;
    expect_shape(migraphx::shape{migraphx::shape::float_type,
1076
                                 {{2, 3, {2}}, {2, 2}, {3, 3}, {6, 9, {7}}, {12, 14, {13}}}},
Brian Pickrell's avatar
Brian Pickrell committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
                 migraphx::make_op("gather", {{"axis", axis}}),
                 input,
                 indices);
}

TEST_CASE(gather_dyn2)
{
    // Insert scalar (static) index into dynamic shape
    migraphx::shape input{migraphx::shape::float_type,
1086
                          {{2, 3, {2}}, {3, 4, {3}}, {6, 9, {7}}, {12, 14, {13}}}};
Brian Pickrell's avatar
Brian Pickrell committed
1087
1088
1089

    std::vector<std::size_t> mins;
    std::vector<std::size_t> maxes;
1090
    std::vector<std::set<std::size_t>> opts;
Brian Pickrell's avatar
Brian Pickrell committed
1091
1092
    migraphx::shape indices{migraphx::shape::int32_type, mins, maxes, opts};
    int axis = 1;
1093
1094
1095
1096
1097
    expect_shape(
        migraphx::shape{migraphx::shape::float_type, {{2, 3, {2}}, {6, 9, {7}}, {12, 14, {13}}}},
        migraphx::make_op("gather", {{"axis", axis}}),
        input,
        indices);
Brian Pickrell's avatar
Brian Pickrell committed
1098
1099
1100
1101
1102
1103
}

TEST_CASE(gather_dyn3)
{
    // Insert dynamic index into static shape, axis 1
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 6, 12}};
1104
    migraphx::shape indices{migraphx::shape::int32_type, {{2, 3, {2}}, {3, 4, {3}}}};
Brian Pickrell's avatar
Brian Pickrell committed
1105
1106
    int axis = 1;
    expect_shape(migraphx::shape{migraphx::shape::float_type,
1107
                                 {{2, 2}, {2, 3, {2}}, {3, 4, {3}}, {6, 6}, {12, 12}}},
Brian Pickrell's avatar
Brian Pickrell committed
1108
1109
1110
1111
1112
1113
1114
1115
1116
                 migraphx::make_op("gather", {{"axis", axis}}),
                 input,
                 indices);
}

TEST_CASE(gather_dyn4)
{
    // Insert dynamic index into static shape, axis 0
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 6, 12}};
1117
    migraphx::shape indices{migraphx::shape::int32_type, {{2, 3, {2}}, {3, 4, {3}}}};
Brian Pickrell's avatar
Brian Pickrell committed
1118
1119
    int axis = 0;
    expect_shape(migraphx::shape{migraphx::shape::float_type,
1120
                                 {{2, 3, {2}}, {3, 4, {3}}, {3, 3}, {6, 6}, {12, 12}}},
Brian Pickrell's avatar
Brian Pickrell committed
1121
1122
1123
1124
1125
                 migraphx::make_op("gather", {{"axis", axis}}),
                 input,
                 indices);
}

Shucai Xiao's avatar
Shucai Xiao committed
1126
TEST_CASE(get_tuple_elem_test)
1127
{
Shucai Xiao's avatar
Shucai Xiao committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
    migraphx::shape s0{migraphx::shape::bool_type, {1, 1}};
    migraphx::shape s1{migraphx::shape::float_type, {2, 3}};
    migraphx::shape s2{migraphx::shape::int32_type, {5, 6}};
    migraphx::shape s_tuple({s0, s1, s2});

    expect_shape(s0, migraphx::make_op("get_tuple_elem", {{"index", 0}}), s_tuple);
    expect_shape(s1, migraphx::make_op("get_tuple_elem", {{"index", 1}}), s_tuple);
    expect_shape(s2, migraphx::make_op("get_tuple_elem", {{"index", 2}}), s_tuple);
    throws_shape(migraphx::make_op("get_tuple_elem", {{"index", 3}}), s_tuple);
    throws_shape(migraphx::make_op("get_tuple_elem", {{"index", 0}}), s0);
    throws_shape(migraphx::make_op("get_tuple_elem", {{"index", 1}}), s1);
    throws_shape(migraphx::make_op("get_tuple_elem", {{"index", 0}}), s2);
1140
1141
}

Shucai Xiao's avatar
Shucai Xiao committed
1142
TEST_CASE(gru)
1143
{
Shucai Xiao's avatar
Shucai Xiao committed
1144
1145
1146
1147
1148
1149
1150
    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;
1151

Shucai Xiao's avatar
Shucai Xiao committed
1152
1153
1154
1155
1156
1157
1158
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
1159

Shucai Xiao's avatar
Shucai Xiao committed
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }
1176

Shucai Xiao's avatar
Shucai Xiao committed
1177
1178
1179
1180
1181
1182
1183
    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;
1184

Shucai Xiao's avatar
Shucai Xiao committed
1185
1186
1187
1188
1189
1190
1191
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
1192

Shucai Xiao's avatar
Shucai Xiao committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::reverse)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }
1209

Shucai Xiao's avatar
Shucai Xiao committed
1210
1211
1212
1213
1214
1215
1216
    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;
1217

Shucai Xiao's avatar
Shucai Xiao committed
1218
1219
1220
1221
1222
1223
1224
        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
1225

Shucai Xiao's avatar
Shucai Xiao committed
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size + 1},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "gru",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }
1335
1336
}

Shucai Xiao's avatar
Shucai Xiao committed
1337
TEST_CASE(inconsistent_attr_shape)
1338
{
Shucai Xiao's avatar
Shucai Xiao committed
1339
1340
1341
1342
1343
1344
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape weights{migraphx::shape::float_type, {4, 3, 3, 3}};
    throws_shape(migraphx::make_op("convolution",
                                   {{"padding", {1, 1}}, {"stride", {2}}, {"dilation", {3, 3, 3}}}),
                 input,
                 weights);
1345
    throws_shape(migraphx::make_op("convolution_backwards",
Shucai Xiao's avatar
Shucai Xiao committed
1346
1347
1348
                                   {{"padding", {1, 1}}, {"stride", {2}}, {"dilation", {3, 3, 3}}}),
                 input,
                 weights);
1349
1350
1351
1352
1353
1354
    throws_shape(migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {1}},
                                    {"stride", {0}},
                                    {"lengths", {1, 1}}}),
                 input);
1355
1356
}

1357
void test_softmax_variations(const std::string& name)
Shucai Xiao's avatar
Shucai Xiao committed
1358
{
1359
1360
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
1361
1362
1363
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}},
                     migraphx::make_op(name, {{"axis", 0}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
1364
1365
1366
1367
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
1368
1369
1370
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}},
                     migraphx::make_op(name, {{"axis", 1}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
1371
1372
1373
1374
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
1375
1376
1377
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}},
                     migraphx::make_op(name, {{"axis", 2}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
1378
1379
1380
1381
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
1382
1383
1384
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 5}},
                     migraphx::make_op(name, {{"axis", 3}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
1385
1386
1387
1388
1389
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        int axis = 4;
1390
        throws_shape(migraphx::make_op(name, {{"axis", axis}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
1391
1392
    }
}
1393
1394
1395
TEST_CASE(logsoftmax) { test_softmax_variations("logsoftmax"); }

TEST_CASE(softmax) { test_softmax_variations("softmax"); }
Shucai Xiao's avatar
Shucai Xiao committed
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458

TEST_CASE(lstm)
{
    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};

        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape);
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::reverse)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
1459
1460
1461
    }

    {
Shucai Xiao's avatar
Shucai Xiao committed
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

Shucai Xiao's avatar
Shucai Xiao committed
1477
        expect_shape(
Shucai Xiao's avatar
Shucai Xiao committed
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
Shucai Xiao's avatar
Shucai Xiao committed
1492
    }
Shucai Xiao's avatar
Shucai Xiao committed
1493

Shucai Xiao's avatar
Shucai Xiao committed
1494
    {
Shucai Xiao's avatar
Shucai Xiao committed
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size + 1},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
Shucai Xiao's avatar
Shucai Xiao committed
1523
    }
Shucai Xiao's avatar
Shucai Xiao committed
1524

1525
    {
Shucai Xiao's avatar
Shucai Xiao committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
1554
    }
Shucai Xiao's avatar
Shucai Xiao committed
1555

1556
    {
Shucai Xiao's avatar
Shucai Xiao committed
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape w_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type,
                                {num_dirct, 3 * hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 6 * hidden_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};

        throws_shape(
            migraphx::make_op(
                "lstm",
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
1585
1586
1587
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
1588
TEST_CASE(multibroadcast)
1589
1590
{
    {
Shucai Xiao's avatar
Shucai Xiao committed
1591
1592
1593
        std::vector<std::size_t> lens{4, 2, 5, 3};
        migraphx::shape input{migraphx::shape::float_type, {2, 1, 3}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 3, 0, 1}},
1594
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1595
                     input);
1596
1597
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1598
1599
1600
        std::vector<std::size_t> lens{4, 2, 5, 3};
        migraphx::shape input{migraphx::shape::float_type, {2, 1, 1}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 1, 0, 0}},
1601
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1602
                     input);
1603
1604
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1605
1606
1607
        std::vector<std::size_t> lens{4, 2, 5, 3};
        migraphx::shape input{migraphx::shape::float_type, {5, 1}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 0, 1, 0}},
1608
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1609
                     input);
1610
1611
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1612
1613
1614
        std::vector<std::size_t> lens{4, 2, 5, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {1, 0, 0, 0}},
1615
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1616
                     input);
1617
1618
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1619
1620
1621
        std::vector<std::size_t> lens{4, 2, 5, 3};
        migraphx::shape input{migraphx::shape::float_type, {3}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 0, 0, 1}},
1622
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1623
                     input);
1624
    }
1625
    {
Shucai Xiao's avatar
Shucai Xiao committed
1626
1627
1628
        std::vector<std::size_t> lens{4, 4, 1, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 1, 3}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {0, 3, 3, 1}},
1629
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1630
                     input);
1631
1632
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1633
1634
1635
        std::vector<std::size_t> lens{4, 1, 1, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}};
        expect_shape(migraphx::shape{migraphx::shape::float_type, lens, {1, 1, 1, 0}},
1636
                     migraphx::make_op("multibroadcast", {{"out_lens", lens}}),
Shucai Xiao's avatar
Shucai Xiao committed
1637
                     input);
1638
1639
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1640
1641
        std::vector<std::size_t> lens{4, 1, 3};
        migraphx::shape input{migraphx::shape::float_type, {4, 1, 1, 1}};
1642
        throws_shape(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), input);
1643
1644
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1645
        std::vector<std::size_t> lens{4, 1, 3};
Charlie Lin's avatar
Charlie Lin committed
1646
1647
        std::vector<std::size_t> empt = {};
        migraphx::shape input{migraphx::shape::float_type, empt};
1648
        throws_shape(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), input);
1649
1650
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
1651
1652
        std::vector<std::size_t> lens{2, 3, 4, 5};
        migraphx::shape input{migraphx::shape::float_type, {3, 4}};
1653
        throws_shape(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
1654
1655
1656
1657
    }
    {
        std::vector<std::size_t> lens{2, 3, 4, 5};
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4}};
1658
        throws_shape(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), input);
1659
1660
1661
    }
}

1662
1663
1664
1665
1666
1667
1668
1669
TEST_CASE(multibroadcast_1in_dyn_error_0)
{
    // multibroadcast doesn't support single dynamic shape input
    std::vector<std::size_t> lens{4, 4, 1, 3};
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {4, 4}}};
    throws_shape(migraphx::make_op("multibroadcast", {{"out_lens", lens}}), input);
}

1670
1671
1672
TEST_CASE(multibroadcast_2in_static_dyn0)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {4, 4}};
1673
    std::vector<migraphx::shape::dynamic_dimension> b{{1, 4}, {4, 4, {4}}, {4, 4}};
1674
    migraphx::shape b_shape{migraphx::shape::float_type, b};
1675
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {4, 4}}},
1676
1677
1678
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
1679
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}, {4, 4}}},
1680
1681
1682
1683
1684
1685
1686
1687
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_dyn1)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 6}};
1688
    std::vector<migraphx::shape::dynamic_dimension> b{{8, 8}, {6, 6}};
1689
    migraphx::shape b_shape{migraphx::shape::float_type, b};
1690
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{8, 8}, {6, 6}}},
1691
1692
1693
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
1694
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{8, 8}, {6, 6}}},
1695
1696
1697
1698
1699
1700
1701
1702
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_dyn2)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 6}};
1703
    std::vector<migraphx::shape::dynamic_dimension> b{{8, 8}, {6, 6}};
1704
    migraphx::shape b_shape{migraphx::shape::float_type, b};
1705
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{8, 8}, {6, 6}}},
1706
1707
1708
                 migraphx::make_op("multibroadcast", {{"out_dyn_dims", migraphx::to_value(b)}}),
                 a_shape,
                 b_shape);
1709
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{8, 8}, {6, 6}}},
1710
1711
1712
1713
1714
1715
1716
1717
1718
                 migraphx::make_op("multibroadcast", {{"out_dyn_dims", migraphx::to_value(b)}}),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_dyn_error0)
{
    // doesn't match on first dimension
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
1719
    std::vector<migraphx::shape::dynamic_dimension> b{{1, 3}, {6, 6}};
1720
1721
1722
1723
1724
1725
1726
1727
1728
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

TEST_CASE(multibroadcast_2in_static_dyn_error1)
{
    // doesn't match on first dimension
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
1729
    std::vector<migraphx::shape::dynamic_dimension> b{{1, 4}, {6, 6}};
1730
1731
1732
1733
1734
1735
1736
1737
1738
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

TEST_CASE(multibroadcast_2in_static_dyn_error2)
{
    // doesn't match on first dimension
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
1739
    std::vector<migraphx::shape::dynamic_dimension> b{{1, 2}, {6, 6}};
1740
1741
1742
1743
1744
1745
1746
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

TEST_CASE(multibroadcast_2in_dyn_dyn0)
{
1747
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
1748
    migraphx::shape a_shape{migraphx::shape::float_type, a};
1749
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 4, {2}}, {2, 4}};
1750
    migraphx::shape b_shape{migraphx::shape::float_type, b};
1751
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 4, {2}}, {2, 4}}},
1752
1753
1754
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
1755
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 4, {2}}, {2, 4}}},
1756
1757
1758
1759
1760
1761
1762
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_dyn_dyn1)
{
1763
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
1764
    migraphx::shape a_shape{migraphx::shape::float_type, a};
1765
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 4, {2}}, {2, 4}};
1766
    migraphx::shape b_shape{migraphx::shape::float_type, b};
1767
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 4, {2}}, {2, 4}}},
1768
1769
1770
                 migraphx::make_op("multibroadcast", {{"out_dyn_dims", migraphx::to_value(a)}}),
                 a_shape,
                 b_shape);
1771
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {2, 4, {2}}, {2, 4}}},
1772
1773
1774
1775
1776
1777
1778
1779
                 migraphx::make_op("multibroadcast", {{"out_dyn_dims", migraphx::to_value(a)}}),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_dyn_dyn_error0)
{
    // max doesn't match on second dimension of a
1780
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
1781
    migraphx::shape a_shape{migraphx::shape::float_type, a};
1782
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 5, {2}}, {2, 4}};
1783
1784
1785
1786
1787
1788
1789
1790
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

TEST_CASE(multibroadcast_2in_dyn_dyn_error1)
{
    // opt doesn't match on second dimension of a
1791
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
1792
    migraphx::shape a_shape{migraphx::shape::float_type, a};
1793
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 4, {3}}, {2, 4}};
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

TEST_CASE(multibroadcast_2in_static_static0)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
    migraphx::shape b_shape{migraphx::shape::float_type, {3, 6}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 6}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 6}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_static1)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {1, 8}};
    migraphx::shape b_shape{migraphx::shape::float_type, {4, 8}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 8}, {0, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 8}, {8, 1}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_static2)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {8}};
    migraphx::shape b_shape{migraphx::shape::float_type, {4, 4, 1}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 4, 8}, {0, 0, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {4, 4, 8}, {4, 1, 0}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_static3)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape b_shape{migraphx::shape::float_type, {4, 1}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 4, 4}, {16, 4, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 4, 4}, {0, 1, 0}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_static4)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 1, 4}};
    migraphx::shape b_shape{migraphx::shape::float_type, {4, 1}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 4, 4}, {4, 0, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {3, 4, 4}, {0, 1, 0}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape);
}

TEST_CASE(multibroadcast_2in_static_static_error0)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape b_shape{migraphx::shape::float_type, {4, 3}};
    throws_shape(migraphx::make_op("multibroadcast"), a_shape, b_shape);
    throws_shape(migraphx::make_op("multibroadcast"), b_shape, a_shape);
}

1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
TEST_CASE(multibroadcast_3in_static)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
    migraphx::shape b_shape{migraphx::shape::float_type, {1, 2, 3, 6}};
    migraphx::shape c_shape{migraphx::shape::float_type, {5, 1, 1, 1}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {0, 0, 6, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape,
                 c_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {0, 18, 6, 1}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape,
                 c_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {1, 0, 0, 0}},
                 migraphx::make_op("multibroadcast"),
                 c_shape,
                 a_shape,
                 b_shape);
}

TEST_CASE(multibroadcast_4in_static)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {3, 6}};
    migraphx::shape b_shape{migraphx::shape::float_type, {2, 3, 6}};
    migraphx::shape c_shape{migraphx::shape::float_type, {5, 1, 1, 1}};
    migraphx::shape d_shape{migraphx::shape::float_type, {6}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {0, 0, 6, 1}},
                 migraphx::make_op("multibroadcast"),
                 a_shape,
                 b_shape,
                 c_shape,
                 d_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {0, 18, 6, 1}},
                 migraphx::make_op("multibroadcast"),
                 b_shape,
                 a_shape,
                 c_shape,
                 d_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {1, 0, 0, 0}},
                 migraphx::make_op("multibroadcast"),
                 c_shape,
                 a_shape,
                 b_shape,
                 d_shape);
    expect_shape(migraphx::shape{migraphx::shape::float_type, {5, 2, 3, 6}, {0, 0, 0, 1}},
                 migraphx::make_op("multibroadcast"),
                 d_shape,
                 a_shape,
                 b_shape,
                 c_shape);
}

TEST_CASE(multibroadcast_3in_dyn_static)
{
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
    migraphx::shape a_shape{migraphx::shape::float_type, a};
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 4, {2}}, {2, 4}};
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    migraphx::shape c_shape{migraphx::shape::float_type, {5, 1, 1, 1}};
    migraphx::shape expected_shape{migraphx::shape::float_type,
                                   {{5, 5}, {1, 4}, {2, 4, {2}}, {2, 4}}};
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), a_shape, b_shape, c_shape);
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), b_shape, a_shape, c_shape);
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), c_shape, a_shape, b_shape);
}

TEST_CASE(multibroadcast_3in_dyn_dyn)
{
    std::vector<migraphx::shape::dynamic_dimension> a{{1, 4}, {2, 4, {2}}, {2, 4}};
    migraphx::shape a_shape{migraphx::shape::float_type, a};
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 4, {2}}, {2, 4}};
    migraphx::shape b_shape{migraphx::shape::float_type, b};
    std::vector<migraphx::shape::dynamic_dimension> c{{1, 5, {1, 5}}, {1, 1}, {2, 4, {2}}, {2, 4}};
    migraphx::shape c_shape{migraphx::shape::float_type, c};
    migraphx::shape expected_shape{migraphx::shape::float_type,
                                   {{1, 5, {1, 5}}, {1, 4}, {2, 4, {2}}, {2, 4}}};
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), a_shape, b_shape, c_shape);
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), b_shape, a_shape, c_shape);
    expect_shape(expected_shape, migraphx::make_op("multibroadcast"), c_shape, a_shape, b_shape);
}

Brian Pickrell's avatar
Brian Pickrell committed
1960
TEST_CASE(multinomial_bool_type)
turneram's avatar
turneram committed
1961
{
Brian Pickrell's avatar
Brian Pickrell committed
1962
1963
    migraphx::shape s1{migraphx::shape::float_type, {1, 2}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 4}};
turneram's avatar
turneram committed
1964
1965
    int dtype = 0;

Brian Pickrell's avatar
Brian Pickrell committed
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
    throws_shape(migraphx::make_op("multinomial", {{"dtype", dtype}}), s1, s2);
}

TEST_CASE(multinomial)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 2}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 4}};
    migraphx::shape s3{migraphx::shape::float_type, {1, 4}};
    int dtype = 2;

    expect_shape(s3, migraphx::make_op("multinomial", {{"dtype", dtype}}), s1, s2);
}

TEST_CASE(multinomial_0size_input)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 2}};
    migraphx::shape s2{migraphx::shape::float_type, {}};
    int dtype = 2;

    throws_shape(migraphx::make_op("multinomial", {{"dtype", dtype}}), s1, s2);
}

TEST_CASE(multinomial_dyn)
{
    migraphx::shape s1{migraphx::shape::int32_type, {{2, 3}, {5, 6}}};
    migraphx::shape s2{migraphx::shape::int32_type, {{7, 8}, {9, 10}}};
    migraphx::shape s3{migraphx::shape::int32_type, {{2, 3}, {9, 10}}};

    expect_shape(
        s3, migraphx::make_op("multinomial", {{"dtype", migraphx::shape::int32_type}}), s1, s2);
turneram's avatar
turneram committed
1996
1997
}

Charlie Lin's avatar
Charlie Lin committed
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
TEST_CASE(nms_shape)
{
    // use_dyn_output == false
    migraphx::shape boxes_s{migraphx::shape::float_type, {1, 6, 4}};
    migraphx::shape scores_s{migraphx::shape::float_type, {1, 1, 6}};
    migraphx::shape max_out_s{migraphx::shape::int64_type, {1}};
    migraphx::shape iou_thres_s{migraphx::shape::float_type, {1}};
    migraphx::shape score_thres_s{migraphx::shape::float_type, {1}};
    migraphx::shape output_s{migraphx::shape::int64_type, {6, 3}};
    expect_shape(output_s,
                 migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", false}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // use_dyn_output == true
2017
    output_s = {migraphx::shape::int64_type, {{0, 6}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
    expect_shape(output_s,
                 migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic batches
2028
2029
2030
    boxes_s  = {migraphx::shape::float_type, {{1, 3}, {6, 6}, {4, 4}}};
    scores_s = {migraphx::shape::float_type, {{1, 3}, {1, 1}, {6, 6}}};
    output_s = {migraphx::shape::int64_type, {{0, 18}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
    expect_shape(output_s,
                 migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic num boxes
2041
2042
2043
    boxes_s  = {migraphx::shape::float_type, {{1, 1}, {6, 20}, {4, 4}}};
    scores_s = {migraphx::shape::float_type, {{1, 1}, {1, 1}, {6, 20}}};
    output_s = {migraphx::shape::int64_type, {{0, 20}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
    expect_shape(output_s,
                 migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // use_dyn_output false with dynamic input shape
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", false}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic classes
2063
2064
2065
    boxes_s  = {migraphx::shape::float_type, {{1, 1}, {6, 6}, {4, 4}}};
    scores_s = {migraphx::shape::float_type, {{1, 1}, {1, 3}, {6, 6}}};
    output_s = {migraphx::shape::int64_type, {{0, 6}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
    expect_shape(output_s,
                 migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // fixed mismatch batches
    boxes_s  = {migraphx::shape::float_type, {2, 6, 4}};
    scores_s = {migraphx::shape::float_type, {1, 1, 6}};
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // fixed mismatch num boxes
    boxes_s  = {migraphx::shape::float_type, {1, 6, 4}};
    scores_s = {migraphx::shape::float_type, {1, 1, 4}};
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic mismatch batches
2098
2099
    boxes_s  = {migraphx::shape::float_type, {{1, 4}, {6, 6}, {4, 4}}};
    scores_s = {migraphx::shape::float_type, {{2, 8}, {1, 1}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
2100
2101
2102
2103
2104
2105
2106
2107
2108
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic mismatch num boxes
2109
2110
    boxes_s  = {migraphx::shape::float_type, {{1, 1}, {6, 8}, {4, 4}}};
    scores_s = {migraphx::shape::float_type, {{1, 1}, {1, 1}, {3, 9}}};
Charlie Lin's avatar
Charlie Lin committed
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);

    // dynamic number of classes, fixed boxes_s, mismatch batches
    boxes_s  = {migraphx::shape::float_type, {1, 6, 4}};
2121
    scores_s = {migraphx::shape::float_type, {{1, 3}, {1, 3}, {6, 6}}};
Charlie Lin's avatar
Charlie Lin committed
2122
2123
2124
2125
2126
2127
2128
2129
2130
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);
    // dynamic number of classes, fixed boxes_s, mismatch num boxes
    boxes_s  = {migraphx::shape::float_type, {1, 6, 4}};
2131
    scores_s = {migraphx::shape::float_type, {{1, 1}, {1, 3}, {4, 8}}};
Charlie Lin's avatar
Charlie Lin committed
2132
2133
2134
2135
2136
2137
2138
2139
2140
    throws_shape(migraphx::make_op("nonmaxsuppression",
                                   {{"center_point_box", true}, {"use_dyn_output", true}}),
                 boxes_s,
                 scores_s,
                 max_out_s,
                 iou_thres_s,
                 score_thres_s);
}

Charlie Lin's avatar
Charlie Lin committed
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
TEST_CASE(pad_shape0)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {2, 3, 5, 5}};
    expect_shape(output, migraphx::make_op("pad", {{"pads", {0, 0, 1, 1, 0, 0, 1, 1}}}), input);
}

TEST_CASE(pad_shape1)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {2, 3, 6, 6}};
    expect_shape(output, migraphx::make_op("pad", {{"pads", {0, 0, 2, 2, 0, 0, 1, 1}}}), input);
}

TEST_CASE(pad_dyn_shape0)
{
2157
2158
    migraphx::shape input{migraphx::shape::float_type, {{1, 4, {2}}, {3, 3}, {3, 5}, {3, 5}}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 4, {2}}, {3, 3}, {5, 7}, {5, 7}}};
Charlie Lin's avatar
Charlie Lin committed
2159
2160
2161
2162
2163
2164
    expect_shape(output, migraphx::make_op("pad", {{"pads", {0, 0, 1, 1, 0, 0, 1, 1}}}), input);
}

TEST_CASE(pad_dyn_shape1)
{
    migraphx::shape input{migraphx::shape::float_type,
2165
                          {{1, 4, {2}}, {3, 3}, {3, 5, {5}}, {3, 5, {5}}}};
Charlie Lin's avatar
Charlie Lin committed
2166
    migraphx::shape output{migraphx::shape::float_type,
2167
                           {{1, 4, {2}}, {3, 3}, {5, 7, {7}}, {5, 7, {7}}}};
Charlie Lin's avatar
Charlie Lin committed
2168
2169
2170
    expect_shape(output, migraphx::make_op("pad", {{"pads", {0, 0, 1, 1, 0, 0, 1, 1}}}), input);
}

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
TEST_CASE(pointwise_no_module)
{
    migraphx::shape input{migraphx::shape::float_type, {0}, {0}};
    throws_shape(migraphx::make_op("pointwise"), input);
}

TEST_CASE(pointwise_no_input)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::module m;
    std::vector<migraphx::instruction_ref> args{};
    auto output = migraphx::shape(migraphx::shape::float_type, {1}, {0});
    auto l      = m.add_literal(migraphx::literal(output, {1}));
    m.add_return({l});
    EXPECT(test::throws([&] { mm->add_instruction(migraphx::make_op("pointwise"), args, {&m}); }));
}

TEST_CASE(pointwise_no_output)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::module m;
    std::vector<migraphx::instruction_ref> args{};
    EXPECT(test::throws([&] { mm->add_instruction(migraphx::make_op("pointwise"), args, {&m}); }));
}

Charlie Lin's avatar
Charlie Lin committed
2198
TEST_CASE(pooling_shape0)
2199
{
Charlie Lin's avatar
Charlie Lin committed
2200
2201
2202
2203
2204
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    throws_shape(migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {1}},
                                    {"stride", {0}},
2205
2206
                                    {"lengths", {1}},
                                    {"dilations", {1}}}),
Charlie Lin's avatar
Charlie Lin committed
2207
2208
2209
2210
2211
2212
                 input);
}

TEST_CASE(pooling_shape1)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
2213
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 1, 1}};
Charlie Lin's avatar
Charlie Lin committed
2214
2215
2216
2217
2218
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
2219
2220
                                    {"lengths", {1, 1}},
                                    {"dilations", {1, 1}}}),
Charlie Lin's avatar
Charlie Lin committed
2221
2222
2223
2224
2225
                 input);
}

TEST_CASE(pooling_shape2)
{
Shucai Xiao's avatar
Shucai Xiao committed
2226
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
Charlie Lin's avatar
Charlie Lin committed
2227
2228
2229
2230
2231
2232
2233
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 2, 2}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
                                    {"lengths", {1, 1}},
2234
                                    {"dilations", {1, 1}},
Charlie Lin's avatar
Charlie Lin committed
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
                                    {"ceil_mode", true}}),
                 input);
}

TEST_CASE(pooling_shape3)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 3, 3}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {2, 2}},
                                    {"stride", {3, 3}},
                                    {"lengths", {3, 3}},
2249
                                    {"dilations", {1, 1}},
Charlie Lin's avatar
Charlie Lin committed
2250
2251
2252
2253
                                    {"ceil_mode", true}}),
                 input);
}

Brian Pickrell's avatar
Brian Pickrell committed
2254
2255
2256
2257
2258
2259
2260
TEST_CASE(pooling_shape4)
{
    migraphx::shape tiny_input{migraphx::shape::float_type, {4, 1}};
    throws_shape(migraphx::make_op("pooling", {{"mode", migraphx::op::pooling_mode::max}}),
                 tiny_input);
}

2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
TEST_CASE(pooling_shape5)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 1, 1}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {1, 1}},
                                    {"lengths", {2, 2}},
                                    {"dilations", {2, 2}}}),
                 input);
}

TEST_CASE(pooling_shape6)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 2, 2}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {2, 2}},
                                    {"lengths", {1, 1}},
                                    {"dilations", {2, 2}}}),
                 input);
}

TEST_CASE(pooling_shape7)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 2, 2}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
                                    {"lengths", {1, 1}},
                                    {"dilations", {3, 3}},
                                    {"ceil_mode", true}}),
                 input);
}

TEST_CASE(pooling_shape8)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::shape output{migraphx::shape::float_type, {4, 3, 3, 3}};
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {2, 2}},
                                    {"stride", {1, 1}},
                                    {"lengths", {3, 3}},
                                    {"dilations", {2, 2}}}),
                 input);
}

Charlie Lin's avatar
Charlie Lin committed
2318
2319
TEST_CASE(pooling_dyn_shape0)
{
2320
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3, {3}}, {3, 3, {3}}, {3, 3}}};
2321
2322
2323
2324
    throws_shape(migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {1}},
                                    {"stride", {0}},
2325
2326
                                    {"lengths", {1}},
                                    {"dilations", {1}}}),
2327
                 input);
Charlie Lin's avatar
Charlie Lin committed
2328
2329
2330
2331
}

TEST_CASE(pooling_dyn_shape1)
{
2332
2333
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3, {3}}, {3, 3, {3}}, {3, 3}}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 4}, {3, 3}, {1, 1}, {1, 1}}};
2334
2335
2336
2337
2338
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
2339
2340
                                    {"lengths", {1, 1}},
                                    {"dilations", {1, 1}}}),
2341
                 input);
Charlie Lin's avatar
Charlie Lin committed
2342
}
2343

Charlie Lin's avatar
Charlie Lin committed
2344
2345
TEST_CASE(pooling_dyn_shape2)
{
2346
2347
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {5, 5}, {3, 3, {3}}, {3, 3}}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 4}, {5, 5}, {2, 2}, {2, 2}}};
Charlie Lin's avatar
Charlie Lin committed
2348
    expect_shape(output,
Shucai Xiao's avatar
Shucai Xiao committed
2349
                 migraphx::make_op("pooling",
2350
                                   {{"mode", migraphx::op::pooling_mode::max},
Shucai Xiao's avatar
Shucai Xiao committed
2351
2352
2353
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
                                    {"lengths", {1, 1}},
2354
                                    {"dilations", {1, 1}},
Shucai Xiao's avatar
Shucai Xiao committed
2355
2356
2357
                                    {"ceil_mode", true}}),
                 input);
}
2358

Charlie Lin's avatar
Charlie Lin committed
2359
2360
2361
TEST_CASE(pooling_dyn_shape3)
{
    migraphx::shape input{migraphx::shape::float_type,
2362
2363
                          {{4, 4}, {3, 3}, {4, 12, {8}}, {4, 12, {8}}}};
    migraphx::shape output{migraphx::shape::float_type, {{4, 4}, {3, 3}, {2, 4}, {2, 4}}};
Charlie Lin's avatar
Charlie Lin committed
2364
2365
2366
2367
2368
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {0, 0}},
                                    {"stride", {3, 3}},
2369
2370
                                    {"lengths", {1, 1}},
                                    {"dilations", {1, 1}}}),
Charlie Lin's avatar
Charlie Lin committed
2371
2372
2373
2374
2375
2376
                 input);
}

TEST_CASE(pooling_dyn_shape4)
{
    migraphx::shape input{migraphx::shape::float_type,
2377
2378
                          {{4, 4}, {3, 3}, {4, 12, {8}}, {4, 12, {8}}}};
    migraphx::shape output{migraphx::shape::float_type, {{4, 4}, {3, 3}, {3, 6}, {3, 6}}};
Charlie Lin's avatar
Charlie Lin committed
2379
2380
2381
2382
2383
2384
    expect_shape(output,
                 migraphx::make_op("pooling",
                                   {{"mode", migraphx::op::pooling_mode::max},
                                    {"padding", {2, 2}},
                                    {"stride", {3, 3}},
                                    {"lengths", {3, 3}},
2385
                                    {"dilations", {1, 1}},
Charlie Lin's avatar
Charlie Lin committed
2386
2387
2388
2389
                                    {"ceil_mode", true}}),
                 input);
}

Shucai Xiao's avatar
Shucai Xiao committed
2390
2391
TEST_CASE(prefix_scan_sum)
{
2392
    {
Shucai Xiao's avatar
Shucai Xiao committed
2393
2394
2395
2396
        migraphx::shape s{migraphx::shape::float_type, {1, 2, 3}};
        throws_shape(
            migraphx::make_op("prefix_scan_sum", {{"axis", 3}, {"exclusive", 0}, {"reverse", 0}}),
            s);
2397
2398
2399
    }

    {
Shucai Xiao's avatar
Shucai Xiao committed
2400
2401
2402
2403
        migraphx::shape s{migraphx::shape::float_type, {1, 2}};
        throws_shape(
            migraphx::make_op("prefix_scan_sum", {{"axis", -3}, {"exclusive", 0}, {"reverse", 0}}),
            s);
2404
    }
Shucai Xiao's avatar
Shucai Xiao committed
2405
}
2406

2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
TEST_CASE(prefix_scan_sum_dyn)
{
    {
        std::vector<migraphx::shape::dynamic_dimension> dd{{5, 8}};
        migraphx::shape s{migraphx::shape::float_type, dd};

        expect_shape(
            s,
            migraphx::make_op("prefix_scan_sum", {{"axis", 0}, {"exclusive", 0}, {"reverse", 0}}),
            s);
    }
}

TEST_CASE(prefix_scan_sum_dyn_2d)
{
    {
        std::vector<migraphx::shape::dynamic_dimension> dd{{5, 8}, {3, 7}};
        migraphx::shape s{migraphx::shape::float_type, dd};

        expect_shape(
            s,
            migraphx::make_op("prefix_scan_sum", {{"axis", 1}, {"exclusive", 0}, {"reverse", 0}}),
            s);
    }
}

2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
TEST_CASE(random_uniform)
{
    std::vector<migraphx::shape::dynamic_dimension> dd{{5, 8}, {3, 7}};
    migraphx::shape s0{migraphx::shape::uint64_type, {1}};
    migraphx::shape s1{migraphx::shape::float_type, dd};
    expect_shape(s1, migraphx::make_op("random_uniform"), s0, s1);
}

TEST_CASE(random_seed)
{
    migraphx::shape s{migraphx::shape::uint64_type, {1}, {0}};
    expect_shape(s, migraphx::make_op("random_seed"));
}

Shucai Xiao's avatar
Shucai Xiao committed
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
TEST_CASE(quant_convolution_shape)
{
    migraphx::shape output{migraphx::shape::int32_type, {4, 4, 1, 1}};
    migraphx::shape input{migraphx::shape::int8_type, {4, 3, 3, 3}};
    migraphx::shape weights{migraphx::shape::int8_type, {4, 3, 3, 3}};
    expect_shape(output, migraphx::make_op("quant_convolution"), input, weights);
    throws_shape(migraphx::make_op("quant_convolution"), input);
    throws_shape(migraphx::make_op("quant_convolution",
                                   {{"padding", {0}}, {"stride", {1, 1}}, {"dilation", {1, 1}}}),
                 input,
                 weights);
    throws_shape(migraphx::make_op("quant_convolution",
                                   {{"padding", {0}}, {"stride", {1}}, {"dilation", {1}}}),
                 input,
                 weights);
2462

Shucai Xiao's avatar
Shucai Xiao committed
2463
2464
2465
2466
    migraphx::shape input2{migraphx::shape::int32_type, {3, 3}};
    migraphx::shape weights2{migraphx::shape::float_type, {3, 3}};
    throws_shape(migraphx::make_op("quant_convolution"), input2, weights2);
    throws_shape(migraphx::make_op("quant_convolution"), input2, weights);
2467

Shucai Xiao's avatar
Shucai Xiao committed
2468
2469
2470
2471
2472
2473
    migraphx::shape input3{migraphx::shape::int32_type, {4, 3, 3, 3}};
    migraphx::shape weight3{migraphx::shape::float_type, {4, 3, 3, 3}};
    throws_shape(migraphx::make_op("quant_convolution"), input3, weights);
    throws_shape(migraphx::make_op("quant_convolution"), input, weight3);
    throws_shape(migraphx::make_op("quant_convolution"), input3, weight3);
}
2474

Shucai Xiao's avatar
Shucai Xiao committed
2475
2476
2477
// quant_dot
TEST_CASE(quant_dot_2args)
{
2478
    {
Shucai Xiao's avatar
Shucai Xiao committed
2479
2480
2481
2482
2483
2484
        migraphx::shape s_m1{migraphx::shape::int8_type, {2, 4}};
        migraphx::shape s_m2{migraphx::shape::int8_type, {4, 8}};
        expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 8}},
                     migraphx::make_op("quant_dot"),
                     s_m1,
                     s_m2);
2485
2486
2487
    }

    {
Shucai Xiao's avatar
Shucai Xiao committed
2488
2489
2490
        migraphx::shape s_m1{migraphx::shape::int8_type, {3, 8}};
        migraphx::shape s_m2{migraphx::shape::int8_type, {8, 7}};
        expect_shape(migraphx::shape{migraphx::shape::int32_type, {3, 7}},
2491
                     migraphx::make_op("quant_dot"),
Shucai Xiao's avatar
Shucai Xiao committed
2492
2493
2494
                     s_m1,
                     s_m2);
    }
2495

Shucai Xiao's avatar
Shucai Xiao committed
2496
2497
2498
2499
    {
        migraphx::shape s_m1{migraphx::shape::int8_type, {2, 4}};
        migraphx::shape s_m2{migraphx::shape::int8_type, {8, 8}};
        throws_shape(migraphx::make_op("quant_dot"), s_m1, s_m2);
2500
2501
2502
    }
}

2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
TEST_CASE(qlinear)
{
    migraphx::shape scales{migraphx::shape::float_type, {2, 4}};
    migraphx::shape input{migraphx::shape::float_type, {2, 4}};
    migraphx::shape result{migraphx::shape::uint8_type, {2, 4}};
    expect_shape(result, migraphx::make_op("quantizelinear"), input, scales);
}

TEST_CASE(qlinear_zeros)
{
    migraphx::shape zeros{migraphx::shape::int8_type, {2, 4}};
    migraphx::shape scales{migraphx::shape::float_type, {2, 4}};
    migraphx::shape input{migraphx::shape::float_type, {2, 4}};
    migraphx::shape result{migraphx::shape::int8_type, {2, 4}};
    expect_shape(result, migraphx::make_op("quantizelinear"), input, scales, zeros);
}

TEST_CASE(qlinear_fp16)
{
    migraphx::shape scales{migraphx::shape::half_type, {2, 4}};
    migraphx::shape input{migraphx::shape::half_type, {2, 4}};
    migraphx::shape result{migraphx::shape::uint8_type, {2, 4}};
    expect_shape(result, migraphx::make_op("quantizelinear"), input, scales);
}

TEST_CASE(qlinear_mismatch_type)
{
    migraphx::shape scales{migraphx::shape::int8_type, {2, 4}};
    migraphx::shape input{migraphx::shape::float_type, {2, 4}};
    throws_shape(migraphx::make_op("quantizelinear"), input, scales);
}

TEST_CASE(dqlinear)
{
    migraphx::shape scales{migraphx::shape::float_type, {2, 4}};
    migraphx::shape input{migraphx::shape::int8_type, {2, 4}};
    migraphx::shape result{migraphx::shape::float_type, {2, 4}};
    expect_shape(result, migraphx::make_op("dequantizelinear"), input, scales);
}

TEST_CASE(dqlinear_fp16)
{
    migraphx::shape scales{migraphx::shape::half_type, {2, 4}};
    migraphx::shape input{migraphx::shape::int8_type, {2, 4}};
    migraphx::shape result{migraphx::shape::half_type, {2, 4}};
    expect_shape(result, migraphx::make_op("dequantizelinear"), input, scales);
}

TEST_CASE(dqlinear_mismatch_type)
{
    migraphx::shape zeros{migraphx::shape::float_type, {2, 4}};
    migraphx::shape scales{migraphx::shape::float_type, {2, 4}};
    migraphx::shape input{migraphx::shape::int8_type, {2, 4}};
    throws_shape(migraphx::make_op("dequantizelinear"), input, scales, zeros);
}

2559
void test_reduce_ops(const std::string& name)
Shucai Xiao's avatar
Shucai Xiao committed
2560
{
Shucai Xiao's avatar
Shucai Xiao committed
2561
    {
Shucai Xiao's avatar
Shucai Xiao committed
2562
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2563
2564
2565
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 1}},
                     migraphx::make_op(name),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
2566
    }
Shucai Xiao's avatar
Shucai Xiao committed
2567

Shucai Xiao's avatar
Shucai Xiao committed
2568
2569
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2570
2571
2572
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 1}},
                     migraphx::make_op(name, {{"axes", {0, 1, 2, 3}}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
2573
2574
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
2575
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2576
2577
2578
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 1, 1}},
                     migraphx::make_op(name, {{"axes", {2, 3}}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
2579
2580
    }
    {
Shucai Xiao's avatar
Shucai Xiao committed
2581
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2582
2583
2584
        expect_shape(migraphx::shape{migraphx::shape::float_type, {1, 3, 4, 5}},
                     migraphx::make_op(name, {{"axes", {0}}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
2585
2586
2587
    }
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2588
2589
2590
        expect_shape(migraphx::shape{migraphx::shape::float_type, {2, 3, 4, 1}},
                     migraphx::make_op(name, {{"axes", {-1}}}),
                     input);
Shucai Xiao's avatar
Shucai Xiao committed
2591
2592
2593
    }
    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
2594
        throws_shape(migraphx::make_op(name, {{"axes", {4}}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
2595
2596
    }
}
Shucai Xiao's avatar
Shucai Xiao committed
2597

Brian Pickrell's avatar
Brian Pickrell committed
2598
// dynamic shape
2599
void test_dyn_reduce_ops(const std::string& name)
Brian Pickrell's avatar
Brian Pickrell committed
2600
2601
{
    {
2602
2603
2604
2605
        migraphx::shape input{migraphx::shape::float_type, {{2, 3, {3}}, {2, 4, {4}}}};
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            std::vector<migraphx::shape::dynamic_dimension>({{2, 3, {3}}, {1, 1}})},
2606
            migraphx::make_op(name, {{"axes", {-1}}}),
2607
            input);
Brian Pickrell's avatar
Brian Pickrell committed
2608
2609
    }
    {
2610
2611
2612
2613
        migraphx::shape input{migraphx::shape::float_type, {{2, 3, {3}}, {2, 4, {4}}}};
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            std::vector<migraphx::shape::dynamic_dimension>({{1, 1}, {2, 4, {4}}})},
2614
            migraphx::make_op(name, {{"axes", {0}}}),
2615
            input);
Brian Pickrell's avatar
Brian Pickrell committed
2616
2617
2618
    }
    {
        // Empty axis argument reduces all axes
2619
2620
2621
2622
        migraphx::shape input{migraphx::shape::float_type, {{2, 3, {3}}, {2, 4, {4}}}};
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            std::vector<migraphx::shape::dynamic_dimension>({{1, 1}, {1, 1}})},
2623
            migraphx::make_op(name),
2624
            input);
Brian Pickrell's avatar
Brian Pickrell committed
2625
2626
    }
    {
2627
        migraphx::shape input{migraphx::shape::float_type, {{2, 3, {3}}, {2, 4, {4}}}};
2628
        throws_shape(migraphx::make_op(name, {{"axes", {4}}}), input);
Brian Pickrell's avatar
Brian Pickrell committed
2629
2630
2631
    }
}

2632
2633
2634
2635
TEST_CASE(reduce_max) { test_reduce_ops("reduce_max"); }
TEST_CASE(reduce_mean) { test_reduce_ops("reduce_mean"); }
TEST_CASE(reduce_prod) { test_reduce_ops("reduce_prod"); }
TEST_CASE(reduce_sum) { test_reduce_ops("reduce_sum"); }
Shucai Xiao's avatar
Shucai Xiao committed
2636

2637
2638
2639
2640
TEST_CASE(reduce_max_dyn) { test_dyn_reduce_ops("reduce_max"); }
TEST_CASE(reduce_mean_dyn) { test_dyn_reduce_ops("reduce_mean"); }
TEST_CASE(reduce_prod_dyn) { test_dyn_reduce_ops("reduce_prod"); }
TEST_CASE(reduce_sum_dyn) { test_dyn_reduce_ops("reduce_sum"); }
Brian Pickrell's avatar
Brian Pickrell committed
2641

Shucai Xiao's avatar
Shucai Xiao committed
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
TEST_CASE(reshape_shape)
{
    migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}};
    for(auto&& new_shape :
        std::vector<std::vector<int64_t>>{{8, 3, 1, 1}, {1, 3, 4, 2}, {1, 3, 4, 2}})
    {
        std::vector<std::size_t> lens(new_shape.size());
        std::copy(new_shape.begin(), new_shape.end(), lens.begin());
        migraphx::shape output{migraphx::shape::float_type, lens};
        expect_shape(output, migraphx::make_op("reshape", {{"dims", new_shape}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
2652
    }
Ted Themistokleous's avatar
Ted Themistokleous committed
2653
}
Shucai Xiao's avatar
Shucai Xiao committed
2654

Ted Themistokleous's avatar
Ted Themistokleous committed
2655
2656
2657
TEST_CASE(reshape_shape_invalid)
{
    migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}};
Shucai Xiao's avatar
Shucai Xiao committed
2658
    for(auto&& new_shape :
2659
        std::vector<std::vector<int64_t>>{{8, 3, 2, 2}, {1, 3, -1, -1}, {3, 0}, {3, 2}})
Shucai Xiao's avatar
Shucai Xiao committed
2660
    {
Shucai Xiao's avatar
Shucai Xiao committed
2661
2662
        throws_shape(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
    }
Ted Themistokleous's avatar
Ted Themistokleous committed
2663
}
Shucai Xiao's avatar
Shucai Xiao committed
2664

Ted Themistokleous's avatar
Ted Themistokleous committed
2665
2666
2667
TEST_CASE(reshape_shape_minus1_reshapes)
{
    migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}};
Shucai Xiao's avatar
Shucai Xiao committed
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
    std::vector<std::pair<std::vector<int64_t>, migraphx::shape>> minus1_tests{
        {{2, -1, 3}, {migraphx::shape::float_type, {2, 4, 3}}},
        {{0, -1, 0}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{2, -1, 0}, {migraphx::shape::float_type, {2, 12, 1}}},
        {{0, 0, -1}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{2, 0, -1}, {migraphx::shape::float_type, {2, 1, 12}}},
        {{-1, 2, 3}, {migraphx::shape::float_type, {4, 2, 3}}},
        {{-1, 0, 3}, {migraphx::shape::float_type, {8, 1, 3}}},
        {{-1, 0, 0}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{-1, 3, 0}, {migraphx::shape::float_type, {8, 3, 1}}}};
Shucai Xiao's avatar
Shucai Xiao committed
2678

Shucai Xiao's avatar
Shucai Xiao committed
2679
2680
2681
    for(auto& it : minus1_tests)
    {
        expect_shape(it.second, migraphx::make_op("reshape", {{"dims", it.first}}), input);
Shucai Xiao's avatar
Shucai Xiao committed
2682
2683
2684
    }
}

2685
2686
2687
2688
2689
TEST_CASE(reshape_nonstandard)
{
    auto input = migraphx::shape::from_permutation(migraphx::shape::float_type,
                                                   {4, 24, 1, 1, 1},
                                                   migraphx::invert_permutation({1, 0, 2, 3, 4}));
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
    std::vector<std::vector<std::size_t>> tests{{4, 24},
                                                {4, 24, 1, 1, 1, 1},
                                                {4, 8, 3, 1, 1},
                                                {4, 1, 3, 4, 2},
                                                {4, 1, 4, 3, 2},
                                                {4, 2, 4, 3},
                                                {4, 2, 12, 1},
                                                {4, 2, 1, 12},
                                                {4, 4, 2, 3},
                                                {4, 8, 1, 3},
                                                {4, 8, 3, 1}};

    for(auto dims : tests)
    {
        migraphx::shape output = migraphx::shape{migraphx::shape::float_type, dims};
2705
2706
2707
2708
2709
2710
2711
2712
2713
        expect_shape(output, migraphx::make_op("reshape", {{"dims", dims}}), input);
    }
}

TEST_CASE(reshape_nonstandard_squeeze)
{
    auto input = migraphx::shape::from_permutation(
        migraphx::shape::float_type, {2, 16, 16, 1280}, migraphx::invert_permutation({0, 2, 3, 1}));
    std::vector<std::size_t> lens = {2, 256, 1280};
2714
    migraphx::shape output        = migraphx::shape{migraphx::shape::float_type, lens};
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
    expect_shape(output, migraphx::make_op("reshape", {{"dims", lens}}), input);
}

TEST_CASE(reshape_nonstandard_error)
{
    auto input = migraphx::shape::from_permutation(migraphx::shape::float_type,
                                                   {4, 24, 1, 1, 1},
                                                   migraphx::invert_permutation({1, 0, 2, 3, 4}));
    for(auto&& new_shape : std::vector<std::vector<int64_t>>{{4, 8, 3, 2, 2},
                                                             {1},
                                                             {4, 8, 4},
                                                             {4, 24, 1, 1, 1, 1, 2},
                                                             {8, 4, 4},
                                                             {4, 1, 3, -1, -1},
                                                             {4, 3, 0},
                                                             {4, 3, 2},
                                                             {3, 0},
                                                             {3, 2}})
    {
        throws_shape(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
    }
}

2738
2739
2740
2741
2742
2743
2744
TEST_CASE(reshape_transposed_squeeze)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {1, 4}};
    migraphx::shape output{migraphx::shape::float_type, {64}};
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

2745
2746
2747
TEST_CASE(reshape_nonpacked_unsqueeze1)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
2748
    migraphx::shape output{migraphx::shape::float_type, {4, 2, 8}};
2749
2750
2751
2752
2753
2754
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_nonpacked_unsqueeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
2755
2756
2757
2758
2759
2760
2761
2762
    migraphx::shape output{migraphx::shape::float_type, {2, 2, 16}};
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_nonpacked_squeeze1)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
    migraphx::shape output{migraphx::shape::float_type, {64}};
2763
2764
2765
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

2766
TEST_CASE(reshape_nonpacked_squeeze2)
2767
2768
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
2769
    migraphx::shape output{migraphx::shape::float_type, {64}};
2770
2771
2772
2773
2774
2775
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_broadcast_unsqueeze1)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 256, 1280}, {0, 0, 1}};
2776
    migraphx::shape output{migraphx::shape::float_type, {2, 16, 16, 1280}};
2777
2778
2779
2780
2781
2782
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_broadcast_unsqueeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 256, 1280}, {0, 0, 1}};
2783
    migraphx::shape output{migraphx::shape::float_type, {2, 256, 16, 80}};
2784
2785
2786
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

2787
TEST_CASE(reshape_broadcast_squeeze1)
2788
2789
{
    migraphx::shape input{migraphx::shape::float_type, {2, 16, 16, 1280}, {0, 0, 0, 1}};
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
    migraphx::shape output{migraphx::shape::float_type, {2, 256, 1280}};
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_broadcast_squeeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {0, 1}};
    migraphx::shape output{migraphx::shape::float_type, {64}};
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_broadcast_squeeze3)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {1, 0}};
    migraphx::shape output{migraphx::shape::float_type, {64}};
2805
2806
2807
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
}

Ted Themistokleous's avatar
Ted Themistokleous committed
2808
TEST_CASE(reshape_broadcast_squeeze_memlayout_change)
2809
2810
{
    migraphx::shape input{migraphx::shape::float_type, {2, 16, 16, 1280}, {0, 0, 0, 1}};
2811
    migraphx::shape output{migraphx::shape::float_type, {2, 16, 256, 80}};
Ted Themistokleous's avatar
Ted Themistokleous committed
2812
    expect_shape(output, migraphx::make_op("reshape", {{"dims", output.lens()}}), input);
2813
2814
}

2815
TEST_CASE(reshape_dyn_1in)
2816
{
2817
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
    for(auto&& new_shape : std::vector<std::vector<int64_t>>{
            {-1, 1, 1, 24}, {0, 8, 3, 1}, {-1, 3, 4, 2}, {0, 2, 4, 3}})
    {
        std::vector<migraphx::shape::dynamic_dimension> out_dyn_dims{};
        for(std::size_t i = 0; i < new_shape.size(); ++i)
        {
            if(new_shape[i] == 0 or new_shape[i] == -1)
            {
                out_dyn_dims.push_back(input.dyn_dims().at(i));
            }
            else
            {
                std::size_t d = new_shape[i];
2831
                out_dyn_dims.push_back({d, d});
2832
2833
2834
2835
2836
2837
2838
            }
        }
        migraphx::shape output{migraphx::shape::float_type, out_dyn_dims};
        expect_shape(output, migraphx::make_op("reshape", {{"dims", new_shape}}), input);
    }
}

2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
TEST_CASE(reshape_dyn_2in_0)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 4}, {8, 8}, {3, 3}, {1, 1}}};
    expect_shape(output, migraphx::make_op("reshape"), input, output);
}

TEST_CASE(reshape_dyn_2in_1)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    migraphx::shape output{migraphx::shape::float_type, {{12, 12}, {2, 2}, {1, 1}, {1, 4}}};
    expect_shape(output, migraphx::make_op("reshape"), input, output);
}

TEST_CASE(reshape_dyn_2in_2)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 24, 1, 1}};
    migraphx::shape output{migraphx::shape::float_type, {{1, 2}, {6, 12}, {1, 1}, {4, 4}}};
    expect_shape(output, migraphx::make_op("reshape"), input, output);
}

2860
2861
TEST_CASE(reshape_multiple_non_fixed_error)
{
2862
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {10, 20}, {1, 1}}};
2863
2864
2865
2866
2867
2868
    std::vector<int64_t> new_shape = {0, 1, 0, 24};
    throws_shape(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
}

TEST_CASE(reshape_fixed_ele_not_matching_error)
{
2869
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {10, 10}, {1, 1}}};
2870
2871
2872
2873
2874
2875
    std::vector<int64_t> new_shape = {0, 1, 5, 24};
    throws_shape(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
}

TEST_CASE(reshape_non_fixed_not_matching_error)
{
2876
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
2877
2878
2879
2880
    std::vector<int64_t> new_shape = {2, 1, 1, 24};
    throws_shape(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
}

Ted Themistokleous's avatar
Ted Themistokleous committed
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
TEST_CASE(reshape_lazy_shape)
{
    migraphx::shape input{migraphx::shape::float_type, {24, 1, 1, 1}};
    for(auto&& new_shape :
        std::vector<std::vector<int64_t>>{{8, 3, 1, 1}, {1, 3, 4, 2}, {1, 3, 4, 2}})
    {
        std::vector<std::size_t> lens(new_shape.size());
        std::copy(new_shape.begin(), new_shape.end(), lens.begin());
        migraphx::shape output{migraphx::shape::float_type, lens};
        expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
    }

    for(auto&& new_shape :
        std::vector<std::vector<int64_t>>{{8, 3, 2, 2}, {1, 3, -1, -1}, {3, 0}, {3, 2}})
    {
        throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
    }

    std::vector<std::pair<std::vector<int64_t>, migraphx::shape>> minus1_tests{
        {{2, -1, 3}, {migraphx::shape::float_type, {2, 4, 3}}},
        {{0, -1, 0}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{2, -1, 0}, {migraphx::shape::float_type, {2, 12, 1}}},
        {{0, 0, -1}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{2, 0, -1}, {migraphx::shape::float_type, {2, 1, 12}}},
        {{-1, 2, 3}, {migraphx::shape::float_type, {4, 2, 3}}},
        {{-1, 0, 3}, {migraphx::shape::float_type, {8, 1, 3}}},
        {{-1, 0, 0}, {migraphx::shape::float_type, {24, 1, 1}}},
        {{-1, 3, 0}, {migraphx::shape::float_type, {8, 3, 1}}}};

    for(auto& it : minus1_tests)
    {
        expect_shape(it.second, migraphx::make_op("reshape_lazy", {{"dims", it.first}}), input);
    }
}

// This uses the permutation to compute the reshape_lazy since its simpler than
// trying to calculate strides. As we collapse or expand dimensions, we
// remove the collapsed dimensions or duplicate the expanded dimensions in
// the permutation. Then we renumber the permutation. So for dimensions of 4,
// 24, 1, 1, 1 with a permutation of 1, 0, 2, 3, 4 that reshape_lazys to 4, 1, 3,
// 4, 2, we first remove the collapsed dimensions or duplicate the expanded
// dimensions which gives 1, 0, 0, 0, 0. Then after renumbering we get a
// final permutation of 4, 0, 1, 2, 3.
TEST_CASE(reshape_lazy_nonstandard)
{
    auto input = migraphx::shape::from_permutation(migraphx::shape::float_type,
                                                   {4, 24, 1, 1, 1},
                                                   migraphx::invert_permutation({1, 0, 2, 3, 4}));
    std::vector<std::pair<std::vector<std::size_t>, std::vector<int64_t>>> tests{
        {{4, 24}, {1, 0}},
        {{4, 24, 1, 1, 1, 1}, {1, 0, 2, 3, 4, 5}},
        {{4, 8, 3, 1, 1}, {2, 0, 1, 3, 4}},
        {{4, 1, 3, 4, 2}, {4, 0, 1, 2, 3}},
        {{4, 1, 4, 3, 2}, {4, 0, 1, 2, 3}},
        {{4, 2, 4, 3}, {3, 0, 1, 2}},
        {{4, 2, 12, 1}, {2, 0, 1, 3}},
        {{4, 2, 1, 12}, {3, 0, 1, 2}},
        {{4, 4, 2, 3}, {3, 0, 1, 2}},
        {{4, 8, 1, 3}, {3, 0, 1, 2}},
        {{4, 8, 3, 1}, {2, 0, 1, 3}}};

    for(const auto& [dims, perm] : tests)
    {
        migraphx::shape output = migraphx::shape::from_permutation(
            migraphx::shape::float_type, dims, migraphx::invert_permutation(perm));
        expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", dims}}), input);
    }
}

TEST_CASE(reshape_lazy_nonstandard_squeeze)
{
    auto input = migraphx::shape::from_permutation(
        migraphx::shape::float_type, {2, 16, 16, 1280}, migraphx::invert_permutation({0, 2, 3, 1}));
    std::vector<std::size_t> lens = {2, 256, 1280};
    migraphx::shape output        = migraphx::shape::from_permutation(
        migraphx::shape::float_type, lens, migraphx::invert_permutation({0, 2, 1}));
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", lens}}), input);
}

TEST_CASE(reshape_lazy_nonstandard_error)
{
    auto input = migraphx::shape::from_permutation(migraphx::shape::float_type,
                                                   {4, 24, 1, 1, 1},
                                                   migraphx::invert_permutation({1, 0, 2, 3, 4}));
    for(auto&& new_shape : std::vector<std::vector<int64_t>>{{4, 8, 3, 2, 2},
                                                             {1},
                                                             {4, 8, 4},
                                                             {4, 24, 1, 1, 1, 1, 2},
                                                             {8, 4, 4},
                                                             {4, 1, 3, -1, -1},
                                                             {4, 3, 0},
                                                             {4, 3, 2},
                                                             {3, 0},
                                                             {3, 2}})
    {
        throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
    }
}

Paul Fultz II's avatar
Paul Fultz II committed
2980
2981
2982
2983
2984
2985
TEST_CASE(reshape_lazy_transposed_squeeze)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {1, 4}};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", {64}}}), input);
}

Ted Themistokleous's avatar
Ted Themistokleous committed
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
TEST_CASE(reshape_lazy_nonpacked_unsqueeze1)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
    migraphx::shape output{migraphx::shape::float_type, {4, 2, 8}, {32, 16, 2}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_lazy_nonpacked_unsqueeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
    migraphx::shape output{migraphx::shape::float_type, {2, 2, 16}, {64, 32, 2}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

Paul Fultz II's avatar
Paul Fultz II committed
3000
TEST_CASE(reshape_lazy_nonpacked_squeeze1)
Ted Themistokleous's avatar
Ted Themistokleous committed
3001
3002
3003
3004
3005
3006
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 2}};
    migraphx::shape output{migraphx::shape::float_type, {64}, {2}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

Paul Fultz II's avatar
Paul Fultz II committed
3007
3008
3009
3010
3011
3012
TEST_CASE(reshape_lazy_nonpacked_squeeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {32, 1}};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", {64}}}), input);
}

Ted Themistokleous's avatar
Ted Themistokleous committed
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
TEST_CASE(reshape_lazy_broadcast_unsqueeze1)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 256, 1280}, {0, 0, 1}};
    migraphx::shape output{migraphx::shape::float_type, {2, 16, 16, 1280}, {0, 0, 0, 1}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

TEST_CASE(reshape_lazy_broadcast_unsqueeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 256, 1280}, {0, 0, 1}};
    migraphx::shape output{migraphx::shape::float_type, {2, 256, 16, 80}, {0, 0, 80, 1}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

Paul Fultz II's avatar
Paul Fultz II committed
3027
TEST_CASE(reshape_lazy_broadcast_squeeze1)
Ted Themistokleous's avatar
Ted Themistokleous committed
3028
3029
3030
3031
3032
3033
{
    migraphx::shape input{migraphx::shape::float_type, {2, 16, 16, 1280}, {0, 0, 0, 1}};
    migraphx::shape output{migraphx::shape::float_type, {2, 256, 1280}, {0, 0, 1}};
    expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", output.lens()}}), input);
}

Paul Fultz II's avatar
Paul Fultz II committed
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
TEST_CASE(reshape_lazy_broadcast_squeeze2)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {0, 1}};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", {64}}}), input);
}

TEST_CASE(reshape_lazy_broadcast_squeeze3)
{
    migraphx::shape input{migraphx::shape::float_type, {4, 16}, {1, 0}};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", {64}}}), input);
}

Ted Themistokleous's avatar
Ted Themistokleous committed
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
TEST_CASE(reshape_lazy_broadcast_squeeze_error)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 16, 16, 1280}, {0, 0, 0, 1}};
    std::vector<int64_t> new_shape = {2, 16, 20480};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
}

TEST_CASE(reshape_lazy_dyn_shape)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    for(auto&& new_shape : std::vector<std::vector<int64_t>>{
            {-1, 1, 1, 24}, {0, 8, 3, 1}, {-1, 3, 4, 2}, {0, 2, 4, 3}})
    {
        std::vector<migraphx::shape::dynamic_dimension> out_dyn_dims{};
        for(std::size_t i = 0; i < new_shape.size(); ++i)
        {
            if(new_shape[i] == 0 or new_shape[i] == -1)
            {
                out_dyn_dims.push_back(input.dyn_dims().at(i));
            }
            else
            {
                std::size_t d = new_shape[i];
                out_dyn_dims.push_back({d, d});
            }
        }
        migraphx::shape output{migraphx::shape::float_type, out_dyn_dims};
        expect_shape(output, migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
    }
}

TEST_CASE(reshape_lazy_multiple_non_fixed_error)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {10, 20}, {1, 1}}};
    std::vector<int64_t> new_shape = {0, 1, 0, 24};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
}

TEST_CASE(reshape_lazy_fixed_ele_not_matching_error)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {10, 10}, {1, 1}}};
    std::vector<int64_t> new_shape = {0, 1, 5, 24};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
}

TEST_CASE(reshape_lazy_non_fixed_not_matching_error)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    std::vector<int64_t> new_shape = {2, 1, 1, 24};
    throws_shape(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
TEST_CASE(return_shape_tuple)
{
    using migraphx::shape;
    auto op = migraphx::make_op("@return");
    shape s0{shape::bool_type, {1, 1}};
    shape s1{shape::float_type, {2, 3}};

    std::vector<shape> s{s0, s1};
    auto s_out = op.compute_shape(s);
    EXPECT(s_out.type() == shape::tuple_type);
    EXPECT(s0 == s_out.sub_shapes()[0]);
    EXPECT(s1 == s_out.sub_shapes()[1]);
}

TEST_CASE(return_shape_half)
{
    using migraphx::shape;
    auto op = migraphx::make_op("@return");
    std::vector<shape> s{{shape::half_type}};
    EXPECT(op.compute_shape(s) == shape{shape::half_type});
}

TEST_CASE(return_shape_empty)
{
    using migraphx::shape;
    auto op = migraphx::make_op("@return");
    std::vector<shape> s;
    EXPECT(op.compute_shape(s) == shape{});
}

Shucai Xiao's avatar
Shucai Xiao committed
3128
TEST_CASE(rnn)
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
{
    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3139
3140
3141
3142
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3143
3144
3145
3146

        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
3147
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3148
                "rnn",
3149
3150
3151
3152
3153
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
3154
3155
            in_shape,
            w_shape,
Shucai Xiao's avatar
Shucai Xiao committed
3156
3157
3158
            r_shape,
            b_shape,
            ih_shape);
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3171
3172
3173
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3174
3175
3176
3177

        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
3178
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3179
                "rnn",
3180
3181
3182
3183
3184
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::reverse)},
                 {"clip", clip}}),
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3202
3203
3204
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3205

3206
3207
3208
3209
        expect_shape(
            migraphx::shape{migraphx::shape::float_type,
                            {seq_len, num_dirct, batch_size, hidden_size}},
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3210
                "rnn",
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3233
3234
3235
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3236

3237
3238
        throws_shape(
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3239
                "rnn",
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
                {{"hidden_size", hidden_size + 1},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 1;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3262
3263
3264
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3265

3266
3267
        throws_shape(
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3268
                "rnn",
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::bidirectional)},
                 {"clip", clip}}),
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
    }

    {
        std::size_t batch_size  = 2;
        std::size_t seq_len     = 2;
        std::size_t hidden_size = 4;
        std::size_t input_size  = 3;
        std::size_t num_dirct   = 2;
        float clip              = 0.0f;

        migraphx::shape in_shape{migraphx::shape::float_type, {seq_len, batch_size, input_size}};
        migraphx::shape ih_shape{migraphx::shape::float_type, {num_dirct, batch_size, hidden_size}};
Shucai Xiao's avatar
Shucai Xiao committed
3291
3292
3293
        migraphx::shape w_shape{migraphx::shape::float_type, {num_dirct, hidden_size, input_size}};
        migraphx::shape r_shape{migraphx::shape::float_type, {num_dirct, hidden_size, hidden_size}};
        migraphx::shape b_shape{migraphx::shape::float_type, {num_dirct, 2 * hidden_size}};
3294
3295

        throws_shape(
3296
            migraphx::make_op(
Shucai Xiao's avatar
Shucai Xiao committed
3297
                "rnn",
3298
3299
3300
3301
3302
                {{"hidden_size", hidden_size},
                 {"actv_func",
                  migraphx::to_value(std::vector<migraphx::operation>{migraphx::make_op("tanh")})},
                 {"direction", migraphx::to_value(migraphx::op::rnn_direction::forward)},
                 {"clip", clip}}),
3303
3304
3305
3306
3307
3308
3309
3310
            in_shape,
            w_shape,
            r_shape,
            b_shape,
            ih_shape);
    }
}

Charlie Lin's avatar
Charlie Lin committed
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
TEST_CASE(select_module_dyn)
{
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3}, {255, 255}, {255, 255}}};
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {1000, 1000}}});
    migraphx::shape out_attr = migraphx::shape{sub_shapes};
    expect_shape(
        out_attr,
        migraphx::make_op("select_module", {{"output_dyn_shapes", migraphx::to_value(out_attr)}}),
        input);
}

Charlie Lin's avatar
Charlie Lin committed
3323
TEST_CASE(slice_static_shape)
Shucai Xiao's avatar
Shucai Xiao committed
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
{
    migraphx::shape input{migraphx::shape::int32_type, {2, 2, 3}};
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}},
                 migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1}}, {"ends", {3}}}),
                 input);
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 2}, {6, 3, 1}},
                 migraphx::make_op(
                     "slice", {{"axes", {0, 1, 2}}, {"starts", {0, 0, 1}}, {"ends", {2, 2, 3}}}),
                 input);
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 1}, {6, 3, 1}},
                 migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {10}}}),
                 input);
Brian Pickrell's avatar
Brian Pickrell committed
3336
3337
3338
3339
3340
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {2, 2, 1}, {6, 3, 1}},
                 migraphx::make_op("slice", {{"axes", {2}}, {"starts", {-1}}, {"ends", {10}}}),
                 input);
}

Charlie Lin's avatar
Charlie Lin committed
3341
3342
TEST_CASE(slice_var_inputs_static_shape0)
{
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
    // attr ends and axes set; inputs are (data, input_starts)
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"ends", {2, 3}}, {"axes", {1, 2}}}),
                 input,
                 starts);
}

TEST_CASE(slice_var_inputs_static_mismatch_error0)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    throws_shape(
        migraphx::make_op("slice", {{"ends", {2, 3, 4}}, {"axes", {0, 1, 2}}}), input, starts);
}

TEST_CASE(slice_var_inputs_static_shape1)
{
    // attr starts and axes set; inputs are (data, input_ends)
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"starts", {0, 1}}, {"axes", {1, 2}}}),
                 input,
                 ends);
}

TEST_CASE(slice_var_inputs_static_mismatch_error1)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    throws_shape(
        migraphx::make_op("slice", {{"starts", {0, 1, 2}}, {"axes", {0, 1, 2}}}), input, ends);
}

TEST_CASE(slice_var_inputs_static_shape2)
{
    // attr starts and ends set; inputs are (data, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"starts", {0, 1}}, {"ends", {1, 2}}}),
                 input,
                 axes);
}

TEST_CASE(slice_var_inputs_static_mismatch_error2)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(
        migraphx::make_op("slice", {{"starts", {0, 1, 2}}, {"ends", {3, 4, 4}}}), input, axes);
}

TEST_CASE(slice_var_inputs_static_shape3)
{
    // attr axes set; inputs are (data, input_starts, input_ends)
Charlie Lin's avatar
Charlie Lin committed
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"axes", {1, 2}}}),
                 input,
                 starts,
                 ends);
}

3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
TEST_CASE(slice_var_inputs_static_mismatch_error3)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"axes", {0, 1, 2}}}), input, starts, ends);
}

TEST_CASE(slice_var_inputs_static_shape4)
{
    // attr ends set; inputs are (data, input_starts, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"ends", {3, 4}}}),
                 input,
                 starts,
                 axes);
}

TEST_CASE(slice_var_inputs_static_mismatch_error4)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"ends", {3, 3, 3}}}), input, starts, axes);
}

TEST_CASE(slice_var_inputs_static_shape5)
{
    // attr starts set; inputs are (data, input_ends, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice", {{"starts", {0, 2}}}),
                 input,
                 ends,
                 axes);
}

TEST_CASE(slice_var_inputs_static_mismatch_error5)
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"starts", {0, 1, 2}}}), input, ends, axes);
}

TEST_CASE(slice_var_inputs_static_shape6)
Charlie Lin's avatar
Charlie Lin committed
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 3}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice"),
                 input,
                 starts,
                 ends,
                 axes);
}

3475
TEST_CASE(slice_var_inputs_static_mismatch_error6)
Charlie Lin's avatar
Charlie Lin committed
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
{
    migraphx::shape input{migraphx::shape::float_type, {3, 4, 4}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {3}};
    throws_shape(migraphx::make_op("slice"), input, starts, ends, axes);
}

TEST_CASE(slice_var_inputs_dyn_shape0)
{
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
    // attr ends and axes set; inputs are (data, input_starts)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 6}, {0, 6}, {0, 6}}},
                 migraphx::make_op("slice", {{"ends", {2, 3}}, {"axes", {1, 2}}}),
                 input,
                 starts);
}

TEST_CASE(slice_var_inputs_dyn_mismatch_error0)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
Charlie Lin's avatar
Charlie Lin committed
3498
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
3499
3500
3501
3502
3503
3504
3505
3506
    throws_shape(
        migraphx::make_op("slice", {{"ends", {2, 3, 4}}, {"axes", {0, 1, 2}}}), input, starts);
}

TEST_CASE(slice_var_inputs_dyn_shape1)
{
    // attr starts and axes set; inputs are (data, input_ends)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
Charlie Lin's avatar
Charlie Lin committed
3507
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 6}, {0, 6}, {0, 6}}},
                 migraphx::make_op("slice", {{"starts", {0, 1}}, {"axes", {1, 2}}}),
                 input,
                 ends);
}

TEST_CASE(slice_var_inputs_dyn_mismatch_error1)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    throws_shape(
        migraphx::make_op("slice", {{"starts", {0, 1, 2}}, {"axes", {0, 1, 2}}}), input, ends);
}

TEST_CASE(slice_var_inputs_dyn_shape2)
{
    // attr starts and ends set; inputs are (data, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 6}, {0, 6}, {0, 6}}},
                 migraphx::make_op("slice", {{"starts", {0, 1}}, {"ends", {8, 8}}}),
                 input,
                 axes);
}

TEST_CASE(slice_var_inputs_dyn_mismatch_error2)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(
        migraphx::make_op("slice", {{"starts", {0, 1, 2}}, {"ends", {3, 4, 4}}}), input, axes);
}

TEST_CASE(slice_var_inputs_dyn_shape3)
{
    // attr axes set; inputs are (data, input_starts, input_ends)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{3, 6}, {0, 6}, {0, 6}}},
Charlie Lin's avatar
Charlie Lin committed
3548
3549
3550
3551
3552
3553
                 migraphx::make_op("slice", {{"axes", {1, 2}}}),
                 input,
                 starts,
                 ends);
}

3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
TEST_CASE(slice_var_inputs_dyn_mismatch_error3)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"axes", {0, 1, 2}}}), input, starts, ends);
}

TEST_CASE(slice_var_inputs_dyn_shape4)
{
    // attr ends set; inputs are (data, input_starts, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 6}, {0, 6}, {0, 6}}},
                 migraphx::make_op("slice", {{"ends", {3, 4}}}),
                 input,
                 starts,
                 axes);
}

TEST_CASE(slice_var_inputs_dyn_mismatch_error4)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"ends", {3, 3, 3}}}), input, starts, axes);
}

TEST_CASE(slice_var_inputs_dyn_shape5)
{
    // attr starts set; inputs are (data, input_ends, input_axes)
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 6}, {0, 6}, {0, 6}}},
                 migraphx::make_op("slice", {{"starts", {0, 2}}}),
                 input,
                 ends,
                 axes);
}

TEST_CASE(slice_var_inputs_dyn_mismatch_error5)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    throws_shape(migraphx::make_op("slice", {{"starts", {0, 1, 2}}}), input, ends, axes);
}

TEST_CASE(slice_var_inputs_dyn_shape6)
Charlie Lin's avatar
Charlie Lin committed
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {2, 4, {2, 4}}, {2, 4, {2, 4}}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {2}};
    expect_shape(migraphx::shape{migraphx::shape::float_type, {{0, 6}, {0, 4}, {0, 4}}},
                 migraphx::make_op("slice"),
                 input,
                 starts,
                 ends,
                 axes);
}

3618
3619
3620
3621
3622
3623
3624
3625
3626
TEST_CASE(slice_var_inputs_dyn_mismatch_error6)
{
    migraphx::shape input{migraphx::shape::float_type, {{3, 6}, {4, 6}, {4, 6}}};
    migraphx::shape starts{migraphx::shape::int64_type, {2}};
    migraphx::shape ends{migraphx::shape::int64_type, {2}};
    migraphx::shape axes{migraphx::shape::int64_type, {3}};
    throws_shape(migraphx::make_op("slice"), input, starts, ends, axes);
}

Brian Pickrell's avatar
Brian Pickrell committed
3627
3628
TEST_CASE(slice_dyn_shape0)
{
3629
    migraphx::shape input{migraphx::shape::int32_type, {{2, 3}, {7, 7}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3630
3631

    // Slice axis 1 to size 4-1=3
3632
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {{2, 3}, {3, 3}, {2, 3}}},
Brian Pickrell's avatar
Brian Pickrell committed
3633
3634
3635
3636
3637
3638
                 migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {4}}}),
                 input);
}

TEST_CASE(slice_dyn_shape1)
{
3639
    migraphx::shape input{migraphx::shape::int32_type, {{2, 3}, {7, 7}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3640
    // Slice axis 1 with negative index
3641
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {{2, 3}, {2, 2}, {2, 3}}},
Brian Pickrell's avatar
Brian Pickrell committed
3642
3643
3644
3645
3646
3647
                 migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {-4}}}),
                 input);
}

TEST_CASE(slice_dyn_shape2)
{
3648
    migraphx::shape input{migraphx::shape::int32_type, {{2, 3}, {7, 7}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3649
    // Sliced range max bigger than dimension; is clipped
3650
    expect_shape(migraphx::shape{migraphx::shape::int32_type, {{2, 3}, {6, 6}, {2, 3}}},
Brian Pickrell's avatar
Brian Pickrell committed
3651
3652
3653
3654
3655
3656
                 migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {10}}}),
                 input);
}

TEST_CASE(slice_dyn_shape3)
{
Charlie Lin's avatar
Charlie Lin committed
3657
    // TODO: When non-fixed dimension slicing is allowed, Slice to a size smaller than min.
Brian Pickrell's avatar
Brian Pickrell committed
3658
    // Until then, this action is an error.
3659
    migraphx::shape input{migraphx::shape::int32_type, {{2, 3}, {7, 8}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3660
3661
3662
    throws_shape(migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}),
                 input);
    // clang-format off
3663
    //     expect_shape(migraphx::shape{migraphx::shape::int32_type, {{2, 3}, {1, 1}, {2, 3}}},
Brian Pickrell's avatar
Brian Pickrell committed
3664
3665
3666
3667
3668
3669
3670
    //                  migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}),
    //                  input);
    // clang-format on
}

TEST_CASE(slice_dyn_shape4)
{
3671
    migraphx::shape input{migraphx::shape::int32_type, {{2, 2}, {7, 7}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3672
3673
    // Slice multiple axes:  axis 0 to size 2-1=1 and axis 1 to size 4-1=3
    expect_shape(
3674
        migraphx::shape{migraphx::shape::int32_type, {{1, 1}, {3, 3}, {2, 3}}},
Brian Pickrell's avatar
Brian Pickrell committed
3675
3676
3677
3678
3679
3680
3681
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 1}}, {"ends", {2, 4}}}),
        input);
}

TEST_CASE(slice_dyn_shape5)
{
    // Axis out of range.
3682
    migraphx::shape input{migraphx::shape::int32_type, {{2, 2}, {7, 7}, {2, 3}}};
Brian Pickrell's avatar
Brian Pickrell committed
3683
3684
3685
    throws_shape(
        migraphx::make_op("slice", {{"axes", {0, 20}}, {"starts", {1, 1}}, {"ends", {2, 4}}}),
        input);
Shucai Xiao's avatar
Shucai Xiao committed
3686
3687
}

3688
3689
TEST_CASE(softmax_dyn0)
{
3690
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {3, 3}, {4, 4}, {5, 5}}};
3691
3692
3693
3694
3695
    expect_shape(input, migraphx::make_op("softmax", {{"axis", 0}}), input);
}

TEST_CASE(softmax_dyn1)
{
3696
    migraphx::shape input{migraphx::shape::float_type, {{1, 1}, {3, 3}, {4, 6}, {5, 8, {6}}}};
3697
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
3727
3728
3729
3730
3731
3732
3733
3734
3735
    expect_shape(input, migraphx::make_op("softmax", {{"axis", 0}}), input);
}

TEST_CASE(test_argmax)
{
    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {1, 3, 4, 5}},
                     migraphx::make_op("argmax", {{"axis", 0}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 1, 4, 5}},
                     migraphx::make_op("argmax", {{"axis", 1}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 1, 5}},
                     migraphx::make_op("argmax", {{"axis", 2}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 4, 1}},
                     migraphx::make_op("argmax", {{"axis", 3}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        throws_shape(migraphx::make_op("argmax", {{"axis", 4}}), input);
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
TEST_CASE(test_argmin)
{
    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {1, 3, 4, 5}},
                     migraphx::make_op("argmin", {{"axis", 0}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 1, 4, 5}},
                     migraphx::make_op("argmin", {{"axis", 1}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 1, 5}},
                     migraphx::make_op("argmin", {{"axis", 2}}),
                     input);
    }

    {
        migraphx::shape input{migraphx::shape::half_type, {2, 3, 4, 5}};
        expect_shape(migraphx::shape{migraphx::shape::int64_type, {2, 3, 4, 1}},
                     migraphx::make_op("argmin", {{"axis", 3}}),
                     input);
turneram's avatar
turneram committed
3764
    }
Shucai Xiao's avatar
Shucai Xiao committed
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784

    {
        migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
        throws_shape(migraphx::make_op("argmin", {{"axis", 4}}), input);
    }
}

TEST_CASE(test_scalar)
{
    migraphx::shape s1{migraphx::shape::float_type, {1}, {1}};
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 4, 5}, {0, 0, 0, 0}};
    expect_shape(s2, migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 3, 4, 5}}}), s1);
}

TEST_CASE(test_scalar_nelemnts)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 3, 4, 5}};
    throws_shape(migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 3, 4, 5}}}), input);
}

Brian Pickrell's avatar
Brian Pickrell committed
3785
3786
3787
3788
3789
3790
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
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
TEST_CASE(test_gathernd)
{
    {
        // k > r
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {2, 4}};
        migraphx::shape ds{dtype, {8}};

        int batch_dims(1);
        throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
    }

    {
        // k > r - batch_dims
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {2, 4}};
        migraphx::shape ds{dtype, {2}};

        int batch_dims(1);
        throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
    }

    {
        // batch_dims >= r
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {2, 1}};
        migraphx::shape ds{dtype, {2, 5, 6, 7}};

        int batch_dims(3);
        throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
    }

    {
        // int(q) + r - k - batch_dims - 1 = 0 => returns a scalar
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {1}};
        migraphx::shape ds{dtype, {2}};

        migraphx::shape s0{dtype, {1}};
        expect_shape(s0, migraphx::make_op("gathernd"), ds, is);
    }

    {
        // See Example 4 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {2, 2}};
        migraphx::shape ds{dtype, {2, 2}};

        migraphx::shape s0{dtype, {2}};
        expect_shape(s0, migraphx::make_op("gathernd"), ds, is);
    }

    {
        // See Example 5 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
        auto dtype = migraphx::shape::float_type;
        auto itype = migraphx::shape::int64_type;
        migraphx::shape is{itype, {2, 1}};
        migraphx::shape ds{dtype, {2, 2, 2}};

        int batch_dims(1);
        migraphx::shape s0{dtype, {2, 2}};
        expect_shape(s0, migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
    }
}

TEST_CASE(test_gathernd_dynamic0)
{
    // k > r
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 4}};
3861
    std::vector<migraphx::shape::dynamic_dimension> b{{8, 8}};
Brian Pickrell's avatar
Brian Pickrell committed
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
    migraphx::shape ds{dtype, b};

    int batch_dims(1);
    throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic1)
{
    // k > r - batch_dims
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 4}};
3874
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
    migraphx::shape ds{dtype, b};

    int batch_dims(1);
    throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic2)
{
    // batch_dims >= r
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 1}};
3887
    migraphx::shape ds{dtype, {{2, 3, {3}}, {5, 6, {5}}, {6, 9, {7}}, {7, 8, {8}}}};
Brian Pickrell's avatar
Brian Pickrell committed
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898

    int batch_dims(3);
    throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic3)
{
    // int(q) + r - k - batch_dims - 1 = 0 => returns a scalar
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {1}};
3899
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3900
3901
    migraphx::shape ds{dtype, b};

3902
    migraphx::shape::dynamic_dimension ddout{1, 1};
Brian Pickrell's avatar
Brian Pickrell committed
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd"), ds, is);
}

TEST_CASE(test_gathernd_dynamic4)
{
    // See Example 1 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 2}};
3913
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 2}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3914
3915
    migraphx::shape ds{dtype, b};

3916
    migraphx::shape::dynamic_dimension ddout{2, 2};
Brian Pickrell's avatar
Brian Pickrell committed
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd"), ds, is);
}

TEST_CASE(test_gathernd_dynamic5)
{
    // See Example 5 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
    // index static shape, data dynamic
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 1}};
3928
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 2}, {2, 2}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3929
3930
    migraphx::shape ds{dtype, b};

3931
    std::vector<migraphx::shape::dynamic_dimension> ddout{{2, 2}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
    int batch_dims(1);
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic6)
{
    // See Example 5 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
    // index dynamic shape, data static
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
3943
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 3}, {1, 1}};
Brian Pickrell's avatar
Brian Pickrell committed
3944
3945
3946
    migraphx::shape is{itype, b};
    migraphx::shape ds{dtype, {2, 2, 2}};

3947
    std::vector<migraphx::shape::dynamic_dimension> ddout{{2, 3}, {2, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
    int batch_dims(1);
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic6a)
{
    // indices with non-fixed dynamic dimension k
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
3958
    std::vector<migraphx::shape::dynamic_dimension> b{{2, 2}, {1, 3}};
Brian Pickrell's avatar
Brian Pickrell committed
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
    migraphx::shape is{itype, b};
    migraphx::shape ds{dtype, {2, 2, 2}};

    int batch_dims(1);
    throws_shape(migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic7)
{
    // See Example 5 at https://github.com/onnx/onnx/blob/main/docs/Operators.md#GatherND
    // index and data both dynamic shapes
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
3972
    std::vector<migraphx::shape::dynamic_dimension> idyn{{2, 5}, {1, 1}};
Brian Pickrell's avatar
Brian Pickrell committed
3973
    migraphx::shape is{itype, idyn};
3974
    std::vector<migraphx::shape::dynamic_dimension> bdyn{{1, 2}, {1, 2}, {1, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3975
3976
    migraphx::shape ds{dtype, bdyn};

3977
    std::vector<migraphx::shape::dynamic_dimension> ddout{{2, 5}, {1, 2}};
Brian Pickrell's avatar
Brian Pickrell committed
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
    int batch_dims(1);
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

TEST_CASE(test_gathernd_dynamic8)
{
    // Same shapes as ref_ops_test gathernd_dynamic
    // index static shape, data dynamic
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape is{itype, {2, 5, 1}};
3990
    std::vector<migraphx::shape::dynamic_dimension> b{{6, 7, {7}}, {3, 3}, {1, 4}};
Brian Pickrell's avatar
Brian Pickrell committed
3991
3992
    migraphx::shape ds{dtype, b};

3993
    std::vector<migraphx::shape::dynamic_dimension> ddout{{2, 2}, {5, 5}, {1, 4}};
Brian Pickrell's avatar
Brian Pickrell committed
3994
3995
3996
3997
3998
    int batch_dims(1);
    migraphx::shape s0{dtype, {ddout}};
    expect_shape(s0, migraphx::make_op("gathernd", {{"batch_dims", batch_dims}}), ds, is);
}

3999
TEST_CASE(test_scatternd0)
turneram's avatar
turneram committed
4000
{
4001
4002
4003
4004
4005
4006
4007
4008
    // good
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {4, 1}};
    migraphx::shape us{dtype, {4}};
    expect_shape(ds, migraphx::make_op("scatternd_none"), ds, is, us);
}
turneram's avatar
turneram committed
4009

4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
TEST_CASE(test_scatternd1)
{
    // good, broadcasted
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {4, 1}, {4, 0}};
    migraphx::shape us{dtype, {4}};
    expect_shape(ds, migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd2)
{
    // too many inputs
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {4, 1}};
    migraphx::shape us{dtype, {4}};
    migraphx::shape zs{dtype, {4}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us, zs);
}

TEST_CASE(test_scatternd3)
{
    // q + r - k - 1 matches upd_lens.size(), but k > r
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {5, 4, 2}};
    migraphx::shape us{dtype, {4}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd4)
{
    // q + r - k - 1 != upd_lens.size()
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {4, 1}};
    migraphx::shape us{dtype, {2, 2}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd5)
{
    // dimensions don't match: update.lens != indices.lens[0:q-1]
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8, 3}};
    migraphx::shape is{itype, {4, 1}};
    migraphx::shape us{dtype, {2, 2}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn0)
{
    // one dynamic input, invalid index
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {4}};
    migraphx::shape is{itype, {4, 13}};
4073
    migraphx::shape::dynamic_dimension dd{4, 4};
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
    migraphx::shape us{dtype, {dd}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn1)
{
    // one dynamic input
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {8}};
    migraphx::shape is{itype, {4, 1}};
4085
    migraphx::shape::dynamic_dimension dd{4, 4};
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
    migraphx::shape us{dtype, {dd}};
    expect_shape(ds, migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn2)
{
    // one dynamic input and broadcasted data
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {2, 3, 1, 4}, {0, 1, 1, 0}};
    migraphx::shape ds_std{dtype, {2, 3, 1, 4}};
    migraphx::shape is{itype, {4, 4}};
4098
    migraphx::shape::dynamic_dimension dd{4, 4};
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
    migraphx::shape us{dtype, {dd}};
    expect_shape(ds_std, migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn3)
{
    // one dynamic input and standard, static data
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {2, 3, 1, 4}};
    migraphx::shape is{itype, {4, 4}};
4110
    migraphx::shape::dynamic_dimension dd{4, 4};
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
    migraphx::shape us{dtype, {dd}};
    expect_shape(ds, migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn4)
{
    // index is dynamic with last dimension not fixed
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {2, 3, 1, 4}};
4121
    migraphx::shape::dynamic_dimension dd{4, 5};
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
    migraphx::shape is{itype, {dd, dd}};
    migraphx::shape us{dtype, {dd}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
}

TEST_CASE(test_scatternd_dyn5)
{
    // dimensions don't match: update.lens != indices.lens[0:q-1]
    auto dtype = migraphx::shape::float_type;
    auto itype = migraphx::shape::int64_type;
    migraphx::shape ds{dtype, {2, 3, 1, 4}};
4133
4134
    migraphx::shape::dynamic_dimension dd{4, 4};
    migraphx::shape::dynamic_dimension dbad{2, 3};
4135
4136
4137
    migraphx::shape is{itype, {dd, dd}};
    migraphx::shape us{dtype, {dbad}};
    throws_shape(migraphx::make_op("scatternd_none"), ds, is, us);
turneram's avatar
turneram committed
4138
4139
}

Shucai Xiao's avatar
Shucai Xiao committed
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
TEST_CASE(test_squeeze)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {3}}}), s1);
}

TEST_CASE(test_squeeze_all)
{
    migraphx::shape s1{migraphx::shape::float_type, {1}};
    migraphx::shape s2{migraphx::shape::float_type};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {0}}}), s1);
}

4154
4155
TEST_CASE(test_squeeze_dyn)
{
4156
4157
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {1, 1}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {3, 3}}};
4158
4159
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {3}}}), s1);

4160
    migraphx::shape s3{migraphx::shape::float_type, {{1, 4}, {3, 3}, {3, 3}}};
4161
4162
4163
4164
4165
4166
4167
    expect_shape(s3, migraphx::make_op("squeeze"), s1);

    throws_shape(migraphx::make_op("squeeze", {{"axes", {0}}}), s1);
}

TEST_CASE(test_squeeze_dyn_neg_axes)
{
4168
4169
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {1, 1}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{1, 4}, {1, 1}, {3, 3}, {3, 3}}};
4170
4171
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {-2}}}), s1);

4172
    migraphx::shape s3{migraphx::shape::float_type, {{1, 4}, {3, 3}, {3, 3}}};
4173
4174
4175
    expect_shape(s3, migraphx::make_op("squeeze", {{"axes", {-2, -4}}}), s1);
}

4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
TEST_CASE(test_squeeze_transpose)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 4, 1}, {4, 1, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 4}, {4, 1}};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {2}}}), s1);
}

TEST_CASE(test_squeeze_multibroadcast)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 1, 4}, {0, 1, 1, 0}};
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 4}, {0, 1, 0}};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {2}}}), s1);
}

TEST_CASE(test_squeeze_slice)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 1, 4}, {108, 36, 6, 1}};
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 4}, {108, 36, 1}};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {2}}}), s1);
}

Shucai Xiao's avatar
Shucai Xiao committed
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
TEST_CASE(test_squeeze_negative_axis)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 1, 3, 3}};
    expect_shape(s2, migraphx::make_op("squeeze", {{"axes", {-2}}}), s1);
}

TEST_CASE(test_squeeze_wrong_axis)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3, 1, 3}};
    throws_shape(migraphx::make_op("squeeze", {{"axes", {0}}}), s1);
}

Lakhinder Walia's avatar
Lakhinder Walia committed
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
TEST_CASE(test_unique_axis_invalid)
{
    migraphx::shape x_shape{migraphx::shape::float_type, {10, 4, 3}};
    throws_shape(migraphx::make_op("unique", {{"axis", -1}}), x_shape);
}

TEST_CASE(test_unique_axis_negative)
{
    migraphx::shape x_shape{migraphx::shape::float_type, {10, 4, 3}};

    std::vector<migraphx::shape::dynamic_dimension> y_dims{{1, 10}, {4, 4}, {3, 3}};
    std::vector<migraphx::shape::dynamic_dimension> idx_dims{{1, 10}};
    std::vector<migraphx::shape> y_dyn_shape{{migraphx::shape::float_type, y_dims},
                                             {migraphx::shape::int64_type, idx_dims},
                                             {migraphx::shape::int64_type, idx_dims},
                                             {migraphx::shape::int64_type, idx_dims}};

    expect_shape(y_dyn_shape, migraphx::make_op("unique", {{"axis", -3}}), x_shape);
}

TEST_CASE(test_unique_axis_none)
{
    migraphx::shape x_shape{migraphx::shape::half_type, {10, 4, 3}};

    std::vector<migraphx::shape::dynamic_dimension> y_dims{{1, 120}};
    std::vector<migraphx::shape::dynamic_dimension> idx_dims{{1, 120}};
    std::vector<migraphx::shape> y_dyn_shape{{migraphx::shape::half_type, y_dims},
                                             {migraphx::shape::int64_type, idx_dims},
                                             {migraphx::shape::int64_type, idx_dims},
                                             {migraphx::shape::int64_type, idx_dims}};

    expect_shape(y_dyn_shape, migraphx::make_op("unique"), x_shape);
}

Shucai Xiao's avatar
Shucai Xiao committed
4244
4245
TEST_CASE(test_unsqueeze)
{
4246
4247
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 5, 1, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
4248
4249
4250
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}}), s1);
}

4251
4252
TEST_CASE(test_unsqueeze_dyn)
{
4253
4254
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {1, 1}, {3, 3}}};
4255
4256
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}}), s1);

4257
    migraphx::shape s3{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {1, 1}, {3, 3}, {1, 1}}};
4258
4259
4260
4261
4262
4263
4264
    expect_shape(s3, migraphx::make_op("unsqueeze", {{"axes", {2, 4}}}), s1);

    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {2, 4}}, {"steps", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_dyn_neg_axes)
{
4265
4266
    migraphx::shape s1{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {1, 1}, {3, 3}}};
4267
4268
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-2}}}), s1);

4269
    migraphx::shape s3{migraphx::shape::float_type, {{1, 4, {3}}, {2, 5}, {1, 1}, {3, 3}, {1, 1}}};
4270
4271
4272
    expect_shape(s3, migraphx::make_op("unsqueeze", {{"axes", {-1, -3}}}), s1);
}

4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
TEST_CASE(test_unsqueeze_step)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 12}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 5, 2, 6}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_step_non_divisable)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 3}};
    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_step_zero)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 12}};
    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {0}}}), s1);
}

TEST_CASE(test_unsqueeze_step_at_end)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 12}};
    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {3}}, {"steps", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_mismatch_step_axis)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 12}};
    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2, 3}}}), s1);
}

4304
TEST_CASE(test_unsqueeze_negative_axis1)
Shucai Xiao's avatar
Shucai Xiao committed
4305
{
4306
4307
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 5, 1, 3}};
Shucai Xiao's avatar
Shucai Xiao committed
4308
4309
4310
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-2}}}), s1);
}

4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
TEST_CASE(test_unsqueeze_negative_axis2)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 5, 3, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-1}}}), s1);
}

TEST_CASE(test_unsqueeze_negative_axis3)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 5, 3}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 1, 5, 3}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-3}}}), s1);
}

Shucai Xiao's avatar
Shucai Xiao committed
4325
4326
4327
4328
4329
4330
4331
4332
4333
TEST_CASE(test_unsqueeze_scalar)
{
    migraphx::shape s1{migraphx::shape::float_type, {1}, {0}};
    migraphx::shape s2{migraphx::shape::float_type, {1}, {1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {0}}}), s1);
}

TEST_CASE(test_unsqueeze_scalar_tensor1)
{
4334
4335
4336
    migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}, {0, 0, 0}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 3, 1, 3}, {0, 0, 1, 0}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-2}}}), s1);
Shucai Xiao's avatar
Shucai Xiao committed
4337
4338
4339
4340
}

TEST_CASE(test_unsqueeze_scalar_tensor2)
{
4341
4342
4343
4344
4345
4346
4347
4348
4349
    migraphx::shape s1{migraphx::shape::float_type, {1, 1, 1}, {0, 0, 0}};
    migraphx::shape s2{migraphx::shape::float_type, {1, 1, 1, 1}, {0, 0, 0, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-1}}}), s1);
}

TEST_CASE(test_unsqueeze_scalar_step)
{
    migraphx::shape s{migraphx::shape::float_type, {6, 1, 2}, {0, 0, 0}};
    throws_shape(migraphx::make_op("unsqueeze", {{"axes", {0}}, {"steps", {3}}}), s);
Shucai Xiao's avatar
Shucai Xiao committed
4350
4351
}

4352
4353
4354
TEST_CASE(test_unsqueeze_transpose)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 4, 3}, {12, 1, 4}};
4355
    migraphx::shape s2{migraphx::shape::float_type, {4, 4, 1, 3}, {12, 1, 12, 4}};
4356
4357
4358
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}}), s1);
}

4359
4360
4361
4362
4363
4364
4365
TEST_CASE(test_unsqueeze_transpose_step)
{
    migraphx::shape s1{migraphx::shape::float_type, {4, 4, 6}, {24, 1, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {4, 4, 2, 3}, {24, 1, 12, 4}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), s1);
}

4366
4367
4368
TEST_CASE(test_unsqueeze_multibroadcast)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}, {0, 1, 0}};
4369
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 1, 4}, {0, 1, 0, 0}};
4370
4371
4372
4373
4374
4375
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_slice)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}, {108, 36, 1}};
4376
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 1, 4}, {108, 36, 4, 1}};
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2}}}), s1);
}

TEST_CASE(test_unsqueeze_axis_zero)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {1, 2, 3, 4}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {0}}}), s1);
}

TEST_CASE(test_unsqueeze_axis_last)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {2, 3, 4, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {-1}}}), s1);
}

TEST_CASE(test_unsqueeze_multiple_axes_1)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {1, 2, 3, 4, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {0, -1}}}), s1);
}

TEST_CASE(test_unsqueeze_multiple_axes_2)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4}};
    migraphx::shape s2{migraphx::shape::float_type, {1, 1, 2, 3, 4}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {0, 1}}}), s1);
}

4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
TEST_CASE(test_unsqueeze_multiple_axes_3)
{
    migraphx::shape s1{migraphx::shape::float_type, {3, 4, 5}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 4, 1, 5, 1, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2, 4, 5}}}), s1);
}

TEST_CASE(test_unsqueeze_multiple_axes_4)
{
    migraphx::shape s1{migraphx::shape::float_type, {3, 4, 5}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 4, 1, 5, 1, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {5, 4, 2}}}), s1);
}

TEST_CASE(test_unsqueeze_multiple_axes_step)
{
    migraphx::shape s1{migraphx::shape::float_type, {3, 4, 10}};
    migraphx::shape s2{migraphx::shape::float_type, {3, 4, 2, 5, 1, 1}};
    expect_shape(s2, migraphx::make_op("unsqueeze", {{"axes", {2, 4, 5}}, {"steps", {2}}}), s1);
}

Shucai Xiao's avatar
Shucai Xiao committed
4429
4430
4431
4432
TEST_CASE(transpose_shape)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 2}};
    migraphx::shape output{migraphx::shape::float_type, {2, 2}, {1, 2}};
4433
4434
4435
    expect_shape(input, migraphx::make_op("transpose", {{"permutation", {0, 1}}}), input);
    expect_shape(output, migraphx::make_op("transpose", {{"permutation", {1, 0}}}), input);
    throws_shape(migraphx::make_op("transpose", {{"permutation", {1, 2}}}), input);
turneram's avatar
turneram committed
4436
4437
}

Charlie Lin's avatar
Charlie Lin committed
4438
4439
TEST_CASE(transpose_dyn_shape0)
{
4440
4441
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {2, 2}}};
    migraphx::shape output{migraphx::shape::float_type, {{2, 2}, {1, 4}}};
Charlie Lin's avatar
Charlie Lin committed
4442
4443
4444
4445
4446
4447
    expect_shape(input, migraphx::make_op("transpose", {{"permutation", {0, 1}}}), input);
    expect_shape(output, migraphx::make_op("transpose", {{"permutation", {1, 0}}}), input);
}

TEST_CASE(transpose_dyn_shape1)
{
4448
4449
    migraphx::shape input{migraphx::shape::float_type, {{1, 4}, {4, 4}, {4, 4}}};
    migraphx::shape output{migraphx::shape::float_type, {{4, 4}, {4, 4}, {1, 4}}};
Charlie Lin's avatar
Charlie Lin committed
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
    expect_shape(input, migraphx::make_op("transpose", {{"permutation", {0, 1, 2}}}), input);
    expect_shape(output, migraphx::make_op("transpose", {{"permutation", {2, 1, 0}}}), input);
}

TEST_CASE(transpose_axes_error)
{
    migraphx::shape input{migraphx::shape::float_type, {2, 2}};
    throws_shape(migraphx::make_op("transpose", {{"permutation", {1}}}), input);
}

Shucai Xiao's avatar
Shucai Xiao committed
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
TEST_CASE(step_test)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 2, 4}};
    {
        migraphx::shape s2{migraphx::shape::float_type, {1, 1, 2}, {8, 8, 3}};
        expect_shape(s2, migraphx::make_op("step", {{"axes", {1, 2}}, {"steps", {2, 3}}}), s1);
    }

    {
        migraphx::shape s{migraphx::shape::float_type, {1, 2, 4}};
        throws_shape(migraphx::make_op("step", {{"axes", {1, 2}}, {"steps", {1}}}), s1);
    }

    {
        migraphx::shape s{migraphx::shape::float_type, {1, 2, 4}};
        throws_shape(migraphx::make_op("step", {{"axes", {2, 3}}, {"steps", {2, 3}}}), s1);
    }
}

4479
4480
4481
4482
4483
4484
4485
4486
4487
TEST_CASE(unary_scalar_input)
{
    migraphx::shape ss{migraphx::shape::half_type};
    expect_shape(ss, migraphx::make_op("sin"), ss);

    migraphx::shape s{migraphx::shape::float_type, {1}};
    expect_shape(s, migraphx::make_op("sin"), s);
}

4488
4489
4490
4491
4492
4493
4494
TEST_CASE(unary_broadcast_input)
{
    migraphx::shape ss{migraphx::shape::half_type, {2, 3}, {1, 0}};
    migraphx::shape s{migraphx::shape::half_type, {2, 3}};
    expect_shape(s, migraphx::make_op("sin"), ss);
}

turneram's avatar
turneram committed
4495
4496
4497
4498
4499
4500
4501
4502
TEST_CASE(where_broadcast_input)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 2}, {3, 0}};
    migraphx::shape s2{migraphx::shape::float_type, {2, 2}};
    migraphx::shape s3{migraphx::shape::bool_type, {2, 2}};
    expect_shape(s2, migraphx::make_op("where"), s3, s1, s2);
}

4503
4504
4505
TEST_CASE(where_dyn_input0)
{
    // dynamic shapes not the same
4506
4507
    migraphx::shape s1{migraphx::shape::float_type, {{2, 3}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{2, 3}, {2, 3}}};
4508
4509
4510
4511
4512
4513
4514
4515
    migraphx::shape s3{migraphx::shape::bool_type, {2, 2}};
    throws_shape(migraphx::make_op("where"), s3, s1, s2);
}

TEST_CASE(where_dyn_input1)
{
    // mixed static/dynamic inputs (not allowed)
    migraphx::shape s1{migraphx::shape::float_type, {2, 2}, {2, 1}};
4516
    migraphx::shape s2{migraphx::shape::float_type, {{2, 2}, {2, 2}}};
4517
4518
4519
4520
4521
4522
4523
    migraphx::shape s3{migraphx::shape::bool_type, {2, 2}, {2, 1}};
    throws_shape(migraphx::make_op("where"), s3, s1, s2);
}

TEST_CASE(where_dyn_input2)
{
    // dynamic shapes
4524
4525
4526
    migraphx::shape s1{migraphx::shape::float_type, {{2, 3}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{2, 3}, {3, 3}}};
    migraphx::shape s3{migraphx::shape::bool_type, {{2, 3}, {3, 3}}};
4527
4528
4529
4530
4531
4532
    expect_shape(s2, migraphx::make_op("where"), s3, s1, s2);
}

TEST_CASE(where_dyn_input3)
{
    // dynamic shapes, predicate shape is different
4533
4534
4535
    migraphx::shape s1{migraphx::shape::float_type, {{2, 3}, {3, 3}}};
    migraphx::shape s2{migraphx::shape::float_type, {{2, 3}, {3, 3}}};
    migraphx::shape s3{migraphx::shape::bool_type, {{2, 3}, {3, 4}}};
4536
4537
4538
    throws_shape(migraphx::make_op("where"), s3, s1, s2);
}

Shucai Xiao's avatar
Shucai Xiao committed
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
TEST_CASE(roialign_test)
{
    migraphx::shape sx{migraphx::shape::float_type, {3, 4, 5, 6}};
    migraphx::shape srois{migraphx::shape::float_type, {2, 4}};
    migraphx::shape sbi{migraphx::shape::int64_type, {2}};
    migraphx::shape sout{migraphx::shape::float_type, {2, 4, 1, 1}};

    expect_shape(sout, migraphx::make_op("roialign"), sx, srois, sbi);

    migraphx::shape sbi1{migraphx::shape::int64_type, {2, 3}};
    throws_shape(migraphx::make_op("roialign"), sx, srois, sbi1);

    migraphx::shape sbi2{migraphx::shape::int64_type, {3}};
    throws_shape(migraphx::make_op("roialign"), sx, srois, sbi2);

    migraphx::shape srois1{migraphx::shape::float_type, {2, 4, 3}};
    throws_shape(migraphx::make_op("roialign"), sx, srois1, sbi);

    migraphx::shape srois2{migraphx::shape::float_type, {2, 3}};
    throws_shape(migraphx::make_op("roialign"), sx, srois2, sbi);
}

4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
TEST_CASE(test_concat)
{
    migraphx::shape sx{migraphx::shape::float_type, {3, 4, 5, 6}};
    migraphx::shape sy{migraphx::shape::float_type, {3, 4, 1, 6}};
    migraphx::shape sout{migraphx::shape::float_type, {3, 4, 6, 6}};

    expect_shape(sout, migraphx::make_op("concat", {{"axis", 2}}), sx, sy);

    // axis out of range
    throws_shape(migraphx::make_op("concat", {{"axis", 11}}), sx, sy);

    // 1 input; no-op
    expect_shape(sx, migraphx::make_op("concat", {{"axis", 2}}), sx);

    // rank doesn't match
    migraphx::shape sbi1{migraphx::shape::int64_type, {2, 3}};
    throws_shape(migraphx::make_op("concat", {{"axis", 0}}), sx, sbi1);

    // non-matching dimension 2
    throws_shape(migraphx::make_op("concat", {{"axis", 1}}), sx, sy);

    // no input shapes (at least one is required)
    throws_shape(migraphx::make_op("concat", {{"axis", 0}}));
}

TEST_CASE(test_dyn_concat)
{
4588
4589
4590
    migraphx::shape sx{migraphx::shape::float_type, {{1, 3, {3}}, {4, 4}, {1, 5, {5}}, {6, 6}}};
    migraphx::shape sy{migraphx::shape::float_type, {{1, 3, {3}}, {4, 4}, {1, 4, {4}}, {6, 6}}};
    migraphx::shape sout{migraphx::shape::float_type, {{1, 3, {3}}, {4, 4}, {2, 9}, {6, 6}}};
4591
4592
4593
4594
4595
4596
4597

    expect_shape(sout, migraphx::make_op("concat", {{"axis", 2}}), sx, sy);

    // axis out of range
    throws_shape(migraphx::make_op("concat", {{"axis", 4}}), sx, sy);

    // rank doesn't match
4598
    migraphx::shape srank{migraphx::shape::int64_type, {{1, 3, {3}}, {4, 4}}};
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
    throws_shape(migraphx::make_op("concat", {{"axis", 0}}), sx, srank);

    // non-matching dimension 2
    throws_shape(migraphx::make_op("concat", {{"axis", 1}}), sx, sy);

    // static and dynamic shapes together
    migraphx::shape sstat{migraphx::shape::float_type, {3, 4, 1, 6}};
    throws_shape(migraphx::make_op("concat", {{"axis", 2}}), sx, sstat);
}

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