utils.py 3.19 KB
Newer Older
1
2
3
4
"""Utilities for selecting and loading models."""
import contextlib
from typing import Tuple, Type

zhuwenwen's avatar
zhuwenwen committed
5
import os
6
7
8
9
10
import torch
from torch import nn

from vllm.config import ModelConfig
from vllm.model_executor.models import ModelRegistry
zhuwenwen's avatar
zhuwenwen committed
11

12
from vllm.model_executor.models.adapters import as_embedding_model
13
14
15
16
17
18
19
20
21
22
23
24
25
26


@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(
        model_config: ModelConfig) -> Tuple[Type[nn.Module], str]:
    architectures = getattr(model_config.hf_config, "architectures", [])
zhuwenwen's avatar
zhuwenwen committed
27
    visions = getattr(model_config.hf_config, "visual", []) or getattr(model_config.hf_config, "vision_config", [])
zhuwenwen's avatar
zhuwenwen committed
28
    support_nn_architectures = ['LlamaForCausalLM', 'QWenLMHeadModel', 'Qwen2ForCausalLM', 'Qwen2MoeForCausalLM', 'Qwen2VLForConditionalGeneration', 'ChatGLMModel', 'ChatGLMForConditionalGeneration', 'BaichuanForCausalLM', 'BloomForCausalLM', 'MedusaModel', 'MixtralForCausalLM', 'MLPSpeculatorPreTrainedModel', 'FalconForCausalLM']  
29
    if any(arch in architectures for arch in support_nn_architectures): 
zhuwenwen's avatar
zhuwenwen committed
30
        if os.getenv('LLAMA_NN') != '0': 
zhuwenwen's avatar
zhuwenwen committed
31
             if (architectures == ['QWenLMHeadModel'] or architectures == ['ChatGLMModel'] ) and visions != []:
zhuwenwen's avatar
zhuwenwen committed
32
33
34
                os.environ['LLAMA_NN'] = '0'
             else:
                os.environ['LLAMA_NN'] = '1'
zhuwenwen's avatar
zhuwenwen committed
35
        if (architectures == ['BloomForCausalLM'] or architectures == ['FalconForCausalLM']) or os.getenv('LM_NN') == '0':
zhuwenwen's avatar
zhuwenwen committed
36
            os.environ['LM_NN'] = '0'
zhuwenwen's avatar
zhuwenwen committed
37
        else:
zhuwenwen's avatar
zhuwenwen committed
38
            os.environ['LM_NN'] = '1'
39
40
        if os.getenv('GEMM_PAD') != '1': 
            os.environ['GEMM_PAD'] = '0'
zhuwenwen's avatar
zhuwenwen committed
41
42
        if os.getenv('FA_PAD') != '1': 
            os.environ['FA_PAD'] = '0'
zhuwenwen's avatar
zhuwenwen committed
43
44
45
46
47
48
49
50
51
52
        try:
            if os.getenv('AWQ_PAD') == '0' or ((torch.cuda.isCurrentDeviceEco(torch.cuda.current_device())) and os.getenv('AWQ_PAD') == None):
                os.environ['AWQ_PAD'] = '0'
            else:
                os.environ['AWQ_PAD'] = '1'
        except Exception as e:
            if os.getenv('AWQ_PAD') != '0': 
                os.environ['AWQ_PAD'] = '1'
            else:
                os.environ['AWQ_PAD'] = '0'
zhuwenwen's avatar
zhuwenwen committed
53
54
    else:
        os.environ['LLAMA_NN'] = '0'
zhuwenwen's avatar
zhuwenwen committed
55
        os.environ['LM_NN'] = '0'
56
57
        os.environ['GEMM_PAD'] = '0'
        os.environ['FA_PAD'] = '0'
zhuwenwen's avatar
zhuwenwen committed
58
        os.environ['AWQ_PAD'] = '0'
59
        
60
61
    # Special handling for quantized Mixtral.
    # FIXME(woosuk): This is a temporary hack.
62
63
64
    mixtral_supported = [
        "fp8", "compressed-tensors", "gptq_marlin", "awq_marlin"
    ]
65

66
    if (model_config.quantization is not None
67
            and model_config.quantization not in mixtral_supported
68
69
            and "MixtralForCausalLM" in architectures):
        architectures = ["QuantMixtralForCausalLM"]
70

71
    model_cls, arch = ModelRegistry.resolve_model_cls(architectures)
72
    if model_config.runner_type == "pooling":
73
74
75
        model_cls = as_embedding_model(model_cls)

    return model_cls, arch
76
77
78
79


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