-
Notifications
You must be signed in to change notification settings - Fork 164
Adding long context benchmark MRCR #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3b9aaaf
prepare mrcr data
fayejf 58fca84
init loc
fayejf c886242
eval
fayejf ad8f095
change default max
fayejf a6a6231
test_datasets
fayejf 94732ca
readme
fayejf 94e93bd
Merge branch 'main' into fayejf/mrcr
fayejf a4e0c67
Update tests/test_datasets.py
fayejf e6d5437
Update nemo_skills/dataset/mrcr/prepare.py
fayejf 5746b33
Update nemo_skills/dataset/mrcr/prepare.py
fayejf d673cab
Update nemo_skills/dataset/mrcr/prepare.py
fayejf f1301a0
move install and import tiktoken
fayejf 4c36e45
revert dynamically override __init__.py. leave instruction
fayejf c6f0c46
Merge branch 'main' into fayejf/mrcr
fayejf 08c77af
Fix import error
Kipok cbf3ae9
Merge branch 'main' into fayejf/mrcr
fayejf 15ee906
fix init
fayejf db200e8
update prompt_format to openai
fayejf 176277c
PROMPT CONFIG None
fayejf b5c993f
Merge branch 'main' into fayejf/mrcr
fayejf 2ee1a57
fix
fayejf 18027bd
remove convert string
fayejf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| EVAL_SPLIT = 'all' | ||
| PROMPT_CONFIG = 'null' | ||
| DATASET_GROUP = 'long-context' | ||
| METRICS_TYPE = 'mrcr' | ||
| EVAL_ARGS = '++eval_type=mrcr' | ||
| GENERATION_ARGS = '++prompt_format=openai' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import argparse | ||
| import json | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import tiktoken | ||
| from datasets import load_dataset | ||
| from tqdm import tqdm | ||
|
|
||
| """ | ||
| Usage | ||
| # default. setup is all. | ||
| python prepare.py | ||
|
|
||
| # prepare subset needle2_128k. | ||
| python prepare.py --max_context_window 131072 --needles_subset 2 --setup needle2_128k | ||
| python prepare.py --max_context_window 131072 --needles_subset 2 4 --setup needle2_needle_4_128k | ||
| """ | ||
|
|
||
|
|
||
| def count_n_tokens(messages: list[dict]) -> int: | ||
| """ | ||
| Follow the official way to count tokens in messages. | ||
| with tokenizer o200k_base | ||
| """ | ||
| enc = tiktoken.get_encoding("o200k_base") | ||
| return sum([len(enc.encode(m["content"])) for m in messages]) | ||
|
|
||
|
|
||
| def write_data_to_file(output_file, data, max_context_window, needles_subset): | ||
| with open(output_file, "wt", encoding="utf-8") as fout: | ||
| for idx, entry in tqdm(enumerate(data), desc=f"Writing {output_file.name}"): | ||
| messages = json.loads(entry["prompt"]) | ||
|
|
||
| if entry['n_needles'] not in needles_subset: | ||
| print(f"Skipping {idx} because it has {entry['n_needles']} needle") | ||
| continue | ||
|
|
||
| # find n_tokens | ||
| n_tokens = count_n_tokens(messages) | ||
| if max_context_window is not None: | ||
| if n_tokens > max_context_window: | ||
| print(f"Skipping {idx} because it has {n_tokens} tokens") | ||
| continue | ||
|
|
||
| entry['messages'] = entry.pop('prompt') | ||
| entry['expected_answer'] = entry.pop('answer') | ||
| entry['n_tokens'] = n_tokens | ||
| json.dump(entry, fout) | ||
| fout.write("\n") | ||
|
|
||
|
|
||
| def get_mrcr_data(needles_subset, setup, max_context_window): | ||
| dataset = load_dataset("openai/mrcr")['train'] | ||
| data_dir = Path(__file__).absolute().parent | ||
|
|
||
| output_file = data_dir / f"{setup}.jsonl" | ||
| write_data_to_file(output_file, dataset, max_context_window, needles_subset) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Prepare MRCR dataset.") | ||
| parser.add_argument( | ||
| "--max_context_window", | ||
| type=int, | ||
| default=None, | ||
| help="Maximum context window size.", | ||
| ) | ||
| parser.add_argument( | ||
| "--needles_subset", | ||
| nargs="+", | ||
| type=int, | ||
| choices=[2, 4, 8], | ||
| default=[2, 4, 8], | ||
| help="Needles subset to include.", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--setup", | ||
| type=str, | ||
| default="all", | ||
| help="setup name. e.g. all or <needle2>_<128k>", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| print(f"Preparing MRCR dataset with additional arguments: {args}") | ||
| get_mrcr_data(args.needles_subset, args.setup, args.max_context_window) | ||
| print(f"MRCR dataset preparation with setup {args.setup} completed. Use --split=${args.setup} to evaluate!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import json | ||
| import logging | ||
| from tqdm import tqdm | ||
| from nemo_skills.utils import get_logger_name, unroll_files | ||
| from difflib import SequenceMatcher | ||
|
|
||
| LOG = logging.getLogger(get_logger_name(__file__)) | ||
|
|
||
|
|
||
| def eval_mrcr(cfg): | ||
| def grade(response, answer, random_string_to_prepend) -> float: | ||
| """ | ||
| Compare response and answer. | ||
| # Offical grading function: https://huggingface.co/datasets/openai/mrcr | ||
| """ | ||
| if not response.startswith(random_string_to_prepend): | ||
| return 0 | ||
| response = response.removeprefix(random_string_to_prepend) | ||
| answer = answer.removeprefix(random_string_to_prepend) | ||
| return float(SequenceMatcher(None, response, answer).ratio()) | ||
|
|
||
|
|
||
|
|
||
| for file in unroll_files(cfg.input_files): | ||
| with open(file, 'rt', encoding='utf-8') as fin: | ||
| data = [json.loads(line) for line in fin] | ||
| with open(file, 'wt', encoding='utf-8') as fout: | ||
| for sample in tqdm(data): | ||
| sample['seq_match_ratio'] = grade( | ||
| sample['generation'], | ||
| sample['expected_answer'], | ||
| sample['random_string_to_prepend'] | ||
| ) | ||
| fout.write(json.dumps(sample) + "\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from nemo_skills.evaluation.metrics.base import BaseMetrics | ||
|
|
||
|
|
||
| class MRCRMetrics(BaseMetrics): | ||
| """Metrics for MRCR (Multi-Round Coreference) evaluation.""" | ||
|
|
||
| def _get_score_dict(self, prediction: dict) -> dict[str, bool | int | float]: | ||
| return {"accuracy": prediction['seq_match_ratio']} | ||
|
|
||
| def update(self, predictions): | ||
| super().update(predictions) | ||
| self._compute_pass_at_k(predictions=predictions) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.