forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryOps.cpp
184 lines (156 loc) · 5.52 KB
/
BinaryOps.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
#include <ATen/native/BinaryOps.h>
#include <ATen/ATen.h>
#include <ATen/Dispatch.h>
#include <ATen/NativeFunctions.h>
#include <ATen/native/TensorIterator.h>
namespace at {
namespace native {
DEFINE_DISPATCH(add_stub);
DEFINE_DISPATCH(sub_stub);
DEFINE_DISPATCH(mul_stub);
DEFINE_DISPATCH(div_stub);
Tensor& add_out(Tensor& result, const Tensor& self, const Tensor& other, Scalar alpha) {
if (other.is_sparse()) {
if (self.is_sparse()) {
at::_sparse_add_out(result, self, other, alpha);
} else {
at::_sparse_dense_add_out(result, self, SparseTensorRef(other), alpha);
}
return result;
} else if (self.is_sparse()) {
AT_ERROR("add(sparse, dense) is not supported. Use add(dense, sparse) instead.");
}
auto iter = TensorIterator::binary_op(result, self, other);
add_stub(iter->device_type(), *iter, alpha);
return result;
}
Tensor add(const Tensor& self, const Tensor& other, Scalar alpha) {
Tensor result;
if (other.is_sparse()) {
result = at::empty({0}, self.options());
return native::add_out(result, self, other, alpha);
}
auto iter = TensorIterator::binary_op(result, self, other);
add_stub(iter->device_type(), *iter, alpha);
return iter->output();
}
Tensor& add_(Tensor& self, const Tensor& other, Scalar alpha) {
return native::add_out(self, self, other, alpha);
}
Tensor& div_out(Tensor& result, const Tensor& self, const Tensor& other) {
if (self.is_sparse()) {
if (other.dim() != 0) {
AT_ERROR("div(): sparse division only supports division by a scalar ",
"(got shape ", other.sizes(), " for argument 'other')");
}
return at::_sparse_div_zerodim_out(result, self, other);
}
auto iter = TensorIterator::binary_op(result, self, other);
div_stub(iter->device_type(), *iter);
return result;
}
Tensor div(const Tensor& self, const Tensor& other) {
Tensor result;
if (self.is_sparse()) {
result = at::empty({0}, self.options());
return native::div_out(result, self, other);
}
auto iter = TensorIterator::binary_op(result, self, other);
div_stub(iter->device_type(), *iter);
return iter->output();
}
Tensor& div_(Tensor& self, const Tensor& other) {
return native::div_out(self, self, other);
}
Tensor& mul_out(Tensor& result, const Tensor& self, const Tensor& other) {
if (self.is_sparse() || other.is_sparse()) {
return at::_sparse_mul_out(result, self, other);
}
auto iter = TensorIterator::binary_op(result, self, other);
mul_stub(iter->device_type(), *iter);
return result;
}
Tensor mul(const Tensor& self, const Tensor& other) {
Tensor result;
if (self.is_sparse() || other.is_sparse()) {
result = at::empty({0}, self.options());
return native::mul_out(result, self, other);
}
auto iter = TensorIterator::binary_op(result, self, other);
mul_stub(iter->device_type(), *iter);
return iter->output();
}
Tensor& mul_(Tensor& self, const Tensor& other) {
return native::mul_out(self, self, other);
}
Tensor& sub_out(Tensor& result, const Tensor& self, const Tensor& other, Scalar alpha) {
if (other.is_sparse()) {
if (!self.sizes().equals(other.sizes())) {
AT_ERROR("sizes do not match");
}
if (self.is_sparse()) {
at::_sparse_add_out(result, self, other, -alpha);
} else {
at::_sparse_dense_add_out(result, self, SparseTensorRef(other), -alpha);
}
return result;
} else if (self.is_sparse()) {
AT_ERROR("sub(sparse, dense) is not supported. Use sub(dense, sparse) instead.");
}
auto iter = TensorIterator::binary_op(result, self, other);
sub_stub(iter->device_type(), *iter, alpha);
return result;
}
Tensor sub(const Tensor& self, const Tensor& other, Scalar alpha) {
Tensor result;
if (other.is_sparse()) {
result = at::empty({0}, self.options());
return native::sub_out(result, self, other, alpha);
}
auto iter = TensorIterator::binary_op(result, self, other);
sub_stub(iter->device_type(), *iter, alpha);
return iter->output();
}
Tensor& sub_(Tensor& self, const Tensor& other, Scalar alpha) {
return native::sub_out(self, self, other, alpha);
}
Tensor rsub(const Tensor& self, const Tensor& other, Scalar alpha) {
return native::sub(other, self, alpha);
}
// These are still needed because we don't have C++ conversions from number
// types (int, float, etc.) to Tensor (only to Scalar). They're not exposed
// to Python.
static Tensor wrapped_scalar_tensor(Scalar scalar) {
auto tensor = scalar_to_tensor(scalar);
tensor.unsafeGetTensorImpl()->set_wrapped_number(true);
return tensor;
}
Tensor add(const Tensor& self, Scalar other, Scalar alpha) {
return native::add(self, wrapped_scalar_tensor(other), alpha);
}
Tensor& add_(Tensor& self, Scalar other, Scalar alpha) {
return native::add_(self, wrapped_scalar_tensor(other), alpha);
}
Tensor div(const Tensor& self, Scalar other) {
return native::div(self, wrapped_scalar_tensor(other));
}
Tensor& div_(Tensor& self, Scalar other) {
return native::div_(self, wrapped_scalar_tensor(other));
}
Tensor mul(const Tensor& self, Scalar other) {
return native::mul(self, wrapped_scalar_tensor(other));
}
Tensor& mul_(Tensor& self, Scalar other) {
return native::mul_(self, wrapped_scalar_tensor(other));
}
Tensor sub(const Tensor& self, Scalar other, Scalar alpha) {
return native::sub(self, wrapped_scalar_tensor(other), alpha);
}
Tensor& sub_(Tensor& self, Scalar other, Scalar alpha) {
return native::sub_(self, wrapped_scalar_tensor(other), alpha);
}
Tensor rsub(const Tensor& self, Scalar other, Scalar alpha) {
return native::rsub(self, wrapped_scalar_tensor(other), alpha);
}
}
} // namespace at