registry.py 4.44 KB
Newer Older
1
import os
lintangsutawika's avatar
lintangsutawika committed
2
import logging
3
import evaluate
4
import collections
lintangsutawika's avatar
lintangsutawika committed
5
6
from functools import partial

7
from lm_eval.api.model import LM
lintangsutawika's avatar
lintangsutawika committed
8

9
eval_logger = logging.getLogger("lm-eval")
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

MODEL_REGISTRY = {}


def register_model(*names):
    # either pass a list or a single alias.
    # function receives them as a tuple of strings

    def decorate(cls):
        for name in names:
            assert issubclass(
                cls, LM
            ), f"Model '{name}' ({cls.__name__}) must extend LM class"

            assert (
                name not in MODEL_REGISTRY
            ), f"Model named '{name}' conflicts with existing model! Please register with a non-conflicting alias instead."

            MODEL_REGISTRY[name] = cls
        return cls

    return decorate


def get_model(model_name):
haileyschoelkopf's avatar
haileyschoelkopf committed
35
36
37
    try:
        return MODEL_REGISTRY[model_name]
    except KeyError:
38
39
40
        raise ValueError(
            f"Attempted to load model '{model_name}', but no model for this name found! Supported model names: {', '.join(MODEL_REGISTRY.keys())}"
        )
41
42
43
44


TASK_REGISTRY = {}
GROUP_REGISTRY = {}
45
ALL_TASKS = set()
46
47
48
49
50
51
52
53
54
55
func2task_index = {}


def register_task(name):
    def decorate(fn):
        assert (
            name not in TASK_REGISTRY
        ), f"task named '{name}' conflicts with existing registered task!"

        TASK_REGISTRY[name] = fn
56
        ALL_TASKS.add(name)
57
58
59
60
61
62
63
64
65
66
67
68
69
        func2task_index[fn.__name__] = name
        return fn

    return decorate


def register_group(name):
    def decorate(fn):
        func_name = func2task_index[fn.__name__]
        if name in GROUP_REGISTRY:
            GROUP_REGISTRY[name].append(func_name)
        else:
            GROUP_REGISTRY[name] = [func_name]
70
            ALL_TASKS.add(name)
71
72
73
74
75
        return fn

    return decorate


76
77
METRIC_REGISTRY = collections.defaultdict(dict)
AGGREGATION_REGISTRY = collections.defaultdict(dict)
78
79

DEFAULT_METRIC_REGISTRY = {
80
81
82
83
    "loglikelihood": [],
    "loglikelihood_rolling": [],
    "multiple_choice": [],
    "generate_until": [],
84
85
86
}


87
def register_metric(
lintangsutawika's avatar
lintangsutawika committed
88
    metric=None,
89
90
    higher_is_better=None,
    output_type=None,
91
    aggregation=None,
92
):
93
94
95
    # TODO: do we want to enforce a certain interface to registered metrics?
    def decorate(fn):

lintangsutawika's avatar
lintangsutawika committed
96
97
98
99
100
101
        if type(metric) == str:
            metric_list = [metric]
        elif type(metric) == list:
            metric_list = metric

        for _metric in metric_list:
102
103
104
105
            METRIC_REGISTRY[_metric]["function"] = fn

            if aggregation is not None:
                METRIC_REGISTRY[_metric]["aggregation"] = aggregation
lintangsutawika's avatar
lintangsutawika committed
106
107

            if higher_is_better is not None:
108
                METRIC_REGISTRY[_metric]["higher_is_better"] = higher_is_better
lintangsutawika's avatar
lintangsutawika committed
109
110
111
112
113
114
115
116
117
118

            if output_type is not None:
                if type(output_type) == str:
                    output_type_list = [output_type]
                elif type(output_type) == list:
                    output_type_list = output_type

                for _output_type in output_type_list:
                    DEFAULT_METRIC_REGISTRY[_output_type].append(_metric)

119
120
121
122
123
        return fn

    return decorate


124
def get_metric(name):
125

126
127
128
129
130
131
132
    if name in METRIC_REGISTRY:
        return METRIC_REGISTRY[name]
    else:
        eval_logger.error(f"Could not find registered metric '{name}' in lm-eval")


def get_evaluate(name, **kwargs):
Chris's avatar
Chris committed
133

134
    try:
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

        class HFEvaluateAdaptor:
            def __init__(self, name, **kwargs):

                self.name = name
                metric_object = evaluate.load(name)
                self.hf_evaluate_fn = partial(metric_object.compute, **kwargs)

            def __call__(self, items):
                refs = list(zip(*items))[0]
                preds = list(zip(*items))[1]

                return self.hf_evaluate_fn(references=refs, predictions=preds)[
                    self.name
                ]

151
        return HFEvaluateAdaptor(name, **kwargs)
152
153
154
    except Exception:
        eval_logger.error(
            f"{name} not found in the evaluate library! Please check https://huggingface.co/evaluate-metric",
155
156
157
        )


158
159
160
161
162
163
164
165
166
167
168
169
170
def register_aggregation(name):
    def decorate(fn):
        assert (
            name not in AGGREGATION_REGISTRY
        ), f"aggregation named '{name}' conflicts with existing registered aggregation!"

        AGGREGATION_REGISTRY[name] = fn
        return fn

    return decorate


def get_aggregation(name):
haileyschoelkopf's avatar
haileyschoelkopf committed
171
    try:
172
        return AGGREGATION_REGISTRY[name]
haileyschoelkopf's avatar
haileyschoelkopf committed
173
    except KeyError:
174
        eval_logger.warning(
175
            "{} not a registered aggregation metric!".format(name),
176
        )