utils.py 1.41 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
from vllm.model_executor.models.adapters import as_embedding_model
11
12
13
14
15
16
17
18
19
20
21
22


@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(
23
24
        model_config: ModelConfig) -> Tuple[Type[nn.Module], str]:
    architectures = getattr(model_config.hf_config, "architectures", [])
25

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

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

37
38
39
40
41
    model_cls, arch = ModelRegistry.resolve_model_cls(architectures)
    if model_config.task == "embedding":
        model_cls = as_embedding_model(model_cls)

    return model_cls, arch
42
43
44
45


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