-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlayout.cpp
More file actions
365 lines (317 loc) · 9.7 KB
/
Copy pathlayout.cpp
File metadata and controls
365 lines (317 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
101
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
140
141
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
182
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
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
237
238
239
240
241
242
243
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
282
283
284
285
286
287
288
289
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "core/base/include/layout.h"
#include <cstring>
#include <numeric>
#include <string>
#define rep(i, n) for (auto i = decltype(n){0}; i < (n); ++i)
namespace nncore {
Shape::Shape(const std::vector<nn_size> &init_shape) {
nn_assert(init_shape.size() <= MAX_NDIM,
"The shape you specified has too many dims, which is %llu, the "
"max is %d\n",
init_shape.size(), MAX_NDIM);
ndim = init_shape.size();
memcpy(shape, init_shape.data(), sizeof(nn_size) * ndim);
}
Shape::Shape(nn_size *init_shape, nn_size ndim) {
this->ndim = ndim;
memcpy(shape, init_shape, ndim * sizeof(nn_size));
}
Shape::Shape(const std::initializer_list<nn_size> &init_shape)
: Shape(std::vector<nn_size>{init_shape}) {}
bool Shape::is_scalar() const { return ndim == 1 && shape[0] == 1; }
bool Shape::is_empty() const {
if (ndim == 0) return true;
for (nn_size i = 0; i < ndim; i++) {
if (shape[i] == 0) return true;
}
return false;
}
bool Shape::is_shape(const Shape &rhs) const {
if (ndim != rhs.ndim) return false;
for (nn_size i = 0; i < ndim; i++) {
if (shape[i] != rhs.shape[i]) return false;
}
return true;
}
bool Shape::is_equivalent_shape(const Shape &rhs) const {
nn_size min_ndim = ndim > rhs.ndim ? rhs.ndim : ndim;
for (nn_size i = 0; i < min_ndim; i++) {
if (shape[ndim - i - 1] != rhs.shape[rhs.ndim - i - 1]) return false;
}
for (nn_size i = min_ndim; i < ndim; i++) {
if (shape[i] != 1) return false;
}
for (nn_size i = min_ndim; i < rhs.ndim; i++) {
if (rhs.shape[i] != 1) return false;
}
return true;
}
nn_size Shape::total_elems() const {
nn_size r = 1;
for (nn_size i = 0; i < ndim; i++) r *= shape[i];
return r;
}
std::string Shape::to_string() const {
std::string r = "{";
if (ndim > 0) {
for (nn_size i = 0; i < ndim; i++) {
r += std::to_string(shape[i]);
if (i != ndim - 1) r += ", ";
}
}
r += "}";
return r;
}
Layout::Layout() : dtype() {}
Layout::Layout(const DType &dtype) : dtype(dtype) {}
Layout::Layout(const Shape &shape, const DType &dtype)
: Shape(shape), dtype(dtype) {
init_contiguous_stride();
}
Layout::Layout(const Shape &shape, const std::vector<nn_size> &stride,
const DType &dtype)
: Shape(shape), dtype(dtype) {
nn_assert(shape.ndim == static_cast<nn_size>(stride.size()),
"Size of shape mismatched that of stride.");
for (nn_size i = 0; i < shape.ndim; i++) this->stride[i] = stride[i];
}
nn_size Layout::init_contiguous_stride() {
nn_assert(ndim);
nn_assert(ndim <= Layout::MAX_NDIM);
nn_size s = 1;
for (nn_size i = 0; i < ndim; i++) {
stride[ndim - i - 1] = s;
s *= shape[ndim - i - 1];
}
return s;
}
nn_size Layout::init_contiguous_stride(const Shape &shape) {
this->Shape::operator=(shape);
return init_contiguous_stride();
}
void Layout::broadcast_inplace(const Shape &target) {
nn_assert(ndim && target.ndim, "Empty tensor in broadcast.");
if (is_scalar()) {
ndim = target.ndim;
for (nn_size i = 0; i < target.ndim; i++) {
shape[i] = target.shape[i];
stride[i] = (target.shape[i] == 1);
}
return;
}
nn_assert(target.ndim >= ndim,
"dimension for broadcast less than "
"dst_shape: src_shape=%s dst_shape=%s",
to_string().c_str(), target.to_string().c_str());
for (nn_size i = 0; i < target.ndim; ++i) {
nn_size target_idx = target.ndim - i - 1;
nn_size cur_shape = i < ndim ? shape[ndim - i - 1] : 1,
cur_stride = i < ndim ? stride[ndim - i - 1] : 0;
if (target.shape[target_idx] != cur_shape) {
nn_assert(cur_shape == 1 || cur_stride == 0,
"broadcast on dim with shape not equal to 1: "
"src_shape=%s dst_shape=%s",
to_string().c_str(), target.to_string().c_str());
shape[target_idx] = target.shape[target_idx];
stride[target_idx] = 0;
} else {
shape[target_idx] = cur_shape;
stride[target_idx] = cur_stride;
}
}
ndim = target.ndim;
}
Layout Layout::broadcast(const Shape &target) const {
Layout result(dtype);
result.broadcast_inplace(target);
return result;
}
bool Layout::is_same_layout(const Layout &rhs) const {
if (dtype != rhs.dtype || !is_shape(rhs)) return false;
for (nn_size i = 0; i < ndim; i++) {
if (stride[i] != rhs.stride[i]) return false;
}
return true;
}
bool Layout::is_equivalent_layout(const Layout &rhs) const {
return dtype == rhs.dtype && is_equivalent_shape(rhs);
}
void Layout::offset_to_indices(nn_size offset, nn_size *indices) const {
for (nn_size i = 0; i < ndim; i++) {
int mod = stride[i] != 0 ? stride[i] : shape[i];
indices[i] = offset / mod;
offset %= mod;
}
}
nn_size Layout::indices_to_offset(nn_size *indices) const {
nn_size res = 0;
for (nn_size i = 0; i < ndim; i++) {
res += indices[i] * stride[i];
}
return res;
}
std::string Layout::to_string() const {
std::string r = "(";
if (!ndim) {
r += " Scalar";
} else {
r += "shape = {";
for (nn_size i = 0; i < ndim; i++) {
r += std::to_string(shape[i]);
if (i != ndim - 1) r += ", ";
}
r += "}, stride = {";
for (nn_size i = 0; i < ndim; i++) {
r += std::to_string(stride[i]);
if (i != ndim - 1) r += ", ";
}
r += "}";
}
r += ", dtype = ";
r += dtype.name();
r += ")";
return r;
}
nn_size Layout::content_bytes() const { return total_elems() * dtype.size(); }
Layout Layout::dimshuffle(const std::vector<nn_size> &dims) const {
Layout res{dtype};
res.ndim = this->ndim;
nn_assert(static_cast<nn_size>(dims.size()) == this->ndim);
auto ndim = this->ndim;
rep(i, ndim) {
auto dest = dims[i];
nn_assert(dest < ndim);
res.shape[i] = this->shape[dest];
res.stride[i] = this->stride[dest];
}
return res;
}
Layout Layout::remove_axis(nn_size idx) const {
Layout res{*this};
res.remove_axis_inplace(idx);
return res;
}
void Layout::remove_axis_inplace(nn_size axis) {
nn_assert(ndim >= 2 && axis < ndim);
ndim--;
for (nn_size i = axis; i < ndim; i++) {
shape[i] = shape[i + 1];
stride[i] = stride[i + 1];
}
}
void Layout::add_axis_inplace(nn_size axis, nn_size shape, nn_size stride) {
nn_assert(ndim + 1 <= MAX_NDIM && axis <= ndim && shape,
"can not add axis at %d (current ndim %d, MAX_NDIM %d)", axis, ndim,
MAX_NDIM);
ndim++;
for (int i = ndim - 1; i > axis; i--) {
this->shape[i] = this->shape[i - 1];
this->stride[i] = this->stride[i - 1];
}
this->shape[axis] = shape;
this->stride[axis] = stride;
}
bool Layout::is_contiguous() const {
nn_size expected = 1;
for (nn_size i = 0; i < ndim; ++i) {
if (shape[ndim - i - 1] != 1 && stride[ndim - i - 1] != expected)
return false;
expected *= shape[ndim - i - 1];
}
// empty tensors are not contiguous
return expected != 0;
}
Layout Layout::collapse_contiguous() const {
nn_assert(ndim);
Layout res{*this};
// remove all dims with shape 1
for (nn_size i = 0; i <= res.ndim - 1 && res.ndim >= 2; i++) {
if (!res.shape[i]) {
// empty tensor
res.ndim = 1;
res.shape[0] = 0;
res.stride[0] = 1;
return res;
}
if (res.shape[i] == 1) res.remove_axis_inplace(i);
}
if (res.ndim == 1) {
if (res.shape[0] <= 1) {
res.stride[0] = 1;
}
return res;
}
nn_assert(res.ndim && res.shape[res.ndim - 1]);
for (int i = static_cast<int>(res.ndim) - 2; i >= 0; i--) {
nn_assert(res.shape[i]);
if (res.stride[i] == res.stride[i + 1] * res.shape[i + 1]) {
res.shape[i] *= res.shape[i + 1];
res.stride[i] = res.stride[i + 1];
res.remove_axis_inplace(i + 1);
}
}
return res;
}
bool Layout::try_reshape(Layout &result, const Shape &tshp,
bool is_image) const {
nn_assert(tshp.ndim);
bool is_empty_shape = false;
for (nn_size i = 0; i < tshp.ndim; ++i) {
if (!tshp.shape[i]) {
is_empty_shape = true;
break;
}
}
nn_assert(tshp.ndim && total_elems() == tshp.total_elems(),
"number of elements do not match "
"in reshape: src=%s dest=%s",
static_cast<const Shape &>(*this).to_string().c_str(),
tshp.to_string().c_str());
// So far only the swap of width and height is supported.
if (is_image) {
nn_assert(this->ndim >= 2 && tshp.ndim == this->ndim &&
this->shape[ndim - 1] == tshp.shape[tshp.ndim - 2] &&
this->shape[ndim - 2] == tshp.shape[tshp.ndim - 1]);
for (nn_size i = 0; i < this->ndim - 2; i++) {
if (tshp.shape[i] != this->shape[i]) return false;
}
result.dtype = this->dtype;
result.Shape::operator=(tshp);
std::swap(result.stride[this->ndim - 1], result.stride[this->ndim - 2]);
return true;
}
auto cont = collapse_contiguous();
result.dtype = this->dtype;
result.Shape::operator=(tshp);
if (is_empty_shape) {
result.init_contiguous_stride();
return true;
}
nn_size sdim = 0, prod = 1, cont_sdim = 0;
for (nn_size i = 0; i < tshp.ndim; i++) {
nn_assert(cont_sdim < cont.ndim);
prod *= result.shape[i];
if (prod > cont.shape[cont_sdim]) return false;
if (prod == cont.shape[cont_sdim] &&
(i + 1 >= tshp.ndim || tshp.shape[i + 1] != 1)) {
auto s = cont.stride[cont_sdim];
for (int j = i; j >= sdim; j--) {
result.stride[j] = s;
s *= result.shape[j];
}
cont_sdim++;
sdim = i + 1;
prod = 1;
}
}
nn_assert(cont_sdim == cont.ndim);
return true;
}
Layout Layout::reshape(const Shape &shape, bool is_image) const {
Layout ret;
auto succ = try_reshape(ret, shape, is_image);
nn_assert(succ, "can not reshape from %s to %s", to_string().c_str(),
shape.to_string().c_str());
return ret;
}
} // namespace nncore