Skip to content

Commit 3a581e9

Browse files
authored
Dynamic model class loading (#101)
1 parent 0147f94 commit 3a581e9

6 files changed

Lines changed: 40 additions & 28 deletions

File tree

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies = [
2020
[project.optional-dependencies]
2121
srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn",
2222
"zmq", "vllm>=0.2.5", "interegular", "lark", "numba",
23-
"pydantic", "diskcache", "cloudpickle"]
23+
"pydantic", "diskcache", "cloudpickle", "pillow"]
2424
openai = ["openai>=1.0", "numpy"]
2525
anthropic = ["anthropic", "numpy"]
2626
all = ["sglang[srt]", "sglang[openai]", "sglang[anthropic]"]

python/sglang/srt/managers/router/model_runner.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import importlib
12
import logging
23
from dataclasses import dataclass
3-
from enum import Enum, auto
4+
from functools import lru_cache
5+
from pathlib import Path
46
from typing import List
57

68
import numpy as np
79
import torch
10+
import sglang
811
from sglang.srt.managers.router.infer_batch import Batch, ForwardMode
912
from sglang.srt.memory_pool import ReqToTokenPool, TokenToKVPool
1013
from sglang.srt.utils import is_multimodal_model
@@ -20,6 +23,32 @@
2023
global_model_mode: List[str] = []
2124

2225

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+
2352
@dataclass
2453
class InputMetadata:
2554
model_runner: "ModelRunner"
@@ -237,34 +266,9 @@ def __init__(
237266

238267
def load_model(self):
239268
"""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-
245269
# Select model class
246270
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)
268272
logger.info(f"Rank {self.tp_rank}: load weight begin.")
269273

270274
# Load weights

python/sglang/srt/models/llama2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,5 @@ def load_weights(
318318
param = params_dict[name]
319319
weight_loader = getattr(param, "weight_loader", default_weight_loader)
320320
weight_loader(param, loaded_weight)
321+
322+
EntryClass = LlamaForCausalLM

python/sglang/srt/models/llava.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,5 @@ def monkey_path_clip_vision_embed_forward():
330330
"forward",
331331
clip_vision_embed_forward,
332332
)
333+
334+
EntryClass = LlavaLlamaForCausalLM

python/sglang/srt/models/mixtral.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,5 @@ def load_weights(
376376
param = params_dict[name]
377377
weight_loader = getattr(param, "weight_loader", default_weight_loader)
378378
weight_loader(param, loaded_weight)
379+
380+
EntryClass = MixtralForCausalLM

python/sglang/srt/models/qwen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,5 @@ def load_weights(
258258
param = params_dict[name]
259259
weight_loader = getattr(param, "weight_loader", default_weight_loader)
260260
weight_loader(param, loaded_weight)
261+
262+
EntryClass = QWenLMHeadModel

0 commit comments

Comments
 (0)