Skip to content

Commit 55a4934

Browse files
author
Lagmator22
committed
Revert "[CPU] Fix TopK reference implementation to handle NaN inputs correctly"
This reverts commit 1d1007d.
1 parent f921135 commit 55a4934

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/sort/top-k-1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ If there are several elements with the same value then their output order is not
7878

7979
**NaN Handling**
8080

81-
For floating-point types, NaN values are treated as smaller than any valid number (consistent with NumPy behavior). In ``max`` mode, NaN values will not appear in top-K results unless fewer than K valid numbers exist. Note that this differs from PyTorch, which treats NaN as larger than all valid numbers.
81+
For floating-point types, the ordering of NaN values in the output is implementation-defined. Per IEEE 754, NaN fails all ordered comparisons, so there is no single correct placement for NaN in a sorted result. Different frameworks (e.g., NumPy, PyTorch) handle NaN differently, and the TopK implementation does not guarantee a specific NaN ordering. If deterministic behavior is required, NaN values should be sanitized before the TopK operation (e.g., replaced with ``-inf`` or ``+inf`` depending on the desired placement).
8282

8383
**Example**
8484

docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/sort/top-k-11.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The relative order of equivalent elements is only preserved if the ``stable`` at
9797

9898
**NaN Handling**
9999

100-
For floating-point types, NaN values are treated as smaller than any valid number (consistent with NumPy behavior). In ``max`` mode, NaN values will not appear in top-K results unless fewer than K valid numbers exist. Note that this differs from PyTorch, which treats NaN as larger than all valid numbers.
100+
For floating-point types, the ordering of NaN values in the output is implementation-defined. Per IEEE 754, NaN fails all ordered comparisons, so there is no single correct placement for NaN in a sorted result. Different frameworks (e.g., NumPy, PyTorch) handle NaN differently, and the TopK implementation does not guarantee a specific NaN ordering. If deterministic behavior is required, NaN values should be sanitized before the TopK operation (e.g., replaced with ``-inf`` or ``+inf`` depending on the desired placement).
101101

102102
The "by index" order means that the input tensor's elements are still sorted by value but their order in the output tensor is additionally determined by the indices of those elements in the input tensor. This might yield multiple correct results though. For example if the input tensor contains the following elements:
103103

docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/sort/top-k-3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ If there are several elements with the same value then their output order is not
8585

8686
**NaN Handling**
8787

88-
For floating-point types, NaN values are treated as smaller than any valid number (consistent with NumPy behavior). In ``max`` mode, NaN values will not appear in top-K results unless fewer than K valid numbers exist. Note that this differs from PyTorch, which treats NaN as larger than all valid numbers.
88+
For floating-point types, the ordering of NaN values in the output is implementation-defined. Per IEEE 754, NaN fails all ordered comparisons, so there is no single correct placement for NaN in a sorted result. Different frameworks (e.g., NumPy, PyTorch) handle NaN differently, and the TopK implementation does not guarantee a specific NaN ordering. If deterministic behavior is required, NaN values should be sanitized before the TopK operation (e.g., replaced with ``-inf`` or ``+inf`` depending on the desired placement).
8989

9090
**Example**
9191

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,7 +2511,7 @@ 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 (std::isnan(max_values[i3 - 1]) || compare(max_values[i3], max_values[i3 - 1])) {
2514+
if (compare(max_values[i3], max_values[i3 - 1])) {
25152515
swap_func(i3, i3 - 1);
25162516
}
25172517
}
@@ -2520,7 +2520,7 @@ void TopK::topk_ref_process(const float* src_data,
25202520
max_values[top_k] = src_data[s_index];
25212521
max_indexes[top_k] = i2;
25222522
for (int i3 = top_k; i3 > 0; i3--) {
2523-
if (std::isnan(max_values[i3 - 1]) || compare(max_values[i3], max_values[i3 - 1])) {
2523+
if (compare(max_values[i3], max_values[i3 - 1])) {
25242524
swap_func(i3, i3 - 1);
25252525
} else {
25262526
break;

0 commit comments

Comments
 (0)