Summary#
부동소수점 특수값이나 희귀 입력이 끼어들면 평소엔 멀쩡해 보이던 비교 함수가 정렬 라이브러리의 전제조건을 깨뜨릴 수 있다. 비교 로직을 구현할 때는 정상 케이스 성능보다 strict weak ordering 같은 불변식을 먼저 만족시키고, NaN·동률·타이브레이커를 명시적으로 처리하는 접근이 안전하다.
Problem#
TensorFlow TopK comparator는 NaN이 있을 때 모든 비교가 false로 흘러가며 사실상 서로를 같게 취급해 추이성을 깨뜨렸고, 그 결과 std::sort/std::make_heap의 계약을 위반할 수 있었다.
Solution#
비교 함수에서 NaN과 동률을 별도 분기하여 전순서에 가까운 일관된 순서를 정의하고, 디버그/검증 모드에서 불변식 위반이 드러나는 테스트를 추가한다.
Failure Modes#
- NaN, null, sentinel 값이 일반 비교 경로에 섞여 비일관적 결과를 만든다.
- 동률 해소를 인덱스/ID로 미루더라도 그 이전 비교가 불완전하면 추이성 위반이 남는다.
- 릴리스 빌드에서는 조용히 지나가고 디버그 STL이나 다른 표준 라이브러리에서만 폭발한다.
Sources#
- https://github.com/tensorflow/tensorflow/pull/124214
- https://github.com/tensorflow/tensorflow/pull/124280
- https://github.com/tensorflow/tensorflow/pull/124289
- https://github.com/tensorflow/tensorflow/pull/124206
- https://github.com/tensorflow/tensorflow/pull/124283
- https://github.com/tensorflow/tensorflow/pull/124259
- https://github.com/tensorflow/tensorflow/pull/124275
- https://github.com/tensorflow/tensorflow/pull/124279
- https://github.com/tensorflow/tensorflow/pull/124246
- https://github.com/huggingface/transformers/pull/47315
- https://github.com/huggingface/transformers/pull/47179
- https://github.com/huggingface/transformers/pull/47626
- https://github.com/huggingface/transformers/pull/47301
- https://github.com/huggingface/transformers/pull/47304
- https://github.com/huggingface/transformers/pull/47459
- https://github.com/huggingface/transformers/pull/47623
- https://github.com/microsoft/ML-For-Beginners/pull/1002
- https://github.com/microsoft/ML-For-Beginners/pull/1001
- https://github.com/microsoft/ML-For-Beginners/pull/1000
- https://github.com/microsoft/ML-For-Beginners/pull/994
- https://github.com/microsoft/ML-For-Beginners/pull/991
- https://github.com/microsoft/ML-For-Beginners/pull/990
- https://github.com/microsoft/ML-For-Beginners/pull/989
- https://github.com/ClickHouse/ClickHouse/pull/112443
- https://github.com/ClickHouse/ClickHouse/pull/110449
- https://github.com/ClickHouse/ClickHouse/pull/110619
- https://github.com/ClickHouse/ClickHouse/pull/109232
- https://github.com/ClickHouse/ClickHouse/pull/107927
- https://github.com/ClickHouse/ClickHouse/pull/105701
- https://github.com/ClickHouse/ClickHouse/pull/107059
- https://github.com/ClickHouse/ClickHouse/pull/109703
- https://github.com/ClickHouse/ClickHouse/pull/112434
- https://github.com/ClickHouse/ClickHouse/pull/105896
- https://github.com/ClickHouse/ClickHouse/pull/109977
- https://github.com/ClickHouse/ClickHouse/pull/110284
- https://github.com/ClickHouse/ClickHouse/pull/112297
- https://github.com/vercel/turborepo/pull/13558
- https://github.com/vercel/turborepo/pull/13556
- https://github.com/vercel/turborepo/pull/13554
- https://github.com/vercel/turborepo/pull/13546
- https://github.com/vercel/turborepo/pull/13543
- https://github.com/vercel/turborepo/pull/13540
- https://github.com/vercel/turborepo/pull/13539
- https://github.com/vercel/turborepo/pull/13538
- mined_at: 2026-07-30T02:54:16Z
Sagwan Revalidation 2026-07-30T03:34:22Z#
- verdict:
ok - note: 비교 불변식·NaN 명시 처리 권고는 현재도 표준 관행과 부합한다.
Sagwan Revalidation 2026-08-01T14:21:06Z#
- verdict:
ok - note: 비교 불변식과 NaN 명시 처리 권장은 여전히 최신 관행에 부합한다.