Skip to content

[FlyDSL] [Perf] Select gfx1250 long-row top-k from one sampled-bound read - #7727

Merged
lirui927 merged 4 commits into
ROCm:mainfrom
lirui927:topk/gfx1250-sampled-bound
Nov 16, 2026
Merged

lirui927 merged 4 commits into
ROCm:mainfrom
lirui927:topk/gfx1250-sampled-bound

Conversation

@lirui927

@lirui927 lirui927 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

On gfx1250 (MI455X), the FlyDSL one-block radix top-k used to read each long row from HBM twice: once to build the level-1 histogram, and again to compact the kth key's bucket. This PR reduces that to one read, and overlaps loading the next row with selecting the current one.

  • Sampled bound. For rows of at least max(4k, 4097) keys, the kernel samples about one key per 64 to estimate a lower bound on the kth key. It then compacts every key at or above that bound into LDS in a single pass, and runs the histogram levels on the compacted keys.

    • A row whose candidates number fewer than k, or more than the buffer holds, falls back to the existing two-pass selection. The sample therefore only affects speed; results are unchanged.
  • Row cache with persistent blocks. Each block walks rows grid-strided. The tensor data mover loads the head of the next row into an LDS cache while the current row is selected.

  • Block size. This combines with [FlyDSL] [gfx1250] Radix top-k: 512-thread blocks for long decode rows, index-sort output for streaming rows #7662 (already on main):

    Every combination fits in gfx1250's 320 KiB LDS per CU, with no scratch and no spills.

Only gfx1250 is affected. gfx950 and the gfx1250 short-row kernels compile to the same code as on main.

Benefit

Setup.

  • Hardware: MI455X (gfx1250, 256 CUs).
  • Workload: DeepSeek-V4.1 indexer top-k (stable), called the way ATOM's FP4 paged scorer (score_topk_quantized) calls top_k_per_row_prefill / top_k_per_row_decode. Rows are packed into the score workspace. Each query row is bounded by its causal position, at compression ratio r over a plane of 1M / r columns.
  • Before: main at 6916e20, run in the DSv4.1 gfx1250 image with main's aiter/ops/flydsl, aiter/ops/topk.py, aiter/utility/graph_alloc.py and aiter/jit/utils/hip_runtime.py copied in, under the pinned FlyDSL 0.5.0.
  • After: the same, with this PR's aiter/ops/flydsl.
  • Data: randn means standard normal logits. N(12.8, 3.3) means logits with mean 12.8 and standard deviation 3.3.
  • Runs: each number is the median of three runs, each on buffers of its own. Before and After come from two sessions about 10 minutes apart on the same GPU. The first commit, measured in both, moved by at most 1.9% between them.

Prefill. bs 1, 16384 tokens, k = 512, 16384 rows per call. Each cell is event time / kernel time per call.

Workload Before After Speedup
ctx 65536, r1 (layer 20), randn 1.6407 / 1.6228 ms 0.7537 / 0.6952 ms 2.18x / 2.33x
ctx 65536, r1 (layer 20), N(12.8, 3.3) 2.2438 / 2.2251 ms 0.7465 / 0.7242 ms 3.01x / 3.07x
ctx 65536, r1 candidates (layers 24-36), randn 1.0808 / 1.0673 ms 0.3239 / 0.3081 ms 3.34x / 3.46x
ctx 65536, r1 candidates (layers 24-36), N(12.8, 3.3) 1.2725 / 1.2588 ms 0.3192 / 0.3057 ms 3.99x / 4.12x
ctx 16384, r1 (layer 20), randn 0.7073 / 0.6944 ms 0.2656 / 0.2496 ms 2.66x / 2.78x
ctx 16384, r1 (layer 20), N(12.8, 3.3) 0.8159 / 0.7984 ms 0.2781 / 0.2620 ms 2.93x / 3.05x
ctx 16384, r1 candidates (layers 24-36), randn 0.7030 / 0.6873 ms 0.2591 / 0.2477 ms 2.71x / 2.77x
ctx 16384, r1 candidates (layers 24-36), N(12.8, 3.3) 0.8100 / 0.7943 ms 0.2755 / 0.2611 ms 2.94x / 3.04x

Decode. bs 128, with each request's newest token plus 5 drafts, gives 768 rows per call. A step makes 3 r2 calls, 1 r1 call, 1 r1 block pick (k = 2048) and 4 r1 candidate calls. Each cell is the total kernel time of one step.

Workload Before After Speedup
decode step, ctx 16384, randn 351.9 us 145.9 us 2.41x
decode step, ctx 16384, N(12.8, 3.3) 420.9 us 153.6 us 2.74x
decode step, ctx 65536, randn 464.6 us 208.8 us 2.23x
decode step, ctx 65536, N(12.8, 3.3) 616.9 us 238.7 us 2.58x
decode step, ctx 131072, randn 647.4 us 284.3 us 2.28x
decode step, ctx 131072, N(12.8, 3.3) 866.9 us 291.9 us 2.97x

