model_loader.py 2.29 KB
Newer Older
1
"""Utilities for selecting and loading models."""
2
3
from typing import Type

Woosuk Kwon's avatar
Woosuk Kwon committed
4
import torch
Woosuk Kwon's avatar
Woosuk Kwon committed
5
import torch.nn as nn
6
from transformers import PretrainedConfig
Woosuk Kwon's avatar
Woosuk Kwon committed
7

Woosuk Kwon's avatar
Woosuk Kwon committed
8
from vllm.config import ModelConfig
Woosuk Kwon's avatar
Woosuk Kwon committed
9
from vllm.model_executor.models import *  # pylint: disable=wildcard-import
Woosuk Kwon's avatar
Woosuk Kwon committed
10
from vllm.model_executor.weight_utils import initialize_dummy_weights
Woosuk Kwon's avatar
Woosuk Kwon committed
11

Woosuk Kwon's avatar
Woosuk Kwon committed
12
13
# TODO(woosuk): Lazy-load the model classes.
_MODEL_REGISTRY = {
shunxing1234's avatar
shunxing1234 committed
14
    "AquilaModel": AquilaForCausalLM,
15
16
    "BaiChuanForCausalLM": BaiChuanForCausalLM,  # baichuan-7b
    "BaichuanForCausalLM": BaichuanForCausalLM,  # baichuan-13b
Woosuk Kwon's avatar
Woosuk Kwon committed
17
    "BloomForCausalLM": BloomForCausalLM,
Zhuohan Li's avatar
Zhuohan Li committed
18
    "FalconForCausalLM": FalconForCausalLM,
Woosuk Kwon's avatar
Woosuk Kwon committed
19
    "GPT2LMHeadModel": GPT2LMHeadModel,
20
    "GPTBigCodeForCausalLM": GPTBigCodeForCausalLM,
21
    "GPTJForCausalLM": GPTJForCausalLM,
Woosuk Kwon's avatar
Woosuk Kwon committed
22
    "GPTNeoXForCausalLM": GPTNeoXForCausalLM,
Jia Guoqing's avatar
Jia Guoqing committed
23
    "InternLMForCausalLM": InternLMForCausalLM,
Woosuk Kwon's avatar
Woosuk Kwon committed
24
    "LlamaForCausalLM": LlamaForCausalLM,
25
26
    "LLaMAForCausalLM": LlamaForCausalLM,  # For decapoda-research/llama-*
    "MPTForCausalLM": MPTForCausalLM,
Woosuk Kwon's avatar
Woosuk Kwon committed
27
    "OPTForCausalLM": OPTForCausalLM,
Qing's avatar
Qing committed
28
    "QWenLMHeadModel": QWenLMHeadModel,
Zhuohan Li's avatar
Zhuohan Li committed
29
    "RWForCausalLM": FalconForCausalLM,
Woosuk Kwon's avatar
Woosuk Kwon committed
30
31
}

32

33
def _get_model_architecture(config: PretrainedConfig) -> Type[nn.Module]:
Woosuk Kwon's avatar
Woosuk Kwon committed
34
35
36
37
38
39
    architectures = getattr(config, "architectures", [])
    for arch in architectures:
        if arch in _MODEL_REGISTRY:
            return _MODEL_REGISTRY[arch]
    raise ValueError(
        f"Model architectures {architectures} are not supported for now. "
40
        f"Supported architectures: {list(_MODEL_REGISTRY.keys())}")
Woosuk Kwon's avatar
Woosuk Kwon committed
41
42


43
44
45
def get_model(model_config: ModelConfig) -> nn.Module:
    model_class = _get_model_architecture(model_config.hf_config)
    torch.set_default_dtype(model_config.dtype)
Woosuk Kwon's avatar
Woosuk Kwon committed
46
47
48

    # Create a model instance.
    # The weights will be initialized as empty tensors.
49
50
    model = model_class(model_config.hf_config)
    if model_config.use_dummy_weights:
Woosuk Kwon's avatar
Woosuk Kwon committed
51
52
53
54
55
56
        model = model.cuda()
        # NOTE(woosuk): For accurate performance evaluation, we assign
        # random values to the weights.
        initialize_dummy_weights(model)
    else:
        # Load the weights from the cached or downloaded files.
57
58
        model.load_weights(model_config.model, model_config.download_dir,
                           model_config.use_np_weights)
Woosuk Kwon's avatar
Woosuk Kwon committed
59
        model = model.cuda()
60
    return model.eval()