Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PaddlePaddle hackathon] + convert paddle "expand" ops #8446

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/expand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <ngraph/opsets/opset6.hpp>
#include <node_context.hpp>
#include <paddlepaddle_frontend/utility.hpp>

namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {
NamedOutputs expand(const NodeContext& node) {
auto x = node.get_ng_input("X");
Output<Node> 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<ngraph::opset6::Convert>(input, element::i32);
node_vec.push_back(cast);
}
times_expected_node = std::make_shared<ngraph::opset6::Concat>(node_vec, 0);
} else {
std::vector<int32_t> times_expected;
if (node.has_attribute<std::vector<int32_t>>("expand_times")) {
times_expected = node.get_attribute<std::vector<int32_t>>("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<ngraph::opset6::Tile>(
x,
std::make_shared<ngraph::opset6::Convert>(times_expected_node, element::i64))},
{"Out"});
}

} // namespace op
} // namespace pdpd
} // namespace frontend
} // namespace ngraph
2 changes: 2 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -108,6 +109,7 @@ std::map<std::string, CreatorFunction> 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},
Expand Down
3 changes: 3 additions & 0 deletions ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ static const std::vector<std::string> 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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# expand paddle model generator
#
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, 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 = fluid.layers.expand(node_x, expand_times=expand_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, 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:
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:
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])
# 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()