shape_test.cpp 33.6 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

Paul's avatar
Paul committed
25
#include <migraphx/shape.hpp>
26
#include <migraphx/serialize.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
27
#include <migraphx/ranges.hpp>
28
29
#include <migraphx/permutation.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
30
31
32
#include <array>
#include <algorithm>
#include <numeric>
33
#include <migraphx/verify.hpp>
Paul's avatar
Paul committed
34
35
#include "test.hpp"

Paul's avatar
Paul committed
36
TEST_CASE(test_shape_default)
37
{
Paul's avatar
Paul committed
38
    migraphx::shape s{};
39
40
41
    EXPECT(s.elements() == 0);
    EXPECT(s.bytes() == 0);
}
42
43
44

TEST_CASE(test_dyn_4arg_constructor)
{
45
46
47
48
49
50
51
    migraphx::shape s0{migraphx::shape::float_type, {1, 4, 4}, {4, 4, 4}, {{}, {}, {}}};
    migraphx::shape s1{migraphx::shape::float_type, {1, 4, 4}, {4, 4, 4}, {}};
    std::vector<migraphx::shape::dynamic_dimension> expected_dyn_dims = {{1, 4}, {4, 4}, {4, 4}};
    EXPECT(s0.dynamic());
    EXPECT(s0.dyn_dims() == expected_dyn_dims);
    EXPECT(s1.dynamic());
    EXPECT(s1.dyn_dims() == expected_dyn_dims);
52
53
}

Paul's avatar
Paul committed
54
TEST_CASE(test_shape_assign)
Paul's avatar
Paul committed
55
{
Paul's avatar
Paul committed
56
57
    migraphx::shape s1{migraphx::shape::float_type, {100, 32, 8, 8}};
    migraphx::shape s2 = s1; // NOLINT
Paul's avatar
Paul committed
58
    EXPECT(s1 == s2);
59
    EXPECT(not(s1 != s2));
Paul's avatar
Paul committed
60
61
}

Paul's avatar
Paul committed
62
TEST_CASE(test_shape_packed_default)
Paul's avatar
Paul committed
63
{
Paul's avatar
Paul committed
64
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
Paul's avatar
Paul committed
65
    EXPECT(s.standard());
Paul's avatar
Paul committed
66
    EXPECT(s.packed());
Paul's avatar
Paul committed
67
68
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
69
70
}

71
72
73
74
75
76
77
78
79
TEST_CASE(test_shape_standard)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 3, 1}};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

80
81
82
83
84
85
86
87
88
TEST_CASE(test_shape_standard_singleton_dim)
{
    migraphx::shape s{migraphx::shape::float_type, {5, 1, 8}, {8, 4, 1}};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

Charlie Lin's avatar
Charlie Lin committed
89
90
91
92
93
TEST_CASE(test_shape_min_max_opt)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 3, 1}};
    EXPECT(s.min_lens() == s.lens());
    EXPECT(s.max_lens() == s.lens());
94
    EXPECT(s.opt_lens().empty());
Charlie Lin's avatar
Charlie Lin committed
95
96
97
98
}

TEST_CASE(test_shape_dynamic_fixed)
{
99
    migraphx::shape s{migraphx::shape::float_type, {{2, 2}, {2, 2}, {3, 3}}};
Charlie Lin's avatar
Charlie Lin committed
100
101
102
103
104
105
106
107
108
109
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
    EXPECT(s.dynamic());
    EXPECT(s.dyn_dims().size() == 3);
    EXPECT(s.dyn_dims().at(0).is_fixed());
    EXPECT(not s.dyn_dims().at(0).has_optimal());
    EXPECT(s.min_lens() == std::vector<std::size_t>{2, 2, 3});
    EXPECT(s.max_lens() == std::vector<std::size_t>{2, 2, 3});
110
111
    std::vector<std::set<std::size_t>> e_opt_lens = {{}, {}, {}};
    EXPECT(s.opt_lens() == e_opt_lens);
Charlie Lin's avatar
Charlie Lin committed
112
113
114
115
116
117
118
    EXPECT(s.bytes() == 2 * 2 * 3 * sizeof(float));
}

