base_engine.py 3.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16

17
import abc
18
import logging
19
20
21

from common.chat_processor import ChatProcessor
from vllm.engine.arg_utils import AsyncEngineArgs
22
23
24
25
26
from vllm.entrypoints.openai.api_server import (
    build_async_engine_client_from_engine_args,
)

logger = logging.getLogger("vllm")
27
28
29
30
31
32
33
34


class BaseVllmEngine:
    """
    Request handler for the generate endpoint
    """

    def __init__(self, engine_args: AsyncEngineArgs):
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        self.engine_args = engine_args
        self.model_config = self.engine_args.create_model_config()
        self.engine_client = None
        self.chat_processor = None
        self._engine_context = None

    async def initialize(self):
        """Initialize the engine client and related components."""
        print("Initializing engine client")
        self._engine_context = build_async_engine_client_from_engine_args(
            self.engine_args
        )
        if self._engine_context is not None:
            self.engine_client = await self._engine_context.__aenter__()
            self.chat_processor = ChatProcessor(self.engine_client, self.model_config)
        else:
            raise RuntimeError("Failed to initialize engine client")

    async def cleanup(self):
        """Cleanup resources."""
        print("Cleaning up engine client")
        if self._engine_context is not None:
            await self._engine_context.__aexit__(None, None, None)
            self._engine_context = None
            self.engine_client = None
            self.chat_processor = None

    async def __aenter__(self):
        await self.initialize()
        """Initialize with context manager syntax."""
        return self

    async def __aexit__(self, exc_type, exc_value, traceback):
        await self.cleanup()
69
70

    async def _parse_raw_request(self, raw_request):
71
        assert self.engine_client is not None
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        request = self.chat_processor.parse_raw_request(raw_request)
        (
            conversation,
            request_prompt,
            engine_prompt,
        ) = await self.chat_processor.preprocess(raw_request)
        default_max_tokens = self.model_config.max_model_len - len(
            engine_prompt["prompt_token_ids"]
        )
        default_sampling_params = self.model_config.get_diff_sampling_param()
        sampling_params = request.to_sampling_params(
            default_max_tokens,
            self.model_config.logits_processor_pattern,
            default_sampling_params,
        )
        return request, conversation, request_prompt, engine_prompt, sampling_params

    async def _stream_response(self, request, generator, request_id, conversation):
90
        assert self.engine_client is not None
91
92
93
94
95
96
97
98
99
100
        return self.chat_processor.stream_response(
            request,
            generator,
            request_id,
            conversation,
        )

    @abc.abstractmethod
    async def generate(self, raw_request):
        pass