-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathForeachOpScalarList.cpp
More file actions
165 lines (146 loc) · 7.65 KB
/
ForeachOpScalarList.cpp
File metadata and controls
165 lines (146 loc) · 7.65 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
/*
* Copyright 2020-2026 Intel Corporation
*
* Licensed 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
*/
#include <ATen/native/BinaryOps.h>
#include <ATen/native/ForeachUtils.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_clamp_max_native.h>
#include <ATen/ops/_foreach_clamp_min_native.h>
#include <ATen/ops/_foreach_div_native.h>
#include <ATen/ops/_foreach_lerp_native.h>
#include <ATen/ops/_foreach_mul_native.h>
#include <ATen/ops/_foreach_pow_native.h>
#include <ATen/ops/_foreach_sub_native.h>
#include <ATen/ops/empty_like.h>
#include <ATen/native/xpu/sycl/ForeachBinaryOpScalarListKernels.h>
#include <ATen/native/xpu/sycl/ForeachPointwiseOpScalarListKernels.h>
#include <ATen/native/xpu/sycl/ForeachTernaryOpScalarListKernels.h>
namespace at {
namespace native {
#define FOREACH_BINARY_OP_SCALARLIST(NAME, DIV_OP) \
void foreach_tensor_##NAME##_scalarlist_kernel_xpu_( \
TensorList tensors, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(tensors, scalars); \
if (!can_use_fast_route(tensors, scalars, DIV_OP)) { \
return foreach_tensor_##NAME##_scalarlist_kernel_slow_( \
tensors, scalars); \
} \
\
xpu::FOREACH_BINARY_SCALARLIST_INPLACE_KERNEL_NAME(NAME)( \
tensors, scalars); \
} \
\
std::vector<Tensor> foreach_tensor_##NAME##_scalarlist_kernel_xpu( \
TensorList tensors, at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(tensors, scalars); \
if (!can_use_fast_route(tensors, scalars, DIV_OP)) { \
return foreach_tensor_##NAME##_scalarlist_kernel_slow(tensors, scalars); \
} \
\
return xpu::FOREACH_BINARY_SCALARLIST_KERNEL_NAME(NAME)(tensors, scalars); \
}
// This does not use FOREACH_BINARY_OP_SCALARLIST because
// In the case of subtraction, we dont allow scalar to be boolean following the
// torch.sub logic
void foreach_tensor_sub_scalarlist_kernel_xpu_(
TensorList tensors,
at::ArrayRef<Scalar> scalars) {
check_foreach_api_restrictions(tensors, scalars);
for (const auto i : c10::irange(tensors.size())) {
sub_check(tensors[i], scalars[i]);
}
if (!can_use_fast_route({tensors}, scalars, false)) {
return foreach_tensor_sub_scalarlist_kernel_slow_(tensors, scalars);
}
xpu::FOREACH_BINARY_SCALARLIST_INPLACE_KERNEL_NAME(sub)(tensors, scalars);
}
std::vector<Tensor> foreach_tensor_sub_scalarlist_kernel_xpu(
TensorList tensors,
at::ArrayRef<Scalar> scalars) {
check_foreach_api_restrictions(tensors, scalars);
for (const auto i : c10::irange(tensors.size())) {
sub_check(tensors[i], scalars[i]);
}
if (!can_use_fast_route({tensors}, scalars, false)) {
return foreach_tensor_sub_scalarlist_kernel_slow(tensors, scalars);
}
return xpu::FOREACH_BINARY_SCALARLIST_KERNEL_NAME(sub)(tensors, scalars);
}
FOREACH_BINARY_OP_SCALARLIST(add, /*div_op*/ false);
FOREACH_BINARY_OP_SCALARLIST(mul, /*div_op*/ false);
FOREACH_BINARY_OP_SCALARLIST(div, /*div_op*/ true);
FOREACH_BINARY_OP_SCALARLIST(clamp_max, /*div_op*/ true);
FOREACH_BINARY_OP_SCALARLIST(clamp_min, /*div_op*/ true);
FOREACH_BINARY_OP_SCALARLIST(pow, true);
#define FOREACH_POINTWISE_OP_SCALARLIST(NAME) \
std::vector<Tensor> foreach_tensor_##NAME##_scalarlist_xpu( \
TensorList input, \
TensorList tensors1, \
TensorList tensors2, \
at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
\
if (!can_use_fast_route({input, tensors1, tensors2}, scalars) || \
has_integral_tensor(input, /* includeBool */ true)) { \
return foreach_tensor_##NAME##_scalarlist_slow( \
input, tensors1, tensors2, scalars); \
} \
\
return xpu::foreach_##NAME##_kernel(input, tensors1, tensors2, scalars); \
} \
\
void foreach_tensor_##NAME##_scalarlist_xpu_( \
TensorList input, \
TensorList tensors1, \
TensorList tensors2, \
at::ArrayRef<Scalar> scalars) { \
check_foreach_api_restrictions(input, tensors1, tensors2, scalars); \
\
if (!can_use_fast_route({input, tensors1, tensors2}, scalars) || \
has_integral_tensor(input, /* includeBool */ true)) { \
return foreach_tensor_##NAME##_scalarlist_slow_( \
input, tensors1, tensors2, scalars); \
} \
\
xpu::foreach_##NAME##_kernel_(input, tensors1, tensors2, scalars); \
}
FOREACH_POINTWISE_OP_SCALARLIST(addcmul)
FOREACH_POINTWISE_OP_SCALARLIST(addcdiv)
std::vector<at::Tensor> foreach_tensor_lerp_scalarlist_xpu(
TensorList tensors1,
TensorList tensors2,
at::ArrayRef<Scalar> scalars) {
check_foreach_api_restrictions(tensors1, tensors2, scalars);
if (!can_use_fast_route({tensors1, tensors2}, scalars, true)) {
return foreach_tensor_lerp_scalarlist_kernel_slow(
tensors1, tensors2, scalars);
}
std::vector<at::Tensor> vec_res;
vec_res.reserve(tensors1.size());
for (const auto& t : tensors1) {
vec_res.emplace_back(at::empty_like(t));
}
xpu::foreach_lerp_scalarlist_kernel(tensors1, tensors2, scalars, vec_res);
return vec_res;
}
void foreach_tensor_lerp_scalarlist_xpu_(
TensorList tensors1,
TensorList tensors2,
at::ArrayRef<Scalar> scalars) {
check_foreach_api_restrictions(tensors1, tensors2, scalars);
if (!can_use_fast_route({tensors1, tensors2}, scalars, true)) {
return foreach_tensor_lerp_scalarlist_kernel_slow_(
tensors1, tensors2, scalars);
}
xpu::foreach_lerp_scalarlist_kernel_(tensors1, tensors2, scalars);
}
}; // namespace native
} // namespace at