forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryOps.cpp
161 lines (131 loc) · 4.84 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
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>
#include <ATen/Config.h>
#include <ATen/ExpandUtils.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/add_native.h>
#include <ATen/ops/empty_native.h>
#include <ATen/ops/mul_native.h>
#endif
#if !AT_MKLDNN_ENABLED()
namespace at {
namespace native {
Tensor& mkldnn_add_out(
const Tensor& self,
const Tensor& other,
const Scalar& alpha,
Tensor& result
) {
TORCH_CHECK(false, "mkldnn_add_out: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_add(const Tensor& self, const Tensor& other, const Scalar& alpha) {
TORCH_CHECK(false, "mkldnn_add: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_add_(Tensor& self, const Tensor& other, const Scalar& alpha) {
TORCH_CHECK(false, "mkldnn_add_: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_mul_out(const Tensor& self, const Tensor& other, Tensor& result) {
TORCH_CHECK(false, "mkldnn_mul_out: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_mul(const Tensor& self, const Tensor& other) {
TORCH_CHECK(false, "mkldnn_mul: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_mul_(Tensor& self, const Tensor& other) {
TORCH_CHECK(false, "mkldnn_mul_: ATen not compiled with MKLDNN support");
}
} // namespace native
} // namespace at
#else // AT_MKLDNN_ENABLED
#include <ATen/native/mkldnn/MKLDNNCommon.h>
namespace at {
namespace native {
static Tensor emptyBinaryOp(const Tensor& self, const Tensor& other) {
if (!self.requires_grad() && !other.requires_grad()) {
auto out_size = infer_size(self.sizes(), other.sizes());
auto out_dtype = promoteTypes(
c10::typeMetaToScalarType(self.dtype()),
c10::typeMetaToScalarType(other.dtype()));
TORCH_CHECK(
self.device() == other.device(),
"Expected same device for binary mkldnn op");
return empty_mkldnn(
out_size,
out_dtype,
self.options().layout_opt(),
self.options().device_opt(),
self.options().pinned_memory_opt());
} else {
TORCH_CHECK(
false,
"MKLDNN does not support Binary Ops with a 0-dimension Tensor in training");
}
}
Tensor& mkldnn_add_out(
const Tensor& self,
const Tensor& other,
const Scalar& alpha,
Tensor& result
) {
ideep::tensor& x = itensor_from_mkldnn(self);
ideep::tensor& y = itensor_from_mkldnn(other);
ideep::tensor& z = itensor_from_mkldnn(result);
if (result.is_same(other)) {
const std::vector<float> scales{alpha.to<float>(), 1.0};
ideep::sum::compute(scales, {y, x}, z);
} else {
const std::vector<float> scales{1.0, alpha.to<float>()};
ideep::sum::compute(scales, {x, y}, z);
}
return result;
}
Tensor mkldnn_add(const Tensor& self, const Tensor& other, const Scalar& alpha) {
if (self.numel() == 0 || other.numel() == 0) {
return emptyBinaryOp(self, other);
}
ideep::tensor& x = itensor_from_mkldnn(self);
ideep::tensor& y = itensor_from_mkldnn(other);
ideep::tensor z;
const std::vector<float> scales{1.0, alpha.to<float>()};
ideep::sum::compute(scales, {x, y}, z);
return new_with_itensor_mkldnn(std::move(z), optTypeMetaToScalarType(self.options().dtype_opt()),
self.options().device_opt());
}
Tensor& mkldnn_add_(Tensor& self, const Tensor& other, const Scalar& alpha) {
return native::mkldnn_add_out(self, other, alpha, self);
}
Tensor& mkldnn_mul_out(const Tensor& self, const Tensor& other, Tensor& result) {
TORCH_CHECK(result.sizes() == self.sizes(),
"mkldnn_mul_out: the output size should be same as input size");
ideep::tensor& z = itensor_from_mkldnn(result);
ideep::tensor& x = itensor_from_mkldnn(self);
// for zero_dim tensor
if (other.ndimension() == 0) {
ideep::eltwise_forward::compute(
x, z, ideep::algorithm::eltwise_linear,
ideep::prop_kind::forward_inference, /*alpha*/ other.item().to<float>());
return result;
} else {
TORCH_CHECK(self.sizes() == other.sizes(),
"mkldnn_mul_out: currently mkldnn not support broadcasting");
ideep::tensor y = itensor_from_mkldnn(other);
ideep::binary::compute(x, y, z, dnnl::algorithm::binary_mul);
return result;
}
}
Tensor mkldnn_mul(const Tensor& self, const Tensor& other) {
if (self.numel() == 0 || other.numel() == 0) {
return emptyBinaryOp(self, other);
}
Tensor result = empty_mkldnn(self.sizes(), optTypeMetaToScalarType(self.options().dtype_opt()),
self.options().layout_opt(), self.options().device_opt(),
self.options().pinned_memory_opt());
return native::mkldnn_mul_out(self, other, result);
}
Tensor& mkldnn_mul_(Tensor& self, const Tensor& other) {
return native::mkldnn_mul_out(self, other, self);
}
} // namespace native
} // namespace at
#endif // AT_MKLDNN_ENABLED