neuron_executor.py 4.26 KB
Newer Older
1
from typing import List, Set, Tuple
2

3
from vllm.executor.executor_base import ExecutorAsyncBase, ExecutorBase
4
from vllm.logger import init_logger
5
from vllm.lora.request import LoRARequest
6
7
from vllm.model_executor.layers.sampler import SamplerOutput
from vllm.sequence import ExecuteModelRequest
8
9
from vllm.utils import (get_distributed_init_method, get_ip, get_open_port,
                        make_async)
10
11
12
13
14
15

logger = init_logger(__name__)


class NeuronExecutor(ExecutorBase):

16
17
    uses_ray: bool = False

18
19
20
21
    def _init_executor(self) -> None:
        assert (self.lora_config is
                None), "LoRA is not supported for Neuron backend."
        assert (not self.speculative_config
22
                ), "Speculative decoding not yet supported for Neuron backend."
23
24
25
26
27
28

        # Instantiate the worker and load the model to the device.
        self._init_worker()

    def _init_worker(self):
        from vllm.worker.neuron_worker import NeuronWorker
29
30
        distributed_init_method = get_distributed_init_method(
            get_ip(), get_open_port())
31
        self.driver_worker = NeuronWorker(
32
33
34
35
36
37
38
39
            model_config=self.model_config,
            parallel_config=self.parallel_config,
            scheduler_config=self.scheduler_config,
            device_config=self.device_config,
            cache_config=self.cache_config,
            local_rank=0,
            rank=0,
            distributed_init_method=distributed_init_method)
40
41
42
        self.driver_worker.init_device()
        self.driver_worker.load_model()

43
    def determine_num_available_blocks(self) -> Tuple[int, int]:
44
45
46
47
48
49
50
51
52
53
54
        """Determine the number of available KV blocks by invoking the
        underlying worker.
        """
        return self.driver_worker.determine_num_available_blocks()

    def initialize_cache(self, num_gpu_blocks: int,
                         num_cpu_blocks: int) -> None:
        """Initialize the KV cache by invoking the underlying worker.
        """
        self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)

55
56
57
    def execute_model(
            self,
            execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]:
58
59
60
        assert (not execute_model_req.blocks_to_swap_in
                and not execute_model_req.blocks_to_swap_out
                and not execute_model_req.blocks_to_copy), (
61
                    "Cache operations are not supported for Neuron backend.")
62
        assert execute_model_req.num_lookahead_slots == 0, (
63
            "lookahead not supported for Neuron backend.")
64

65
        output = self.driver_worker.execute_model(execute_model_req)
66
67
68
        return output

    def add_lora(self, lora_request: LoRARequest) -> bool:
69
        return self.driver_worker.add_lora(lora_request)
70
71

    def remove_lora(self, lora_id: int) -> bool:
72
        return self.driver_worker.remove_lora(lora_id)
73

74
75
76
    def pin_lora(self, lora_id: int) -> bool:
        return self.driver_worker.pin_lora(lora_id)

77
    def list_loras(self) -> Set[int]:
78
        return self.driver_worker.list_loras()
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    def add_prompt_adapter(self, prompt_adapter_request) -> bool:
        raise NotImplementedError(
            "Soft prompt is currently not supported by the Neuron backend.")

    def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool:
        raise NotImplementedError(
            "Soft prompt is currently not supported by the Neuron backend.")

    def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool:
        raise NotImplementedError(
            "Soft prompt is currently not supported by the Neuron backend.")

    def list_prompt_adapters(self) -> Set[int]:
        raise NotImplementedError(
            "Soft prompt is currently not supported by the Neuron backend.")

96
97
98
99
    def check_health(self) -> None:
        # NeuronExecutor will always be healthy as long as
        # it's running.
        return
100
101
102
103
104
105


class NeuronExecutorAsync(NeuronExecutor, ExecutorAsyncBase):

    async def execute_model_async(
        self,
106
        execute_model_req: ExecuteModelRequest,
107
    ) -> List[SamplerOutput]:
108
109
        output = await make_async(self.driver_worker.execute_model
                                  )(execute_model_req=execute_model_req, )
110
111
112
113
114
115
        return output

    async def check_health_async(self) -> None:
        # NeuronExecutor will always be healthy as long as
        # it's running.
        return