[Rebase] Rebase to vllm 0.26.0 (#5443)
Signed-off-by: tzhouam <tzhouam@connect.ust.hk> Signed-off-by: tzhouam <tzhouam@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: tzhouam <tzhouam@gmail.com>
This commit is contained in:
41 files changed
+334
-183
No files matched your search
@@ -1023,7 +1023,7 @@ class OmniKVTransferManager:
|
||||
value_cache: list[torch.Tensor | None] = [None] * num_layers
|
||||
|
||||
for layer_idx, layer_kv in enumerate(kv_caches):
|
||||
kv_pair = normalize_layer_kv(layer_kv, req_id=req_id, layer_idx=layer_idx)
|
||||
kv_pair = normalize_layer_kv(layer_kv, req_id=req_id, layer_idx=layer_idx, block_size=block_size)
|
||||
if kv_pair is None:
|
||||
continue
|
||||
key_blocks, value_blocks = kv_pair
|
||||
|
||||
@@ -402,6 +402,7 @@ def normalize_layer_kv(
|
||||
*,
|
||||
req_id: str = "",
|
||||
layer_idx: int = -1,
|
||||
block_size: int | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor] | None:
|
||||
"""Normalize one layer KV cache to a ``(key_blocks, value_blocks)`` tuple.
|
||||
|
||||
@@ -424,6 +425,10 @@ def normalize_layer_kv(
|
||||
dim-0 selects key / value.
|
||||
* **Stacked tensor** ``[num_blocks, 2, block_size, n_heads, head_dim]`` –
|
||||
dim-1 selects key / value.
|
||||
* **Packed tensor** ``[num_blocks, n_heads, block_size, 2 * head_dim]`` –
|
||||
vLLM >= 0.23 FlashAttention packs K and V into the last (content)
|
||||
dimension. Only recognized when *block_size* is provided and matches
|
||||
dim 2, to avoid misreading other 4-D layouts.
|
||||
* **Tuple** ``(key_tensor, value_tensor)`` – returned as-is after
|
||||
validation.
|
||||
|
||||
@@ -431,13 +436,29 @@ def normalize_layer_kv(
|
||||
layer_kv: The raw KV cache (tensor or tuple) for the layer.
|
||||
req_id: Request ID used only for diagnostic log messages.
|
||||
layer_idx: Layer index used only for diagnostic log messages.
|
||||
block_size: The paged-attention block size, required to recognize
|
||||
the packed 4-D layout.
|
||||
|
||||
Returns:
|
||||
``(key_blocks, value_blocks)`` if *layer_kv* is valid, ``None``
|
||||
otherwise.
|
||||
"""
|
||||
if isinstance(layer_kv, torch.Tensor):
|
||||
if layer_kv.ndim >= 3 and layer_kv.shape[0] == 2:
|
||||
if (
|
||||
layer_kv.ndim == 4
|
||||
and block_size is not None
|
||||
and layer_kv.shape[2] == block_size
|
||||
and layer_kv.shape[-1] % 2 == 0
|
||||
):
|
||||
# vLLM >= 0.23 FlashAttention layout: (num_blocks, n_heads,
|
||||
# block_size, 2 * head_dim). Unpack the same way
|
||||
# FlashAttentionImpl does: move block_size before heads, then
|
||||
# split the content dim into the K and V halves. Checked before
|
||||
# the stacked layouts (which are 5-D in practice) so that a
|
||||
# 2-KV-head packed tensor is not misread as a dim-1 stack.
|
||||
head_dim = layer_kv.shape[-1] // 2
|
||||
key_blocks, value_blocks = layer_kv.transpose(1, 2).split(head_dim, dim=-1)
|
||||
elif layer_kv.ndim >= 3 and layer_kv.shape[0] == 2:
|
||||
key_blocks = layer_kv[0]
|
||||
value_blocks = layer_kv[1]
|
||||
elif layer_kv.ndim >= 3 and layer_kv.shape[1] == 2:
|
||||
@@ -446,7 +467,8 @@ def normalize_layer_kv(
|
||||
else:
|
||||
logger.warning(
|
||||
f"Layer {layer_idx} for request {req_id} has invalid stacked KV shape: "
|
||||
f"expected [2, ...] or [..., 2, ...] at dim 0/1, got {tuple(layer_kv.shape)}"
|
||||
f"expected [2, ...], [..., 2, ...] at dim 0/1, or packed "
|
||||
f"[num_blocks, n_heads, block_size, 2*head_dim], got {tuple(layer_kv.shape)}"
|
||||
)
|
||||
return None
|
||||
elif isinstance(layer_kv, tuple):
|
||||
|
||||
Reference in new issue
Block a user