2023-02-23 21:31:39 +00:00
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
import torch
|
2023-02-13 09:36:12 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
|
2023-02-22 18:08:25 +00:00
|
|
|
from cacheflow.models.opt import OPTForCausalLM
|
2023-02-13 09:36:12 +00:00
|
|
|
|
|
|
|
MODEL_CLASSES = {
|
|
|
|
'opt': OPTForCausalLM,
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:31:39 +00:00
|
|
|
STR_DTYPE_TO_TORCH_DTYPE = {
|
|
|
|
'half': torch.half,
|
|
|
|
'float': torch.float,
|
|
|
|
'float16': torch.float16,
|
|
|
|
'float32': torch.float32,
|
|
|
|
}
|
|
|
|
|
2023-02-13 09:36:12 +00:00
|
|
|
|
2023-02-23 21:31:39 +00:00
|
|
|
def get_model(
|
|
|
|
model_name: str,
|
|
|
|
dtype: Union[torch.dtype, str],
|
|
|
|
) -> nn.Module:
|
|
|
|
if isinstance(dtype, str):
|
|
|
|
torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype.lower()]
|
|
|
|
else:
|
|
|
|
torch_dtype = dtype
|
2023-02-13 22:51:03 +00:00
|
|
|
for model_class, model in MODEL_CLASSES.items():
|
|
|
|
if model_class in model_name:
|
2023-02-23 21:31:39 +00:00
|
|
|
return model.from_pretrained(model_name, torch_dtype=torch_dtype)
|
2023-02-13 22:51:03 +00:00
|
|
|
raise ValueError(f'Invalid model name: {model_name}')
|