Skip to content

Commit 74e240a

Browse files
committed
add fd plugins && rm model_classed
1 parent fe17410 commit 74e240a

File tree

21 files changed

+349
-50
lines changed

21 files changed

+349
-50
lines changed

fastdeploy/__init__.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
os.environ["GLOG_minloglevel"] = "2"
2323
# suppress log from aistudio
2424
os.environ["AISTUDIO_LOG"] = "critical"
25+
import typing
26+
2527
from fastdeploy.engine.sampling_params import SamplingParams
2628
from fastdeploy.entrypoints.llm import LLM
27-
from fastdeploy.utils import version
28-
29-
__all__ = ["LLM", "SamplingParams", "version"]
3029

3130
try:
3231
import use_triton_in_paddle
@@ -86,3 +85,30 @@ def _patch_fastsafetensors():
8685

8786

8887
_patch_fastsafetensors()
88+
89+
90+
MODULE_ATTRS = {
91+
"ModelRegistry": ".model_executor.models.model_base:ModelRegistry",
92+
}
93+
94+
95+
if typing.TYPE_CHECKING:
96+
from fastdeploy.model_executor.models.model_base import ModelRegistry
97+
else:
98+
99+
def __getattr__(name: str) -> typing.Any:
100+
from importlib import import_module
101+
102+
if name in MODULE_ATTRS:
103+
module_name, attr_name = MODULE_ATTRS[name].split(":")
104+
module = import_module(module_name, __package__)
105+
return getattr(module, attr_name)
106+
else:
107+
raise AttributeError(f"module {__package__} has no attribute {name}")
108+
109+
110+
__all__ = [
111+
"LLM",
112+
"SamplingParams",
113+
"ModelRegistry",
114+
]

fastdeploy/envs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
"EXPORTER_OTLP_HEADERS": lambda: os.getenv("EXPORTER_OTLP_HEADERS"),
8181
# enable kv cache block scheduler v1 (no need for kv_cache_ratio)
8282
"ENABLE_V1_KVCACHE_SCHEDULER": lambda: int(os.getenv("ENABLE_V1_KVCACHE_SCHEDULER", "0")),
83+
# Whether to use PLUGINS.
84+
"FD_PLUGINS": lambda: None if "FD_PLUGINS" not in os.environ else os.environ["FD_PLUGINS"].split(","),
8385
}
8486

8587

fastdeploy/model_executor/model_loader/default_loader.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
measure_time,
2525
)
2626
from fastdeploy.model_executor.model_loader.base_loader import BaseModelLoader
27-
from fastdeploy.model_executor.model_loader.utils import get_pretrain_cls
2827
from fastdeploy.model_executor.models.model_base import ModelRegistry
2928
from fastdeploy.platforms import current_platform
3029

@@ -52,7 +51,7 @@ def clean_memory_fragments(self, state_dict: dict) -> None:
5251

