test_evaluator.py 3.9 KB
Newer Older
1
import os
2
import re
3
4
5
from typing import List

import pytest
haileyschoelkopf's avatar
haileyschoelkopf committed
6

7
import lm_eval.api as api
Leo Gao's avatar
Leo Gao committed
8
import lm_eval.evaluator as evaluator
9
from lm_eval import tasks
10
from lm_eval.utils import make_table
11

Leo Gao's avatar
Leo Gao committed
12

13
os.environ["TOKENIZERS_PARALLELISM"] = "false"
Leo Gao's avatar
Leo Gao committed
14
15
16
# TODO: more fine grained unit tests rather than this big honking integration
# test once we break evaluator into smaller, more manageable pieces

17
18

@pytest.mark.parametrize(
19
    "task_name,limit,model,model_args,bootstrap_iters",
20
21
22
23
24
25
    [
        (
            ["arc_easy"],
            10,
            "hf",
            "pretrained=EleutherAI/pythia-160m,dtype=float32,device=cpu",
26
            0,
27
28
29
30
31
32
        ),
        (
            ["mmlu_abstract_algebra"],
            None,
            "hf",
            "pretrained=EleutherAI/pythia-160m,dtype=float32,device=cpu",
33
            10000,
34
        ),
35
36
    ],
)
37
38
39
def test_evaluator(
    task_name: List[str], limit: int, model: str, model_args: str, bootstrap_iters: int
):
baberabb's avatar
baberabb committed
40
    e1 = evaluator.simple_evaluate(
41
42
43
44
        model=model,
        tasks=task_name,
        limit=limit,
        model_args=model_args,
45
        bootstrap_iters=bootstrap_iters,
46
    )
47
48
49
50
51
52
53
54
55
56
    assert e1 is not None

    lm = api.registry.get_model(model).create_from_arg_string(
        model_args,
        {
            "batch_size": None,
            "max_batch_size": None,
            "device": None,
        },
    )
57
58
    task_manager = tasks.TaskManager()
    task_dict = tasks.get_task_dict(task_name, task_manager)
59
60
61
62

    e2 = evaluator.evaluate(
        lm=lm,
        task_dict=task_dict,
63
        limit=limit,
64
        bootstrap_iters=bootstrap_iters,
65
    )
66

67
    assert e2 is not None
68
    # check that caching is working
69
70

    def r(x):
71
72
73
74
        if "arc_easy" in x["results"]:
            return x["results"]["arc_easy"]
        else:
            return x["results"]["mmlu_abstract_algebra"]
75
76
77
78
79

    assert all(
        x == y
        for x, y in zip([y for _, y in r(e1).items()], [y for _, y in r(e2).items()])
    )
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


@pytest.mark.parametrize(
    "task_name,limit,model,model_args",
    [
        (
            ["ai2_arc"],
            10,
            "hf",
            "pretrained=EleutherAI/pythia-14m,dtype=float32,device=cpu",
        ),
        (
            ["mmlu_abstract_algebra", "mmlu_global_facts", "mmlu_public_relations"],
            10,
            "hf",
            "pretrained=EleutherAI/pythia-14m,dtype=float32,device=cpu",
        ),
        (
            ["lambada_openai"],
            10,
            "hf",
            "pretrained=EleutherAI/pythia-14m,dtype=float32,device=cpu",
        ),
        (
            ["wikitext"],
            10,
            "hf",
            "pretrained=EleutherAI/pythia-14m,dtype=float32,device=cpu",
        ),
    ],
)
def test_printed_results(task_name: List[str], limit: int, model: str, model_args: str):
    results = evaluator.simple_evaluate(
        model=model,
        tasks=task_name,
        limit=limit,
        model_args=model_args,
        bootstrap_iters=0,
        random_seed=0,
        numpy_random_seed=0,
        torch_random_seed=0,
        fewshot_random_seed=0,
    )

    filename = "_".join(
        (
            "-".join(task_name),
            str(limit),
            str(model),
            re.sub(r"[^a-zA-Z0-9_\-\.]", "-", model_args),
        )
    )
    filepath = f"./tests/testdata/{filename}.txt"
    with open(filepath, "r") as f:
        t1 = f.read().strip()

    t2 = make_table(results).strip()

    t1_lines, t2_lines = t1.splitlines(), t2.splitlines()
    assert len(t1_lines) == len(t2_lines)
    for t1_line, t2_line in zip(t1_lines, t2_lines):
        t1_items, t2_items = t1_line.split("|"), t2_line.split("|")
        assert len(t1_items) == len(t2_items)
        for t1_item, t2_item in zip(t1_items, t2_items):
            try:
                t1_item = float(t1_item)
                t2_item = float(t2_item)
                assert abs(t1_item - t2_item) < 0.1
            except ValueError:
                assert t1_item == t2_item