TEST_CASE(test_shape_dynamic_not_fixed)
{
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims = {};
119
120
    dims.push_back(shape::dynamic_dimension{2, 5, {2}});
    dims.push_back(shape::dynamic_dimension{2, 8});
Charlie Lin's avatar
Charlie Lin committed
121
122
123
124
125
126
127
128
129
130
131
    migraphx::shape s{migraphx::shape::float_type, dims};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
    EXPECT(s.dynamic());
    EXPECT(s.dyn_dims().size() == 2);
    EXPECT(not s.dyn_dims().at(0).is_fixed());
    EXPECT(s.dyn_dims().at(0).has_optimal());
    EXPECT(s.min_lens() == std::vector<std::size_t>{2, 2});
    EXPECT(s.max_lens() == std::vector<std::size_t>{5, 8});
132
    EXPECT(s.opt_lens() == std::vector<std::set<std::size_t>>{{2}, {}});
Charlie Lin's avatar
Charlie Lin committed
133
134
135
136
137
138
    EXPECT(s.bytes() == 5 * 8 * sizeof(float));
}

TEST_CASE(test_shape_dynamic_compares)
{
    using migraphx::shape;
139
140
141
    auto a = shape::dynamic_dimension{2, 5, {2}};
    auto c = shape::dynamic_dimension{2, 5, {2}};
    auto d = shape::dynamic_dimension{3, 8};
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
    EXPECT(a == c);
    EXPECT(a != d);

    migraphx::shape s0{shape::float_type, {a, d}};
    migraphx::shape s1 = s0;
    migraphx::shape s2{shape::float_type, {a, d}};
    migraphx::shape s3{shape::int32_type, {a}};
    EXPECT(s0 == s1);
    EXPECT(s0 == s2);
    EXPECT(s0 != s3);

    std::stringstream ss0;
    std::stringstream ss1;
    std::stringstream ss3;
    ss0 << s0;
    ss1 << s1;
    ss3 << s3;
    EXPECT(ss0.str() == ss1.str());
    EXPECT(ss0.str() != ss3.str());
}

163
164
165
TEST_CASE(dynamic_dimension_size_t_compares)
{
    using migraphx::shape;
166
    auto a = shape::dynamic_dimension{2, 2, {2}};
167
168
169
170
171
    EXPECT(a == 2);
    EXPECT(a != 3);
    EXPECT(static_cast<std::size_t>(2) == a);
    EXPECT(static_cast<std::size_t>(3) != a);

172
    auto b = shape::dynamic_dimension{2, 4};
173
174
175
176
    EXPECT(b != 2);
    EXPECT(static_cast<std::size_t>(2) != b);
}

Charlie Lin's avatar
Charlie Lin committed
177
178
179
TEST_CASE(dynamic_dimension_add_sub_fixed)
{
    using migraphx::shape;
180
    auto a = shape::dynamic_dimension{2, 5, {2}};
Charlie Lin's avatar
Charlie Lin committed
181
182

    a += 3;
183
    EXPECT(a == shape::dynamic_dimension{5, 8, {5}});
Charlie Lin's avatar
Charlie Lin committed
184
    a -= 3;
185
    EXPECT(a == shape::dynamic_dimension{2, 5, {2}});
Charlie Lin's avatar
Charlie Lin committed
186

187
    auto b = shape::dynamic_dimension{3, 6, {3}};
Charlie Lin's avatar
Charlie Lin committed
188
189
190
191
    EXPECT((a + 1) == b);
    EXPECT((1 + a) == b);
    EXPECT((b - 1) == a);

192
    auto c = shape::dynamic_dimension{4, 7, {4}};
Charlie Lin's avatar
Charlie Lin committed
193
194
195
196
    EXPECT((a + 2) == c);
    EXPECT((2 + a) == c);
    EXPECT((c - 2) == a);

197
198
    auto d = shape::dynamic_dimension{4, 8};
    auto e = shape::dynamic_dimension{2, 6};
Charlie Lin's avatar
Charlie Lin committed
199
200
201
202
203
    EXPECT((d - 2) == e);
    EXPECT((e + 2) == d);
    EXPECT((2 + e) == d);
}

charlie's avatar
charlie committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
TEST_CASE(dynamic_dimension_within_range)
{
    using migraphx::shape;
    auto a = shape::dynamic_dimension{2, 5, {2, 5}};
    auto b = shape::dynamic_dimension{3, 4};
    EXPECT(b.within_range(a));
    EXPECT(not a.within_range(b));

    auto c = shape::dynamic_dimension{3, 4};
    EXPECT(c.within_range(b));
    EXPECT(b.within_range(c));

    auto d = shape::dynamic_dimension{0, std::numeric_limits<std::size_t>::max()};
    EXPECT(a.within_range(d));
    EXPECT(b.within_range(d));
    EXPECT(not d.within_range(a));
    EXPECT(not d.within_range(b));
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
TEST_CASE(dynamic_dimension_serialize)
{
    using migraphx::shape;
    auto a  = shape::dynamic_dimension{2, 5, {2, 3}};
    auto b  = shape::dynamic_dimension{3, 6, {3}};
    auto v1 = migraphx::to_value(a);
    auto v2 = migraphx::to_value(b);
    EXPECT(v1 != v2);
    auto c = migraphx::from_value<shape::dynamic_dimension>(v1);
    EXPECT(a == c);
    auto d = migraphx::from_value<shape::dynamic_dimension>(v2);
    EXPECT(b == d);
}

Charlie Lin's avatar
Charlie Lin committed
237
238
239
240
TEST_CASE(test_shape_dynamic_errors)
{
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims = {};
241
242
    dims.push_back(shape::dynamic_dimension{2, 5, {2}});
    dims.push_back(shape::dynamic_dimension{2, 8});
Charlie Lin's avatar
Charlie Lin committed
243
244
245
246
247
248
249
    migraphx::shape s{shape::float_type, dims};
    EXPECT(test::throws([&] { s.elements(); }));
    EXPECT(test::throws([&] { s.index({0, 1}); }));
    EXPECT(test::throws([&] { s.index(1); }));
    EXPECT(test::throws([&] { s.index(std::vector<std::size_t>{0, 1}); }));
    EXPECT(test::throws([&] { s.with_lens({3, 5}); }));
    EXPECT(test::throws([&] { s.with_lens(shape::float_type, {3, 5}); }));
250
251
252
253
254
255
256
257
258
    EXPECT(test::throws([&] { s.lens(); }));
    EXPECT(test::throws([&] { s.strides(); }));
}

TEST_CASE(test_shape_static_dyn_dim_error)
{
    using migraphx::shape;
    migraphx::shape s{shape::float_type, {2, 3, 4}};
    EXPECT(test::throws([&] { s.dyn_dims(); }));
Charlie Lin's avatar
Charlie Lin committed
259
260
261
262
263
264
}

TEST_CASE(test_shape_dynamic_serialize)
{
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims1 = {};
265
266
    dims1.push_back(shape::dynamic_dimension{2, 5, {2}});
    dims1.push_back(shape::dynamic_dimension{2, 8});
Charlie Lin's avatar
Charlie Lin committed
267
268
269
270
    migraphx::shape s1{shape::float_type, dims1};
    auto v1 = migraphx::to_value(s1);

    std::vector<shape::dynamic_dimension> dims2 = {};
271
    dims2.push_back(shape::dynamic_dimension{2, 5, {2}});
Charlie Lin's avatar
Charlie Lin committed
272
273
274
275
276
277
278
279
280
281
282
    migraphx::shape s2{shape::uint64_type, dims2};
    auto v2 = migraphx::to_value(s2);
    EXPECT(v1 != v2);

    auto s3 = migraphx::from_value<shape>(v1);
    EXPECT(s3 == s1);
    auto s4 = migraphx::from_value<shape>(v2);
    EXPECT(s4 == s2);
    EXPECT(s3 != s4);
}

Charlie Lin's avatar
Charlie Lin committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
TEST_CASE(any_of_dynamic_true)
{
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}}});
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s0{sub_shapes};
    EXPECT(s0.any_of_dynamic());

    sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 1}, {4, 4}}});
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s1{sub_shapes};
    EXPECT(s1.any_of_dynamic());
}

TEST_CASE(any_of_dynamic_false)
{
    std::vector<migraphx::shape> sub_shapes = {};
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {1, 4}});
    sub_shapes.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s{sub_shapes};
    EXPECT(not s.any_of_dynamic());
}

Paul's avatar
Paul committed
307
TEST_CASE(test_shape_packed)
Paul's avatar
Paul committed
308
{
Paul's avatar
Paul committed
309
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {2, 1}};
Paul's avatar
Paul committed
310
    EXPECT(s.standard());
Paul's avatar
Paul committed
311
    EXPECT(s.packed());
Paul's avatar
Paul committed
312
313
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
314
315
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
TEST_CASE(test_shape_ndim_static)
{
    migraphx::shape s0{migraphx::shape::float_type, {2, 2}};
    EXPECT(s0.ndim() == 2);

    migraphx::shape s1{migraphx::shape::float_type, {1, 2, 4, 4}};
    EXPECT(s1.ndim() == 4);

    migraphx::shape s2{migraphx::shape::float_type, {2, 4, 4, 1, 3}};
    EXPECT(s2.ndim() == 5);
}

