53 lines
2.2 KiB
Python
53 lines
2.2 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
from dataclasses import dataclass
|
|
|
|
import torch
|
|
|
|
|
|
@dataclass
|
|
class CommonAttentionMetadata:
|
|
"""
|
|
Attention metadata attributes that can be shared by layers in different KV
|
|
cache groups and thus having different block table.
|
|
"""
|
|
|
|
query_start_loc: torch.Tensor
|
|
"""(batch_size + 1,), the start location of each request in query Tensor"""
|
|
seq_lens: torch.Tensor
|
|
"""(batch_size,), the length of each request including both computed tokens
|
|
and newly scheduled tokens"""
|
|
|
|
|
|
def validate_kv_sharing_target(current_layer_name, target_layer_name,
|
|
static_forward_context):
|
|
error_msg = (f"Specified KV sharing target layer for {current_layer_name} "
|
|
f"is not valid: target layer {target_layer_name} ")
|
|
|
|
if current_layer_name == target_layer_name:
|
|
raise ValueError(error_msg +
|
|
"cannot be the same as the current layer.")
|
|
|
|
if target_layer_name not in static_forward_context:
|
|
from vllm.model_executor.models.utils import extract_layer_index
|
|
|
|
# If target layer name is not in the static fwd context, it means either
|
|
# a) the target layer does not come BEFORE the current layer, or
|
|
# b) the target layer is not an Attention layer that exists in the model
|
|
current_layer_idx = extract_layer_index(current_layer_name)
|
|
target_layer_idx = extract_layer_index(target_layer_name)
|
|
if current_layer_idx <= target_layer_idx:
|
|
raise ValueError(error_msg + "must come before the current layer.")
|
|
else:
|
|
raise ValueError(error_msg +
|
|
"is not a valid Attention layer in the model.")
|
|
|
|
# Currently KV sharing is only supported between layers of the same type
|
|
target_layer_attn_type = static_forward_context[
|
|
target_layer_name].attn_type
|
|
expected = static_forward_context[current_layer_name].attn_type
|
|
if target_layer_attn_type != expected:
|
|
raise ValueError(
|
|
error_msg +
|
|
f"must be the same type as the current layer ({expected}).")
|