Skip to content

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

[PT FE] Normalize a negative start in aten::narrow before computing the stop (#38259)

Anish Mehta43119faa6914
diff --git a/src/frontends/pytorch/src/op/narrow.cpp b/src/frontends/pytorch/src/op/narrow.cpp
index 783a05a6077..11c14ac9118 100644
--- a/src/frontends/pytorch/src/op/narrow.cpp
+++ b/src/frontends/pytorch/src/op/narrow.cpp
@@ -5,6 +5,9 @@
#include "openvino/frontend/pytorch/node_context.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/convert_like.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/shape_of.hpp"
#include "openvino/op/slice.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "utils.hpp"
@@ -23,10 +26,19 @@ OutputVector translate_narrow(const NodeContext& context) {
auto start_input = context.get_input(2);
auto length = context.get_input(3);
- auto start = context.mark_node(std::make_shared<v0::Unsqueeze>(start_input, const_0));
- auto stop = context.mark_node(std::make_shared<v1::Add>(start, length));
+ Output<Node> start = context.mark_node(std::make_shared<v0::Unsqueeze>(start_input, const_0));
auto axis = context.mark_node(std::make_shared<v0::Unsqueeze>(axis_input, const_0));
+ // A negative start counts from the end of the dimension. Slice would accept it as is, but the stop
+ // must be computed from the non-negative position: otherwise start=-2, length=2 gives stop=0, an
+ // empty slice instead of the last two elements.
+ auto shape = context.mark_node(std::make_shared<v3::ShapeOf>(input_tensor, element::i32));
+ auto scalar_0 = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));
+ Output<Node> dim_size = context.mark_node(std::make_shared<v8::Gather>(shape, axis, scalar_0));
+ dim_size = context.mark_node(std::make_shared<v1::ConvertLike>(dim_size, start));
+ start = normalize_axis(context, start, dim_size);
+ auto stop = context.mark_node(std::make_shared<v1::Add>(start, length));
+
auto narrow = context.mark_node(std::make_shared<v8::Slice>(input_tensor, start, stop, const_1, axis));
return {narrow};
};
diff --git a/tests/layer_tests/pytorch_tests/test_narrow.py b/tests/layer_tests/pytorch_tests/test_narrow.py
index 75f6023079e..32f01c691a5 100644
--- a/tests/layer_tests/pytorch_tests/test_narrow.py
+++ b/tests/layer_tests/pytorch_tests/test_narrow.py
@@ -37,3 +37,19 @@ class TestNarrow(PytorchLayerTest):
def test_narrow(self, input_shape, dim, start, length, ie_device, precision, ir_version):
self.input_shape = input_shape
self._test(*self.create_model(dim, start, length), ie_device, precision, ir_version)
+
+ @pytest.mark.parametrize("input_shape", [
+ [3, 3], [3, 4, 5]
+ ])
+ @pytest.mark.parametrize("dim", [0, 1, -1])
+ @pytest.mark.parametrize("start,length", [
+ # A negative start counts from the end of the dimension. The windows reaching the end of
+ # the axis (start + length == 0) used to come out empty.
+ (-1, 1), (-2, 2), (-2, 1), (-3, 2), (-3, 3),
+ ])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_narrow_negative_start(self, input_shape, dim, start, length, ie_device, precision, ir_version):
+ self.input_shape = input_shape
+ self._test(*self.create_model(dim, start, length), ie_device, precision, ir_version)