+5








e126687a9a
Signed-off-by: huanghaoyan.hhy <huanghaoyan.hhy@alibaba-inc.com> Signed-off-by: andyluo7 <andy.luo@amd.com> Signed-off-by: lishunyang12 <lishunyang03@gmail.com> Signed-off-by: Harry Huang <huanghaoyan.hhy@alibaba-inc.com> Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg> Signed-off-by: Isotr0py <Isotr0py@outlook.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: KungYork <firelikest@163.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: andyluo7 <43718156+andyluo7@users.noreply.github.com> Co-authored-by: Isotr0py <mozf@inferact.ai> Co-authored-by: andyluo7 <andy.luo@amd.com> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: lishunyang12 <lishunyang03@gmail.com> Co-authored-by: Vadim Gimpelson <156319763+vadiklyutiy@users.noreply.github.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: OpenAI Codex <codex@openai.com>
186 lines
6.3 KiB
Python
186 lines
6.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
"""Qwen4Exp decode GEMM selection on 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
|
|
standard linear implementation.
|
|
"""
|
|
|
|
import torch
|
|
from torch import nn
|
|
|
|
import vllm.envs as envs
|
|
from vllm.model_executor.kernels.linear.cute_dsl.skinny_gemm import (
|
|
SkinnyGemmConfig,
|
|
shape_dynamic_skinny_gemm,
|
|
)
|
|
from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod
|
|
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
|
ParallelLMHead,
|
|
UnquantizedEmbeddingMethod,
|
|
)
|
|
from vllm.platforms import current_platform
|
|
from vllm.utils.torch_utils import direct_register_custom_op
|
|
|
|
QWEN4_EXP_GEMM_PLANS: dict[tuple[int, int], dict[int, SkinnyGemmConfig]] = {
|
|
# GDN fused QKVZ projection, TP=4.
|
|
(4096, 2560): {
|
|
1: SkinnyGemmConfig(1, 64, 4, k_unroll=4),
|
|
2: SkinnyGemmConfig(2, 64, 4, k_unroll=4),
|
|
},
|
|
# GDN and QSA output projections, TP=4.
|
|
(2560, 1536): {
|
|
1: SkinnyGemmConfig(1, 128, 2, k_unroll=2, vector_width=4),
|
|
2: SkinnyGemmConfig(2, 128, 2, k_unroll=2, vector_width=4),
|
|
4: SkinnyGemmConfig(4, 64, 2, k_unroll=2),
|
|
},
|
|
# GDN fused B/A projection, TP=4.
|
|
(24, 2560): {
|
|
1: SkinnyGemmConfig(1, 128, 2, k_unroll=4, vector_width=4),
|
|
2: SkinnyGemmConfig(2, 128, 2, k_unroll=4, vector_width=4),
|
|
4: SkinnyGemmConfig(4, 128, 2, k_unroll=4, vector_width=4),
|
|
8: SkinnyGemmConfig(8, 128, 1, k_unroll=4, vector_width=4),
|
|
16: SkinnyGemmConfig(16, 128, 1, k_unroll=4, vector_width=4),
|
|
},
|
|
# QSA fused QKV/gate projection, TP=4.
|
|
(3584, 2560): {
|
|
1: SkinnyGemmConfig(1, 128, 4, k_unroll=4, vector_width=4),
|
|
2: SkinnyGemmConfig(2, 64, 2, k_unroll=2),
|
|
4: SkinnyGemmConfig(4, 64, 2, k_unroll=2),
|
|
},
|
|
# QSA indexer Q/K projection, replicated in a TP=4 deployment.
|
|
(640, 2560): {
|
|
1: SkinnyGemmConfig(1, 128, 1, k_unroll=4, vector_width=4),
|
|
2: SkinnyGemmConfig(2, 128, 1, k_unroll=4, vector_width=4),
|
|
4: SkinnyGemmConfig(4, 128, 1, k_unroll=4, vector_width=4),
|
|
8: SkinnyGemmConfig(8, 128, 1, k_unroll=4, vector_width=4),
|
|
},
|
|
# Shared-expert fused gate/up projection, TP=4.
|
|
(320, 2560): {
|
|
1: SkinnyGemmConfig(1, 128, 2, k_unroll=4, vector_width=4),
|
|
2: SkinnyGemmConfig(2, 128, 2, k_unroll=4, vector_width=4),
|
|
4: SkinnyGemmConfig(4, 128, 2, k_unroll=4, vector_width=4),
|
|
8: SkinnyGemmConfig(8, 64, 1, k_unroll=4),
|
|
16: SkinnyGemmConfig(16, 128, 2, k_unroll=4, vector_width=4),
|
|
},
|
|
# LM head, TP=4.
|
|
(62080, 2560): {
|
|
1: SkinnyGemmConfig(1, 64, 4, k_unroll=2),
|
|
2: SkinnyGemmConfig(2, 32, 4, k_unroll=2),
|
|
},
|
|
# HC merged down/injection projection, replicated in a TP=4 deployment.
|
|
(336, 10240): {
|
|
1: SkinnyGemmConfig(1, 128, 1, static_k=10240),
|
|
2: SkinnyGemmConfig(2, 128, 1, static_k=10240),
|
|
4: SkinnyGemmConfig(4, 128, 2, static_k=10240),
|
|
8: SkinnyGemmConfig(8, 128, 1, k_unroll=4),
|
|
},
|
|
}
|
|
|
|
|
|
def _is_sm103() -> bool:
|
|
return current_platform.is_device_capability((10, 3))
|
|
|
|
|
|
def _is_packed_row_major(tensor: torch.Tensor) -> bool:
|
|
return tensor.dim() == 2 and tensor.stride() == (tensor.shape[1], 1)
|
|
|
|
|
|
def _runtime_ok(x: torch.Tensor, weight: torch.Tensor) -> bool:
|
|
return (
|
|
not envs.VLLM_BATCH_INVARIANT
|
|
and _is_packed_row_major(x)
|
|
and _is_packed_row_major(weight)
|
|
and x.dtype == torch.bfloat16
|
|
and weight.dtype == torch.bfloat16
|
|
and x.is_cuda
|
|
and weight.is_cuda
|
|
and x.device == weight.device
|
|
and x.shape[1] == weight.shape[1]
|
|
)
|
|
|
|
|
|
class _Qwen4ExpLowLatencyApply:
|
|
def apply(
|
|
self,
|
|
layer: nn.Module,
|
|
x: torch.Tensor,
|
|
bias: torch.Tensor | None = None,
|
|
) -> torch.Tensor:
|
|
if bias is None and not envs.VLLM_BATCH_INVARIANT:
|
|
return torch.ops.vllm.qwen4_exp_low_latency_gemm(x, layer.weight)
|
|
return super().apply(layer, x, bias) # type: ignore[misc]
|
|
|
|
|
|
class Qwen4ExpLowLatencyLinearMethod(_Qwen4ExpLowLatencyApply, UnquantizedLinearMethod):
|
|
pass
|
|
|
|
|
|
class Qwen4ExpLowLatencyEmbeddingMethod(
|
|
_Qwen4ExpLowLatencyApply, UnquantizedEmbeddingMethod
|
|
):
|
|
pass
|
|
|
|
|
|
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]))
|
|
config = None if plan is None else plan.get(x.shape[0])
|
|
if (
|
|
config is not None
|
|
and _runtime_ok(x, weight)
|
|
and shape_dynamic_skinny_gemm.is_available()
|
|
):
|
|
return shape_dynamic_skinny_gemm(x, weight, config)
|
|
return torch.nn.functional.linear(x, weight)
|
|
|
|
|
|
def _qwen4_exp_low_latency_gemm_fake(
|
|
x: torch.Tensor, weight: torch.Tensor
|
|
) -> torch.Tensor:
|
|
return x.new_empty((*x.shape[:-1], weight.shape[0]))
|
|
|
|
|
|
direct_register_custom_op(
|
|
op_name="qwen4_exp_low_latency_gemm",
|
|
op_func=_qwen4_exp_low_latency_gemm,
|
|
fake_impl=_qwen4_exp_low_latency_gemm_fake,
|
|
)
|
|
|
|
|
|
def enable_qwen4_exp_low_latency_gemm(
|
|
module: nn.Module,
|
|
dtype: torch.dtype,
|
|
) -> None:
|
|
if dtype != torch.bfloat16 or not _is_sm103():
|
|
return
|
|
if not shape_dynamic_skinny_gemm.is_available():
|
|
return
|
|
|
|
warmup_configs: set[SkinnyGemmConfig] = set()
|
|
for child in module.modules():
|
|
is_linear = (
|
|
isinstance(child, LinearBase)
|
|
and type(child.quant_method) is UnquantizedLinearMethod
|
|
)
|
|
is_head = (
|
|
isinstance(child, ParallelLMHead)
|
|
and type(child.quant_method) is UnquantizedEmbeddingMethod
|
|
)
|
|
if not (is_linear or is_head):
|
|
continue
|
|
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]))
|
|
if plan is None:
|
|
continue
|
|
if is_linear:
|
|
child.quant_method = Qwen4ExpLowLatencyLinearMethod()
|
|
else:
|
|
child.quant_method = Qwen4ExpLowLatencyEmbeddingMethod()
|
|
warmup_configs.update(plan.values())
|
|
|
|
if warmup_configs:
|
|
shape_dynamic_skinny_gemm.request_warmup_configs(dtype, warmup_configs)
|