Skip to content

Commit 59eb860

Browse files
authored
force all inputs to be dpcpp tensor (#77)
fix convolution_overrideable cannot be fallbacked to cpu
1 parent e29a7a4 commit 59eb860

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

intel_pytorch_extension_py/ops/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@
55
from .reshape import *
66
from .mlp import *
77
from .linear_fuse_relu import *
8-
from .module import *
98
from .jit_script import *

tests/cpu/test_lazy_reorder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import torch
1414
import intel_pytorch_extension as ipex
1515

16+
from common_ipex_conf import AutoMixPrecision, AutoDNNL
17+
1618
import torch.nn as nn
1719
import torch.backends.cudnn as cudnn
1820
from torch.nn import Parameter
@@ -882,7 +884,7 @@ def test_view(self):
882884
y = torch.randn(new_shape)
883885
out_cpu = x_cpu_view * y
884886
# test if the shape of x_dpcpp_view is compatible with y
885-
out_dpcpp = x_dpcpp_view * y
887+
out_dpcpp = x_dpcpp_view * y.to(device)
886888
self.assertTrue(ipex.is_dil_tensor(out_dpcpp))
887889
self.assertEqual(ipex.get_dil_tensor_sizes(out_dpcpp), [1, 4, 4, 4])
888890
self.assertEqual(ipex.get_dil_tensor_strides(out_dpcpp), [64, 16, 4, 1])

torch_ipex/csrc/cpu/DevOPs.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "dbl/Common.h"
1515
#include "dbl/Conv.h"
1616
#include "dbl/Pool.h"
17+
#include "dbl/DNNLChecker.h"
1718
#include "ShadeDataContext.h"
1819

1920
#include "dil/dil.hpp"
@@ -173,19 +174,31 @@ std::tuple<at::Tensor,at::Tensor,at::Tensor> AtenIpexCPUDev::dil_convolution_bac
173174

174175
at::Tensor AtenIpexCPUDev::dil_convolution_overrideable(const at::Tensor & input, const at::Tensor & weight, const at::Tensor & bias, at::IntArrayRef stride, at::IntArrayRef padding, at::IntArrayRef dilation, bool transposed, at::IntArrayRef output_padding, int64_t groups) {
175176
DEBUG("AtenIpexCPUDev::convolution_overrideable\n");
176-
// NOTE: DO NOT always call contiguous. It may break lazy-reorder. Because contiguous will call reorder instantly.
177-
if (check_auto_dnnl()) {
178-
return dil_convolution(
179-
input.is_contiguous() ? input : input.contiguous(),
180-
weight.is_contiguous() ? weight : weight.contiguous(),
181-
bias.defined() ? (bias.is_contiguous() ? bias :bias.contiguous()) : bias,
182-
stride,
183-
padding,
184-
dilation,
185-
groups);
186-
} else {
187-
return mkldnn_convolution(input, weight, bias, padding, stride, dilation, groups);
177+
178+
try {
179+
if (check_auto_dnnl()) {
180+
std::vector<at::Tensor> dnnl_input_tensors;
181+
dnnl_input_tensors.push_back(input);
182+
dnnl_input_tensors.push_back(weight);
183+
dnnl_input_tensors.push_back(bias);
184+
if (dbl::chk::dnnl_support_the_tensors(dnnl_input_tensors))
185+
return AtenIpexCPUDev::dil_convolution(input.is_contiguous() ? input : input.contiguous(), weight.is_contiguous() ? weight : weight.contiguous(), bias.is_contiguous() ? bias : bias.contiguous(), stride, padding, dilation, groups);
186+
}
187+
} catch (std::exception& e) {
188+
#if defined(_DEBUG)
189+
TORCH_WARN(e.what());
190+
#endif
188191
}
192+
193+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(input.layout() == c10::kStrided);
194+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(weight.layout() == c10::kStrided);
195+
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(bias.layout() == c10::kStrided);
196+
auto&& _ipex_input = bridge::shallowFallbackToCPUTensor(input);
197+
auto&& _ipex_weight = bridge::shallowFallbackToCPUTensor(weight);
198+
auto&& _ipex_bias = bridge::shallowFallbackToCPUTensor(bias);
199+
auto&& _ipex_result = at::mkldnn_convolution(_ipex_input, _ipex_weight, _ipex_bias, padding, stride, dilation, groups);
200+
static_cast<void>(_ipex_result); // Avoid warnings in case not used
201+
return bridge::shallowUpgradeToDPCPPTensor(_ipex_result);
189202
}
190203

191204
at::Tensor AtenIpexCPUDev::mkldnn_convolution(const at::Tensor & self, const at::Tensor & weight, const at::Tensor & bias, at::IntArrayRef padding, at::IntArrayRef stride, at::IntArrayRef dilation, int64_t groups) {

torch_ipex/csrc/cpu/dbl/DNNLChecker.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace dbl {
99
namespace chk {
1010

1111
bool dnnl_support_the_tensors(const std::vector<at::Tensor> &tensor_vec) {
12-
return dnnl_tensor_has_data(tensor_vec) &&
12+
return all_is_dpcpp(tensor_vec) &&
13+
dnnl_tensor_has_data(tensor_vec) &&
1314
dnnl_support_the_dimension_of(tensor_vec) &&
1415
dnnl_support_the_data_type_of(tensor_vec);
1516
}
@@ -62,6 +63,14 @@ bool dnnl_tensor_has_data(const std::vector<at::Tensor> &tensor_vec) {
6263
return true;
6364
}
6465

66+
bool all_is_dpcpp(const std::vector<at::Tensor> &tensor_vec) {
67+
for (auto it = tensor_vec.begin(); it != tensor_vec.end(); ++it)
68+
if (!it->device().is_dpcpp())
69+
return false;
70+
71+
return true;
72+
}
73+
6574
} // namespace chk
6675
} // namespace dbl
6776
} // namespace cpu

torch_ipex/csrc/cpu/dbl/DNNLChecker.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,21 @@ bool dnnl_support_the_data_type_of(const std::vector<at::Tensor> &tensor_vec);
6262
bool dnnl_support_the_dimension_of(const std::vector<at::Tensor> &tensor_vec);
6363

6464
/**
65-
* Check if the input tensor has data
65+
* Check if all input tensors has data
6666
*
6767
* @param tensor_vec input tensors
6868
*
6969
*/
7070
static inline bool dnnl_tensor_has_data(const std::vector<at::Tensor> &tensor_vec);
7171

72+
/**
73+
* Check if all input tensors are dpcpp tensor
74+
*
75+
* @param tensor_vec input tensors
76+
*
77+
*/
78+
bool all_is_dpcpp(const std::vector<at::Tensor> &tensor_vec);
79+
7280
} // namespace chk
7381
} // namespace dbl
7482
} // namespace cpu

0 commit comments

Comments
 (0)