Skip to content

Commit d839939

Browse files
Port gfx950 B3: early_stop (skip final radix pass, num_rows<=1)
Kernel (topk_per_row_decode_tiered.py): - add early_stop param + _es kernel-name tag (cache separation) - add early-stop write helpers (process_loaded_early_vec / early_write_vec_block / early_write_all): take the boundary bucket whole via prefix_for_key(key, prev_start_bit) <= kth_bits (HIP-mb previous_bits <= kth_value_bits), same 4x-staged unroll as the normal last-pass write - persistent loop: when early_stop, run passes[0..n-2], and if the boundary bucket is taken whole (local_len == local_k) skip the final radix pass and write directly; else run it. The early flag is computed from the merged histogram so it is identical across a row's blocks -> the final row_barrier is entered/skipped by all blocks together (no deadlock). Dispatch (topk_per_row_decode.py): - es_on = _env_int(FLYDSL_TOPK_TIERED_ES, 1 if gfx950 else 0) - early_stop = bool(es_on) and num_rows <= 1 (gated: pays off only in the single-sequence long tier; neutral/loss at rows>=2) Verified gfx950: correctness 768/768 vs torch.topk (ES on/off x regime A+B x rows{1,4,16,32} x L{8k,65k,120k,256k} x k{256..2048} x random/ties/10LSBits, poison over-scan checked). Perf (rocprofv3 min): rows=1 -14..17% (L120k k2048 29.76->25.68us), rows=4 (gated off) 0 regression. Co-authored-by: Cursor <[email protected]>
1 parent 6f7042a commit d839939

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

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

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def create_topk_per_row_decode_tiered_kernel(
138138
tiered_long_cap: int = 32,
139139
mask_non_finite: bool = True,
140140
row_proportional_parts: bool = False,
141+
early_stop: bool = False,
141142
) -> Any:
142143
"""Build a launcher that selects the Top-K largest values' column indices per
143144
decode row (unordered set, matching torch.topk by value). Implemented as a
@@ -220,6 +221,7 @@ def create_topk_per_row_decode_tiered_kernel(
220221
f"{'_1wg' if short_tier else ''}"
221222
f"{'_mf' if mask_non_finite else ''}"
222223
f"{'_rpp' if row_proportional_parts else ''}"
224+
f"{'_es' if early_stop else ''}"
223225
)
224226

225227
@fx.struct
@@ -737,6 +739,69 @@ def write_vec_block(vblk, local_k, kth_bits):
737739
col_base, load_row_vec(col_base), local_k, kth_bits
738740
)
739741

742+
def process_loaded_early_vec(col_base, vec, previous_start_bit: int, kth_bits):
743+
# Early-stop write: the boundary bucket after the previous pass is taken
744+
# whole (remaining_len == remaining_k), so every element whose prefix at
745+
# the previous resolution is <= the boundary prefix is in the top-k. No
746+
# tie-break needed. Mirrors HIP mb `previous_bits <= kth_value_bits`.
747+
for j in range_constexpr(LOAD_VEC):
748+
col_i32 = ArithValue(col_base) + ArithValue(fx.Int32(j))
749+
if ArithValue(col_i32) < ArithValue(row_len):
750+
val = vector.extract(vec, static_position=[j], dynamic_position=[])
751+
key = radix_twiddle_key(val)
752+
prefix = prefix_for_key(key, previous_start_bit)
753+
if arith.cmpi(arith.CmpIPredicate.ule, prefix, kth_bits):
754+
pos = global_atomic_add_i32(
755+
counter_slot(COUNTER_OUT_FRONT), c_one
756+
)
757+
if ArithValue(pos) < ArithValue(c_top_k):
758+
buffer_ops.buffer_store(
759+
col_i32, indices_rsrc, row_out + ArithValue(pos)
760+
)
761+
762+
def early_write_vec_block(vblk, previous_start_bit: int, kth_bits):
763+
col_base = ArithValue(arith.index_cast(T.i32, vblk)) * ArithValue(c_vec)
764+
process_loaded_early_vec(
765+
col_base, load_row_vec(col_base), previous_start_bit, kth_bits
766+
)
767+
768+
def early_write_all(previous_start_bit: int, kth_bits):
769+
# Same 4x-staged unroll as the normal last-pass write so the early-stop
770+
# row re-scan is not slower than the pass it replaces.
771+
unroll_limit_idx = ArithValue(vec_blocks_idx) - ArithValue(
772+
active_stride3_idx
773+
)
774+
for vblk, write_state in range(
775+
ArithValue(global_vec_tid_idx),
776+
unroll_limit_idx,
777+
ArithValue(active_stride4_idx),
778+
init=[global_vec_tid_idx],
779+
):
780+
for unroll_id in range_constexpr(4):
781+
early_write_vec_block(
782+
ArithValue(vblk)
783+
+ ArithValue(
784+
arith.index_cast(
785+
T.index,
786+
ArithValue(active_threads)
787+
* ArithValue(fx.Int32(unroll_id)),
788+
)
789+
),
790+
previous_start_bit,
791+
kth_bits,
792+
)
793+
write_results = yield [
794+
ArithValue(vblk) + ArithValue(active_stride4_idx)
795+
]
796+
for vblk, write_state in range(
797+
write_results,
798+
ArithValue(vec_blocks_idx),
799+
ArithValue(active_stride_idx),
800+
init=[c_zero],
801+
):
802+
early_write_vec_block(vblk, previous_start_bit, kth_bits)
803+
write_results = yield [write_state[0]]
804+
740805
global_vec_tid = part * ArithValue(c_block_i32) + tid
741806
global_vec_tid_idx = arith.index_cast(T.index, global_vec_tid)
742807
vec_blocks_i32 = ArithValue(
@@ -1160,11 +1225,33 @@ def reread_pass(chunk_fn):
11601225

11611226
if persistent_active:
11621227
local_k = c_top_k
1228+
local_len = row_len
11631229
kth_bits = c_zero
1164-
for pass_id in range_constexpr(num_passes):
1165-
local_k, _local_len, kth_bits = scan_pass(
1166-
pass_id, local_k, kth_bits, pass_id + 1
1167-
)
1230+
if early_stop:
1231+
# HIP-mb-style early termination: run every pass but the last (none of
1232+
# these write), then skip the final radix pass when the boundary bucket
1233+
# is taken whole (remaining_len == remaining_k) and write those elements
1234+
# directly. `early` is computed from the globally merged histogram, so it
1235+
# is identical across every block of the row -> the last pass's row_barrier
1236+
# is entered (or skipped) by all blocks together, no deadlock.
1237+
for pass_id in range_constexpr(num_passes - 1):
1238+
local_k, local_len, kth_bits = scan_pass(
1239+
pass_id, local_k, kth_bits, pass_id + 1
1240+
)
1241+
last_pass = num_passes - 1
1242+
prev_start_bit = max(32 - last_pass * bits_per_pass, 0)
1243+
early = ArithValue(local_len) == ArithValue(local_k)
1244+
if early:
1245+
early_write_all(prev_start_bit, kth_bits)
1246+
if ~early:
1247+
local_k, local_len, kth_bits = scan_pass(
1248+
last_pass, local_k, kth_bits, last_pass + 1
1249+
)
1250+
else:
1251+
for pass_id in range_constexpr(num_passes):
1252+
local_k, local_len, kth_bits = scan_pass(
1253+
pass_id, local_k, kth_bits, pass_id + 1
1254+
)
11681255

11691256
@flyc.jit
11701257
def launcher(

‎aiter/ops/flydsl/topk_per_row_decode.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ def _default_kernel_config(
226226
# are unchanged). Env FLYDSL_TOPK_TIERED_RPP (0/1) overrides; gfx942 frozen (0).
227227
rpp_on = _env_int("FLYDSL_TOPK_TIERED_RPP", 1 if arch == "gfx950" else 0)
228228

229+
# Early-stop (gfx950 only). Skips the final radix pass when the boundary bucket
230+
# is taken whole; only enabled for the single-row (num_rows <= 1) decode case,
231+
# where the merged histogram / row_barrier are cheapest and the win is cleanest.
232+
# Env FLYDSL_TOPK_TIERED_ES (0/1) overrides; gfx942 frozen (0).
233+
es_on = _env_int("FLYDSL_TOPK_TIERED_ES", 1 if arch == "gfx950" else 0)
234+
229235
return dict(
230236
blocks_per_row=blocks_per_row,
231237
bits_per_pass=bits_per_pass,
@@ -237,6 +243,7 @@ def _default_kernel_config(
237243
mask_non_finite=True,
238244
tier_mode="auto",
239245
row_proportional_parts=bool(rpp_on),
246+
early_stop=bool(es_on) and num_rows <= 1,
240247
)
241248

242249

0 commit comments

Comments
 (0)