diff --git a/ngraph/frontend/paddlepaddle/src/op/clip.cpp b/ngraph/frontend/paddlepaddle/src/op/clip.cpp new file mode 100644 index 00000000000000..18d035b3e799ce --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/clip.cpp @@ -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 +#include "clip.hpp" +#include + +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("min"); + auto max = node.get_attribute("max"); + PDPD_ASSERT(max >= min, "clip: max value must greater than min value!"); + return node.default_single_output_mapping({std::make_shared(data, min, max)}, {"Out"}); +} + +}}}} \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op/clip.hpp b/ngraph/frontend/paddlepaddle/src/op/clip.hpp new file mode 100644 index 00000000000000..667591ca0b09f0 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/clip.hpp @@ -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); + +}}}} \ No newline at end of file diff --git a/ngraph/frontend/paddlepaddle/src/op_table.cpp b/ngraph/frontend/paddlepaddle/src/op_table.cpp index 68dc6f822fbf39..2df2937f790ce4 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -46,10 +46,10 @@ #include "op/unsqueeze.hpp" #include "op/slice.hpp" #include "op/hard_swish.hpp" +#include "op/clip.hpp" #include "op/greater_equal.hpp" #include "op/log.hpp" #include "op/fill_constant_batch_size_like.hpp" - #include "op_table.hpp" @@ -101,6 +101,7 @@ std::map get_supported_ops() { {"unsqueeze2", op::unsqueeze}, {"slice", op::slice}, {"hard_swish", op::hard_swish}, + {"clip", op::clip}, {"greater_equal", op::greater_equal}, {"log", op::log}, {"fill_constant_batch_size_like", op::fill_constant_batch_size_like} diff --git a/ngraph/test/files/paddlepaddle/gen_scripts/generate_clip.py b/ngraph/test/files/paddlepaddle/gen_scripts/generate_clip.py new file mode 100644 index 00000000000000..e07a7c5ba49cd5 --- /dev/null +++ b/ngraph/test/files/paddlepaddle/gen_scripts/generate_clip.py @@ -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() \ No newline at end of file