Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Add stable parameter for topk (#33199)

liangfcf8bdef634d
diff --git a/src/frontends/onnx/frontend/src/op/topk.cpp b/src/frontends/onnx/frontend/src/op/topk.cpp
index 22e942254ac..bf8328b1c24 100644
--- a/src/frontends/onnx/frontend/src/op/topk.cpp
+++ b/src/frontends/onnx/frontend/src/op/topk.cpp
@@ -38,7 +38,8 @@ ov::OutputVector topk(const ov::frontend::onnx::Node& node) {
axis,
v11::TopK::Mode::MAX,
v11::TopK::SortType::SORT_VALUES,
- ov::element::i64);
+ ov::element::i64,
+ true);
return {top_k->output(0), top_k->output(1)};
}
@@ -56,7 +57,8 @@ ov::OutputVector topk(const ov::frontend::onnx::Node& node) {
axis,
v11::TopK::Mode::MAX,
v11::TopK::SortType::SORT_VALUES,
- ov::element::i64);
+ ov::element::i64,
+ true);
return {top_k->output(0), top_k->output(1)};
}
@@ -76,11 +78,13 @@ ov::OutputVector topk(const ov::frontend::onnx::Node& node) {
// Map attribute values to OpenVINO enums
const auto sort_type = sorted ? v11::TopK::SortType::SORT_VALUES : v11::TopK::SortType::NONE;
+ const auto stable_sort = (sort_type != v11::TopK::SortType::NONE);
const auto compute_max = static_cast<bool>(largest);
const auto mode = compute_max ? v11::TopK::Mode::MAX : v11::TopK::Mode::MIN;
- std::shared_ptr<ov::Node> top_k = std::make_shared<v11::TopK>(data, k, axis, mode, sort_type, ov::element::i64);
+ std::shared_ptr<ov::Node> top_k =
+ std::make_shared<v11::TopK>(data, k, axis, mode, sort_type, ov::element::i64, stable_sort);
return {top_k->output(0), top_k->output(1)};
}
diff --git a/src/frontends/onnx/tests/models/top_3_all_same.prototxt b/src/frontends/onnx/tests/models/top_3_all_same.prototxt
new file mode 100644
index 00000000000..f003a21e6d2
--- /dev/null
+++ b/src/frontends/onnx/tests/models/top_3_all_same.prototxt
@@ -0,0 +1,85 @@
+ir_version: 12
+producer_name: ""
+producer_version: ""
+model_version: 0
+graph {
+ name: "test"
+ node {
+ input: "X"
+ input: "K"
+ output: "Values"
+ output: "Indices"
+ name: "node1"
+ op_type: "TopK"
+ attribute {
+ name: "axis"
+ i: -1
+ type: INT
+ }
+ doc_string: "TopK"
+ }
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "K"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 1
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Values"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 10
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index 822c76e6537..f758b11aeca 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -2907,6 +2907,18 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_top_k_repeating_unsorted) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_model_top_3_all_same) {
+ auto model = convert_model("top_3_all_same.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(Shape{2, 4}, {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f});
+ test_case.add_input<int64_t>({3});
+
+ test_case.add_expected_output<float>(Shape{2, 3}, {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f});
+ test_case.add_expected_output<int64_t>(Shape{2, 3}, {0, 1, 2, 0, 1, 2});
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_acosh) {
auto model = convert_model("acosh.onnx");