Skip to content

fix(topk): bind dsa_topk_transform through pybind so it builds on main - #6

Merged
jiejingzhangamd merged 1 commit into
mainfrom
fix/dsa-topk-pybind
Aug 28, 2026
Merged

jiejingzhangamd merged 1 commit into
mainfrom
fix/dsa-topk-pybind

Conversation

@jiejingzhangamd

Copy link
Copy Markdown
Collaborator

dsa_topk_transform (#5) landed with a ctypes entry, and that entry does not compile on this base. Calling aiter.dsa_topk_transform on main today fails the JIT build, verified on 7bc2347a against a gfx950 MI355X:

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

Registering any ffi_type="ctypes" op forces torch_exclude on the whole module (aiter/jit/core.py), which strips every torch include from the compile line — but three of module_top_k_per_row's four sources still #include <torch/all.h>.

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 are ctypes entries on the same module and fail identically; they are simply never called, so nothing reached the broken path. #5 added the first one that callers actually use.

What this does

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

#5 justified ctypes as saving per-call binding cost on a ~20 us op. Measured against the ctypes build on the same machine (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.4 us faster with no overlap between the two sets.

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.

Testing

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.

1 participant