__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import importlib
  2. from typing import Dict, List, Optional, Type
  3. from loguru import logger
  4. import torch.nn as nn
  5. from aphrodite.common.utils import is_hip
  6. # Architecture -> (module, class).
  7. _MODELS = {
  8. "AquilaModel": ("llama", "LlamaForCausalLM"),
  9. "AquilaForCausalLM": ("llama", "LlamaForCausalLM"), # AquilaChat2
  10. "BaiChuanForCausalLM": ("baichuan", "BaiChuanForCausalLM"), # baichuan-7b
  11. "BaichuanForCausalLM": ("baichuan", "BaichuanForCausalLM"), # baichuan-13b
  12. "BloomForCausalLM": ("bloom", "BloomForCausalLM"),
  13. "ChatGLMModel": ("chatglm", "ChatGLMForCausalLM"),
  14. "ChatGLMForConditionalGeneration": ("chatglm", "ChatGLMForCausalLM"),
  15. "DbrxForCausalLM": ("dbrx", "DbrxForCausalLM"),
  16. "CohereForCausalLM": ("cohere", "CohereForCausalLM"),
  17. "DeciLMForCausalLM": ("decilm", "DeciLMForCausalLM"),
  18. "DeepseekForCausalLM": ("deepseek", "DeepseekForCausalLM"),
  19. "FalconForCausalLM": ("falcon", "FalconForCausalLM"),
  20. "GemmaForCausalLM": ("gemma", "GemmaForCausalLM"),
  21. "GPT2LMHeadModel": ("gpt2", "GPT2LMHeadModel"),
  22. "GPTBigCodeForCausalLM": ("gpt_bigcode", "GPTBigCodeForCausalLM"),
  23. "GPTJForCausalLM": ("gpt_j", "GPTJForCausalLM"),
  24. "GPTNeoXForCausalLM": ("gpt_neox", "GPTNeoXForCausalLM"),
  25. "InternLMForCausalLM": ("llama", "LlamaForCausalLM"),
  26. "InternLM2ForCausalLM": ("internlm2", "InternLM2ForCausalLM"),
  27. "LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
  28. # For decapoda-research/llama-*
  29. "LLaMAForCausalLM": ("llama", "LlamaForCausalLM"),
  30. "LlavaForConditionalGeneration":
  31. ("llava", "LlavaForConditionalGeneration"),
  32. "MistralForCausalLM": ("llama", "LlamaForCausalLM"),
  33. "MixtralForCausalLM": ("mixtral", "MixtralForCausalLM"),
  34. "YiForCausalLM": ("llama", "LlamaForCausalLM"),
  35. # transformers's mpt class has lower case
  36. "MptForCausalLM": ("mpt", "MPTForCausalLM"),
  37. "MPTForCausalLM": ("mpt", "MPTForCausalLM"),
  38. "OLMoForCausalLM": ("olmo", "OLMoForCausalLM"),
  39. "OPTForCausalLM": ("opt", "OPTForCausalLM"),
  40. "PhiForCausalLM": ("phi", "PhiForCausalLM"),
  41. "QWenLMHeadModel": ("qwen", "QWenLMHeadModel"),
  42. "Qwen2ForCausalLM": ("qwen2", "Qwen2ForCausalLM"),
  43. "Qwen2MoeForCausalLM": ("qwen2_moe", "Qwen2MoeForCausalLM"),
  44. "RWForCausalLM": ("falcon", "FalconForCausalLM"),
  45. "StableLMEpochForCausalLM": ("stablelm", "StablelmForCausalLM"),
  46. "StableLmForCausalLM": ("stablelm", "StablelmForCausalLM"),
  47. }
  48. # Architecture -> type.
  49. # out of tree models
  50. _OOT_MODELS: Dict[str, Type[nn.Module]] = {}
  51. # Models not supported by ROCm.
  52. _ROCM_UNSUPPORTED_MODELS = []
  53. # Models partially supported by ROCm.
  54. # Architecture -> Reason.
  55. _ROCM_PARTIALLY_SUPPORTED_MODELS = {
  56. "Qwen2ForCausalLM":
  57. "Sliding window attention is not yet supported in ROCm's flash attention",
  58. "MistralForCausalLM":
  59. "Sliding window attention is not yet supported in ROCm's flash attention",
  60. "MixtralForCausalLM":
  61. "Sliding window attention is not yet supported in ROCm's flash attention",
  62. }
  63. class ModelRegistry:
  64. @staticmethod
  65. def load_model_cls(model_arch: str) -> Optional[Type[nn.Module]]:
  66. if model_arch in _OOT_MODELS:
  67. return _OOT_MODELS[model_arch]
  68. if model_arch not in _MODELS:
  69. return None
  70. if is_hip():
  71. if model_arch in _ROCM_UNSUPPORTED_MODELS:
  72. raise ValueError(
  73. f"Model architecture {model_arch} is not supported by "
  74. "ROCm for now.")
  75. if model_arch in _ROCM_PARTIALLY_SUPPORTED_MODELS:
  76. logger.warning(
  77. f"Model architecture {model_arch} is partially supported "
  78. "by ROCm: " + _ROCM_PARTIALLY_SUPPORTED_MODELS[model_arch])
  79. module_name, model_cls_name = _MODELS[model_arch]
  80. module = importlib.import_module(
  81. f"aphrodite.modeling.models.{module_name}")
  82. return getattr(module, model_cls_name, None)
  83. @staticmethod
  84. def get_supported_archs() -> List[str]:
  85. return list(_MODELS.keys())
  86. @staticmethod
  87. def register_model(model_arch: str, model_cls: Type[nn.Module]):
  88. if model_arch in _MODELS:
  89. logger.warning(
  90. f"Model architecture {model_arch} is already registered, "
  91. "and will be overwritten by the new model "
  92. f"class {model_cls.__name__}.")
  93. global _OOT_MODELS
  94. _OOT_MODELS[model_arch] = model_cls
  95. __all__ = [
  96. "ModelRegistry",
  97. ]