Skip to content

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

[Paddle FE] enble paddle.roll (#26249)

Ethan Yang72a1fb982dc3
diff --git a/src/frontends/paddle/src/op/roll.cpp b/src/frontends/paddle/src/op/roll.cpp
new file mode 100644
index 00000000000..d35378ad212
--- /dev/null
+++ b/src/frontends/paddle/src/op/roll.cpp
@@ -0,0 +1,43 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "default_opset.hpp"
+#include "openvino/frontend/paddle/node_context.hpp"
+
+namespace ov {
+namespace frontend {
+namespace paddle {
+namespace op {
+NamedOutputs roll(const NodeContext& node) {
+ auto input_node = node.get_input("X");
+ Output<Node> shifts_node;
+ if (node.has_input("ShiftsTensor")) {
+ auto shifts = node.get_input("ShiftsTensor");
+ auto shifts_var_node = std::make_shared<default_opset::Convert>(shifts, element::i64);
+ shifts_node = std::make_shared<default_opset::Squeeze>(shifts_var_node);
+ } else {
+ const auto shifts = node.get_attribute<std::vector<int64_t>>("shifts");
+ shifts_node = default_opset::Constant::create(element::i64, {shifts.size()}, shifts);
+ }
+
+ std::vector<int64_t> axis = node.get_attribute<std::vector<int64_t>>("axis");
+ if (axis.empty()) {
+ const auto const_minus_1 = default_opset::Constant::create(element::i64, Shape{1}, {-1});
+ Output<Node> axis_node = default_opset::Constant::create(element::i64, Shape{1}, {0});
+ const auto flat = std::make_shared<default_opset::Reshape>(input_node, const_minus_1, false);
+ const auto roll = std::make_shared<default_opset::Roll>(flat, shifts_node, axis_node);
+ const auto shape_of_data = std::make_shared<default_opset::ShapeOf>(input_node, element::i64);
+ const auto reshape = std::make_shared<default_opset::Reshape>(roll, shape_of_data, false);
+ return node.default_single_output_mapping(reshape, {"Out"});
+ } else {
+ Output<Node> axis_node = default_opset::Constant::create(element::i64, {axis.size()}, axis);
+ return node.default_single_output_mapping(
+ std::make_shared<default_opset::Roll>(input_node, shifts_node, axis_node),
+ {"Out"});
+ }
+}
+} // namespace op
+} // namespace paddle
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/paddle/src/op_table.cpp b/src/frontends/paddle/src/op_table.cpp
index 7eec375b13e..769492eb13d 100644
--- a/src/frontends/paddle/src/op_table.cpp
+++ b/src/frontends/paddle/src/op_table.cpp
@@ -98,6 +98,7 @@ OP_CONVERTER(reshape2);
OP_CONVERTER(reverse);
OP_CONVERTER(rnn);
OP_CONVERTER(roi_align);
+OP_CONVERTER(roll);
OP_CONVERTER(round);
OP_CONVERTER(rsqrt);
OP_CONVERTER(scale);
@@ -238,6 +239,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"reverse", op::reverse},
{"rnn", op::rnn},
{"roi_align", op::roi_align},
+ {"roll", op::roll},
{"round", op::round},
{"rsqrt", op::rsqrt},
{"scale", op::scale},
diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp
index 69bac920e2d..99357a3a336 100644
--- a/src/frontends/paddle/tests/op_fuzzy.cpp
+++ b/src/frontends/paddle/tests/op_fuzzy.cpp
@@ -513,6 +513,11 @@ static const std::vector<std::string> models{
std::string("rnn_lstm_layer_2_bidirectional_seq_len_4/rnn_lstm_layer_2_bidirectional_seq_len_4.pdmodel"),
std::string("roi_align_test/roi_align_test.pdmodel"),
std::string("roi_align_test2/roi_align_test2.pdmodel"),
+ std::string("roll_test_0/roll_test_0.pdmodel"),
+ std::string("roll_test_1/roll_test_1.pdmodel"),
+ std::string("roll_test_2/roll_test_2.pdmodel"),
+ std::string("roll_test_3/roll_test_3.pdmodel"),
+ std::string("roll_test_4/roll_test_4.pdmodel"),
std::string("round/round.pdmodel"),
std::string("scale_bias_after_float32/scale_bias_after_float32.pdmodel"),
std::string("scale_bias_after_int32/scale_bias_after_int32.pdmodel"),
diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_roll.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_roll.py
new file mode 100644
index 00000000000..6c53d809116
--- /dev/null
+++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_roll.py
@@ -0,0 +1,54 @@
+# Copyright (C) 2018-2023 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+#
+# round paddle model generator
+#
+import numpy as np
+from save_model import saveModel
+import paddle
+import sys
+
+data_type = 'float32'
+
+
+def paddle_roll(name: str, x, shifts, axis=None, shifts_is_var=False):
+ paddle.enable_static()
+
+ shifts = np.array([shifts], dtype='int64') if shifts_is_var else shifts
+
+ with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
+ x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype)
+ shifts_node = paddle.static.data(name="shifts", shape=[1], dtype='int64') if shifts_is_var else shifts
+ out = paddle.roll(x_node, shifts_node, axis) if axis is not None else paddle.roll(
+ x_node, shifts)
+
+ cpu = paddle.static.cpu_places(1)
+ exe = paddle.static.Executor(cpu[0])
+ # startup program will call initializer to initialize the parameters.
+ exe.run(paddle.static.default_startup_program())
+
+ feed_list = {'x': x, 'shifts': shifts} if shifts_is_var else {'x': x}
+ outs = exe.run(
+ feed=feed_list,
+ fetch_list=[out])
+
+ feed_vars = [x_node, shifts_node] if shifts_is_var else [x_node]
+ input_list = [x, shifts] if shifts_is_var else [x]
+ saveModel(name, exe, feed_vars=feed_vars, fetchlist=[out], inputs=input_list, outputs=[outs[0]], target_dir=sys.argv[1])
+
+ return outs[0]
+
+
+def main():
+ x = np.random.randn(2, 3, 4).astype(data_type)
+
+ paddle_roll("roll_test_0", x, shifts=[1])
+ paddle_roll("roll_test_1", x, shifts=[1], axis=[0])
+ paddle_roll("roll_test_2", x, shifts=1, axis=0)
+ paddle_roll("roll_test_3", x, shifts=[0, 1], axis=[0, 1])
+ paddle_roll("roll_test_4", x, shifts=1, axis=0, shifts_is_var=True)
+
+
+if __name__ == "__main__":
+ main()