| 
 | 1 | +from dataclasses import dataclass  | 
 | 2 | +from typing import List, Tuple  | 
 | 3 | + | 
 | 4 | +import openvino as ov  | 
 | 5 | +import torch  | 
 | 6 | + | 
 | 7 | +from vllm.attention.backends.abstract import (AttentionBackend,  | 
 | 8 | +                                              AttentionMetadata)  | 
 | 9 | + | 
 | 10 | + | 
 | 11 | +class OpenVINOAttentionBackend(AttentionBackend):  | 
 | 12 | + | 
 | 13 | +    @staticmethod  | 
 | 14 | +    def get_name() -> str:  | 
 | 15 | +        return "openvino"  | 
 | 16 | + | 
 | 17 | +    @staticmethod  | 
 | 18 | +    def get_impl_cls():  | 
 | 19 | +        # OpenVINO implements PagedAttention as part of the Optimum  | 
 | 20 | +        # exported model  | 
 | 21 | +        raise NotImplementedError  | 
 | 22 | + | 
 | 23 | +    @staticmethod  | 
 | 24 | +    def make_metadata(*args, **kwargs) -> "AttentionMetadata":  | 
 | 25 | +        raise NotImplementedError  | 
 | 26 | + | 
 | 27 | +    @staticmethod  | 
 | 28 | +    def make_openvino_metadata(*args, **kwargs) -> "OpenVINOAttentionMetadata":  | 
 | 29 | +        return OpenVINOAttentionMetadata(*args, **kwargs)  | 
 | 30 | + | 
 | 31 | +    @staticmethod  | 
 | 32 | +    def get_kv_cache_shape(  | 
 | 33 | +        num_blocks: int,  | 
 | 34 | +        block_size: int,  | 
 | 35 | +        num_kv_heads: int,  | 
 | 36 | +        head_size: int,  | 
 | 37 | +    ) -> Tuple[int, ...]:  | 
 | 38 | +        return (2, num_blocks, num_kv_heads, block_size, head_size)  | 
 | 39 | + | 
 | 40 | +    @staticmethod  | 
 | 41 | +    def swap_blocks(  | 
 | 42 | +        src_kv_cache: ov.Tensor,  | 
 | 43 | +        dst_kv_cache: ov.Tensor,  | 
 | 44 | +        src_to_dst: torch.Tensor,  | 
 | 45 | +    ) -> None:  | 
 | 46 | +        # OpenVINO currently supports only CPU, which does not require  | 
 | 47 | +        # swap of KV cache blocks  | 
 | 48 | +        raise NotImplementedError  | 
 | 49 | + | 
 | 50 | +    @staticmethod  | 
 | 51 | +    def copy_blocks(  | 
 | 52 | +        kv_caches: List[Tuple[ov.Tensor, ov.Tensor]],  | 
 | 53 | +        src_to_dists: List[Tuple[int, int]],  | 
 | 54 | +    ) -> None:  | 
 | 55 | +        for src, dst in src_to_dists:  | 
 | 56 | +            for key_cache, value_cache in kv_caches:  | 
 | 57 | +                key_cache.data[dst, :] = key_cache.data[src, :]  | 
 | 58 | +                value_cache.data[dst, :] = value_cache.data[src, :]  | 
 | 59 | + | 
 | 60 | + | 
 | 61 | +@dataclass  | 
 | 62 | +class OpenVINOAttentionMetadata:  | 
 | 63 | +    """Metadata for OpenVINOAttentionBackend.  | 
 | 64 | +
  | 
 | 65 | +    Basic terms used below:  | 
 | 66 | +    - batch_size_in_sequences - total number of sequences to execute  | 
 | 67 | +    - prompt_lens – per sequence size number of scheduled tokens  | 
 | 68 | +    - batch_size_in_tokens = sum(prompt_lens)  | 
 | 69 | +    - max_context_len = max(context_lens)  | 
 | 70 | +    - max_num_blocks = div_up(max_context_len / BLOCK_SIZE)  | 
 | 71 | +    - num_blocks – total number of blocks in block_indices  | 
 | 72 | +    """  | 
 | 73 | + | 
 | 74 | +    # Describes past KV cache size for each sequence within a batch  | 
 | 75 | +    # Shape: [batch_size_in_sequences]  | 
 | 76 | +    # Type: i32  | 
 | 77 | +    past_lens: torch.Tensor  | 
 | 78 | + | 
 | 79 | +    # Describes start indices of input / speculative tokens from  | 
 | 80 | +    # current sequences within a batch sequence  | 
 | 81 | +    # Shape: [batch_size_in_sequences + 1]  | 
 | 82 | +    # Type: i32  | 
 | 83 | +    subsequence_begins: torch.Tensor  | 
 | 84 | + | 
 | 85 | +    # Describes block tables for each sequence within a batch -  | 
 | 86 | +    # indices along 0th dimension in key_cache and value_cache inputs  | 
 | 87 | +    # Shape: [num_blocks]  | 
 | 88 | +    # Type: i32  | 
 | 89 | +    block_indices: torch.Tensor  | 
 | 90 | + | 
 | 91 | +    # Describes block tables for each sequence within a batch -  | 
 | 92 | +    # for i-th element, it is an index in block_indices with the  | 
 | 93 | +    # first block belonging to i-th sequence  | 
 | 94 | +    # Shape: [batch_size_in_sequences + 1]  | 
 | 95 | +    # Type: i32  | 
 | 96 | +    block_indices_begins: torch.Tensor  | 
 | 97 | + | 
 | 98 | +    # Describes max context length  | 
 | 99 | +    # Shape: scalar  | 
 | 100 | +    # Type: i32  | 
 | 101 | +    max_context_len: torch.Tensor  | 
0 commit comments