[Core] enable out-of-tree model register (#3871)

This commit is contained in:
youkaichao
2024-04-06 17:11:41 -07:00
committed by GitHub
parent e4be7d70bb
commit 95baec828f
6 changed files with 148 additions and 2 deletions
+17 -1
View File
@@ -1,5 +1,5 @@
import importlib
from typing import List, Optional, Type
from typing import Dict, List, Optional, Type
import torch.nn as nn
@@ -55,6 +55,10 @@ _MODELS = {
"XverseForCausalLM": ("xverse", "XverseForCausalLM"),
}
# Architecture -> type.
# out of tree models
_OOT_MODELS: Dict[str, Type[nn.Module]] = {}
# Models not supported by ROCm.
_ROCM_UNSUPPORTED_MODELS = []
@@ -74,6 +78,8 @@ class ModelRegistry:
@staticmethod
def load_model_cls(model_arch: str) -> Optional[Type[nn.Module]]:
if model_arch in _OOT_MODELS:
return _OOT_MODELS[model_arch]
if model_arch not in _MODELS:
return None
if is_hip():
@@ -95,6 +101,16 @@ class ModelRegistry:
def get_supported_archs() -> List[str]:
return list(_MODELS.keys())
@staticmethod
def register_model(model_arch: str, model_cls: Type[nn.Module]):
if model_arch in _MODELS:
logger.warning(
f"Model architecture {model_arch} is already registered, "
"and will be overwritten by the new model "
f"class {model_cls.__name__}.")
global _OOT_MODELS
_OOT_MODELS[model_arch] = model_cls
__all__ = [
"ModelRegistry",