Skip to content

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

[PT FE] Support aten::repeat_interleave when repeats are tensor (#29380)

Maxim Vafin19841085dccd
diff --git a/src/frontends/pytorch/src/op/repeat_interleave.cpp b/src/frontends/pytorch/src/op/repeat_interleave.cpp
index b7bcb58ea03..8f8281fa274 100644
--- a/src/frontends/pytorch/src/op/repeat_interleave.cpp
+++ b/src/frontends/pytorch/src/op/repeat_interleave.cpp
@@ -3,17 +3,22 @@
//
#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/gather.hpp"
+#include "openvino/op/less.hpp"
+#include "openvino/op/non_zero.hpp"
#include "openvino/op/range.hpp"
+#include "openvino/op/reduce_max.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/squeeze.hpp"
#include "openvino/op/tile.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/unsqueeze.hpp"
-#include "pt_framework_node.hpp"
+#include "utils.hpp"
namespace ov {
namespace frontend {
@@ -23,15 +28,14 @@ namespace op {
using namespace ov::op;
namespace {
-OutputVector generate_indices_from_repeats_tensor(const NodeContext& context, const std::vector<int32_t>& repeats) {
- OutputVector all_indices;
- for (size_t i = 0; i < repeats.size(); i++) {
- Shape indices_shape{static_cast<size_t>(repeats.at(i))};
- std::vector<int32_t> indices_vec(repeats.at(i), static_cast<int32_t>(i));
- auto indices = context.mark_node(v0::Constant::create(element::i32, indices_shape, indices_vec));
- all_indices.push_back(indices);
+Output<Node> generate_indices_from_repeats_tensor(const NodeContext& context, const std::vector<int32_t>& repeats) {
+ std::vector<int32_t> result;
+ for (size_t i = 0; i < repeats.size(); ++i) {
+ for (int32_t j = 0; j < repeats[i]; ++j) {
+ result.push_back(static_cast<int32_t>(i));
+ }
}
- return all_indices;
+ return context.mark_node(v0::Constant::create(element::i32, Shape{result.size()}, result));
};
} // namespace
@@ -45,43 +49,64 @@ OutputVector translate_repeat_interleave(const NodeContext& context) {
// inputs
auto input = context.get_input(0);
+ auto repeats = context.get_input(1);
std::shared_ptr<ov::Node> result;
- auto repeats_ext_node = context.get_input_from_visible_context(1).get_node_shared_ptr();
- auto repeats_fw_node = ov::as_type_ptr<v0::Constant>(repeats_ext_node);
- if (repeats_fw_node && repeats_fw_node->cast_vector<int32_t>().size() > 1) {
+ auto repeats_ext = context.get_input_from_visible_context(1);
+ auto repeats_const = ov::as_type_ptr<v0::Constant>(repeats_ext.get_node_shared_ptr());
+ if (repeats_const && repeats_const->cast_vector<int32_t>().size() > 1) {
// repeats is Constant with more then 1 element
- auto repeats = repeats_fw_node->cast_vector<int32_t>();
+ auto repeats = repeats_const->cast_vector<int32_t>();
if (context.input_is_none(2)) {
// case (repeats=tensor, dim=None)
auto flat_shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
auto reshape = context.mark_node(std::make_shared<v1::Reshape>(input, flat_shape, false));
- OutputVector all_indices = generate_indices_from_repeats_tensor(context, repeats);
- auto concat = context.mark_node(std::make_shared<v0::Concat>(all_indices, 0));
- result = std::make_shared<v8::Gather>(reshape, concat, const_0);
+ auto all_indices = generate_indices_from_repeats_tensor(context, repeats);
+ result = std::make_shared<v8::Gather>(reshape, all_indices, const_0);
} else {
// case (repeats=tensor, dim=number)
auto dimension = context.get_input(2);
- OutputVector all_indices = generate_indices_from_repeats_tensor(context, repeats);
- auto concat = context.mark_node(std::make_shared<v0::Concat>(all_indices, 0));
- result = std::make_shared<v8::Gather>(input, concat, dimension);
+ auto all_indices = generate_indices_from_repeats_tensor(context, repeats);
+ result = std::make_shared<v8::Gather>(input, all_indices, dimension);
}
} else {
- // repeats is not Constant or single element constant
- // Currently we support only case when repeats contains only one element. Otherwise next Reshape will fail.
- auto repeats_input = context.mark_node(std::make_shared<v0::Convert>(context.get_input(1), element::i32));
- repeats_input = context.mark_node(std::make_shared<v1::Reshape>(repeats_input, const_1_list, false));
- auto repeats = context.mark_node(std::make_shared<v0::Concat>(OutputVector{repeats_input, const_1_list}, 0));
- auto shape_perm = context.mark_node(v0::Constant::create(element::i32, Shape{2}, {1, 0}));
+ repeats = context.mark_node(std::make_shared<v0::Convert>(repeats, element::i32));
+ repeats = context.mark_node(std::make_shared<v1::Reshape>(repeats, const_neg_1, false));
if (context.input_is_none(2)) {
// case (repeats=number, dim=None)
- auto flat_shape = context.mark_node(v0::Constant::create(element::i32, Shape{2}, {1, -1}));
- auto reshape = context.mark_node(std::make_shared<v1::Reshape>(input, flat_shape, false));
- auto tile = context.mark_node(std::make_shared<v0::Tile>(reshape, repeats));
- auto transpose = context.mark_node(std::make_shared<v1::Transpose>(tile, shape_perm));
- result = std::make_shared<v1::Reshape>(transpose, const_neg_1, false);
+ // Prepare the input and determine the maximum repeat value
+ input = context.mark_node(std::make_shared<v1::Reshape>(input, const_neg_1, false));
+ auto new_input = context.mark_node(std::make_shared<v0::Unsqueeze>(input, const_1));
+ auto input_shape = context.mark_node(std::make_shared<v3::ShapeOf>(input, element::i32));
+ auto repeats_bc = context.mark_node(std::make_shared<v3::Broadcast>(repeats, input_shape));
+ auto repeat_max = context.mark_node(std::make_shared<v1::ReduceMax>(repeats_bc, const_0, true));
+
+ // Tile the input based on the maximum repeat value
+ auto max_repeat_for_tile =
+ context.mark_node(std::make_shared<v0::Concat>(OutputVector{const_1_list, repeat_max}, 0));
+ auto tile = context.mark_node(std::make_shared<v0::Tile>(new_input, max_repeat_for_tile));
+ auto tile_flat = context.mark_node(std::make_shared<v1::Reshape>(tile, const_neg_1, false));
+
+ // Generate a range and compare to determine valid indices
+ auto stop = context.mark_node(std::make_shared<v15::Squeeze>(repeat_max, const_0));
+ auto range = context.mark_node(std::make_shared<v4::Range>(const_0, stop, const_1, element::i32));
+ auto repeats_1 = context.mark_node(std::make_shared<v0::Unsqueeze>(repeats_bc, const_neg_1));
+ auto less = context.mark_node(std::make_shared<v1::Less>(range, repeats_1));
+ auto less_flat = context.mark_node(std::make_shared<v1::Reshape>(less, const_neg_1, false));
+
+ // Identify non-zero indices from the comparison result
+ auto non_zero = context.mark_node(std::make_shared<v3::NonZero>(less_flat));
+ auto indices = context.mark_node(std::make_shared<v15::Squeeze>(non_zero, const_0));
+ // Gather the final result using valid indices
+ result = std::make_shared<v8::Gather>(tile_flat, indices, const_0);
} else {
// case (repeats=number, dim=number)
+ // this only supports repeats as a single element tensor
+ FRONT_END_CHECK_IMPLEMENTED(
+ repeats.get_partial_shape().is_dynamic() || shape_size(repeats.get_partial_shape().to_shape()) == 1,
+ "Repeats must be a single element tensor");
+ repeats = context.mark_node(std::make_shared<v0::Concat>(OutputVector{repeats, const_1_list}, 0));
+ auto shape_perm = context.mark_node(v0::Constant::create(element::i32, Shape{2}, {1, 0}));
auto dimension = context.get_input(2);
auto input_shape = context.mark_node(std::make_shared<v3::ShapeOf>(input, element::i32));
auto input_dim_size = context.mark_node(std::make_shared<v8::Gather>(input_shape, dimension, const_0));
diff --git a/tests/layer_tests/pytorch_tests/test_repeat_interleave.py b/tests/layer_tests/pytorch_tests/test_repeat_interleave.py
index 75dbb583d3c..f4ba2f8a602 100644
--- a/tests/layer_tests/pytorch_tests/test_repeat_interleave.py
+++ b/tests/layer_tests/pytorch_tests/test_repeat_interleave.py
@@ -75,4 +75,31 @@ class TestRepeatInterleaveNonConstRepeats(PytorchLayerTest):
self.repeats = input_data['repeats']
dim = input_data['dim']
self._test(*self.create_model_non_const_repeat(dim),
- ie_device, precision, ir_version, dynamic_shapes=False, use_mo_convert=False)
+ ie_device, precision, ir_version, dynamic_shapes=False)
+
+
+class TestRepeatInterleaveTensorRepeats(PytorchLayerTest):
+ def _prepare_input(self):
+ return (np.random.randn(10), self.repeats)
+
+ def create_model_non_const_repeat(self, dim):
+ class aten_repeat_interleave_non_const_repeat(torch.nn.Module):
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.dim = dim
+
+ def forward(self, input_tensor, repeats):
+ return input_tensor.repeat_interleave(repeats, self.dim)
+
+ return aten_repeat_interleave_non_const_repeat(), None, "aten::repeat_interleave"
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.parametrize('input_data', ({'repeats': np.array([2, 3, 4, 1, 2, 4, 6, 7, 4, 3]).astype(np.int32), 'dim': None},
+ {'repeats': np.array([3, 7, 1, 9, 4, 6, 2, 8, 5, 0]).astype(np.int32), 'dim': None}))
+ def test_repeat_interleave_tensor_repeats(self, ie_device, precision, ir_version, input_data):
+ self.repeats = input_data['repeats']
+ dim = input_data['dim']
+ self._test(*self.create_model_non_const_repeat(dim),
+ ie_device, precision, ir_version, dynamic_shapes=False)