Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PT FE] Support aten.nonzero and short strides for convolution (#31684)

Maxim Vafin16159d353e8b
diff --git a/src/frontends/pytorch/src/op/convolution.cpp b/src/frontends/pytorch/src/op/convolution.cpp
index b0a7ff4b5ba..f6a4dc4c059 100644
--- a/src/frontends/pytorch/src/op/convolution.cpp
+++ b/src/frontends/pytorch/src/op/convolution.cpp
@@ -16,31 +16,53 @@ namespace op {
using namespace ov::op;
+namespace {
+template <typename T>
+std::vector<T> resizeVector(ov::PartialShape kernel_shape, const std::vector<T>& inputVector) {
+ PYTORCH_OP_CONVERSION_CHECK(!inputVector.empty(), "Input vector must contain at least one element.");
+ if (kernel_shape.rank().is_dynamic()) {
+ // can't resize, use original
+ return inputVector;
+ }
+ PYTORCH_OP_CONVERSION_CHECK(kernel_shape.rank().get_length() > 2, "Rank must be at least 3.");
+ size_t newSize = kernel_shape.rank().get_length() - 2;
+
+ std::vector<T> result;
+ if (inputVector.size() == newSize) {
+ result = inputVector;
+ } else if (inputVector.size() == 1) {
+ result.resize(newSize, inputVector[0]);
+ } else {
+ PYTORCH_OP_CONVERSION_CHECK(false, "Input vector must be of the specified size or contain only one element.");
+ }
+
+ return result;
+}
+} // namespace
+
OutputVector translate_convolution(const NodeContext& context) {
// Schema: aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[]
// dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool
// cudnn_enabled, bool allow_tf32) -> Tensor
num_inputs_check(context, 9, 13);
- auto strides = context.const_input<Strides>(3);
- auto pads = context.const_input<CoordinateDiff>(4);
- auto dilations = context.const_input<Strides>(5);
+ auto kernel = context.get_input(1);
+ auto kernel_shape = kernel.get_partial_shape();
+ auto strides = resizeVector(kernel_shape, context.const_input<Strides>(3));
+ auto pads = resizeVector(kernel_shape, context.const_input<CoordinateDiff>(4));
+ auto dilations = resizeVector(kernel_shape, context.const_input<Strides>(5));
bool transposed = context.const_input<bool>(6);
- auto output_padding = context.const_input<CoordinateDiff>(7);
+ auto output_padding = resizeVector(kernel_shape, context.const_input<CoordinateDiff>(7));
auto groups = context.const_input<int64_t>(8);
std::shared_ptr<ov::Node> conv;
if (groups == 1) {
if (!transposed) {
- conv = context.mark_node(std::make_shared<v1::Convolution>(context.get_input(0),
- context.get_input(1),
- strides,
- pads,
- pads,
- dilations));
+ conv = context.mark_node(
+ std::make_shared<v1::Convolution>(context.get_input(0), kernel, strides, pads, pads, dilations));
} else {
conv = context.mark_node(std::make_shared<v1::ConvolutionBackpropData>(context.get_input(0),
- context.get_input(1),
+ kernel,
strides,
pads,
pads,
@@ -52,7 +74,7 @@ OutputVector translate_convolution(const NodeContext& context) {
if (!transposed) {
conv = context.mark_node(
std::make_shared<v1::GroupConvolution>(context.get_input(0),
- reshape_kernel_for_group(context, context.get_input(1), groups),
+ reshape_kernel_for_group(context, kernel, groups),
strides,
pads,
pads,
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 1694e7a0645..2ad462b2d18 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -1003,6 +1003,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
{"aten.new_full.default", op::translate_new_full_fx},
{"aten.new_ones.default", op::translate_new_ones_fx},
{"aten.new_zeros.default", op::translate_new_zeros_fx},
+ {"aten.nonzero.default", op::translate_nonzero},
{"aten.ones.default", op::translate_ones_fx},
{"aten.ones.names", op::translate_ones_fx},
{"aten.ones_like.default", op::translate_ones_like_fx},
diff --git a/tests/layer_tests/pytorch_tests/test_convnd.py b/tests/layer_tests/pytorch_tests/test_convnd.py
index 27b9a97d559..22c36533026 100644
--- a/tests/layer_tests/pytorch_tests/test_convnd.py
+++ b/tests/layer_tests/pytorch_tests/test_convnd.py
@@ -53,6 +53,7 @@ class TestConv2D(PytorchLayerTest):
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
def test_conv2d(self, params, bias, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias),
ie_device, precision, ir_version)
@@ -98,6 +99,7 @@ class TestConv1D(PytorchLayerTest):
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
def test_conv1d(self, params, bias, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias),
ie_device, precision, ir_version)
@@ -157,6 +159,7 @@ class TestConv3D(PytorchLayerTest):
@pytest.mark.parametrize("bias", [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
def test_conv3d(self, params, bias, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias),
ie_device, precision, ir_version)
diff --git a/tests/layer_tests/pytorch_tests/test_nonzero.py b/tests/layer_tests/pytorch_tests/test_nonzero.py
index 30e853883fb..b413f35e02b 100644
--- a/tests/layer_tests/pytorch_tests/test_nonzero.py
+++ b/tests/layer_tests/pytorch_tests/test_nonzero.py
@@ -43,6 +43,7 @@ class TestNonZero(PytorchLayerTest):
@pytest.mark.parametrize("as_tuple", [False, True])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
def test_nonzero(self, mask_fill, mask_dtype, as_tuple, ie_device, precision, ir_version):
self._test(*self.create_model(as_tuple),
ie_device, precision, ir_version,