forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【Hackathon 6th No.44】add paddle rsqrt (openvinotoolkit#23985)
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
1 parent
90139c2
commit 24137ff
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/frontends/paddle/tests/test_models/gen_scripts/generate_rsqrt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |