Repository navigation
[FlyDSL] Add flydsl radix topk one block kernel - #5363
Merged
Merged
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
coderfeli
reviewed
Sep 9, 2026
lirui927
force-pushed
the
lirui/flydsl_topk_gfx1250
branch
from
September 10, 2026 02:44
9c21608 to
b431360
Compare
lirui927
marked this pull request as draft
September 10, 2026 08:30
lirui927
marked this pull request as ready for review
September 10, 2026 14:59
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
The six TopK kernel modules were loose in kernels/ alongside unrelated families. Move them into kernels/topk/ and update the five importers. Co-Authored-By: Claude Opus 5 <[email protected]>
lirui927
force-pushed
the
lirui/flydsl_topk_gfx1250
branch
from
September 15, 2026 07:50
3bb05d9 to
2628565
Compare
Mirrors the kernels/topk/ grouping: the three TopK launch wrappers move out of flydsl/ into flydsl/topk/, and the ten importers follow. Co-Authored-By: Claude Opus 5 <[email protected]>
…radix. Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
lirui927
marked this pull request as ready for review
September 15, 2026 11:17
Co-authored-by: Cursor <[email protected]> # Conflicts: # aiter/ops/flydsl/topk/topk_per_row_argmax.py # aiter/ops/flydsl/topk/topk_per_row_small_k.py # aiter/ops/topk_select.py
…ule. Co-authored-by: Cursor <[email protected]>
None of this changes what the kernel selects. - stable_stage_capacity was an alias for stable_sort_capacity. - The candidate column array is written and read only under `not stable or stable_sort_enabled`, so the other configuration no longer pays for it: the entry size drops to the key alone, which doubles the candidate buffer at stable k > 2048 (9458 -> 18917 on gfx950, 16106 -> 32213 on gfx1250) and pushes back the global rescan. - merge_histograms folded a lone replica into itself, costing a pass over the bins plus a barrier on every row of the gfx950/gfx1250 long path. - accumulate_prefix_histogram re-tested a bound its callers already hold, and the column it took for that test is now unused. - choose_threshold took the one metadata slot and the one scan storage that all six call sites pass. - write_direct_scalar is only reached at k <= block_threads, so its outer loop always ran once. - The decode row bounds are _row_length, already imported here. - The three radix levels have to cover the whole key; say so where the table is read, and name the LDS budget by its table rather than by the one architecture that first had an entry. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The inline transform matched _f32_to_ord bit for bit except that it left NaN alone, so a negative NaN sorted below -inf instead of above +inf. Which selector serves a call is a shape decision -- inside top_k_per_row_decode, a shape in the _FLYDSL_TOPK_DECODE_GATES bands goes to the multi-block kernel and a shape outside them lands here -- so the same row answered differently depending on a choice the caller never made. That is the case ord_signed_f32 exists to rule out, and it folds -0.0 onto +0.0 as well, so the stable contract's smallest-index tie-break now holds for signed zeros. On a row carrying both NaN signs the one-block kernel now selects the same columns as the multi-block kernel and as torch.topk. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
get_gfx() honours GPU_ARCHS, which is what build-time codegen wants and the opposite of what this dispatch wants: the arch chooses the wave size, the LDS budget and the radix widths, so a GPU_ARCHS that names another card builds a kernel the device cannot match -- get_warp_size spells out that a wrong wave size silently drops the upper lanes' work. The multi-block launcher next door already reads the device. With GPU_ARCHS=gfx1250 on a gfx950 the one-block path picked wave32 and a 158K budget; it now picks gfx950 and wave64. Also drop the stable argument the predicate discarded and the row_starts branch its only caller cannot reach. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Both sweeps recorded all_close into the summary table and nothing read it back, so a kernel that selected the wrong columns printed False in a markdown row and still exited 0. Assert on the column instead, with the table as the message. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
One flag gated both sweeps, so widening it for the one-block prefill kernel also started driving the multi-block decode kernel on gfx1250 -- an arch _FLYDSL_TOPK_DECODE_GATES has no entry for, so production never dispatches it there. Ask each path for its own list instead of keeping a third copy in the test, and the two sweeps stop moving together. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The one-block kernel measured no clear win on gfx942, and it has no _ONE_BLOCK_LDS_BUDGET_BYTES entry, so its candidate buffer sits at the 4096 compact floor and overflows into the four-pass global rescan far sooner than on the two arches that do. Prefill and decode there fall back to HIP the way they did before this PR; the sweeps follow the list on their own. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Collaborator
|
LGTM |
…selectors." This reverts commit b1e27f4.
valarLip
approved these changes
Sep 17, 2026
JH-Leon-KIM-AMD
added a commit
that referenced
this pull request
Sep 20, 2026
The table is keyed by (arch, CU count) and had one entry, so MI355X kept the upstream path. This adds its entry, fitted from its own 1000-cell sweep rather than reused from the other card: the two share no number, and the shape of the result differs because the fallback does. gfx950 is in _FLYDSL_TOPK_ONE_BLOCK_ARCHES, so a declined cell here runs #5363's FlyDSL port, which wins 554 of the 1000 cells outright and is a much harder baseline than the C++ kernel gfx942 falls back to. Measured on MI355X (gfx950, 256 CU) over 1000 cells covering k 256..4096, width 4K..1M and 1..512 rows, in the same three passes as gfx942. Scoring every cell rather than the ones the gate takes, and picking each declined cell's baseline out of upstream's own arch-keyed chunked gate, the table returns 1.133x geomean with no cell behind, at slack 1.00. Loosening slack to 1.10 does not raise that number at all on this card and puts 87 cells more than 3% behind, so 1.00 stands for a sharper reason here than it did there. k=256 carries the result: _FLYDSL_TOPK_DECODE_KS stops at 512, so upstream never runs its chunked path there and all 200 of those cells fall back to the port. Over the 67 the table takes it is 2.077x, against 1.28x to 1.31x for every other k group. Run without the table the kernel is 0.896x of the per-cell oracle with 349 cells more than 10% off the best, so the result is the gate's, not the kernel's. The routing check runs both sides of all 91 band edges of this entry on the card and confirms the kernel the table names is the one that launches, 91/91. Co-Authored-By: Claude Opus 5 <[email protected]>
JH-Leon-KIM-AMD
added a commit
that referenced
this pull request
Sep 20, 2026
The table is keyed by (arch, CU count) and had one entry, so MI355X kept the upstream path. This adds its entry, fitted from its own 1000-cell sweep rather than reused from the other card: the two share no number, and the shape of the result differs because the fallback does. gfx950 is in _FLYDSL_TOPK_ONE_BLOCK_ARCHES, so a declined cell here runs #5363's FlyDSL port, which wins 554 of the 1000 cells outright and is a much harder baseline than the C++ kernel gfx942 falls back to. Measured on MI355X (gfx950, 256 CU) over 1000 cells covering k 256..4096, width 4K..1M and 1..512 rows, in the same three passes as gfx942. Scoring every cell rather than the ones the gate takes, and picking each declined cell's baseline out of upstream's own arch-keyed chunked gate, the table returns 1.133x geomean with no cell behind, at slack 1.00. Loosening slack to 1.10 does not raise that number at all on this card and puts 87 cells more than 3% behind, so 1.00 stands for a sharper reason here than it did there. k=256 carries the result: _FLYDSL_TOPK_DECODE_KS stops at 512, so upstream never runs its chunked path there and all 200 of those cells fall back to the port. Over the 67 the table takes it is 2.077x, against 1.28x to 1.31x for every other k group. Run without the table the kernel is 0.896x of the per-cell oracle with 349 cells more than 10% off the best, so the result is the gate's, not the kernel's. The routing check runs both sides of all 91 band edges of this entry on the card and confirms the kernel the table names is the one that launches, 91/91. Co-Authored-By: Claude Opus 5 <[email protected]>
JH-Leon-KIM-AMD
added a commit
that referenced
this pull request
Sep 20, 2026
The table is keyed by (arch, CU count) and had one entry, so MI355X kept the upstream path. This adds its entry, fitted from its own 1000-cell sweep rather than reused from the other card: the two share no number, and the shape of the result differs because the fallback does. gfx950 is in _FLYDSL_TOPK_ONE_BLOCK_ARCHES, so a declined cell here runs #5363's FlyDSL port, which wins 554 of the 1000 cells outright and is a much harder baseline than the C++ kernel gfx942 falls back to. Measured on MI355X (gfx950, 256 CU) over 1000 cells covering k 256..4096, width 4K..1M and 1..512 rows, in the same three passes as gfx942. Scoring every cell rather than the ones the gate takes, and picking each declined cell's baseline out of upstream's own arch-keyed chunked gate, the table returns 1.133x geomean with no cell behind, at slack 1.00. Loosening slack to 1.10 does not raise that number at all on this card and puts 87 cells more than 3% behind, so 1.00 stands for a sharper reason here than it did there. k=256 carries the result: _FLYDSL_TOPK_DECODE_KS stops at 512, so upstream never runs its chunked path there and all 200 of those cells fall back to the port. Over the 67 the table takes it is 2.077x, against 1.28x to 1.31x for every other k group. Run without the table the kernel is 0.896x of the per-cell oracle with 349 cells more than 10% off the best, so the result is the gate's, not the kernel's. The routing check runs both sides of all 91 band edges of this entry on the card and confirms the kernel the table names is the one that launches, 91/91.
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.
Motivation
This PR adds a FlyDSL one-block radix per-row TopK kernel and wires it into the default dispatch on gfx950, and gfx1250, replacing the HIP one-block path in those cases.
Goals:
row_starts/row_ends) and decode (seq_lens/next_n) share the same kernel.k, optional values output, and deterministic stable mode.klive elements, pad indices with-1and values with-inf.Implementation
One-block radix TopK
New file:
aiter/ops/flydsl/kernels/topk/radix_topk_one_block.py.<= 4096) cache sorted keys in LDS. Long rows use a radix histogram plus candidate compaction, and fall back to a global-memory rescan if candidates overflow LDS.row_len <= k, skip radix selection and copy/pad the output directly.rows <= 256, otherwise 1024.Dispatch
_is_flydsl_radix_topk_one_block_supportedreturns false._FLYDSL_TOPK_ONE_BLOCK_ARCHES = ("gfx950", "gfx1250").logits.shape[1]and the row count, not device-side row bounds, so it is CUDAGraph-safe.topk_use_mulblocksmulti-block path or per-arch HIP fallbacks.Layout cleanup
TopK files are grouped into their own subpackages. In this PR these moves are renames only:
aiter/ops/flydsl/kernels/topk/— six kernel modules.aiter/ops/flydsl/topk/— three launch wrappers.Optimizations for concentrated value distributions
Real logits often span a narrow range, so radix bins on float bits collapse onto a few buckets. That causes atomic contention on the first-pass histogram, and more first-pass survivors than the LDS candidate buffer can hold. This PR addresses both:
These changes affect performance only; output semantics are unchanged.
Correctness
op_tests/test_topk_per_row.pynow calls the one-block kernel directly (bypassing dispatch). On supported arches, each prefill shape also runs the four stable × write_values combinations, so dispatch changes cannot hide a kernel regression.Coverage:
next_n=1and MTP decode.How to test
Performance
Method
GFX1250
Conclusion: default FlyDSL one-block is faster overall, with no systematic regression.
775 cases, geomean 1.41×; 731 faster, 44 slower.
Representative points (k=2048, stable+values, full rows):
Of the 44 slower cases, 42 differ by at most 5 µs; the median relative regression is 3.3%. The remaining 2 cases regress by 6.6%–6.9%.
GFX950
Conclusion: default FlyDSL one-block is faster overall, with no systematic regression.
775 cases, geomean 1.44×; 741 faster, 34 slower (worst about 0.88×, ~5.5 µs slower).
Representative points (k=2048, stable+values, full rows):
Slower cases are mostly large batches that also write values; most are only a few percent slower, typically about 5 µs.
Checklist