-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy path__init__.py
More file actions
405 lines (341 loc) · 11.5 KB
/
__init__.py
File metadata and controls
405 lines (341 loc) · 11.5 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
from typing import Optional
import torch
from loguru import logger
from transformers.configuration_utils import PretrainedConfig
from lorax_server.models.bloom import BLOOMSharded
from lorax_server.models.causal_lm import CausalLM
from lorax_server.models.flash_causal_lm import FlashCausalLM
from lorax_server.models.galactica import GalacticaSharded
from lorax_server.models.model import Model
from lorax_server.models.mpt import MPTSharded
from lorax_server.models.opt import OPTSharded
from lorax_server.models.santacoder import SantaCoder
from lorax_server.models.seq2seq_lm import Seq2SeqLM
from lorax_server.models.t5 import T5Sharded
from lorax_server.utils.sources import get_s3_model_local_dir
from lorax_server.utils.torch_utils import is_bf16_supported
# The flag below controls whether to allow TF32 on matmul. This flag defaults to False
# in PyTorch 1.12 and later.
torch.backends.cuda.matmul.allow_tf32 = True
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = True
# Disable gradients
torch.set_grad_enabled(False)
__all__ = [
"Model",
"BLOOMSharded",
"CausalLM",
"FlashCausalLM",
"GalacticaSharded",
"Seq2SeqLM",
"SantaCoder",
"OPTSharded",
"T5Sharded",
"get_model",
]
def get_model(
model_id: str,
adapter_id: str,
revision: Optional[str],
sharded: bool,
quantize: Optional[str],
compile: bool,
dtype: Optional[str],
trust_remote_code: bool,
source: str,
adapter_source: str,
merge_adapter_weights: bool,
embedding_dim: Optional[int] = None,
) -> Model:
config_dict = None
if source == "s3":
# change the model id to be the local path to the folder so
# we can load the config_dict locally
logger.info("Using the local files since we are coming from s3")
model_path = get_s3_model_local_dir(model_id)
logger.info(f"model_path: {model_path}")
config_dict, _ = PretrainedConfig.get_config_dict(
model_path, revision=revision, trust_remote_code=trust_remote_code
)
logger.info(f"config_dict: {config_dict}")
model_id = str(model_path)
elif source == "hub":
config_dict, _ = PretrainedConfig.get_config_dict(
model_id, revision=revision, trust_remote_code=trust_remote_code
)
else:
raise ValueError(f"Unknown source {source}")
model_type = config_dict["model_type"]
is_dtype_provided = dtype is not None
dtype = dtype or config_dict.get("torch_dtype", "float16")
if dtype in {"float16", "float32"}:
dtype = torch.float16
elif dtype == "bfloat16":
if not is_bf16_supported():
if is_dtype_provided:
raise RuntimeError("bfloat16 is not supported on this device, set --dtype float16.")
logger.warning("bfloat16 is not supported on this device, falling back to float16")
dtype = torch.float16
else:
dtype = torch.bfloat16
else:
try:
dtype = getattr(torch, dtype)
except AttributeError:
raise RuntimeError(f"Unknown dtype {dtype}")
if "facebook/galactica" in model_id:
return GalacticaSharded(
model_id,
revision,
quantize=quantize,
compile=compile,
dtype=dtype,
dtypetrust_remote_code=trust_remote_code,
)
if model_type == "bert":
from lorax_server.models.flash_bert import FlashBert
if config_dict["architectures"][0] == "BertForTokenClassification":
return FlashBert(model_id, revision=revision, dtype=dtype, classifcation_head=True)
return FlashBert(model_id, revision=revision, dtype=dtype)
if model_type == "distilbert":
from lorax_server.models.flash_distilbert import FlashDistilBert
if config_dict["architectures"][0] == "DistilBertForMaskedLM":
return FlashDistilBert(model_id, revision=revision, dtype=dtype)
if config_dict["architectures"][0] == "DistilBertForTokenClassification":
return FlashDistilBert(model_id, revision=revision, dtype=dtype, classifcation_head=True)
if model_type == "xlm-roberta":
from lorax_server.models.flash_roberta import FlashXlmRoberta
return FlashXlmRoberta(
model_id,
adapter_id,
adapter_source,
revision=revision,
dtype=dtype,
merge_adapter_weights=merge_adapter_weights,
)
flash_causal_lm_kwargs = dict(
quantize=quantize,
compile=compile,
dtype=dtype,
trust_remote_code=trust_remote_code,
merge_adapter_weights=merge_adapter_weights,
)
if model_id.startswith("bigcode/") or model_type == "gpt_bigcode":
from lorax_server.models.flash_santacoder import FlashSantacoderSharded
return FlashSantacoderSharded(
model_id,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "bloom":
return BLOOMSharded(
model_id,
revision,
quantize=quantize,
compile=compile,
dtype=dtype,
trust_remote_code=trust_remote_code,
)
if model_type == "mpt":
return MPTSharded(
model_id,
revision,
quantize=quantize,
compile=compile,
trust_remote_code=trust_remote_code,
)
if model_type == "gpt_neox":
from lorax_server.models.flash_neox import FlashNeoXSharded
return FlashNeoXSharded(
model_id,
revision,
quantize=quantize,
compile=compile,
dtype=dtype,
trust_remote_code=trust_remote_code,
)
if model_type == "llama":
from lorax_server.models.flash_llama import FlashLlama
return FlashLlama(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "gpt2":
from lorax_server.models.flash_gpt2 import FlashGPT2
return FlashGPT2(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type in ["RefinedWeb", "RefinedWebModel", "falcon"]:
from lorax_server.models.flash_rw import FlashRWSharded
return FlashRWSharded(
model_id,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "mistral":
from lorax_server.models.flash_mistral import FlashMistral
return FlashMistral(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "mixtral":
from lorax_server.models.flash_mixtral import FlashMixtral
return FlashMixtral(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "qwen":
from lorax_server.models.flash_qwen import FlashQwen
return FlashQwen(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "qwen2":
from lorax_server.models.flash_qwen2 import FlashQwen2
return FlashQwen2(
model_id,
adapter_id,
adapter_source,
revision,
embedding_dim=embedding_dim,
**flash_causal_lm_kwargs,
)
if model_type in ["phi-msft", "phi"]:
from lorax_server.models.flash_phi import FlashPhi
return FlashPhi(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "phi3":
from lorax_server.models.flash_phi3 import FlashPhi3
return FlashPhi3(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "solar":
from lorax_server.models.flash_solar import FlashSolar
return FlashSolar(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "gemma":
from lorax_server.models.flash_gemma import FlashGemma
return FlashGemma(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "gemma2":
from lorax_server.models.flash_gemma2 import FlashGemma2
return FlashGemma2(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "cohere":
from lorax_server.models.flash_cohere import FlashCohere
return FlashCohere(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "dbrx":
from lorax_server.models.flash_dbrx import FlashDbrx
return FlashDbrx(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "llava_next" or model_type == "llava":
from lorax_server.models.custom_modeling.llava_next import LlavaNextForConditionalGeneration
from lorax_server.models.vlm_causal_lm import VlmCausalLM
return VlmCausalLM(
model_class=LlavaNextForConditionalGeneration,
model_id=model_id,
adapter_id=adapter_id,
adapter_source=adapter_source,
revision=revision,
**flash_causal_lm_kwargs,
)
if model_type == "mllama":
from lorax_server.models.custom_modeling.mllama import MllamaForConditionalGeneration
from lorax_server.models.mllama import MllamaCausalLM, MllamaCausalLMBatch
return MllamaCausalLM(
model_id=model_id,
model_class=MllamaForConditionalGeneration,
batch_class=MllamaCausalLMBatch,
adapter_id=adapter_id,
adapter_source=adapter_source,
revision=revision,
**flash_causal_lm_kwargs,
)
if model_type == "opt":
return OPTSharded(
model_id,
revision,
quantize=quantize,
compile=compile,
dtype=dtype,
trust_remote_code=trust_remote_code,
)
if model_type == "t5":
return T5Sharded(
model_id,
revision,
quantize=quantize,
compile=compile,
dtype=dtype,
trust_remote_code=trust_remote_code,
)
if model_type == "granite":
from lorax_server.models.flash_granite import FlashGranite
return FlashGranite(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
if model_type == "exaone":
from lorax_server.models.flash_exaone import FlashExaOne
return FlashExaOne(
model_id,
adapter_id,
adapter_source,
revision,
**flash_causal_lm_kwargs,
)
raise ValueError(f"Unsupported model type {model_type}")