register_model_class compatible with plugins (#4236)
Some checks failed
CE Compile Job / ce_job_pre_check (push) Has been cancelled
CE Compile Job / print_ce_job_pre_check_outputs (push) Has been cancelled
CE Compile Job / FD-Clone-Linux (push) Has been cancelled
CE Compile Job / Show Code Archive Output (push) Has been cancelled
CE Compile Job / BUILD_SM8090 (push) Has been cancelled
CE Compile Job / BUILD_SM8689 (push) Has been cancelled
CE Compile Job / CE_UPLOAD (push) Has been cancelled
Deploy GitHub Pages / deploy (push) Has been cancelled

This commit is contained in:
Yuanle Liu
2025-09-24 11:17:12 +08:00
committed by GitHub
parent d6e59447f5
commit b455fd39f3
10 changed files with 25 additions and 28 deletions

View File

@@ -596,7 +596,7 @@ class DeepSeekV3Model(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="DeepseekV3ForCausalLM", architecture="DeepseekV3ForCausalLM",
module_path="deepseek_v3", module_name="deepseek_v3",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -484,7 +484,7 @@ class Ernie4_5_Model(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Ernie4_5_MoeForCausalLM", architecture="Ernie4_5_MoeForCausalLM",
module_path="ernie4_5_moe", module_name="ernie4_5_moe",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )
@@ -665,7 +665,7 @@ class Ernie4_5_MoeForCausalLM(ModelForCasualLM):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Ernie4_5_ForCausalLM", architecture="Ernie4_5_ForCausalLM",
module_path="ernie4_5_moe", module_name="ernie4_5_moe",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )
@@ -684,7 +684,7 @@ class Ernie4_5_ForCausalLM(Ernie4_5_MoeForCausalLM):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Ernie4_5ForCausalLM", architecture="Ernie4_5ForCausalLM",
module_path="ernie4_5_moe", module_name="ernie4_5_moe",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -331,7 +331,7 @@ class Ernie4_5_MTPModel(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Ernie4_5_MTPForCausalLM", architecture="Ernie4_5_MTPForCausalLM",
module_path="ernie4_5_mtp", module_name="ernie4_5_mtp",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -798,7 +798,7 @@ class Ernie4_5_VLMoeForConditionalGeneration(ModelForCasualLM):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Ernie4_5_VLMoeForConditionalGeneration", architecture="Ernie4_5_VLMoeForConditionalGeneration",
module_path="ernie4_5_vl.ernie4_5_vl_moe", module_name="ernie4_5_vl.ernie4_5_vl_moe",
category=ModelCategory.MULTIMODAL, category=ModelCategory.MULTIMODAL,
primary_use=ModelCategory.MULTIMODAL, primary_use=ModelCategory.MULTIMODAL,
) )

View File

