-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathauto_model.py
More file actions
184 lines (179 loc) · 9.4 KB
/
auto_model.py
File metadata and controls
184 lines (179 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from .llama import Llama, LlamaAwq, LlamaOffload, LlamaAwqOffload, LlamaCudagraph
from .qwen import Qwen, QwenOffload, QwenAwq, QwenAwqOffload, QwenCudagraph
from .gemma import Gemma2
from .mistral import Mistral, MistralAwqOffload, MistralOffload, MistralCudagraph, MistralAwq
from .glm4 import Glm4
class AutoModelLM:
"""
自动模型加载器,根据模型类型动态加载对应的类。
"""
_OFFLOAD_MODEL_MAPPING = {
"ibnzterrell/Meta-Llama-3.3-70B-Instruct-AWQ-INT4": LlamaAwqOffload,
"lambdalabs/Llama-3.3-70B-Instruct-AWQ-4bit": LlamaAwqOffload,
"casperhansen/llama-3.3-70b-instruct-awq": LlamaAwqOffload,
"hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4": LlamaAwqOffload,
"hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4": LlamaAwqOffload,
"casperhansen/deepseek-r1-distill-llama-70b-awq": LlamaAwqOffload,
"meta-llama/Llama-3.3-70B-Instruct": LlamaOffload,
"meta-llama/Llama-3.1-70B-Instruct": LlamaOffload,
"meta-llama/Llama-3.1-8B-Instruct": LlamaOffload,
"meta-llama/Meta-Llama-3-70B-Instruct": LlamaOffload,
"meta-llama/Meta-Llama-3-8B-Instruct": LlamaOffload,
"Qwen/Qwen2.5-Coder-72B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-32B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-14B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-7B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-3B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-1.5B-Instruct": QwenOffload,
"Qwen/Qwen2.5-Coder-0.5B-Instruct": QwenOffload,
"Qwen/Qwen2.5-0.5B-Instruct": QwenOffload,
"Qwen/Qwen2.5-1.5B-Instruct": QwenOffload,
"Qwen/Qwen2.5-3B-Instruct": QwenOffload,
"Qwen/Qwen2.5-7B-Instruct": QwenOffload,
"Qwen/Qwen2.5-14B-Instruct": QwenOffload,
"Qwen/Qwen2.5-32B-Instruct": QwenOffload,
"Qwen/Qwen2.5-72B-Instruct": QwenOffload,
"Qwen/QwQ-32B-Preview": QwenOffload,
"Qwen/Qwen2.5-Coder-72B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-32B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-14B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-3B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-1.5B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-Coder-0.5B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-0.5B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-1.5B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-3B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-7B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-14B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-32B-Instruct-AWQ": QwenAwqOffload,
"Qwen/Qwen2.5-72B-Instruct-AWQ": QwenAwqOffload,
"KirillR/QwQ-32B-Preview-AWQ": QwenAwqOffload,
"casperhansen/deepseek-r1-distill-qwen-32b-awq":QwenAwqOffload,
"mistralai/Mistral-7B-Instruct-v0.3": MistralOffload,
"solidrust/Mistral-7B-Instruct-v0.3-AWQ": MistralAwqOffload,
"mistralai/Mistral-Small-24B-Instruct-2501": MistralOffload,
"stelterlab/Mistral-Small-24B-Instruct-2501-AWQ": MistralAwqOffload
}
_MODEL_MAPPING = {
"ibnzterrell/Meta-Llama-3.3-70B-Instruct-AWQ-INT4": LlamaAwq,
"lambdalabs/Llama-3.3-70B-Instruct-AWQ-4bit": LlamaAwq,
"casperhansen/llama-3.3-70b-instruct-awq": LlamaAwq,
"hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4": LlamaAwq,
"hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4": LlamaAwq,
"casperhansen/deepseek-r1-distill-llama-70b-awq": LlamaAwq,
"meta-llama/Llama-3.3-70B-Instruct": Llama,
"meta-llama/Llama-3.1-70B-Instruct": Llama,
"meta-llama/Llama-3.1-8B-Instruct": Llama,
"meta-llama/Meta-Llama-3-70B-Instruct": Llama,
"meta-llama/Meta-Llama-3-8B-Instruct": Llama,
"meta-llama/Llama-3.2-1B-Instruct": Llama,
"meta-llama/Llama-3.2-3B-Instruct": Llama,
"Felladrin/Llama-68M-Chat-v1": Llama,
"facebook/layerskip-llama3.2-1B": Llama,
"Zhuominc/Llama-3-330M": Llama,
"Zhuominc/Coder-670M": Llama,
"Zhuominc/Coder-400M": Llama,
"Zhuominc/Coder-400M-IT": Llama,
"Zhuominc/FastCode-500M": Llama,
"InfiniAILab/CodeDrafter-500M": Llama,
"Qwen/Qwen2.5-Coder-72B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-32B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-14B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-7B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-3B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-1.5B-Instruct": Qwen,
"Qwen/Qwen2.5-Coder-0.5B-Instruct": Qwen,
"Qwen/Qwen2.5-0.5B-Instruct": Qwen,
"Qwen/Qwen2.5-1.5B-Instruct": Qwen,
"Qwen/Qwen2.5-3B-Instruct": Qwen,
"Qwen/Qwen2.5-7B-Instruct": Qwen,
"Qwen/Qwen2.5-14B-Instruct": Qwen,
"Qwen/Qwen2.5-32B-Instruct": Qwen,
"Qwen/Qwen2.5-72B-Instruct": Qwen,
"Qwen/QwQ-32B-Preview": Qwen,
"Qwen/Qwen2.5-Coder-72B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-32B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-14B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-3B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-1.5B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-Coder-0.5B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-0.5B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-1.5B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-3B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-7B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-14B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-32B-Instruct-AWQ": QwenAwq,
"Qwen/Qwen2.5-72B-Instruct-AWQ": QwenAwq,
"KirillR/QwQ-32B-Preview-AWQ": QwenAwq,
"casperhansen/deepseek-r1-distill-qwen-32b-awq":QwenAwq,
"google/gemma-2-2b-it": Gemma2,
"google/gemma-2-9b-it": Gemma2,
"google/gemma-2-27b-it": Gemma2,
"google/gemma-2-2b": Gemma2,
"mistralai/Mistral-7B-Instruct-v0.3": Mistral,
"solidrust/Mistral-7B-Instruct-v0.3-AWQ": MistralAwq,
"mistralai/Mistral-Small-24B-Instruct-2501": Mistral,
"stelterlab/Mistral-Small-24B-Instruct-2501-AWQ": MistralAwq,
"PyrTools/Ministral-8B-Instruct-2410-AWQ": MistralAwq,
"mistralai/Ministral-8B-Instruct-2410": Mistral,
"THUDM/glm-4-9b-chat-hf": Glm4,
}
_CUDAGRAPH_MODEL_MAPPING = {
"meta-llama/Llama-3.1-8B-Instruct": LlamaCudagraph,
"meta-llama/Meta-Llama-3-8B-Instruct": LlamaCudagraph,
"meta-llama/Llama-3.2-1B-Instruct": LlamaCudagraph,
"meta-llama/Llama-3.2-3B-Instruct": LlamaCudagraph,
"Felladrin/Llama-68M-Chat-v1": LlamaCudagraph,
"facebook/layerskip-llama3.2-1B": LlamaCudagraph,
"Zhuominc/Llama-3-330M": LlamaCudagraph,
"Zhuominc/Coder-670M": LlamaCudagraph,
"Zhuominc/Coder-400M": LlamaCudagraph,
"Zhuominc/Coder-400M-IT": LlamaCudagraph,
"Zhuominc/FastCode-500M": LlamaCudagraph,
"InfiniAILab/CodeDrafter-500M": LlamaCudagraph,
"Qwen/Qwen2.5-Coder-72B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-32B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-14B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-7B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-3B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-1.5B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-Coder-0.5B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-0.5B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-1.5B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-3B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-7B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-14B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-32B-Instruct": QwenCudagraph,
"Qwen/Qwen2.5-72B-Instruct": QwenCudagraph,
"Qwen/QwQ-32B-Preview": QwenCudagraph,
"mistralai/Mistral-7B-Instruct-v0.3": MistralCudagraph,
"mistralai/Ministral-8B-Instruct-2410": MistralCudagraph
}
@classmethod
def from_pretrained(cls, model_name, offload=False, cuda_graph=False, **kwargs):
"""
根据模型类型加载预训练模型。
:param model_name: 模型类型,例如 'llama' 或 'gpt'
:param kwargs: 额外参数
:return: 对应的模型实例
"""
if cuda_graph:
if model_name not in cls._CUDAGRAPH_MODEL_MAPPING:
raise ValueError(f"Model type '{model_name}' is not supported. "
f"Supported types: {list(cls._CUDAGRAPH_MODEL_MAPPING.keys())}")
model_class = cls._CUDAGRAPH_MODEL_MAPPING[model_name]
return model_class(model_name = model_name, **kwargs)
if not offload:
if model_name not in cls._MODEL_MAPPING:
raise ValueError(f"Model type '{model_name}' is not supported. "
f"Supported types: {list(cls._MODEL_MAPPING.keys())}")
model_class = cls._MODEL_MAPPING[model_name]
return model_class(model_name = model_name, **kwargs)
else:
if model_name not in cls._OFFLOAD_MODEL_MAPPING:
raise ValueError(f"Model type '{model_name}' is not supported (offload). "
f"Supported (offload) types: {list(cls._OFFLOAD_MODEL_MAPPING.keys())}")
model_class = cls._OFFLOAD_MODEL_MAPPING[model_name]
return model_class(model_name = model_name, **kwargs)