protocol.py 11.7 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
import asyncio
from abc import ABC, abstractmethod
6
from typing import AsyncGenerator, Mapping, Optional
7

8
from vllm.beam_search import BeamSearchSequence, create_sort_beams_key_function
9
from vllm.config import DecodingConfig, ModelConfig, VllmConfig
10
from vllm.core.scheduler import SchedulerOutputs
11
from vllm.inputs.data import PromptType, TokensPrompt
12
from vllm.inputs.parse import is_explicit_encoder_decoder_prompt
13
from vllm.inputs.preprocess import InputPreprocessor
14
from vllm.logger import init_logger
15
from vllm.lora.request import LoRARequest
16
from vllm.model_executor.layers.sampler import SamplerOutput
17
from vllm.outputs import CompletionOutput, PoolingRequestOutput, RequestOutput
18
19
from vllm.pooling_params import PoolingParams
from vllm.prompt_adapter.request import PromptAdapterRequest
20
from vllm.sampling_params import BeamSearchParams, SamplingParams
21
from vllm.transformers_utils.tokenizer import AnyTokenizer
22
from vllm.utils import Device, collect_from_async_generator, random_uuid
23

24
logger = init_logger(__name__)
25

26
27

class EngineClient(ABC):
28
    """Protocol class for Clients to Engine"""
29
30

    @property
31
    @abstractmethod
32
33
34
35
    def is_running(self) -> bool:
        ...

    @property
36
    @abstractmethod
37
38
39
40
    def is_stopped(self) -> bool:
        ...

    @property
41
    @abstractmethod
42
43
44
    def errored(self) -> bool:
        ...

45
    @property
46
    @abstractmethod
47
48
    def dead_error(self) -> BaseException:
        ...
49

50
    @abstractmethod
51
    def generate(
52
        self,
53
        prompt: PromptType,
54
55
56
57
        sampling_params: SamplingParams,
        request_id: str,
        lora_request: Optional[LoRARequest] = None,
        trace_headers: Optional[Mapping[str, str]] = None,
58
59
        prompt_adapter_request: Optional[PromptAdapterRequest] = None,
        priority: int = 0,
60
    ) -> AsyncGenerator[RequestOutput, None]:
61
        """Generate outputs for a request."""
62
        ...
63

64
65
    async def beam_search(
        self,
66
        prompt: PromptType,
67
68
        request_id: str,
        params: BeamSearchParams,
69
        lora_request: Optional[LoRARequest] = None,
70
71
72
73
74
75
76
    ) -> AsyncGenerator[RequestOutput, None]:

        beam_width = params.beam_width
        max_tokens = params.max_tokens
        ignore_eos = params.ignore_eos
        temperature = params.temperature
        length_penalty = params.length_penalty
77
        include_stop_str_in_output = params.include_stop_str_in_output
78

79
80
81
        preprocessor = await self.get_input_preprocessor()
        tokenizer_group = preprocessor.get_tokenizer_group()
        tokenizer = await tokenizer_group.get_lora_tokenizer_async()
82

83
84
85
        if is_explicit_encoder_decoder_prompt(prompt):
            raise NotImplementedError
        else:
86
            processed_inputs = preprocessor._prompt_to_llm_inputs(prompt)
87

88
89
90
        if processed_inputs["type"] == "embeds":
            raise NotImplementedError

91
92
93
94
95
96
97
98
99
100
101
        # This is a workaround to fix multimodal beam search; this is a
        # bandaid fix for 2 small problems:
        # 1. Multi_modal_data on the processed_inputs currently resolves to
        #    `None`.
        # 2. preprocessing above expands the multimodal placeholders. However,
        #    this happens again in generation, so the double expansion causes
        #    a mismatch.
        # TODO - would be ideal to handle this more gracefully.
        prompt_token_ids = prompt.get("prompt_token_ids")
        multi_modal_data = prompt.get("multi_modal_data")

102
103
104
        prompt_text = processed_inputs.get("prompt")
        mm_processor_kwargs = processed_inputs.get("mm_processor_kwargs")

