← Back to Pull requests

fix(rocprim): radix sort merge comparator must not quiet signaling NaNs #16568

Open
unknown opened this pull request with 444 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 23, 2026 18:32 UTC
Author

radix_sort_keys / radix_sort_pairs (and hipcub::DeviceRadixSort, so torch.sort) lose and
duplicate elements when floating point keys mix signaling and quiet NaNs, for 1025 to 2^20 items (the
merge-sort path; below 100000 items for half and bfloat16). Short segments of segmented_radix_sort_keys/pairs order such keys wrongly too.

The merge path sorts 1024-item tiles with the radix block sort (raw bits, -0.0 folded into +0.0) and
then merges them with compare_nan_sensitive, which folded -0.0 with a + T(0) since #3617. That
addition quiets signaling NaNs, so the merge compared different bits than the block sort; tiles were no
longer sorted under the merge comparator and the merge-path partitions wrote some outputs twice and
others never. Before the fix the result is not deterministic and a run can hang or fault the GPU.

compare_nan_sensitive now maps the key bits from sign-magnitude to two's complement (a negative key
becomes minus its magnitude) and compares them as signed integers. -0.0 and +0.0 both map to 0 and all
other bit patterns, NaNs included, keep exactly the order of the radix key codec. No floating point
arithmetic, so the __HIP_NO_HALF_OPERATORS__ special case is no longer needed.

Checked against the radix key codec in both directions for every float, half and bfloat16 bit pattern
and for 2^31 adjacent plus 2^31 random double pairs and special values: no mismatch (develop: 2, 2, 2 and 73).

The change is applied to all three copies of rocPRIM in the repository: projects/hipccl/hipccl3/rocprim,
projects/hipccl/hipccl2/rocprim and the deprecated projects/rocprim (the copy shipped in ROCm 10.2).
CHANGELOG entries are added for hipccl3 (rocPRIM 5.1.0) and projects/rocprim.

Tests: RocprimDeviceRadixSort.SortPairsMixedNaNs and RocprimDeviceSegmentedRadixSort.SortPairsMixedNaNs
(float, double, half, bfloat16; ascending and descending; random keys with signaling and quiet NaNs; exact
comparison with a host stable sort). They fail without the fix and pass with it on gfx1201 and gfx1100 for
hipccl3, and on gfx1100 for hipccl2 and projects/rocprim; the existing radix sort, segmented radix sort and radix
key codec tests still pass.

Performance, benchmark_device_radix_sort and benchmark_device_segmented_radix_sort_{keys,pairs},
merge-path sizes and f32/f64/half keys (f64 only in the segmented benchmark; radix sort f64 timed with a standalone
loop: 1M keys 728 -> 448 us on gfx1201, 479 -> 319 us on gfx1100), 3 repetitions, throughput patched/develop (no MI
GPU was available):

  • gfx1100: radix sort 1.00 to 1.05 (the 1.05 row is noisy); segmented keys geomean 1.07 (0.98 to 2.17), pairs 1.01 (0.91 to 1.09).

  • gfx1201: radix sort 1.00 to 1.02 except f32 keys-only at 1M items, 0.76; segmented keys geomean 1.02

  • (0.84 to 1.70), pairs 1.02 (0.97 to 1.42).
  • f64 keys gain on RDNA, where the old comparator's f64 add is slow. On CDNA (ISA only, not run):

  • gfx942 double merge kernels are +0 to +5% instructions and the segmented warp-sort helpers +12%, float
    +1 to +2%; please benchmark on MI300/MI355. The gfx1201 f32 1M keys-only loss comes from
    the merge-path search loop, where the select in the new mapping adds a VCC dependency wait on gfx12; a
    branch-free mapping recovers about 10% of it there but is slower for f64 and on gfx1100. The comparator
    alone is 1 to 3 VALU shorter than develop for float and half (about 20 shorter for bfloat16) on gfx1201,
    gfx1100, gfx942, gfx90a and gfx950; for double it is 1 shorter on RDNA and 1 longer on CDNA.

    Fixes #16364

    Diff: 444 lines (+333 / -111) across 5 files Reviewer: umfranzw Review duration: 14752m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 444 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.365
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #16568"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.3% (MEDIUM) · Higher = larger blast radius
    45.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.2% · Safe to merge
    24.2%