[Kernel][Qwen] Add Hopper LL-GEMM tuning table for Qwen4Exp (#54560)

Signed-off-by: Zheng Cai <8370601+zigzagcai@users.noreply.github.com>
This commit is contained in:
Zheng Cai
2026-09-01 11:21:41 +00:00
committed by GitHub
parent 481839ad9e
commit 191cecd51e
2 changed files with 151 additions and 6 deletions
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Qwen4Exp decode GEMM selection on Blackwell.
"""Qwen4Exp decode GEMM selection on Hopper and Blackwell.
Dispatch follows Kimi-K3 and uses the local ``(N, K)`` shape and token count.
Plans contain measured CUDA graph capture sizes; other token counts use the
@@ -78,11 +78,88 @@ QWEN4_EXP_GEMM_PLANS: dict[tuple[int, int], dict[int, SkinnyGemmConfig]] = {
},
}
# H200 plans selected by exhaustive CUDA graph replay measurements over
# M={1, 2, 4, 8, 16}. Only points that beat the standard linear implementation
# in both hot-cache and L2-flush measurements are retained; other token counts
# keep the standard implementation and its GEMM heuristics.
QWEN4_EXP_SM90_GEMM_PLANS: dict[tuple[int, int], dict[int, SkinnyGemmConfig]] = {
# GDN fused QKVZ projection, TP=4.
(4096, 2560): {
1: SkinnyGemmConfig(1, 128, 2, vector_width=4, static_k=2560),
2: SkinnyGemmConfig(2, 64, 4, vector_width=4, static_k=2560),
},
# GDN and QSA output projections, TP=4.
(2560, 1536): {
1: SkinnyGemmConfig(1, 128, 4, vector_width=2, static_k=1536),
2: SkinnyGemmConfig(2, 128, 4, vector_width=4, static_k=1536),
4: SkinnyGemmConfig(4, 64, 4, k_unroll=6, vector_width=4),
},
# GDN fused B/A projection, TP=4.
(24, 2560): {
1: SkinnyGemmConfig(1, 128, 3, vector_width=4, static_k=2560),
2: SkinnyGemmConfig(2, 64, 2, vector_width=4, static_k=2560),
4: SkinnyGemmConfig(4, 64, 1, static_k=2560),
8: SkinnyGemmConfig(8, 128, 1, vector_width=4, static_k=2560),
16: SkinnyGemmConfig(16, 128, 1, vector_width=4, static_k=2560),
},
# QSA fused QKV/gate projection, TP=4.
(3584, 2560): {
1: SkinnyGemmConfig(1, 128, 4, vector_width=2, static_k=2560),
2: SkinnyGemmConfig(2, 64, 4, k_unroll=5),
},
# QSA indexer Q/K projection, replicated in a TP=4 deployment.
(640, 2560): {
1: SkinnyGemmConfig(1, 256, 2, vector_width=2, static_k=2560),
2: SkinnyGemmConfig(2, 128, 2, vector_width=4, static_k=2560),
4: SkinnyGemmConfig(4, 128, 1, vector_width=2, static_k=2560),
8: SkinnyGemmConfig(8, 128, 2, vector_width=4, static_k=2560),
},
# Shared-expert fused gate/up projection, TP=4.
(320, 2560): {
1: SkinnyGemmConfig(1, 64, 2, vector_width=4, static_k=2560),
2: SkinnyGemmConfig(2, 128, 4, k_unroll=5, vector_width=4),
4: SkinnyGemmConfig(4, 160, 1, k_unroll=2),
8: SkinnyGemmConfig(8, 128, 1, vector_width=4, static_k=2560),
16: SkinnyGemmConfig(16, 128, 1, vector_width=4, static_k=2560),
},
# LM head, TP=4.
(62080, 2560): {
1: SkinnyGemmConfig(1, 64, 2, vector_width=2, static_k=2560),
2: SkinnyGemmConfig(2, 64, 2, vector_width=2, static_k=2560),
},
# HC merged down/injection projection, replicated in a TP=4 deployment.
(336, 10240): {
1: SkinnyGemmConfig(1, 256, 1, k_unroll=5),
2: SkinnyGemmConfig(2, 256, 3, static_k=10240),
4: SkinnyGemmConfig(4, 256, 3, static_k=10240),
8: SkinnyGemmConfig(8, 256, 3, static_k=10240),
},
# Final HC down projection, replicated in a TP=4 deployment.
(320, 10240): {
1: SkinnyGemmConfig(1, 256, 1, static_k=10240),
2: SkinnyGemmConfig(2, 128, 1, k_unroll=10),
4: SkinnyGemmConfig(4, 128, 1, k_unroll=10),
8: SkinnyGemmConfig(8, 128, 1, k_unroll=10),
},
}
def _is_sm103() -> bool:
return current_platform.is_device_capability((10, 3))
def _is_sm90() -> bool:
return current_platform.is_device_capability((9, 0))
def _gemm_plans() -> dict[tuple[int, int], dict[int, SkinnyGemmConfig]]:
if _is_sm103():
return QWEN4_EXP_GEMM_PLANS
if _is_sm90():
return QWEN4_EXP_SM90_GEMM_PLANS
return {}
def _is_packed_row_major(tensor: torch.Tensor) -> bool:
return tensor.dim() == 2 and tensor.stride() == (tensor.shape[1], 1)
@@ -124,7 +201,7 @@ class Qwen4ExpLowLatencyEmbeddingMethod(
def _qwen4_exp_low_latency_gemm(x: torch.Tensor, weight: torch.Tensor) -> torch.Tensor:
plan = QWEN4_EXP_GEMM_PLANS.get((weight.shape[0], weight.shape[1]))
plan = _gemm_plans().get((weight.shape[0], weight.shape[1]))
config = None if plan is None else plan.get(x.shape[0])
if (
config is not None
@@ -152,7 +229,8 @@ def enable_qwen4_exp_low_latency_gemm(
module: nn.Module,
dtype: torch.dtype,
) -> None:
if dtype != torch.bfloat16 or not _is_sm103():
plans = _gemm_plans()
if dtype != torch.bfloat16 or not plans:
return
if not shape_dynamic_skinny_gemm.is_available():
return
@@ -172,7 +250,7 @@ def enable_qwen4_exp_low_latency_gemm(
weight = getattr(child, "weight", None)
if weight is None or weight.dim() != 2:
continue
plan = QWEN4_EXP_GEMM_PLANS.get((weight.shape[0], weight.shape[1]))
plan = plans.get((weight.shape[0], weight.shape[1]))
if plan is None:
continue
if is_linear: