__init__.py 3.23 KB
Newer Older
lintangsutawika's avatar
lintangsutawika committed
1
2
3
4
5
6
7
8
9
10
11
12
13
import os
import yaml

from lm_eval import utils
from lm_eval.tasks import register_configurable_task, check_prompt_config
from lm_eval.logger import eval_logger
from lm_eval.api.registry import (
    TASK_REGISTRY,
    GROUP_REGISTRY,
    ALL_TASKS,
)


Ethan Smith's avatar
Ethan Smith committed
14
def include_benchmarks(task_dir: str) -> None:
lintangsutawika's avatar
lintangsutawika committed
15
    for root, subdirs, file_list in os.walk(task_dir):
16
        if (subdirs == [] or "__pycache__" in subdirs) and (len(file_list) > 0):
lintangsutawika's avatar
lintangsutawika committed
17
18
19
20
21
22
23
24
            for f in file_list:
                if f.endswith(".yaml"):
                    try:
                        benchmark_path = os.path.join(root, f)

                        with open(benchmark_path, "rb") as file:
                            yaml_config = yaml.full_load(file)

25
26
27
                        if "prompts" in yaml_config:
                            continue  # Skip it

lintangsutawika's avatar
lintangsutawika committed
28
29
30
31
32
33
34
35
36
37
38
                        assert "group" in yaml_config
                        group = yaml_config["group"]
                        all_task_list = yaml_config["task"]
                        config_list = [
                            task for task in all_task_list if type(task) != str
                        ]
                        task_list = [
                            task for task in all_task_list if type(task) == str
                        ]

                        for task_config in config_list:
39
40
41
42
43
44
45
46
47
48
                            yaml_dir = os.path.dirname(benchmark_path)
                            task_config = utils.load_yaml_config(
                                yaml_config=task_config, yaml_dir=yaml_dir
                            )
                            if "use_prompt" in task_config:
                                if "yaml" in task_config["use_prompt"]:
                                    task_config["use_prompt"] = os.path.join(
                                        root, task_config["use_prompt"]
                                    )

lintangsutawika's avatar
lintangsutawika committed
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
                            var_configs = check_prompt_config(
                                {
                                    **task_config,
                                    **{"group": group},
                                }
                            )
                            for config in var_configs:
                                register_configurable_task(config)

                        task_names = utils.pattern_match(task_list, ALL_TASKS)
                        for task in task_names:
                            if task in TASK_REGISTRY:
                                if group in GROUP_REGISTRY:
                                    GROUP_REGISTRY[group].append(task)
                                else:
                                    GROUP_REGISTRY[group] = [task]
                                    ALL_TASKS.add(group)
                    except Exception as error:
                        eval_logger.warning(
                            "Failed to load benchmark in\n"
                            f"                                 {benchmark_path}\n"
                            "                                 Benchmark will not be added to registry\n"
                            f"                                 Error: {error}"
                        )

lintangsutawika's avatar
lintangsutawika committed
74

lintangsutawika's avatar
lintangsutawika committed
75
task_dir = os.path.dirname(os.path.abspath(__file__)) + "/"
lintangsutawika's avatar
lintangsutawika committed
76
include_benchmarks(task_dir)