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
3643def _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
6555def _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
0 commit comments