|
| 1 | +from typing import Dict, List, Optional, Tuple |
| 2 | + |
| 3 | +import intel_extension_for_pytorch.llm.modules as ipex_modules |
| 4 | +import torch |
| 5 | + |
| 6 | +from vllm import _custom_ops as ops |
| 7 | + |
| 8 | + |
| 9 | +class PagedAttention: |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def get_supported_head_sizes() -> List[int]: |
| 13 | + return [64, 80, 96, 112, 128, 256] |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def get_kv_cache_shape( |
| 17 | + num_blocks: int, |
| 18 | + block_size: int, |
| 19 | + num_kv_heads: int, |
| 20 | + head_size: int, |
| 21 | + *args, |
| 22 | + ) -> Tuple[int, ...]: |
| 23 | + return (2, num_blocks, block_size * num_kv_heads * head_size) |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def split_kv_cache( |
| 27 | + kv_cache: torch.Tensor, |
| 28 | + num_kv_heads: int, |
| 29 | + head_size: int, |
| 30 | + *args, |
| 31 | + ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 32 | + num_blocks = kv_cache.shape[1] |
| 33 | + |
| 34 | + key_cache = kv_cache[0] |
| 35 | + key_cache = key_cache.view(num_blocks, num_kv_heads, -1, head_size) |
| 36 | + value_cache = kv_cache[1] |
| 37 | + value_cache = value_cache.view(num_blocks, num_kv_heads, -1, head_size) |
| 38 | + return key_cache, value_cache |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def write_to_paged_cache( |
| 42 | + key: torch.Tensor, |
| 43 | + value: torch.Tensor, |
| 44 | + key_cache: torch.Tensor, |
| 45 | + value_cache: torch.Tensor, |
| 46 | + slot_mapping: torch.Tensor, |
| 47 | + kv_cache_dtype: str, |
| 48 | + kv_scale: float, |
| 49 | + *args, |
| 50 | + ) -> None: |
| 51 | + ipex_modules.PagedAttention.reshape_and_cache( |
| 52 | + key, value, key_cache, value_cache, |
| 53 | + slot_mapping.flatten().int()) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def forward_decode( |
| 57 | + query: torch.Tensor, |
| 58 | + key_cache: torch.Tensor, |
| 59 | + value_cache: torch.Tensor, |
| 60 | + block_tables: torch.Tensor, |
| 61 | + context_lens: torch.Tensor, |
| 62 | + max_context_len: int, |
| 63 | + kv_cache_dtype: str, |
| 64 | + num_kv_heads: int, |
| 65 | + scale: float, |
| 66 | + alibi_slopes: Optional[torch.Tensor], |
| 67 | + kv_scale: float, |
| 68 | + *args, |
| 69 | + ) -> torch.Tensor: |
| 70 | + output = torch.empty_like(query) |
| 71 | + block_size = value_cache.shape[2] |
| 72 | + head_mapping = torch.arange( |
| 73 | + 0, |
| 74 | + num_kv_heads, |
| 75 | + device="cpu", |
| 76 | + dtype=torch.int32, |
| 77 | + ).view(num_kv_heads, |
| 78 | + 1).repeat_interleave(query.size(1) // num_kv_heads).flatten() |
| 79 | + ipex_modules.PagedAttention.single_query_cached_kv_attention( |
| 80 | + output, query.contiguous(), key_cache, value_cache, head_mapping, |
| 81 | + scale, block_tables, context_lens, block_size, max_context_len, |
| 82 | + alibi_slopes) |
| 83 | + |
| 84 | + return output |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def forward_prefix( |
| 88 | + query: torch.Tensor, |
| 89 | + key: torch.Tensor, |
| 90 | + value: torch.Tensor, |
| 91 | + key_cache: torch.Tensor, |
| 92 | + value_cache: torch.Tensor, |
| 93 | + block_tables: torch.Tensor, |
| 94 | + subquery_start_loc: torch.Tensor, |
| 95 | + prompt_lens_tensor: torch.Tensor, |
| 96 | + context_lens: torch.Tensor, |
| 97 | + max_subquery_len: int, |
| 98 | + alibi_slopes: Optional[torch.Tensor], |
| 99 | + *args, |
| 100 | + ) -> torch.Tensor: |
| 101 | + raise NotImplementedError |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def swap_blocks( |
| 105 | + src_kv_cache: torch.Tensor, |
| 106 | + dst_kv_cache: torch.Tensor, |
| 107 | + src_to_dst: Dict[int, int], |
| 108 | + *args, |
| 109 | + ) -> None: |
| 110 | + raise NotImplementedError |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def copy_blocks( |
| 114 | + kv_caches: List[torch.Tensor], |
| 115 | + src_to_dists: Dict[int, List[int]], |
| 116 | + *args, |
| 117 | + ) -> None: |
| 118 | + key_caches = [kv_cache[0] for kv_cache in kv_caches] |
| 119 | + value_caches = [kv_cache[1] for kv_cache in kv_caches] |
| 120 | + ops.copy_blocks(key_caches, value_caches, src_to_dists) |
0 commit comments