forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from BaiYM0117/new_API
Clip op implemented in new API
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
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,34 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2021 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#include <ngraph/opsets/opset6.hpp> | ||
#include "clip.hpp" | ||
#include <paddlepaddle_frontend/utility.hpp> | ||
|
||
namespace ngraph { | ||
namespace frontend { | ||
namespace pdpd { | ||
namespace op { | ||
|
||
NamedOutputs clip (const NodeContext& node) { | ||
auto data = node.get_ng_input("X"); | ||
auto min = node.get_attribute<float>("min"); | ||
auto max = node.get_attribute<float>("max"); | ||
PDPD_ASSERT(max >= min, "clip: max value must greater than min value!"); | ||
return node.default_single_output_mapping({std::make_shared<ngraph::opset6::Clamp>(data, min, max)}, {"Out"}); | ||
} | ||
|
||
}}}} |
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,27 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2021 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#pragma once | ||
#include "node_context.hpp" | ||
|
||
namespace ngraph { | ||
namespace frontend { | ||
namespace pdpd { | ||
namespace op { | ||
|
||
NamedOutputs clip (const NodeContext& node); | ||
|
||
}}}} |
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
39 changes: 39 additions & 0 deletions
39
ngraph/test/files/paddlepaddle/gen_scripts/generate_clip.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,39 @@ | ||
# | ||
# clip paddle model generator | ||
# | ||
import numpy as np | ||
from save_model import saveModel | ||
|
||
|
||
def clip(name: str, x, min, max): | ||
import paddle as pdpd | ||
pdpd.enable_static() | ||
|
||
with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()): | ||
node_x = pdpd.static.data(name='x', shape=x.shape, dtype='float32') | ||
out = pdpd.fluid.layers.clip(node_x, min=min, max=max) | ||
|
||
cpu = pdpd.static.cpu_places(1) | ||
exe = pdpd.static.Executor(cpu[0]) | ||
# startup program will call initializer to initialize the parameters. | ||
exe.run(pdpd.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]]) | ||
|
||
return outs[0] | ||
|
||
|
||
def main(): | ||
data = np.random.random([2, 3, 4]).astype('float32') | ||
min = 0 | ||
max = 0.8 | ||
|
||
clip("clip", data, min, max) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |