Skip to content

Commit 70caa2e

Browse files
bddppqfacebook-github-bot
authored andcommitted
Add mkldnn sigmoid operator
Summary: Pull Request resolved: #20820 Reviewed By: dzhulgakov Differential Revision: D15455866 fbshipit-source-id: 712b06dfbd441051dc284a1acdf94926df09bc1d
1 parent 8dedb04 commit 70caa2e

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <ATen/ATen.h>
2+
#include <ATen/Config.h>
3+
#include <ATen/NativeFunctions.h>
4+
5+
#if !AT_MKLDNN_ENABLED()
6+
7+
namespace at {
8+
namespace native {
9+
10+
Tensor mkldnn_sigmoid(const Tensor& self) {
11+
AT_ERROR("mkldnn_sigmoid: ATen not compiled with MKLDNN support");
12+
}
13+
14+
Tensor& mkldnn_sigmoid_(Tensor& self) {
15+
AT_ERROR("mkldnn_sigmoid_: ATen not compiled with MKLDNN support");
16+
}
17+
18+
} // namespace native
19+
} // namespace at
20+
21+
#else // AT_MKLDNN_EBABLED
22+
23+
#include <ATen/native/mkldnn/MKLDNNCommon.h>
24+
25+
namespace at {
26+
namespace native {
27+
28+
Tensor mkldnn_sigmoid(const Tensor& self) {
29+
ideep::tensor& x = itensor_from_mkldnn(self);
30+
ideep::tensor y;
31+
ideep::eltwise_forward::compute(
32+
x, y, ideep::algorithm::eltwise_logistic, ideep::prop_kind::forward);
33+
return new_with_itensor_mkldnn(std::move(y), self.options());
34+
}
35+
36+
Tensor& mkldnn_sigmoid_(Tensor& self) {
37+
ideep::tensor& x = itensor_from_mkldnn(self);
38+
ideep::eltwise_forward::compute(
39+
x, x, ideep::algorithm::eltwise_logistic, ideep::prop_kind::forward);
40+
return self;
41+
}
42+
43+
} // namespace native
44+
} // namespace at
45+
46+
#endif // AT_MKLDNN_EBABLED

aten/src/ATen/native/native_functions.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,12 +1593,17 @@
15931593

15941594
- func: sigmoid(Tensor self) -> Tensor
15951595
variants: function, method
1596+
dispatch:
1597+
CPU: sigmoid
1598+
CUDA: sigmoid
1599+
MkldnnCPU: mkldnn_sigmoid
15961600

15971601
- func: sigmoid_(Tensor(a!) self) -> Tensor(a!)
15981602
variants: function, method
15991603
dispatch:
16001604
CPU: _sigmoid__cpu
16011605
CUDA: _sigmoid__cuda
1606+
MkldnnCPU: mkldnn_sigmoid_
16021607

16031608
- func: sigmoid(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
16041609
dispatch:

test/test_mkldnn.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ def test_linear(self):
248248
self._test_serialization(mkldnn_linear, (x.to_mkldnn(),))
249249
self._test_tracing(mkldnn_linear, (x.to_mkldnn(),))
250250

251+
def test_sigmoid(self):
252+
x = torch.randn(4, 5, dtype=torch.float32) * 10
253+
mkldnn_x = x.to_mkldnn()
254+
self.assertEqual(
255+
torch.sigmoid(x),
256+
torch.sigmoid(mkldnn_x).to_dense(),
257+
)
258+
# inplace
259+
torch.sigmoid_(x)
260+
torch.sigmoid_(mkldnn_x)
261+
self.assertEqual(x, mkldnn_x.to_dense())
262+
251263
def _test_serialization(self, module, inputs):
252264
with TemporaryFileName() as fname:
253265
torch.jit.save(module, fname)

0 commit comments

Comments
 (0)