Skip to content

Commit 01741ab

Browse files
committed
Add tests and support for non pow2 K. Import constants from kernel module instead of redefining.
1 parent c0b6909 commit 01741ab

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
BLOCK_THREADS = 1024
6666
WARP_SIZE = 64
6767
LOAD_VEC = 4
68+
# Default histogram-scan staging (one of 1/2/4/8)
69+
SCAN_STAGES = 2
6870

6971
# 128B-spaced inter-workgroup counter groups (32 int32 == 128B each), kept in the int32 workspace.
7072
COUNTER_STRIDE = 32
@@ -116,7 +118,7 @@ def create_topk_per_row_decode_tiered_kernel(
116118
*,
117119
top_k: int,
118120
bits_per_pass: int = 11,
119-
scan_stages: int = 2,
121+
scan_stages: int = SCAN_STAGES,
120122
tiered: bool = True,
121123
tiered_short_max: int = 16384,
122124
tiered_mid_cap: int = 16,

‎aiter/ops/flydsl/topk_per_row_decode.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
from .kernels.tensor_shim import _run_compiled
1212
from .kernels.topk_per_row_decode_tiered import (
13+
BLOCK_THREADS as _TIERED_BLOCK_THREADS,
14+
LOAD_VEC as _TIERED_LOAD_VEC,
15+
SCAN_STAGES as _TIERED_SCAN_STAGES,
1316
create_topk_per_row_decode_tiered_kernel,
1417
needs_workspace_zero,
1518
topk_workspace_slots,
@@ -21,11 +24,6 @@
2124

2225
##################################
2326

24-
SUPPORTED_TOP_KS = (256, 512, 1024, 2048)
25-
_TIERED_BLOCK_THREADS = 1024
26-
_TIERED_LOAD_VEC = 4
27-
_TIERED_SCAN_STAGES = 2
28-
2927
# Independent of K: K only affects the final O(K) index scatter, negligible vs
3028
# the O(L) scan.
3129
_TIERED_MID_MAX = 65536
@@ -311,11 +309,8 @@ def _validate_inputs(
311309
raise ValueError(f"numRows={numRows} exceeds logits rows={logits.shape[0]}")
312310
if numRows > indices.shape[0]:
313311
raise ValueError(f"numRows={numRows} exceeds indices rows={indices.shape[0]}")
314-
if k not in SUPPORTED_TOP_KS:
315-
raise NotImplementedError(
316-
f"FlyDSL decode TopK only accepts compile-time k in "
317-
f"{SUPPORTED_TOP_KS}, got {k}"
318-
)
312+
if k <= 0:
313+
raise ValueError(f"k must be positive, got {k}")
319314
if ordered:
320315
raise ValueError(
321316
"FlyDSL decode TopK returns unordered set output only; "

‎op_tests/flydsl_tests/test_flydsl_topk_per_row_decode.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ def test_k2048_poison_tail_matrix(num_rows, L, dist):
198198
"nextn4-k512-short-rows8": (512, 8, 4, 4096, "random", True),
199199
"nextn4-k2048-mid-rows8": (2048, 8, 4, 32769, "random", True),
200200
"nextn2-k1024-long-rows4": (1024, 4, 2, 120003, "ties", True),
201+
# arbitrary (non-power-of-2) K -- the kernel is k-independent; tiny / odd /
202+
# >2048 K across short, long, and direct-fill (L <= k) tiers.
203+
"arbitrary-k1-long": (1, 1, 1, 120003, "random", True),
204+
"arbitrary-k7-short": (7, 1, 1, 8192, "random", True),
205+
"arbitrary-k777-long": (777, 1, 1, 120003, "random", True),
206+
"arbitrary-k3000-long": (3000, 1, 1, 120003, "random", True),
207+
"arbitrary-k3000-directfill": (3000, 1, 1, 2000, "random", True),
201208
}
202209

203210

@@ -345,10 +352,10 @@ def test_num_rows_subset_leaves_extra_untouched():
345352
("logits_dtype", "call_kwargs", "exc"),
346353
[
347354
(torch.float32, {"k": 512, "ordered": True}, ValueError),
348-
(torch.float32, {"k": 777}, NotImplementedError),
355+
(torch.float32, {"k": 0}, ValueError),
349356
(torch.float16, {"k": 512}, TypeError),
350357
],
351-
ids=["ordered-true", "unsupported-k", "fp16-logits"],
358+
ids=["ordered-true", "nonpositive-k", "fp16-logits"],
352359
)
353360
def test_invalid_args_rejected(logits_dtype, call_kwargs, exc):
354361
k = call_kwargs["k"]

0 commit comments

Comments
 (0)