forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryOps.cpp
116 lines (89 loc) · 3.5 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
#include <ATen/ATen.h>
#include <ATen/Config.h>
#include <ATen/NativeFunctions.h>
#if !AT_MKLDNN_ENABLED()
namespace at {
namespace native {
Tensor& mkldnn_add_out(
Tensor& result,
const Tensor& self,
const Tensor& other,
Scalar alpha) {
TORCH_CHECK(false, "mkldnn_add_out: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_add(const Tensor& self, const Tensor& other, Scalar alpha) {
TORCH_CHECK(false, "mkldnn_add: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_add_(Tensor& self, const Tensor& other, Scalar alpha) {
TORCH_CHECK(false, "mkldnn_add_: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_mul_out(Tensor& result, const Tensor& self, const Tensor& other) {
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_EBABLED
#include <ATen/native/mkldnn/MKLDNNCommon.h>
namespace at {
namespace native {
Tensor& mkldnn_add_out(
Tensor& result,
const Tensor& self,
const Tensor& other,
Scalar alpha) {
ideep::tensor& x = itensor_from_mkldnn(self);
ideep::tensor& y = itensor_from_mkldnn(other);
ideep::tensor& z = itensor_from_mkldnn(result);
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, Scalar alpha) {
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, Scalar alpha) {
return native::mkldnn_add_out(self, self, other, alpha);
}
Tensor& mkldnn_mul_out(Tensor& result, const Tensor& self, const Tensor& other) {
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) {
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(result, self, other);
}
Tensor& mkldnn_mul_(Tensor& self, const Tensor& other) {
return native::mkldnn_mul_out(self, self, other);
}
} // namespace native
} // namespace at
#endif // AT_MKLDNN_EBABLED