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 | """Freshly initialised encoder models."""
import os
from functools import cached_property
from json import JSONDecodeError
from transformers import (
AutoConfig,
AutoTokenizer,
ElectraForQuestionAnswering,
ElectraForSequenceClassification,
ElectraForTokenClassification,
PretrainedConfig,
PreTrainedModel,
PreTrainedTokenizer,
XLMRobertaForQuestionAnswering,
XLMRobertaForSequenceClassification,
XLMRobertaForTokenClassification,
)
from ..data_models import BenchmarkConfig, DatasetConfig, ModelConfig
from ..enums import InferenceBackend, ModelType, TaskGroup
from ..exceptions import (
InvalidBenchmark,
InvalidModel,
NeedsEnvironmentVariable,
NeedsExtraInstalled,
)
from ..utils import block_terminal_output, create_model_cache_dir
from .hf import (
HuggingFaceEncoderModel,
align_model_and_tokenizer,
setup_model_for_question_answering,
)
class FreshEncoderModel(HuggingFaceEncoderModel):
"""A freshly initialised encoder model."""
fresh_model = True
def __init__(
self,
model_config: ModelConfig,
dataset_config: DatasetConfig,
benchmark_config: BenchmarkConfig,
) -> None:
"""Initialise the model.
Args:
model_config:
The model configuration.
dataset_config:
The dataset configuration.
benchmark_config:
The benchmark configuration.
"""
# This is already set when calling `super.__init__`, but we need it to get a
# value from `self.model_max_length`, so we set it here as well.
self.model_config = model_config
model, tokenizer = load_model_and_tokenizer(
model_config=model_config,
dataset_config=dataset_config,
benchmark_config=benchmark_config,
model_max_length=self.model_max_length,
)
self._model: PreTrainedModel = model
self._tokenizer: PreTrainedTokenizer = tokenizer
self._model, self._tokenizer = align_model_and_tokenizer(
model=self._model,
tokenizer=self._tokenizer,
model_max_length=self.model_max_length,
raise_errors=benchmark_config.raise_errors,
)
# We specify `HuggingFaceEncoderModel` here instead of `VLLMModel`, as we want
# to call the `__init__` method of the `BenchmarkModule` class.
super(HuggingFaceEncoderModel, self).__init__(
model_config=model_config,
dataset_config=dataset_config,
benchmark_config=benchmark_config,
)
@cached_property
def num_params(self) -> int:
"""The number of parameters in the model.
Returns:
The number of parameters in the model.
"""
match self.model_config.model_id:
case "fresh-xlm-roberta-base":
return 278_885_778
case "fresh-electra-small":
return 13_738_755
case _:
raise NotImplementedError(
f"Number of parameters for model {self.model_config.model_id} is "
"not implemented."
)
@cached_property
def vocab_size(self) -> int:
"""The vocabulary size of the model.
Returns:
The vocabulary size of the model.
"""
match self.model_config.model_id:
case "fresh-xlm-roberta-base":
return 250_002
case "fresh-electra-small":
return 32_000
case _:
raise NotImplementedError(
f"Vocabulary size for model {self.model_config.model_id} is not "
"implemented."
)
@cached_property
def model_max_length(self) -> int:
"""The maximum context length of the model.
Returns:
The maximum context length of the model.
"""
match self.model_config.model_id:
case "fresh-xlm-roberta-base":
return 512
case "fresh-electra-small":
return 128
case _:
raise NotImplementedError(
f"Maximum context length for model {self.model_config.model_id} is "
"not implemented."
)
@classmethod
def model_exists(
cls, model_id: str, benchmark_config: BenchmarkConfig
) -> bool | NeedsExtraInstalled | NeedsEnvironmentVariable:
"""Check if a model exists.
Args:
model_id:
The model ID.
benchmark_config:
The benchmark configuration.
Returns:
Whether the model exists, or an error describing why we cannot check
whether the model exists.
"""
valid_models = ["fresh-electra-small", "fresh-xlm-roberta-base"]
return model_id in valid_models
@classmethod
def get_model_config(
cls, model_id: str, benchmark_config: BenchmarkConfig
) -> ModelConfig:
"""Fetch the model configuration.
Args:
model_id:
The model ID.
benchmark_config:
The benchmark configuration.
Returns:
The model configuration.
"""
return ModelConfig(
model_id=model_id,
task="fill-mask",
languages=list(),
revision="main",
merge=False,
inference_backend=InferenceBackend.TRANSFORMERS,
model_type=ModelType.ENCODER,
fresh=True,
model_cache_dir=create_model_cache_dir(
cache_dir=benchmark_config.cache_dir, model_id=model_id
),
adapter_base_model_id=None,
)
def load_model_and_tokenizer(
model_config: ModelConfig,
dataset_config: DatasetConfig,
benchmark_config: BenchmarkConfig,
model_max_length: int,
) -> tuple[PreTrainedModel, PreTrainedTokenizer]:
"""Load the model and tokenizer.
Args:
model_config:
The model configuration.
dataset_config:
The dataset configuration.
benchmark_config:
The benchmark configuration.
model_max_length:
The maximum context length of the model.
Returns:
The loaded model and tokenizer.
"""
config: "PretrainedConfig"
block_terminal_output()
# Get the fresh model ID and the corresponding real model ID
model_id = model_config.model_id.replace("-", "_")
fresh_to_real_model_id_mapping = dict(
fresh_xlm_roberta_base="FacebookAI/xlm-roberta-base",
fresh_electra_small="google/electra-small-discriminator",
)
real_model_id = fresh_to_real_model_id_mapping[model_id]
match dataset_config.task.task_group:
case (
TaskGroup.SEQUENCE_CLASSIFICATION
| TaskGroup.MULTIPLE_CHOICE_CLASSIFICATION
):
model_cls_mapping = dict(
fresh_xlm_roberta_base=XLMRobertaForSequenceClassification,
fresh_electra_small=ElectraForSequenceClassification,
)
case TaskGroup.TOKEN_CLASSIFICATION:
model_cls_mapping = dict(
fresh_xlm_roberta_base=XLMRobertaForTokenClassification,
fresh_electra_small=ElectraForTokenClassification,
)
case TaskGroup.QUESTION_ANSWERING:
model_cls_mapping = dict(
fresh_xlm_roberta_base=XLMRobertaForQuestionAnswering,
fresh_electra_small=ElectraForQuestionAnswering,
)
case _:
raise InvalidBenchmark(
f"Task group {dataset_config.task.task_group} is not "
f"supported for model {model_config.model_id}."
)
model_cls = model_cls_mapping[model_id]
config = AutoConfig.from_pretrained(
real_model_id,
token=benchmark_config.api_key or os.getenv("HUGGINGFACE_API_KEY") or True,
num_labels=dataset_config.num_labels,
id2label=dataset_config.id2label,
label2id=dataset_config.label2id,
cache_dir=model_config.model_cache_dir,
trust_remote_code=benchmark_config.trust_remote_code,
)
model = model_cls(config)
if dataset_config.task.task_group == TaskGroup.QUESTION_ANSWERING:
model = setup_model_for_question_answering(model=model)
# Load the tokenizer. If the model is a subclass of a RoBERTa model then we
# have to add a prefix space to the tokens, by the way the model is constructed
prefix_models = ["Roberta", "GPT", "Deberta"]
prefix = any(model_type in type(model).__name__ for model_type in prefix_models)
try:
tokenizer: "PreTrainedTokenizer" = AutoTokenizer.from_pretrained(
real_model_id,
revision=model_config.revision,
token=benchmark_config.api_key or os.getenv("HUGGINGFACE_API_KEY") or True,
add_prefix_space=prefix,
cache_dir=model_config.model_cache_dir,
use_fast=True,
verbose=False,
trust_remote_code=benchmark_config.trust_remote_code,
)
except (JSONDecodeError, OSError):
raise InvalidModel(f"Could not load tokenizer for model {real_model_id!r}.")
model, tokenizer = align_model_and_tokenizer(
model=model,
tokenizer=tokenizer,
model_max_length=model_max_length,
raise_errors=benchmark_config.raise_errors,
)
return model, tokenizer
|