[New model] Kimi K3 (#50000)
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Signed-off-by: Isotr0py <Isotr0py@outlook.com> Signed-off-by: Bugen Zhao <i@bugenzhao.com> Signed-off-by: Jee Jee Li <jeejeelee@inferact.ai> Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Signed-off-by: wzhao18 <wzhao18.sz@gmail.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Ziming Huang <zelda.huanghuang@gmail.com> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: Isotr0py <mozf@inferact.ai> Co-authored-by: aoshen02 <aoshen02@users.noreply.github.com> Co-authored-by: Woosuk Kwon <woosuk@inferact.ai> Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: aoshen02 <aoshen@inferact.ai> Co-authored-by: Summer Yang <girasoleyang@gmail.com> Co-authored-by: Kevin H. Luu <khluu000@gmail.com> Co-authored-by: Bowen Wang <abmfy@icloud.com> Co-authored-by: gnovack <novackgm@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: xiaozhoupy <peiyuanzhou1994@gmail.com> Co-authored-by: Roy Wang <yasong.wang@inferact.ai> Co-authored-by: Jeff (Junze) Ma <93145857+majunze2001@users.noreply.github.com> Co-authored-by: Codex <codex@openai.com> Co-authored-by: Wei Zhao <51183510+wzhao18@users.noreply.github.com>
This commit is contained in:
co-authored by
Isotr0py
Thien Tran
Bugen Zhao
Yongye Zhu
Ziming Huang
Roger Wang
Isotr0py
aoshen02
Woosuk Kwon
Jee Jee Li
aoshen02
Summer Yang
Kevin H. Luu
Bowen Wang
gnovack
Nick Hill
Yifan Qiao
xiaozhoupy
Roy Wang
Jeff Ma
Codex
Wei Zhao
parent
0c64be8873
commit
aeeb36b1f1
@@ -234,7 +234,7 @@ def _deepgemm_fp8_gemm_nt_warmup(
|
||||
|
||||
device = w.device
|
||||
a1q = torch.empty((max_tokens, k), device=device, dtype=torch.float8_e4m3fn)
|
||||
a1q_scales = torch.empty(
|
||||
a1q_scales = torch.zeros(
|
||||
(max_tokens, k // block_m), device=device, dtype=torch.float32
|
||||
)
|
||||
out = torch.empty((max_tokens, n), device=device, dtype=torch.bfloat16)
|
||||
@@ -336,7 +336,7 @@ def _deepgemm_grouped_fp8_gemm_nt_contiguous_warmup(
|
||||
def _warmup(w: torch.Tensor, w_scale: torch.Tensor):
|
||||
_, n, k = w.size()
|
||||
a1q = torch.empty((MAX_M, k), device=device, dtype=torch.float8_e4m3fn)
|
||||
a1q_scales = torch.empty(
|
||||
a1q_scales = torch.zeros(
|
||||
(MAX_M, k // block_m), device=device, dtype=torch.float32
|
||||
)
|
||||
out = torch.empty((MAX_M, n), device=device, dtype=torch.bfloat16)
|
||||
|
||||
@@ -28,6 +28,9 @@ from vllm.model_executor.warmup.flashinfer_sparse_mla_warmup import (
|
||||
deepseek_v4_sparse_mla_attention_warmup,
|
||||
flashinfer_sparse_mla_decode_autotune_warmup,
|
||||
)
|
||||
from vllm.model_executor.warmup.kimi_k3_triton_warmup import (
|
||||
kimi_k3_triton_warmup,
|
||||
)
|
||||
from vllm.model_executor.warmup.qwen_triton_warmup import qwen_triton_warmup
|
||||
from vllm.model_executor.warmup.sparse_mla_triton_warmup import (
|
||||
sparse_mla_triton_warmup,
|
||||
@@ -123,6 +126,7 @@ def kernel_warmup(worker: "Worker", *, process_local_only: bool = False):
|
||||
|
||||
# Run next so input-prep kernels JIT against pristine runner state.
|
||||
if worker.vllm_config.kernel_config.enable_jit_warmup:
|
||||
kimi_k3_triton_warmup(worker)
|
||||
fa4_cutedsl_warmup(worker)
|
||||
sparse_mla_triton_warmup(worker)
|
||||
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Warm up Kimi-K3 Triton kernels."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.models.kimi_k3.nvidia.kda import KimiK3DeltaAttention
|
||||
from vllm.v1.worker.gpu_worker import Worker
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def _get_kda_layer(worker: Worker) -> KimiK3DeltaAttention | None:
|
||||
from vllm.models.kimi_k3.nvidia.kda import KimiK3DeltaAttention
|
||||
|
||||
compilation_config = getattr(
|
||||
worker.model_runner,
|
||||
"compilation_config",
|
||||
None,
|
||||
)
|
||||
static_context = getattr(compilation_config, "static_forward_context", None)
|
||||
if not isinstance(static_context, dict):
|
||||
return None
|
||||
return next(
|
||||
(
|
||||
layer
|
||||
for layer in static_context.values()
|
||||
if isinstance(layer, KimiK3DeltaAttention)
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
def _warm_attn_res(worker: Worker) -> None:
|
||||
from vllm.models.kimi_k3.nvidia.ops.attn_res import (
|
||||
attn_res,
|
||||
get_attn_res_triton_warmup_profiles,
|
||||
)
|
||||
|
||||
config = worker.model_config.hf_text_config
|
||||
block_size = getattr(config, "attn_res_block_size", None)
|
||||
if block_size is None:
|
||||
return
|
||||
|
||||
hidden_size = int(config.hidden_size)
|
||||
max_blocks = (int(config.num_hidden_layers) + block_size - 1) // block_size
|
||||
if max_blocks < 2:
|
||||
return
|
||||
|
||||
dtype = worker.model_config.dtype
|
||||
device = torch.device("cuda")
|
||||
eps = float(config.rms_norm_eps)
|
||||
prefix = torch.zeros((1, hidden_size), dtype=dtype, device=device)
|
||||
delta = torch.zeros_like(prefix)
|
||||
blocks = torch.zeros(
|
||||
(1, max_blocks, hidden_size),
|
||||
dtype=dtype,
|
||||
device=device,
|
||||
)
|
||||
norm_weight = torch.zeros(hidden_size, dtype=dtype, device=device)
|
||||
qk_weight = torch.zeros_like(norm_weight)
|
||||
output_norm_weight = torch.zeros_like(norm_weight)
|
||||
|
||||
for (
|
||||
num_blocks,
|
||||
has_delta,
|
||||
block_write_idx,
|
||||
apply_output_norm,
|
||||
) in get_attn_res_triton_warmup_profiles(max_blocks):
|
||||
attn_res(
|
||||
prefix,
|
||||
delta if has_delta else None,
|
||||
blocks,
|
||||
norm_weight,
|
||||
qk_weight,
|
||||
output_norm_weight if apply_output_norm else None,
|
||||
num_blocks=num_blocks,
|
||||
block_write_idx=block_write_idx,
|
||||
eps=eps,
|
||||
output_norm_eps=eps if apply_output_norm else 0.0,
|
||||
)
|
||||
|
||||
|
||||
def _warm_recurrent_kda(
|
||||
layer: KimiK3DeltaAttention,
|
||||
input_dtype: torch.dtype,
|
||||
) -> None:
|
||||
from vllm.models.kimi_k3.nvidia.ops.third_party.kda.fused_recurrent import (
|
||||
fused_recurrent_kda,
|
||||
get_fused_recurrent_kda_fwd_warmup_profiles,
|
||||
)
|
||||
|
||||
num_speculative_tokens = int(layer.num_spec)
|
||||
# fused_recurrent_kda_fwd_kernel is only used by speculative decode.
|
||||
if num_speculative_tokens <= 0:
|
||||
return
|
||||
|
||||
kv_cache = layer.kv_cache
|
||||
if not isinstance(kv_cache, (list, tuple)) or len(kv_cache) < 2:
|
||||
return
|
||||
state = kv_cache[1]
|
||||
if not isinstance(state, torch.Tensor) or not state.numel():
|
||||
return
|
||||
|
||||
logger.info("Warming up Kimi-K3 speculative KDA kernels.")
|
||||
h = int(layer.local_num_heads)
|
||||
d = int(layer.head_dim)
|
||||
tokens_per_sequence = num_speculative_tokens + 1
|
||||
for num_sequences in get_fused_recurrent_kda_fwd_warmup_profiles(h):
|
||||
num_tokens = num_sequences * tokens_per_sequence
|
||||
packed_qkv = torch.empty(
|
||||
(num_tokens, 3 * h * d),
|
||||
dtype=input_dtype,
|
||||
device=state.device,
|
||||
)
|
||||
q, k, v = (
|
||||
tensor.view(1, num_tokens, h, d)
|
||||
for tensor in packed_qkv.split(h * d, dim=-1)
|
||||
)
|
||||
fused_recurrent_kda(
|
||||
q=q,
|
||||
k=k,
|
||||
v=v,
|
||||
raw_g=torch.empty(
|
||||
(1, num_tokens, h, d),
|
||||
dtype=input_dtype,
|
||||
device=state.device,
|
||||
),
|
||||
raw_beta=torch.empty(
|
||||
(1, num_tokens, h),
|
||||
dtype=input_dtype,
|
||||
device=state.device,
|
||||
),
|
||||
A_log=layer.A_log,
|
||||
dt_bias=layer.dt_bias,
|
||||
lower_bound=layer.gate_lower_bound,
|
||||
initial_state=state[:1],
|
||||
cu_seqlens=torch.arange(
|
||||
0,
|
||||
num_tokens + 1,
|
||||
tokens_per_sequence,
|
||||
dtype=torch.int32,
|
||||
device=state.device,
|
||||
),
|
||||
ssm_state_indices=torch.zeros(
|
||||
(num_sequences, tokens_per_sequence),
|
||||
dtype=torch.int32,
|
||||
device=state.device,
|
||||
),
|
||||
num_accepted_tokens=torch.ones(
|
||||
num_sequences,
|
||||
dtype=torch.int32,
|
||||
device=state.device,
|
||||
),
|
||||
out=torch.empty(
|
||||
(1, num_tokens, h, d),
|
||||
dtype=input_dtype,
|
||||
device=state.device,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@torch.inference_mode()
|
||||
def kimi_k3_triton_warmup(worker: Worker) -> None:
|
||||
"""Warm Kimi-K3 Triton kernels reachable by this server."""
|
||||
if not current_platform.is_cuda():
|
||||
return
|
||||
|
||||
layer = _get_kda_layer(worker)
|
||||
if layer is None:
|
||||
return
|
||||
|
||||
_warm_attn_res(worker)
|
||||
_warm_recurrent_kda(layer, worker.model_config.dtype)
|
||||
Reference in New Issue
Block a user