Skip to content

[Triton/Gluon] [Bugfix] Fix Triton topk for rows containing -inf - #6095

Merged
azaidy merged 3 commits into
ROCm:mainfrom
truong-v:fix/triton-topk-sentinel
Oct 19, 2026
Merged

azaidy merged 3 commits into
ROCm:mainfrom
truong-v:fix/triton-topk-sentinel

Conversation

@truong-v

@truong-v truong-v commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6093

Triton topk marks selected and out-of-range elements with a finfo(float32).min sentinel, and the stage-2 merge slices pairs by multiplying with a 0/1 mask. Rows containing -inf (masked logits) or finfo.min get repeated or out-of-range indices and NaN.

Technical Details

aiter/ops/triton/_triton_kernels/topk.py:

  • _topk_kernel and topk_stage1_kernel: track remaining candidates with an explicit valid mask instead of overwriting their values. Each step picks the lowest valid index whose bit pattern equals the max, so a returned -0.0/+0.0 always matches the element at its index.
  • _compare_and_swap: get the partner by XOR-reducing the bit patterns of the pair, as upstream Triton's sort does. This is exact for ±inf and -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 -inf as the fill value and INT32_MAX as the stage-2 padding index.

Test Plan

New test_topk_inf_and_extrema in op_tests/triton_tests/test_topk.py. It covers rows with -inf, finfo extrema 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 equal torch.topk's.

python -m pytest -q op_tests/triton_tests/test_topk.py

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

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.
@truong-v
truong-v requested a review from a team October 2, 2026 14:02
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6095 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot changed the title [Bugfix] Fix Triton topk for rows containing -inf [Triton/Gluon] [Bugfix] Fix Triton topk for rows containing -inf Oct 2, 2026
@Boss2002n
Boss2002n requested a balanced review from Copilot October 2, 2026 17:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity

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.

Comment thread op_tests/triton_tests/test_topk.py Resolved
@zufayu
zufayu requested review from a team and vgokhale October 3, 2026 00:35
@azaidy
azaidy requested a review from lburzawa October 5, 2026 22:20
@azaidy azaidy assigned lburzawa and unassigned vgokhale Oct 5, 2026
@lburzawa

lburzawa commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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.
@truong-v

Copy link
Copy Markdown
Contributor Author

@lburzawa Thanks, both Copilot comments are addressed in 91fadb1:

  • The kernels now select the element whose bit pattern matches the max, so the value and index of a signed zero always come from the same element.
  • The test also checks the sign of each returned value against the element at its index.

test_topk.py passes 94/94 on MI325X (gfx942), with no measurable perf change. I left the threads open for you to resolve.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The changed gfx1250 descriptor-load path needs runtime confirmation beyond the reported gfx942 results.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@azaidy azaidy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: Triton topk returns repeated / out-of-range indices and NaN for rows containing -inf

5 participants