Repository navigation
[Triton/Gluon] [Bugfix] Fix Triton topk for rows containing -inf - #6095
Conversation
Track remaining candidates with a validity mask instead of overwriting them with a finfo.min sentinel, and fetch bitonic-merge partners by XOR of bit patterns instead of multiplying by a 0/1 mask (which turned -inf into NaN and -0.0 into +0.0). Break ties by lower index so padding ranks below real entries of equal value.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Independent value and index reductions can still return mismatched signed-zero pairs, which the new assertions do not detect.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Fixes Triton Top-K handling of infinities, extrema, signed zeros, and padded lanes.
Changes:
- Tracks candidate validity independently from values.
- Makes stage-2 swaps bit-exact and index-aware.
- Adds regression coverage for extrema and masked logits.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/topk.py |
Updates selection and merge logic. |
aiter/ops/triton/topk.py |
Uses infinity sentinels and safe padding indices. |
op_tests/triton_tests/test_topk.py |
Adds extrema and signed-zero tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Please address or resolve copilot comments. |
tl.max can return +0.0 for a mix of -0.0 and +0.0, while the selected index pointed at the lowest equal lane, which could hold -0.0. Select the lane whose bit pattern equals the max instead. The test now also checks that each value keeps the sign of the element at its index.
|
@lburzawa Thanks, both Copilot comments are addressed in 91fadb1:
|


Motivation
Fixes #6093
Triton topk marks selected and out-of-range elements with a
finfo(float32).minsentinel, and the stage-2 merge slices pairs by multiplying with a 0/1 mask. Rows containing-inf(masked logits) orfinfo.minget repeated or out-of-range indices and NaN.Technical Details
aiter/ops/triton/_triton_kernels/topk.py:_topk_kernelandtopk_stage1_kernel: track remaining candidates with an explicitvalidmask instead of overwriting their values. Each step picks the lowest valid index whose bit pattern equals the max, so a returned-0.0/+0.0always matches the element at its index._compare_and_swap: get the partner by XOR-reducing the bit patterns of the pair, as upstream Triton'ssortdoes. This is exact for±infand-0.0. Ties are broken by lower index, so padding (index>= N) ranks below real entries of equal value.topk_stage2_kernel: on the TDM path, mark the zero-filled out-of-range lanes as padding.aiter/ops/triton/topk.py: use-infas the fill value andINT32_MAXas the stage-2 padding index.Test Plan
New
test_topk_inf_and_extremainop_tests/triton_tests/test_topk.py. It covers rows with-inf,finfoextrema and±0.0, on both the 1-stage and 2-stage paths, in fp32/bf16/fp16. It checks that indices are in range, distinct, and point at the returned values (including the sign of zeros), and that the values equaltorch.topk's.Test Result
MI325X (gfx942): 94 passed. On base, all 54 new cases fail.
The 2-stage path is 10-17% faster than base, e.g. B=1024, N=128256, k=50 fp32: 3141 → 2667 µs. The 1-stage path is within ±3%. The signed-zero change has no measurable cost.
Submission Checklist