-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathdata.cpp
More file actions
360 lines (308 loc) · 10.8 KB
/
data.cpp
File metadata and controls
360 lines (308 loc) · 10.8 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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/data.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
#include <half.hpp>
#pragma GCC diagnostic pop
#include <af/arith.h>
#include <af/array.h>
#include <af/complex.h>
#include <af/defines.h>
#include <af/gfor.h>
#include <af/half.h>
#include <af/traits.hpp>
#include "error.hpp"
#include <type_traits>
using af::array;
using af::dim4;
using af::dtype;
using std::enable_if;
namespace {
// NOTE: we are repeating this here so that we don't need to access the
// is_complex types in backend/common. This is done to isolate the C++ API from
// the internal API
template<typename T>
struct is_complex {
static const bool value = false;
};
template<>
struct is_complex<af::cfloat> {
static const bool value = true;
};
template<>
struct is_complex<af::cdouble> {
static const bool value = true;
};
array constant(af_half val, const dim4 &dims, const dtype type) {
af_array res;
UNUSED(val);
AF_THROW(af_constant(&res, 0, //(double)val,
dims.ndims(), dims.get(), type));
return array(res);
}
template<typename T, typename = typename enable_if<
!static_cast<bool>(is_complex<T>::value), T>::type>
array constant(T val, const dim4 &dims, dtype type) {
af_array res;
if (type != s64 && type != u64) {
AF_THROW(
af_constant(&res, (double)val, dims.ndims(), dims.get(), type));
} else if (type == s64) {
AF_THROW(
af_constant_long(&res, (long long)val, dims.ndims(), dims.get()));
} else {
AF_THROW(af_constant_ulong(&res, (unsigned long long)val, dims.ndims(),
dims.get()));
}
return array(res);
}
template<typename T>
typename enable_if<static_cast<bool>(is_complex<T>::value), array>::type
constant(T val, const dim4 &dims, const dtype type) {
if (type != c32 && type != c64) {
return ::constant(real(val), dims, type);
}
af_array res;
AF_THROW(af_constant_complex(&res, real(val), imag(val), dims.ndims(),
dims.get(), type));
return array(res);
}
} // namespace
namespace af {
template<typename T>
array constant(T val, const dim4 &dims, const af::dtype type) {
return ::constant(val, dims, type);
}
template<typename T>
array constant(T val, const dim_t d0, const af::dtype ty) {
return ::constant(val, dim4(d0), ty);
}
template<typename T>
array constant(T val, const dim_t d0, const dim_t d1, const af::dtype ty) {
return ::constant(val, dim4(d0, d1), ty);
}
template<typename T>
array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2,
const af::dtype ty) {
return ::constant(val, dim4(d0, d1, d2), ty);
}
template<typename T>
array constant(T val, const dim_t d0, const dim_t d1, const dim_t d2,
const dim_t d3, const af::dtype ty) {
return ::constant(val, dim4(d0, d1, d2, d3), ty);
}
#define CONSTANT(TYPE) \
template AFAPI array constant<TYPE>(TYPE val, const dim4 &dims, \
const af::dtype ty); \
template AFAPI array constant<TYPE>(TYPE val, const dim_t d0, \
const af::dtype ty); \
template AFAPI array constant<TYPE>(TYPE val, const dim_t d0, \
const dim_t d1, const af::dtype ty); \
template AFAPI array constant<TYPE>(TYPE val, const dim_t d0, \
const dim_t d1, const dim_t d2, \
const af::dtype ty); \
template AFAPI array constant<TYPE>(TYPE val, const dim_t d0, \
const dim_t d1, const dim_t d2, \
const dim_t d3, const af::dtype ty);
CONSTANT(double);
CONSTANT(float);
CONSTANT(int);
CONSTANT(unsigned);
CONSTANT(char);
CONSTANT(signed char);
CONSTANT(unsigned char);
CONSTANT(cfloat);
CONSTANT(cdouble);
CONSTANT(long);
CONSTANT(unsigned long);
CONSTANT(long long);
CONSTANT(unsigned long long);
CONSTANT(bool);
CONSTANT(short);
CONSTANT(unsigned short);
CONSTANT(half);
CONSTANT(half_float::half);
#undef CONSTANT
array range(const dim4 &dims, const int seq_dim, const af::dtype ty) {
af_array out;
AF_THROW(af_range(&out, dims.ndims(), dims.get(), seq_dim, ty));
return array(out);
}
array range(const dim_t d0, const dim_t d1, const dim_t d2, const dim_t d3,
const int seq_dim, const af::dtype ty) {
return range(dim4(d0, d1, d2, d3), seq_dim, ty);
}
array iota(const dim4 &dims, const dim4 &tile_dims, const af::dtype ty) {
af_array out;
AF_THROW(af_iota(&out, dims.ndims(), dims.get(), tile_dims.ndims(),
tile_dims.get(), ty));
return array(out);
}
array identity(const dim4 &dims, const af::dtype type) {
af_array res;
AF_THROW(af_identity(&res, dims.ndims(), dims.get(), type));
return array(res);
}
array identity(const dim_t d0, const af::dtype ty) {
return identity(dim4(d0), ty);
}
array identity(const dim_t d0, const dim_t d1, const af::dtype ty) {
return identity(dim4(d0, d1), ty);
}
array identity(const dim_t d0, const dim_t d1, const dim_t d2,
const af::dtype ty) {
return identity(dim4(d0, d1, d2), ty);
}
array identity(const dim_t d0, const dim_t d1, const dim_t d2, const dim_t d3,
const af::dtype ty) {
return identity(dim4(d0, d1, d2, d3), ty);
}
array diag(const array &in, const int num, const bool extract) {
af_array res;
if (extract) {
AF_THROW(af_diag_extract(&res, in.get(), num));
} else {
AF_THROW(af_diag_create(&res, in.get(), num));
}
return array(res);
}
array moddims(const array &in, const unsigned ndims, const dim_t *const dims) {
af_array out = 0;
AF_THROW(af_moddims(&out, in.get(), ndims, dims));
return array(out);
}
array moddims(const array &in, const dim4 &dims) {
return af::moddims(in, dims.ndims(), dims.get());
}
array moddims(const array &in, const dim_t d0, const dim_t d1, const dim_t d2,
const dim_t d3) {
dim_t dims[4] = {d0, d1, d2, d3};
return af::moddims(in, 4, dims);
}
array flat(const array &in) {
af_array out = 0;
AF_THROW(af_flat(&out, in.get()));
return array(out);
}
array join(const int dim, const array &first, const array &second) {
af_array out = 0;
AF_THROW(af_join(&out, dim, first.get(), second.get()));
return array(out);
}
array join(const int dim, const array &first, const array &second,
const array &third) {
af_array out = 0;
af_array inputs[3] = {first.get(), second.get(), third.get()};
AF_THROW(af_join_many(&out, dim, 3, inputs));
return array(out);
}
array join(const int dim, const array &first, const array &second,
const array &third, const array &fourth) {
af_array out = 0;
af_array inputs[4] = {first.get(), second.get(), third.get(), fourth.get()};
AF_THROW(af_join_many(&out, dim, 4, inputs));
return array(out);
}
array tile(const array &in, const unsigned x, const unsigned y,
const unsigned z, const unsigned w) {
af_array out = 0;
AF_THROW(af_tile(&out, in.get(), x, y, z, w));
return array(out);
}
array tile(const array &in, const af::dim4 &dims) {
af_array out = 0;
AF_THROW(af_tile(&out, in.get(), dims[0], dims[1], dims[2], dims[3]));
return array(out);
}
array reorder(const array &in, const unsigned x, const unsigned y,
const unsigned z, const unsigned w) {
af_array out = 0;
AF_THROW(af_reorder(&out, in.get(), x, y, z, w));
return array(out);
}
array shift(const array &in, const int x, const int y, const int z,
const int w) {
af_array out = 0;
AF_THROW(af_shift(&out, in.get(), x, y, z, w));
return array(out);
}
array flip(const array &in, const unsigned dim) {
af_array out = 0;
AF_THROW(af_flip(&out, in.get(), dim));
return array(out);
}
array lower(const array &in, bool is_unit_diag) {
af_array res;
AF_THROW(af_lower(&res, in.get(), is_unit_diag));
return array(res);
}
array upper(const array &in, bool is_unit_diag) {
af_array res;
AF_THROW(af_upper(&res, in.get(), is_unit_diag));
return array(res);
}
array select(const array &cond, const array &a, const array &b) {
af_array res;
AF_THROW(af_select(&res, cond.get(), a.get(), b.get()));
return array(res);
}
array select(const array &cond, const array &a, const double &b) {
af_array res;
AF_THROW(af_select_scalar_r(&res, cond.get(), a.get(), b));
return array(res);
}
array select(const array &cond, const double &a, const array &b) {
af_array res;
AF_THROW(af_select_scalar_l(&res, cond.get(), a, b.get()));
return array(res);
}
void replace(array &a, const array &cond, const array &b) {
AF_THROW(af_replace(a.get(), cond.get(), b.get()));
}
void replace(array &a, const array &cond, const double &b) {
AF_THROW(af_replace_scalar(a.get(), cond.get(), b));
}
void replace(array &a, const array &cond, const long long b) {
AF_THROW(af_replace_scalar_long(a.get(), cond.get(), b));
}
void replace(array &a, const array &cond, const unsigned long long b) {
AF_THROW(af_replace_scalar_ulong(a.get(), cond.get(), b));
}
array select(const array &cond, const array &a, const long long b) {
af_array res;
AF_THROW(af_select_scalar_r_long(&res, cond.get(), a.get(), b));
return array(res);
}
array select(const array &cond, const array &a, const unsigned long long b) {
af_array res;
AF_THROW(af_select_scalar_r_ulong(&res, cond.get(), a.get(), b));
return array(res);
}
array select(const array &cond, const long long a, const array &b) {
af_array res;
AF_THROW(af_select_scalar_l_long(&res, cond.get(), a, b.get()));
return array(res);
}
array select(const array &cond, const unsigned long long a, const array &b) {
af_array res;
AF_THROW(af_select_scalar_l_ulong(&res, cond.get(), a, b.get()));
return array(res);
}
array pad(const array &in, const dim4 &beginPadding, const dim4 &endPadding,
const borderType padFillType) {
af_array out = 0;
// FIXME(pradeep) Cannot use dim4::ndims() since that will
// always return 0 if any one of dimensions
// has no padding completely
AF_THROW(af_pad(&out, in.get(), 4, beginPadding.get(), 4, endPadding.get(),
padFillType));
return array(out);
}
} // namespace af