main
py 65 lines 2.31 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Config definitions for ExtractHiddenStatesModel, to be used with
4 the extract_hidden_states spec decoding method."""
5
6 import os
7
8 from transformers import PreTrainedConfig
9
10 from vllm.transformers_utils.utils import without_trust_remote_code
11
12
13 class ExtractHiddenStatesConfig(PreTrainedConfig):
14 model_type = "extract_hidden_states"
15
16 def __init__(
17 self,
18 model: PreTrainedConfig | dict | None = None,
19 method: str | None = "extract_hidden_states",
20 **kwargs,
21 ):
22 assert method == "extract_hidden_states"
23
24 if isinstance(model, dict):
25 model_dict = model
26 source_text_config = None
27 elif isinstance(model, PreTrainedConfig):
28 model_dict = model.to_dict()
29 text_config = model.get_text_config()
30 source_text_config = text_config if text_config is not model else None
31 else:
32 model_dict = {}
33 source_text_config = None
34
35 # Combine: model_dict first, then kwargs override
36 combined = {**model_dict, **kwargs}
37 # Remove architectures from the base, we'll set it explicitly
38 combined = {k: v for k, v in combined.items() if k != "architectures"}
39
40 combined["architectures"] = ["ExtractHiddenStatesModel"]
41
42 # to_dict() and kwargs both flatten text_config to a plain dict;
43 # downstream get_hf_text_config() needs it as a PreTrainedConfig
44 # for attribute access. Re-insert the original object.
45 if source_text_config is not None:
46 combined["text_config"] = source_text_config
47
48 super().__init__(**combined)
49
50 @classmethod
51 def from_pretrained(
52 cls,
53 pretrained_model_name_or_path: str | os.PathLike,
54 **kwargs,
55 ) -> "ExtractHiddenStatesConfig":
56 config_dict, kwargs = cls.get_config_dict(
57 pretrained_model_name_or_path, **without_trust_remote_code(kwargs)
58 )
59 return cls.from_dict(config_dict, **kwargs)
60
61 def to_json_string(self, use_diff: bool = True) -> str:
62 # we override use_diff to False as initializing
63 # ExtractHiddenStatesConfig with default arguments is not supported
64 del use_diff
65 return super().to_json_string(use_diff=False)