← Back to Pull requests

[DSA] Make the fused top-k exact on an overflowing threshold bin #37941

Open
unknown opened this pull request with 565 lines diff across 7 files
View on GitHub
unknown unknown commented on Sep 04, 2026 06:24 UTC
Author

Motivation

The coarse histogram bin comes from the top bits of the fp16 cast, so one bin spans a quarter-binade. The collect pass stores at most 2048 threshold-bin candidates; when the bin is larger, handle_tie ranks whichever arrived first and silently returns a wrong top-k set.

Found while profiling GLM-5.2 decode on MI355X: ~0.04% of rows entered this path across all 8 ranks, with a largest observed bin of 4139 against the 2048 cap and none of those rows bit-identical. #35257 reports the same defect independently from the CUDA side — referenced, not closed.

Modifications

When the threshold bin overflows, narrow it with three exact-key radix rounds (12 / 12 / 8 bits), then make one final input scan to emit candidates above the selected bucket and re-stage the bucket for the existing tie handler. The refinement histogram overlays the dead arrival-order buffer, so LDS is unchanged.

The cold scan uses a separate reversed-lane load path. This keeps its load-address state out of the fast path's registers and restores most of the CUDA regression reported in review.

Replace the fp16-bin boundary conversion with exact fp32 bounds. In particular, when a bin's high edge is +0.0, the boundary advances to the smallest positive subnormal so -0.0 cannot be misclassified as above the bin.

The ROCm long-row split path from #39931 gets the same refinement: each block publishes its uncapped bin count, and the last block refines the whole row when the bin overflows.

The fast path only adds the overflow comparison. The ROCm __launch_bounds__ argument also states the physical floor of 4 waves/SIMD; the CUDA spelling is unchanged.

Accuracy Tests

The exactness test is ROCm-only because CUDA may route these shapes to the still-unrefined cluster path. It covers register, streaming and split (4×65536) shapes with six distributions, including adjacent fp32 values and the +0.0/-0.0 boundary.

| suite | result before the fix |
|---|---|
| 300 pre-existing cases | 300 passed |
| 10 overflowing non-identical cases | 10 failed |
| 2 all_equal controls | 2 passed |

The signed-zero cases also fail on the pre-review refinement and pass with the new boundary conversion. All 324 cases pass on the merged tree; on the split shape, 4 of the 6 distributions failed before the refinement.

Benchmarking and Profiling

MI355X (gfx950), five paired runs per arm on one GPU, 200 iterations after warmup:

| distribution | shape | main | this PR | |
|---|---|---|---|---|
| randn (hot path) | 6×8192 | 5.430 µs | 5.338 µs | −1.7% |
| randn (hot path) | 4×32768 | 12.270 µs | 12.188 µs | −0.7% |
| narrow_bin | 6×8192 | 16.821 µs | 20.937 µs | +24.5% |
| narrow_bin | 4×32768 | 29.652 µs | 50.030 µs | +68.7% |
| two_values (32-bit refinement) | 6×8192 | 11.487 µs | 34.862 µs | +203.5% |
| two_values (32-bit refinement) | 4×32768 | 36.314 µs | 117.756 µs | +224.3% |

The cold-path delta is the cost of exact work that main skips by returning a wrong answer. It is entered on ~0.04% of rows in the serving traffic above. Against the previous PR implementation, the new scan is 2.6–5.3% faster on narrow_bin. On the latest tree, a clean local JIT build is 10.50 s versus 10.02 s on main (+4.8%), rather than the nearly 2× CUDA increase reported before this rewrite.

Without overflow the split path is unchanged (15.5–20.1 µs for batch 1–8, 64K–128K, before and after).

On gfx950: 63 → 71 VGPRs, zero spills, LDS unchanged at 26928 B. We do not have NVIDIA hardware; CUDA register pressure and B200 performance need CI/reviewer measurement.

Known gap

The CUDA-only TopKCluster path still truncates. Refining there needs cluster-wide histogram and emit counters rather than the block-local ones used here.

Checklist

  • Format code with pre-commit.

  • Add exactness tests.

  • Provide accuracy and performance results.

  • Keep documentation and comments synchronized.
  • <!-- pr-states:start -->
    ---

    CI States

    Latest PR Test (Base): <!-- slot:pr-test:start -->:x: Run #37716590753<!-- slot:pr-test:end -->
    Latest PR Test (Extra): <!-- slot:pr-test-extra:start -->:x: Run #37716590551<!-- slot:pr-test-extra:end -->
    Latest PR Test (AMD ROCm 10): <!-- slot:pr-test-amd:start -->:x: Run #37716590754<!-- slot:pr-test-amd:end -->
    <!-- pr-states:end -->

    Diff: 565 lines (+423 / -142) across 7 files Reviewer: DarkSharpness Review duration: 49196m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.57) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 565 lines diff across 7 files
    0.57
    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%
    44%
    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.379
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #37941"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.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: 56.7% (MEDIUM) · Higher = larger blast radius
    56.7%
    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: 44.7% · Lower = rushed or shallow review
    44.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: 31.4% · Safe to merge
    31.4%