5352
@measure_time
5453
def load_weights(self, model, fd_config: FDConfig, architectures: str) -> None:
55-
model_class = get_pretrain_cls(architectures)
54+
model_class = ModelRegistry.get_pretrain_cls(architectures)
5655
state_dict = load_composite_checkpoint(
5756
fd_config.model_config.model,
5857
model_class,

fastdeploy/model_executor/model_loader/utils.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

fastdeploy/model_executor/models/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import os
2020
from pathlib import Path
2121

22+
from paddleformers.transformers import PretrainedModel
23+
2224
from .model_base import ModelForCasualLM, ModelRegistry
2325

2426

@@ -44,7 +46,14 @@ def auto_models_registry(dir_path, register_path="fastdeploy.model_executor.mode
4446
for attr_name in dir(module):
4547
attr = getattr(module, attr_name)
4648
if inspect.isclass(attr) and issubclass(attr, ModelForCasualLM) and attr is not ModelForCasualLM:
47-
ModelRegistry.register(attr)
49+
ModelRegistry.register_model_class(attr)
50+
if (
51+
inspect.isclass(attr)
52+
and issubclass(attr, PretrainedModel)
53+
and attr is not PretrainedModel
54+
and hasattr(attr, "arch_name")
55+
):
56+
ModelRegistry.register_pretrained_model(attr)
4857
except ImportError:
4958
raise ImportError(f"{module_file=} import error")
5059

fastdeploy/model_executor/models/deepseek_v3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ def _init_weight(self, layer):
673673
"""
674674
return None
675675

676+
@classmethod
677+
def arch_name(self):
678+
return "DeepseekV3ForCausalLM"
679+
676680
@classmethod
677681
def _get_tensor_parallel_mappings(cls, config, is_split=True):
678682

fastdeploy/model_executor/models/ernie4_5_moe.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ def _init_weight(self, layer):
473473
"""
474474
return None
475475

476+
@classmethod
477+
def arch_name(self):
478+
return "Ernie4_5_MoeForCausalLM"
479+
476480
weight_infos = [
477481
WeightMeta(
478482
f".layers.{{{layerid.LAYER_ID}}}.self_attn.qkv_proj.weight",
@@ -594,3 +598,16 @@ def get_tensor_parallel_split_mappings(num_layers, moe_num_experts, moe_layer_st
594598
config.prefix_name,
595599
)
596600
return mappings
601+
602+
603+
class Ernie4_5_DensePretrainedModel(Ernie4_5_PretrainedModel):
604+
"""
605+
Ernie4_5_DensePretrainedModel
606+
"""
607+
608+
@classmethod
609+
def arch_name(self):
610+
"""
611+
Model Architecture Name
612+
"""
613+
return "Ernie4_5_ForCausalLM"

fastdeploy/model_executor/models/ernie4_5_mtp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def _init_weight(self, layer):
4646
"""
4747
return None
4848

49+
@classmethod
50+
def arch_name(self):
51+
return "Ernie4_5_MTPForCausalLM"
52+
4953
@classmethod
5054
def _get_tensor_parallel_mappings(cls, config, is_split=True):
5155
"""

fastdeploy/model_executor/models/ernie4_5_vl/ernie4_5_vl_moe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ def _init_weight(self, layer):
624624
"""
625625
return None
626626

627+
@classmethod
628+
def arch_name(self):
629+
return "Ernie4_5_VLMoeForConditionalGeneration"
630+
627631
from fastdeploy.model_executor.models.tp_utils import TensorSplitMode as tsm
628632
from fastdeploy.model_executor.models.utils import LayerIdPlaceholder as layerid
629633
from fastdeploy.model_executor.models.utils import WeightMeta

fastdeploy/model_executor/models/model_base.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import numpy as np
2121
import paddle
2222
from paddle import nn
23+
from paddleformers.transformers import PretrainedModel
2324

2425

2526
class ModelRegistry:
@@ -28,21 +29,52 @@ class ModelRegistry:
2829
"""
2930

3031
_registry = {}
32+
_model_classes = {}
3133

3234
@classmethod
33-
def register(cls, model_class):
35+
def register_model_class(cls, model_class):
3436
"""register model class"""
3537
if issubclass(model_class, ModelForCasualLM) and model_class is not ModelForCasualLM:
38+
# print(model_class.name())
3639
cls._registry[model_class.name()] = model_class
3740
return model_class
3841

42+
@classmethod
43+
def register_pretrained_model(cls, pretrained_model):
44+
"""register pretrained model class"""
45+
if (
46+
issubclass(pretrained_model, PretrainedModel)
47+
and pretrained_model is not PretrainedModel
48+
and hasattr(pretrained_model, "arch_name")
49+
):
50+
# print(pretrained_model.arch_name())
51+
cls._model_classes[pretrained_model.arch_name()] = pretrained_model
52+
53+
return pretrained_model
54+
55+
@classmethod
56+
def register(cls, model_class, pretrained_model):
57+
"""register model class and pretrained model class"""
58+
cls.register_model_class(model_class)
59+
cls.register_pretrained_model(pretrained_model)
60+
61+
@classmethod
62+
def get_pretrain_cls(cls, architectures: str):
63+
"""get_pretrain_cls"""
64+
return cls._model_classes[architectures]
65+
3966
@classmethod
4067
def get_class(cls, name):
4168
"""get model class"""
4269
if name not in cls._registry:
4370
raise ValueError(f"Model '{name}' is not registered!")
4471
return cls._registry[name]
4572

73+
@classmethod
74+
def get_supported_archs(cls):
75+
assert len(cls._registry) == len(cls._registry), "model class / pretrained model registry num is not same"
76+
return [key for key in cls._registry.keys()]
77+
4678

4779
class ModelForCasualLM(nn.Layer, ABC):
4880
"""

0 commit comments

Comments
 (0)