[Kernel] Use flashinfer for decoding (#4353)
Co-authored-by: LiuXiaoxuanPKU <llilyliupku@gmail.com>
This commit is contained in:
co-authored by
LiuXiaoxuanPKU
parent
f8e7adda21
commit
43c413ec57
+97
-26
@@ -9,6 +9,7 @@ import torch.nn as nn
|
||||
|
||||
from vllm.attention import (AttentionMetadata, AttentionMetadataPerStage,
|
||||
get_attn_backend)
|
||||
from vllm.attention.backends.flashinfer import FlashInferBackend
|
||||
from vllm.config import (DeviceConfig, LoadConfig, LoRAConfig, ModelConfig,
|
||||
ParallelConfig, SchedulerConfig, VisionLanguageConfig)
|
||||
from vllm.distributed import broadcast_tensor_dict, with_pynccl_for_all_reduce
|
||||
@@ -23,8 +24,8 @@ from vllm.model_executor.model_loader import get_model
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.sequence import (MultiModalData, SamplerOutput, SequenceData,
|
||||
SequenceGroupMetadata)
|
||||
from vllm.utils import (CudaMemoryProfiler, is_hip, is_pin_memory_available,
|
||||
make_tensor_with_pad)
|
||||
from vllm.utils import (CudaMemoryProfiler, get_kv_cache_torch_dtype, is_hip,
|
||||
is_pin_memory_available, make_tensor_with_pad)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -155,6 +156,9 @@ class ModelRunner:
|
||||
# (max batch size to capture, max context len to capture / block size).
|
||||
self.graph_block_tables: torch.Tensor # Set after initial profiling.
|
||||
|
||||
# Set if the backend is flashinfer.
|
||||
self.flashinfer_workspace_buffer: torch.Tensor
|
||||
|
||||
def load_model(self) -> None:
|
||||
with CudaMemoryProfiler() as m:
|
||||
self.model = get_model(
|
||||
@@ -315,6 +319,7 @@ class ModelRunner:
|
||||
|
||||
# Compute the slot mapping.
|
||||
block_table = seq_group_metadata.block_tables[seq_id]
|
||||
|
||||
# Mask the [0, start_idx) tokens of the prompt with _PAD_SLOT_ID,
|
||||
# where start_idx is max(0, seq_len - sliding_window).
|
||||
# For example, if the prompt len is 10, sliding window is 8, and
|
||||
@@ -390,18 +395,26 @@ class ModelRunner:
|
||||
dtype=seq_start_loc.dtype,
|
||||
out=seq_start_loc[1:])
|
||||
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=True,
|
||||
seq_lens=seq_lens,
|
||||
seq_lens_tensor=seq_lens_tensor,
|
||||
max_query_len=max_query_len,
|
||||
max_seq_len=max_seq_len,
|
||||
subquery_start_loc=subquery_start_loc,
|
||||
seq_start_loc=seq_start_loc,
|
||||
context_lens_tensor=context_lens_tensor,
|
||||
block_tables=block_tables,
|
||||
use_cuda_graph=False,
|
||||
)
|
||||
if self.attn_backend is FlashInferBackend:
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=True,
|
||||
use_cuda_graph=False,
|
||||
seq_start_loc=seq_start_loc,
|
||||
max_seq_len=max_seq_len,
|
||||
block_tables=block_tables)
|
||||
else:
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=True,
|
||||
seq_lens=seq_lens,
|
||||
seq_lens_tensor=seq_lens_tensor,
|
||||
max_query_len=max_query_len,
|
||||
max_seq_len=max_seq_len,
|
||||
subquery_start_loc=subquery_start_loc,
|
||||
seq_start_loc=seq_start_loc,
|
||||
context_lens_tensor=context_lens_tensor,
|
||||
block_tables=block_tables,
|
||||
use_cuda_graph=False,
|
||||
)
|
||||
|
||||
return PreparePromptMetadata(
|
||||
input_tokens=input_tokens,
|
||||
@@ -429,6 +442,24 @@ class ModelRunner:
|
||||
lora_prompt_mapping: List[int] = []
|
||||
lora_requests: Set[LoRARequest] = set()
|
||||
|
||||
# The following fields are only for flashinfer
|
||||
# Please follow https://docs.flashinfer.ai/tutorials/kv_layout.html#page-layout
|
||||
# for the precise definition of the following fields.
|
||||
# An example:
|
||||
# request 1, page indices [0, 5, 8]
|
||||
# request 2, page indices [1, 6, 7]
|
||||
# request 3, page indices [3, 4]
|
||||
# paged_kv_indices is a concatenation of page indices of all requests:
|
||||
# [0, 5, 8, 1, 6, 7, 3, 4]
|
||||
# paged_kv_indptr is used to index into paged_kv_indices:
|
||||
# [0, 3, 6, 8]
|
||||
paged_kv_indices: List[int] = []
|
||||
# 0 at the beginning of paged_kv_indptr indicates the start of the
|
||||
# first request’s page indices in the paged_kv_indices list.
|
||||
paged_kv_indptr: List[int] = [0]
|
||||
# paged_kv_last_page_len is the length of the last page of each request
|
||||
paged_kv_last_page_len: List[int] = []
|
||||
|
||||
if len(seq_group_metadata_list) == 0:
|
||||
return PrepareDecodeMetadata.empty()
|
||||
|
||||
@@ -469,6 +500,13 @@ class ModelRunner:
|
||||
block_table = block_table[-sliding_window_blocks:]
|
||||
block_tables.append(block_table)
|
||||
|
||||
paged_kv_indices.extend(block_table)
|
||||
paged_kv_indptr.append(paged_kv_indptr[-1] + len(block_table))
|
||||
last_page_len = seq_data.get_len() % self.block_size
|
||||
if last_page_len == 0:
|
||||
last_page_len = self.block_size
|
||||
paged_kv_last_page_len.append(last_page_len)
|
||||
|
||||
# vLLM uses cuda graph only for decoding requests.
|
||||
# See `capture_model` API for more details.
|
||||
# For decoding requests, batch_size == input_tokens.
|
||||
@@ -518,18 +556,51 @@ class ModelRunner:
|
||||
device=self.device,
|
||||
)
|
||||
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=False,
|
||||
seq_lens=None,
|
||||
seq_lens_tensor=seq_lens_tensor,
|
||||
max_query_len=None,
|
||||
max_seq_len=max_seq_len,
|
||||
subquery_start_loc=None,
|
||||
seq_start_loc=None,
|
||||
context_lens_tensor=None,
|
||||
block_tables=block_tables,
|
||||
use_cuda_graph=use_captured_graph,
|
||||
)
|
||||
if self.attn_backend is FlashInferBackend:
|
||||
if not hasattr(self, "flashinfer_workspace_buffer"):
|
||||
# Allocate 16MB workspace buffer
|
||||
# Follow the example of flashinfer: https://docs.flashinfer.ai/api/python/decode.html
|
||||
self.flashinfer_workspace_buffer = torch.empty(
|
||||
16 * 1024 * 1024, dtype=torch.uint8, device=self.device)
|
||||
paged_kv_indptr = torch.tensor(paged_kv_indptr,
|
||||
dtype=torch.int,
|
||||
device=self.device)
|
||||
paged_kv_indices = torch.tensor(paged_kv_indices,
|
||||
dtype=torch.int,
|
||||
device=self.device)
|
||||
paged_kv_last_page_len = torch.tensor(paged_kv_last_page_len,
|
||||
dtype=torch.int,
|
||||
device=self.device)
|
||||
kv_cache_dtype = get_kv_cache_torch_dtype(self.kv_cache_dtype,
|
||||
self.model_config.dtype)
|
||||
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=False,
|
||||
use_cuda_graph=False,
|
||||
workspace_buffer=self.flashinfer_workspace_buffer,
|
||||
paged_kv_indptr=paged_kv_indptr,
|
||||
paged_kv_indices=paged_kv_indices,
|
||||
paged_kv_last_page_len=paged_kv_last_page_len,
|
||||
num_qo_heads=self.model_config.get_num_attention_heads(
|
||||
self.parallel_config),
|
||||
num_kv_heads=self.model_config.get_num_kv_heads(
|
||||
self.parallel_config),
|
||||
head_dim=self.model_config.get_head_size(),
|
||||
page_size=self.block_size,
|
||||
data_type=kv_cache_dtype)
|
||||
else:
|
||||
attn_metadata = self.attn_backend.make_metadata(
|
||||
is_prompt=False,
|
||||
seq_lens=None,
|
||||
seq_lens_tensor=seq_lens_tensor,
|
||||
max_query_len=None,
|
||||
max_seq_len=max_seq_len,
|
||||
subquery_start_loc=None,
|
||||
seq_start_loc=None,
|
||||
context_lens_tensor=None,
|
||||
block_tables=block_tables,
|
||||
use_cuda_graph=use_captured_graph,
|
||||
)
|
||||
return PrepareDecodeMetadata(
|
||||
input_tokens=input_tokens,
|
||||
input_positions=input_positions,
|
||||
|
||||
Reference in New Issue
Block a user