[Core] Invoke custom KV cache bind hooks
Signed-off-by: yangzhuxinyzx <153831768+yangzhuxinyzx@users.noreply.github.com>
This commit is contained in:
1 parent
f73cedb3ec
commit
d64150fb9b
3 files changed
+45
-1
No files matched your search
@@ -0,0 +1,38 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.models.qwen4_exp.common.qsa_cache import QSAKeyStateCache
|
||||
from vllm.v1.worker.utils import bind_kv_cache
|
||||
|
||||
|
||||
def test_bind_qsa_key_cache_builds_key_and_mrope_views() -> None:
|
||||
prefix = "model.layers.0.self_attn.raw_key_cache"
|
||||
static_forward_context = {}
|
||||
layer = QSAKeyStateCache(
|
||||
head_size=128,
|
||||
dtype=torch.float16,
|
||||
cache_rope_positions=True,
|
||||
prefix=prefix,
|
||||
cache_config=SimpleNamespace(block_size=16),
|
||||
compress_ratio=4,
|
||||
vllm_config=SimpleNamespace(
|
||||
compilation_config=SimpleNamespace(
|
||||
static_forward_context=static_forward_context
|
||||
)
|
||||
),
|
||||
)
|
||||
cache = torch.empty(2, 8, 1, layer.head_size, dtype=torch.float16)
|
||||
runner_kv_caches = []
|
||||
|
||||
bind_kv_cache({prefix: cache}, static_forward_context, runner_kv_caches)
|
||||
|
||||
assert layer.kv_cache is cache
|
||||
assert layer.key_cache.shape == (2, 8, 1, 128)
|
||||
assert layer.key_cache.untyped_storage().data_ptr() == cache.data_ptr()
|
||||
assert layer.rope_position_cache.shape == (2, 8, 1, 3)
|
||||
assert layer.rope_position_cache.dtype == torch.int64
|
||||
assert runner_kv_caches == [cache]
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.attention.backend import AttentionBackend, AttentionImpl
|
||||
from vllm.v1.kv_cache_interface import KVCacheSpec
|
||||
@@ -20,6 +22,10 @@ class AttentionLayerBase(ABC):
|
||||
|
||||
impl: "AttentionImpl"
|
||||
|
||||
def bind_kv_cache(self, kv_cache: torch.Tensor) -> None:
|
||||
"""Bind an allocated KV cache view to this layer."""
|
||||
self.kv_cache = kv_cache
|
||||
|
||||
@abstractmethod
|
||||
def get_attn_backend(self) -> type[AttentionBackend]:
|
||||
"""Get the attention backend class for this layer."""
|
||||
|
||||
@@ -609,7 +609,7 @@ def bind_kv_cache(
|
||||
|
||||
# Bind kv_caches to forward context
|
||||
for layer_name, kv_cache in kv_caches.items():
|
||||
forward_context[layer_name].kv_cache = kv_cache
|
||||
forward_context[layer_name].bind_kv_cache(kv_cache)
|
||||
|
||||
|
||||
def is_residual_scattered_for_sp(
|
||||
|
||||
Reference in new issue
Block a user