-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathtypes.cpp
More file actions
338 lines (326 loc) · 9.99 KB
/
types.cpp
File metadata and controls
338 lines (326 loc) · 9.99 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
#include "uTensor/core/types.hpp"
#include <cstring>
uint8_t type_size(ttype t) {
switch (t) {
case i8:
return sizeof(int8_t);
case u8:
return sizeof(uint8_t);
case i16:
return sizeof(int16_t);
case u16:
return sizeof(uint16_t);
case i32:
return sizeof(int32_t);
case u32:
return sizeof(uint32_t);
case flt:
return sizeof(float);
default:
// TODO print error
return 0;
}
}
TensorShape::TensorShape(uint16_t shape) : _num_dims(1) {
_shape[0] = shape;
_shape[1] = 0;
_shape[2] = 0;
_shape[3] = 0;
}
TensorShape::TensorShape(array<uint16_t, 1> shape) : _num_dims(1) {
_shape[0] = shape[0];
_shape[1] = 0;
_shape[2] = 0;
_shape[3] = 0;
}
TensorShape::TensorShape(array<uint16_t, 2> shape) : _num_dims(2) {
_shape[0] = shape[0];
_shape[1] = shape[1];
_shape[2] = 0;
_shape[3] = 0;
}
TensorShape::TensorShape(array<uint16_t, 3> shape) : _num_dims(3) {
_shape[0] = shape[0];
_shape[1] = shape[1];
_shape[2] = shape[2];
_shape[3] = 0;
}
TensorShape::TensorShape(array<uint16_t, 4> shape) : _num_dims(4) {
_shape[0] = shape[0];
_shape[1] = shape[1];
_shape[2] = shape[2];
_shape[3] = shape[3];
}
TensorShape::TensorShape(uint16_t shape0, uint16_t shape1) : _num_dims(2) {
_shape[0] = shape0;
_shape[1] = shape1;
_shape[2] = 0;
_shape[3] = 0;
}
TensorShape::TensorShape(uint16_t shape0, uint16_t shape1, uint16_t shape2)
: _num_dims(3) {
_shape[0] = shape0;
_shape[1] = shape1;
_shape[2] = shape2;
_shape[3] = 0;
}
TensorShape::TensorShape(uint16_t shape0, uint16_t shape1, uint16_t shape2,
uint16_t shape3)
: _num_dims(4) {
_shape[0] = shape0;
_shape[1] = shape1;
_shape[2] = shape2;
_shape[3] = shape3;
}
uint16_t TensorShape::operator[](int i) const {
return _shape[i]; /* Do additional checks*/
}
uint16_t& TensorShape::operator[](int i) {
return _shape[i];
} // Maybe handle update case
void TensorShape::update_dims() {
// implicit assuming the last positive dim is the num_dims
for (int i = 0; i < 4; i++) {
if (_shape[i] > 0) _num_dims = i + 1;
}
}
bool TensorShape::operator==(const TensorShape& other) {
if (_num_dims != other.num_dims()) {
return false;
}
bool all_eq = true;
for (int i = 0; i < _num_dims; ++i) {
all_eq = all_eq && (_shape[i] == other[i]);
}
return all_eq;
}
bool TensorShape::operator!=(const TensorShape& other) {
return !(*this == other);
}
uint32_t TensorShape::get_linear_size() const {
uint32_t sum = 1;
for (int i = 0; i < _num_dims; i++) {
if (_shape[i] == 0) break;
const uint32_t s = _shape[i];
sum *= s;
}
return sum;
}
uint8_t TensorShape::num_dims() const { return _num_dims; }
// TODO FIX FOR HIGHER DIMENSIONS
// https://www.tensorflow.org/xla/shapes
uint32_t TensorShape::linear_index(uint16_t i, uint16_t j, uint16_t k,
uint16_t l) const {
/*
// TODO
uint32_t d1 = _shape[1] > 0 ? 1 : 0;
d1 *= _shape[0];
uint32_t d2 = _shape[2] > 0 ? 1 : 0;
d2 *= d1 * _shape[1];
uint32_t d3 = _shape[3] > 0 ? 1 : 0;
d3 *= d2 * _shape[2];
// Image order
// return i + j * d1 + k * d2 + l * d3;
// Matrix order
return j + i * d1 + k * d2 + l * d3;
*/
uint32_t num_channels = _shape[3] > 0 ? _shape[3] : 1;
uint32_t num_cols = _shape[2] > 0 ? _shape[2] : 1;
uint32_t num_rows = _shape[1] > 0 ? _shape[1] : 1;
// Simple factorization can reduce the number of mults here, but for clarity
return i * num_rows * num_cols * num_channels + j * num_cols * num_channels +
k * num_channels + l;
}
uint32_t TensorShape::num_elems() const {
uint32_t num = 1;
for (size_t dim_idx = 0; dim_idx < _num_dims; ++dim_idx) {
num *= _shape[dim_idx];
}
return num;
}
TensorStrides::TensorStrides(TensorShape& shape) {
_num_dims = shape.num_dims();
size_t last_idx = _num_dims - 1;
for (size_t i = last_idx + 1; i < 3; ++i) {
_strides[i] = 0;
}
_strides[last_idx] = 1;
uint32_t s = 1;
for (int32_t i = last_idx - 1; i >= 0; --i) {
s *= shape[i + 1];
_strides[i] = s;
}
}
uint8_t TensorStrides::num_dims() { return _num_dims; }
uint32_t TensorStrides::operator[](size_t i) const { return _strides[i]; }
uint32_t& TensorStrides::operator[](size_t i) { return _strides[i]; }
IntegralValue::IntegralValue(void* p) : p(p), num_bytes(0) {}
/*
IntegralValue::IntegralValue(const uint8_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<uint8_t*>(p) = u;
}
IntegralValue::IntegralValue(const int8_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<int8_t*>(p) = u;
}
IntegralValue::IntegralValue(const uint16_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<uint16_t*>(p) = u;
}
IntegralValue::IntegralValue(const int16_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<int16_t*>(p) = u;
}
IntegralValue::IntegralValue(const uint32_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<uint32_t*>(p) = u;
}
IntegralValue::IntegralValue(const int32_t& u): num_bytes(sizeof(u)) {
*reinterpret_cast<int32_t*>(p) = u;
}
IntegralValue::IntegralValue(const float& u): num_bytes(sizeof(u)) {
*reinterpret_cast<float*>(p) = u;
}
*/
IntegralValue::IntegralValue(const uint8_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const int8_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const uint16_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const int16_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const uint32_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const int32_t& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const float& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(float));
p = reinterpret_cast<void*>(tmp);
}
/*
IntegralValue::IntegralValue( uint8_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( int8_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( uint16_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( int16_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( uint32_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( int32_t u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue( float u): num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(float));
p = reinterpret_cast<void*>(tmp);
}
*/
IntegralValue::IntegralValue(uint8_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(int8_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int8_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(uint16_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(int16_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int16_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(uint32_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(uint32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(int32_t&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(int32_t));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(float&& u) : num_bytes(sizeof(u)) {
memcpy(tmp, &u, sizeof(float));
p = reinterpret_cast<void*>(tmp);
}
IntegralValue::IntegralValue(const IntegralValue& that) {
p = that.p;
memcpy(tmp, that.tmp, sizeof(tmp));
num_bytes = that.num_bytes;
if (that.p == &that.tmp[0]) p = &tmp[0];
}
IntegralValue& IntegralValue::operator=(const IntegralValue& that) {
p = that.p;
memcpy(tmp, that.tmp, sizeof(tmp));
num_bytes = that.num_bytes;
if (that.p == &that.tmp[0]) p = &tmp[0];
return *this;
}
IntegralValue& IntegralValue::operator=(IntegralValue&& that) {
memmove(p, that.p, that.num_bytes);
num_bytes = that.num_bytes;
return *this;
}
IntegralValue::operator uint8_t() const {
return static_cast<uint8_t>(*reinterpret_cast<uint8_t*>(p));
}
IntegralValue::operator uint8_t&() {
return static_cast<uint8_t&>(*reinterpret_cast<uint8_t*>(p));
}
IntegralValue::operator int8_t() const {
return static_cast<int8_t>(*reinterpret_cast<int8_t*>(p));
}
IntegralValue::operator int8_t&() {
return static_cast<int8_t&>(*reinterpret_cast<int8_t*>(p));
}
IntegralValue::IntegralValue::operator uint16_t() const {
return static_cast<uint16_t>(*reinterpret_cast<uint16_t*>(p));
}
IntegralValue::operator uint16_t&() {
return static_cast<uint16_t&>(*reinterpret_cast<uint16_t*>(p));
}
IntegralValue::operator int16_t() const {
return static_cast<int16_t>(*reinterpret_cast<int16_t*>(p));
}
IntegralValue::operator int16_t&() {
return static_cast<int16_t&>(*reinterpret_cast<int16_t*>(p));
}
IntegralValue::IntegralValue::operator uint32_t() const {
return static_cast<uint32_t>(*reinterpret_cast<uint32_t*>(p));
}
IntegralValue::operator uint32_t&() {
return static_cast<uint32_t&>(*reinterpret_cast<uint32_t*>(p));
}
IntegralValue::operator int32_t() const {
return static_cast<int32_t>(*reinterpret_cast<int32_t*>(p));
}
IntegralValue::operator int32_t&() {
return static_cast<int32_t&>(*reinterpret_cast<int32_t*>(p));
}
IntegralValue::IntegralValue::operator float() const {
return static_cast<float>(*reinterpret_cast<float*>(p));
}
IntegralValue::operator float&() {
return static_cast<float&>(*reinterpret_cast<float*>(p));
}