Skip to content

[module_topk_*] de-torch topk_per_row / topk_plain + externalize workspaces - #4702

Merged
amd-ruitang3 merged 1 commit into
ROCm:mainfrom
amd-ruitang3:module_topk_refactor
Aug 13, 2026
Merged

amd-ruitang3 merged 1 commit into
ROCm:mainfrom
amd-ruitang3:module_topk_refactor

Conversation

@amd-ruitang3

@amd-ruitang3 amd-ruitang3 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Build-time drop (rebuilt from a clean tree): module_top_k_per_row
~47s → ~33s, module_topk_plain ~127s → ~92s.

1. Remove torch/ATen from the compiled TUs (Strategy A, develop=True)

topk_per_row_kernels.cu and topk_plain_kernels.cu no longer #include the
torch/ATen/c10 header tree — the single biggest JIT build-time cost. The host
entry points now take POD aiter_tensor_t (optional args via
std::optional<aiter_tensor_t>); the Python @compile_ops(..., develop=True)
decorator auto-converts torch.Tensor args and sets the HIP stream
(AITER_SET_STREAM_PYBIND). topk_plain additionally gets a torch-free
hip2ck type map (replacing t2ck) and VLLM_DISPATCH_FLOATING_TYPES_rmTorch.

2. Externalize device scratch (no more in-C++ AiterTensor::empty)

The kernels previously self-allocated scratch. AiterTensor::empty/zeros go
through hipMallocAsync/hipMalloc with no block reuse, unlike
torch.empty (torch caching allocator). The scratch is now produced Python-side
(torch-cached) and passed in via an optional workspace param, backed by new
size-query ops (topk_mb_workspace_size / topk_ob_workspace_size /
topk_plain_workspace_size). C++ AITER_CHECKs a caller-provided buffer instead
of allocating. Workspace sizes exactly match the original torch::empty({...})
sizing; the mb path reuses a zeroed, self-reset cached buffer.

Compatibility

This also merges with the new stable top-k parameter (deterministic,
ascending-ordered, smallest-index tie-break so every TP rank selects an identical
KV set): when stable=True the one-block path is forced and runs on the
caller-provided workspace (no device allocation in C++).

Testing

  • op_tests/test_topk_per_row.py — PASS
  • op_tests/test_topk_row_prefill.py — PASS
  • op_tests/test_topk_plain.py — PASS

Dependencies

  • No new third-party dependencies added

@amd-ruitang3
amd-ruitang3 requested a review from a team August 12, 2026 05:53
@github-actions

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:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
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 4702 --add-label <label>

@amd-ruitang3
amd-ruitang3 requested a review from ukannika August 12, 2026 08:55
@amd-ruitang3
amd-ruitang3 merged commit a11f551 into ROCm:main Aug 13, 2026
40 checks passed
JH-Leon-KIM-AMD added a commit that referenced this pull request Aug 13, 2026
main's #4702 moved scratch allocation for the one-block top-k kernels out of C++
and onto the Python side: the host now sizes the buffer with
topk_ob_workspace_size, allocates it with get_topk_scratch_workspace, and passes
it down. The C++ entry no longer allocates anything and aborts on a null
workspace. That collided with this branch, which had given
top_k_per_row_decode a `workspace` argument of its own.

The two meanings are not interchangeable. Ours is a caller-owned int32 buffer
laid out for the FlyDSL tiered kernel's counters and histograms; main's is plain
uint8 ob scratch sized by a different formula. Forwarding one where the other is
expected would either hand the HIP kernel a buffer it misreads or leave it with
none at all, so the fallback now sizes and allocates its own, under a separate
name, and the caller's buffer still reaches the FlyDSL path only. The docstring
claim that the HIP kernel allocates its own scratch and ignores the argument was
true until today and is now rewritten: the argument is still dropped on
fallback, but for a different reason.

Two tests assumed the old contract. The direct binding call in
test_hip_and_flydsl_agree_on_a_gated_shape passed a null workspace, which now
aborts the process, so it sizes one the way the dispatcher does.
test_hip_fallback_allocates_no_workspace still checks something true -- that a
fallback leaves the FlyDSL cache empty -- but its name asserted the opposite of
the new behaviour, so it is renamed to say what it verifies.

gfx950: dispatch unit tests 35/35, FlyDSL kernel suite 109/109, stable suite ALL
PASS, top_k_per_row sweep 561/561 cells. On a gate-accepted shape (rows=1,
width=131072, k=2048) the dispatch counters still split the way stable requires:
stable=False takes FlyDSL, stable=True takes HIP and emits ascending indices.
amd-ruitang3 added a commit to amd-ruitang3/aiter that referenced this pull request Aug 14, 2026
Follow-up to the topk cleanup: remove the vestigial
`#include "py_itfs_common.h"` from the other non-CK TUs that include it but use
none of its symbols. These files keep their own direct torch includes (they are
still torch-bound modules), so this is not an immediate compile-time win — it
removes a latent regression: when these modules are later de-torched, a leftover
py_itfs_common.h (which has an unguarded `#include <torch/all.h>`) would silently
re-introduce the torch header tree, exactly the trap that left
module_top_k_per_row half-de-torched after ROCm#4702.