TEST_CASE(test_shape_ndim_dyn)
{
330
    migraphx::shape s0{migraphx::shape::float_type, {{2, 2}, {2, 2}}};
331
332
    EXPECT(s0.ndim() == 2);

333
    migraphx::shape s1{migraphx::shape::float_type, {{1, 1}, {2, 4}, {2, 4}, {2, 4}}};
334
335
    EXPECT(s1.ndim() == 4);

336
    migraphx::shape s2{migraphx::shape::float_type, {{1, 1}, {2, 4}, {2, 4}, {1, 1}, {3, 3}}};
337
338
339
    EXPECT(s2.ndim() == 5);
}

340
341
342
343
344
345
346
347
348
TEST_CASE(test_shape_non_packed_single_dim)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 64, 35, 35}, {156800, 1225, 35, 1}};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

Paul's avatar
Paul committed
349
TEST_CASE(test_shape_transposed1)
Paul's avatar
Paul committed
350
{
Paul's avatar
Paul committed
351
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 2}};
Paul's avatar
Paul committed
352
353
354
355
356
357
    EXPECT(not s.standard());
    EXPECT(s.packed());
    EXPECT(s.transposed());
    EXPECT(not s.broadcasted());
}

Paul's avatar
Paul committed
358
359
360
361
362
363
364
365
366
TEST_CASE(test_shape_transposed2)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 1, 1, 1, 2}, {2, 2, 2, 2, 1}};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

367
368
369
370
TEST_CASE(test_shape_static_to_dynamic)
{
    migraphx::shape s0{migraphx::shape::float_type, {1, 2, 4, 4}};
    migraphx::shape s1 = s0.to_dynamic();
371
    migraphx::shape s2{migraphx::shape::float_type, {{1, 1}, {2, 2}, {4, 4}, {4, 4}}};
372
373
374
375
376
    EXPECT(s1 == s2);
}

TEST_CASE(test_shape_dyn_to_dynamic)
{
377
    migraphx::shape s0{migraphx::shape::float_type, {{1, 1}, {2, 4}, {2, 4}, {2, 4}}};
378
379
380
381
    migraphx::shape s1 = s0.to_dynamic();
    EXPECT(s0 == s1);
}

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
TEST_CASE(test_shape_subshapes_to_dynamic)
{
    std::vector<migraphx::shape> sub_shapes0 = {};
    sub_shapes0.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}}});
    sub_shapes0.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s0{sub_shapes0};
    migraphx::shape s1                       = s0.to_dynamic();
    std::vector<migraphx::shape> sub_shapes1 = {};
    sub_shapes1.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}}});
    sub_shapes1.push_back(migraphx::shape{migraphx::shape::float_type, {{3, 3}, {4, 4}, {5, 5}}});
    migraphx::shape s2{sub_shapes1};
    EXPECT(s1 == s2);
}

TEST_CASE(test_shape_dyn_to_static)
{
    migraphx::shape s0{migraphx::shape::float_type, {{1, 1}, {2, 2}, {2, 10}, {2, 10}}};
    migraphx::shape s1 = s0.to_static(4);
    migraphx::shape s2{migraphx::shape::float_type, {1, 2, 4, 4}};
    EXPECT(s1 == s2);
}

TEST_CASE(test_shape_static_to_static)
{
    migraphx::shape s0{migraphx::shape::float_type, {1, 2, 4, 4}};
    migraphx::shape s1 = s0.to_static(8);
    EXPECT(s0 == s1);
}

TEST_CASE(test_shape_subshapes_to_static)
{
    std::vector<migraphx::shape> sub_shapes0 = {};
    sub_shapes0.push_back(migraphx::shape{migraphx::shape::float_type, {{1, 4}, {4, 4}}});
    sub_shapes0.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s0{sub_shapes0};
    migraphx::shape s1                       = s0.to_static(3);
    std::vector<migraphx::shape> sub_shapes1 = {};
    sub_shapes1.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4}});
    sub_shapes1.push_back(migraphx::shape{migraphx::shape::float_type, {3, 4, 5}});
    migraphx::shape s2{sub_shapes1};
    EXPECT(s1 == s2);
}

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
TEST_CASE(test_shape_overlap)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 3, 2}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

TEST_CASE(test_shape_overlap2)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 2, 1}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

