Skip to content

Commit c0b6909

Browse files
committed
Update params to include gfx950. Added asm kernel to rocprof bench scripts
1 parent b3a0d97 commit c0b6909

3 files changed

Lines changed: 74 additions & 29 deletions

File tree

‎aiter/ops/flydsl/topk_per_row_decode.py‎

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@
2626
_TIERED_LOAD_VEC = 4
2727
_TIERED_SCAN_STAGES = 2
2828

29-
# Both thresholds are independent of K.
30-
# K only affects the final O(K) index scatter,
31-
# which is negligible compared to the O(L) scan.
32-
_TIERED_SHORT_MAX = 16384
29+
# Independent of K: K only affects the final O(K) index scatter, negligible vs
30+
# the O(L) scan.
3331
_TIERED_MID_MAX = 65536
3432

33+
# Batch-aware short-vs-multi-block crossover per arch for
34+
# short_max = min(cap, base + num_rows*slope).
35+
# These params were found empirically on MI300X and MI355X
36+
_SHORT_MAX_PARAMS = {
37+
# arch: (base, slope, cap)
38+
"gfx942": (16384, 1536, 40960),
39+
"gfx950": (24576, 1792, 57344),
40+
}
41+
3542

3643
def _env_int(name: str, default: int | None = None) -> int | None:
3744
value = os.environ.get(name)
@@ -44,23 +51,6 @@ def _env_int(name: str, default: int | None = None) -> int | None:
4451
raise ValueError(f"{name} must be an integer, got {value!r}") from exc
4552

4653

