write_out.py 2.42 KB
Newer Older
Jason Phang's avatar
Jason Phang committed
1
2
import argparse
import numpy as np
3
import json
Jason Phang's avatar
Jason Phang committed
4
import os
Leo Gao's avatar
Leo Gao committed
5
from lm_eval.utils import join_iters
6
7
from lm_eval.tasks import include_path
from lm_eval.logger import eval_logger
Jason Phang's avatar
Jason Phang committed
8
9
10
11
12
13

EXAMPLE_DIVIDER = "!!@@##@@!! -- Example {i}\n"


def parse_args():
    parser = argparse.ArgumentParser()
Fabrizio Milo's avatar
Fabrizio Milo committed
14
15
16
17
18
19
    parser.add_argument("--output_base_path", required=True)
    parser.add_argument("--tasks", default="all_tasks")
    parser.add_argument("--sets", type=str, default="val")  # example: val,test
    parser.add_argument("--num_fewshot", type=int, default=1)
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--num_examples", type=int, default=1)
20
21
22
23
24
25
    parser.add_argument(
        "--include_path",
        type=str,
        default=None,
        help="Additional path to include if there are external tasks to include.",
    )
Jason Phang's avatar
Jason Phang committed
26
27
28
29
30
31
32
    return parser.parse_args()


def main():
    args = parse_args()
    np.random.seed(args.seed)

33
34
35
36
    if args.include_path is not None:
        eval_logger.info(f"Including path: {args.include_path}")
        include_path(args.include_path)

Jason Phang's avatar
Jason Phang committed
37
38
39
40
    if args.tasks == "all_tasks":
        task_names = tasks.ALL_TASKS
    else:
        task_names = args.tasks.split(",")
41
    task_dict = tasks.get_task_dict(task_names)
42
43
    
          os.makedirs(args.output_base_path, exist_ok=True)
Jason Phang's avatar
Jason Phang committed
44
    for task_name, task in task_dict.items():
Leo Gao's avatar
Leo Gao committed
45
46
        rnd = random.Random()
        rnd.seed(args.seed)
Leo Gao's avatar
Leo Gao committed
47
48
49
50

        iters = []

        for set in args.sets.split(","):
51
            docs = None
Fabrizio Milo's avatar
Fabrizio Milo committed
52
            if set == "train" and task.has_training_docs():
jeffhsu3's avatar
jeffhsu3 committed
53
                docs = task.training_docs()
Fabrizio Milo's avatar
Fabrizio Milo committed
54
            if set == "val" and task.has_validation_docs():
Leo Gao's avatar
Leo Gao committed
55
                docs = task.validation_docs()
Fabrizio Milo's avatar
Fabrizio Milo committed
56
            if set == "test" and task.has_test_docs():
Leo Gao's avatar
Leo Gao committed
57
                docs = task.test_docs()
58
59
            if docs is not None:
                iters.append(docs)
jeffhsu3's avatar
jeffhsu3 committed
60

Leo Gao's avatar
Leo Gao committed
61
62
        docs = join_iters(iters)

Lintang Sutawika's avatar
Lintang Sutawika committed
63
64
65
        with open(
            os.path.join(args.output_base_path, task_name), "w", encoding="utf8"
        ) as f:
Fabrizio Milo's avatar
Fabrizio Milo committed
66
67
68
69
70
            for i, doc in (
                zip(range(args.num_examples), docs)
                if args.num_examples > 0
                else enumerate(docs)
            ):
Jason Phang's avatar
Jason Phang committed
71
                f.write(EXAMPLE_DIVIDER.format(i=i))
72
                ctx = task.fewshot_context(
Jason Phang's avatar
Jason Phang committed
73
74
75
76
77
78
79
80
                    doc=doc,
                    num_fewshot=args.num_fewshot,
                )
                f.write(ctx + "\n")


if __name__ == "__main__":
    main()