-
Notifications
You must be signed in to change notification settings - Fork 7
/
minitensor.h
283 lines (269 loc) · 7.2 KB
/
minitensor.h
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
// A small wrapper class for Torch tensors.
#ifndef minitensor__
#define minitensor__
extern "C" {
#include <assert.h>
}
#include <TH/TH.h>
#include <TH/THTensorMacros.h>
#include <string>
#include <sstream>
#include <fstream>
#ifdef SAFE
#define CHECK(expr) \
do{if(!(expr)) abort();}while(0)
#define RCHECK(i, n) \
do{if(unsigned(i) >= unsigned(n)) abort();}while(0)
#else
#define CHECK(expr) \
do{}while(0)
#define RCHECK(i, n) \
do{}while(0)
#endif
namespace {
#if 0
std::ofstream memlog("memlog");
void mlog(THFloatTensor *p, const char *msg, int line, const char *note) {
std::stringstream entry;
entry
<< std::dec << msg << " " << line << " " << note << " "
<< std::hex << (void*)p << " "
<< std::dec << p->refcount << std::endl;
memlog << entry.str();
}
#define M mlog(p, __FUNCTION__, __LINE__, note)
#else
#define M
#endif
}
class TFloat {
public:
THFloatTensor *p = 0;
const char *note = "_";
TFloat() {
p = THFloatTensor_new(); M;
}
TFloat(TFloat &other) {
p = THFloatTensor_newWithTensor(other.p); M;
}
TFloat(const TFloat &other) {
THFloatTensor_newWithTensor(other.p); M;
}
TFloat(THFloatTensor *p, const char *note) : p(p), note(note) {
THFloatTensor_retain(p); M;
}
TFloat(THFloatTensor *p, bool retain, const char *note) : p(p), note(note) {
if(retain) THFloatTensor_retain(p); M;
}
TFloat(int x0) {
p = THFloatTensor_newWithSize1d(x0); M;
}
TFloat(int x0, int x1) {
p = THFloatTensor_newWithSize2d(x0, x1); M;
}
TFloat(int x0, int x1, int x2) {
p = THFloatTensor_newWithSize3d(x0, x1, x2); M;
}
TFloat(int x0, int x1, int x2, int x3) {
p = THFloatTensor_newWithSize4d(x0, x1, x2, x3); M;
}
~TFloat() {
assert(p); M;
THFloatTensor_free(p);
p = 0;
}
std::string info() {
std::stringstream result;
for (int i=0; i<dim(); i++)
result << " " << size(i);
result << " " << min();
result << " " << max();
return result.str();
}
operator THFloatTensor *() {
return p;
}
bool isSameSizeAs(TFloat &other) {
return THFloatTensor_isSameSizeAs(p, other.p);
}
TFloat &operator=(const TFloat &other) {
THFloatTensor_retain(other.p);
THFloatTensor_free(p);
p = other.p;
return *this;
}
TFloat &operator=(THFloatTensor *other_p) {
THFloatTensor_retain(other_p);
THFloatTensor_free(p);
p = other_p;
return *this;
}
TFloat &operator=(float value) {
THFloatTensor_fill(p, value);
return *this;
}
TFloat &assign(TFloat &other) {
THFloatTensor_resizeAs(p, other.p);
THFloatTensor_copy(p, other.p);
return *this;
}
TFloat ©(TFloat &other) {
THFloatTensor_copy(p, other.p);
return *this;
}
TFloat &fill(float value) {
THFloatTensor_fill(p, value);
return *this;
}
TFloat &zero() {
THFloatTensor_zero(p);
return *this;
}
TFloat narrow(int dimension, int start, int size) {
TFloat result(THFloatTensor_newNarrow(p, dimension, start, size), false, "narrow");
return result;
}
TFloat select(int dimension, int index) {
TFloat result(THFloatTensor_newSelect(p, dimension, index), false, "select");
return result;
}
TFloat squeeze() {
TFloat result;
THFloatTensor_squeeze(result.p, p);
return result;
}
TFloat squeeze(int dimension) {
TFloat result;
THFloatTensor_squeeze1d(result.p, p, dimension);
return result;
}
#if 0
TFloat unsqueeze(int dimension) {
TFloat result;
THFloatTensor_unsqueeze1d(result.p, p, dimension);
return result;
}
#endif
TFloat transpose(int dim1, int dim2) {
TFloat result(THFloatTensor_newTranspose(p, dim1, dim2), false, "transpose");
return result;
}
TFloat &resize(int x0) {
THFloatTensor_resize1d(p, x0);
return *this;
}
TFloat &resize(int x0, int x1) {
THFloatTensor_resize2d(p, x0, x1);
return *this;
}
TFloat &resize(int x0, int x1, int x2) {
THFloatTensor_resize3d(p, x0, x1, x2);
return *this;
}
TFloat &resize(int x0, int x1, int x2, int x3) {
THFloatTensor_resize4d(p, x0, x1, x2, x3);
return *this;
}
int dim() {
return THFloatTensor_nDimension(p);
}
int size(int i) {
return THFloatTensor_size(p, i);
}
float &operator()(int x0) {
CHECK(dim()==1);
RCHECK(x0, size(0));
return THTensor_fastGet1d(p, x0);
}
float &operator()(int x0, int x1) {
CHECK(dim()==2);
RCHECK(x0, size(0));
RCHECK(x1, size(1));
return THTensor_fastGet2d(p, x0, x1);
}
float &operator()(int x0, int x1, int x2) {
CHECK(dim()==3);
RCHECK(x0, size(0));
RCHECK(x1, size(1));
RCHECK(x2, size(2));
return THTensor_fastGet3d(p, x0, x1, x2);
}
float &operator()(int x0, int x1, int x2, int x3) {
CHECK(dim()==4);
RCHECK(x0, size(0));
RCHECK(x1, size(1));
RCHECK(x2, size(2));
RCHECK(x3, size(3));
return THTensor_fastGet4d(p, x0, x1, x2, x3);
}
TFloat &resizeAs(TFloat &other) {
THFloatTensor_resizeAs(p, other.p);
return *this;
}
TFloat &operator+=(TFloat &other) {
THFloatTensor_add(p, other.p, 1.0);
return *this;
}
TFloat &cadd(TFloat &input1, float c, TFloat &input2) {
THFloatTensor_cadd(p, input1.p, c, input2.p);
return *this;
}
float min() {
return THFloatTensor_minall(p);
}
float max() {
return THFloatTensor_maxall(p);
}
float sum() {
return THFloatTensor_sumall(p);
}
float prod() {
return THFloatTensor_prodall(p);
}
TFloat operator+(float x) {
TFloat result;
THFloatTensor_add(result.p, p, x);
return result;
}
TFloat operator-(float x) {
TFloat result;
THFloatTensor_add(result.p, p, -x);
return result;
}
TFloat operator*(float x) {
TFloat result;
THFloatTensor_mul(result.p, p, x);
return result;
}
TFloat operator/(float x) {
TFloat result;
THFloatTensor_div(result.p, p, x);
return result;
}
TFloat clamp(float x, float y) {
TFloat result;
THFloatTensor_clamp(result.p, p, x, y);
return result;
}
TFloat operator+(TFloat &other) {
TFloat result;
THFloatTensor_cadd(result.p, p, 1.0, other.p);
return result;
}
TFloat operator-(TFloat &other) {
TFloat result;
THFloatTensor_cadd(result.p, p, -1.0, other.p);
return result;
}
TFloat operator*(TFloat &other) {
TFloat result;
THFloatTensor_cmul(result.p, p, other.p);
return result;
}
TFloat operator/(TFloat &other) {
TFloat result;
THFloatTensor_cdiv(result.p, p, other.p);
return result;
}
};
#endif