-
Notifications
You must be signed in to change notification settings - Fork 515
/
Copy pathnormalized_config.py
310 lines (262 loc) · 11.3 KB
/
normalized_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# coding=utf-8
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Normalization configuration classes."""
import functools
from typing import Callable, Dict, Type, Union
from transformers import PretrainedConfig
class NormalizedConfig:
"""
Handles the normalization of [`PretrainedConfig`] attribute names, allowing to access attributes in a general way.
Attributes:
config ([`PretrainedConfig`]):
The config to normalize.
"""
def __init__(self, config: Union[PretrainedConfig, Dict], allow_new: bool = False, **kwargs):
self.config = config
for key, value in kwargs.items():
if allow_new or hasattr(self, key.upper()):
setattr(self, key.upper(), value)
else:
raise AttributeError(
f"{self.__class__} has not attribute {key}. Set allow_new=True to add a new attribute."
)
@classmethod
def with_args(cls, allow_new: bool = False, **kwargs) -> Callable[[PretrainedConfig], "NormalizedConfig"]:
return functools.partial(cls, allow_new=allow_new, **kwargs)
def __getattr__(self, attr_name):
if attr_name == "config":
return super().__getattr__(attr_name)
try:
attr_name = super().__getattribute__(attr_name.upper())
except AttributeError: # e.g. in the NormalizedTextAndVisionConfig case
pass
attr_name = attr_name.split(".")
leaf_attr_name = attr_name[-1]
config = self.config
for attr in attr_name[:-1]:
config = getattr(config, attr)
attr = getattr(config, leaf_attr_name, None)
# If the attribute was not specified manually, try to fallback on the attribute_map.
if attr is None:
attribute_map = getattr(self.config, "attribute_map", {})
attr = getattr(self.config, attribute_map.get(leaf_attr_name, ""), None)
if attr is None:
raise AttributeError(f'Could not find the attribute named "{leaf_attr_name}" in the normalized config.')
return attr
def has_attribute(self, attr_name):
try:
self.__getattr__(attr_name)
except AttributeError:
return False
return True
class NormalizedTimeSeriesForecastingConfig(NormalizedConfig):
NUM_INPUT_CHANNELS = "num_input_channels"
CONTEXT_LENGTH = "context_length"
class NormalizedTextConfig(NormalizedConfig):
VOCAB_SIZE = "vocab_size"
HIDDEN_SIZE = "hidden_size"
NUM_LAYERS = "num_hidden_layers"
NUM_ATTENTION_HEADS = "num_attention_heads"
EOS_TOKEN_ID = "eos_token_id"
class NormalizedTextConfigWithGQA(NormalizedTextConfig):
NUM_KEY_VALUE_HEADS = "num_key_value_heads"
class NormalizedSeq2SeqConfig(NormalizedTextConfig):
ENCODER_NUM_LAYERS = NormalizedTextConfig.NUM_LAYERS
DECODER_NUM_LAYERS = NormalizedTextConfig.NUM_LAYERS
ENCODER_NUM_ATTENTION_HEADS = NormalizedTextConfig.NUM_ATTENTION_HEADS
DECODER_NUM_ATTENTION_HEADS = NormalizedTextConfig.NUM_ATTENTION_HEADS
class NormalizedVisionConfig(NormalizedConfig):
IMAGE_SIZE = "image_size"
NUM_CHANNELS = "num_channels"
INPUT_SIZE = "input_size"
class NormalizedSegformerConfig(NormalizedVisionConfig):
NUM_ATTENTION_HEADS = "num_attention_heads"
HIDDEN_SIZE = "hidden_sizes"
# If the attribute is a list, return 0
# 0 means let the optimizer infer the correct value based on the model graph
def __getattr__(self, attr_name):
attr_value = super().__getattr__(attr_name)
if isinstance(attr_value, list):
attr_value = 0
return attr_value
class NormalizedTextAndVisionConfig(NormalizedTextConfig, NormalizedVisionConfig):
TEXT_CONFIG = None
VISION_CONFIG = None
def __getattr__(self, attr_name):
if self.TEXT_CONFIG is not None and attr_name.upper() in dir(NormalizedTextConfig):
attr_name = f"{self.TEXT_CONFIG}.{attr_name}"
elif self.VISION_CONFIG is not None and attr_name.upper() in dir(NormalizedVisionConfig):
attr_name = f"{self.VISION_CONFIG}.{attr_name}"
return super().__getattr__(attr_name)
Pix2StructNormalizedTextConfig = NormalizedTextAndVisionConfig.with_args(
text_config="text_config", vision_config="vision_config"
)
class NormalizedEncoderDecoderConfig(NormalizedConfig):
ENCODER_NORMALIZED_CONFIG_CLASS = None
DECODER_NORMALIZED_CONFIG_CLASS = None
def __getattr__(self, attr_name):
if self.ENCODER_NORMALIZED_CONFIG_CLASS is not None and attr_name.upper() in dir(
self.ENCODER_NORMALIZED_CONFIG_CLASS
):
return self.ENCODER_NORMALIZED_CONFIG_CLASS.__getattr__(attr_name)
if self.DECODER_NORMALIZED_CONFIG_CLASS is not None and attr_name.upper() in dir(
self.DECODER_NORMALIZED_CONFIG_CLASS
):
return self.DECODER_NORMALIZED_CONFIG_CLASS.__getattr__(attr_name)
return super().__getattr__(attr_name)
# TODO: this config is bug prone, as `encoder_attention_heads` and `decoder_attention_heads` may be different
BartLikeNormalizedTextConfig = NormalizedTextConfig.with_args(
num_attention_heads="encoder_attention_heads",
hidden_size="d_model",
)
GPT2LikeNormalizedTextConfig = NormalizedTextConfig.with_args(num_attention_heads="n_head", hidden_size="n_embd")
T5LikeNormalizedTextConfig = NormalizedTextConfig.with_args(
num_attention_heads="num_heads",
hidden_size="d_model",
)
MPTNormalizedTextConfig = NormalizedTextConfig.with_args(
num_attention_heads="n_heads", hidden_size="d_model", num_layers="n_layers"
)
GPTBigCodeNormalizedTextConfig = NormalizedTextConfig.with_args(
num_attention_heads="n_head", hidden_size="n_embd", num_layers="n_layer"
)
WhisperLikeNormalizedTextConfig = NormalizedTextConfig.with_args(
hidden_size="d_model",
)
TrOCRLikeNormalizedTextConfig = NormalizedTextConfig.with_args(
num_layers="decoder_layers",
num_attention_heads="decoder_attention_heads",
hidden_size="hidden_size",
)
SpeechToTextLikeNormalizedTextConfig = NormalizedSeq2SeqConfig.with_args(
decoder_num_layers="decoder_layers",
num_layers="decoder_layers",
input_features_per_channel="input_feat_per_channel",
allow_new=True,
)
class NormalizedConfigManager:
"""
A class that contains all the information needed by ONNX Runtime optimization for a given model type.
Attributes:
_conf (`Dict[str, tuple]`):
A dictionary mapping each supported model type to a tuple containing the number of attention heads
and the hidden size model config attribute names as well as the corresponding ONNX Runtime model type.
"""
"""
TODO: missing normalized configs (currently not useful)
['beit',
'clip',
'convbert',
'convnext',
'convnextv2',
'data2vec-text',
'data2vec-vision',
'detr',
'dinov2',
'flaubert',
'groupvit',
'hiera',
'ibert',
'layoutlm',
'layoutlmv3',
'levit',
'mobilebert',
'mobilevit',
'owlv2',
'owlvit',
'perceiver',
'roformer',
'segformer',
'siglip',
'squeezebert',
'table-transformer',
"""
# Contribution note: Please add new models in alphabetical order
_conf = {
"albert": NormalizedTextConfig,
"bart": BartLikeNormalizedTextConfig,
"bert": NormalizedTextConfig,
"big-bird": NormalizedTextConfig,
"bigbird-pegasus": BartLikeNormalizedTextConfig,
"blenderbot": BartLikeNormalizedTextConfig,
"blenderbot-small": BartLikeNormalizedTextConfig,
"bloom": NormalizedTextConfig.with_args(num_layers="n_layer"),
"falcon": NormalizedTextConfig,
"camembert": NormalizedTextConfig,
"codegen": GPT2LikeNormalizedTextConfig,
"cvt": NormalizedVisionConfig,
"deberta": NormalizedTextConfig,
"deberta-v2": NormalizedTextConfig,
"deit": NormalizedVisionConfig,
"distilbert": NormalizedTextConfig.with_args(num_attention_heads="n_heads", hidden_size="dim"),
"donut-swin": NormalizedVisionConfig,
"electra": NormalizedTextConfig,
"encoder-decoder": NormalizedEncoderDecoderConfig,
"gemma": NormalizedTextConfigWithGQA,
"gpt2": GPT2LikeNormalizedTextConfig,
"gpt-bigcode": GPTBigCodeNormalizedTextConfig,
"gpt-neo": NormalizedTextConfig.with_args(num_attention_heads="num_heads"),
"gpt-neox": NormalizedTextConfig,
"gptj": GPT2LikeNormalizedTextConfig,
"imagegpt": GPT2LikeNormalizedTextConfig,
"llama": NormalizedTextConfigWithGQA,
"longt5": T5LikeNormalizedTextConfig,
"marian": BartLikeNormalizedTextConfig,
"markuplm": NormalizedTextConfig,
"mbart": BartLikeNormalizedTextConfig,
"mistral": NormalizedTextConfigWithGQA,
"mixtral": NormalizedTextConfigWithGQA,
"modernbert": NormalizedTextConfig,
"mpnet": NormalizedTextConfig,
"mpt": MPTNormalizedTextConfig,
"mt5": T5LikeNormalizedTextConfig,
"m2m-100": BartLikeNormalizedTextConfig,
"nystromformer": NormalizedTextConfig,
"opt": NormalizedTextConfig,
"pegasus": BartLikeNormalizedTextConfig,
"pix2struct": Pix2StructNormalizedTextConfig,
"phi": NormalizedTextConfig,
"phi3": NormalizedTextConfigWithGQA,
"phi3small": NormalizedTextConfigWithGQA,
"poolformer": NormalizedVisionConfig,
"regnet": NormalizedVisionConfig,
"resnet": NormalizedVisionConfig,
"roberta": NormalizedTextConfig,
"segformer": NormalizedSegformerConfig,
"speech-to-text": SpeechToTextLikeNormalizedTextConfig,
"splinter": NormalizedTextConfig,
"t5": T5LikeNormalizedTextConfig,
"trocr": TrOCRLikeNormalizedTextConfig,
"vision-encoder-decoder": NormalizedEncoderDecoderConfig,
"vit": NormalizedVisionConfig,
"whisper": WhisperLikeNormalizedTextConfig,
"xlm-roberta": NormalizedTextConfig,
"yolos": NormalizedVisionConfig,
"qwen2": NormalizedTextConfig,
"granite": NormalizedTextConfigWithGQA,
}
@classmethod
def check_supported_model(cls, model_type: str):
if model_type not in cls._conf:
model_types = ", ".join(cls._conf.keys())
raise KeyError(
f"{model_type} model type is not supported yet in NormalizedConfig. Only {model_types} are supported. "
f"If you want to support {model_type} please propose a PR or open up an issue."
)
@classmethod
def get_normalized_config_class(cls, model_type: str) -> Type:
model_type = model_type.replace("_", "-")
cls.check_supported_model(model_type)
return cls._conf[model_type]