[Kimi-K3] Add FlashInfer speculative KDA backend (#54255)
Signed-off-by: Duncan Moss <dmoss@nvidia.com> Co-authored-by: Roger Wang <hey@rogerw.io> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg>
This commit is contained in:
co-authored by
Roger Wang
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thien Tran
parent
edaf3e7b61
commit
766b4ae54c
@@ -55,8 +55,10 @@ from vllm.third_party.flash_linear_attention.ops.kda import FusedRMSNormGated
|
||||
from vllm.transformers_utils.configs.kimi_linear import KimiLinearConfig
|
||||
from vllm.utils.flashinfer import (
|
||||
flashinfer_fused_kda_decode,
|
||||
flashinfer_packed_fused_kda_decode,
|
||||
flashinfer_recurrent_kda,
|
||||
has_flashinfer_fused_kda_decode,
|
||||
has_flashinfer_packed_fused_kda_decode,
|
||||
has_flashinfer_recurrent_kda,
|
||||
)
|
||||
from vllm.v1.attention.backend import AttentionBackend
|
||||
@@ -214,6 +216,73 @@ def is_flashinfer_fused_kda_decode_supported(
|
||||
)
|
||||
|
||||
|
||||
def is_flashinfer_fused_kda_spec_decode_supported(
|
||||
num_heads: int,
|
||||
head_dim: int,
|
||||
conv_width: int,
|
||||
num_spec: int,
|
||||
input_dtype: torch.dtype,
|
||||
conv_state_dtype: torch.dtype,
|
||||
recurrent_state_dtype: torch.dtype,
|
||||
lower_bound: float | None,
|
||||
use_recoverssm: bool,
|
||||
) -> bool:
|
||||
return (
|
||||
has_flashinfer_packed_fused_kda_decode()
|
||||
and current_platform.is_device_capability_family(100)
|
||||
and num_heads in (12, 24, 32, 48, 96)
|
||||
and head_dim == 128
|
||||
and conv_width == 4
|
||||
and 1 <= num_spec <= 7
|
||||
and input_dtype == torch.bfloat16
|
||||
and conv_state_dtype == torch.bfloat16
|
||||
and recurrent_state_dtype in (torch.float32, torch.bfloat16)
|
||||
and lower_bound is not None
|
||||
and lower_bound < 0
|
||||
and not use_recoverssm
|
||||
and not is_conv_state_dim_first()
|
||||
)
|
||||
|
||||
|
||||
def resolve_kda_spec_decode_backend(
|
||||
backend: str,
|
||||
num_heads: int,
|
||||
head_dim: int,
|
||||
conv_width: int,
|
||||
num_spec: int,
|
||||
input_dtype: torch.dtype,
|
||||
conv_state_dtype: torch.dtype,
|
||||
recurrent_state_dtype: torch.dtype,
|
||||
lower_bound: float | None,
|
||||
use_recoverssm: bool,
|
||||
) -> str:
|
||||
if backend not in ("auto", "native", "flashinfer"):
|
||||
raise ValueError(f"Unsupported KDA speculative decode backend: {backend}")
|
||||
supported = is_flashinfer_fused_kda_spec_decode_supported(
|
||||
num_heads,
|
||||
head_dim,
|
||||
conv_width,
|
||||
num_spec,
|
||||
input_dtype,
|
||||
conv_state_dtype,
|
||||
recurrent_state_dtype,
|
||||
lower_bound,
|
||||
use_recoverssm,
|
||||
)
|
||||
if backend == "flashinfer" and not supported:
|
||||
raise RuntimeError(
|
||||
"FlashInfer packed fused KDA decode requires its "
|
||||
"packed_fused_kda_decode API, CUDA SM10x, bfloat16 inputs and "
|
||||
"convolution state, float32 or bfloat16 recurrent state, D=128, "
|
||||
"W=4, a bounded gate, 1-7 speculative tokens, a supported head "
|
||||
"count, the SD convolution-state layout, and RecoverSSM disabled."
|
||||
)
|
||||
if supported and backend != "native":
|
||||
logger.info_once("Using FlashInfer packed fused KDA speculative decode.")
|
||||
return "flashinfer"
|
||||
return "native"
|
||||
|
||||
|
||||
def resolve_kda_decode_backend(
|
||||
backend: str,
|
||||
num_heads: int,
|
||||
@@ -537,6 +606,13 @@ class KimiK3DeltaAttention(GatedDeltaNetAttention):
|
||||
self.projection_size = self.head_dim * self.num_heads
|
||||
self.local_projection_size = divide(self.projection_size, self.tp_size)
|
||||
self.conv_size = kda_config["short_conv_kernel_size"]
|
||||
self.gate_lower_bound: float | None = kda_config.get("gate_lower_bound", None)
|
||||
if self.gate_lower_bound is not None:
|
||||
assert _KDA_GATE_LOGBOUND_MIN <= self.gate_lower_bound < 0, (
|
||||
"KDA gate lower bound must be in "
|
||||
f"[{_KDA_GATE_LOGBOUND_MIN}, 0). "
|
||||
f"Got {self.gate_lower_bound}."
|
||||
)
|
||||
assert kda_config.get("use_full_rank_gate", False), (
|
||||
"KimiK3DeltaAttention requires a full-rank gate"
|
||||
)
|
||||
@@ -618,8 +694,28 @@ class KimiK3DeltaAttention(GatedDeltaNetAttention):
|
||||
conv_state_dtype,
|
||||
recurrent_state_dtype,
|
||||
)
|
||||
spec_decode_backend = (
|
||||
additional_config.get("kda_spec_decode_backend", "auto")
|
||||
if isinstance(additional_config, dict)
|
||||
else "auto"
|
||||
)
|
||||
self.kda_spec_decode_backend = resolve_kda_spec_decode_backend(
|
||||
spec_decode_backend,
|
||||
self.local_num_heads,
|
||||
self.head_dim,
|
||||
self.conv_size,
|
||||
self.num_spec,
|
||||
vllm_config.model_config.dtype,
|
||||
conv_state_dtype,
|
||||
recurrent_state_dtype,
|
||||
self.gate_lower_bound,
|
||||
self.use_recoverssm,
|
||||
)
|
||||
decode_conv1d_weight = None
|
||||
if self.kda_decode_backend != "triton":
|
||||
if (
|
||||
self.kda_decode_backend != "triton"
|
||||
or self.kda_spec_decode_backend == "flashinfer"
|
||||
):
|
||||
decode_conv1d_weight = torch.empty(
|
||||
3,
|
||||
self.conv_size,
|
||||
@@ -648,14 +744,6 @@ class KimiK3DeltaAttention(GatedDeltaNetAttention):
|
||||
)
|
||||
set_weight_attrs(self.A_log, {"weight_loader": a_log_weight_loader(0)})
|
||||
|
||||
self.gate_lower_bound: float | None = kda_config.get("gate_lower_bound", None)
|
||||
if self.gate_lower_bound is not None:
|
||||
assert _KDA_GATE_LOGBOUND_MIN <= self.gate_lower_bound < 0, (
|
||||
"KDA gate lower bound must be in "
|
||||
f"[{_KDA_GATE_LOGBOUND_MIN}, 0). "
|
||||
f"Got {self.gate_lower_bound}."
|
||||
)
|
||||
|
||||
backend = (
|
||||
additional_config.get("kda_prefill_backend", "auto")
|
||||
if isinstance(additional_config, dict)
|
||||
@@ -860,6 +948,39 @@ class KimiK3DeltaAttention(GatedDeltaNetAttention):
|
||||
if not is_conv_state_dim_first():
|
||||
conv_state = conv_state.transpose(-1, -2)
|
||||
|
||||
if (
|
||||
self.kda_spec_decode_backend == "flashinfer"
|
||||
and has_spec_decode
|
||||
and m.num_prefills == 0
|
||||
and m.num_decodes == 0
|
||||
):
|
||||
assert self.decode_conv1d_weight is not None
|
||||
assert self.decode_norm_weight is not None
|
||||
assert spec_state_indices_tensor is not None
|
||||
assert spec_query_start_loc is not None
|
||||
assert num_accepted_tokens is not None
|
||||
assert self.gate_lower_bound is not None
|
||||
# "Packed" is FlashInfer's name for the varlen T>1 speculative path.
|
||||
flashinfer_packed_fused_kda_decode(
|
||||
x=mixed_qkv,
|
||||
weight=self.decode_conv1d_weight,
|
||||
conv_state=conv_state,
|
||||
raw_gate=g1,
|
||||
raw_beta=beta,
|
||||
A_log=self.A_log,
|
||||
dt_bias=self.dt_bias,
|
||||
state_indices=spec_state_indices_tensor,
|
||||
query_start_loc=spec_query_start_loc,
|
||||
num_accepted_tokens=num_accepted_tokens,
|
||||
state=recurrent_state,
|
||||
output_gate=g2[:num_actual_tokens],
|
||||
norm_weight=self.decode_norm_weight,
|
||||
lower_bound=self.gate_lower_bound,
|
||||
norm_eps=self.o_norm.eps,
|
||||
output=core_attn_out[:, :num_actual_tokens],
|
||||
)
|
||||
return
|
||||
|
||||
if (
|
||||
self.kda_decode_backend != "triton"
|
||||
and self.decode_conv1d_weight is not None
|
||||
|
||||
Reference in New Issue
Block a user