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

Top k v2 #11731

Merged
merged 13 commits into from
Jun 15, 2022
Merged

Top k v2 #11731

Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions src/core/tests/frontend/paddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ static const std::vector<std::string> models{
std::string("swish_default_params"),
std::string("swish_beta"),
std::string("tanh"),
std::string("top_k_v2_test_1"),
std::string("top_k_v2_test_2"),
std::string("top_k_v2_test_3"),
std::string("top_k_v2_test_4"),
std::string("top_k_v2_test_5"),
std::string("top_k_v2_test_6"),
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,55 @@
#
# top_k_v2 paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle
import sys

data_type = 'float32'


def top_k_v2(name: str, x, k: int, axis=None, largest=True, sorted=True, k_is_var=True):

paddle.enable_static()

k = np.array([k], dtype='int32') if k_is_var else k

with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
node_x = paddle.static.data(name='x', shape=x.shape, dtype='float32')
input_k = paddle.static.data(name='k', shape=[1], dtype='int32') if k_is_var else k
value, indices = paddle.topk(
node_x, k=input_k, axis=axis, largest=largest, sorted=sorted, name="top_k")
indices = paddle.cast(indices, np.float32)

cpu = paddle.static.cpu_places(1)
exe = paddle.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(paddle.static.default_startup_program())

feed_list = {'x': x, 'k': k} if k_is_var else {'x': x}
outs = exe.run(
feed=feed_list,
fetch_list=[value, indices])

feedkey_list = ['x', 'k'] if k_is_var else ['x']
input_list = [x, k] if k_is_var else [x]
saveModel(name, exe, feedkeys=feedkey_list, fetchlist=[value, indices], inputs=input_list, outputs=outs, target_dir=sys.argv[1])

return outs[0]


def main():
data = np.random.random([8, 9, 10]).astype("float32")
# sorted must be true
top_k_v2("top_k_v2_test_1", data, k=5, axis=-2, largest=True, sorted=True)
top_k_v2("top_k_v2_test_2", data, k=6, axis=-1, largest=True, sorted=True)
top_k_v2("top_k_v2_test_3", data, k=4, axis=0, largest=False, sorted=True)
top_k_v2("top_k_v2_test_4", data, k=7,
axis=None, largest=True, sorted=True)
top_k_v2("top_k_v2_test_5", data, k=6, axis=2, largest=False, sorted=True)
top_k_v2("top_k_v2_test_6", data, k=6, axis=2, largest=False, sorted=True, k_is_var=False)


if __name__ == "__main__":
main()
43 changes: 43 additions & 0 deletions src/frontends/paddle/src/op/top_k_v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "default_opset.hpp"
#include "openvino/frontend/paddle/node_context.hpp"

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
NamedOutputs top_k_v2(const NodeContext& node) {
auto x = node.get_input("X");
Output<Node> k_expected_node;
if (node.has_input("K")) {
auto k_variable = node.get_input("K");
auto k_var_node = std::make_shared<default_opset::Convert>(k_variable, element::i32);
k_expected_node = std::make_shared<default_opset::Squeeze>(k_var_node);
} else {
const auto k_expected = node.get_attribute<int>("k", 1);
k_expected_node = default_opset::Constant::create(element::i32, {}, {k_expected});
}

auto axis = node.get_attribute<int32_t>("axis");
bool sorted = node.get_attribute<bool>("sorted");
bool largest = node.get_attribute<bool>("largest");
const element::Type& index_element_type = element::i32;

std::string sort_type = sorted ? "value" : "none";
std::string mode = largest ? "max" : "min";

auto node_topk =
std::make_shared<default_opset::TopK>(x, k_expected_node, axis, mode, sort_type, index_element_type);

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

return named_outputs;
}
} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov
2 changes: 2 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ OP_CONVERTER(stack);
OP_CONVERTER(strided_slice);
OP_CONVERTER(swish);
OP_CONVERTER(tanh);
OP_CONVERTER(top_k_v2);
OP_CONVERTER(transpose2);
OP_CONVERTER(trilinear_interp_v2);
OP_CONVERTER(unsqueeze);
Expand Down Expand Up @@ -182,6 +183,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"swish", op::swish},
{"sync_batch_norm", op::batch_norm},
{"tanh", op::tanh},
{"top_k_v2", op::top_k_v2},
{"transpose2", op::transpose2},
{"trilinear_interp_v2", op::trilinear_interp_v2},
{"unsqueeze2", op::unsqueeze},
Expand Down