"include/infiniop/ops/zeros.h" did not exist on "f5e6d7294dc54e196396c80e18fb251eb9cd703a"
model_loader.py 3.13 KB
Newer Older
1
"""Utilities for selecting and loading models."""
2
import contextlib
3
4
from typing import Type

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

Woosuk Kwon's avatar
Woosuk Kwon committed
9
from vllm.config import ModelConfig
10
from vllm.model_executor.models import ModelRegistry
11
12
from vllm.model_executor.weight_utils import (get_quant_config,
                                              initialize_dummy_weights)
13

14

15
16
17
18
19
20
21
22
23
@contextlib.contextmanager
def _set_default_torch_dtype(dtype: torch.dtype):
    """Sets the default torch dtype to the given dtype."""
    old_dtype = torch.get_default_dtype()
    torch.set_default_dtype(dtype)
    yield
    torch.set_default_dtype(old_dtype)


24
def _get_model_architecture(config: PretrainedConfig) -> Type[nn.Module]:
Woosuk Kwon's avatar
Woosuk Kwon committed
25
26
    architectures = getattr(config, "architectures", [])
    for arch in architectures:
27
28
29
        model_cls = ModelRegistry.load_model_cls(arch)
        if model_cls is not None:
            return model_cls
Woosuk Kwon's avatar
Woosuk Kwon committed
30
31
    raise ValueError(
        f"Model architectures {architectures} are not supported for now. "
32
        f"Supported architectures: {ModelRegistry.get_supported_archs()}")
Woosuk Kwon's avatar
Woosuk Kwon committed
33
34


35
36
def get_model(model_config: ModelConfig) -> nn.Module:
    model_class = _get_model_architecture(model_config.hf_config)
37

38
39
    # Get the (maybe quantized) linear method.
    linear_method = None
40
41
42
    if model_config.quantization is not None:
        quant_config = get_quant_config(model_config.quantization,
                                        model_config.model,
43
                                        model_config.hf_config,
44
                                        model_config.download_dir)
45
46
47
48
49
50
51
52
        capability = torch.cuda.get_device_capability()
        capability = capability[0] * 10 + capability[1]
        if capability < quant_config.get_min_capability():
            raise ValueError(
                f"The quantization method {model_config.quantization} is not "
                "supported for the current GPU. "
                f"Minimum capability: {quant_config.get_min_capability()}. "
                f"Current capability: {capability}.")
53
54
55
56
57
58
        supported_dtypes = quant_config.get_supported_act_dtypes()
        if model_config.dtype not in supported_dtypes:
            raise ValueError(
                f"{model_config.dtype} is not supported for quantization "
                f"method {model_config.quantization}. Supported dtypes: "
                f"{supported_dtypes}")
59
        linear_method = quant_config.get_linear_method()
60

61
62
63
    with _set_default_torch_dtype(model_config.dtype):
        # Create a model instance.
        # The weights will be initialized as empty tensors.
64
65
        with torch.device("cuda"):
            model = model_class(model_config.hf_config, linear_method)
66
        if model_config.load_format == "dummy":
67
68
69
70
71
72
            # 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.
            model.load_weights(model_config.model, model_config.download_dir,
Jasmond L's avatar
Jasmond L committed
73
                               model_config.load_format, model_config.revision)
74
    return model.eval()