prefill_worker.py 2.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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.
import logging

17
from common.base_engine import BaseEngineConfig, BaseTensorrtLLMEngine
18
19
20
from common.parser import parse_tensorrt_llm_args
from common.protocol import TRTLLMWorkerRequest

21
from dynamo.sdk import async_on_start, dynamo_context, endpoint, on_shutdown, service
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from dynamo.sdk.lib.config import ServiceConfig

logger = logging.getLogger(__name__)


@service(
    dynamo={
        "namespace": "dynamo",
    },
    resources={"gpu": 1, "cpu": "10", "memory": "20Gi"},
    workers=1,
)
class TensorRTLLMPrefillWorker(BaseTensorrtLLMEngine):
    def __init__(self):
        logger.info("Initializing TensorRT-LLM Prefill Worker")
        class_name = self.__class__.__name__
        config = ServiceConfig.get_instance()
        config_args = config.as_args(class_name, prefix="")
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        args = parse_tensorrt_llm_args(config_args)
        lease_id = dynamo_context["endpoints"][0].lease_id()
        namespace, _ = TensorRTLLMPrefillWorker.dynamo_address()  # type: ignore

        engine_config = BaseEngineConfig(
            namespace=namespace,
            component=class_name,
            endpoint="generate",
            model_path=args.model_path,
            served_model_name=args.served_model_name,
            kv_block_size=args.kv_block_size,
            extra_engine_args=args.extra_engine_args,
            publish_events_and_metrics=False,
            disaggregation_mode="prefill",
            remote_prefill_endpoint=None,
            lease_id=lease_id,
56
57
        )

58
59
        super().__init__(config=engine_config)

60
61
    @async_on_start
    async def async_init(self):
62
63
        runtime = dynamo_context["runtime"]
        await self.initialize(runtime)
64
65
        logger.info("TensorRT-LLM Prefill Worker initialized")

66
67
68
69
70
    @on_shutdown
    async def async_cleanup(self):
        logger.info("Cleaning up TensorRT-LLM Prefill Worker")
        await self.cleanup()
        logger.info("TensorRT-LLM Prefill Worker cleanup completed")
71

72
    @endpoint()
73
74
75
    async def generate(self, request: TRTLLMWorkerRequest):
        async for response in super().generate(request):
            yield response