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 | """All the Hugging Face metrics used in EuroEval."""
import collections.abc as c
import os
import typing as t
from pathlib import Path
import evaluate
import numpy as np
from datasets import DownloadConfig, DownloadMode
from ..exceptions import InvalidBenchmark
from ..logging_utils import no_terminal_output
from .base import Metric
if t.TYPE_CHECKING:
from datasets.arrow_dataset import Dataset
from evaluate import EvaluationModule
from ..data_models import BenchmarkConfig, DatasetConfig
class HuggingFaceMetric(Metric):
"""A metric which is implemented in the `evaluate` package.
Attributes:
name:
The name of the metric in snake_case.
pretty_name:
The pretty name of the metric, used for display purposes.
huggingface_id:
The Hugging Face ID of the metric.
results_key:
The name of the key used to extract the metric scores from the results
dictionary.
compute_kwargs:
Keyword arguments to pass to the metric's compute function. Defaults to
an empty dictionary.
"""
def __init__(
self,
name: str,
pretty_name: str,
huggingface_id: str,
results_key: str,
compute_kwargs: dict[str, t.Any] | None = None,
postprocessing_fn: t.Callable[[float], tuple[float, str]] | None = None,
) -> None:
"""Initialise the Hugging Face metric.
Args:
name:
The name of the metric in snake_case.
pretty_name:
The pretty name of the metric, used for display purposes.
huggingface_id:
The Hugging Face ID of the metric.
results_key:
The name of the key used to extract the metric scores from the results
dictionary.
compute_kwargs:
Keyword arguments to pass to the metric's compute function. Defaults to
an empty dictionary.
postprocessing_fn:
A function to apply to the metric scores after they are computed, taking
the score to the postprocessed score along with its string
representation. Defaults to x -> (100 * x, f"{x:.2%}").
"""
super().__init__(
name=name, pretty_name=pretty_name, postprocessing_fn=postprocessing_fn
)
self.huggingface_id = huggingface_id
self.results_key = results_key
self.compute_kwargs: dict[str, t.Any] = (
dict() if compute_kwargs is None else compute_kwargs
)
self.metric: "EvaluationModule | None" = None
def download(self, cache_dir: str) -> "HuggingFaceMetric":
"""Initiates the download of the metric if needed.
Args:
cache_dir:
The directory where the metric will be downloaded to.
Returns:
The metric object itself.
"""
metric_cache_dir = Path(cache_dir) / "metrics"
metric_cache_dir.mkdir(parents=True, exist_ok=True)
download_config = DownloadConfig(cache_dir=metric_cache_dir)
self.metric = evaluate.load(
path=self.huggingface_id,
download_config=download_config,
download_mode=DownloadMode.REUSE_CACHE_IF_EXISTS,
cache_dir=metric_cache_dir.as_posix(),
)
return self
def __call__(
self,
predictions: c.Sequence,
references: c.Sequence,
dataset: "Dataset",
dataset_config: "DatasetConfig",
benchmark_config: "BenchmarkConfig",
) -> float | None:
"""Calculate the metric score.
Args:
predictions:
The model predictions.
references:
The ground truth references.
dataset:
The dataset used for evaluation. This is only used in case any
additional metadata is used to compute the metrics.
dataset_config:
The dataset configuration.
benchmark_config:
The benchmark configuration.
Returns:
The calculated metric score, or None if the score should be ignored.
"""
if self.metric is None:
self.download(cache_dir=benchmark_config.cache_dir)
assert self.metric is not None, (
"Metric has not been downloaded. Please call download() before using the "
"__call__ method."
)
with no_terminal_output(disable=os.getenv("FULL_LOG", "0") == "1"):
results = self.metric.compute(
predictions=predictions, references=references, **self.compute_kwargs
)
# The metric returns None if we are running on multi-GPU and the current
# process is not the main process
if results is None:
return None
# Convert the results to a float score
score = results[self.results_key]
if isinstance(score, list):
score = sum(score) / len(score)
if isinstance(score, np.floating):
score = float(score)
return score
class SourceBasedMetric(HuggingFaceMetric):
"""Subclass of HuggingfaceMetric for metrics also requiring source text as input."""
def __call__(
self,
predictions: c.Sequence,
references: c.Sequence,
dataset: "Dataset",
dataset_config: "DatasetConfig",
benchmark_config: "BenchmarkConfig",
) -> float | None:
"""Calculate metric score for metrics requiring original source text.
Passes the source text to the evaluate function via its `sources` param.
Args:
predictions:
The model predictions.
references:
The ground truth references.
dataset:
The dataset used for evaluation. This is used for collecting the source
text and in case any additional metadata is used to compute the metrics.
dataset_config:
The dataset configuration.
benchmark_config:
The benchmark configuration.
Returns:
The calculated metric score, or None if the score should be ignored.
"""
if dataset is None:
raise InvalidBenchmark("SourceBasedMetric requires `dataset` to be passed.")
if self.metric is None:
self.download(cache_dir=benchmark_config.cache_dir)
sources = dataset["text"]
if not len(sources) == len(predictions):
raise InvalidBenchmark(
f"SourceBasedMetric expects same number of inputs as predictions."
f"Got {len(sources)} sources and {len(predictions)} predictions "
f"instead."
)
with no_terminal_output(disable=os.getenv("FULL_LOG", "0") == "1"):
results = self.metric.compute(
sources=sources,
predictions=predictions,
references=[[r] for r in references],
**self.compute_kwargs,
)
# The metric returns None if we are running on multi-GPU and the current
# process is not the main process
if results is None:
return None
# Convert the results to a float score
score = results[self.results_key]
if isinstance(score, list):
score = sum(score) / len(score)
if isinstance(score, np.floating):
score = float(score)
return score
mcc_metric = HuggingFaceMetric(
name="mcc",
pretty_name="Matthew's Correlation Coefficient",
huggingface_id="matthews_correlation",
results_key="matthews_correlation",
)
macro_f1_metric = HuggingFaceMetric(
name="macro_f1",
pretty_name="Macro-average F1-score",
huggingface_id="f1",
results_key="f1",
compute_kwargs=dict(average="macro"),
)
micro_f1_metric = HuggingFaceMetric(
name="micro_f1",
pretty_name="Micro-average F1-score with MISC tags",
huggingface_id="seqeval",
results_key="overall_f1",
)
micro_f1_no_misc_metric = HuggingFaceMetric(
name="micro_f1_no_misc",
pretty_name="Micro-average F1-score without MISC tags",
huggingface_id="seqeval",
results_key="overall_f1",
)
f1_metric = HuggingFaceMetric(
name="f1",
pretty_name="F1-score",
huggingface_id="squad_v2",
results_key="f1",
postprocessing_fn=lambda x: (x, f"{x:.2f}%"),
)
em_metric = HuggingFaceMetric(
name="em",
pretty_name="Exact Match",
huggingface_id="squad_v2",
results_key="exact",
postprocessing_fn=lambda x: (x, f"{x:.2f}%"),
)
bert_score_metric = HuggingFaceMetric(
name="bertscore",
pretty_name="BERTScore",
huggingface_id="bertscore",
results_key="f1",
compute_kwargs=dict(
model_type="microsoft/mdeberta-v3-base", device="auto", batch_size=1
),
)
rouge_l_metric = HuggingFaceMetric(
name="rouge_l", pretty_name="ROUGE-L", huggingface_id="rouge", results_key="rougeL"
)
accuracy_metric = HuggingFaceMetric(
name="accuracy",
pretty_name="Accuracy",
huggingface_id="accuracy",
results_key="accuracy",
)
meteor_metric = HuggingFaceMetric(
name="meteor", pretty_name="METEOR", huggingface_id="meteor", results_key="meteor"
)
sari_metric = SourceBasedMetric(
name="sari",
pretty_name="SARI",
huggingface_id="sari",
results_key="sari",
postprocessing_fn=lambda x: (x, f"{x:.2f}%"),
)
|