TEST_CASE(test_shape_overlap3)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {4, 2, 1}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
}

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
TEST_CASE(test_shape_scalar1)
{
    migraphx::shape s{migraphx::shape::float_type};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

TEST_CASE(test_shape_scalar2)
{
    migraphx::shape s{migraphx::shape::float_type, {1}, {0}};
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

TEST_CASE(test_shape_scalar_broadcast)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 2, 3, 3}, {0, 0, 0, 0}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

Paul's avatar
Paul committed
479
TEST_CASE(test_shape_broadcasted)
Paul's avatar
Paul committed
480
{
Paul's avatar
Paul committed
481
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 0}};
Paul's avatar
Paul committed
482
    EXPECT(not s.standard());
Paul's avatar
Paul committed
483
    EXPECT(not s.packed());
Paul's avatar
Paul committed
484
485
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
Paul's avatar
Paul committed
486
487
}

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
TEST_CASE(test_shape_broadcasted2)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 2}, {0, 1}};
    EXPECT(not s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

TEST_CASE(test_shape_broadcasted3)
{
    migraphx::shape s{migraphx::shape::float_type, {3, 2}, {0, 1}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

TEST_CASE(test_shape_broadcasted4)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 0, 1}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

TEST_CASE(test_shape_broadcasted5)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {1, 0, 6}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(s.transposed());
    EXPECT(s.broadcasted());
}

524
525
526
527
528
529
530
531
532
TEST_CASE(test_shape_step_broadcasted)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {0, 3}};
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
}

Paul's avatar
Paul committed
533
TEST_CASE(test_shape_default_copy)
Paul's avatar
Paul committed
534
{
Paul's avatar
Paul committed
535
536
    migraphx::shape s1{};
    migraphx::shape s2{};
Paul's avatar
Paul committed
537
    EXPECT(s1 == s2);
538
    EXPECT(not(s1 != s2));
Paul's avatar
Paul committed
539
540
}

541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
TEST_CASE(test_shape_normalize_standard1)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2, 3}, {6, 3, 1}};
    EXPECT(s.standard());
    auto n = s.normalize_standard();
    EXPECT(n == s);
}

TEST_CASE(test_shape_normalize_standard2)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 64, 35, 35}, {156800, 1225, 35, 1}};
    EXPECT(s.standard());
    auto n = s.normalize_standard();
    EXPECT(n.standard());
    EXPECT(n != s);
    EXPECT(n.lens() == s.lens());
    EXPECT(n.type() == s.type());
}

TEST_CASE(test_shape_normalize_standard3)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 2}};
    EXPECT(not s.standard());
    auto n = s.normalize_standard();
    EXPECT(n == s);
}

TEST_CASE(test_shape_normalize_scalar1)
{
    migraphx::shape s{migraphx::shape::float_type};
    EXPECT(s.standard());
    EXPECT(s.scalar());
    auto n = s.normalize_standard();
    EXPECT(n != s);
    EXPECT(n.standard());
    EXPECT(not n.scalar());
}

TEST_CASE(test_shape_normalize_scalar2)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {0, 0}};
    EXPECT(not s.standard());
    EXPECT(s.scalar());
    auto n = s.normalize_standard();
    EXPECT(n == s);
}

Paul's avatar
Paul committed
588
TEST_CASE(test_shape4)
Paul's avatar
Paul committed
589
{
Paul's avatar
Paul committed
590
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}};
Paul's avatar
Paul committed
591
    EXPECT(s.standard());
Paul's avatar
Paul committed
592
    EXPECT(s.packed());
Paul's avatar
Paul committed
593
594
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
595
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
596
597
598
599
600
601
602
603
    EXPECT(s.lens()[0] == 100);
    EXPECT(s.lens()[1] == 32);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == s.lens()[1] * s.strides()[1]);
    EXPECT(s.strides()[1] == s.lens()[2] * s.strides()[2]);
    EXPECT(s.strides()[2] == s.lens()[3] * s.strides()[3]);
    EXPECT(s.strides()[3] == 1);
Paul's avatar
Paul committed
604
605
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
Paul's avatar
Paul committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == s.index(0));
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
    EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 32));
    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index(8) == 8);
    EXPECT(s.index(8 * 8) == 8 * 8);
    EXPECT(s.index(8 * 8 * 32) == 8 * 8 * 32);
    EXPECT(s.index(s.elements() - 1) == s.elements() - 1);
}

Paul's avatar
Paul committed
621
TEST_CASE(test_shape42)
Paul's avatar
Paul committed
622
{
Paul's avatar
Paul committed
623
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}, {2048, 64, 8, 1}};
Paul's avatar
Paul committed
624
625
626
627
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
628
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    EXPECT(s.lens()[0] == 100);
    EXPECT(s.lens()[1] == 32);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == s.lens()[1] * s.strides()[1]);
    EXPECT(s.strides()[1] == s.lens()[2] * s.strides()[2]);
    EXPECT(s.strides()[2] == s.lens()[3] * s.strides()[3]);
    EXPECT(s.strides()[3] == 1);
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == s.index(0));
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
    EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 32));
    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index(8) == 8);
    EXPECT(s.index(8 * 8) == 8 * 8);
    EXPECT(s.index(8 * 8 * 32) == 8 * 8 * 32);
    EXPECT(s.index(s.elements() - 1) == s.elements() - 1);
}

Paul's avatar
Paul committed
654
TEST_CASE(test_shape4_transposed)
Paul's avatar
Paul committed
655
{
Paul's avatar
Paul committed
656
    migraphx::shape s{migraphx::shape::float_type, {32, 100, 8, 8}, {64, 2048, 8, 1}};
Paul's avatar
Paul committed
657
658
659
660
    EXPECT(s.transposed());
    EXPECT(s.packed());
    EXPECT(not s.standard());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
661
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
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
    EXPECT(s.lens()[0] == 32);
    EXPECT(s.lens()[1] == 100);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == 64);
    EXPECT(s.strides()[1] == 2048);
    EXPECT(s.strides()[2] == 8);
    EXPECT(s.strides()[3] == 1);
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == s.index(0));
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
    EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 100));
    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index(8) == 8);
    EXPECT(s.index(8 * 8) == 2048);
    EXPECT(s.index(8 * 8 * 100) == 64);
    EXPECT(s.index(s.elements() - 1) == s.elements() - 1);
}

Paul's avatar
Paul committed
687
TEST_CASE(test_shape4_nonpacked)
Paul's avatar
Paul committed
688
689
{
    std::vector<std::size_t> lens       = {100, 32, 8, 8};
Paul's avatar
Paul committed
690
691
    std::array<std::size_t, 4> offsets  = {{5, 10, 0, 6}};
    std::array<std::size_t, 4> adj_lens = {{0, 0, 0, 0}};
Paul's avatar
Paul committed
692
693
694
695
696
697

    std::transform(
        lens.begin(), lens.end(), offsets.begin(), adj_lens.begin(), std::plus<size_t>());
    // adj_lens should be: { 105, 42, 8, 14 }
    std::vector<std::size_t> strides(4);
    strides.back() = 1;
Paul's avatar
Paul committed
698
699
700
701
    std::partial_sum(adj_lens.rbegin(),
                     adj_lens.rend() - 1,
                     strides.rbegin() + 1,
                     std::multiplies<std::size_t>());
Paul's avatar
Paul committed
702

Paul's avatar
Paul committed
703
    migraphx::shape s{migraphx::shape::float_type, lens, strides};
Paul's avatar
Paul committed
704
705
706
707
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
708
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
    EXPECT(s.lens()[0] == 100);
    EXPECT(s.lens()[1] == 32);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == 4704);
    EXPECT(s.strides()[1] == 112);
    EXPECT(s.strides()[2] == 14);
    EXPECT(s.strides()[3] == 1);
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == sizeof(float) * 469274);

    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
Paul's avatar
Paul committed
724
725
726
727
    EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 32));
    EXPECT(s.index(s.elements() - 1) == 469273);
Paul's avatar
Paul committed
728
729
}

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
TEST_CASE(test_serialize)
{
    migraphx::shape s1{migraphx::shape::float_type, {100, 32, 8, 8}};
    auto v1 = migraphx::to_value(s1);
    migraphx::shape s2{migraphx::shape::uint64_type, {2, 2}};
    auto v2 = migraphx::to_value(s2);
    EXPECT(v1 != v2);

    auto s3 = migraphx::from_value<migraphx::shape>(v1);
    EXPECT(s3 == s1);
    auto s4 = migraphx::from_value<migraphx::shape>(v2);
    EXPECT(s4 == s2);
    EXPECT(s3 != s4);
}

Paul Fultz II's avatar
Paul Fultz II committed
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
TEST_CASE(tuple)
{
    migraphx::shape s{{migraphx::shape{migraphx::shape::float_type},
                       migraphx::shape{migraphx::shape::int8_type}}};
    EXPECT(s.type() == migraphx::shape::tuple_type);
    EXPECT(s.bytes() == 4 + 1);
    EXPECT(s.type_size() == 0);
    EXPECT(s.type_string() == "tuple_type");
    EXPECT(s.lens().empty());
    EXPECT(s.strides().empty());
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.broadcasted());
    EXPECT(not s.transposed());
    EXPECT(not s.scalar());
    EXPECT(s.sub_shapes().size() == 2);
    EXPECT(s.sub_shapes()[0].type() == migraphx::shape::float_type);
    EXPECT(s.sub_shapes()[0].elements() == 1);
    EXPECT(s.sub_shapes()[1].type() == migraphx::shape::int8_type);
    EXPECT(s.sub_shapes()[1].elements() == 1);
    EXPECT(test::throws([&] { s.visit_type([](auto) {}); }));
}

