﻿# ONNX Runtime CPU TopK: NaN ordering is an explicit policy, not ordinary comparison

CPU TopK in the 1.32 notes uses explicit NaN ordering; validate selected indices, ties and algorithm branches rather than only finite values.

Author: Retrivium team
Updated: 2026-11-15
Canonical URL: https://retrivium.ai/journal/week46-engine-ort-cpu-topk-nan-ordering-policy/
Language: en

ONNX Runtime 1.32.0 release notes report CPU TopK ordering NaN above ordinary values. The linked analysis explains why plain greater-than, less-than and equality comparisons do not provide the ordering algorithms need when NaN is present. This makes nonfinite inputs part of backend-consistency testing rather than a case covered by finite random arrays.

## NaN breaks a familiar comparison shortcut

Ordinary floating comparisons do not place NaN on either side of a finite number. A sorting or selection implementation needs a consistent policy beyond those raw operators. The release reports one such policy and the PR relates it to existing CUDA behaviour. Do not describe that implementation choice as a new universal mathematical definition of NaN ordering; it is the behaviour callers should verify for this runtime and provider combination.

## Indices reveal more than values

Two NaNs can look identical in a printed value list while referring to different input positions. Ties among finite values have the same issue. A proposed regression checks indices as well as values, with several NaNs at distinct positions and repeated finite values. Include largest and smallest selection modes. A comparator policy is only fully observable when the test verifies which original elements were chosen and in what order.

## Different K values can exercise different algorithms

TopK implementations often specialize small and larger selections. The PR specifically discusses several paths, so one K value is incomplete coverage. Use a one-element selection, an intermediate K and a near-full selection with the same nonfinite pattern. Compare CPU with the documented reference policy before comparing providers. A finite-only throughput test can remain valuable, but it cannot establish correctness of the NaN-aware branches.

## A stable selection does not repair upstream NaNs

Ordering nonfinite values consistently makes selection predictable; it does not make the model’s earlier arithmetic healthy. Preserve diagnostics about where NaNs originated if they are unexpected. This article reports the release policy and proposed regression structure without claiming to have run the PR’s tests or measured a new speedup. Applications that reject nonfinite inputs should still test that rejection separately from the operator’s defined handling.

## Sources

- [ONNX Runtime v1.32.0 release](https://github.com/microsoft/onnxruntime/releases/tag/v1.32.0)
- [ONNX Runtime pull request #33543](https://github.com/microsoft/onnxruntime/pull/33543)

## Related reading

- [ONNX Runtime provider order: graph partition priority is not request failover](/journal/fg2-onnxruntime-provider-priority-subgraph-partition/)
- [llama.cpp b12202 and the difference between a precision request and an accumulator](/journal/news-llamacpp-b12202-precision-request-vs-accumulator/)