105
        tokenized_length = len(prompt_token_ids)
106
107
108
109

        sort_beams_key = create_sort_beams_key_function(
            tokenizer.eos_token_id, length_penalty)

110
111
112
113
114
        beam_search_params = SamplingParams(
            logprobs=2 * beam_width,
            max_tokens=1,
            temperature=temperature,
        )
115
        all_beams = [
116
117
            BeamSearchSequence(tokens=prompt_token_ids,
                               cum_logprob=0,
118
                               logprobs=[],
119
                               multi_modal_data=multi_modal_data,
120
121
                               mm_processor_kwargs=mm_processor_kwargs,
                               lora_request=lora_request)
122
        ]
123
124
125
        completed = []

        for _ in range(max_tokens):
126
            prompts_batch, lora_req_batch = zip(*[(
127
128
                TokensPrompt(prompt_token_ids=beam.tokens,
                             multi_modal_data=beam.multi_modal_data,
129
130
131
                             mm_processor_kwargs=beam.mm_processor_kwargs),
                beam.lora_request,
            ) for beam in all_beams])
132
133
134
135

            tasks = []

            request_id = f"beam_search-{random_uuid()}"
136
137
            for i, (individual_prompt,
                    lora_req) in enumerate(zip(prompts_batch, lora_req_batch)):
138
139
140
                request_id_item = f"{request_id}-{i}"
                task = asyncio.create_task(
                    collect_from_async_generator(
141
142
143
144
                        self.generate(individual_prompt,
                                      beam_search_params,
                                      request_id_item,
                                      lora_request=lora_req)))
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                tasks.append(task)

            output = await asyncio.gather(*tasks)

            output = [x[0] for x in output]

            new_beams = []
            for i, current_beam in enumerate(all_beams):
                result = output[i]

                if result.outputs[0].logprobs is not None:
                    logprobs = result.outputs[0].logprobs[0]
                    for token_id, logprob_obj in logprobs.items():
                        if token_id == tokenizer.eos_token_id and \
                            not ignore_eos:
160
161
162
163
164
165
166
167
168
169
170
                            completed.append(
                                BeamSearchSequence(
                                    tokens=current_beam.tokens +
                                    [token_id] if include_stop_str_in_output
                                    else current_beam.tokens,
                                    logprobs=current_beam.logprobs +
                                    [logprobs],
                                    cum_logprob=current_beam.cum_logprob +
                                    logprob_obj.logprob,
                                    finish_reason="stop",
                                    stop_reason=tokenizer.eos_token_id))
171
                        else:
172
173
174
175
176
                            new_beams.append(
                                BeamSearchSequence(
                                    tokens=current_beam.tokens + [token_id],
                                    logprobs=current_beam.logprobs +
                                    [logprobs],
177
                                    lora_request=current_beam.lora_request,
178
179
180
181
182
183
                                    cum_logprob=current_beam.cum_logprob +
                                    logprob_obj.logprob,
                                    multi_modal_data=current_beam.
                                    multi_modal_data,
                                    mm_processor_kwargs=current_beam.
                                    mm_processor_kwargs))
184
185
186
187
188
189
190
191
192

            sorted_beams = sorted(new_beams, key=sort_beams_key, reverse=True)
            all_beams = sorted_beams[:beam_width]

        completed.extend(all_beams)
        sorted_completed = sorted(completed, key=sort_beams_key, reverse=True)
        best_beams = sorted_completed[:beam_width]

        for beam in best_beams:
Robert Shaw's avatar
Robert Shaw committed
193
194
195
196
197
198
            if (beam.tokens[-1] == tokenizer.eos_token_id and not ignore_eos):
                # Skip the eos token in the text.
                tokens = beam.tokens[tokenized_length:-1]
            else:
                tokens = beam.tokens[tokenized_length:]
            beam.text = tokenizer.decode(tokens)
