|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | + |
| 7 | +from harness.lm_eval_evaluator import evaluate_wrapper |
| 8 | +from harness.lm_eval_hf_model import HFEvalModel |
| 9 | +from harness.lm_eval_utils import MultiChoice, pattern_match |
| 10 | +from lm_eval.evaluator import make_table |
| 11 | +from lm_eval.tasks import ALL_TASKS |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(): |
| 15 | + parser = argparse.ArgumentParser(description="Evaluates pre-trained models using lm-eval package.") |
| 16 | + |
| 17 | + parser.add_argument("pre_trained_model_path", type=str, help="Path to the pre-trained model file.") |
| 18 | + |
| 19 | + parser.add_argument( |
| 20 | + "hub_tokenizer_path", |
| 21 | + type=str, |
| 22 | + help="Name or path to the Hugging Face hub's tokenizer.", |
| 23 | + ) |
| 24 | + |
| 25 | + parser.add_argument( |
| 26 | + "-t", |
| 27 | + "--tasks", |
| 28 | + choices=MultiChoice(ALL_TASKS), |
| 29 | + type=str, |
| 30 | + default=None, |
| 31 | + help="Tasks to be evaluated (separated by comma), e.g., `wsc,cb,copa`.", |
| 32 | + ) |
| 33 | + |
| 34 | + parser.add_argument( |
| 35 | + "-o", |
| 36 | + "--output_path", |
| 37 | + type=str, |
| 38 | + default=None, |
| 39 | + help="Path to the saved outputs.", |
| 40 | + ) |
| 41 | + |
| 42 | + parser.add_argument( |
| 43 | + "-ns", |
| 44 | + "--n_few_shot_samples", |
| 45 | + type=int, |
| 46 | + default=0, |
| 47 | + help="Number of few-shot samples.", |
| 48 | + ) |
| 49 | + |
| 50 | + parser.add_argument( |
| 51 | + "-ls", |
| 52 | + "--limit_samples", |
| 53 | + type=int, |
| 54 | + default=None, |
| 55 | + help="Limit the number of samples.", |
| 56 | + ) |
| 57 | + |
| 58 | + parser.add_argument( |
| 59 | + "-nc", |
| 60 | + "--no_cache", |
| 61 | + action="store_true", |
| 62 | + help="Whether to not store predictions in a cache database.", |
| 63 | + ) |
| 64 | + |
| 65 | + parser.add_argument( |
| 66 | + "-dnp", |
| 67 | + "--decontamination_ngrams_path", |
| 68 | + type=str, |
| 69 | + default=None, |
| 70 | + help="Path to the de-contamination n-grams file.", |
| 71 | + ) |
| 72 | + |
| 73 | + parser.add_argument( |
| 74 | + "-ddp", |
| 75 | + "--description_dict_path", |
| 76 | + type=str, |
| 77 | + default=None, |
| 78 | + help="Path to the description dictionary file.", |
| 79 | + ) |
| 80 | + |
| 81 | + parser.add_argument( |
| 82 | + "-ci", |
| 83 | + "--check_integrity", |
| 84 | + action="store_true", |
| 85 | + help="Whether to check integrity of tasks.", |
| 86 | + ) |
| 87 | + |
| 88 | + return parser.parse_args() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + args = parse_args() |
| 93 | + |
| 94 | + if args.limit_samples: |
| 95 | + print("Warning: --limit_samples should only be used for testing.") |
| 96 | + |
| 97 | + task_names = ALL_TASKS if args.tasks is None else pattern_match(args.tasks.split(","), ALL_TASKS) |
| 98 | + print(f"Selected Tasks: {task_names}") |
| 99 | + |
| 100 | + description_dict = {} |
| 101 | + if args.description_dict_path: |
| 102 | + with open(args.description_dict_path, "r") as f: |
| 103 | + description_dict = json.load(f) |
| 104 | + |
| 105 | + model = HFEvalModel(args.pre_trained_model_path, args.hub_tokenizer_path) |
| 106 | + |
| 107 | + outputs = evaluate_wrapper( |
| 108 | + model, |
| 109 | + task_names, |
| 110 | + num_fewshot=args.n_few_shot_samples, |
| 111 | + no_cache=args.no_cache, |
| 112 | + limit=args.limit_samples, |
| 113 | + description_dict=description_dict, |
| 114 | + check_integrity=args.check_integrity, |
| 115 | + decontamination_ngrams_path=args.decontamination_ngrams_path, |
| 116 | + ) |
| 117 | + |
| 118 | + output_json = json.dumps(outputs, indent=2) |
| 119 | + if args.output_path: |
| 120 | + with open(args.output_path, "w") as f: |
| 121 | + f.write(output_json) |
| 122 | + |
| 123 | + print(make_table(outputs)) |
0 commit comments