main
py 105 lines 3.51 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import os
5
6 from transformers import AutoConfig, DeepseekV2Config, PreTrainedConfig
7
8 from vllm.transformers_utils.utils import without_trust_remote_code
9
10
11 class EAGLEConfig(PreTrainedConfig):
12 model_type = "eagle"
13
14 def __init__(
15 self,
16 model: PreTrainedConfig | dict | None = None,
17 truncated_vocab_size: int | None = None,
18 method: str | None = "eagle",
19 **kwargs,
20 ):
21 model_config: PreTrainedConfig | DeepseekV2Config | None
22 if isinstance(model, dict):
23 model_config = AutoConfig.for_model(**model)
24 else:
25 model_config = model
26
27 for k, v in kwargs.items():
28 if k != "architectures" and k != "model_type" and hasattr(model_config, k):
29 setattr(model_config, k, v)
30
31 self.model = model_config
32
33 if self.model is None:
34 self.truncated_vocab_size = None
35 else:
36 self.truncated_vocab_size = (
37 self.model.vocab_size
38 if truncated_vocab_size is None
39 else truncated_vocab_size
40 )
41
42 # Eagle model name should follow naming convention of
43 # LlamaForCausalLM -> EagleLlamaForCausalLM
44 # LlamaForCausalLM -> Eagle3LlamaForCausalLM
45 # LlamaForCausalLMEagle3 -> LlamaForCausalLMEagle3
46 if method == "eagle":
47 assert self.model is not None, (
48 "model should not be None when method is eagle"
49 )
50 kwargs["architectures"] = [
51 f"Eagle{arch}" if not arch.startswith("Eagle") else arch
52 for arch in self.model.architectures
53 ]
54
55 elif method == "eagle3":
56 assert self.model is not None, (
57 "model should not be None when method is eagle3"
58 )
59 kwargs["architectures"] = [
60 arch
61 if arch.startswith("Eagle3") or arch.endswith("Eagle3")
62 else f"Eagle3{arch}"
63 for arch in self.model.architectures
64 ]
65 elif method == "dflash":
66 assert self.model is not None, (
67 "model should not be None when method is dflash"
68 )
69 kwargs["architectures"] = [
70 arch
71 if arch.startswith("DFlash")
72 or arch.endswith("DFlash")
73 or arch == "LiLiCorrDraftModel"
74 else f"DFlash{arch}"
75 for arch in self.model.architectures
76 ]
77 else:
78 raise ValueError(
79 f"Invalid method {method}. Supported methods are "
80 "eagle, eagle3, and dflash."
81 )
82
83 super().__init__(**kwargs)
84
85 if self.model is not None:
86 for k, v in self.model.to_dict().items():
87 if k not in kwargs:
88 setattr(self, k, v)
89
90 @classmethod
91 def from_pretrained(
92 cls,
93 pretrained_model_name_or_path: str | os.PathLike,
94 **kwargs,
95 ) -> "EAGLEConfig":
96 config_dict, kwargs = cls.get_config_dict(
97 pretrained_model_name_or_path, **without_trust_remote_code(kwargs)
98 )
99 return cls.from_dict(config_dict, **kwargs)
100
101 def to_json_string(self, use_diff: bool = True) -> str:
102 # we override use_diff to False as initializing
103 # EAGLEConfig with default arguments is not supported
104 del use_diff
105 return super().to_json_string(use_diff=False)