|
| 1 | +import importlib |
1 | 2 | import logging |
2 | 3 | from dataclasses import dataclass |
3 | | -from enum import Enum, auto |
| 4 | +from functools import lru_cache |
| 5 | +from pathlib import Path |
4 | 6 | from typing import List |
5 | 7 |
|
6 | 8 | import numpy as np |
7 | 9 | import torch |
| 10 | +import sglang |
8 | 11 | from sglang.srt.managers.router.infer_batch import Batch, ForwardMode |
9 | 12 | from sglang.srt.memory_pool import ReqToTokenPool, TokenToKVPool |
10 | 13 | from sglang.srt.utils import is_multimodal_model |
|
20 | 23 | global_model_mode: List[str] = [] |
21 | 24 |
|
22 | 25 |
|
| 26 | +@lru_cache() |
| 27 | +def import_model_classes(): |
| 28 | + model_arch_name_to_cls = {} |
| 29 | + for module_path in (Path(sglang.__file__).parent / "srt" / "models").glob("*.py"): |
| 30 | + module = importlib.import_module(f"sglang.srt.models.{module_path.stem}") |
| 31 | + if hasattr(module, "EntryClass"): |
| 32 | + model_arch_name_to_cls[module.EntryClass.__name__] = module.EntryClass |
| 33 | + return model_arch_name_to_cls |
| 34 | + |
| 35 | + |
| 36 | +def get_model_cls_by_arch_name(model_arch_names): |
| 37 | + model_arch_name_to_cls = import_model_classes() |
| 38 | + |
| 39 | + model_class = None |
| 40 | + for arch in model_arch_names: |
| 41 | + if arch in model_arch_name_to_cls: |
| 42 | + model_class = model_arch_name_to_cls[arch] |
| 43 | + break |
| 44 | + else: |
| 45 | + raise ValueError( |
| 46 | + f"Unsupported architectures: {arch}. " |
| 47 | + f"Supported list: {list(model_arch_name_to_cls.keys())}" |
| 48 | + ) |
| 49 | + return model_class |
| 50 | + |
| 51 | + |
23 | 52 | @dataclass |
24 | 53 | class InputMetadata: |
25 | 54 | model_runner: "ModelRunner" |
@@ -237,34 +266,9 @@ def __init__( |
237 | 266 |
|
238 | 267 | def load_model(self): |
239 | 268 | """See also vllm/model_executor/model_loader.py::get_model""" |
240 | | - from sglang.srt.models.llama2 import LlamaForCausalLM |
241 | | - from sglang.srt.models.llava import LlavaLlamaForCausalLM |
242 | | - from sglang.srt.models.mixtral import MixtralForCausalLM |
243 | | - from sglang.srt.models.qwen import QWenLMHeadModel |
244 | | - |
245 | 269 | # Select model class |
246 | 270 | architectures = getattr(self.model_config.hf_config, "architectures", []) |
247 | | - |
248 | | - model_class = None |
249 | | - for arch in architectures: |
250 | | - if arch == "LlamaForCausalLM": |
251 | | - model_class = LlamaForCausalLM |
252 | | - break |
253 | | - if arch == "MistralForCausalLM": |
254 | | - model_class = LlamaForCausalLM |
255 | | - break |
256 | | - if arch == "LlavaLlamaForCausalLM": |
257 | | - model_class = LlavaLlamaForCausalLM |
258 | | - break |
259 | | - if arch == "MixtralForCausalLM": |
260 | | - model_class = MixtralForCausalLM |
261 | | - break |
262 | | - if arch == "QWenLMHeadModel": |
263 | | - model_class = QWenLMHeadModel |
264 | | - break |
265 | | - if model_class is None: |
266 | | - raise ValueError(f"Unsupported architectures: {architectures}") |
267 | | - |
| 271 | + model_class = get_model_cls_by_arch_name(architectures) |
268 | 272 | logger.info(f"Rank {self.tp_rank}: load weight begin.") |
269 | 273 |
|
270 | 274 | # Load weights |
|
0 commit comments