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-03-11 23:23:14 -08:00
|
|
|
from cacheflow.models.memory_analyzer import CacheFlowMemoryAnalyzer
|
|
|
|
from cacheflow.models.memory_analyzer import OPTMemoryAnalyzer
|
2023-02-22 18:08:25 +00:00
|
|
|
from cacheflow.models.opt import OPTForCausalLM
|
2023-03-11 23:23:14 -08:00
|
|
|
from cacheflow.models.utils import get_torch_dtype
|
2023-02-13 09:36:12 +00:00
|
|
|
|
2023-03-11 23:23:14 -08:00
|
|
|
|
|
|
|
_MODELS = {
|
2023-02-13 09:36:12 +00:00
|
|
|
'opt': OPTForCausalLM,
|
|
|
|
}
|
|
|
|
|
2023-03-11 23:23:14 -08:00
|
|
|
_MEMORY_ANALYZERS = {
|
|
|
|
'opt': OPTMemoryAnalyzer,
|
2023-02-23 21:31:39 +00:00
|
|
|
}
|
|
|
|
|
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:
|
2023-03-11 23:23:14 -08:00
|
|
|
torch_dtype = get_torch_dtype(dtype)
|
|
|
|
for model_class, hf_model in _MODELS.items():
|
2023-02-13 22:51:03 +00:00
|
|
|
if model_class in model_name:
|
2023-03-11 23:23:14 -08:00
|
|
|
model = hf_model.from_pretrained(
|
|
|
|
model_name, torch_dtype=torch_dtype)
|
2023-02-24 16:29:36 -08:00
|
|
|
return model.eval()
|
2023-03-11 23:23:14 -08:00
|
|
|
raise ValueError(f'Unsupported model name: {model_name}')
|
2023-03-10 09:58:21 -08:00
|
|
|
|
|
|
|
|
2023-03-11 23:23:14 -08:00
|
|
|
def get_memory_analyzer(
|
|
|
|
model_name: str,
|
|
|
|
block_size: int,
|
|
|
|
dtype: Union[torch.dtype, str],
|
|
|
|
) -> CacheFlowMemoryAnalyzer:
|
|
|
|
torch_dtype = get_torch_dtype(dtype)
|
|
|
|
for model_class, memory_analyzer in _MEMORY_ANALYZERS.items():
|
|
|
|
if model_class in model_name:
|
|
|
|
return memory_analyzer(
|
|
|
|
model_name, block_size, torch_dtype)
|
|
|
|
raise ValueError(f'Unsupported model name: {model_name}')
|