Skip to content

Commit 197eda0

Browse files
Port gfx950 dispatch B2: batch_coresident_cap (grid=1 fold)
Cap the launch grid width so blocks_per_row*num_rows stays within one co-resident wave (envelope = CU*occ), so the persistent barrier does not serialize rows into sequential waves. When even a width-2 grid overflows (budget<2), collapse to grid=(1, num_rows): every row runs the barrier-free single-workgroup short tier (needs the B0 bpr==1 kernel path). Also fold the all-short case (max_model_len<=short_max) to grid=1. - Add _CDNA_OCCUPANCY / _COCAP_OCC2_MAX_ROWS (occ=2 up to 32 rows, else 1). - Make tiered_mid_max a local so the fold can raise it with short_max (kernel requires mid_max >= short_max when force_single_wg lifts short_max to L). - gfx950 only (env FLYDSL_TOPK_TIERED_BATCH_CAP / _OCC override); gfx942 frozen. Verified: correctness lossless (rows{64,128,256} x L{8k,65k,120k} x {random,ties} all PASS); perf rows=256 L=120000 364.11->101.52us (-72%), rows=128 L=65536 117.56->78.56us (-33%); folded config builds; gfx942 frozen (arch-patch check). Co-authored-by: Cursor <[email protected]>
1 parent a87146d commit 197eda0

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

‎aiter/ops/flydsl/topk_per_row_decode.py‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
"gfx950": (18432, 1536, 40960),
3838
}
3939

40+
# Co-resident scheduling envelope = CU_count * occupancy. Occupancy is 2 on CDNA
41+
# (1024-thread wave-limited block). Beyond _COCAP_OCC2_MAX_ROWS rows the 512-wg
42+
# envelope spills into a 2nd wave, so the batch cap switches to occ=1 (one true wave).
43+
_CDNA_OCCUPANCY = 2
44+
_COCAP_OCC2_MAX_ROWS = 32
4045

4146
def _next_pow2(n: int) -> int:
4247
if n <= 1:
@@ -127,6 +132,11 @@ def _default_kernel_config(
127132
short_max_rows = _next_pow2(num_rows)
128133
tiered_short_max = min(cap, base + short_max_rows * slope)
129134

135+
# Local copy so the grid=1 fold below can raise it to keep mid_max >= short_max
136+
# (kernel validation requires it when force_single_wg lifts short_max to L).
137+
tiered_mid_max = _TIERED_MID_MAX
138+
139+
130140
# Dead-block trim (gfx950 only). The launch grid is (blocks_per_row, num_rows) but
131141
# the real workers per row = min(blocks_per_row, tier_cap); the excess workgroups
132142
# return immediately yet still occupy co-resident slots ("dead" blocks). Trimming
@@ -138,12 +148,55 @@ def _default_kernel_config(
138148
# fold requires the kernel's single-workgroup launch path). Env
139149
# FLYDSL_TOPK_TIERED_TRIM (0/1) overrides; gfx942 is untouched (default 0).
140150
trim_on = _env_int("FLYDSL_TOPK_TIERED_TRIM", 1 if arch == "gfx950" else 0)
141-
if trim_on and max_model_len > tiered_short_max and num_rows * 32 <= cu_count * 2:
142-
if max_model_len <= _TIERED_MID_MAX:
143-
max_active_parts = tiered_mid_cap_default
144-
else:
145-
max_active_parts = max(tiered_mid_cap_default, tiered_long_cap_default)
146-
blocks_per_row = max(2, min(blocks_per_row, max_active_parts))
151+
if trim_on:
152+
if max_model_len <= tiered_short_max and bits_per_pass == 11:
153+
# All rows short-tier (active_parts=1): every block but one is dead and the
154+
# barrier-free single-wg tier has nothing for the extras to hide latency for
155+
# -> collapse to grid=1 (HIP one-block shape). Needs the kernel bpr==1 path.
156+
blocks_per_row = 1
157+
elif (
158+
max_model_len > tiered_short_max
159+
and num_rows * 32 <= cu_count * _CDNA_OCCUPANCY
160+
):
161+
if max_model_len <= _TIERED_MID_MAX:
162+
max_active_parts = tiered_mid_cap_default
163+
else:
164+
max_active_parts = max(tiered_mid_cap_default, tiered_long_cap_default)
165+
blocks_per_row = max(2, min(blocks_per_row, max_active_parts))
166+
167+
# Batch co-resident grid-width cap (gfx950 only). Keep blocks_per_row*num_rows within
168+
# one co-resident wave (envelope = CU*occ) so the persistent barrier does not serialize
169+
# rows into sequential waves. Env FLYDSL_TOPK_TIERED_BATCH_CAP (0/1) overrides; gfx942
170+
# frozen (default 0).
171+
force_single_wg = False
172+
batch_cap_on = _env_int("FLYDSL_TOPK_TIERED_BATCH_CAP", 1 if arch == "gfx950" else 0)
173+
if batch_cap_on and num_rows > 1:
174+
# occ=2 co-resides for modest grids, but beyond _COCAP_OCC2_MAX_ROWS the envelope
175+
# spills into a 2nd wave, so occ=1 (one true wave) is faster. Env
176+
# FLYDSL_TOPK_TIERED_OCC forces occ.
177+
occ = _env_int("FLYDSL_TOPK_TIERED_OCC")
178+
if not occ:
179+
occ = _CDNA_OCCUPANCY if num_rows <= _COCAP_OCC2_MAX_ROWS else 1
180+
envelope = cu_count * occ
181+
budget = envelope // num_rows
182+
if budget >= 2:
183+
blocks_per_row = min(blocks_per_row, budget)
184+
elif bits_per_pass == 11:
185+
# budget<2: even a width-2 grid can't fit the batch in one wave. The padded
186+
# grid would launch (blocks_per_row-1)*num_rows dead blocks that occupy
187+
# co-resident slots and serialize the real workers -> catastrophic latency.
188+
# Collapse to grid=(1, num_rows): every row runs the barrier-free single-wg
189+
# short tier (HIP one-block shape). Needs the kernel bpr==1 path.
190+
blocks_per_row = 1
191+
force_single_wg = True
192+
193+
if force_single_wg:
194+
# grid=(1, num_rows): active_parts=1 for every row regardless of length. Make the
195+
# all-short intent explicit so needs_workspace_zero() stays False and the deadlock
196+
# guard early-outs; also skips the mid-batch cap below (tiered_short_max >= L). The
197+
# kernel requires mid_max >= short_max, so raise both together.
198+
tiered_short_max = max(tiered_short_max, max_model_len)
199+
tiered_mid_max = max(tiered_mid_max, tiered_short_max)
147200

148201
# Mid-batch coordination cap (gfx950 only). Once the batch alone fills the device
149202
# (num_rows > 16), the co-resident budget over-provisions blocks_per_row; the measured
@@ -179,7 +232,7 @@ def _default_kernel_config(
179232
scan_stages=_TIERED_SCAN_STAGES,
180233
tiered_short_max=tiered_short_max,
181234
tiered_mid_cap=tiered_mid_cap_default,
182-
tiered_mid_max=_TIERED_MID_MAX,
235+
tiered_mid_max=tiered_mid_max,
183236
tiered_long_cap=tiered_long_cap_default,
184237
mask_non_finite=True,
185238
tier_mode="auto",

0 commit comments

Comments
 (0)