utils.py 1.73 KB
Newer Older
1
2
"""Utilities for selecting and loading models."""
import contextlib
3
from typing import Tuple, Type
4
5
6
7
8
9

import torch
from torch import nn

from vllm.config import ModelConfig
from vllm.model_executor.models import ModelRegistry
10
11
12
from vllm.model_executor.models.adapters import (as_classification_model,
                                                 as_embedding_model,
                                                 as_reward_model)
13
14
15
16
17
18
19
20
21
22
23
24


@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)


def get_model_architecture(
25
26
        model_config: ModelConfig) -> Tuple[Type[nn.Module], str]:
    architectures = getattr(model_config.hf_config, "architectures", [])
27

28
29
    # Special handling for quantized Mixtral.
    # FIXME(woosuk): This is a temporary hack.
30
31
32
    mixtral_supported = [
        "fp8", "compressed-tensors", "gptq_marlin", "awq_marlin"
    ]
33

34
    if (model_config.quantization is not None
35
            and model_config.quantization not in mixtral_supported
36
37
            and "MixtralForCausalLM" in architectures):
        architectures = ["QuantMixtralForCausalLM"]
38

39
    model_cls, arch = ModelRegistry.resolve_model_cls(architectures)
40
    if model_config.task == "embed":
41
        model_cls = as_embedding_model(model_cls)
42
43
44
45
    elif model_config.task == "classify":
        model_cls = as_classification_model(model_cls)
    elif model_config.task == "reward":
        model_cls = as_reward_model(model_cls)
46
47

    return model_cls, arch
48
49
50
51


def get_architecture_class_name(model_config: ModelConfig) -> str:
    return get_model_architecture(model_config)[1]