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

topk update #25

Closed
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
45 changes: 45 additions & 0 deletions ngraph/frontend/paddlepaddle/src/op/top_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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;
NamedOutputs named_outputs;
if (node.has_ng_input("K")) {
auto k_variable = node.get_ng_input("K");
k = std::make_shared<ngraph::opset6::Convert>(k_variable, ngraph::element::i32);
}else {
int32_t k_expected;
if (node.has_attribute<int32_t>("k")) {
k_expected = node.get_attribute<int32_t>("k");
} else {
// k default value is 1
k_expected = 1;
}
k = ngraph::opset6::Constant::create(ngraph::element::i32, {}, {k_expected});
}
// last axis
int32_t axis = -1;
const element::Type& index_element_type = element::i64;
auto node_topk_results = std::make_shared<ngraph::opset6::TopK>(x, k, axis, "max", "value", index_element_type);

named_outputs["Out"] = {node_topk_results->output(0)};
named_outputs["Indices"] = {node_topk_results->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
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 @@ -221,6 +221,9 @@ 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_5"),
std::string("top_k_2"),
std::string("top_k_4"),
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,42 @@
#
# top_k paddle model generator
#
import numpy as np
import paddle as pdpd
from save_model import saveModel
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=data_type)
value, indices = pdpd.fluid.layers.topk(node_x, k, name="top_k")
indices = pdpd.cast(indices, data_type)

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=[value, indices])

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

return outs[0]

def main():
data = np.random.random([5, 2, 10]).astype(data_type)
# top_k k=5
top_k("top_k_5", data, k=5)
# top_k k=2
top_k("top_k_2", data, k=2)
# top_k k=4
top_k("top_k_4", data, k=4)


if __name__ == "__main__":
main()