TEST_CASE(tuple_copy)
{
    migraphx::shape s1{{migraphx::shape{migraphx::shape::float_type},
                        migraphx::shape{migraphx::shape::int8_type}}};
    migraphx::shape s2{{migraphx::shape{migraphx::shape::float_type},
                        migraphx::shape{migraphx::shape::int8_type}}};
    EXPECT(s1 == s2);
    auto s3 = s1;
    EXPECT(s3 == s1);
    EXPECT(s3 == s2);
    migraphx::shape s4{{migraphx::shape{migraphx::shape::int8_type},
                        migraphx::shape{migraphx::shape::float_type}}};
    EXPECT(s4 != s1);
    EXPECT(s4 != s2);
    EXPECT(s4 != s3);
}

TEST_CASE(tuple_print)
{
    migraphx::shape s{{migraphx::shape{migraphx::shape::float_type},
                       migraphx::shape{migraphx::shape::int8_type}}};
    std::string x = migraphx::to_string(s);
    EXPECT(x.front() == '[');
    EXPECT(x.back() == ']');
    EXPECT(migraphx::contains(x, "float"));
    EXPECT(migraphx::contains(x, "int8"));
}

TEST_CASE(tuple_serialize)
{
    migraphx::shape s1{{migraphx::shape{migraphx::shape::float_type},
                        migraphx::shape{migraphx::shape::int8_type}}};
    migraphx::shape s2{{migraphx::shape{migraphx::shape::int8_type},
                        migraphx::shape{migraphx::shape::float_type}}};
    auto v1 = migraphx::to_value(s1);
    auto v2 = migraphx::to_value(s2);
    EXPECT(v1 != v2);

    auto s3 = migraphx::from_value<migraphx::shape>(v1);
    EXPECT(s3 == s1);
    auto s4 = migraphx::from_value<migraphx::shape>(v2);
    EXPECT(s4 == s2);
    EXPECT(s3 != s4);
}

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
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
TEST_CASE(test_with_lens1)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 2}, {1, 2}};
    auto s2 = s1.with_lens({4, 3});
    EXPECT(s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {4, 3}, {1, 4}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens2)
{
    migraphx::shape s1{migraphx::shape::float_type, {2, 2}, {2, 1}};
    auto s2 = s1.with_lens({3, 4});
    EXPECT(s2.standard());
    migraphx::shape s3{migraphx::shape::float_type, {3, 4}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous1)
{
    migraphx::shape s1{migraphx::shape::float_type, {64, 1, 24, 24}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(not s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous2)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 1}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous3)
{
    migraphx::shape s1{migraphx::shape::float_type, {64, 3, 1, 1}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(not s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous4)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {64, 1, 1, 3}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous5)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 5, 24, 24}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(not s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous6)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {1, 24, 24, 5}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous7)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {1, 1, 1, 3}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous8)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 1, 24, 24}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(not s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous9)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {1, 24, 24, 1}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous10)
{
    migraphx::shape s1{migraphx::shape::float_type, {3, 2, 4, 1}};
    auto s2 = s1.with_lens({3, 2, 4, 1});
    EXPECT(not s2.transposed());
    migraphx::shape s3{migraphx::shape::float_type, {3, 2, 4, 1}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous11)
{
    migraphx::shape s1{migraphx::shape::float_type, {64, 1, 1, 1}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s1.standard());
    EXPECT(s2.standard());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous12)
{
    migraphx::shape s1{migraphx::shape::float_type, {1, 64, 1, 1}};
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s1.standard());
    EXPECT(s2.standard());
    migraphx::shape s3{migraphx::shape::float_type, {64, 3, 24, 24}};
    EXPECT(s2 == s3);
}

TEST_CASE(test_with_lens_ambigous13)
{
    auto s1 = migraphx::reorder_shape({migraphx::shape::float_type, {1, 1, 1, 3}}, {0, 3, 1, 2});
    auto s2 = s1.with_lens({64, 3, 24, 24});
    EXPECT(s2.transposed());
    migraphx::shape s3 =
        migraphx::reorder_shape({migraphx::shape::float_type, {64, 24, 24, 3}}, {0, 3, 1, 2});
    EXPECT(s2 == s3);
}

