eagle.py 3.09 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7
8
import os
from typing import Optional, Union

from transformers import AutoConfig, PretrainedConfig

9
10
from vllm.transformers_utils.configs.deepseek_vl2 import DeepseekV2Config

11
12
13
14
15
16
17

class EAGLEConfig(PretrainedConfig):
    model_type = "eagle"

    def __init__(self,
                 model: Union[PretrainedConfig, dict, None] = None,
                 truncated_vocab_size: Optional[int] = None,
18
                 method: Optional[str] = 'eagle',
19
20
                 **kwargs):

21
22
23
24
25
26
27
28
29
30
31
        model_config: Union[PretrainedConfig, DeepseekV2Config, None]
        if isinstance(model, dict):
            archs = model.get("architectures", [])
            target_archs = ["DeepseekV2ForCausalLM", "DeepseekV3ForCausalLM"]
            if any(target_arch in archs for target_arch in target_archs):
                # AutoConfig does not support DeepSeek MoE models yet
                model_config = DeepseekV2Config(**model)
            else:
                model_config = AutoConfig.for_model(**model)
        else:
            model_config = model
32
33
34
35
36
37
38
39
40
41
42
43
44
45

        for k, v in kwargs.items():
            if k != "architectures" and k != "model_type" and hasattr(
                    model_config, k):
                setattr(model_config, k, v)

        self.model = model_config

        if self.model is None:
            self.truncated_vocab_size = None
        else:
            self.truncated_vocab_size = self.model.vocab_size if \
                truncated_vocab_size is None else truncated_vocab_size

46
47
        # Eagle model name should follow naming convention of
        # LlamaForCausalLM -> EagleLlamaForCausalLM
48
        # LlamaForCausalLM -> Eagle3LlamaForCausalLM
49
        # LlamaForCausalLMEagle3 -> LlamaForCausalLMEagle3
50
51
52
53
54
55
56
        if method == "eagle":
            assert self.model is not None, \
                "model should not be None when method is eagle"
            kwargs["architectures"] = [
                f"Eagle{arch}" if not arch.startswith("Eagle") \
                    else arch for arch in self.model.architectures
            ]
57

58
59
60
61
        elif method == "eagle3":
            assert self.model is not None, \
                "model should not be None when method is eagle3"
            kwargs["architectures"] = [
62
63
                arch if arch.startswith("Eagle3") or arch.endswith("Eagle3")
                else f"Eagle3{arch}" for arch in self.model.architectures
64
            ]
65
        else:
66
67
            raise ValueError(f"Invalid method {method}. "
                             "Supported methods are eagle and eagle3.")
68
69
70
71
72

        super().__init__(**kwargs)

        if self.model is not None:
            for k, v in self.model.to_dict().items():
73
                if k not in kwargs:
74
                    setattr(self, k, v)
75
76
77
78
79
80
81
82
83
84

    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path: Union[str, os.PathLike],
        **kwargs,
    ) -> "EAGLEConfig":
        config_dict, kwargs = cls.get_config_dict(
            pretrained_model_name_or_path, **kwargs)
        return cls.from_dict(config_dict, **kwargs)