diff --git a/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md b/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md index b29a40e57c3d53..b2f5ab9f167ecf 100644 --- a/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md +++ b/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md @@ -744,6 +744,7 @@ paddlepaddle >= 2.1 fill_constant fill_constant_batch_size_like flatten_contiguous_range + flip floor gather gather_nd diff --git a/src/frontends/paddle/src/op/flip.cpp b/src/frontends/paddle/src/op/flip.cpp new file mode 100644 index 00000000000000..804edcc7e015d7 --- /dev/null +++ b/src/frontends/paddle/src/op/flip.cpp @@ -0,0 +1,17 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "reverse_op.hpp" + +namespace ov { +namespace frontend { +namespace paddle { +namespace op { +NamedOutputs flip(const NodeContext& node) { + return reverse_op(node); +} +} // namespace op +} // namespace paddle +} // namespace frontend +} // namespace ov diff --git a/src/frontends/paddle/src/op/reverse.cpp b/src/frontends/paddle/src/op/reverse.cpp index b5e87f4c5b9fca..097e13b401986b 100644 --- a/src/frontends/paddle/src/op/reverse.cpp +++ b/src/frontends/paddle/src/op/reverse.cpp @@ -2,32 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "default_opset.hpp" -#include "openvino/frontend/paddle/node_context.hpp" -#include "openvino/opsets/opset1.hpp" +#include "reverse_op.hpp" namespace ov { namespace frontend { namespace paddle { namespace op { - -using namespace default_opset; - NamedOutputs reverse(const NodeContext& node) { - auto x = node.get_input("X"); - // axis is a vector - auto axis = node.get_attribute>("axis"); - // try to keep the axis positive since reverse IR doesn't support negative axis. - const auto dims = static_cast(x.get_partial_shape().rank().get_length()); - std::for_each(axis.begin(), axis.end(), [&dims](int32_t& value) { - if (value < 0) { - value += dims; - } - }); - - auto axis_node = std::make_shared(ngraph::element::i32, Shape{axis.size()}, axis); - auto reverse_op = std::make_shared(x, axis_node, ov::opset1::Reverse::Mode::INDEX); - return node.default_single_output_mapping({reverse_op}, {"Out"}); + return reverse_op(node); } } // namespace op } // namespace paddle diff --git a/src/frontends/paddle/src/op/reverse_op.hpp b/src/frontends/paddle/src/op/reverse_op.hpp new file mode 100644 index 00000000000000..25b7dba26f8580 --- /dev/null +++ b/src/frontends/paddle/src/op/reverse_op.hpp @@ -0,0 +1,34 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "default_opset.hpp" +#include "openvino/frontend/paddle/node_context.hpp" +#include "openvino/opsets/opset1.hpp" + +namespace ov { +namespace frontend { +namespace paddle { +namespace op { +namespace { +NamedOutputs reverse_op(const NodeContext& node) { + auto x = node.get_input("X"); + // axis is a vector + auto axis = node.get_attribute>("axis"); + // try to keep the axis positive since reverse IR doesn't support negative axis. + const auto dims = static_cast(x.get_partial_shape().rank().get_length()); + std::for_each(axis.begin(), axis.end(), [&dims](int32_t& value) { + if (value < 0) { + value += dims; + } + }); + + auto axis_node = std::make_shared(ngraph::element::i32, Shape{axis.size()}, axis); + auto reverse_node = std::make_shared(x, axis_node, ov::opset1::Reverse::Mode::INDEX); + return node.default_single_output_mapping({reverse_node}, {"Out"}); +} +} +} // namespace op +} // namespace paddle +} // namespace frontend +} // namespace ov diff --git a/src/frontends/paddle/src/op/top_k_v2.cpp b/src/frontends/paddle/src/op/top_k_v2.cpp index ba9bfff9b1513f..8f51920f05d1a2 100644 --- a/src/frontends/paddle/src/op/top_k_v2.cpp +++ b/src/frontends/paddle/src/op/top_k_v2.cpp @@ -27,7 +27,7 @@ NamedOutputs top_k_v2(const NodeContext& node) { std::string sort_type = sorted ? "value" : "none"; std::string mode = largest ? "max" : "min"; - auto node_topk = std::make_shared(x, k_expected_node, axis, mode, sort_type); + auto node_topk = std::make_shared(x, k_expected_node, axis, mode, sort_type, element::i64); NamedOutputs named_outputs; named_outputs["Out"] = OutputVector{node_topk->output(0)}; diff --git a/src/frontends/paddle/src/op_table.cpp b/src/frontends/paddle/src/op_table.cpp index 73eb7a1adfa58b..a65c23d97a11df 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -46,6 +46,7 @@ OP_CONVERTER(fill_any_like); OP_CONVERTER(fill_constant_batch_size_like); OP_CONVERTER(fill_constant); OP_CONVERTER(flatten_contiguous_range); +OP_CONVERTER(flip); OP_CONVERTER(floor); OP_CONVERTER(gather); OP_CONVERTER(gather_nd); @@ -160,6 +161,7 @@ std::map get_supported_ops() { {"fill_any_like", op::fill_any_like}, {"fill_constant_batch_size_like", op::fill_constant_batch_size_like}, {"fill_constant", op::fill_constant}, + {"flip", op::flip}, {"flatten_contiguous_range", op::flatten_contiguous_range}, {"floor", op::floor}, {"gather", op::gather}, diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp index 882116f80cb03e..165bf8b5de2f52 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -203,6 +203,14 @@ static const std::vector models{ std::string("fill_constant_shape_tensor"), std::string("fill_constant_shape_tensor_list"), std::string("flatten_contiguous_range_test1"), + std::string("flip_static_1"), + std::string("flip_static_2"), + std::string("flip_static_3"), + std::string("flip_static_4"), + std::string("flip_dynamic_1"), + std::string("flip_dynamic_2"), + std::string("flip_dynamic_3"), + std::string("flip_dynamic_4"), std::string("floor_float32"), std::string("floor_mod1"), std::string("floor_mod2"), diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_flip.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_flip.py new file mode 100644 index 00000000000000..13a1199b40967a --- /dev/null +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_flip.py @@ -0,0 +1,61 @@ +# Copyright (C) 2018-2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +# +# flip paddle model generator +# +import numpy as np +from save_model import saveModel +import sys +import paddle + +def flip(name: str, x, axis, use_static=True, dtype="float32"): + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): + if use_static: + node_x = paddle.static.data(name='x', shape=x.shape, dtype=dtype) + else: + node_x = paddle.fluid.data(name='x', shape=[1, 1, -1, -1], dtype=dtype) + out = paddle.flip(node_x, axis) + + 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()) + + outs = exe.run( + feed={'x': x}, + fetch_list=[out]) + + saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1]) + + return outs[0] + +def main(): + data1 = np.array([0,1,2], dtype='int32') + flip("flip_static_1", data1, 0, True, 'int32') + + data2 = np.array([[0,1,2], [3,4,5], [6,7,8]], dtype='float32') + flip("flip_static_2", data2, 1, True, 'float32') + + data3 = np.array([[0,1,2], [3,4,5], [6,7,8]], dtype='float32') + flip("flip_static_3", data3, [0, 1], True, 'float32') + + data4 = np.array([[0,1,2], [3,4,5], [6,7,8]], dtype='int32') + flip("flip_static_4", data4, -1, True, 'int32') + + data5 = np.random.randn(1, 1, 32, 32).astype('int32') + flip("flip_dynamic_1", data5, [2], False, dtype='int32') + + data6 = np.random.randn(1, 1, 64, 64).astype('float32') + flip("flip_dynamic_2", data6, [3], False, dtype='float32') + + data7 = np.random.randn(1, 1, 112, 112).astype('float32') + flip("flip_dynamic_3", data7, [2,3], False, dtype='float32') + + data8 = np.random.randn(1, 1, 224, 224).astype('int32') + flip("flip_dynamic_4", data8, [-2, -1], False, dtype='int32') + +if __name__ == "__main__": + main() diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_reverse.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_reverse.py index e993057df8389d..7f1b2d8d98777d 100644 --- a/src/frontends/paddle/tests/test_models/gen_scripts/generate_reverse.py +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_reverse.py @@ -33,8 +33,8 @@ def reverse(name: str, x, axis, use_static=True, dtype="float32"): return outs[0] def main(): - data1 = np.array([0,2], dtype='int64') - reverse("reverse_static_1", data1, 0, True, 'int64') + data1 = np.array([0,2], dtype='int32') + reverse("reverse_static_1", data1, 0, True, 'int32') data2 = np.array([[0,1,2], [3,4,5], [6,7,8]], dtype='float32') reverse("reverse_static_2", data2, 1, True, 'float32')