@@ -375,7 +375,7 @@ class Glm4MoeModel(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Glm4MoeForCausalLM", architecture="Glm4MoeForCausalLM",
module_path="glm4_moe", module_name="glm4_moe",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -81,11 +81,12 @@ class LazyRegisteredModel(BaseRegisteredModel):
"""Lazy loaded model""" """Lazy loaded model"""
module_name: str module_name: str
module_path: str
class_name: str class_name: str
def load_model_cls(self) -> Type[nn.Layer]: def load_model_cls(self) -> Type[nn.Layer]:
try: try:
full_module = f"fastdeploy.model_executor.models.{self.module_name}" full_module = f"{self.module_path}.{self.module_name}"
module = importlib.import_module(full_module) module = importlib.import_module(full_module)
return getattr(module, self.class_name) return getattr(module, self.class_name)
except (ImportError, AttributeError) as e: except (ImportError, AttributeError) as e:
@@ -96,18 +97,6 @@ class LazyRegisteredModel(BaseRegisteredModel):
return ModelInfo.from_model_cls(model_cls, self.module_name) return ModelInfo.from_model_cls(model_cls, self.module_name)
@dataclass(frozen=True)
class RegisteredModel(BaseRegisteredModel):
model_cls: Type[nn.Layer]
def load_model_cls(self) -> Type[nn.Layer]:
return self.model_cls
def inspect_model_cls(self) -> ModelInfo:
return ModelInfo.from_model_cls(self.model_cls)
@lru_cache(maxsize=128) @lru_cache(maxsize=128)
def _try_inspect_model_cls( def _try_inspect_model_cls(
model_arch: str, model_arch: str,
@@ -133,7 +122,11 @@ class ModelRegistry:
def _register_enhanced_models(self): def _register_enhanced_models(self):
for arch, model_info in self._enhanced_models.items(): for arch, model_info in self._enhanced_models.items():
model = LazyRegisteredModel(module_name=model_info["module_path"], class_name=model_info["class_name"]) model = LazyRegisteredModel(
module_name=model_info["module_name"],
module_path=model_info["module_path"],
class_name=model_info["class_name"],
)
self.models[arch] = model self.models[arch] = model
self._registered_models[arch] = model self._registered_models[arch] = model
@@ -212,7 +205,8 @@ class ModelRegistry:
model_class=None, model_class=None,
*, *,
architecture: str = None, architecture: str = None,
module_path: str = None, module_name: str = None,
module_path: str = "fastdeploy.model_executor.models",
category: Union[ModelCategory, List[ModelCategory]] = ModelCategory.TEXT_GENERATION, category: Union[ModelCategory, List[ModelCategory]] = ModelCategory.TEXT_GENERATION,
primary_use: ModelCategory = None, primary_use: ModelCategory = None,
): ):
@@ -226,7 +220,8 @@ class ModelRegistry:
Args: Args:
model_class: The model class (when used as simple decorator) model_class: The model class (when used as simple decorator)
architecture (str): Unique identifier for the model architecture architecture (str): Unique identifier for the model architecture
module_path (str): Relative path to the module containing the model module_name (str): Relative path to the module containing the model
module_path (str): Absolute path to the module containing the model
category: Model category or list of categories category: Model category or list of categories
primary_use: Primary category for multi-category models primary_use: Primary category for multi-category models
""" """
@@ -237,13 +232,14 @@ class ModelRegistry:
cls._arch_to_model_cls[model_cls.name()] = model_cls cls._arch_to_model_cls[model_cls.name()] = model_cls
# Enhanced decorator-style registration # Enhanced decorator-style registration
if architecture and module_path: if architecture and module_name:
categories = category if isinstance(category, list) else [category] categories = category if isinstance(category, list) else [category]
# Register main entry # Register main entry
arch_key = architecture arch_key = architecture
cls._enhanced_models[arch_key] = { cls._enhanced_models[arch_key] = {
"class_name": model_cls.__name__, "class_name": model_cls.__name__,
"module_name": module_name,
"module_path": module_path, "module_path": module_path,
"category": primary_use or categories[0], "category": primary_use or categories[0],
"class": model_cls, "class": model_cls,
@@ -255,6 +251,7 @@ class ModelRegistry:
key = f"{arch_key}_{cat.value}" key = f"{arch_key}_{cat.value}"
cls._enhanced_models[key] = { cls._enhanced_models[key] = {
"class_name": model_cls.__name__, "class_name": model_cls.__name__,
"module_name": module_name,
"module_path": module_path, "module_path": module_path,
"category": cat, "category": cat,
"primary_use": primary_use or categories[0], "primary_use": primary_use or categories[0],

View File

@@ -288,7 +288,7 @@ class Qwen2Model(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Qwen2ForCausalLM", architecture="Qwen2ForCausalLM",
module_path="qwen2", module_name="qwen2",
category=[ModelCategory.TEXT_GENERATION, ModelCategory.EMBEDDING], category=[ModelCategory.TEXT_GENERATION, ModelCategory.EMBEDDING],
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -163,7 +163,7 @@ class Qwen2_5_VLModel(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Qwen2_5_VLForConditionalGeneration", architecture="Qwen2_5_VLForConditionalGeneration",
module_path="qwen2_5_vl.qwen2_5_vl", module_name="qwen2_5_vl.qwen2_5_vl",
category=ModelCategory.MULTIMODAL, category=ModelCategory.MULTIMODAL,
primary_use=ModelCategory.MULTIMODAL, primary_use=ModelCategory.MULTIMODAL,
) )

View File

@@ -225,7 +225,7 @@ class Qwen3Model(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Qwen3ForCausalLM", architecture="Qwen3ForCausalLM",
module_path="qwen3", module_name="qwen3",
category=[ModelCategory.TEXT_GENERATION], category=[ModelCategory.TEXT_GENERATION],
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )

View File

@@ -322,7 +322,7 @@ class Qwen3MoeModel(nn.Layer):
@ModelRegistry.register_model_class( @ModelRegistry.register_model_class(
architecture="Qwen3MoeForCausalLM", architecture="Qwen3MoeForCausalLM",
module_path="qwen3moe", module_name="qwen3moe",
category=ModelCategory.TEXT_GENERATION, category=ModelCategory.TEXT_GENERATION,
primary_use=ModelCategory.TEXT_GENERATION, primary_use=ModelCategory.TEXT_GENERATION,
) )