|
| 1 | +# MIT License |
| 2 | + |
| 3 | +# Copyright (c) 2024 The HuggingFace Team |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +from lighteval.logging.evaluation_tracker import EvaluationTracker |
| 24 | +from lighteval.metrics.utils.metric_utils import MetricCategory |
| 25 | +from lighteval.models.abstract_model import ModelInfo |
| 26 | +from lighteval.tasks.lighteval_task import LightevalTask |
| 27 | +from lighteval.tasks.registry import Registry, taskinfo_selector |
| 28 | +from lighteval.utils.utils import as_list |
| 29 | + |
| 30 | + |
| 31 | +def main(args): |
| 32 | + """ |
| 33 | + Compute baselines for given tasks. |
| 34 | +
|
| 35 | + It has been tested with generative and accuracy tasks, but may not work correctly for other task types. |
| 36 | +
|
| 37 | + The baseline is computed as follows: |
| 38 | + - For multiple-choice tasks: It assumes random guessing, so the score is n_correct/number_of_choices. |
| 39 | + - For other metrics: It assigns a score of 0, which may not be appropriate for all task types. |
| 40 | +
|
| 41 | + Note: |
| 42 | + This baseline computation may not be suitable for all task types and should be used with caution. |
| 43 | + """ |
| 44 | + task_registry = Registry(cache_dir=args.cache_dir, custom_tasks=args.custom_tasks) |
| 45 | + task_names_list, fewshots_dict = taskinfo_selector(args.tasks, task_registry) |
| 46 | + task_dict = task_registry.get_task_dict(task_names_list) |
| 47 | + |
| 48 | + evaluation_tracker = EvaluationTracker( |
| 49 | + output_dir=args.output_dir, |
| 50 | + save_details=False, |
| 51 | + push_to_hub=False, |
| 52 | + push_to_tensorboard=False, |
| 53 | + public=False, |
| 54 | + hub_results_org=None, |
| 55 | + ) |
| 56 | + evaluation_tracker.general_config_logger.log_model_info( |
| 57 | + ModelInfo( |
| 58 | + model_name="lighteval/baseline", |
| 59 | + model_sha=None, |
| 60 | + model_dtype=None, |
| 61 | + model_size=None, |
| 62 | + ) |
| 63 | + ) |
| 64 | + evaluation_tracker.task_config_logger.log(task_dict) |
| 65 | + |
| 66 | + LightevalTask.load_datasets(list(task_dict.values()), args.dataset_loading_processes) |
| 67 | + |
| 68 | + for task_name, task in task_dict.items(): |
| 69 | + task_docs = list(task.eval_docs()) |
| 70 | + n_samples = min(args.max_samples, len(task_docs)) if args.max_samples else len(task_docs) |
| 71 | + |
| 72 | + p_correct_score = [ |
| 73 | + len(as_list(task_doc.gold_index)) / len(task_doc.choices) for task_doc in task_docs[:n_samples] |
| 74 | + ] |
| 75 | + |
| 76 | + metric_results = { |
| 77 | + metric.metric_name: p_correct_score |
| 78 | + if metric.category |
| 79 | + in [MetricCategory.MULTICHOICE, MetricCategory.MULTICHOICE_PMI, MetricCategory.MULTICHOICE_ONE_TOKEN] |
| 80 | + else 0 |
| 81 | + for metric in task.metrics |
| 82 | + } |
| 83 | + |
| 84 | + for fewshots, _ in fewshots_dict[task_name]: |
| 85 | + evaluation_tracker.metrics_logger.log(f"{task_name}|{fewshots}", metric_results) |
| 86 | + |
| 87 | + evaluation_tracker.metrics_logger.aggregate(task_dict=task_dict, bootstrap_iters=1000) |
| 88 | + evaluation_tracker.save() |
0 commit comments