Files:
- py_itfs_cu/asm_mha_{fwd,bwd,varlen_fwd,varlen_bwd}.cu — torch/all.h +
  ATen/hip/HIPContext.h remain (load-bearing; ~44-74 torch tokens each);
  getCurrentHIPStream here is at::hip::, so no aiter_hip_common.h symbol was used.
- kernels/fused_ar_mhc_post.cu — already includes aiter_hip_common.h directly
  (line 8) for AITER_CHECK/HIP_CALL; ATen/c10 includes remain.

Verified in-container (gfx942): module_fmha_v3_fwd (39.4s) and module_fused_ar_mhc
(36.5s) both rebuild clean from a cleared build dir. asm_mha_fwd.cu is
include-identical to the other three asm_mha TUs.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
amd-ruitang3 added a commit that referenced this pull request Aug 17, 2026
…per_row now torch-free (#4756)

* [detorch] top_k_per_row: drop vestigial py_itfs_common.h include

asm_topk_per_row_{decode,prefill}.cu each carried an
`#include "py_itfs_common.h"` that was their ONLY torch source (that header
has an unguarded `#include <torch/all.h>`), yet neither TU uses any symbol
py_itfs_common.h exports. The kernel TU (topk_per_row_kernels.cu) was already
de-torched in #4702; these two ASM sibling TUs were missed, so the whole
module_top_k_per_row closure kept pulling the torch/ATen header tree on every
JIT build.

Both files already include aiter_tensor.h, which pulls aiter_hip_common.h
(providing AITER_CHECK / HIP_CALL / HipDeviceGuard + the HIP runtime) — so the
include is simply removed, no replacement needed. The full module closure
(13 headers) is now torch-free.

Verified in-container (gfx942): module_top_k_per_row rebuilds clean;
test_topk_per_row.py (decode) all shapes match torch.topk + mb_workspace_reuse
PASS; test_topk_row_prefill.py standard + fast kernels Passed (all_close=True).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* [detorch] drop remaining vestigial py_itfs_common.h includes (non-CK)

Follow-up to the topk cleanup: remove the vestigial
`#include "py_itfs_common.h"` from the other non-CK TUs that include it but use
none of its symbols. These files keep their own direct torch includes (they are
still torch-bound modules), so this is not an immediate compile-time win — it
removes a latent regression: when these modules are later de-torched, a leftover
py_itfs_common.h (which has an unguarded `#include <torch/all.h>`) would silently
re-introduce the torch header tree, exactly the trap that left
module_top_k_per_row half-de-torched after #4702.

Files:
- py_itfs_cu/asm_mha_{fwd,bwd,varlen_fwd,varlen_bwd}.cu — torch/all.h +
  ATen/hip/HIPContext.h remain (load-bearing; ~44-74 torch tokens each);
  getCurrentHIPStream here is at::hip::, so no aiter_hip_common.h symbol was used.
- kernels/fused_ar_mhc_post.cu — already includes aiter_hip_common.h directly
  (line 8) for AITER_CHECK/HIP_CALL; ATen/c10 includes remain.

Verified in-container (gfx942): module_fmha_v3_fwd (39.4s) and module_fused_ar_mhc
(36.5s) both rebuild clean from a cleared build dir. asm_mha_fwd.cu is
include-identical to the other three asm_mha TUs.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
zufayu added a commit that referenced this pull request Aug 20, 2026
…ependency (#4853)

* [topk_per_row] include <optional> where it is used

topk_per_row_kernels.cu declares its own entry points taking
std::optional<aiter_tensor_t> (L2849/2854/2962) but never includes
<optional>; it does not include its own header csrc/include/topk_per_row.h,
which does. The TU only compiled because something else in the include set
happened to drag <optional> in.

On ROCm 7.0.0 nothing does, and pristine upstream fails to build:

  topk_per_row_kernels.cu:2849:33: error: no template named 'optional' in namespace 'std'
  topk_per_row_kernels.cu:2854:75: error: no member named 'nullopt' in namespace 'std'

Most likely fallout from #4702 de-torching this TU: dropping the torch
headers dropped the transitive <optional> with them, and the ROCm 7.2 CI
image still supplies it by another path so nobody noticed.

Include what we use. No functional change -- the code object is bit-identical
either way; this only decides whether the file compiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* [topk_per_row] drop the hipcub::Traits dependency

ROCm 10 ships hipcub built on CCCL 3.0, which removed the public
hipcub::Traits<> class. topk_per_row_kernels.cu used it in 13 places
(11 live, 2 inside a commented-out twiddle_out), so the two modules that
compile this TU -- module_top_k_per_row and module_topk_plain -- cannot be
built on ROCm 10.

This is the same breakage #4843 is fixing in csrc/cpp_itfs/sampling/
sampling.cuh, and literally the same code: the radix top-k twiddle_in was
written in this file (#1394, 2025-11), copied within it a second time
(#3087, 2026-05-10), and copied into sampling.cuh two weeks later (#3217).
#4843 fixes the third copy; this fixes the first two.

Rather than add a third private stand-in, put it where #4594 already moved
the hipcub comparison functors -- csrc/include/hip_reduce.h now also carries
aiter::radix_traits. #4594 skipped sampling.cuh because Traits "had no local
equivalent"; this adds that equivalent, so #4843's local radix_traits can
collapse onto it later (sampling is built through cpp_itfs, whose include
list is an explicit file whitelist, so that needs three one-line changes in
csrc/cpp_itfs/sampling/*.py -- out of scope here).

radix_traits<float>::UnsignedBits is uint32_t, which is exactly what hipcub
resolved: NumericTraits<float> -> BaseTraits<FLOATING_POINT, true, false,
unsigned int, float>. It is deliberately left undefined for every other T:
every instantiation of this file's radix paths is <float, ...>
(mb::standalone_stable_radix_topk and ob::dispatch_topk_oneblock, 14 call
sites), so a new dtype should fail to compile rather than silently pick a
width. The non-float branch of twiddle_in is dropped for the same reason and
replaced by a static_assert; select_min was already dead there for float
(the mask is symmetric, the ordering flip lives at the call sites). The
float bit math is transcribed unchanged.

Validated on gfx942 + gfx950, ROCm 7.0.0 / hipcc 7.0.51831, by compiled
output rather than tests alone:

- ISA is byte-identical, both arches. .text, .rodata and the full
  disassembly of every code object in .hip_fatbin all match the pre-change
  build (gfx942 .text d115e671f0ea2eeb, 19 kernels, 138991 lines;
  gfx950 .text 051fa067e0199306, 19 kernels, 138548 lines). The raw ELF
  bytes are not a usable check -- .dynsym/.dynstr/.strtab/.hash differ
  between two builds of the *same* source -- so the harness was first
  self-checked by building the baseline twice and confirming .text and the
  disassembly are stable.
- No new warnings. The 3 -Wc++23-extensions warnings the TU now emits come
  from block_reduce in hip_reduce.h and are pre-existing: the unmodified
  header alone emits the same 3, as does topk_softmax_kernels.cu and every
  other consumer.
- module_top_k_per_row builds (19.6s). op_tests/test_topk_per_row.py 737/737
  rows pass plus mb_workspace_reuse PASS; test_topk_per_row_stable.py
  "RESULT: ALL PASS"; test_topk_row_prefill.py standard and fast kernels
  both pass.

Known gaps, deliberately not addressed:

- Not verified on ROCm 10 -- no such toolchain here. The claim rests on this
  being the same edit #4843 validated in a rocm10.0.0rc3 container, removing
  the one symbol that errored there.
- This TU still uses hipcub BlockScan / BlockLoad / BlockStore, which #4843
  proves survive CCCL 3.0, and BlockRadixSort, whose ROCm 10 status neither
  PR covers. So this makes topk_per_row build on ROCm 10 as far as Traits is
  concerned; it does not make the file hipcub-free.
- module_topk_plain still cannot be built here, but for an unrelated
  pre-existing reason: topk_plain_kernels.cu fails with
  ComputeTypeTraits<unsigned short> not specialized (12 errors on pristine
  upstream too). That file does not include hip_reduce.h.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
gyohuangxin pushed a commit to gyohuangxin/aiter that referenced this pull request Aug 20, 2026
…ependency (ROCm#4853)

* [topk_per_row] include <optional> where it is used

topk_per_row_kernels.cu declares its own entry points taking
std::optional<aiter_tensor_t> (L2849/2854/2962) but never includes
<optional>; it does not include its own header csrc/include/topk_per_row.h,
which does. The TU only compiled because something else in the include set
happened to drag <optional> in.

On ROCm 7.0.0 nothing does, and pristine upstream fails to build:

  topk_per_row_kernels.cu:2849:33: error: no template named 'optional' in namespace 'std'
  topk_per_row_kernels.cu:2854:75: error: no member named 'nullopt' in namespace 'std'

Most likely fallout from ROCm#4702 de-torching this TU: dropping the torch
headers dropped the transitive <optional> with them, and the ROCm 7.2 CI
image still supplies it by another path so nobody noticed.

Include what we use. No functional change -- the code object is bit-identical
either way; this only decides whether the file compiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* [topk_per_row] drop the hipcub::Traits dependency

ROCm 10 ships hipcub built on CCCL 3.0, which removed the public
hipcub::Traits<> class. topk_per_row_kernels.cu used it in 13 places
(11 live, 2 inside a commented-out twiddle_out), so the two modules that
compile this TU -- module_top_k_per_row and module_topk_plain -- cannot be
built on ROCm 10.

This is the same breakage ROCm#4843 is fixing in csrc/cpp_itfs/sampling/
sampling.cuh, and literally the same code: the radix top-k twiddle_in was
written in this file (ROCm#1394, 2025-11), copied within it a second time
(ROCm#3087, 2026-05-10), and copied into sampling.cuh two weeks later (ROCm#3217).
ROCm#4843 fixes the third copy; this fixes the first two.

Rather than add a third private stand-in, put it where ROCm#4594 already moved
the hipcub comparison functors -- csrc/include/hip_reduce.h now also carries
aiter::radix_traits. ROCm#4594 skipped sampling.cuh because Traits "had no local
equivalent"; this adds that equivalent, so ROCm#4843's local radix_traits can
collapse onto it later (sampling is built through cpp_itfs, whose include
list is an explicit file whitelist, so that needs three one-line changes in
csrc/cpp_itfs/sampling/*.py -- out of scope here).

radix_traits<float>::UnsignedBits is uint32_t, which is exactly what hipcub
resolved: NumericTraits<float> -> BaseTraits<FLOATING_POINT, true, false,
unsigned int, float>. It is deliberately left undefined for every other T:
every instantiation of this file's radix paths is <float, ...>
(mb::standalone_stable_radix_topk and ob::dispatch_topk_oneblock, 14 call
sites), so a new dtype should fail to compile rather than silently pick a
width. The non-float branch of twiddle_in is dropped for the same reason and
replaced by a static_assert; select_min was already dead there for float
(the mask is symmetric, the ordering flip lives at the call sites). The
float bit math is transcribed unchanged.

Validated on gfx942 + gfx950, ROCm 7.0.0 / hipcc 7.0.51831, by compiled
output rather than tests alone:

- ISA is byte-identical, both arches. .text, .rodata and the full
  disassembly of every code object in .hip_fatbin all match the pre-change
  build (gfx942 .text d115e671f0ea2eeb, 19 kernels, 138991 lines;
  gfx950 .text 051fa067e0199306, 19 kernels, 138548 lines). The raw ELF
  bytes are not a usable check -- .dynsym/.dynstr/.strtab/.hash differ
  between two builds of the *same* source -- so the harness was first
  self-checked by building the baseline twice and confirming .text and the
  disassembly are stable.
- No new warnings. The 3 -Wc++23-extensions warnings the TU now emits come
  from block_reduce in hip_reduce.h and are pre-existing: the unmodified
  header alone emits the same 3, as does topk_softmax_kernels.cu and every
  other consumer.
- module_top_k_per_row builds (19.6s). op_tests/test_topk_per_row.py 737/737
  rows pass plus mb_workspace_reuse PASS; test_topk_per_row_stable.py
  "RESULT: ALL PASS"; test_topk_row_prefill.py standard and fast kernels
  both pass.

Known gaps, deliberately not addressed:

- Not verified on ROCm 10 -- no such toolchain here. The claim rests on this
  being the same edit ROCm#4843 validated in a rocm10.0.0rc3 container, removing
  the one symbol that errored there.
- This TU still uses hipcub BlockScan / BlockLoad / BlockStore, which ROCm#4843
  proves survive CCCL 3.0, and BlockRadixSort, whose ROCm 10 status neither
  PR covers. So this makes topk_per_row build on ROCm 10 as far as Traits is
  concerned; it does not make the file hipcub-free.
- module_topk_plain still cannot be built here, but for an unrelated
  pre-existing reason: topk_plain_kernels.cu fails with
  ComputeTypeTraits<unsigned short> not specialized (12 errors on pristine
  upstream too). That file does not include hip_reduce.h.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
gyohuangxin added a commit that referenced this pull request Aug 20, 2026
* [HIP] fix(sampling): support ROCm 10 by dropping hipcub::Traits dependency (#4843)

* fix(sampling): support ROCm 10 by dropping hipcub::Traits dependency

ROCm 10's hipcub removed the deprecated `hipcub::Traits<T>` class, so
`csrc/cpp_itfs/sampling/sampling.cuh` failed to compile:

    error: no type named 'Traits' in namespace 'hipcub'

which is why #4754 disabled cpp_itfs sampling on ROCm 10 entirely.

The radix_topk code only used `hipcub::Traits<T>::UnsignedBits`, and only
ever with T=float. Replace it with a self-contained `radix_traits<float>`
(UnsignedBits = uint32_t). The float twiddle logic is byte-identical to the
previous float branch (select_min was already a no-op for float), so runtime
behavior is unchanged.

With the toolchain incompatibility fixed, re-enable the kernels on ROCm 10:
- remove the ROCm-10 import guard (csrc/cpp_itfs/sampling/__init__.py)
- revert the ROCm-10 skip in op_tests/test_sampling.py

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(sampling): drop removed hipcub::Max/Min functors for ROCm 10

ROCm 10 hipcub (CCCL 3.0) no longer exposes the deprecated hipcub::Max /
hipcub::Min reduction functors, so BlockReduce().Reduce(..., hipcub::Max()/Min())
failed with "no member named 'Max'/'Min' in namespace 'hipcub'". This surfaced
only on the real ROCm 10 image after the hipcub::Traits fix.

Replace them with self-contained AiterMaxOp / AiterMinOp functors (same
semantics), keeping sampling.cuh independent of hipcub symbols that change
across ROCm versions.

Validated on rocm/ufb-private ...rocm10.0.0rc3 (hipcub 5.0 / CCCL 3.0,
torch 2.12.0+rocm10.0.0rc3, MI308X): all three sampling ops compile for
gfx942/gfx950 and op_tests/test_sampling.py passes 348 passed, 4 skipped
(matched runtime, no HIP_LAUNCH_BLOCKING).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

---------

Co-authored-by: sixifang <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

* [HIP] fix(topk_per_row): support ROCm 10 by dropping hipcub::Traits dependency (#4853)

* [topk_per_row] include <optional> where it is used

topk_per_row_kernels.cu declares its own entry points taking
std::optional<aiter_tensor_t> (L2849/2854/2962) but never includes
<optional>; it does not include its own header csrc/include/topk_per_row.h,
which does. The TU only compiled because something else in the include set
happened to drag <optional> in.

On ROCm 7.0.0 nothing does, and pristine upstream fails to build:

  topk_per_row_kernels.cu:2849:33: error: no template named 'optional' in namespace 'std'
  topk_per_row_kernels.cu:2854:75: error: no member named 'nullopt' in namespace 'std'

Most likely fallout from #4702 de-torching this TU: dropping the torch
headers dropped the transitive <optional> with them, and the ROCm 7.2 CI
image still supplies it by another path so nobody noticed.

Include what we use. No functional change -- the code object is bit-identical
either way; this only decides whether the file compiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* [topk_per_row] drop the hipcub::Traits dependency

ROCm 10 ships hipcub built on CCCL 3.0, which removed the public
hipcub::Traits<> class. topk_per_row_kernels.cu used it in 13 places
(11 live, 2 inside a commented-out twiddle_out), so the two modules that
compile this TU -- module_top_k_per_row and module_topk_plain -- cannot be
built on ROCm 10.

This is the same breakage #4843 is fixing in csrc/cpp_itfs/sampling/
sampling.cuh, and literally the same code: the radix top-k twiddle_in was
written in this file (#1394, 2025-11), copied within it a second time
(#3087, 2026-05-10), and copied into sampling.cuh two weeks later (#3217).
#4843 fixes the third copy; this fixes the first two.

Rather than add a third private stand-in, put it where #4594 already moved
the hipcub comparison functors -- csrc/include/hip_reduce.h now also carries
aiter::radix_traits. #4594 skipped sampling.cuh because Traits "had no local
equivalent"; this adds that equivalent, so #4843's local radix_traits can
collapse onto it later (sampling is built through cpp_itfs, whose include
list is an explicit file whitelist, so that needs three one-line changes in
csrc/cpp_itfs/sampling/*.py -- out of scope here).

radix_traits<float>::UnsignedBits is uint32_t, which is exactly what hipcub
resolved: NumericTraits<float> -> BaseTraits<FLOATING_POINT, true, false,
unsigned int, float>. It is deliberately left undefined for every other T:
every instantiation of this file's radix paths is <float, ...>
(mb::standalone_stable_radix_topk and ob::dispatch_topk_oneblock, 14 call
sites), so a new dtype should fail to compile rather than silently pick a
width. The non-float branch of twiddle_in is dropped for the same reason and
replaced by a static_assert; select_min was already dead there for float
(the mask is symmetric, the ordering flip lives at the call sites). The
float bit math is transcribed unchanged.

Validated on gfx942 + gfx950, ROCm 7.0.0 / hipcc 7.0.51831, by compiled
output rather than tests alone:

- ISA is byte-identical, both arches. .text, .rodata and the full
  disassembly of every code object in .hip_fatbin all match the pre-change
  build (gfx942 .text d115e671f0ea2eeb, 19 kernels, 138991 lines;
  gfx950 .text 051fa067e0199306, 19 kernels, 138548 lines). The raw ELF
  bytes are not a usable check -- .dynsym/.dynstr/.strtab/.hash differ
  between two builds of the *same* source -- so the harness was first
  self-checked by building the baseline twice and confirming .text and the
  disassembly are stable.
- No new warnings. The 3 -Wc++23-extensions warnings the TU now emits come
  from block_reduce in hip_reduce.h and are pre-existing: the unmodified
  header alone emits the same 3, as does topk_softmax_kernels.cu and every
  other consumer.
- module_top_k_per_row builds (19.6s). op_tests/test_topk_per_row.py 737/737
  rows pass plus mb_workspace_reuse PASS; test_topk_per_row_stable.py
  "RESULT: ALL PASS"; test_topk_row_prefill.py standard and fast kernels
  both pass.

Known gaps, deliberately not addressed:

- Not verified on ROCm 10 -- no such toolchain here. The claim rests on this
  being the same edit #4843 validated in a rocm10.0.0rc3 container, removing
  the one symbol that errored there.
- This TU still uses hipcub BlockScan / BlockLoad / BlockStore, which #4843
  proves survive CCCL 3.0, and BlockRadixSort, whose ROCm 10 status neither
  PR covers. So this makes topk_per_row build on ROCm 10 as far as Traits is
  concerned; it does not make the file hipcub-free.
- module_topk_plain still cannot be built here, but for an unrelated
  pre-existing reason: topk_plain_kernels.cu fails with
  ComputeTypeTraits<unsigned short> not specialized (12 errors on pristine
  upstream too). That file does not include hip_reduce.h.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: who who who <[email protected]>
Co-authored-by: sixifang <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: zufayu <[email protected]>
Co-authored-by: zufayu <[email protected]>
nsakkine pushed a commit to nsakkine/aiter that referenced this pull request Aug 21, 2026
…ependency (ROCm#4853)

* [topk_per_row] include <optional> where it is used

topk_per_row_kernels.cu declares its own entry points taking
std::optional<aiter_tensor_t> (L2849/2854/2962) but never includes
<optional>; it does not include its own header csrc/include/topk_per_row.h,
which does. The TU only compiled because something else in the include set
happened to drag <optional> in.

On ROCm 7.0.0 nothing does, and pristine upstream fails to build:

  topk_per_row_kernels.cu:2849:33: error: no template named 'optional' in namespace 'std'
  topk_per_row_kernels.cu:2854:75: error: no member named 'nullopt' in namespace 'std'

Most likely fallout from ROCm#4702 de-torching this TU: dropping the torch
headers dropped the transitive <optional> with them, and the ROCm 7.2 CI
image still supplies it by another path so nobody noticed.

Include what we use. No functional change -- the code object is bit-identical
either way; this only decides whether the file compiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* [topk_per_row] drop the hipcub::Traits dependency

ROCm 10 ships hipcub built on CCCL 3.0, which removed the public
hipcub::Traits<> class. topk_per_row_kernels.cu used it in 13 places
(11 live, 2 inside a commented-out twiddle_out), so the two modules that
compile this TU -- module_top_k_per_row and module_topk_plain -- cannot be
built on ROCm 10.

This is the same breakage ROCm#4843 is fixing in csrc/cpp_itfs/sampling/
sampling.cuh, and literally the same code: the radix top-k twiddle_in was
written in this file (ROCm#1394, 2025-11), copied within it a second time
(ROCm#3087, 2026-05-10), and copied into sampling.cuh two weeks later (ROCm#3217).
ROCm#4843 fixes the third copy; this fixes the first two.

Rather than add a third private stand-in, put it where ROCm#4594 already moved
the hipcub comparison functors -- csrc/include/hip_reduce.h now also carries
aiter::radix_traits. ROCm#4594 skipped sampling.cuh because Traits "had no local
equivalent"; this adds that equivalent, so ROCm#4843's local radix_traits can
collapse onto it later (sampling is built through cpp_itfs, whose include
list is an explicit file whitelist, so that needs three one-line changes in
csrc/cpp_itfs/sampling/*.py -- out of scope here).

radix_traits<float>::UnsignedBits is uint32_t, which is exactly what hipcub
resolved: NumericTraits<float> -> BaseTraits<FLOATING_POINT, true, false,
unsigned int, float>. It is deliberately left undefined for every other T:
every instantiation of this file's radix paths is <float, ...>
(mb::standalone_stable_radix_topk and ob::dispatch_topk_oneblock, 14 call
sites), so a new dtype should fail to compile rather than silently pick a
width. The non-float branch of twiddle_in is dropped for the same reason and
replaced by a static_assert; select_min was already dead there for float
(the mask is symmetric, the ordering flip lives at the call sites). The
float bit math is transcribed unchanged.

Validated on gfx942 + gfx950, ROCm 7.0.0 / hipcc 7.0.51831, by compiled
output rather than tests alone:

- ISA is byte-identical, both arches. .text, .rodata and the full
  disassembly of every code object in .hip_fatbin all match the pre-change
  build (gfx942 .text d115e671f0ea2eeb, 19 kernels, 138991 lines;
  gfx950 .text 051fa067e0199306, 19 kernels, 138548 lines). The raw ELF
  bytes are not a usable check -- .dynsym/.dynstr/.strtab/.hash differ
  between two builds of the *same* source -- so the harness was first
  self-checked by building the baseline twice and confirming .text and the
  disassembly are stable.
- No new warnings. The 3 -Wc++23-extensions warnings the TU now emits come
  from block_reduce in hip_reduce.h and are pre-existing: the unmodified
  header alone emits the same 3, as does topk_softmax_kernels.cu and every
  other consumer.
- module_top_k_per_row builds (19.6s). op_tests/test_topk_per_row.py 737/737
  rows pass plus mb_workspace_reuse PASS; test_topk_per_row_stable.py
  "RESULT: ALL PASS"; test_topk_row_prefill.py standard and fast kernels
  both pass.

Known gaps, deliberately not addressed:

- Not verified on ROCm 10 -- no such toolchain here. The claim rests on this
  being the same edit ROCm#4843 validated in a rocm10.0.0rc3 container, removing
  the one symbol that errored there.
- This TU still uses hipcub BlockScan / BlockLoad / BlockStore, which ROCm#4843
  proves survive CCCL 3.0, and BlockRadixSort, whose ROCm 10 status neither
  PR covers. So this makes topk_per_row build on ROCm 10 as far as Traits is
  concerned; it does not make the file hipcub-free.
- module_topk_plain still cannot be built here, but for an unrelated
  pre-existing reason: topk_plain_kernels.cu fails with
  ComputeTypeTraits<unsigned short> not specialized (12 errors on pristine
  upstream too). That file does not include hip_reduce.h.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
nsakkine pushed a commit to nsakkine/aiter that referenced this pull request Aug 24, 2026
…ependency (ROCm#4853)

* [topk_per_row] include <optional> where it is used

topk_per_row_kernels.cu declares its own entry points taking
std::optional<aiter_tensor_t> (L2849/2854/2962) but never includes
<optional>; it does not include its own header csrc/include/topk_per_row.h,
which does. The TU only compiled because something else in the include set
happened to drag <optional> in.

On ROCm 7.0.0 nothing does, and pristine upstream fails to build:

  topk_per_row_kernels.cu:2849:33: error: no template named 'optional' in namespace 'std'
  topk_per_row_kernels.cu:2854:75: error: no member named 'nullopt' in namespace 'std'

Most likely fallout from ROCm#4702 de-torching this TU: dropping the torch
headers dropped the transitive <optional> with them, and the ROCm 7.2 CI
image still supplies it by another path so nobody noticed.

Include what we use. No functional change -- the code object is bit-identical
either way; this only decides whether the file compiles.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* [topk_per_row] drop the hipcub::Traits dependency

ROCm 10 ships hipcub built on CCCL 3.0, which removed the public
hipcub::Traits<> class. topk_per_row_kernels.cu used it in 13 places
(11 live, 2 inside a commented-out twiddle_out), so the two modules that
compile this TU -- module_top_k_per_row and module_topk_plain -- cannot be
built on ROCm 10.

This is the same breakage ROCm#4843 is fixing in csrc/cpp_itfs/sampling/
sampling.cuh, and literally the same code: the radix top-k twiddle_in was
written in this file (ROCm#1394, 2025-11), copied within it a second time
(ROCm#3087, 2026-05-10), and copied into sampling.cuh two weeks later (ROCm#3217).
ROCm#4843 fixes the third copy; this fixes the first two.

Rather than add a third private stand-in, put it where ROCm#4594 already moved
the hipcub comparison functors -- csrc/include/hip_reduce.h now also carries
aiter::radix_traits. ROCm#4594 skipped sampling.cuh because Traits "had no local
equivalent"; this adds that equivalent, so ROCm#4843's local radix_traits can
collapse onto it later (sampling is built through cpp_itfs, whose include
list is an explicit file whitelist, so that needs three one-line changes in
csrc/cpp_itfs/sampling/*.py -- out of scope here).

radix_traits<float>::UnsignedBits is uint32_t, which is exactly what hipcub
resolved: NumericTraits<float> -> BaseTraits<FLOATING_POINT, true, false,
unsigned int, float>. It is deliberately left undefined for every other T:
every instantiation of this file's radix paths is <float, ...>
(mb::standalone_stable_radix_topk and ob::dispatch_topk_oneblock, 14 call
sites), so a new dtype should fail to compile rather than silently pick a
width. The non-float branch of twiddle_in is dropped for the same reason and
replaced by a static_assert; select_min was already dead there for float
(the mask is symmetric, the ordering flip lives at the call sites). The
float bit math is transcribed unchanged.

Validated on gfx942 + gfx950, ROCm 7.0.0 / hipcc 7.0.51831, by compiled
output rather than tests alone:

- ISA is byte-identical, both arches. .text, .rodata and the full
  disassembly of every code object in .hip_fatbin all match the pre-change
  build (gfx942 .text d115e671f0ea2eeb, 19 kernels, 138991 lines;
  gfx950 .text 051fa067e0199306, 19 kernels, 138548 lines). The raw ELF
  bytes are not a usable check -- .dynsym/.dynstr/.strtab/.hash differ
  between two builds of the *same* source -- so the harness was first
  self-checked by building the baseline twice and confirming .text and the
  disassembly are stable.
- No new warnings. The 3 -Wc++23-extensions warnings the TU now emits come
  from block_reduce in hip_reduce.h and are pre-existing: the unmodified
  header alone emits the same 3, as does topk_softmax_kernels.cu and every
  other consumer.
- module_top_k_per_row builds (19.6s). op_tests/test_topk_per_row.py 737/737
  rows pass plus mb_workspace_reuse PASS; test_topk_per_row_stable.py
  "RESULT: ALL PASS"; test_topk_row_prefill.py standard and fast kernels
  both pass.

Known gaps, deliberately not addressed:

- Not verified on ROCm 10 -- no such toolchain here. The claim rests on this
  being the same edit ROCm#4843 validated in a rocm10.0.0rc3 container, removing
  the one symbol that errored there.
- This TU still uses hipcub BlockScan / BlockLoad / BlockStore, which ROCm#4843
  proves survive CCCL 3.0, and BlockRadixSort, whose ROCm 10 status neither
  PR covers. So this makes topk_per_row build on ROCm 10 as far as Traits is
  concerned; it does not make the file hipcub-free.
- module_topk_plain still cannot be built here, but for an unrelated
  pre-existing reason: topk_plain_kernels.cu fails with
  ComputeTypeTraits<unsigned short> not specialized (12 errors on pristine
  upstream too). That file does not include hip_reduce.h.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: zufayu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
JohnQin-llm pushed a commit to xiaobochen-amd/aiter that referenced this pull request Aug 28, 2026
dsa_topk_transform (#5) landed with a ctypes entry, which does not compile on
this base: registering any ffi_type="ctypes" op forces torch_exclude on the
whole module (aiter/jit/core.py), and three of module_top_k_per_row's four
sources still include torch/all.h. Calling aiter.dsa_topk_transform on main
today dies in the JIT build with

    csrc/include/py_itfs_common.h:6:10: fatal error: 'torch/all.h' file not found

The module has been in this state since before #5 -- main's own
top_k_per_row_decode_fast and top_k_per_row_prefill_fast fail identically, they
are just never called, so nothing reached the broken path. #5 added the first
ctypes entry on this module that callers actually use.

This binds the op through pybind instead: a torch::Tensor signature matching
the two entries beside it, the stream from getCurrentHIPStream, a declaration
in topk_per_row.h and an m.def in TOP_K_PER_ROW_PYBIND. The kernel and
coop_topk.cuh are untouched.

The binding costs nothing measurable. Against the ctypes build on the same
MI355X, GLM-5.2 decode logits (live=100001, page_size=1, k=2048), three runs
each:

    rows    ctypes us                pybind us
       1    30.01 / 27.13 / 28.22    24.97 / 25.35 / 24.93
       8    26.85 / 26.13 / 26.48    26.21 / 26.14 / 26.17
      64    39.65 / 40.15 / 39.27    40.08 / 39.45 / 40.07

pybind is never slower and batch 1 is consistently ~3.4us faster with no
overlap between the two sets, so the per-call binding cost #5 cited as the
reason for ctypes is not visible here.

The proper fix is the detorch migration of this module (ROCm#4702, ROCm#4756), after
which the ctypes form builds and this can go back if it ever measures better.

op_tests/test_dsa_topk_transform.py passes 13/13 on gfx950, and output stays
bit-identical to sgl_kernel's fast_topk_transform_fused at every shape.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants