|
| 1 | +""" |
| 2 | +UcmSparseBase Class provides interfaces for general sparse attention algorithm implementation in vLLM. |
| 3 | +
|
| 4 | +The class provides the following primitives: |
| 5 | + Scheduler-side: runs in the scheduler, binds metadata, which |
| 6 | + is used by the worker-side to retrieval/load KV cache. |
| 7 | + estimate_num_slots_sparsed() - get the number of required slots. |
| 8 | + update_state_after_alloc() - update UcmSparse state after |
| 9 | + temporary buffer alloc by the CacheManager. |
| 10 | + request_finished_in_scheduler() - called when a request is finished, with |
| 11 | + the computed kv cache blocks for the request. |
| 12 | + Returns metadata for the next step. |
| 13 | +
|
| 14 | + Worker-side: runs in each worker, retrieval/load KV cache. |
| 15 | + execute_begin() - hook at the beginning of "ModelRunner->execute_model". |
| 16 | + execute_finished() - hook at the end of "ModelRunner->execute_model". |
| 17 | + attention_begin() - hook at the beginning of "unified_attention". |
| 18 | + attention_finished() - hook at the end of "unified_attention". |
| 19 | + request_finished_in_worker() - release the resources, like block features. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import enum |
| 25 | +from abc import ABC, abstractmethod |
| 26 | +from typing import TYPE_CHECKING, Dict, List, Optional, Union |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from vllm.v1.core.sched.output import SchedulerOutput |
| 30 | + from vllm.v1.request import Request |
| 31 | + from vllm.attention.backends.abstract import AttentionMetadata |
| 32 | + from unifiedcache.ucm_connector.base import UcmKVStoreBase |
| 33 | + from vllm.config import VllmConfig |
| 34 | + |
| 35 | +import torch |
| 36 | +from vllm.distributed.kv_transfer import get_kv_transfer_group, has_kv_transfer_group |
| 37 | +from vllm.forward_context import ForwardContext |
| 38 | +from vllm_ascend.worker.npu_input_batch import CachedRequestState, InputBatch |
| 39 | + |
| 40 | +INVALID_SLOT = -1 |
| 41 | + |
| 42 | + |
| 43 | +class UcmSparseRole(enum.Enum): |
| 44 | + # sparser running in the scheduler process |
| 45 | + SCHEDULER = 0 |
| 46 | + |
| 47 | + # sparser running in the worker process |
| 48 | + WORKER = 1 |
| 49 | + |
| 50 | + |
| 51 | +class UcmSparseMetadata(ABC): # noqa: B024 |
| 52 | + """ |
| 53 | + Abstract Metadata used to communicate between the |
| 54 | + Scheduler UcmSparse instance and Worker UcmSparse instance. |
| 55 | + """ |
| 56 | + |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +class UcmSparseBase(ABC): |
| 61 | + """ |
| 62 | + An general interface for impl sparse attention algorithm in vLLM |
| 63 | + """ |
| 64 | + |
| 65 | + def __init__(self, vllm_config: VllmConfig, role: UcmSparseRole): |
| 66 | + self._sparse_metadata: Optional[UcmSparseMetadata] = None |
| 67 | + self._vllm_config = vllm_config |
| 68 | + self._role = role |
| 69 | + |
| 70 | + @property |
| 71 | + def role(self) -> UcmSparseRole: |
| 72 | + return self._role |
| 73 | + |
| 74 | + # ============================== |
| 75 | + # Worker-side methods |
| 76 | + # ============================== |
| 77 | + |
| 78 | + def bind_sparse_metadata(self, sparse_metadata: UcmSparseMetadata) -> None: |
| 79 | + """Set the connector metadata from the scheduler. |
| 80 | +
|
| 81 | + This function should be called by the model runner every time |
| 82 | + before the model execution. The metadata will be used for runtime |
| 83 | + KV cache loading and saving. |
| 84 | +
|
| 85 | + Args: |
| 86 | + connector_metadata (dict): the connector metadata. |
| 87 | + """ |
| 88 | + self._sparse_metadata = sparse_metadata |
| 89 | + |
| 90 | + def clear_sparse_metadata(self) -> None: |
| 91 | + """Clear the sparse metadata. |
| 92 | +
|
| 93 | + This function should be called by the model runner every time |
| 94 | + after the model execution. |
| 95 | + """ |
| 96 | + self._sparse_metadata = None |
| 97 | + |
| 98 | + def _get_sparse_metadata(self) -> UcmSparseMetadata: |
| 99 | + """Get the sparse metadata. |
| 100 | +
|
| 101 | + This function should only be called inside the UCMSparse. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + SparseMetadata: the UCM sparse metadata. |
| 105 | + """ |
| 106 | + |
| 107 | + # Should only be called while set to valid metadata. |
| 108 | + assert self._sparse_metadata is not None |
| 109 | + return self._sparse_metadata |
| 110 | + |
| 111 | + def register_kv_caches(self, kv_caches: dict[str, torch.Tensor]): |
| 112 | + """ |
| 113 | + Args: kv_caches: |
| 114 | + dictionary of layer names, kv cache |
| 115 | + """ |
| 116 | + pass |
| 117 | + |
| 118 | + def execute_begin(self, scheduler_output: SchedulerOutput): |
| 119 | + """ |
| 120 | + This is called at the beginning of "ModelRunner->execute_model" function. |
| 121 | + """ |
| 122 | + pass |
| 123 | + |
| 124 | + def execute_finished(self): |
| 125 | + """ |
| 126 | + This is called at the end of "ModelRunner->execute_model" function. |
| 127 | + """ |
| 128 | + pass |
| 129 | + |
| 130 | + def attention_begin( |
| 131 | + self, |
| 132 | + query: torch.Tensor, |
| 133 | + key: torch.Tensor, |
| 134 | + value: torch.Tensor, |
| 135 | + layer_name: str, |
| 136 | + forward_context: ForwardContext, |
| 137 | + ) -> None: |
| 138 | + """ |
| 139 | + This is called at the beginning of "unified_attention". |
| 140 | + Sparse attention algorithm can modify forward_context.attn_metadata if necessary. |
| 141 | + (UC_TODO: modify dataclass is not allowed in python?) |
| 142 | + """ |
| 143 | + pass |
| 144 | + |
| 145 | + def attention_finished( |
| 146 | + self, |
| 147 | + query: torch.Tensor, |
| 148 | + key: torch.Tensor, |
| 149 | + value: torch.Tensor, |
| 150 | + attn_output: torch.Tensor, |
| 151 | + layer_name: str, |
| 152 | + forward_context: ForwardContext, |
| 153 | + ) -> None: |
| 154 | + """ |
| 155 | + This is called at the end of "unified_attention". |
| 156 | + """ |
| 157 | + pass |
| 158 | + |
| 159 | + def request_finished_in_worker(self, request_id: Union[int, str]): |
| 160 | + """ |
| 161 | + This function releases the resources of finished requests at worker-side. |
| 162 | + """ |
| 163 | + pass |
| 164 | + |
| 165 | + # ============================== |
| 166 | + # Scheduler-side methods |
| 167 | + # ============================== |
| 168 | + |
| 169 | + @abstractmethod |
| 170 | + def request_begin(self, request_id: Union[int, str], prompt_token_ids: List[int]): |
| 171 | + """ |
| 172 | + This is called at the beginning of "Scheduler->add_request" function. |
| 173 | + """ |
| 174 | + pass |
| 175 | + |
| 176 | + def request_finished_in_scheduler(self, request_id: Union[int, str]): |
| 177 | + """ |
| 178 | + This is called inside "Scheduler->finish_requests" function. |
| 179 | + Generate the metadata required by UcmSparse instance at worker-side. |
| 180 | + """ |
| 181 | + pass |
| 182 | + |
| 183 | + def estimate_num_slots_sparsed(self, request: Request) -> int: |
| 184 | + """ |
| 185 | + This is called by "Scheduler->schedule" function to estimate the number of required blocks. |
| 186 | + """ |
| 187 | + pass |
| 188 | + |
| 189 | + def update_state_after_alloc(self, request: Request, num_blocks: int): |
| 190 | + """ |
| 191 | + Update UcmSparse state after block allocation. |
| 192 | + """ |
| 193 | + pass |
| 194 | + |
| 195 | + def build_sparse_meta( |
| 196 | + self, |
| 197 | + scheduler_output: SchedulerOutput, |
| 198 | + requests: dict[str, CachedRequestState], |
| 199 | + input_batch: InputBatch, |
| 200 | + ) -> UcmSparseMetadata: |
| 201 | + """ |
| 202 | + Build the sparse metadata for this step. |
| 203 | + """ |
| 204 | + pass |
0 commit comments