Skip to content

Commit 78c65d3

Browse files
committed
[CPU] Fix TopK reference implementation to handle NaN inputs correctly
1 parent 657d60a commit 78c65d3

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/plugins/intel_cpu/src/nodes/topk.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,16 +2511,17 @@ void TopK::topk_ref_process(const float* src_data,
25112511
}
25122512
for (int i2 = 0; i2 < top_k - 1; i2++) {
25132513
for (int i3 = top_k - 1; i3 > i2; i3--) {
2514-
if (compare(max_values[i3], max_values[i3 - 1])) {
2514+
if (std::isnan(max_values[i3 - 1]) || compare(max_values[i3], max_values[i3 - 1])) {
25152515
swap_func(i3, i3 - 1);
25162516
}
25172517
}
25182518
}
2519+
// 2. Main Scan Loop
25192520
for (int i2 = top_k; i2 < dim; i2++) {
25202521
max_values[top_k] = src_data[s_index];
25212522
max_indexes[top_k] = i2;
25222523
for (int i3 = top_k; i3 > 0; i3--) {
2523-
if (compare(max_values[i3], max_values[i3 - 1])) {
2524+
if (std::isnan(max_values[i3 - 1]) || compare(max_values[i3], max_values[i3 - 1])) {
25242525
swap_func(i3, i3 - 1);
25252526
} else {
25262527
break;

0 commit comments

Comments
 (0)