utils.py 1.95 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", [])
25
    support_nn_architectures = ['LlamaForCausalLM', 'QWenLMHeadModel', 'Qwen2ForCausalLM', 'ChatGLMModel', 'BaichuanForCausalLM', 'BloomForCausalLM']  
26
    use_triton_fa_architectures = ['DeepseekV2ForCausalLM']  
27
    if any(arch in architectures for arch in support_nn_architectures): 
zhuwenwen's avatar
zhuwenwen committed
28
29
        if os.getenv('LLAMA_NN') != '0': 
            os.environ['LLAMA_NN'] = '1'
30
31
32
33
        if os.getenv('GEMM_PAD') != '1': 
            os.environ['GEMM_PAD'] = '0'
        if os.getenv('FA_PAD') != '1': 
            os.environ['FA_PAD'] = '0'
zhuwenwen's avatar
zhuwenwen committed
34
35
    else:
        os.environ['LLAMA_NN'] = '0'
36
37
        os.environ['GEMM_PAD'] = '0'
        os.environ['FA_PAD'] = '0'
38
39
40
41
    
    if any(arch in architectures for arch in use_triton_fa_architectures): 
        os.environ['VLLM_USE_TRITON_FLASH_ATTN'] = '1'
        os.environ['VLLM_USE_FLASH_ATTN_AUTO'] = '0'
42
        
43
44
45
    # Special handling for quantized Mixtral.
    # FIXME(woosuk): This is a temporary hack.
    if (model_config.quantization is not None
46
            and model_config.quantization != "fp8"
47
48
49
            and "MixtralForCausalLM" in architectures):
        architectures = ["QuantMixtralForCausalLM"]

50
    return ModelRegistry.resolve_model_cls(architectures)
51
52
53
54


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