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

fix variable support issue for paddle top_k_v2 #65

Merged
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
1 change: 1 addition & 0 deletions src/core/tests/frontend/paddle/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ static const std::vector<std::string> models{
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
Expand Up @@ -9,27 +9,32 @@
data_type = 'float32'


def top_k_v2(name: str, x, k: int, axis=None, largest=True, sorted=True):
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=k, axis=axis, largest=largest, sorted=sorted, name="top_k")
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={'x': x},
feed=feed_list,
fetch_list=[value, indices])

saveModel(name, exe, feedkeys=['x'], fetchlist=[value, indices], inputs=[
x], outputs=outs, target_dir=sys.argv[1])
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]

Expand All @@ -43,6 +48,7 @@ def main():
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__":
Expand Down
3 changes: 2 additions & 1 deletion src/frontends/paddle/src/op/top_k_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ NamedOutputs top_k_v2(const NodeContext& node) {
Output<Node> k_expected_node;
if (node.has_input("K")) {
auto k_variable = node.get_input("K");
k_expected_node = std::make_shared<default_opset::Convert>(k_variable, element::i32);
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});
Expand Down