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

4
5
import os

6
from transformers import AutoConfig, DeepseekV2Config, PretrainedConfig
7

8
9
10
11

class EAGLEConfig(PretrainedConfig):
    model_type = "eagle"

12
13
    def __init__(
        self,
14
15
16
        model: PretrainedConfig | dict | None = None,
        truncated_vocab_size: int | None = None,
        method: str | None = "eagle",
17
18
        **kwargs,
    ):
19
        model_config: PretrainedConfig | DeepseekV2Config | None
20
        if isinstance(model, dict):
21
            model_config = AutoConfig.for_model(**model)
22
23
        else:
            model_config = model
24
25

        for k, v in kwargs.items():
26
            if k != "architectures" and k != "model_type" and hasattr(model_config, k):
27
28
29
30
31
32
33
                setattr(model_config, k, v)

        self.model = model_config

        if self.model is None:
            self.truncated_vocab_size = None
        else:
34
35
36
37
38
            self.truncated_vocab_size = (
                self.model.vocab_size
                if truncated_vocab_size is None
                else truncated_vocab_size
            )
39

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

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

        super().__init__(**kwargs)

        if self.model is not None:
            for k, v in self.model.to_dict().items():
72
                if k not in kwargs:
73
                    setattr(self, k, v)
74
75
76
77

    @classmethod
    def from_pretrained(
        cls,
78
        pretrained_model_name_or_path: str | os.PathLike,
79
80
81
        **kwargs,
    ) -> "EAGLEConfig":
        config_dict, kwargs = cls.get_config_dict(
82
83
            pretrained_model_name_or_path, **kwargs
        )
84
        return cls.from_dict(config_dict, **kwargs)
85
86
87
88
89
90

    def to_json_string(self, use_diff: bool = True) -> str:
        # we override use_diff to False as initializing
        # EAGLEConfig with default arguments is not supported
        del use_diff
        return super().to_json_string(use_diff=False)