From bc9d475b0827e45de2cc812856d7934d6cc772ad Mon Sep 17 00:00:00 2001 From: junnyu <573009727@qq.com> Date: Mon, 8 Nov 2021 17:07:48 +0800 Subject: [PATCH 1/2] add expand --- .../frontend/paddlepaddle/src/op/expand.cpp | 47 ++++++++++++ ngraph/frontend/paddlepaddle/src/op_table.cpp | 2 + .../test/frontend/paddlepaddle/op_fuzzy.cpp | 3 + .../gen_scripts/generate_expand.py | 71 +++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 ngraph/frontend/paddlepaddle/src/op/expand.cpp create mode 100644 ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py diff --git a/ngraph/frontend/paddlepaddle/src/op/expand.cpp b/ngraph/frontend/paddlepaddle/src/op/expand.cpp new file mode 100644 index 00000000000000..18a6c115bca185 --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/op/expand.cpp @@ -0,0 +1,47 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include +#include + +namespace ngraph { +namespace frontend { +namespace pdpd { +namespace op { +NamedOutputs expand(const NodeContext& node) { + auto x = node.get_ng_input("X"); + Output times_expected_node; + if (node.has_ng_input("ExpandTimes")) { + times_expected_node = node.get_ng_input("ExpandTimes"); + } else if (node.has_ng_input("expand_times_tensor")) { + auto inputs = node.get_ng_inputs("expand_times_tensor"); + ngraph::NodeVector node_vec; + for (auto& input : inputs) { + auto cast = std::make_shared(input, element::i32); + node_vec.push_back(cast); + } + times_expected_node = std::make_shared(node_vec, 0); + } else { + std::vector times_expected; + if (node.has_attribute>("expand_times")) { + times_expected = node.get_attribute>("expand_times"); + } else { + throw std::runtime_error("expand: has no expand_times attribute"); + } + times_expected_node = + ngraph::opset6::Constant::create(ngraph::element::i32, {times_expected.size()}, times_expected); + } + + return node.default_single_output_mapping( + {std::make_shared( + x, + std::make_shared(times_expected_node, element::i64))}, + {"Out"}); +} + +} // namespace op +} // namespace pdpd +} // namespace frontend +} // namespace ngraph \ 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 b49ae1cf26cede..2e6e039ba5d05b 100644 --- a/ngraph/frontend/paddlepaddle/src/op_table.cpp +++ b/ngraph/frontend/paddlepaddle/src/op_table.cpp @@ -32,6 +32,7 @@ OP_CONVERTER(elementwise_pow); OP_CONVERTER(elementwise_sub); OP_CONVERTER(embedding); OP_CONVERTER(exp); +OP_CONVERTER(expand); OP_CONVERTER(expand_v2); OP_CONVERTER(fill_any_like); OP_CONVERTER(fill_constant_batch_size_like); @@ -108,6 +109,7 @@ std::map get_supported_ops() { {"elementwise_sub", op::elementwise_sub}, {"equal", op::elementwise_equal}, {"exp", op::exp}, + {"expand", op::expand}, {"expand_v2", op::expand_v2}, {"fill_any_like", op::fill_any_like}, {"fill_constant_batch_size_like", op::fill_constant_batch_size_like}, diff --git a/ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp b/ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp index e81cfb9ccdf5b9..fd477ec024fb5e 100644 --- a/ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp +++ b/ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp @@ -95,6 +95,9 @@ static const std::vector models{std::string("argmax"), std::string("embedding_tensorIds"), std::string("embedding_tensorIds_paddings"), std::string("equal"), + std::string("expand"), + std::string("expand_tensor"), + std::string("expand_tensor_list"), std::string("expand_v2"), std::string("expand_v2_tensor"), std::string("expand_v2_tensor_list"), diff --git a/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py b/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py new file mode 100644 index 00000000000000..9e2af986e1033c --- /dev/null +++ b/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py @@ -0,0 +1,71 @@ +# +# expand paddle model generator +# +import numpy as np +from save_model import saveModel +import paddle as pdpd +import sys + +data_type = 'float32' + + +def expand(name:str, x, repeat_times:list): + 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=data_type) + out = pdpd.tile(node_x, repeat_times=repeat_times, name='expand') + + 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]], target_dir=sys.argv[1]) + + return outs[0] + + +def expand_tensor(name:str, x, repeat_times, use_tensor_in_list): + 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=data_type) + if use_tensor_in_list: + repeat_times[0] = pdpd.assign(np.array((repeat_times[0],)).astype('int32')) + out = pdpd.expand(node_x, shape=repeat_times, name='expand') + else: + repeat_times = np.array(repeat_times).astype('int32') + node_shape = pdpd.assign(repeat_times, output=None) + out = pdpd.expand(node_x, shape=node_shape, name='expand') + + 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]], target_dir=sys.argv[1]) + + return outs[0] + + +def main(): + data = np.random.rand(1, 1, 6).astype(data_type) + + expand("expand", data, [2, 3, 1]) + expand_tensor("expand_tensor", data, [2, 3, 1], False) + expand_tensor("expand_tensor_list", data, [2, 3, 1], True) + + +if __name__ == "__main__": + main() From d087f8edf90703b492e9407ded73f409c399cafa Mon Sep 17 00:00:00 2001 From: junnyu <573009727@qq.com> Date: Mon, 8 Nov 2021 20:20:07 +0800 Subject: [PATCH 2/2] update generator --- .../test_models/gen_scripts/generate_expand.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py b/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py index 9e2af986e1033c..c5f57d34003a36 100644 --- a/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py +++ b/ngraph/test/frontend/paddlepaddle/test_models/gen_scripts/generate_expand.py @@ -4,17 +4,18 @@ import numpy as np from save_model import saveModel import paddle as pdpd +import paddle.fluid as fluid import sys data_type = 'float32' -def expand(name:str, x, repeat_times:list): +def expand(name:str, x, expand_times:list): 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=data_type) - out = pdpd.tile(node_x, repeat_times=repeat_times, name='expand') + out = fluid.layers.expand(node_x, expand_times=expand_times, name='expand') cpu = pdpd.static.cpu_places(1) exe = pdpd.static.Executor(cpu[0]) @@ -31,18 +32,18 @@ def expand(name:str, x, repeat_times:list): return outs[0] -def expand_tensor(name:str, x, repeat_times, use_tensor_in_list): +def expand_tensor(name:str, x, expand_times, use_tensor_in_list): 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=data_type) if use_tensor_in_list: - repeat_times[0] = pdpd.assign(np.array((repeat_times[0],)).astype('int32')) - out = pdpd.expand(node_x, shape=repeat_times, name='expand') + expand_times[0] = pdpd.assign(np.array((expand_times[0],)).astype('int32')) + out = fluid.layers.expand(node_x, expand_times=expand_times, name='expand') else: - repeat_times = np.array(repeat_times).astype('int32') - node_shape = pdpd.assign(repeat_times, output=None) - out = pdpd.expand(node_x, shape=node_shape, name='expand') + expand_times = np.array(expand_times).astype('int32') + node_shape = pdpd.assign(expand_times, output=None) + out = fluid.layers.expand(node_x, expand_times=node_shape, name='expand') cpu = pdpd.static.cpu_places(1) exe = pdpd.static.Executor(cpu[0])