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

3
from transformers import AutoConfig, PretrainedConfig
4

5
from vllm.logger import init_logger
6
7
from vllm.transformers_utils.configs import (ChatGLMConfig, DbrxConfig,
                                             JAISConfig, MPTConfig, RWConfig)
8

9
10
logger = init_logger(__name__)

11
_CONFIG_REGISTRY: Dict[str, PretrainedConfig] = {
GoHomeToMacDonal's avatar
GoHomeToMacDonal committed
12
    "chatglm": ChatGLMConfig,
13
    "dbrx": DbrxConfig,
14
    "mpt": MPTConfig,
Zhuohan Li's avatar
Zhuohan Li committed
15
16
    "RefinedWeb": RWConfig,  # For tiiuae/falcon-40b(-instruct)
    "RefinedWebModel": RWConfig,  # For tiiuae/falcon-7b(-instruct)
17
    "jais": JAISConfig,
18
19
20
}


Jasmond L's avatar
Jasmond L committed
21
22
def get_config(model: str,
               trust_remote_code: bool,
23
               revision: Optional[str] = None,
24
25
               code_revision: Optional[str] = None,
               rope_scaling: Optional[dict] = None) -> PretrainedConfig:
26
27
    try:
        config = AutoConfig.from_pretrained(
28
29
30
31
            model,
            trust_remote_code=trust_remote_code,
            revision=revision,
            code_revision=code_revision)
32
33
34
35
36
37
38
39
40
41
42
    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
43
44
    if config.model_type in _CONFIG_REGISTRY:
        config_class = _CONFIG_REGISTRY[config.model_type]
45
46
47
        config = config_class.from_pretrained(model,
                                              revision=revision,
                                              code_revision=code_revision)
48
49
50
51
    if rope_scaling is not None:
        logger.info("Updating rope_scaling from %r to %r",
                    getattr(config, "rope_scaling", None), rope_scaling)
        config.update({"rope_scaling": rope_scaling})
52
    return config
53
54
55
56
57
58
59
60
61
62
63
64
65
66


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:
        return config