forked from kulinseth/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForeachOpsKernels.cpp
340 lines (315 loc) · 23.3 KB
/
ForeachOpsKernels.cpp
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
#include <vector>
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>
#include <ATen/native/ForeachUtils.h>
#include <c10/util/irange.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Operators.h>
#else
#include <ATen/ops/_foreach_abs_native.h>
#include <ATen/ops/_foreach_acos_native.h>
#include <ATen/ops/_foreach_add_native.h>
#include <ATen/ops/_foreach_addcdiv_native.h>
#include <ATen/ops/_foreach_addcmul_native.h>
#include <ATen/ops/_foreach_asin_native.h>
#include <ATen/ops/_foreach_atan_native.h>
#include <ATen/ops/_foreach_ceil_native.h>
#include <ATen/ops/_foreach_cos_native.h>
#include <ATen/ops/_foreach_cosh_native.h>
#include <ATen/ops/_foreach_div_native.h>
#include <ATen/ops/_foreach_erf_native.h>
#include <ATen/ops/_foreach_erfc_native.h>
#include <ATen/ops/_foreach_exp_native.h>
#include <ATen/ops/_foreach_expm1_native.h>
#include <ATen/ops/_foreach_floor_native.h>
#include <ATen/ops/_foreach_frac_native.h>
#include <ATen/ops/_foreach_lerp_native.h>
#include <ATen/ops/_foreach_lgamma_native.h>
#include <ATen/ops/_foreach_log10_native.h>
#include <ATen/ops/_foreach_log1p_native.h>
#include <ATen/ops/_foreach_log2_native.h>
#include <ATen/ops/_foreach_log_native.h>
#include <ATen/ops/_foreach_maximum_native.h>
#include <ATen/ops/_foreach_minimum_native.h>
#include <ATen/ops/_foreach_mul_native.h>
#include <ATen/ops/_foreach_neg_native.h>
#include <ATen/ops/_foreach_norm_native.h>
#include <ATen/ops/_foreach_reciprocal_native.h>
#include <ATen/ops/_foreach_round_native.h>
#include <ATen/ops/_foreach_sigmoid_native.h>
#include <ATen/ops/_foreach_sin_native.h>
#include <ATen/ops/_foreach_sinh_native.h>
#include <ATen/ops/_foreach_sqrt_native.h>
#include <ATen/ops/_foreach_sub_native.h>
#include <ATen/ops/_foreach_tan_native.h>
#include <ATen/ops/_foreach_tanh_native.h>
#include <ATen/ops/_foreach_trunc_native.h>
#include <ATen/ops/_foreach_zero_native.h>
#include <ATen/ops/_foreach_clamp_min_native.h>
#include <ATen/ops/_foreach_clamp_max_native.h>
#include <ATen/ops/_foreach_pow_native.h>
#include <ATen/ops/linalg_vector_norm.h>
#include <ATen/ops/maximum.h>
#include <ATen/ops/minimum.h>
#include <ATen/ops/zeros_like_ops.h>
#include <ATen/ops/pow.h>
#endif
namespace at { namespace native {
#define FOREACH_BINARY_OP_SCALAR(OP) \
void foreach_tensor_##OP##_scalar_kernel_slow_(TensorList tensors, const Scalar& scalar) { \
check_foreach_api_restrictions(tensors); \
\
for (auto& t: tensors) { \
t.OP##_(scalar); \
} \
} \
\
std::vector<Tensor> foreach_tensor_##OP##_scalar_kernel_slow(TensorList tensors, const Scalar& scalar) { \
check_foreach_api_restrictions(tensors); \
\
std::vector<Tensor> result; \
result.reserve(tensors.size()); \
for (const auto& t: tensors) { \
result.emplace_back(t.OP(scalar)); \
} \
\
return result; \
}
#define FOREACH_BINARY_OP_SCALARLIST(OP) \
void foreach_tensor_##OP##_scalarlist_kernel_slow_(TensorList tensors, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(tensors, scalars); \
\
for (const auto i : c10::irange(tensors.size())) { \
tensors[i].OP##_(scalars[i]); \
} \
} \
\
std::vector<Tensor> foreach_tensor_##OP##_scalarlist_kernel_slow(TensorList tensors, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(tensors, scalars); \
std::vector<Tensor> result; \
result.reserve(tensors.size()); \
for (const auto i : c10::irange(tensors.size())) { \
result.emplace_back(tensors[i].OP(scalars[i])); \
} \
\
return result; \
}
#define FOREACH_BINARY_OP_LIST(OP) \
std::vector<Tensor> foreach_tensor_##OP##_list_kernel_slow(TensorList tensors1, TensorList tensors2) { \
check_foreach_api_restrictions(tensors1, tensors2); \
\
std::vector<Tensor> result; \
result.reserve(tensors1.size()); \
for (const auto i : c10::irange(tensors1.size())) { \
result.emplace_back(tensors1[i].OP(tensors2[i])); \
} \
\
return result; \
} \
\
void foreach_tensor_##OP##_list_kernel_slow_(TensorList tensors1, TensorList tensors2) { \
check_foreach_api_restrictions(tensors1, tensors2); \
\
for (const auto i : c10::irange(tensors1.size())) { \
tensors1[i].OP##_(tensors2[i]); \
} \
}
#define FOREACH_BINARY_OP_LIST_ALPHA(OP) \
std::vector<Tensor> foreach_tensor_##OP##_list_kernel_slow(TensorList tensors1, TensorList tensors2, const Scalar& alpha) { \
check_foreach_api_restrictions(tensors1, tensors2); \
\
std::vector<Tensor> result; \
result.reserve(tensors1.size()); \
for (const auto i : c10::irange(tensors1.size())) { \
result.emplace_back(tensors1[i].OP(tensors2[i], alpha)); \
} \
\
return result; \
} \
\
void foreach_tensor_##OP##_list_kernel_slow_(TensorList tensors1, TensorList tensors2, const Scalar& alpha) { \
check_foreach_api_restrictions(tensors1, tensors2); \
\
for (const auto i : c10::irange(tensors1.size())) { \
tensors1[i].OP##_(tensors2[i], alpha); \
} \
}
#define FOREACH_UNARY_OP(OP) \
std::vector<Tensor> foreach_tensor_##OP##_slow(TensorList tensors) { \
check_foreach_api_restrictions(tensors); \
\
std::vector<Tensor> result; \
result.reserve(tensors.size()); \
for (const auto& t : tensors) { \
result.emplace_back(t.OP()); \
} \
\
return result; \
} \
\
void foreach_tensor_##OP##_slow_(TensorList tensors) { \
check_foreach_api_restrictions(tensors); \
\
for (auto& t : tensors) { \
t.OP##_(); \
} \
}
#define FOREACH_POINTWISE_OP_SCALAR(OP) \
std::vector<Tensor> foreach_tensor_##OP##_scalar_slow(TensorList input, TensorList tensors1, TensorList tensors2, const Scalar& scalar) { \
check_foreach_api_restrictions(input, tensors1, tensors2); \
\
std::vector<Tensor> result; \
for(const auto i : c10::irange(input.size())) { \
result.emplace_back(input[i].OP(tensors1[i], tensors2[i], scalar)); \
} \
\
return result; \
} \
\
void foreach_tensor_##OP##_scalar_slow_(TensorList input, TensorList tensors1, TensorList tensors2, const Scalar& scalar) { \
check_foreach_api_restrictions(input, tensors1, tensors2); \
\
for(const auto i : c10::irange(input.size())) { \
input[i].OP##_(tensors1[i], tensors2[i], scalar); \
} \
} \
#define FOREACH_POINTWISE_OP_SCALARLIST(OP) \
std::vector<Tensor> foreach_tensor_##OP##_scalarlist_slow(TensorList input, TensorList tensors1, TensorList tensors2, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
\
std::vector<Tensor> result; \
for(const auto i : c10::irange(input.size())) { \
result.emplace_back(input[i].OP(tensors1[i], tensors2[i], scalars[i])); \
} \
\
return result; \
} \
\
void foreach_tensor_##OP##_scalarlist_slow_(TensorList input, TensorList tensors1, TensorList tensors2, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
\
for(const auto i : c10::irange(input.size())) { \
input[i].OP##_(tensors1[i], tensors2[i], scalars[i]); \
} \
}
#define FOREACH_POINTWISE_OP_TENSOR(OP) \
std::vector<Tensor> foreach_tensor_##OP##_tensor_slow( \
TensorList input, \
TensorList tensors1, \
TensorList tensors2, \
const Tensor& scalars_) { \
auto scalars = convert_tensor_to_scalar_list(scalars_, input.size()); \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
return foreach_tensor_##OP##_scalarlist_slow( \
input, tensors1, tensors2, scalars); \
} \
\
void foreach_tensor_##OP##_tensor_slow_( \
TensorList input, \
TensorList tensors1, \
TensorList tensors2, \
const Tensor& scalars_) { \
auto scalars = convert_tensor_to_scalar_list(scalars_, input.size()); \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
foreach_tensor_##OP##_scalarlist_slow_( \
input, tensors1, tensors2, scalars); \
}
FOREACH_BINARY_OP_LIST_ALPHA(add);
FOREACH_BINARY_OP_LIST_ALPHA(sub);
FOREACH_BINARY_OP_LIST_ALPHA(lerp);
FOREACH_BINARY_OP_SCALAR(add);
FOREACH_BINARY_OP_SCALAR(sub);
FOREACH_BINARY_OP_SCALAR(mul);
FOREACH_BINARY_OP_SCALAR(div);
FOREACH_BINARY_OP_SCALAR(clamp_min);
FOREACH_BINARY_OP_SCALAR(clamp_max);
FOREACH_BINARY_OP_SCALAR(pow);
FOREACH_BINARY_OP_SCALARLIST(add);
FOREACH_BINARY_OP_SCALARLIST(sub);
FOREACH_BINARY_OP_SCALARLIST(mul);
FOREACH_BINARY_OP_SCALARLIST(div);
FOREACH_BINARY_OP_SCALARLIST(clamp_min);
FOREACH_BINARY_OP_SCALARLIST(clamp_max);
FOREACH_BINARY_OP_SCALARLIST(pow);
FOREACH_BINARY_OP_LIST(mul);
FOREACH_BINARY_OP_LIST(div);
FOREACH_BINARY_OP_LIST(clamp_min);
FOREACH_BINARY_OP_LIST(clamp_max);
FOREACH_BINARY_OP_LIST(pow);
FOREACH_UNARY_OP(sqrt);
FOREACH_UNARY_OP(exp);
FOREACH_UNARY_OP(abs);
FOREACH_UNARY_OP(acos);
FOREACH_UNARY_OP(asin);
FOREACH_UNARY_OP(atan);
FOREACH_UNARY_OP(ceil);
FOREACH_UNARY_OP(cos);
FOREACH_UNARY_OP(cosh);
FOREACH_UNARY_OP(erf);
FOREACH_UNARY_OP(erfc);
FOREACH_UNARY_OP(expm1);
FOREACH_UNARY_OP(floor);
FOREACH_UNARY_OP(log);
FOREACH_UNARY_OP(log10);
FOREACH_UNARY_OP(log1p);
FOREACH_UNARY_OP(log2);
FOREACH_UNARY_OP(neg);
FOREACH_UNARY_OP(tan);
FOREACH_UNARY_OP(tanh);
FOREACH_UNARY_OP(sin);
FOREACH_UNARY_OP(sinh);
FOREACH_UNARY_OP(round);
FOREACH_UNARY_OP(lgamma);
FOREACH_UNARY_OP(frac);
FOREACH_UNARY_OP(trunc);
FOREACH_UNARY_OP(reciprocal);
FOREACH_UNARY_OP(sigmoid);
FOREACH_POINTWISE_OP_SCALAR(addcdiv);
FOREACH_POINTWISE_OP_SCALAR(addcmul);
FOREACH_POINTWISE_OP_SCALARLIST(addcdiv);
FOREACH_POINTWISE_OP_SCALARLIST(addcmul);
FOREACH_POINTWISE_OP_TENSOR(addcdiv);
FOREACH_POINTWISE_OP_TENSOR(addcmul);
#define FOREACH_TERNARY_OP(OP) \
std::vector<Tensor> foreach_tensor_ternary_##OP##_slow(TensorList tensors1, TensorList tensors2, TensorList tensors3) { \
check_foreach_api_restrictions(tensors1, tensors2, tensors3); \
std::vector<Tensor> result; \
for (const auto i : c10::irange(tensors1.size())) { \
result.emplace_back(tensors1[i].OP(tensors2[i], tensors3[i])); \
} \
return result; \
} \
\
void foreach_tensor_ternary_##OP##_slow_(TensorList tensors1, TensorList tensors2, TensorList tensors3) { \
check_foreach_api_restrictions(tensors1, tensors2, tensors3); \
for (const auto i : c10::irange(tensors1.size())) { \
tensors1[i].OP##_(tensors2[i], tensors3[i]); \
} \
} \
FOREACH_TERNARY_OP(lerp);
void foreach_tensor_zero_slow_(TensorList tensors) {
check_foreach_api_restrictions(tensors);
for (auto& t : tensors) {
t.zero_();
}
}
std::vector<Tensor> foreach_tensor_norm_slow(TensorList tensors, const Scalar& ord) {
check_foreach_api_restrictions(tensors);
std::vector<Tensor> result;
for (const auto& t : tensors) {
result.emplace_back(at::linalg_vector_norm(t, ord));
}
return result;
}
std::vector<Tensor> foreach_scalar_pow_list_kernel_slow(const Scalar& self, TensorList exponent) {
check_foreach_api_restrictions(exponent);
std::vector<Tensor> result;
result.reserve(exponent.size());
for (const auto & t : exponent) {
result.emplace_back(at::pow(self, t));
}
return result;
}
}} // namespace at::native