- **Add SPDX license headers to python source files** - **Check for SPDX headers using pre-commit** commit 9d7ef44c3cfb72ca4c32e1c677d99259d10d4745 Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:18:24 2025 -0500 Add SPDX license headers to python source files This commit adds SPDX license headers to python source files as recommended to the project by the Linux Foundation. These headers provide a concise way that is both human and machine readable for communicating license information for each source file. It helps avoid any ambiguity about the license of the code and can also be easily used by tools to help manage license compliance. The Linux Foundation runs license scans against the codebase to help ensure we are in compliance with the licenses of the code we use, including dependencies. Having these headers in place helps that tool do its job. More information can be found on the SPDX site: - https://spdx.dev/learn/handling-license-info/ Signed-off-by: Russell Bryant <rbryant@redhat.com> commit 5a1cf1cb3b80759131c73f6a9dddebccac039dea Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:36:32 2025 -0500 Check for SPDX headers using pre-commit Signed-off-by: Russell Bryant <rbryant@redhat.com> --------- Signed-off-by: Russell Bryant <rbryant@redhat.com>
124 lines
5.0 KiB
Python
124 lines
5.0 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
"""CacheEngine class for managing the KV cache."""
|
|
from typing import List
|
|
|
|
import torch
|
|
|
|
from vllm.attention import get_attn_backend
|
|
from vllm.config import CacheConfig, DeviceConfig, ModelConfig, ParallelConfig
|
|
from vllm.logger import init_logger
|
|
from vllm.utils import (STR_DTYPE_TO_TORCH_DTYPE, LayerBlockType,
|
|
get_dtype_size, is_pin_memory_available)
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
class CacheEngine:
|
|
"""Manages the KV cache.
|
|
|
|
This class is responsible for initializing and managing the GPU and CPU KV
|
|
caches. It also provides methods for performing KV cache operations, such
|
|
as swapping and copying.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
cache_config: CacheConfig,
|
|
model_config: ModelConfig,
|
|
parallel_config: ParallelConfig,
|
|
device_config: DeviceConfig,
|
|
) -> None:
|
|
self.cache_config = cache_config
|
|
self.model_config = model_config
|
|
self.parallel_config = parallel_config
|
|
self.device_config = device_config
|
|
|
|
self.head_size = model_config.get_head_size()
|
|
# Models like Jamba, have mixed typed layers, E.g Mamba
|
|
self.num_attention_layers = model_config.get_num_layers_by_block_type(
|
|
parallel_config, LayerBlockType.attention)
|
|
self.num_kv_heads = model_config.get_num_kv_heads(parallel_config)
|
|
|
|
self.block_size = cache_config.block_size
|
|
self.num_gpu_blocks = cache_config.num_gpu_blocks
|
|
if self.num_gpu_blocks:
|
|
self.num_gpu_blocks //= parallel_config.pipeline_parallel_size
|
|
self.num_cpu_blocks = cache_config.num_cpu_blocks
|
|
if self.num_cpu_blocks:
|
|
self.num_cpu_blocks //= parallel_config.pipeline_parallel_size
|
|
|
|
if cache_config.cache_dtype == "auto":
|
|
self.dtype = model_config.dtype
|
|
else:
|
|
self.dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype]
|
|
|
|
# Get attention backend.
|
|
self.attn_backend = get_attn_backend(self.head_size,
|
|
model_config.dtype,
|
|
cache_config.cache_dtype,
|
|
self.block_size,
|
|
model_config.is_attention_free,
|
|
use_mla=model_config.use_mla)
|
|
|
|
# Initialize the cache.
|
|
self.gpu_cache = self._allocate_kv_cache(
|
|
self.num_gpu_blocks, self.device_config.device_type)
|
|
self.cpu_cache = self._allocate_kv_cache(self.num_cpu_blocks, "cpu")
|
|
|
|
def _allocate_kv_cache(
|
|
self,
|
|
num_blocks: int,
|
|
device: str,
|
|
) -> List[torch.Tensor]:
|
|
"""Allocates KV cache on the specified device."""
|
|
kv_cache_shape = self.attn_backend.get_kv_cache_shape(
|
|
num_blocks, self.block_size, self.num_kv_heads, self.head_size)
|
|
pin_memory = is_pin_memory_available() if device == "cpu" else False
|
|
kv_cache: List[torch.Tensor] = []
|
|
for _ in range(self.num_attention_layers):
|
|
# null block in CpuGpuBlockAllocator requires at least that
|
|
# block to be zeroed-out.
|
|
# We zero-out everything for simplicity.
|
|
kv_cache.append(
|
|
torch.zeros(kv_cache_shape,
|
|
dtype=self.dtype,
|
|
pin_memory=pin_memory,
|
|
device=device))
|
|
return kv_cache
|
|
|
|
def swap_in(self, src_to_dst: torch.Tensor) -> None:
|
|
for i in range(self.num_attention_layers):
|
|
self.attn_backend.swap_blocks(self.cpu_cache[i], self.gpu_cache[i],
|
|
src_to_dst)
|
|
|
|
def swap_out(self, src_to_dst: torch.Tensor) -> None:
|
|
for i in range(self.num_attention_layers):
|
|
self.attn_backend.swap_blocks(self.gpu_cache[i], self.cpu_cache[i],
|
|
src_to_dst)
|
|
|
|
def copy(self, src_to_dsts: torch.Tensor) -> None:
|
|
self.attn_backend.copy_blocks(self.gpu_cache, src_to_dsts)
|
|
|
|
@staticmethod
|
|
def get_cache_block_size(
|
|
cache_config: CacheConfig,
|
|
model_config: ModelConfig,
|
|
parallel_config: ParallelConfig,
|
|
) -> int:
|
|
head_size = model_config.get_head_size()
|
|
num_heads = model_config.get_num_kv_heads(parallel_config)
|
|
num_attention_layers = model_config.get_num_layers_by_block_type(
|
|
parallel_config, LayerBlockType.attention)
|
|
|
|
key_cache_block = cache_config.block_size * num_heads * head_size
|
|
# For MLA there is no value cache, since the latent vector
|
|
# is joint keys and values.
|
|
value_cache_block = key_cache_block if not model_config.use_mla else 0
|
|
total = num_attention_layers * (key_cache_block + value_cache_block)
|
|
if cache_config.cache_dtype == "auto":
|
|
dtype = model_config.dtype
|
|
else:
|
|
dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype]
|
|
dtype_size = get_dtype_size(dtype)
|
|
return dtype_size * total
|