199
200
201

        beam_search_output = RequestOutput(
            request_id=request_id,
202
            prompt=prompt_text,
203
            outputs=[
204
205
206
207
208
209
210
211
212
                CompletionOutput(text=beam.text,
                                 cumulative_logprob=beam.cum_logprob,
                                 token_ids=beam.tokens[tokenized_length:],
                                 index=i,
                                 logprobs=beam.logprobs,
                                 finish_reason=beam.finish_reason if
                                 beam.finish_reason is not None else "length",
                                 stop_reason=beam.stop_reason)
                for (i, beam) in enumerate(best_beams)
213
214
            ],
            finished=True,
215
            prompt_token_ids=prompt_token_ids,
216
217
218
219
220
            prompt_logprobs=None)

        yield beam_search_output

    @abstractmethod
221
    def encode(
222
        self,
223
        prompt: PromptType,
224
225
226
227
        pooling_params: PoolingParams,
        request_id: str,
        lora_request: Optional[LoRARequest] = None,
        trace_headers: Optional[Mapping[str, str]] = None,
228
        priority: int = 0,
229
    ) -> AsyncGenerator[PoolingRequestOutput, None]:
230
        """Generate outputs for a request from a pooling model."""
231
        ...
232

233
    @abstractmethod
234
235
236
237
238
239
    async def abort(self, request_id: str) -> None:
        """Abort a request.

        Args:
            request_id: The unique id of the request.
        """
240
        ...
241
242
243
244
245

    @abstractmethod
    async def get_vllm_config(self) -> VllmConfig:
        """Get the vllm configuration of the vLLM engine."""
        ...
246

247
    @abstractmethod
248
249
    async def get_model_config(self) -> ModelConfig:
        """Get the model configuration of the vLLM engine."""
250
        ...
251

252
    @abstractmethod
253
254
    async def get_decoding_config(self) -> DecodingConfig:
        """Get the decoding configuration of the vLLM engine."""
255
256
257
258
259
260
        ...

    @abstractmethod
    async def get_input_preprocessor(self) -> InputPreprocessor:
        """Get the input processor of the vLLM engine."""
        ...
261

262
    @abstractmethod
263
264
265
    async def get_tokenizer(
        self,
        lora_request: Optional[LoRARequest] = None,
266
267
268
    ) -> AnyTokenizer:
        """Get the appropriate tokenizer for the request"""
        ...
269

270
    @abstractmethod
271
    async def is_tracing_enabled(self) -> bool:
272
        ...
273

274
    @abstractmethod
275
276
277
    async def do_log_stats(
        self,
        scheduler_outputs: Optional[SchedulerOutputs] = None,
278
        model_output: Optional[list[SamplerOutput]] = None,
279
    ) -> None:
280
        ...
281

282
    @abstractmethod
283
284
    async def check_health(self) -> None:
        """Raise if unhealthy"""
285
        ...
286

287
    @abstractmethod
288
289
290
291
    async def start_profile(self) -> None:
        """Start profiling the engine"""
        ...

292
    @abstractmethod
293
294
295
    async def stop_profile(self) -> None:
        """Start profiling the engine"""
        ...
296

297
298
299
300
301
    @abstractmethod
    async def reset_mm_cache(self) -> None:
        """Reset the multi-modal cache"""
        ...

302
    @abstractmethod
303
304
    async def reset_prefix_cache(self,
                                 device: Optional[Device] = None) -> None:
305
306
307
        """Reset the prefix cache"""
        ...

308
309
310
311
312
313
    @abstractmethod
    async def sleep(self, level: int = 1) -> None:
        """Sleep the engine"""
        ...

    @abstractmethod
314
    async def wake_up(self, tags: Optional[list[str]] = None) -> None:
315
316
317
        """Wake up the engine"""
        ...

318
319
320
321
322
    @abstractmethod
    async def is_sleeping(self) -> bool:
        """Check whether the engine is sleeping"""
        ...

323
324
325
326
    @abstractmethod
    async def add_lora(self, lora_request: LoRARequest) -> None:
        """Load a new LoRA adapter into the engine for future requests."""
        ...
327
328
329
330
331
332

    async def scale_elastic_ep(self,
                               new_data_parallel_size: int,
                               drain_timeout: int = 300) -> None:
        """Scale the engine"""
        raise NotImplementedError