Repository navigation
fix(topk): bind dsa_topk_transform through pybind so it builds on main - #6
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
dsa_topk_transform(#5) landed with a ctypes entry, and that entry does not compile on this base. Callingaiter.dsa_topk_transformonmaintoday fails the JIT build, verified on7bc2347aagainst a gfx950 MI355X:Registering any
ffi_type="ctypes"op forcestorch_excludeon the whole module (aiter/jit/core.py), which strips every torch include from the compile line — but three ofmodule_top_k_per_row's four sources still#include <torch/all.h>.The module has been in this state since before #5.
main's owntop_k_per_row_decode_fastandtop_k_per_row_prefill_fastare 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::Tensorsignature matching the two entries beside it, the stream fromgetCurrentHIPStream, a declaration intopk_per_row.h, and anm.definTOP_K_PER_ROW_PYBIND. The kernel andcoop_topk.cuhare 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):
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
main(7bc2347a), gfx950 MI355X — before this patch the same call fails to build.op_tests/test_dsa_topk_transform.pypasses 13/13 (that file arrives with feat(topk): index the page table per row so prefill can use the fused transform (2.86x) #3, which stacks on this).sgl_kernel'sfast_topk_transform_fused(mismatch=0) at every measured shape.