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

Paul's avatar
Paul committed
2
#include <migraphx/shape.hpp>
Paul's avatar
Paul committed
3
4
5
#include <array>
#include <algorithm>
#include <numeric>
Paul's avatar
Paul committed
6
7
#include "test.hpp"

Paul's avatar
Paul committed
8
TEST_CASE(test_shape_default)
9
{
Paul's avatar
Paul committed
10
    migraphx::shape s{};
11
12
13
14
    EXPECT(s.elements() == 0);
    EXPECT(s.bytes() == 0);
}

Paul's avatar
Paul committed
15
TEST_CASE(test_shape_assign)
Paul's avatar
Paul committed
16
{
Paul's avatar
Paul committed
17
18
    migraphx::shape s1{migraphx::shape::float_type, {100, 32, 8, 8}};
    migraphx::shape s2 = s1; // NOLINT
Paul's avatar
Paul committed
19
20
21
22
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

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

32
33
34
35
36
37
38
39
40
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());
}

Paul's avatar
Paul committed
41
TEST_CASE(test_shape_packed)
Paul's avatar
Paul committed
42
{
Paul's avatar
Paul committed
43
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {2, 1}};
Paul's avatar
Paul committed
44
    EXPECT(s.standard());
Paul's avatar
Paul committed
45
    EXPECT(s.packed());
Paul's avatar
Paul committed
46
47
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
48
49
}

Paul's avatar
Paul committed
50
TEST_CASE(test_shape_transposed1)
Paul's avatar
Paul committed
51
{
Paul's avatar
Paul committed
52
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 2}};
Paul's avatar
Paul committed
53
54
55
56
57
58
    EXPECT(not s.standard());
    EXPECT(s.packed());
    EXPECT(s.transposed());
    EXPECT(not s.broadcasted());
}

Paul's avatar
Paul committed
59
60
61
62
63
64
65
66
67
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());
}

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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());
}

Paul's avatar
Paul committed
95
TEST_CASE(test_shape_broadcasted)
Paul's avatar
Paul committed
96
{
Paul's avatar
Paul committed
97
    migraphx::shape s{migraphx::shape::float_type, {2, 2}, {1, 0}};
Paul's avatar
Paul committed
98
    EXPECT(not s.standard());
Paul's avatar
Paul committed
99
    EXPECT(not s.packed());
Paul's avatar
Paul committed
100
101
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
Paul's avatar
Paul committed
102
103
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
140
TEST_CASE(test_shape_default_copy)
Paul's avatar
Paul committed
141
{
Paul's avatar
Paul committed
142
143
    migraphx::shape s1{};
    migraphx::shape s2{};
Paul's avatar
Paul committed
144
145
146
147
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

Paul's avatar
Paul committed
148
TEST_CASE(test_shape4)
Paul's avatar
Paul committed
149
{
Paul's avatar
Paul committed
150
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}};
Paul's avatar
Paul committed
151
    EXPECT(s.standard());
Paul's avatar
Paul committed
152
    EXPECT(s.packed());
Paul's avatar
Paul committed
153
154
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
155
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
156
157
158
159
160
161
162
163
    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
164
165
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
Paul's avatar
Paul committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    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
181
TEST_CASE(test_shape42)
Paul's avatar
Paul committed
182
{
Paul's avatar
Paul committed
183
    migraphx::shape s{migraphx::shape::float_type, {100, 32, 8, 8}, {2048, 64, 8, 1}};
Paul's avatar
Paul committed
184
185
186
187
    EXPECT(s.standard());
    EXPECT(s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
188
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    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
214
TEST_CASE(test_shape4_transposed)
Paul's avatar
Paul committed
215
{
Paul's avatar
Paul committed
216
    migraphx::shape s{migraphx::shape::float_type, {32, 100, 8, 8}, {64, 2048, 8, 1}};
Paul's avatar
Paul committed
217
218
219
220
    EXPECT(s.transposed());
    EXPECT(s.packed());
    EXPECT(not s.standard());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
221
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    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
247
TEST_CASE(test_shape4_nonpacked)
Paul's avatar
Paul committed
248
249
{
    std::vector<std::size_t> lens       = {100, 32, 8, 8};
Paul's avatar
Paul committed
250
251
    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
252
253
254
255
256
257

    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
258
259
260
261
    std::partial_sum(adj_lens.rbegin(),
                     adj_lens.rend() - 1,
                     strides.rbegin() + 1,
                     std::multiplies<std::size_t>());
Paul's avatar
Paul committed
262

Paul's avatar
Paul committed
263
    migraphx::shape s{migraphx::shape::float_type, lens, strides};
Paul's avatar
Paul committed
264
265
266
267
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
268
    EXPECT(s.type() == migraphx::shape::float_type);
Paul's avatar
Paul committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    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
284
285
286
287
    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
288
289
}

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