Skip to content

Commit 57f932a

Browse files
PenghuiChengfacebook-github-bot
authored andcommitted
Enable 'empty' function for mkldnn
Summary: Pull Request resolved: #21184 Differential Revision: D15625296 Pulled By: bddppq fbshipit-source-id: 47d26798bcf48e227ffd813f299959a7b8993641
1 parent b869a3b commit 57f932a

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

aten/src/ATen/native/mkldnn/MKLDNNCommon.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ Tensor new_with_itensor_mkldnn(ideep::tensor&& it, const TensorOptions& options)
5050
std::vector<int64_t>(dims.begin(), dims.end()));
5151
}
5252

53-
Tensor new_with_sizes_mkldnn(IntArrayRef sizes, const TensorOptions& options) {
54-
// NOTE: int32_t dims from ideep::tensor but sizes needs int64_t
55-
// TODO: support int64_t dims in ideep::tensor to avoid extra conversion
56-
ideep::tensor::dims dst_dims (sizes.begin(), sizes.end());
57-
ideep::tensor it;
58-
it.resize<AllocForMKLDNN>(dst_dims, ideep::tensor::data_type::f32);
59-
return new_with_itensor_mkldnn(std::move(it), options);
60-
}
61-
6253
ideep::tensor& itensor_from_mkldnn(const MKLDNNTensor& mkldnn_tensor) {
6354
AT_ASSERTM(mkldnn_tensor.is_mkldnn(),
6455
"mkldnn_to_dense expects MKL-DNN tensor input");

aten/src/ATen/native/mkldnn/MKLDNNCommon.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ struct AllocForMKLDNN {
2626
// Construct aten MKL-DNN tensor given an ideep tensor
2727
Tensor new_with_itensor_mkldnn(ideep::tensor&& it, const TensorOptions& options);
2828

29-
// Construct aten MKL-DNN tensor given `sizes` for allocation
30-
Tensor new_with_sizes_mkldnn(IntArrayRef sizes, const TensorOptions& options);
31-
3229
// Retrieve `ideep::tensor` from MKL-DNN tensor
3330
ideep::tensor& itensor_from_mkldnn(const Tensor& mkldnn_tensor);
3431

aten/src/ATen/native/mkldnn/MKLDNNConversions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ Tensor dense_to_mkldnn(const Tensor& cpu_tensor) {
2424
"dense_to_mkldnn expects dense CPU tensor input");
2525
AT_ASSERTM(cpu_tensor.scalar_type() == ScalarType::Float,
2626
"dense_to_mkldnn expects float tensor input");
27+
AT_ASSERTM(cpu_tensor.dim() <= 5,
28+
"Can't convert cpu tensor with the number of dimensions > 5");
2729
// TODO: consider to convert non-contiguous tensor to `ideep::tensor` directly.
2830
auto cpu_tensor_cont = cpu_tensor.contiguous();
29-
Tensor mkldnn_tensor = new_with_sizes_mkldnn(cpu_tensor_cont.sizes(), cpu_tensor_cont.options());
31+
Tensor mkldnn_tensor = empty_mkldnn(cpu_tensor_cont.sizes(), cpu_tensor_cont.options());
3032
ideep::tensor& dtensor = itensor_from_mkldnn(mkldnn_tensor);
3133
dtensor.reorder_from(dtensor.get_dims(),
3234
ideep::tensor::data_type::f32,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <ATen/native/mkldnn/MKLDNNCommon.h>
2+
3+
namespace at { namespace native {
4+
5+
#if AT_MKLDNN_ENABLED()
6+
7+
Tensor empty_mkldnn(IntArrayRef sizes, const TensorOptions& options) {
8+
// NOTE: int32_t dims from ideep::tensor but sizes needs int64_t
9+
// TODO: support int64_t dims in ideep::tensor to avoid extra conversion
10+
ideep::tensor::dims dst_dims (sizes.begin(), sizes.end());
11+
ideep::tensor it;
12+
it.resize<AllocForMKLDNN>(dst_dims, ideep::tensor::data_type::f32);
13+
return new_with_itensor_mkldnn(std::move(it), options);
14+
}
15+
16+
#else
17+
18+
Tensor empty_mkldnn(IntArrayRef sizes, const TensorOptions& options) {
19+
AT_ERROR("empty_mkldnn: MKL-DNN build is disabled");
20+
}
21+
22+
#endif // AT_MKLDNN_ENABLED()
23+
24+
}}

aten/src/ATen/native/native_functions.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@
688688
dispatch:
689689
CPU: empty_cpu
690690
CUDA: empty_cuda
691+
MkldnnCPU: empty_mkldnn
691692
SparseCPU: empty_sparse
692693
SparseCUDA: empty_sparse
693694

test/test_mkldnn.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_unsupported(self):
4545
with self.assertRaises(RuntimeError) as context:
4646
torch.randn(1, 2, 3, 4, dtype=torch.float, device=torch.device('cuda')).to_mkldnn()
4747
# some factory functions
48-
for creator in [torch.empty, torch.ones, torch.zeros, torch.randn, torch.rand]:
48+
for creator in [torch.ones, torch.zeros, torch.randn, torch.rand]:
4949
with self.assertRaises(RuntimeError) as context:
5050
creator(1, 2, 3, 4, dtype=torch.float, device=torch.device('cpu'), layout=torch._mkldnn)
5151

@@ -289,6 +289,11 @@ def test_set_data_tensorimpl_type(self):
289289
with self.assertRaisesRegex(RuntimeError, 'different types of TensorImpl'):
290290
x.data = x_mkldnn
291291

292+
def test_empty(self):
293+
x1 = torch.empty(4, 5, 2, 3, dtype=torch.float32)
294+
x2 = torch.empty(4, 5, 2, 3, dtype=torch.float32, layout=torch._mkldnn)
295+
self.assertEqual(x1.size(), x2.to_dense().size())
296+
self.assertEqual(x1.dtype, x2.to_dense().dtype)
292297

293298
if __name__ == '__main__':
294299
run_tests()

0 commit comments

Comments
 (0)