47-
def _default_tiered_bpp(arch: str | None = None) -> int:
48-
cu_count = _multi_processor_count(arch)
49-
lds_per_cu = SMEM_CAPACITY_MAP.get(arch, 0)
50-
return 11 if cu_count >= 128 or lds_per_cu >= 128 * 1024 else 10
51-
52-
53-
def _default_tiered_blocks_per_row(
54-
*,
55-
max_model_len: int,
56-
block_threads: int,
57-
) -> int:
58-
max_blocks = 64 if block_threads == 512 else 32
59-
items_per_block = _TIERED_LOAD_VEC * block_threads
60-
blocks = max(2, math.ceil(max_model_len / items_per_block))
61-
return min(blocks, max_blocks)
62-
63-
6454
@functools.cache
6555
def _multi_processor_count(arch: str | None = None) -> int:
6656
try:
@@ -88,11 +78,20 @@ def _default_kernel_config(
8878
num_rows: int,
8979
max_model_len: int,
9080
) -> dict:
91-
blocks_per_row = _default_tiered_blocks_per_row(
92-
max_model_len=max_model_len,
93-
block_threads=_TIERED_BLOCK_THREADS,
81+
arch = get_rocm_arch()
82+
83+
# Grid width per row: enough workgroups to cover the row at LOAD_VEC elements
84+
# per thread, clamped to [2, 32] (32 = the wg cap the mid/long tiers can use;
85+
# BLOCK_THREADS is fixed at 1024, so max_blocks is 32).
86+
items_per_block = _TIERED_LOAD_VEC * _TIERED_BLOCK_THREADS
87+
blocks_per_row = min(32, max(2, math.ceil(max_model_len / items_per_block)))
88+
89+
# bits_per_pass: 11 (2048-bin LDS histogram) whenever the arch can afford it;
90+
# the short tier requires 11. gfx942/gfx950 both qualify (CU count >= 128).
91+
cu_count = _multi_processor_count(arch)
92+
bits_per_pass = (
93+
11 if cu_count >= 128 or SMEM_CAPACITY_MAP.get(arch, 0) >= 128 * 1024 else 10
9494
)
95-
bits_per_pass = _default_tiered_bpp()
9695

9796
# Max cooperating workgroups per row for the mid/long tiers (the real wg count
9897
# is min(blocks_per_row, cap)). Scales down with batch size: a single long row
@@ -110,10 +109,11 @@ def _default_kernel_config(
110109
else: # num_rows >= 8
111110
tiered_long_cap_default = 8
112111

113-
# Batch-aware short vs multi-block crossover. The multi-block barrier
114-
# floor grows under CU contention as more rows launch, while the single
115-
# workgroup path is flat in batch.
116-
tiered_short_max = min(40960, _TIERED_SHORT_MAX + num_rows * 1536)
112+
# Batch-aware short vs multi-block crossover (arch-specific base/slope/cap). The
113+
# multi-block barrier floor grows under CU contention as more rows launch, while
114+
# the single-workgroup path is flat in batch.
115+
base, slope, cap = _SHORT_MAX_PARAMS.get(arch, _SHORT_MAX_PARAMS["gfx942"])
116+
tiered_short_max = min(cap, base + num_rows * slope)
117117

118118
return dict(
119119
blocks_per_row=blocks_per_row,
@@ -209,6 +209,13 @@ def _apply_deadlock_guard(
209209
# barrier-free short tier. Requires bits_per_pass == 11 (the short tier);
210210
# target archs (gfx942/gfx950) satisfy this.
211211
kernel_config["tiered_short_max"] = max_model_len
212+
213+
# Keep the mid_max >= short_max validator happy when max_model_len exceeds
214+
# the default mid_max. The tiered kernel builder rejects short_max > mid_max
215+
# at compile time.
216+
kernel_config["tiered_mid_max"] = max(
217+
kernel_config["tiered_mid_max"], max_model_len
218+
)
212219
return kernel_config
213220

214221

‎op_tests/topk_decode_rocprof_driver.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@
3131
"aiter_hip": re.compile(
3232
r"radix_topk_one_block_kernel|radix_kernel|last_filter_kernel"
3333
),
34+
"aiter_asm": re.compile(r"aiter::DecodeTopK::topk_per_row_decode"),
3435
"vllm": re.compile(r"topKPerRowDecode"),
3536
}
3637

38+
# Kernels that only support a fixed K (skip other K in the sweep). The gfx942 asm
39+
# .co is specialized for K=2048 and takes no k argument.
40+
FIXED_K = {"aiter_asm": 2048}
41+
3742
KERNEL_ENV = {
3843
"flydsl_tiered": {
3944
"FLYDSL_TOPK_TIERED": "1",
@@ -239,6 +244,16 @@ def main() -> None:
239244
results: dict[tuple[str, int, int], tuple[float, float, int, str]] = {}
240245
for kernel in args.kernels:
241246
for k in args.k:
247+
if kernel in FIXED_K and k != FIXED_K[kernel]:
248+
# Fixed-K kernel (e.g. asm) doesn't support this K -> record na.
249+
for L in args.L:
250+
results[(kernel, k, L)] = (
251+
float("nan"),
252+
float("nan"),
253+
0,
254+
f"na ({kernel}: K={FIXED_K[kernel]} only)",
255+
)
256+
continue
242257
for L in args.L:
243258
res = run_cell(
244259
kernel,

‎op_tests/topk_decode_rocprof_runner.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* flydsl -> FlyDSL standalone unordered radix-select path (K=2048)
1111
* flydsl_tiered -> K=512 persistent path with in-kernel tier dispatch
1212
* aiter_hip -> aiter.top_k_per_row_decode (HIP radix one/multi-block)
13+
* aiter_asm -> aiter.top_k_per_row_decode_fast (gfx942 asm .co, K=2048 only)
1314
* vllm -> torch.ops._C.top_k_per_row_decode (vLLM sampler.cu kernel)
1415
1516
The vLLM op is registered by loading the prebuilt stable-libtorch .so directly,
@@ -77,6 +78,12 @@ def load_aiter():
7778
return aiter.top_k_per_row_decode
7879

7980

81+
def load_aiter_asm():
82+
import aiter
83+
84+
return aiter.top_k_per_row_decode_fast
85+
86+
8087
def main() -> None:
8188
ap = argparse.ArgumentParser()
8289
ap.add_argument(
@@ -86,6 +93,7 @@ def main() -> None:
8693
"flydsl",
8794
"flydsl_tiered",
8895
"aiter_hip",
96+
"aiter_asm",
8997
"vllm",
9098
],
9199
)
@@ -165,6 +173,21 @@ def run():
165173
k=args.k,
166174
)
167175

176+
elif args.kernel == "aiter_asm":
177+
# gfx942 asm .co: fixed K=2048, no k argument.
178+
op = load_aiter_asm()
179+
180+
def run():
181+
op(
182+
logits,
183+
args.next_n,
184+
seq_lens,
185+
indices,
186+
args.num_rows,
187+
stride0,
188+
stride1,
189+
)
190+
168191
else: # FlyDSL variants
169192
op = load_flydsl()
170193

0 commit comments

Comments
 (0)