forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THTensorDimApply.h
329 lines (324 loc) · 14.5 KB
/
THTensorDimApply.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
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
#ifndef TH_TENSOR_DIM_APPLY_INC
#define TH_TENSOR_DIM_APPLY_INC
// This is an example of SIZE_CHECK argument passable to TH_TENSOR_DIM_APPLY3.
// The TENSOR1, TENSOR2, TENSOR3, DIMENSION will be expanded the same way as
// TH_TENSOR_DIM_APPLY3.
// Specifically, this check ensures that TENSOR1, TENSOR2, TENSOR3 have same
// size except for DIMENSION.
#define TH_TENSOR_DIM_APPLY3_SIZE_EQ_EXCEPT_DIM(TENSOR1, TENSOR2, TENSOR3, DIMENSION) \
{ \
int shape_check_flag = 0; \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
{ \
if (TH_TENSOR_DIM_APPLY_i == DIMENSION) \
continue; \
if (TENSOR1->size(TH_TENSOR_DIM_APPLY_i) != TENSOR2->size(TH_TENSOR_DIM_APPLY_i)) { \
shape_check_flag = 1; \
break; \
} \
if(TENSOR1->size(TH_TENSOR_DIM_APPLY_i) != TENSOR3->size(TH_TENSOR_DIM_APPLY_i)) { \
shape_check_flag = 1; \
break; \
} \
} \
if (shape_check_flag == 1) { \
AT_ERROR("Expected ", #TENSOR1, " ", TENSOR1->sizes(), ", ", #TENSOR2, " ", TENSOR2->sizes(), " and ", #TENSOR3, " ", TENSOR3->sizes(), " to have the same size apart from dimension ", DIMENSION); \
} \
}
#define TH_TENSOR_DIM_APPLY3(TYPE1, TENSOR1, TYPE2, TENSOR2, TYPE3, TENSOR3, DIMENSION, SIZE_CHECK, CODE) \
{ \
TYPE1 *TENSOR1##_data = NULL; \
TH_UNUSED int64_t TENSOR1##_stride = 0, TENSOR1##_size = 0; \
TYPE2 *TENSOR2##_data = NULL; \
TH_UNUSED int64_t TENSOR2##_stride = 0, TENSOR2##_size = 0; \
TYPE3 *TENSOR3##_data = NULL; \
TH_UNUSED int64_t TENSOR3##_stride = 0, TENSOR3##_size = 0; \
int64_t *TH_TENSOR_DIM_APPLY_counter = NULL; \
int TH_TENSOR_DIM_APPLY_hasFinished = THTensor_(numel)(TENSOR1) == 0; \
int TH_TENSOR_DIM_APPLY_i; \
\
if( (DIMENSION < 0) || (DIMENSION >= THTensor_nDimensionLegacyNoScalars(TENSOR1)) ) \
THError("invalid dimension %d (expected to be 0 <= dim < %d)", DIMENSION, THTensor_nDimensionLegacyNoScalars(TENSOR1)); \
int same_dims = 1; \
if( THTensor_nDimensionLegacyNoScalars(TENSOR1) != THTensor_nDimensionLegacyNoScalars(TENSOR2) ) { \
same_dims = 0; \
} \
if( THTensor_nDimensionLegacyNoScalars(TENSOR1) != THTensor_nDimensionLegacyNoScalars(TENSOR3) ) { \
same_dims = 0; \
} \
if (same_dims == 0) { \
AT_ERROR("inconsistent tensor size, expected ", #TENSOR1, " ", TENSOR1->sizes(), ", ", #TENSOR2, " ", TENSOR2->sizes(), " and ", #TENSOR3, " ",TENSOR3->sizes() , " to have the same number of dimensions"); \
} \
SIZE_CHECK(TENSOR1, TENSOR2, TENSOR3, DIMENSION) \
\
if (TH_TENSOR_DIM_APPLY_hasFinished) { \
return; \
} \
TH_TENSOR_DIM_APPLY_counter = (int64_t*)THAlloc(sizeof(int64_t)*(THTensor_nDimensionLegacyNoScalars(TENSOR1))); \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
\
TENSOR1##_data = THTensor_getStoragePtr(TENSOR1)->data<TYPE1>()+(TENSOR1)->storage_offset(); \
TENSOR1##_stride = THTensor_strideLegacyNoScalars((TENSOR1), DIMENSION); \
TENSOR1##_size = THTensor_sizeLegacyNoScalars((TENSOR1), DIMENSION); \
\
TENSOR2##_data = THTensor_getStoragePtr(TENSOR2)->data<TYPE2>()+(TENSOR2)->storage_offset(); \
TENSOR2##_stride = THTensor_strideLegacyNoScalars((TENSOR2), DIMENSION); \
TENSOR2##_size = THTensor_sizeLegacyNoScalars((TENSOR2), DIMENSION); \
\
TENSOR3##_data = THTensor_getStoragePtr(TENSOR3)->data<TYPE3>()+(TENSOR3)->storage_offset(); \
TENSOR3##_stride = THTensor_strideLegacyNoScalars((TENSOR3), DIMENSION); \
TENSOR3##_size = THTensor_sizeLegacyNoScalars((TENSOR3), DIMENSION); \
\
while(!TH_TENSOR_DIM_APPLY_hasFinished) \
{ \
CODE \
\
if(THTensor_nDimensionLegacyNoScalars(TENSOR1) == 1) \
break; \
\
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
{ \
if(TH_TENSOR_DIM_APPLY_i == DIMENSION) \
{ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyNoScalars(TENSOR1)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
continue; \
} \
\
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]++; \
TENSOR1##_data += THTensor_strideLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i); \
TENSOR2##_data += THTensor_strideLegacyNoScalars(TENSOR2, TH_TENSOR_DIM_APPLY_i); \
TENSOR3##_data += THTensor_strideLegacyNoScalars(TENSOR3, TH_TENSOR_DIM_APPLY_i); \
\
if(TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] == THTensor_sizeLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i)) \
{ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyNoScalars(TENSOR1)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
else \
{ \
TENSOR1##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i); \
TENSOR2##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR2, TH_TENSOR_DIM_APPLY_i); \
TENSOR3##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR3, TH_TENSOR_DIM_APPLY_i); \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
} \
} \
else \
break; \
} \
} \
THFree(TH_TENSOR_DIM_APPLY_counter); \
}
/**
* Similar to DIM_APPLY(...) but we maintain two sets of pointers: one for the first tensor
* and one for the second. The two tensors must have the same shape, other than at the
* specified DIMENSION. This function makes it easy to store the output from reducing the
* TENSOR at index. For example, in the sum example described below, we could instead do:
*
* int64_t i = 0;
* TYPE1 sum;
*
* for (i = 0; i < TENSOR1##_size; ++i) {
* sum += TENSOR1##_data[i * TENSOR1##_stride]
* }
* *TENSOR2##_data = (TYPE2) sum;
*
* In particular, we guarantee that the offset into TENSOR2 will be what you would get if
* you applied all of the index values used to generate the offset into TENSOR1.
*/
#define TH_TENSOR_DIM_APPLY2(TYPE1, TENSOR1, TYPE2, TENSOR2, DIMENSION, CODE) \
{ \
TYPE1 *TENSOR1##_data = NULL; \
TH_UNUSED int64_t TENSOR1##_stride = 0, TENSOR1##_size = 0; \
TYPE2 *TENSOR2##_data = NULL; \
TH_UNUSED int64_t TENSOR2##_stride = 0, TENSOR2##_size = 0; \
int64_t *TH_TENSOR_DIM_APPLY_counter = NULL; \
int TH_TENSOR_DIM_APPLY_hasFinished = THTensor_(numel)(TENSOR1) == 0; \
int TH_TENSOR_DIM_APPLY_i; \
\
if( (DIMENSION < 0) || (DIMENSION >= THTensor_nDimensionLegacyNoScalars(TENSOR1)) ) \
THError("invalid dimension %d (expected to be 0 <= dim < %d)", DIMENSION, THTensor_nDimensionLegacyAll(TENSOR1)); \
if( THTensor_nDimensionLegacyNoScalars(TENSOR1) != THTensor_nDimensionLegacyNoScalars(TENSOR2)) { \
AT_ERROR("inconsistent tensor size, expected ", #TENSOR1, " ", TENSOR1->sizes(), " and ", #TENSOR2, " ", TENSOR2->sizes(), " to have the same number of dimensions"); \
} \
TH_UNUSED int shape_check_flag = 0; \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
{ \
if(TH_TENSOR_DIM_APPLY_i == DIMENSION) \
continue; \
if(THTensor_sizeLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i) != THTensor_sizeLegacyNoScalars(TENSOR2, TH_TENSOR_DIM_APPLY_i)) { \
AT_ERROR("Expected ", #TENSOR1, " ", TENSOR1->sizes(), " and ", #TENSOR2, " ", TENSOR2->sizes(), " to have the same size in dimension ", DIMENSION); \
} \
} \
\
if (TH_TENSOR_DIM_APPLY_hasFinished) { \
return; \
} \
TH_TENSOR_DIM_APPLY_counter = (int64_t*)THAlloc(sizeof(int64_t)*(THTensor_nDimensionLegacyNoScalars(TENSOR1))); \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
\
TENSOR1##_data = THTensor_getStoragePtr(TENSOR1)->data<TYPE1>()+(TENSOR1)->storage_offset(); \
TENSOR1##_stride = THTensor_strideLegacyNoScalars((TENSOR1), DIMENSION); \
TENSOR1##_size = THTensor_sizeLegacyNoScalars(TENSOR1, DIMENSION); \
\
TENSOR2##_data = THTensor_getStoragePtr(TENSOR2)->data<TYPE2>()+(TENSOR2)->storage_offset(); \
TENSOR2##_stride = THTensor_strideLegacyNoScalars((TENSOR2), DIMENSION); \
TENSOR2##_size = THTensor_sizeLegacyNoScalars(TENSOR2, DIMENSION); \
\
while(!TH_TENSOR_DIM_APPLY_hasFinished) \
{ \
CODE \
\
if(THTensor_nDimensionLegacyNoScalars(TENSOR1) == 1) \
break; \
\
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyNoScalars(TENSOR1); TH_TENSOR_DIM_APPLY_i++) \
{ \
if(TH_TENSOR_DIM_APPLY_i == DIMENSION) \
{ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyNoScalars(TENSOR1)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
continue; \
} \
\
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]++; \
TENSOR1##_data += THTensor_strideLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i); \
TENSOR2##_data += THTensor_strideLegacyNoScalars(TENSOR2, TH_TENSOR_DIM_APPLY_i); \
\
if(TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] == THTensor_sizeLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i)) \
{ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyNoScalars(TENSOR1)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
else \
{ \
TENSOR1##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR1, TH_TENSOR_DIM_APPLY_i); \
TENSOR2##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR2, TH_TENSOR_DIM_APPLY_i); \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
} \
} \
else \
break; \
} \
} \
THFree(TH_TENSOR_DIM_APPLY_counter); \
}
/**
* The basic idea for DIM_APPLY: Given a TENSOR and a DIMENSION, provide access to the data stored
* at all sets of dimension values other than DIMENSION, such that we can get all the values at those
* fixed indices for the various values at DIMENSION.
*
* Suppose we have a 2x3x4 Tensor A, and we have DIMENSION=2. Then we will hit CODE (2x3) times, and the
* pointer into storage will be at:
*
* A[0][0]
* A[0][1]
* A[0][2]
* A[1][0]
* A[1][1]
* A[1][2]
*
* And at each point, we can access the data for each of the four elements of the Tensor via
* TENSOR##_stride. So for example, if we wanted to sum the elements there, we could do:
*
* int64_t i = 0;
* TYPE sum;
* for (i = 0; i < TENSOR##_size; i++) {
* sum += TENSOR##_data[i * TENSOR##_stride]
* }
*
* Note that we don't have to have DIMENSION be the last tensor. If we have DIMENSION=1, then we will hit the
* code (2x4) times, with pointer into the storage at:
*
* offset +
* stride_0 * 0 + stride_2 * 0
* stride_0 * 1 + stride_2 * 0
* stride_0 * 0 + stride_2 * 1
* stride_0 * 1 + stride_2 * 1
* stride_0 * 0 + stride_2 * 2
* stride_0 * 1 + stride_2 * 2
* stride_0 * 0 + stride_2 * 3
* stride_0 * 1 + stride_2 * 3
*
* So we can again sum over the values at DIMENSION with the other indices fixed.
*/
#define TH_TENSOR_DIM_APPLY(TYPE, TENSOR, DIMENSION, CODE) \
{ \
TYPE *TENSOR##_data = NULL; \
int64_t TENSOR##_stride = 0, TENSOR##_size = 0; \
int64_t *TH_TENSOR_DIM_APPLY_counter = NULL; \
int TH_TENSOR_DIM_APPLY_hasFinished = 0; \
int TH_TENSOR_DIM_APPLY_i; \
\
if( (DIMENSION < 0) || (DIMENSION >= THTensor_nDimensionLegacyAll(TENSOR)) ) \
THError("invalid dimension"); \
\
TENSOR##_data = THTensor_getStoragePtr(TENSOR)->data<TYPE>()+(TENSOR)->storage_offset(); \
TENSOR##_stride = THTensor_strideLegacyNoScalars((TENSOR), DIMENSION); \
TENSOR##_size = THTensor_sizeLegacyNoScalars(TENSOR, DIMENSION); \
/* Counter stores the indices into the Tensor at any time */ \
TH_TENSOR_DIM_APPLY_counter = (int64_t*)THAlloc(sizeof(int64_t)*(THTensor_nDimensionLegacyAll(TENSOR))); \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyAll(TENSOR); TH_TENSOR_DIM_APPLY_i++) \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
\
while(!TH_TENSOR_DIM_APPLY_hasFinished) \
{ \
CODE \
\
if(THTensor_nDimensionLegacyAll(TENSOR) == 1) \
break; \
\
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < THTensor_nDimensionLegacyAll(TENSOR); TH_TENSOR_DIM_APPLY_i++) \
{ \
/* Check if the index is equal to DIMENSION. We don't need to update the */ \
/* offset if this is the case, and can consider the next index. However, */ \
/* in the case that the DIMENSION is the last index in the Tensor, then */ \
/* we have parsed the entire tensor and can exit */ \
if(TH_TENSOR_DIM_APPLY_i == DIMENSION) \
{ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyAll(TENSOR)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
continue; \
} \
\
/* Bump the counter at this index, update the pointer */ \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]++; \
TENSOR##_data += THTensor_strideLegacyNoScalars(TENSOR, TH_TENSOR_DIM_APPLY_i); \
\
if(TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] == THTensor_sizeLegacyNoScalars(TENSOR, TH_TENSOR_DIM_APPLY_i)) \
{ \
/* Handled TENSOR_size(dim) iterations for DIM_APPLY_i. If this is the last dimension, exit */ \
if(TH_TENSOR_DIM_APPLY_i == THTensor_nDimensionLegacyAll(TENSOR)-1) \
{ \
TH_TENSOR_DIM_APPLY_hasFinished = 1; \
break; \
} \
else \
{ \
/* Reset the counter, and the pointer to the beginning of the storage for this combination of indices */ \
TENSOR##_data -= TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i]*THTensor_strideLegacyNoScalars(TENSOR, TH_TENSOR_DIM_APPLY_i); \
TH_TENSOR_DIM_APPLY_counter[TH_TENSOR_DIM_APPLY_i] = 0; \
} \
} \
else \
break; \
} \
} \
THFree(TH_TENSOR_DIM_APPLY_counter); \
}
#endif