Skip to content

Commit 268a6df

Browse files
fix the issue when call contiguous() if one tensor's format is channels_last, but it is not contiguous (#33)
* fix the issue when call contiguous() if one tensor's format is channels_last, but it is not contiguous
1 parent 31f3014 commit 268a6df

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

torch_ipex/csrc/cpu/Conv.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ at::Tensor convolution_impl(
3434
const ideep::attr_t& attr) {
3535
// TODO: the input will be actively converted to channels last format
3636
// after the 5-D tensor supports channels last format.
37-
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous();
37+
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous(input.suggest_memory_format());
3838
const ideep::tensor mkldnn_input = at::native::itensor_view_from_dense(input_);
3939
ideep::tensor mkldnn_weight = get_conv_prepacked_weight(mkldnn_input, weight, stride, padding, dilation, groups, attr);
4040
auto kernel_size = mkldnn_weight.get_dims();
@@ -105,7 +105,7 @@ void convolution_inplace_impl(
105105
const ideep::attr_t& attr) {
106106
// TODO: the input will be actively converted to channels last format
107107
// after the 5-D tensor supports channels last format.
108-
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous();
108+
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous(input.suggest_memory_format());
109109
const ideep::tensor mkldnn_input = at::native::itensor_view_from_dense(input_);
110110
ideep::tensor mkldnn_weight = get_conv_prepacked_weight(mkldnn_input, weight, stride, padding, dilation, groups, attr);
111111
auto kernel_size = mkldnn_weight.get_dims();
@@ -114,7 +114,7 @@ void convolution_inplace_impl(
114114
calc_conv_output_size(input_size, kernel_size, padding, stride, dilation);
115115

116116
bool is_channels_last = input_.suggest_memory_format() == at::MemoryFormat::ChannelsLast;
117-
output = IS_CONTIGUOUS_ANY(output) ? output : output.contiguous();
117+
output = IS_CONTIGUOUS_ANY(output) ? output : output.contiguous(output.suggest_memory_format());
118118
output = output.to(input_.suggest_memory_format());
119119
ideep::tensor mkldnn_output = at::native::itensor_view_from_dense(output);
120120

torch_ipex/csrc/cpu/Pooling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ at::Tensor pooling_impl(
7373

7474
// TODO: the input will be actively converted to channels last format
7575
// after the 5-D tensor supports channels last format.
76-
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous();
76+
auto input_ = IS_CONTIGUOUS_ANY(input) ? input : input.contiguous(input.suggest_memory_format());
7777
const ideep::tensor mkldnn_input = at::native::itensor_view_from_dense(input_);
7878
std::vector<int64_t> output_sizes;
7979

torch_ipex/csrc/cpu/WeightPrepack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ideep::tensor get_conv_prepacked_weight(
2525
if (it != cached_weights.end()) {
2626
return std::get<1>(it->second);
2727
} else {
28-
auto weight_ = IS_CONTIGUOUS_ANY(weight) ? weight : weight.contiguous();
28+
auto weight_ = IS_CONTIGUOUS_ANY(weight) ? weight : weight.contiguous(weight.suggest_memory_format());
2929
ideep::tensor w = at::native::itensor_view_from_dense(weight_);
3030
// TODO: 3d check
3131
bool is_channels_last = input.get_desc().is_nhwc();

torch_ipex/csrc/cpu/WeightPrepack.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace torch_ipex {
1010
namespace cpu {
1111

1212
// Get the convolution's expected ideep weight tensor, may be a block weight.
13-
// if the expected weight doesn't not exist, it will create an expected weight according
13+
// if the expected weight doesn't exist, it will create an expected weight according
1414
// to the queried desc of OneDNN conv, and the expected weight will be cached.
15-
// TODO: if weight is a block weight, direct return the weight(ideep tensor).
15+
// TODO: if weight is already in the expected format, return the weight(ideep tensor) directly.
1616

1717
// input: an ideep tensor, getting from the convolution's input,
1818
// weight: convolution's weight
@@ -28,9 +28,9 @@ ideep::tensor get_conv_prepacked_weight(
2828
const ideep::attr_t& attr);
2929

3030
// Get the linear's expected ideep weight tensor, may be a block weight.
31-
// if the expected weight doesn't not exist, it will create an expected weight according
31+
// if the expected weight doesn't exist, it will create an expected weight according
3232
// to the queried desc of OneDNN linear, and the expected weight will be cached.
33-
// TODO: if weight is a block weight, direct return the weight(ideep tensor).
33+
// TODO: if weight is already in the expected format, return the weight(ideep tensor) directly.
3434

3535
// input: an ideep tensor, getting from the linear's input,
3636
// weight: linear's weight

0 commit comments

Comments
 (0)