Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
265 lines
9.1 KiB
Python
265 lines
9.1 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
"""FlashInfer backend for MLA prefill."""
|
|
|
|
from typing import TYPE_CHECKING, ClassVar
|
|
|
|
import torch
|
|
|
|
import vllm.envs as envs
|
|
from vllm.utils.gpu_sync_debug import gpu_sync_allowed
|
|
from vllm.v1.attention.backends.mla.prefill.base import (
|
|
MLADimensions,
|
|
MLAPrefillBackend,
|
|
)
|
|
from vllm.v1.attention.backends.utils import (
|
|
PerLayerParameters,
|
|
get_per_layer_parameters,
|
|
infer_global_hyperparameters,
|
|
log2_lse_to_ln,
|
|
)
|
|
from vllm.v1.worker.workspace import current_workspace_manager
|
|
|
|
if TYPE_CHECKING:
|
|
from vllm.config import VllmConfig
|
|
from vllm.model_executor.layers.attention.mla_attention import (
|
|
MLACommonPrefillMetadata,
|
|
)
|
|
from vllm.platforms.interface import DeviceCapability
|
|
|
|
try:
|
|
from flashinfer import BatchPrefillWithRaggedKVCacheWrapper
|
|
except ImportError:
|
|
BatchPrefillWithRaggedKVCacheWrapper = object # type: ignore[misc,assignment]
|
|
|
|
_DEFAULT_NUM_CHUNKS = 32
|
|
|
|
|
|
class FlashInferPrefillBackend(MLAPrefillBackend):
|
|
"""FlashInfer backend for MLA prefill."""
|
|
|
|
supported_mla_dimensions: ClassVar[list[MLADimensions]] = [
|
|
MLADimensions(
|
|
qk_nope_head_dim=128,
|
|
qk_rope_head_dim=64,
|
|
v_head_dim=128,
|
|
),
|
|
]
|
|
|
|
@staticmethod
|
|
def get_name() -> str:
|
|
return "FLASHINFER"
|
|
|
|
@classmethod
|
|
def supports_compute_capability(
|
|
cls,
|
|
device_capability: "DeviceCapability | None",
|
|
) -> bool:
|
|
return device_capability is not None and device_capability.major == 10
|
|
|
|
@classmethod
|
|
def is_available(cls) -> bool:
|
|
try:
|
|
from flashinfer import (
|
|
BatchPrefillWithRaggedKVCacheWrapper, # noqa: F401
|
|
)
|
|
|
|
return True
|
|
except ImportError:
|
|
return False
|
|
|
|
def __init__(
|
|
self,
|
|
num_heads: int,
|
|
scale: float,
|
|
kv_lora_rank: int,
|
|
qk_nope_head_dim: int,
|
|
qk_rope_head_dim: int,
|
|
v_head_dim: int,
|
|
vllm_config: "VllmConfig",
|
|
) -> None:
|
|
super().__init__(
|
|
num_heads=num_heads,
|
|
scale=scale,
|
|
kv_lora_rank=kv_lora_rank,
|
|
qk_nope_head_dim=qk_nope_head_dim,
|
|
qk_rope_head_dim=qk_rope_head_dim,
|
|
v_head_dim=v_head_dim,
|
|
vllm_config=vllm_config,
|
|
)
|
|
|
|
self._prefill_main: BatchPrefillWithRaggedKVCacheWrapper | None = None
|
|
self._prefill_chunks: list[BatchPrefillWithRaggedKVCacheWrapper] = []
|
|
self._global_hyperparameters: PerLayerParameters | None = None
|
|
self._wrapper_workspace_ptr = 0
|
|
self._workspace_spec = (
|
|
(envs.VLLM_FLASHINFER_WORKSPACE_BUFFER_SIZE,),
|
|
torch.uint8,
|
|
)
|
|
# Reserve capacity without retaining a view: a retained view would pin
|
|
# the old buffer when another user grows the shared workspace.
|
|
current_workspace_manager().get_simultaneous(self._workspace_spec)
|
|
|
|
def _ensure_chunks(
|
|
self,
|
|
num_chunks: int,
|
|
workspace_buffer: torch.Tensor,
|
|
) -> None:
|
|
if len(self._prefill_chunks) < num_chunks:
|
|
for _ in range(len(self._prefill_chunks), num_chunks):
|
|
self._prefill_chunks.append(
|
|
BatchPrefillWithRaggedKVCacheWrapper(
|
|
workspace_buffer, "NHD", backend="cutlass"
|
|
)
|
|
)
|
|
|
|
def _resolve_global_hyperparameters(self) -> PerLayerParameters:
|
|
if self._global_hyperparameters is not None:
|
|
return self._global_hyperparameters
|
|
|
|
from vllm.model_executor.layers.attention.mla_attention import (
|
|
MLACommonImpl,
|
|
)
|
|
from vllm.model_executor.layers.attention_layer_base import (
|
|
AttentionLayerBase,
|
|
)
|
|
|
|
# Match any layer with an MLA impl, not just the MLAAttention wrapper:
|
|
# fused MLA modules (Kimi-K3's MultiHeadLatentAttention) register a
|
|
# different layer type. Keying on impl also excludes linear/KDA layers.
|
|
forward_context = self.vllm_config.compilation_config.static_forward_context
|
|
layer_names = [
|
|
name
|
|
for name, layer in forward_context.items()
|
|
if isinstance(layer, AttentionLayerBase)
|
|
and isinstance(getattr(layer, "impl", None), MLACommonImpl)
|
|
]
|
|
|
|
self._global_hyperparameters = infer_global_hyperparameters(
|
|
get_per_layer_parameters(
|
|
self.vllm_config,
|
|
layer_names,
|
|
MLACommonImpl, # type: ignore[type-abstract]
|
|
)
|
|
)
|
|
return self._global_hyperparameters
|
|
|
|
def prepare_metadata(
|
|
self,
|
|
prefill_metadata: "MLACommonPrefillMetadata",
|
|
) -> None:
|
|
global_hyperparameters = self._resolve_global_hyperparameters()
|
|
qo_indptr = prefill_metadata.query_start_loc
|
|
has_context = prefill_metadata.chunked_context is not None
|
|
(workspace_buffer,) = current_workspace_manager().get_simultaneous(
|
|
self._workspace_spec
|
|
)
|
|
if (
|
|
self._prefill_main is None
|
|
or self._wrapper_workspace_ptr != workspace_buffer.data_ptr()
|
|
):
|
|
self._prefill_main = BatchPrefillWithRaggedKVCacheWrapper(
|
|
workspace_buffer, "NHD", backend="cutlass"
|
|
)
|
|
self._prefill_chunks = []
|
|
self._ensure_chunks(_DEFAULT_NUM_CHUNKS, workspace_buffer)
|
|
self._wrapper_workspace_ptr = workspace_buffer.data_ptr()
|
|
|
|
if has_context:
|
|
chunked_context = prefill_metadata.chunked_context
|
|
assert chunked_context is not None
|
|
self._ensure_chunks(len(chunked_context.chunks), workspace_buffer)
|
|
|
|
num_qo_heads = self.num_heads
|
|
num_kv_heads = num_qo_heads
|
|
|
|
head_dim_qk = self.qk_nope_head_dim + self.qk_rope_head_dim
|
|
head_dim_vo = self.v_head_dim
|
|
kv_indptr = qo_indptr.clone()
|
|
|
|
assert self._prefill_main is not None
|
|
# FlashInfer's plan() currently copies GPU indptrs to the CPU.
|
|
# This will be fixed by https://github.com/vllm-project/vllm/pull/52657.
|
|
with gpu_sync_allowed():
|
|
self._prefill_main.plan(
|
|
qo_indptr=qo_indptr,
|
|
kv_indptr=kv_indptr,
|
|
num_qo_heads=num_qo_heads,
|
|
num_kv_heads=num_kv_heads,
|
|
head_dim_qk=head_dim_qk,
|
|
head_dim_vo=head_dim_vo,
|
|
causal=True,
|
|
sm_scale=global_hyperparameters.sm_scale,
|
|
window_left=global_hyperparameters.window_left,
|
|
logits_soft_cap=global_hyperparameters.logits_soft_cap,
|
|
q_data_type=prefill_metadata.q_data_type,
|
|
o_data_type=prefill_metadata.output_dtype,
|
|
)
|
|
|
|
if has_context:
|
|
chunked_context = prefill_metadata.chunked_context
|
|
assert chunked_context is not None
|
|
for chunk in chunked_context.chunks:
|
|
with gpu_sync_allowed():
|
|
self._prefill_chunks[chunk.index].plan(
|
|
qo_indptr=chunk.query_start_loc,
|
|
kv_indptr=chunk.cu_seq_lens,
|
|
num_qo_heads=num_qo_heads,
|
|
num_kv_heads=num_kv_heads,
|
|
head_dim_qk=head_dim_qk,
|
|
head_dim_vo=head_dim_vo,
|
|
causal=False,
|
|
sm_scale=global_hyperparameters.sm_scale,
|
|
window_left=global_hyperparameters.window_left,
|
|
logits_soft_cap=global_hyperparameters.logits_soft_cap,
|
|
q_data_type=prefill_metadata.q_data_type,
|
|
o_data_type=prefill_metadata.output_dtype,
|
|
)
|
|
|
|
def supports_out(self) -> bool:
|
|
# Planned with head_dim_vo == v_head_dim, so the output is unpadded.
|
|
return True
|
|
|
|
def run_prefill_new_tokens(
|
|
self,
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
return_softmax_lse: bool,
|
|
out: torch.Tensor | None = None,
|
|
output_scale: torch.Tensor | None = None,
|
|
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
|
|
assert self._prefill_main is not None
|
|
|
|
ret = self._prefill_main.run(
|
|
q=q,
|
|
k=k,
|
|
v=v,
|
|
out=out,
|
|
return_lse=return_softmax_lse,
|
|
)
|
|
|
|
if isinstance(ret, tuple):
|
|
# Convert from (q_len, num_heads) to (num_heads, q_len)
|
|
return ret[0], log2_lse_to_ln(ret[1].transpose(0, 1))
|
|
return ret
|
|
|
|
def run_prefill_context_chunk(
|
|
self,
|
|
chunk: "MLACommonPrefillMetadata.ContextChunk",
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
out: torch.Tensor | None = None,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
attn_out, lse = self._prefill_chunks[chunk.index].run(
|
|
q=q,
|
|
k=k,
|
|
v=v,
|
|
out=out,
|
|
return_lse=True,
|
|
)
|
|
|
|
# Convert from (q_len, num_heads) to (num_heads, q_len)
|
|
return attn_out, log2_lse_to_ln(lse.transpose(0, 1))
|