main
py 70 lines 2.16 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from __future__ import annotations
5
6 import importlib
7
8 from torch import nn
9
10 from vllm.config import ModelConfig, VllmConfig
11 from vllm.config.load import LoadConfig
12 from vllm.model_executor.model_loader.base_loader import BaseModelLoader
13 from vllm.tracing import instrument
14
15 _MODELEXPRESS_LOADER_MODULE = "modelexpress.engines.vllm.loader"
16 _MISSING_MODELEXPRESS_MODULES = frozenset(
17 {
18 "modelexpress",
19 "modelexpress.engines",
20 "modelexpress.engines.vllm",
21 _MODELEXPRESS_LOADER_MODULE,
22 }
23 )
24
25
26 def _missing_modelexpress_error() -> ImportError:
27 return ImportError(
28 "The 'modelexpress' load format requires the ModelExpress Python package. "
29 "Install it with `pip install modelexpress`."
30 )
31
32
33 class ModelExpressModelLoader(BaseModelLoader):
34 """Thin vLLM loader wrapper for ModelExpress."""
35
36 def __init__(self, load_config: LoadConfig):
37 super().__init__(load_config)
38 self._loader = self._load_modelexpress_loader(load_config)
39
40 @staticmethod
41 def _load_modelexpress_loader(load_config: LoadConfig) -> BaseModelLoader:
42 try:
43 module = importlib.import_module(_MODELEXPRESS_LOADER_MODULE)
44 except ModuleNotFoundError as exc:
45 if exc.name not in _MISSING_MODELEXPRESS_MODULES:
46 raise
47 raise _missing_modelexpress_error() from exc
48
49 ModelExpressVllmLoader = module.MxModelLoader
50 return ModelExpressVllmLoader(load_config)
51
52 def download_model(self, model_config: ModelConfig) -> None:
53 self._loader.download_model(model_config)
54
55 def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None:
56 self._loader.load_weights(model, model_config)
57
58 @instrument(span_name="Load model")
59 def load_model(
60 self,
61 vllm_config: VllmConfig,
62 model_config: ModelConfig,
63 prefix: str = "",
64 ) -> nn.Module:
65 model = self._loader.load_model(
66 vllm_config=vllm_config,
67 model_config=model_config,
68 prefix=prefix,
69 )
70 return model.eval()