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] 转换paddle.fluid.layers.topk #17

Closed
wants to merge 1 commit 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
42 changes: 42 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/top_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

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

namespace ngraph {
namespace frontend {
namespace pdpd {
namespace op {
NamedOutputs top_k(const NodeContext& node) {
auto x = node.get_ng_input("X");
Output<Node> k_expected_node;
if (node.has_ng_input("K")) {
auto k_variable = node.get_ng_input("K");
k_expected_node = std::make_shared<ngraph::opset6::Convert>(k_variable, element::i32);
}else {
int32_t k_expected;
if (node.has_attribute<int32_t>("k")) {
k_expected = node.get_attribute<int32_t>("k");
} else {
throw std::runtime_error("top_k: has no k attribute");
}
k_expected_node = ngraph::opset6::Constant::create(ngraph::element::i32, {}, {k_expected});
}
int64_t axis = -1;
const element::Type& index_element_type = element::i64;
auto node_topk = std::make_shared<ngraph::opset6::TopK>(x, k_expected_node, axis, "max", "value", index_element_type);

NamedOutputs named_outputs;
named_outputs["Out"] = {node_topk->output(0)};
named_outputs["Indices"] = {node_topk->output(1)};

return named_outputs;

}

} // 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 @@ -68,6 +68,7 @@ OP_CONVERTER(split);
OP_CONVERTER(squeeze);
OP_CONVERTER(stack);
OP_CONVERTER(tanh);
OP_CONVERTER(top_k);
OP_CONVERTER(transpose2);
OP_CONVERTER(trilinear_interp_v2);
OP_CONVERTER(unsqueeze);
Expand Down Expand Up @@ -149,6 +150,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"stack", op::stack},
{"sync_batch_norm", op::batch_norm},
{"tanh", op::tanh},
{"top_k", op::top_k},
{"transpose2", op::transpose2},
{"trilinear_interp_v2", op::trilinear_interp_v2},
{"unsqueeze2", op::unsqueeze},
Expand Down
1 change: 1 addition & 0 deletions ngraph/test/frontend/paddlepaddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ static const std::vector<std::string> models{std::string("argmax"),
std::string("stack_test_neg_axis"),
std::string("stack_test_none_axis"),
std::string("tanh"),
std::string("top_k"),
std::string("trilinear_downsample_false_0"),
std::string("trilinear_downsample_false_1"),
std::string("trilinear_downsample_true_0"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# top_k 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 top_k(name : str, x, k:int):
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')
val, ind = fluid.layers.topk(node_x, k=k, name="top_k")

ind = pdpd.cast(ind, "float32")
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=[val, ind])

saveModel(name, exe, feedkeys=['x'], fetchlist=[val, ind], inputs=[x], outputs=outs, target_dir=sys.argv[1])

return outs[0]

def main():
data = np.random.random([1,3,5]).astype("float32")

top_k("top_k", data, k=2)


if __name__ == "__main__":
main()