forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorShape.cpp
114 lines (90 loc) · 3.49 KB
/
TensorShape.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
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/Config.h>
#include <ATen/InferSize.h>
#include <ATen/WrapDimUtils.h>
#include <ATen/core/Tensor.h>
#include <c10/core/SymIntArrayRef.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_mkldnn_reshape_native.h>
#include <ATen/ops/_mkldnn_transpose_native.h>
#include <ATen/ops/clone_native.h>
#include <ATen/ops/view_native.h>
#endif
#if !AT_MKLDNN_ENABLED()
namespace at {
namespace native {
Tensor mkldnn_view(const Tensor& self, IntArrayRef size) {
TORCH_CHECK(false, "mkldnn_reshape: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_reshape(const Tensor& self, IntArrayRef size) {
TORCH_CHECK(false, "mkldnn_reshape: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_clone(const Tensor& self, c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(false, "mkldnn_clone: ATen not compiled with MKLDNN support");
}
Tensor mkldnn_transpose(const Tensor& self, int64_t dim0, int64_t dim1) {
TORCH_CHECK(false, "mkldnn_transpose: ATen not compiled with MKLDNN support");
}
Tensor& mkldnn_transpose_(Tensor& self, int64_t dim0, int64_t dim1) {
TORCH_CHECK(false, "mkldnn_transpose_: ATen not compiled with MKLDNN support");
}
} // namespace native
} // namespace at
#else // AT_MKLDNN_ENABLED
#include <ATen/native/mkldnn/MKLDNNCommon.h>
namespace at {
namespace native {
Tensor mkldnn_view(const Tensor& self, IntArrayRef size) {
TORCH_CHECK(false,
"Currently Mkldnn tensor does not support view. Change to use reshape instead");
}
Tensor mkldnn_reshape(const Tensor& self, IntArrayRef size) {
auto inferred_size = at::infer_size(size, self.numel());
if (self.sizes() == inferred_size) {
return self;
}
const ideep::tensor& x = itensor_from_mkldnn(self);
ideep::tensor y{x};
y.reshape(inferred_size);
return new_with_itensor_mkldnn(std::move(y), optTypeMetaToScalarType(self.options().dtype_opt()),
self.options().device_opt());
}
Tensor mkldnn_clone(const Tensor& self, c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(
!optional_memory_format.has_value(),
"unsupported memory format option ",
optional_memory_format.value());
ideep::tensor& src = itensor_from_mkldnn(self);
ideep::tensor dst;
ideep::direct_copy::compute(src, dst);
return new_with_itensor_mkldnn(std::move(dst), optTypeMetaToScalarType(self.options().dtype_opt()),
self.options().device_opt());
}
Tensor mkldnn_transpose(const Tensor& self, int64_t dim0, int64_t dim1) {
auto ndims = self.dim();
dim0 = maybe_wrap_dim(dim0, ndims);
dim1 = maybe_wrap_dim(dim1, ndims);
const ideep::tensor& x = itensor_from_mkldnn(self);
ideep::tensor y;
std::vector<int> axes(x.ndims());
std::iota(axes.begin(), axes.end(), 0);
std::swap(axes[dim0], axes[dim1]);
y.transpose_from(x, axes);
return new_with_itensor_mkldnn(std::move(y), optTypeMetaToScalarType(self.options().dtype_opt()),
self.options().device_opt());
}
Tensor& mkldnn_transpose_(Tensor& self, int64_t dim0, int64_t dim1) {
TORCH_CHECK(false, "mkldnn_transpose_: in-place mkldnn operations are not supported yet");
}
} // namespace native
} // namespace at
#endif // AT_MKLDNN_ENABLED
namespace at {
namespace native {
Tensor mkldnn_view_symint(const Tensor& self, c10::SymIntArrayRef size) {
return mkldnn_view(self, C10_AS_INTARRAYREF_SLOW(size));
}
} // namespace native
} // namespace at