|
| 1 | +import os |
| 2 | +import re |
| 3 | +from typing import List |
| 4 | +from datasets import load_dataset, Features, Value, Sequence, Dataset |
| 5 | +from helm.benchmark.scenarios.scenario import ( |
| 6 | + Scenario, |
| 7 | + Instance, |
| 8 | + Input, |
| 9 | + Reference, |
| 10 | + Output, |
| 11 | + CORRECT_TAG, |
| 12 | + TEST_SPLIT, |
| 13 | +) |
| 14 | +from helm.common.general import ensure_directory_exists |
| 15 | + |
| 16 | + |
| 17 | +class InfiniteBenchSumScenario(Scenario): |
| 18 | + """InfiniteBench Sum |
| 19 | +
|
| 20 | + InfiniteBench is a benchmark tailored for evaluating the capabilities of language models to process, |
| 21 | + understand, and reason over super long contexts (100k+ tokens). InfiniteBench Sum is a subset of |
| 22 | + InfiniteBench that requires models to generate a concise summary of the novel. The subset is referred |
| 23 | + to as "En.Sum" in the original paper. |
| 24 | + """ |
| 25 | + |
| 26 | + name = "infinite_bench_sum" |
| 27 | + description = "Summarize a novel from InfiniteBench" |
| 28 | + tags = ["summarization"] |
| 29 | + |
| 30 | + def __init__(self, min_num_word: float = 0.0, max_num_word: float = 100e6): |
| 31 | + self.min_num_word = min_num_word |
| 32 | + self.max_num_word = max_num_word |
| 33 | + super().__init__() |
| 34 | + |
| 35 | + def get_instances(self, output_path: str) -> List[Instance]: |
| 36 | + # Get InfiniteBench from HuggingFace |
| 37 | + cache_dir = os.path.join(output_path, "data") |
| 38 | + ensure_directory_exists(cache_dir) |
| 39 | + |
| 40 | + # Define the features schema |
| 41 | + ft = Features( |
| 42 | + { |
| 43 | + "id": Value("int64"), |
| 44 | + "context": Value("string"), |
| 45 | + "input": Value("string"), |
| 46 | + "answer": Sequence(Value("string")), |
| 47 | + "options": Sequence(Value("string")), |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + # Load the dataset with the specified features |
| 52 | + dataset = load_dataset( |
| 53 | + "xinrongzhang2022/InfiniteBench", |
| 54 | + split="longbook_sum_eng", |
| 55 | + features=ft, |
| 56 | + revision="90f0394333616266d9fe85824ceaf505093cbaa5", |
| 57 | + ) |
| 58 | + |
| 59 | + assert isinstance(dataset, Dataset) |
| 60 | + |
| 61 | + def count_words(text: str) -> int: |
| 62 | + return len(re.split(r"\s+", text.strip())) |
| 63 | + |
| 64 | + dataset = dataset.map( |
| 65 | + lambda example: {"prompt_wc": count_words(example["context"]) + count_words(example["input"])} |
| 66 | + ).filter(lambda example: self.min_num_word <= example["prompt_wc"] <= self.max_num_word) |
| 67 | + |
| 68 | + # Read all instances |
| 69 | + instances: List[Instance] = [] |
| 70 | + for row in dataset: |
| 71 | + id = row["id"] |
| 72 | + input = Input(text=row["context"] + "\n\n" + row["input"]) |
| 73 | + instance = Instance( |
| 74 | + id=id, |
| 75 | + input=input, |
| 76 | + references=[Reference(Output(text=row["answer"][0]), tags=[CORRECT_TAG])], |
| 77 | + split=TEST_SPLIT, |
| 78 | + extra_data={"word_count": row["prompt_wc"]}, |
| 79 | + ) |
| 80 | + instances.append(instance) |
| 81 | + |
| 82 | + return instances |
0 commit comments