Repository navigation
fix(fp4_gemm autotune) SM107 kernel family excluded from autotune candidates and heuristics update #4856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(fp4_gemm autotune) SM107 kernel family excluded from autotune candidates and heuristics update #4856
Changes from all commits
edf3d56
6dc8da2
2259d8e
6d50b57
58ce04d
bd1ec91
66072c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,33 @@ | |
| _SM100_MM_FP4_TACTIC_CACHE: dict[tuple, dict] = {} | ||
|
|
||
| # M bucket boundaries — powers of 2 for fast bucketing via | ||
| # next_positive_power_of_2 (imported from flashinfer.utils). | ||
| _M_BUCKETS = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096) | ||
| # next_positive_power_of_2 (imported from flashinfer.utils). Precompute through | ||
| # the largest profiled prefill shape so those decisions stay off the inference | ||
| # hot path, then clamp larger shapes to the final bucket. | ||
| _M_BUCKETS = ( | ||
| 1, | ||
| 2, | ||
| 4, | ||
| 8, | ||
| 16, | ||
| 32, | ||
| 64, | ||
| 128, | ||
| 256, | ||
| 512, | ||
| 1024, | ||
| 2048, | ||
| 4096, | ||
| 8192, | ||
| 16384, | ||
| 32768, | ||
| ) | ||
|
|
||
| # The matched SM100/SM107 microbenchmarks show that the old narrow-M | ||
| # orientation rule is no longer predictive above M=8192. Large-M preference | ||
| # also varies with N and cache state, so leave both orientations unpenalized | ||
| # there instead of imposing a new swap_ab=True rule. | ||
| _MM_FP4_SWAP_PENALTY_MAX_M = 8192 | ||
|
|
||
|
|
||
| _SM100_CLUSTER_SHAPE_MN_CANDIDATES = [ | ||
|
|
@@ -56,7 +81,7 @@ def _compute_tactic_for_m(rep_m, n, real_k, sm_count, sf_vec_size): | |
| """Compute the best tactic for a specific (M, N, K) on a GPU with sm_count SMs. | ||
|
|
||
| Used for mm_fp4(backend='cute-dsl') without autotune by taking the | ||
| argmax of _score_sm100_mm_fp4_tactic over kernel-feasible | ||
| argmax of _score_mm_fp4_tactic over kernel-feasible | ||
| (tile, cluster, swap_ab), with prefetch disabled. | ||
| """ | ||
| import cutlass | ||
|
|
@@ -92,7 +117,7 @@ def is_feasible(tile, cluster, swap_ab): | |
| continue # 2-CTA MMA (tile_m == 256) requires cluster_m >= 2 | ||
| # swap_ab is only valid for 8-aligned n | ||
| for swap_ab in (False,) if not n_aligned else (False, True): | ||
| score = _score_sm100_mm_fp4_tactic( | ||
| score = _score_mm_fp4_tactic( | ||
| rep_m, n, real_k, sm_count, tile, cluster, swap_ab | ||
| ) | ||
| if score > best_score and is_feasible(tile, cluster, swap_ab): | ||
|
|
@@ -101,7 +126,7 @@ def is_feasible(tile, cluster, swap_ab): | |
| return best_tactic | ||
|
|
||
|
|
||
| def _score_sm100_mm_fp4_tactic( | ||
| def _score_mm_fp4_tactic( | ||
| m, n, real_k, sm_count, mma_tiler_mn, cluster_shape_mn, swap_ab | ||
| ): | ||
| """Score a mm_fp4 cute-dsl tactic (higher means better). | ||
|
|
@@ -156,13 +181,33 @@ def _score_sm100_mm_fp4_tactic( | |
| if tile_n < 64 and prob_n > tile_n: | ||
| score *= 1e-6 | ||
|
|
||
| # 6. Penalize deviations from the swap rule mildly, so both swap variants stay in the top-N. | ||
| # 6. Penalize deviations from the established narrow-M orientation rule | ||
| # mildly. At large M, N-dependent launch geometry and cache state can flip | ||
| # the preferred orientation, so neither orientation receives a score | ||
| # offset. | ||
| rule_swap = (n % 8 == 0) and (1 <= m <= 32) and n > m | ||
| if swap_ab != rule_swap: | ||
| if m <= _MM_FP4_SWAP_PENALTY_MAX_M and swap_ab != rule_swap: | ||
| score *= 0.95 | ||
| return score | ||
|
|
||
|
|
||
| def _rank_mm_fp4_autotune_tactics(valid_tactics, m, n, real_k, sm_count, max_tactics): | ||
| """Rank and return at most ``max_tactics`` actual mm_fp4 tactics. | ||
|
|
||
| SM100 has two tactics per structural configuration because | ||
| ``use_prefetch`` is a Boolean. SM103 and SM107 encode their architecture | ||
| knobs in the final tuple field and usually have one tactic per grouping | ||
| key. Counting actual tactics avoids assuming every ranked group expands | ||
| to two benchmark candidates. | ||
| """ | ||
|
|
||
| def score(tactic): | ||
| tile, cluster, swap_ab, _, _, _ = tactic | ||
| return _score_mm_fp4_tactic(m, n, real_k, sm_count, tile, cluster, swap_ab) | ||
|
|
||
| return sorted(valid_tactics, key=score, reverse=True)[:max_tactics] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🤖 get_repo_knowledge executed:
Length of output: 35390 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- changed file context ---'
sed -n '150,235p' flashinfer/gemm/kernels/utils.py
printf '%s\n' '--- ranking helper references ---'
rg -n -A18 -B8 "_rank_mm_fp4_autotune_tactics|valid_tactics|max_tactics|def score" flashinfer/gemm
printf '%s\n' '--- tactic construction and ordering ---'
rg -n -A20 -B12 "SM100|SM103|SM107|fp4|FP4" flashinfer/gemm/gemm_base.py flashinfer/gemm/kernelsRepository: flashinfer-ai/flashinfer Length of output: 50381 🏁 Script executed: #!/bin/bash
set -eu
sed -n '150,235p' flashinfer/gemm/kernels/utils.py
printf '\n--- references ---\n'
rg -n -A18 -B8 "_rank_mm_fp4_autotune_tactics|valid_tactics|max_tactics|def score" flashinfer/gemm
printf '\n--- tactic ordering ---\n'
rg -n -A20 -B12 "SM100|SM103|SM107|fp4|FP4" flashinfer/gemm/gemm_base.py flashinfer/gemm/kernelsRepository: flashinfer-ai/flashinfer Length of output: 50381 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- utils.py ---'
cat -n flashinfer/gemm/kernels/utils.py | sed -n '120,220p'
printf '%s\n' '--- gemm_base.py tactic assembly ---'
rg -n -A35 -B15 "_rank_mm_fp4_autotune_tactics|append|tactics" flashinfer/gemm/gemm_base.py
printf '%s\n' '--- relevant definitions ---'
rg -n "class .*Tactic|kernel_type|prefetch|instruction|SM107|SM103|SM100" flashinfer/gemm -g '*.py' -g '*.cu' -g '*.h' -g '*.hpp'Repository: flashinfer-ai/flashinfer Length of output: 50381 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- all rank-helper call sites ---'
rg -n -A45 -B25 "_rank_mm_fp4_autotune_tactics" flashinfer/gemm
printf '%s\n' '--- FP4 tactic candidate constants and selector ---'
sed -n '1,130p' flashinfer/gemm/kernels/utils.py
sed -n '330,405p' flashinfer/gemm/kernels/utils.py
printf '%s\n' '--- FP4 runner candidate assembly ---'
rg -n -A80 -B35 "get_valid_tactics|precompile_mm_fp4_tactics|valid_tactics" flashinfer/gemm/gemm_mm_fp4_cute_dsl.py flashinfer/gemm/gemm_base.pyRepository: flashinfer-ai/flashinfer Length of output: 50381 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- rank call sites only ---'
rg -n "_rank_mm_fp4_autotune_tactics" flashinfer/gemm --glob '*.py'
printf '%s\n' '--- candidate assembly identifiers ---'
rg -n "SM100|SM103|SM107|sm100|sm103|sm107|valid_tactics|tactics =|tactics.append|extend\\(" flashinfer/gemm/gemm_mm_fp4_cute_dsl.py flashinfer/gemm/gemm_base.py flashinfer/gemm/kernels/utils.py --glob '*.py' | head -n 240Repository: flashinfer-ai/flashinfer Length of output: 22601 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- FP4 runner assembly and ranking ---'
sed -n '7200,7410p' flashinfer/gemm/gemm_base.py
printf '%s\n' '--- base tactic enumeration ---'
sed -n '5598,5680p' flashinfer/gemm/gemm_base.py
printf '%s\n' '--- candidate constants ---'
sed -n '1,85p' flashinfer/gemm/kernels/utils.pyRepository: flashinfer-ai/flashinfer Length of output: 14213 Preserve architecture diversity at the tuning cutoff.
🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| def _select_sm100_mm_fp4_cute_dsl_tactic(m, n, real_k, sm_count, sf_vec_size): | ||
| """Select the best tactic for mm_fp4(backend='cute-dsl'). | ||
|
|
||
|
|
@@ -205,7 +250,7 @@ def _select_sm100_mm_fp4_cute_dsl_tactic(m, n, real_k, sm_count, sf_vec_size): | |
| # sm107 4 at M 33-512, and sm107 41 / sm100 19 above that -- narrow sm100 tiles | ||
| # win decode, wide sm107 tiles win prefill. | ||
| # | ||
| # So this scores the union with the same _score_sm100_mm_fp4_tactic used for | ||
| # So this scores the union with the same _score_mm_fp4_tactic used for | ||
| # sm100 and for the top-N ranking, and emits whichever kernel won. | ||
| _SM107_MMA_TILER_MN_CANDIDATES = [ | ||
| (128, 64), | ||
|
|
@@ -292,7 +337,7 @@ def sm107_feasible(tile, inst_shape, cluster, swap_ab): | |
| best_score = -1.0 | ||
|
|
||
| # sm107 candidates are scored FIRST so that ties resolve to the | ||
| # Rubin-native kernel. _score_sm100_mm_fp4_tactic reads only tile, cluster | ||
| # Rubin-native kernel. _score_mm_fp4_tactic reads only tile, cluster | ||
| # and swap_ab, so a tile offered by both kernels scores identically and the | ||
| # strict `>` below keeps whichever was seen first. The autotuner's measured | ||
| # preference is sm107 at large M (41/60 winners above M=512), so sm107 wins | ||
|
|
@@ -317,7 +362,7 @@ def sm107_feasible(tile, inst_shape, cluster, swap_ab): | |
| for swap_ab in swap_options: | ||
| if swap_ab and not n_aligned: | ||
| continue | ||
| score = _score_sm100_mm_fp4_tactic( | ||
| score = _score_mm_fp4_tactic( | ||
| rep_m, n, real_k, sm_count, tile, cluster, swap_ab | ||
| ) | ||
| if score > best_score and sm107_feasible( | ||
|
|
@@ -346,7 +391,7 @@ def sm107_feasible(tile, inst_shape, cluster, swap_ab): | |
| if tile[0] == 256 and cluster[0] < 2: | ||
| continue # 2-CTA MMA (tile_m == 256) requires cluster_m >= 2 | ||
| for swap_ab in (False,) if not n_aligned else (False, True): | ||
| score = _score_sm100_mm_fp4_tactic( | ||
| score = _score_mm_fp4_tactic( | ||
| rep_m, n, real_k, sm_count, tile, cluster, swap_ab | ||
| ) | ||
| if score > best_score and sm100_feasible(tile, cluster, swap_ab): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.