Skip to content

Commit a87146d

Browse files
Port gfx950 kernel B1: row_proportional_parts
Cap each row's cooperating workgroups by its actual coverage need (ceil(row_len / items_per_block), floored at 2) in the mid/long tiers, undoing the pow2 round-up of blocks_per_row for rows whose coverage is not a power of two. Only reduces active parts (a min), so results are identical. Kernel: add row_proportional_parts param + _rpp name tag + the row_cover cap after mid/long_parts. Dispatch: emit row_proportional_parts in the config (gfx950 default on, env FLYDSL_TOPK_TIERED_RPP override, gfx942 frozen), passed through the existing **kernel_config spread. Verified: correctness lossless (random/ties/10LSBits x rows{8,32} x L{49k,120k,256k} all PASS); perf rows=1 L=49152 22.44->22.20us (-1.1%, parts 16->12), L=98304 28.64->27.84us (-2.8%, 32->24); null cell unchanged. blocks_per_row unchanged; gfx942 frozen. Co-authored-by: Cursor <[email protected]>
1 parent 2fc5ddc commit a87146d

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎aiter/ops/flydsl/kernels/topk_per_row_decode_tiered.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def create_topk_per_row_decode_tiered_kernel(
137137
tiered_mid_max: int = 65536,
138138
tiered_long_cap: int = 32,
139139
mask_non_finite: bool = True,
140+
row_proportional_parts: bool = False,
140141
) -> Any:
141142
"""Build a launcher that selects the Top-K largest values' column indices per
142143
decode row (unordered set, matching torch.topk by value). Implemented as a
@@ -218,6 +219,7 @@ def create_topk_per_row_decode_tiered_kernel(
218219
f"{_cap_tag}"
219220
f"{'_1wg' if short_tier else ''}"
220221
f"{'_mf' if mask_non_finite else ''}"
222+
f"{'_rpp' if row_proportional_parts else ''}"
221223
)
222224

223225
@fx.struct
@@ -308,6 +310,28 @@ def topk_per_row_decode_tiered_kernel(
308310
long_parts = (ArithValue(c_parts) < ArithValue(c_long_cap)).select(
309311
c_parts, c_long_cap
310312
)
313+
if const_expr(row_proportional_parts):
314+
# gfx950: the launch grid (c_parts) is sized for the padded buffer width,
315+
# so a short row would otherwise spin up far more cooperating workgroups
316+
# than it needs -- each extra part only adds cross-CU barrier/merge latency
317+
# (the mid/long floor). Cap participating workgroups by the row's actual
318+
# coverage need: one part per items_per_block (LOAD_VEC*BLOCK_THREADS)
319+
# elements, floored at 2 so the persistent tier still has >1 workgroup.
320+
# Fewer parts is always correct (the scan stride covers all vec-blocks).
321+
items_per_block = LOAD_VEC * block_threads
322+
cover_shift = (items_per_block).bit_length() - 1
323+
row_cover = ArithValue(
324+
ArithValue(row_len) + ArithValue(fx.Int32(items_per_block - 1))
325+
).shrui(fx.Int32(cover_shift))
326+
row_cover = (ArithValue(row_cover) < ArithValue(c_two)).select(
327+
c_two, row_cover
328+
)
329+
mid_parts = (ArithValue(mid_parts) < ArithValue(row_cover)).select(
330+
mid_parts, row_cover
331+
)
332+
long_parts = (ArithValue(long_parts) < ArithValue(row_cover)).select(
333+
long_parts, row_cover
334+
)
311335
if const_expr(tier_mode == "short"):
312336
active_parts = c_one
313337
elif const_expr(tier_mode == "mid"):

‎aiter/ops/flydsl/topk_per_row_decode.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ def _default_kernel_config(
167167
if mb_cap:
168168
blocks_per_row = max(2, min(blocks_per_row, mb_cap))
169169

170+
# Row-proportional parts (gfx950 only). The launch grid width is sized for the
171+
# padded buffer, so short rows over-provision cooperating workgroups; the kernel
172+
# caps participating parts by each row's actual coverage need (a min, so results
173+
# are unchanged). Env FLYDSL_TOPK_TIERED_RPP (0/1) overrides; gfx942 frozen (0).
174+
rpp_on = _env_int("FLYDSL_TOPK_TIERED_RPP", 1 if arch == "gfx950" else 0)
175+
170176
return dict(
171177
blocks_per_row=blocks_per_row,
172178
bits_per_pass=bits_per_pass,
@@ -177,6 +183,7 @@ def _default_kernel_config(
177183
tiered_long_cap=tiered_long_cap_default,
178184
mask_non_finite=True,
179185
tier_mode="auto",
186+
row_proportional_parts=bool(rpp_on),
180187
)
181188

182189

0 commit comments

Comments
 (0)