registry.py 4.6 KB
Newer Older
1
import logging
Baber Abbasi's avatar
Baber Abbasi committed
2
from typing import Callable, Dict
3

Baber Abbasi's avatar
Baber Abbasi committed
4
5
import evaluate as hf_evaluate

6
from lm_eval.api.model import LM
7

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
76
        return fn

    return decorate


OUTPUT_TYPE_REGISTRY = {}
77
78
METRIC_REGISTRY = {}
METRIC_AGGREGATION_REGISTRY = {}
Baber Abbasi's avatar
Baber Abbasi committed
79
AGGREGATION_REGISTRY: Dict[str, Callable[[], Dict[str, Callable]]] = {}
80
81
82
83
84
85
86
87
HIGHER_IS_BETTER_REGISTRY = {}

DEFAULT_METRIC_REGISTRY = {
    "loglikelihood": [
        "perplexity",
        "acc",
    ],
    "loglikelihood_rolling": ["word_perplexity", "byte_perplexity", "bits_per_byte"],
88
    "multiple_choice": ["acc", "acc_norm"],
89
    "generate_until": ["exact_match"],
90
91
92
93
94
95
96
97
98
99
100
101
}


def register_metric(**args):
    # TODO: do we want to enforce a certain interface to registered metrics?
    def decorate(fn):
        assert "metric" in args
        name = args["metric"]

        for key, registry in [
            ("metric", METRIC_REGISTRY),
            ("higher_is_better", HIGHER_IS_BETTER_REGISTRY),
102
            ("aggregation", METRIC_AGGREGATION_REGISTRY),
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        ]:
            if key in args:
                value = args[key]
                assert (
                    value not in registry
                ), f"{key} named '{value}' conflicts with existing registered {key}!"

                if key == "metric":
                    registry[name] = fn
                elif key == "aggregation":
                    registry[name] = AGGREGATION_REGISTRY[value]
                else:
                    registry[name] = value

        return fn

    return decorate


Baber Abbasi's avatar
Baber Abbasi committed
122
def get_metric(name: str, hf_evaluate_metric=False) -> Callable:
123
124
125
126
127
128
129
    if not hf_evaluate_metric:
        if name in METRIC_REGISTRY:
            return METRIC_REGISTRY[name]
        else:
            eval_logger.warning(
                f"Could not find registered metric '{name}' in lm-eval, searching in HF Evaluate library..."
            )
Chris's avatar
Chris committed
130

131
    try:
Baber Abbasi's avatar
Baber Abbasi committed
132
        metric_object = hf_evaluate.load(name)
133
134
135
136
        return metric_object.compute
    except Exception:
        eval_logger.error(
            f"{name} not found in the evaluate library! Please check https://huggingface.co/evaluate-metric",
137
138
139
        )


Baber Abbasi's avatar
Baber Abbasi committed
140
def register_aggregation(name: str):
141
142
143
144
145
146
147
148
149
150
151
    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


Baber Abbasi's avatar
Baber Abbasi committed
152
def get_aggregation(name: str) -> Callable[[], Dict[str, Callable]]:
153
154
155
    try:
        return AGGREGATION_REGISTRY[name]
    except KeyError:
156
        eval_logger.warning(f"{name} not a registered aggregation metric!")
haileyschoelkopf's avatar
haileyschoelkopf committed
157
158


Baber Abbasi's avatar
Baber Abbasi committed
159
def get_metric_aggregation(name: str) -> Callable[[], Dict[str, Callable]]:
160
161
162
    try:
        return METRIC_AGGREGATION_REGISTRY[name]
    except KeyError:
163
        eval_logger.warning(f"{name} metric is not assigned a default aggregation!")
164
165


Baber Abbasi's avatar
Baber Abbasi committed
166
def is_higher_better(metric_name) -> bool:
haileyschoelkopf's avatar
haileyschoelkopf committed
167
168
169
    try:
        return HIGHER_IS_BETTER_REGISTRY[metric_name]
    except KeyError:
170
171
172
        eval_logger.warning(
            f"higher_is_better not specified for metric '{metric_name}'!"
        )