Skip to content

Commit

Permalink
【Hackathon 6th No.44】add paddle rsqrt (openvinotoolkit#23985)
Browse files Browse the repository at this point in the history
Add `paddle.rsqrt` op for openvino

paddle.rsqrt(x, name=None)

The rsqrt activation function.

The calculation formula is as follows:

$$
out=\frac{1}{\sqrt{x}}
$$


![image](https://github.com/openvinotoolkit/openvino/assets/55493212/b197c068-96f8-47f4-8d48-913ee93d01a9)

Waiting for:
- openvinotoolkit#23010

---------

Co-authored-by: mei, yang <[email protected]>
  • Loading branch information
AndSonder and meiyang-intel authored May 21, 2024
1 parent 90139c2 commit 24137ff
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/frontends/paddle/src/op/rsqrt.cpp
Original file line number Diff line number Diff line change
@@ -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<default_opset::Power>(
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
2 changes: 2 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -237,6 +238,7 @@ std::map<std::string, CreatorFunction> 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},
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 24137ff

Please sign in to comment.