shape_test.cpp 22.3 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraphx/shape.hpp>
3
#include <migraphx/serialize.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
4
#include <migraphx/ranges.hpp>
5
6
#include <migraphx/permutation.hpp>
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
7
8
9
#include <array>
#include <algorithm>
#include <numeric>
Paul's avatar
Paul committed
10
11
#include "test.hpp"

Paul's avatar
Paul committed
12
TEST_CASE(test_shape_default)
13
{
Paul's avatar
Paul committed
14
    migraphx::shape s{};
15
16
17
    EXPECT(s.elements() == 0);
    EXPECT(s.bytes() == 0);
}
Paul's avatar
Paul committed
18
TEST_CASE(test_shape_assign)
Paul's avatar
Paul committed
19
{
Paul's avatar
Paul committed
20
21
    migraphx::shape s1{migraphx::shape::float_type, {100, 32, 8, 8}};
    migraphx::shape s2 = s1; // NOLINT
Paul's avatar
Paul committed
22
23
24
25
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

Paul's avatar
Paul committed
26
TEST_CASE(test_shape_packed_default)
Paul's avatar
Paul committed
27
{
Paul's avatar
Paul committed
28
    migraphx::shape s{migraphx::shape::float_type, {2, 2}};
Paul's avatar
Paul committed
29
    EXPECT(s.standard());
Paul's avatar
Paul committed
30
    EXPECT(s.packed());
Paul's avatar
Paul committed
31
32
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
33
34
}

35
36
37
38
39
40
41
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());
42
43
44
45
}

TEST_CASE(test_shape_dynamic_fixed)
{
46
    migraphx::shape s{migraphx::shape::float_type, {{2, 2, 0}, {2, 2, 0}, {3, 3, 0}}};
47
48
49
50
51
52
53
54
    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());
charlie's avatar
charlie committed
55
56
57
    EXPECT(s.min_lens() == std::vector<std::size_t>{2, 2, 3});
    EXPECT(s.max_lens() == std::vector<std::size_t>{2, 2, 3});
    EXPECT(s.opt_lens() == std::vector<std::size_t>{0, 0, 0});
charlie's avatar
charlie committed
58
    EXPECT(s.bytes() == 2 * 2 * 3 * sizeof(float));
59
60
61
62
}

TEST_CASE(test_shape_dynamic_not_fixed)
{
63
64
65
66
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims = {};
    dims.push_back(shape::dynamic_dimension{2, 5, 2});
    dims.push_back(shape::dynamic_dimension{2, 8, 0});
67
    migraphx::shape s{migraphx::shape::float_type, dims};
68
69
70
71
72
73
74
75
    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());
charlie's avatar
charlie committed
76
77
78
    EXPECT(s.min_lens() == std::vector<std::size_t>{2, 2});
    EXPECT(s.max_lens() == std::vector<std::size_t>{5, 8});
    EXPECT(s.opt_lens() == std::vector<std::size_t>{2, 0});
charlie's avatar
charlie committed
79
    EXPECT(s.bytes() == 5 * 8 * sizeof(float));
charlie's avatar
charlie committed
80
81
82
83
}

TEST_CASE(test_shape_dynamic_compares)
{
84
85
    using migraphx::shape;
    auto a = shape::dynamic_dimension{2, 5, 2};
charlie's avatar
charlie committed
86
    auto b = a;
87
88
    auto c = shape::dynamic_dimension{2, 5, 2};
    auto d = shape::dynamic_dimension{3, 8, 4};
charlie's avatar
charlie committed
89
90
91
92
    EXPECT(a == b);
    EXPECT(a == c);
    EXPECT(a != d);

93
    migraphx::shape s0{shape::float_type, {a, d}};
charlie's avatar
charlie committed
94
    migraphx::shape s1 = s0;
95
96
    migraphx::shape s2{shape::float_type, {a, d}};
    migraphx::shape s3{shape::int32_type, {a}};
charlie's avatar
charlie committed
97
98
99
    EXPECT(s0 == s1);
    EXPECT(s0 == s2);
    EXPECT(s0 != s3);
charlie's avatar
charlie committed
100
101
102
103
104
105
106
107
108

    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());
charlie's avatar
charlie committed
109
110
111
112
}

TEST_CASE(test_shape_dynamic_errors)
{
113
114
115
116
117
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims = {};
    dims.push_back(shape::dynamic_dimension{2, 5, 2});
    dims.push_back(shape::dynamic_dimension{2, 8, 0});
    migraphx::shape s{shape::float_type, dims};
charlie's avatar
charlie committed
118
119
120
121
    EXPECT(test::throws([&] { s.elements(); }));
    EXPECT(test::throws([&] { s.index({0, 1}); }));
    EXPECT(test::throws([&] { s.index(1); }));
    EXPECT(test::throws([&] { s.with_lens({3, 5}); }));
122
    EXPECT(test::throws([&] { s.with_lens(shape::float_type, {3, 5}); }));
charlie's avatar
charlie committed
123
124
125
126
}

TEST_CASE(test_shape_dynamic_serialize)
{
127
128
129
130
131
    using migraphx::shape;
    std::vector<shape::dynamic_dimension> dims1 = {};
    dims1.push_back(shape::dynamic_dimension{2, 5, 2});
    dims1.push_back(shape::dynamic_dimension{2, 8, 0});
    migraphx::shape s1{shape::float_type, dims1};
charlie's avatar
charlie committed
132
133
    auto v1 = migraphx::to_value(s1);

134
135
136
    std::vector<shape::dynamic_dimension> dims2 = {};
    dims2.push_back(shape::dynamic_dimension{2, 5, 2});
    migraphx::shape s2{shape::uint64_type, dims2};
charlie's avatar
charlie committed
137
138
139
    auto v2 = migraphx::to_value(s2);
    EXPECT(v1 != v2);

140
    auto s3 = migraphx::from_value<shape>(v1);
charlie's avatar
charlie committed
141
    EXPECT(s3 == s1);
142
    auto s4 = migraphx::from_value<shape>(v2);
charlie's avatar
charlie committed
143
144
    EXPECT(s4 == s2);
    EXPECT(s3 != s4);
145
146
}

Paul's avatar
Paul committed
147
TEST_CASE(test_shape_packed)
Paul's avatar
Paul committed
148
{
Paul's avatar
Paul committed
149
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {2, 1}};
Paul's avatar
Paul committed
150
    EXPECT(s.standard());
Paul's avatar
Paul committed
151
    EXPECT(s.packed());
Paul's avatar
Paul committed
152
153
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
154
155
}

156
157
158
159
160
161
162
163
164
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
165
TEST_CASE(test_shape_transposed1)
Paul's avatar
Paul committed
166
{
Paul's avatar
Paul committed
167
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 2}};
Paul's avatar
Paul committed
168
169
170
171
172
173
    EXPECT(not s.standard());
    EXPECT(s.packed());
    EXPECT(s.transposed());
    EXPECT(not s.broadcasted());
}

Paul's avatar
Paul committed
174
175
176
177
178
179
180
181
182
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());
}

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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());
}

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
237
TEST_CASE(test_shape_broadcasted)
Paul's avatar
Paul committed
238
{
Paul's avatar
Paul committed
239
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 0}};
Paul's avatar
Paul committed
240
    EXPECT(not s.standard());
Paul's avatar
Paul committed
241
    EXPECT(not s.packed());
Paul's avatar
Paul committed
242
243
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
Paul's avatar
Paul committed
244
245
}

246
247
248
249
250
251
252
253
254
255
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
281
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());
}

Paul's avatar
Paul committed
282
TEST_CASE(test_shape_default_copy)
Paul's avatar
Paul committed
283
{
Paul's avatar
Paul committed
284
285
    migraphx::shape s1{};
    migraphx::shape s2{};
Paul's avatar
Paul committed
286
287
288
289
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
337
TEST_CASE(test_shape4)
Paul's avatar
Paul committed
338
{
Paul's avatar
Paul committed
339
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}};
Paul's avatar
Paul committed
340
    EXPECT(s.standard());
Paul's avatar
Paul committed
341
    EXPECT(s.packed());
Paul's avatar
Paul committed
342
343
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
344
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
345
346
347
348
349
350
351
352
    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
353
354
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
Paul's avatar
Paul committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    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
370
TEST_CASE(test_shape42)
Paul's avatar
Paul committed
371
{
Paul's avatar
Paul committed
372
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}, {2048, 64, 8, 1}};
Paul's avatar
Paul committed
373
374
375
376
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
377
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    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
403
TEST_CASE(test_shape4_transposed)
Paul's avatar
Paul committed
404
{
Paul's avatar
Paul committed
405
    migraphx::shape s{migraphx::shape::float_type, {32, 100, 8, 8}, {64, 2048, 8, 1}};
Paul's avatar
Paul committed
406
407
408
409
    EXPECT(s.transposed());
    EXPECT(s.packed());
    EXPECT(not s.standard());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
410
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    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
436
TEST_CASE(test_shape4_nonpacked)
Paul's avatar
Paul committed
437
438
{
    std::vector<std::size_t> lens       = {100, 32, 8, 8};
Paul's avatar
Paul committed
439
440
    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
441
442
443
444
445
446

    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
447
448
449
450
    std::partial_sum(adj_lens.rbegin(),
                     adj_lens.rend() - 1,
                     strides.rbegin() + 1,
                     std::multiplies<std::size_t>());
Paul's avatar
Paul committed
451

Paul's avatar
Paul committed
452
    migraphx::shape s{migraphx::shape::float_type, lens, strides};
Paul's avatar
Paul committed
453
454
455
456
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
457
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    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
473
474
475
476
    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
477
478
}

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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);
}

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
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
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
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
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
705
706
707
708
709
710
711
712
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); }));
}

713
714
715
716
717
718
719
720
721
722
723
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());
}

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