"vllm/vscode:/vscode.git/clone" did not exist on "2a602b055a180c982126e8a438d188325fdb01a5"
executor_base.py 4.88 KB
Newer Older
1
import asyncio
2
from abc import ABC, abstractmethod
3
from typing import List, Optional, Set, Tuple
4

5
from vllm.config import (CacheConfig, DeviceConfig, LoadConfig, LoRAConfig,
6
7
                         ModelConfig, MultiModalConfig, ParallelConfig,
                         SchedulerConfig, SpeculativeConfig)
8
from vllm.lora.request import LoRARequest
9
from vllm.sequence import ExecuteModelRequest, SamplerOutput
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


class ExecutorBase(ABC):
    """Base class for all executors.

    An executor is responsible for executing the model on a specific device
    type (e.g., CPU, GPU, Neuron, etc.). Or it can be a distributed executor
    that can execute the model on multiple devices.
    """

    def __init__(
        self,
        model_config: ModelConfig,
        cache_config: CacheConfig,
        parallel_config: ParallelConfig,
        scheduler_config: SchedulerConfig,
        device_config: DeviceConfig,
27
        load_config: LoadConfig,
28
        lora_config: Optional[LoRAConfig],
29
        multimodal_config: Optional[MultiModalConfig],
30
        speculative_config: Optional[SpeculativeConfig],
31
    ) -> None:
32
33
34
        self.model_config = model_config
        self.cache_config = cache_config
        self.lora_config = lora_config
35
        self.load_config = load_config
36
37
38
        self.parallel_config = parallel_config
        self.scheduler_config = scheduler_config
        self.device_config = device_config
39
        self.multimodal_config = multimodal_config
40
41
42
43
44
45
46
        self.speculative_config = speculative_config

        self._init_executor()

    @abstractmethod
    def _init_executor(self) -> None:
        pass
47

48
    @abstractmethod
49
    def determine_num_available_blocks(self) -> Tuple[int, int]:
50
51
52
53
54
55
56
        """Determine the number of available blocks for the GPU KV cache and
        swappable CPU KV cache.

        Normally, this should simply delegate to the underlying Worker. Some
        ExecutorBase may require modification of the result, e.g. to ensure the
        selected cache sizes are compatible with all workers.

57
        Returns a Tuple[num_gpu_blocks, num_cpu_blocks], where num_gpu_blocks
58
59
60
61
62
63
64
65
66
67
68
69
70
        are blocks that are "active" on the device and can be appended to.
        num_cpu_blocks refers to "swapped" blocks in CPU memory and cannot be
        appended to.
        """
        raise NotImplementedError

    @abstractmethod
    def initialize_cache(self, num_gpu_blocks: int,
                         num_cpu_blocks: int) -> None:
        """Initialize the KV cache with the given size in blocks.
        """
        raise NotImplementedError

71
    @abstractmethod
72
    def execute_model(
73
74
        self, execute_model_req: ExecuteModelRequest
    ) -> Optional[List[SamplerOutput]]:
75
        """Executes at least one model step on the given sequences."""
76
77
        raise NotImplementedError

78
79
80
81
    def stop_remote_worker_execution_loop(self) -> None:
        """Releases parallel workers from model loop."""
        return

82
83
84
85
86
87
88
89
    @abstractmethod
    def add_lora(self, lora_request: LoRARequest) -> bool:
        raise NotImplementedError

    @abstractmethod
    def remove_lora(self, lora_id: int) -> bool:
        raise NotImplementedError

90
91
92
93
    @abstractmethod
    def pin_lora(self, lora_id: int) -> bool:
        raise NotImplementedError  # type: ignore

94
    @abstractmethod
95
    def list_loras(self) -> Set[int]:
96
97
98
99
100
101
102
103
        raise NotImplementedError

    @abstractmethod
    def check_health(self) -> None:
        """Checks if the executor is healthy. If not, it should raise an
        exception."""
        raise NotImplementedError

104
105
106
107
108
109
110
    def shutdown(self) -> None:
        """Shutdown the executor."""
        return

    def __del__(self):
        self.shutdown()

111
112
113

class ExecutorAsyncBase(ExecutorBase):

114
115
116
117
118
119
120
121
122
    def __init__(
        self,
        model_config: ModelConfig,
        cache_config: CacheConfig,
        parallel_config: ParallelConfig,
        scheduler_config: SchedulerConfig,
        device_config: DeviceConfig,
        load_config: LoadConfig,
        lora_config: Optional[LoRAConfig],
123
        multimodal_config: Optional[MultiModalConfig],
124
125
        speculative_config: Optional[SpeculativeConfig],
    ) -> None:
126
        self.pp_locks: Optional[List[asyncio.Lock]] = None
127
128
129

        super().__init__(model_config, cache_config, parallel_config,
                         scheduler_config, device_config, load_config,
130
                         lora_config, multimodal_config, speculative_config)
131

132
133
    @abstractmethod
    async def execute_model_async(
134
135
            self,
            execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]:
136
137
138
        """Executes one model step on the given sequences."""
        raise NotImplementedError

139
140
141
142
    async def stop_remote_worker_execution_loop_async(self) -> None:
        """Releases parallel workers from model loop."""
        return

143
144
145
    async def check_health_async(self) -> None:
        """Checks if the executor is healthy. If not, it should raise an
        exception."""
146
        self.check_health()