-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
core_tensor.i
executable file
·374 lines (297 loc) · 12.7 KB
/
core_tensor.i
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
366
367
368
369
370
371
372
373
374
/************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
/*interface file for swig */
%module core_tensor
%include "config.i"
%include "std_vector.i"
%include "std_string.i"
%include "std_shared_ptr.i"
%{
#define SWIG_FILE_WITH_INIT
#include "singa/core/tensor.h"
#include "singa/core/device.h"
#include "singa/proto/core.pb.h"
// #include "singa/proto/model.pb.h"
using singa::DataType;
%}
%shared_ptr(singa::Device)
#if USE_PYTHON
%include "numpy.i"
%init %{
import_array();
%}
// better use (int DIM1, float* IN_ARRAY1)
// otherwise, the generated py method will have the arg name src,
// which in fact accepts num as the input
%apply (float *IN_ARRAY1, int DIM1) {
(const float *src, const size_t num)
}
%apply (int *IN_ARRAY1, int DIM1) {
(const int *src, const size_t num)
}
%apply (float *ARGOUT_ARRAY1, int DIM1) {
(float *value, const size_t num)
}
%apply (int *ARGOUT_ARRAY1, int DIM1) {
(int *value, const size_t num)
}
#endif // USE_PYTHON
#if USE_JAVA
%include "arrays_java.i"
%apply int[] {int *};
%apply float[] {float *};
#endif // USE_JAVA
namespace std {
%template(VecTensor) vector<singa::Tensor>;
%template(VecVecSize) vector<vector<size_t>>;
}
%template(Shape) std::vector<size_t>;
namespace singa{
enum DataType {
kFloat32, kFloat16, kInt, kChar, kDouble
};
inline size_t Product(const std::vector<size_t> &shape,
int start = 0, size_t len = 0);
inline size_t SizeOf(DataType t);
class Tensor {
public:
Tensor();
explicit Tensor(const std::vector<size_t> &shape,
DataType dtype = kFloat32);
Tensor(const std::vector<size_t> &shape,
std::shared_ptr<singa::Device> dev,
DataType dtype = kFloat32);
Tensor(const Tensor &from);
std::shared_ptr<singa::Device> device() const;
template <typename SType> void GetValue(SType* value, const size_t num) const;
%template(GetFloatValue) GetValue<float>;
%template(GetIntValue) GetValue<int>;
template <typename SType> void SetValue(const SType x);
%template(SetFloatValue) SetValue<float>;
const DataType data_type() const;
const std::vector<size_t> &shape() const;
const size_t shape(size_t idx) const;
bool transpose() const;
size_t nDim() const;
size_t Size() const;
size_t MemSize() const;
void ResetLike(const Tensor &t);
Tensor AsType(DataType type);
void ToDevice(std::shared_ptr<singa::Device> dev);
void ToHost();
float L2() const;
float L1() const;
template <typename DType> void CopyDataFromHostPtr(const DType *src,
const size_t num,
const size_t offset = 0) const;
%template(CopyFloatDataFromHostPtr) CopyDataFromHostPtr<float>;
%template(CopyIntDataFromHostPtr) CopyDataFromHostPtr<int>;
void CopyData(const Tensor &other);
void RepeatData(std::vector<size_t> repeats, int axis, int total_repeats, const Tensor &src);
Tensor Clone() const;
Tensor Repeat(std::vector<size_t> repeats, int axis);
#if USE_JAVA
%rename(iAdd) operator+=(const Tensor &t);
%rename(iSub) operator-=(const Tensor &t);
%rename(iMul) operator*=(const Tensor &t);
%rename(iDiv) operator/=(const Tensor &t);
#endif // USE_JAVA
Tensor &operator+=(const Tensor &t);
Tensor &operator-=(const Tensor &t);
Tensor &operator*=(const Tensor &t);
Tensor &operator/=(const Tensor &t);
template <typename DType> Tensor &operator+=(const DType x);
%template(iAddFloat) operator+=<float>;
template <typename DType> Tensor &operator-=(DType x);
%template(iSubFloat) operator-=<float>;
template <typename DType> Tensor &operator*=(DType x);
%template(iMulFloat) operator*=<float>;
template <typename DType> Tensor &operator/=(DType x);
%template(iDivFloat) operator/=<float>;
/*TODO(chonho-04)
amax
amin
asum
*/
};
void CopyDataToFrom(Tensor *dst, const Tensor &src, size_t num,
size_t src_offset = 0, size_t dst_offset = 0);
void RepeatDataToFrom(bool broadcast_flag, std::vector<size_t> repeats, int axis,
Tensor *dst, const Tensor &src, const size_t num);
Tensor Reshape(const Tensor &in, const std::vector<size_t> &s);
Tensor Contiguous(const Tensor &in);
Tensor Transpose(const Tensor &in, const std::vector<size_t> &axes);
%rename(DefaultTranspose) Transpose(const Tensor &in);
Tensor Transpose(const Tensor &in);
Tensor Abs(const Tensor &t);
Tensor Ceil(const Tensor &t);
Tensor Floor(const Tensor &t);
Tensor Round(const Tensor &t);
Tensor RoundE(const Tensor &t);
Tensor Exp(const Tensor &t);
Tensor Erf(const Tensor &t);
Tensor Log(const Tensor &t);
Tensor ReLU(const Tensor &t);
Tensor Sigmoid(const Tensor &t);
Tensor Sign(const Tensor &t);
Tensor Sqrt(const Tensor &t);
Tensor Square(const Tensor &t);
Tensor Cos(const Tensor &t);
Tensor Cosh(const Tensor &t);
Tensor Acos(const Tensor &t);
Tensor Acosh(const Tensor &t);
Tensor Sin(const Tensor &t);
Tensor Sinh(const Tensor &t);
Tensor Asin(const Tensor &t);
Tensor Asinh(const Tensor &t);
Tensor Tan(const Tensor &t);
Tensor Tanh(const Tensor &t);
Tensor Atan(const Tensor &t);
Tensor Atanh(const Tensor &t);
Tensor ReLUBackward(const Tensor &in1, const Tensor& in2);
Tensor Sum(const Tensor &t, int axis);
template <typename SType> SType Sum(const Tensor &t);
%template(SumAsFloat) Sum<float>;
Tensor SumAll(const Tensor &t);
Tensor Average(const Tensor &t, int axis);
Tensor SoftMax(const Tensor &t);
Tensor SoftMax(const Tensor &t, int axis);
Tensor SoftMaxBackward(const Tensor &t, int axis, const Tensor &fdout);
Tensor Pow(const Tensor &base, const Tensor &exp);
%rename(PowWithRet) Pow(const Tensor &base, const Tensor &exp, Tensor *out);
void Pow(const Tensor &base, const Tensor &exp, Tensor *out);
template <typename SType> Tensor Pow(const Tensor &in, const SType x);
%template(PowFloat) Pow<float>;
template <typename SType>
void Pow(const Tensor &in, const SType x, Tensor *out);
%template(PowFloatWithRet) Pow<float>;
%rename(__lt__) operator<(const Tensor &lhs, const Tensor &rhs);
%rename(__le__) operator<=(const Tensor &lhs, const Tensor &rhs);
%rename(__gt__) operator>(const Tensor &lhs, const Tensor &rhs);
%rename(__ge__) operator>=(const Tensor &lhs, const Tensor &rhs);
%rename(__eq__) operator==(const Tensor &lhs, const Tensor &rhs);
Tensor operator<(const Tensor &lhs, const Tensor &rhs);
Tensor operator<=(const Tensor &lhs, const Tensor &rhs);
Tensor operator>(const Tensor &lhs, const Tensor &rhs);
Tensor operator>=(const Tensor &lhs, const Tensor &rhs);
Tensor operator==(const Tensor &lhs, const Tensor &rhs);
%rename(LTFloat) operator<(const Tensor &t, const float x);
template <typename DType>
Tensor operator<(const Tensor &t, const DType x);
%template(oplt) operator< <float>;
%rename(LEFloat) operator<=(const Tensor &t, const float x);
template <typename DType> Tensor operator<=(const Tensor &t, const DType x);
%template(ople) operator<= <float>;
%rename(GTFloat) operator>(const Tensor &t, const float x);
template <typename DType> Tensor operator>(const Tensor &t, const DType x);
%template(opgt) operator> <float>;
%rename(GEFloat) operator>=(const Tensor &t, const float x);
template <typename DType> Tensor operator>=(const Tensor &t, const DType x);
%template(opge) operator>= <float>;
%rename(EQFloat) operator==(const Tensor &t, const float x);
template <typename DType> Tensor operator==(const Tensor &t, const DType x);
%template(opeq) operator== <float>;
Tensor ConcatOn(const std::vector<Tensor> &in, int axis);
Tensor SliceOn(const Tensor&in, const size_t start, const size_t end, int axis);
/* ========== Arithmetic operations ========== */
%rename(__add__) operator+(const Tensor &lhs, const Tensor &rhs);
%rename(__sub__) operator-(const Tensor &lhs, const Tensor &rhs);
%rename(__mul__) operator*(const Tensor &lhs, const Tensor &rhs);
%rename(__div__) operator/(const Tensor &lhs, const Tensor &rhs);
Tensor operator+(const Tensor &lhs, const Tensor &rhs);
Tensor operator-(const Tensor &lhs, const Tensor &rhs);
Tensor operator*(const Tensor &lhs, const Tensor &rhs);
Tensor operator/(const Tensor &lhs, const Tensor &rhs);
void Add(const Tensor &lhs, const Tensor &rhs, Tensor *ret);
void Sub(const Tensor &lhs, const Tensor &rhs, Tensor *ret);
void EltwiseMult(const Tensor &lhs, const Tensor &rhs, Tensor *ret);
void Div(const Tensor &lhs, const Tensor &rhs, Tensor *ret);
%rename(AddFloat) operator+(const Tensor &t, float x);
template <typename DType> Tensor operator+(const Tensor &t, DType x);
%template(opadd) operator+ <float>;
%rename(SubFloat) operator-(const Tensor &t, float x);
template <typename DType> Tensor operator-(const Tensor &t, DType x);
%template(opsub) operator- <float>;
%rename(MultFloat) operator*(const Tensor &t, float x);
template <typename DType> Tensor operator*(const Tensor &t, DType x);
%template(opmul) operator* <float>;
%rename(DivFloat) operator/(const Tensor &t, float x);
template <typename DType> Tensor operator/(const Tensor &t, DType x);
%template(opdiv) operator/ <float>;
template <typename DType> void Add(const Tensor &t, DType x, Tensor *ret);
%template(AddFloatWithRet) Add<float>;
template <typename DType>
void Sub(const Tensor &t, DType x, Tensor *ret);
%template(SubFloatWithRet) Sub<float>;
template <typename DType>
void EltwiseMult(const Tensor &t, DType x, Tensor *ret);
%template(EltwiseMultFloatWithRet) EltwiseMult<float>;
template <typename DType>
void Div(const Tensor &t, DType x, Tensor *ret);
%template(DivFloatWithRet) Div<float>;
/* ========== Random operations ========== */
template <typename SType>
void Bernoulli(const SType p, Tensor *out);
%template(Bernoulli) Bernoulli<float>;
template <typename SType>
void Gaussian(const SType mean, const SType std, Tensor *out);
%template(Gaussian) Gaussian<float>;
template <typename SType>
void Uniform(const SType low, const SType high, Tensor *out);
%template(Uniform) Uniform<float>;
/* ========== Blas operations ========== */
template <typename SType>
void Axpy(SType alpha, const Tensor &in, Tensor *out);
%template(Axpy) Axpy<float>;
void Axpy(const Tensor &alpha, const Tensor &in, Tensor *out);
Tensor Mult(const Tensor &A, const Tensor &B);
%rename(MultWithRet) Mult(const Tensor &A, const Tensor &B, Tensor *C);
void Mult(const Tensor &A, const Tensor &B, Tensor *C);
template <typename SType>
void Mult(const SType alpha, const Tensor &A, const Tensor &B,
const SType beta, Tensor *C);
%template(MultWithScale) Mult<float>;
/* =========== Matrix operations ==========*/
void AddColumn(const Tensor &v, Tensor *M);
template <typename SType>
void AddColumn(const SType alpha, const SType beta, const Tensor &v,
Tensor *M);
%template(AddColumnWithScale) AddColumn<float>;
void AddRow(const Tensor &v, Tensor *M);
template <typename SType>
void AddRow(const SType alpha, const SType beta, const Tensor &v,
Tensor *M);
%template(AddRowWithScale) AddRow<float>;
void DivColumn(const Tensor &v, Tensor *M);
void DivRow(const Tensor &v, Tensor *M);
void MultColumn(const Tensor &v, Tensor *M);
void MultRow(const Tensor &v, Tensor *M);
void SubColumn(const Tensor &v, Tensor *M);
void SubRow(const Tensor &v, Tensor *M);
void SumColumns(const Tensor &M, Tensor *v);
void SumRows(const Tensor &M, Tensor *v);
Tensor SoftMax(const Tensor &in);
void SoftMax(const Tensor &in, Tensor *out);
Tensor SoftMax(const Tensor &in, int axis);
void SoftMax(const Tensor &in, Tensor *out, int axis);
Tensor CrossEntropyFwd(const Tensor& p, const Tensor& t);
Tensor SoftmaxCrossEntropyBwd(const Tensor& p, const Tensor& t);
}