Paul Fultz II's avatar
Paul Fultz II committed
956
957
958
959
960
961
962
963
TEST_CASE(cpp_type_name)
{
    EXPECT(migraphx::shape::cpp_type(migraphx::shape::int8_type) == "int8_t");
    EXPECT(migraphx::shape::cpp_type(migraphx::shape::float_type) == "float");
    EXPECT(migraphx::shape::cpp_type(migraphx::shape::half_type) == "half");
    EXPECT(test::throws([&] { migraphx::shape::cpp_type(migraphx::shape::tuple_type); }));
}

964
965
966
967
968
969
970
971
972
973
974
TEST_CASE(test_with_type)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 0}};
    EXPECT(s.type() == migraphx::shape::float_type);
    auto new_s = s.with_type(migraphx::shape::half_type);
    EXPECT(s.type() == migraphx::shape::float_type);
    EXPECT(s.type() != new_s.type());
    EXPECT(s.lens() == new_s.lens());
    EXPECT(s.strides() == new_s.strides());
}

975
976
977
TEST_CASE(test_multi_index)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 4, 6}};
978
979
980
981
982
983
984
    EXPECT(migraphx::verify::verify_rms_range(s.multi(0), std::vector<size_t>{0, 0, 0}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(4), std::vector<size_t>{0, 0, 4}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(6), std::vector<size_t>{0, 1, 0}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(8), std::vector<size_t>{0, 1, 2}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(24), std::vector<size_t>{1, 0, 0}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(30), std::vector<size_t>{1, 1, 0}));
    EXPECT(migraphx::verify::verify_rms_range(s.multi(34), std::vector<size_t>{1, 1, 4}));
985
986
}

987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
TEST_CASE(find_permutation_2d_standard)
{
    migraphx::shape s                = {migraphx::shape::float_type, {2, 3}};
    std::vector<int64_t> permutation = {0, 1};
    EXPECT(migraphx::find_permutation(s) == permutation);
}

TEST_CASE(find_permutation_2d_transpose)
{
    migraphx::shape s                = {migraphx::shape::float_type, {2, 3}, {1, 2}};
    std::vector<int64_t> permutation = {1, 0};
    EXPECT(migraphx::find_permutation(s) == permutation);
}

TEST_CASE(find_permutation_3d)
{
    migraphx::shape s                = {migraphx::shape::float_type, {2, 3, 4}, {1, 8, 2}};
    std::vector<int64_t> permutation = {1, 2, 0};
    EXPECT(migraphx::find_permutation(s) == permutation);
}

TEST_CASE(find_permutation_4d)
{
    // ori_lens = 2, 3, 4, 5
    // ori_strides = 60, 20, 5, 1
    // perm = 3, 2, 0, 1
    // inv_perm = 2, 3, 1, 0
    // out_strides = 5, 1, 20, 60
    migraphx::shape s                = {migraphx::shape::float_type, {5, 4, 2, 3}, {5, 1, 20, 60}};
    std::vector<int64_t> permutation = {3, 2, 0, 1};
    EXPECT(migraphx::find_permutation(s) == permutation);
}

TEST_CASE(from_2d_permutation)
{
    std::vector<std::size_t> out_lens = {2, 3};
    std::vector<int64_t> permutation  = {1, 0};
    migraphx::shape out_shape =
        migraphx::shape::from_permutation(migraphx::shape::float_type, out_lens, permutation);
    EXPECT(out_shape.lens() == out_lens);
    EXPECT(migraphx::find_permutation(out_shape) == permutation);
}

TEST_CASE(from_3d_permutation)
{
    std::vector<std::size_t> out_lens = {2, 3, 4};
    std::vector<int64_t> permutation  = {1, 2, 0};
    migraphx::shape out_shape =
        migraphx::shape::from_permutation(migraphx::shape::float_type, out_lens, permutation);
    EXPECT(out_shape.lens() == out_lens);
    EXPECT(migraphx::find_permutation(out_shape) == permutation);
}

TEST_CASE(from_4d_permutation)
{
    std::vector<std::size_t> out_lens = {5, 4, 2, 3};
    std::vector<int64_t> permutation  = {3, 2, 0, 1};
    migraphx::shape out_shape =
        migraphx::shape::from_permutation(migraphx::shape::float_type, out_lens, permutation);
    EXPECT(out_shape.lens() == out_lens);
    EXPECT(migraphx::find_permutation(out_shape) == permutation);
}

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