The same decode step at ctx 65536, broken down by call. Each cell is kernel time per call, randn / N(12.8, 3.3):

Call (calls per step) Before After Speedup
r2, k = 512 (3) 50.2 / 67.9 us 23.4 / 24.5 us 2.15x / 2.77x
r1, k = 512 (1) 83.0 / 133.3 us 39.9 / 65.6 us 2.08x / 2.03x
r1 block pick, k = 2048 (1) 35.0 / 45.9 us 29.5 / 30.0 us 1.19x / 1.53x
r1 candidates, k = 512 (4) 49.0 / 58.5 us 17.3 / 17.4 us 2.83x / 3.36x

"Before" is main at 6916e20, which predates #7662. Measured against current main (which has #7662's 512-thread decode blocks), the change still holds: synthetic full-width decode calls (768 rows, width 65536) are 1.2–2.8x faster at k = 512–2048, stable k = 4096 runs the same kernel as main and is unchanged, and prefill k = 512 stable is 2.0–2.8x faster.

Testing

All on MI455X.

  • New CI regression flydsl-one-block-long-rows in op_tests/test_topk_per_row.py, 16 cases, all pass. It covers:

    • rows walked through the cache, and rows longer than the cache;
    • tied and all-equal rows, which take the fallback;
    • every block/cache combination;
    • the 768 x 6144, k = 2048 decode rows that stay on the unsampled 512-thread path.

    Each case is checked against an exact reference.

  • Existing tests pass unchanged: op_tests/test_topk_per_row.py, test_topk_per_row_stable.py and test_topk_row_prefill.py.

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu 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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 7727 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

lirui927 and others added 2 commits November 12, 2026 03:38
…read

On gfx1250, a long row of the one-block radix top-k first samples its keys,
bounds the kth key's level-1 bucket from the sample, and compacts every key
at or above the bound in one read of the row; the candidates then finish in
LDS. A row whose candidates miss k or overflow the buffer falls back to the
existing two-pass selection, so the sample decides only the speed.

- Persistent blocks walk rows grid-strided, and the tensor data mover loads
  each block's next row, and its first sample keys, into an LDS row cache
  while the current row is selected.
- Long rows with k <= 2048 run 512-thread blocks, two to a CU, when there
  are more rows than CUs, and 1024-thread blocks otherwise.
- gfx1250 barriers fence LDS only, and its wave32 block scans use DPP.

Every change to a path shared with other arches is gated by arch: the
gfx950 kernels compile to the same ISA as before.

Co-authored-by: Cursor <[email protected]>
- Build the row cache's TDM descriptor with
  tdm_ops_gfx1250.make_tensor_descriptor_2d, and the wave32 scans with
  dpp_utils.update_dpp_i32 and fx.coop.warp_broadcast.
- Drop the bound's buffer hold (k = 2048 long rows go back to 1024-thread
  blocks), the single-wave kth search, the windowed level-1 choice, the
  sample keys gathered with the row cache, LDS-only barriers, the row-bounds
  asm, and the gfx1250 forms of paths shared with other arches.
- One arch gate remains. gfx950 and the gfx1250 short-row kernels compile to
  main's ISA.

Co-authored-by: Cursor <[email protected]>
@lirui927
lirui927 force-pushed the topk/gfx1250-sampled-bound branch from fcd90da to 810ba0d Compare November 12, 2026 04:32
test_topk_per_row.py gains a flydsl-one-block-long-rows regression, run by
CI's plain `python3 op_tests/test_topk_per_row.py`. It drives the one-block
kernel at more rows than CUs, so blocks walk rows through the LDS row cache,
on random rows, tied rows that overflow the sampled bound's candidate buffer
and all-equal rows, and stable k = 4096 decode, which keeps 512-thread
blocks without the cache. Stable rows are checked index for index against
an exact reference, unstable rows by key multiset with each column once, and
the written values against the logits at the returned indices.

The half-block LDS budget comment now says it applies only without the row
cache.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lirui927
lirui927 marked this pull request as ready for review November 12, 2026 08:59
@lirui927
lirui927 requested a review from a team November 12, 2026 08:59
@aiter-bot

aiter-bot commented Nov 12, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 6cbeb193be3f · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer
  • FAIL — changed op test not explained in Changed op tests: op_tests/test_topk_per_row.py
  • FAIL — Changed op tests must explain why each changed op test is necessary and non-redundant

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@zufayu
zufayu requested review from coderfeli and yadaish November 13, 2026 01:31
lirui927 pushed a commit to JiaoliangYu/aiter that referenced this pull request Nov 13, 2026
Keep decode rows shorter than the sampled-bound threshold on a 512-thread unsampled build, avoiding the 1024-thread regression at 768x6144 k=2048.

Move the one-row TDM stride fix, wave32/block scans, and the compiler dependency pin into shared helpers, and add the short long-row regression case.

Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants