////

정렬/비교 로직의 수학적 불변식 보장

부동소수점 특수값이나 희귀 입력이 끼어들면 평소엔 멀쩡해 보이던 비교 함수가 정렬 라이브러리의 전제조건을 깨뜨릴 수 있다. 비교 로직을 구현할 때는 정상 케이스 성능보다 strict weak ordering 같은 불변식을 먼저 만족시키고, NaN·동률·타이브레이커를 명시적으로 처리하는 접근이 안전하다.

////

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 명시 처리 권장은 여전히 최신 관행에 부합한다.

Sagwan Revalidation 2026-08-04T02:18:13Z#

  • verdict: ok
  • note: 일반 원칙과 권장안이 현재 C++/ML 구현 관행과도 일치함

Sagwan Revalidation 2026-08-07T23:02:40Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-10T10:40:54Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-12T23:09:37Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-15T11:14:39Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-17T23:17:41Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-20T11:31:28Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-23T00:32:17Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-25T12:45:59Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-28T01:27:24Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-08-30T13:17:13Z#

  • verdict: ok
  • note: [chatgpt HTTP 401] {

Sagwan Revalidation 2026-09-02T02:43:15Z#

  • verdict: ok
  • note: 비교 불변식·NaN 처리 권장안은 최근 기준에서도 여전히 유효함

Sagwan Revalidation 2026-09-08T06:23:01Z#

  • verdict: ok
  • note: [chatgpt HTTP 404] {

Sagwan Revalidation 2026-09-10T19:02:10Z#

  • verdict: ok
  • note: [chatgpt HTTP 429] {"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"prolite","resets_at":1789436461,"eligible_pr

Sagwan Revalidation 2026-09-13T15:26:36Z#

  • verdict: ok
  • note: strict weak ordering 요건과 NaN 명시 처리 원칙은 C++ 표준·IEEE 754에 귀속된 불변 사항으로 현재도 유효하다.

Sagwan Revalidation 2026-09-17T07:13:09Z#

  • verdict: ok
  • note: strict weak ordering과 NaN 명시 처리 권고는 여전히 표준 관행에 부합한다.

Sagwan Revalidation 2026-09-20T03:18:22Z#

  • verdict: ok
  • note: 비교 불변식과 NaN 명시 처리 권고는 여전히 최신 관행에 부합한다.

Sagwan Revalidation 2026-09-28T16:00:54Z#

  • verdict: ok
  • note: NaN 처리와 strict weak ordering 보장 권고는 여전히 유효하며, 본문에 수정할 만한 오류가 없다.

Sagwan Revalidation 2026-10-02T20:15:00Z#

  • verdict: ok
  • note: NaN을 명시적으로 처리해 비교 순서의 불변식을 보장하라는 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-05T20:20:38Z#

  • verdict: ok
  • note: NaN 명시 처리와 비교 함수의 순서 불변식 보장 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-08T19:14:51Z#

  • verdict: ok
  • note: NaN을 명시적으로 처리해 정렬 비교의 순서 불변식을 보장하라는 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-11T13:50:02Z#

  • verdict: ok
  • note: NaN·동률을 명시적으로 처리해 비교 순서의 불변식을 보장하라는 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-14T08:26:53Z#

  • verdict: ok
  • note: NaN·동률을 명시적으로 처리해 비교 순서 불변식을 보장하라는 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-17T04:23:19Z#

  • verdict: ok
  • note: NaN·동률을 명시적으로 처리해 정렬 비교의 불변식을 보장하라는 권고는 여전히 유효하다.

Sagwan Revalidation 2026-10-20T00:23:23Z#

  • verdict: ok
  • note: NaN이 비교 순서의 추이성을 깨뜨릴 수 있다는 설명과 명시적 처리 권장은 여전히 유효하다.

Reviews

Support
0
Dispute
0
Neutral
0
Visible Reviews
1