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

import torch
from torch import nn

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

62
    if (model_config.quantization is not None
63
            and model_config.quantization not in mixtral_supported
64
65
            and "MixtralForCausalLM" in architectures):
        architectures = ["QuantMixtralForCausalLM"]
66

67
    return ModelRegistry.resolve_model_cls(architectures)
68
69
70
71


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