config.py 3.12 KB
Newer Older
1
import contextlib
2
from typing import Dict, Optional, Type
Jasmond L's avatar
Jasmond L committed
3

4
from transformers import PretrainedConfig
5

6
from vllm.envs import VLLM_USE_MODELSCOPE
7
from vllm.logger import init_logger
8
from vllm.transformers_utils.configs import (ChatGLMConfig, DbrxConfig,
9
10
11
12
13
14
15
                                             JAISConfig, MLPSpeculatorConfig,
                                             MPTConfig, RWConfig)

if VLLM_USE_MODELSCOPE:
    from modelscope import AutoConfig
else:
    from transformers import AutoConfig
16

17
18
logger = init_logger(__name__)

19
_CONFIG_REGISTRY: Dict[str, Type[PretrainedConfig]] = {
GoHomeToMacDonal's avatar
GoHomeToMacDonal committed
20
    "chatglm": ChatGLMConfig,
21
    "dbrx": DbrxConfig,
22
    "mpt": MPTConfig,
Zhuohan Li's avatar
Zhuohan Li committed
23
24
    "RefinedWeb": RWConfig,  # For tiiuae/falcon-40b(-instruct)
    "RefinedWebModel": RWConfig,  # For tiiuae/falcon-7b(-instruct)
25
    "jais": JAISConfig,
26
    "mlp_speculator": MLPSpeculatorConfig,
27
28
}

29
30
31
32
for name, cls in _CONFIG_REGISTRY.items():
    with contextlib.suppress(ValueError):
        AutoConfig.register(name, cls)

33

Jasmond L's avatar
Jasmond L committed
34
35
def get_config(model: str,
               trust_remote_code: bool,
36
               revision: Optional[str] = None,
37
               code_revision: Optional[str] = None,
38
39
               rope_scaling: Optional[dict] = None,
               rope_theta: Optional[float] = None) -> PretrainedConfig:
40
41
    try:
        config = AutoConfig.from_pretrained(
42
43
44
45
            model,
            trust_remote_code=trust_remote_code,
            revision=revision,
            code_revision=code_revision)
46
47
48
49
50
51
52
53
54
55
56
    except ValueError as e:
        if (not trust_remote_code and
                "requires you to execute the configuration file" in str(e)):
            err_msg = (
                "Failed to load the model config. If the model is a custom "
                "model not yet available in the HuggingFace transformers "
                "library, consider setting `trust_remote_code=True` in LLM "
                "or using the `--trust-remote-code` flag in the CLI.")
            raise RuntimeError(err_msg) from e
        else:
            raise e
57
58
    if config.model_type in _CONFIG_REGISTRY:
        config_class = _CONFIG_REGISTRY[config.model_type]
59
60
61
        config = config_class.from_pretrained(model,
                                              revision=revision,
                                              code_revision=code_revision)
62
63
64
65
66
67
    for key, value in [("rope_scaling", rope_scaling),
                       ("rope_theta", rope_theta)]:
        if value is not None:
            logger.info("Updating %s from %r to %r", key,
                        getattr(config, key, None), value)
            config.update({key: value})
68
    return config
69
70
71
72
73
74
75
76
77
78
79
80
81


def get_hf_text_config(config: PretrainedConfig):
    """Get the "sub" config relevant to llm for multi modal models.
        No op for pure text models.
    """
    if hasattr(config, "text_config"):
        # The code operates under the assumption that text_config should have
        # `num_attention_heads` (among others). Assert here to fail early
        # if transformers config doesn't align with this assumption.
        assert hasattr(config.text_config, "num_attention_heads")
        return config.text_config
    else:
82
        return config