diff --git a/src/frontends/paddle/src/op/rsqrt.cpp b/src/frontends/paddle/src/op/rsqrt.cpp new file mode 100644 index 00000000000000..ab494059a14aba --- /dev/null +++ b/src/frontends/paddle/src/op/rsqrt.cpp @@ -0,0 +1,26 @@ +// 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 rsqrt(const NodeContext& node) { + auto data = node.get_input("X"); + + auto rsqrt = std::make_shared( + data, + default_opset::Constant::create(data.get_element_type(), Shape{}, {-0.5})); + + return node.default_single_output_mapping({rsqrt}, {"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 6c5f2c24819121..9c371bdd281da9 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -99,6 +99,7 @@ OP_CONVERTER(reverse); OP_CONVERTER(rnn); OP_CONVERTER(roi_align); OP_CONVERTER(round); +OP_CONVERTER(rsqrt); OP_CONVERTER(scale); OP_CONVERTER(select_input); OP_CONVERTER(set_value); @@ -237,6 +238,7 @@ std::map get_supported_ops() { {"rnn", op::rnn}, {"roi_align", op::roi_align}, {"round", op::round}, + {"rsqrt", op::rsqrt}, {"scale", op::scale}, {"select_input", op::select_input}, {"set_value", op::set_value}, diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_rsqrt.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_rsqrt.py new file mode 100644 index 00000000000000..888574c6176aee --- /dev/null +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_rsqrt.py @@ -0,0 +1,45 @@ +import sys +import paddle +import numpy as np +from save_model import saveModel + + +def rsqrt(name: str, x): + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): + node_x = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) + out = paddle.rsqrt(node_x) + + cpu = paddle.static.cpu_places(1) + exe = paddle.static.Executor(cpu[0]) + # Initialize the variables with the provided inputs + exe.run(paddle.static.default_startup_program()) + outs = exe.run(feed={"x": x}, fetch_list=[out]) + + saveModel( + name, + exe, + feed_vars=[node_x], + fetchlist=[out], + inputs=[x], + outputs=[outs[0]], + target_dir=sys.argv[1], + ) + + +def main(): + # Test case 1: float32, 1D array + x1 = np.array([0.25, 1, 4, 16], dtype=np.float32) + rsqrt("rsqrt_float32_1D", x1) + + # Test case 2: float32, 2D array + x2 = np.array([[0.25, 1], [4, 16]], dtype=np.float32) + rsqrt("rsqrt_float32_2D", x2) + + x3 = np.random.rand(2, 3, 4, 5).astype(np.float32) + rsqrt("rsqrt_float32_4D", x3) + + +if __name__ == "__main__": + main()