__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import importlib
  2. from typing import Dict, List, Optional, Type
  3. import torch.nn as nn
  4. from loguru import logger
  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. "CohereForCausalLM": ("commandr", "CohereForCausalLM"),
  16. "DbrxForCausalLM": ("dbrx", "DbrxForCausalLM"),
  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. "JAISLMHeadModel": ("jais", "JAISLMHeadModel"),
  28. "LlamaForCausalLM": ("llama", "LlamaForCausalLM"),
  29. "LlavaForConditionalGeneration":
  30. ("llava", "LlavaForConditionalGeneration"),
  31. # For decapoda-research/llama-*
  32. "LLaMAForCausalLM": ("llama", "LlamaForCausalLM"),
  33. "MistralForCausalLM": ("llama", "LlamaForCausalLM"),
  34. "MixtralForCausalLM": ("mixtral", "MixtralForCausalLM"),
  35. "QuantMixtralForCausalLM": ("mixtral_quant", "MixtralForCausalLM"),
  36. # transformers's mpt class has lower case
  37. "MptForCausalLM": ("mpt", "MPTForCausalLM"),
  38. "MPTForCausalLM": ("mpt", "MPTForCausalLM"),
  39. "MiniCPMForCausalLM": ("minicpm", "MiniCPMForCausalLM"),
  40. "OlmoForCausalLM": ("olmo", "OlmoForCausalLM"),
  41. "OPTForCausalLM": ("opt", "OPTForCausalLM"),
  42. "OrionForCausalLM": ("orion", "OrionForCausalLM"),
  43. "PhiForCausalLM": ("phi", "PhiForCausalLM"),
  44. "Phi3ForCausalLM": ("llama", "LlamaForCausalLM"),
  45. "QWenLMHeadModel": ("qwen", "QWenLMHeadModel"),
  46. "Qwen2ForCausalLM": ("qwen2", "Qwen2ForCausalLM"),
  47. "Qwen2MoeForCausalLM": ("qwen2_moe", "Qwen2MoeForCausalLM"),
  48. "RWForCausalLM": ("falcon", "FalconForCausalLM"),
  49. "StableLMEpochForCausalLM": ("stablelm", "StablelmForCausalLM"),
  50. "StableLmForCausalLM": ("stablelm", "StablelmForCausalLM"),
  51. "Starcoder2ForCausalLM": ("starcoder2", "Starcoder2ForCausalLM"),
  52. "XverseForCausalLM": ("xverse", "XverseForCausalLM"),
  53. }
  54. # Architecture -> type.
  55. # out of tree models
  56. _OOT_MODELS: Dict[str, Type[nn.Module]] = {}
  57. # Models not supported by ROCm.
  58. _ROCM_UNSUPPORTED_MODELS = []
  59. # Models partially supported by ROCm.
  60. # Architecture -> Reason.
  61. _ROCM_PARTIALLY_SUPPORTED_MODELS = {
  62. "Qwen2ForCausalLM":
  63. "Sliding window attention is not yet supported in ROCm's flash attention",
  64. "MistralForCausalLM":
  65. "Sliding window attention is not yet supported in ROCm's flash attention",
  66. "MixtralForCausalLM":
  67. "Sliding window attention is not yet supported in ROCm's flash attention",
  68. }
  69. class ModelRegistry:
  70. @staticmethod
  71. def load_model_cls(model_arch: str) -> Optional[Type[nn.Module]]:
  72. if model_arch in _OOT_MODELS:
  73. return _OOT_MODELS[model_arch]
  74. if model_arch not in _MODELS:
  75. return None
  76. if is_hip():
  77. if model_arch in _ROCM_UNSUPPORTED_MODELS:
  78. raise ValueError(
  79. f"Model architecture {model_arch} is not supported by "
  80. "ROCm for now.")
  81. if model_arch in _ROCM_PARTIALLY_SUPPORTED_MODELS:
  82. logger.warning(
  83. "Model architecture %s is partially supported by ROCm: %s",
  84. model_arch, _ROCM_PARTIALLY_SUPPORTED_MODELS[model_arch])
  85. module_name, model_cls_name = _MODELS[model_arch]
  86. module = importlib.import_module(
  87. f"aphrodite.modeling.models.{module_name}")
  88. return getattr(module, model_cls_name, None)
  89. @staticmethod
  90. def get_supported_archs() -> List[str]:
  91. return list(_MODELS.keys())
  92. @staticmethod
  93. def register_model(model_arch: str, model_cls: Type[nn.Module]):
  94. if model_arch in _MODELS:
  95. logger.warning(
  96. "Model architecture %s is already registered, and will be "
  97. "overwritten by the new model class %s.", model_arch,
  98. model_cls.__name__)
  99. global _OOT_MODELS
  100. _OOT_MODELS[model_arch] = model_cls
  101. __all__ = [
  102. "ModelRegistry",
  103. ]