-
Notifications
You must be signed in to change notification settings - Fork 136
Add needle in haystack test #121
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
9 commits
Select commit
Hold shift + click to select a range
e2fe544
needle
alessiodevoto 4ce1c43
niah
alessiodevoto 5e777b5
rouge scorer
alessiodevoto a46c963
add inits
alessiodevoto 7412875
refactor
alessiodevoto be5b6dc
refactor eval
alessiodevoto e96247d
add niah
alessiodevoto 2b7d181
style
alessiodevoto 0eb4fa6
revert
alessiodevoto 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,2 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
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,2 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
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,2 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
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,3 @@ | ||
| # Needle in a Haystack | ||
| This benchmark evaluates a model's ability to retrieve a specific piece of information, the "needle," hidden within a large body of text, the "haystack." The test challenges a model's long-context understanding and its ability to maintain information accuracy over increasing document lengths. | ||
| We follow the vast majority of the literature and use [Paul Graham's essays](https://huggingface.co/datasets/alessiodevoto/paul_graham_essays) as the haystack. |
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,2 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
15 changes: 15 additions & 0 deletions
15
evaluation/benchmarks/needle_in_haystack/calculate_metrics.py
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,15 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pandas as pd | ||
| from rouge import Rouge | ||
|
|
||
| scorer = Rouge() | ||
|
|
||
|
|
||
| def calculate_metrics(df: pd.DataFrame) -> list[dict]: | ||
| scores = [] | ||
| for index, row in df.iterrows(): | ||
| score = scorer.get_scores(row["needle"], row["predicted_answer"])[0] | ||
| scores.append(score) | ||
| return scores |
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,49 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import logging | ||
|
|
||
| import pandas as pd | ||
| from transformers import PreTrainedTokenizer | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def insert_needle_in_haystack( | ||
| df: pd.DataFrame, tokenizer: PreTrainedTokenizer, max_context_length: int, needle_depth: int | ||
| ) -> pd.DataFrame: | ||
| """ | ||
| Inserts the "needle" string into the "context" of each row in the DataFrame at a specified depth. | ||
| Adapted from the original implementation: https://github.com/gkamradt/LLMTest_NeedleInAHaystack | ||
|
|
||
| Parameters | ||
| ---------- | ||
| df : pd.DataFrame | ||
| The input DataFrame containing at least the columns "context" and "needle". | ||
| tokenizer : PreTrainedTokenizer | ||
| The tokenizer used to encode and decode the context and needle. | ||
| max_context_length : int | ||
| The maximum allowed length (in tokens) for the context, including the needle. | ||
| needle_depth : int | ||
| The percentage (0-100) indicating how deep into the context the needle should be inserted. | ||
|
|
||
| Returns | ||
| ------- | ||
| pd.DataFrame | ||
| The DataFrame with the "context" column modified to include the needle at the specified depth. | ||
| """ | ||
| logger.info(f"Preparing dataset for inference with needle in haystack. Needle: {df['needle'][0]}") | ||
| tokenized_needle = tokenizer.encode(df["needle"][0], add_special_tokens=False) | ||
| context_length = max_context_length - len(tokenized_needle) - 150 # account for system prompts | ||
| needle_index = int(context_length * needle_depth / 100) | ||
| # tokenize the context | ||
| df["context"] = df["context"].apply(lambda x: tokenizer.encode(x, add_special_tokens=False)[:context_length]) | ||
| # insert the needle at the depth specified in the config | ||
| df["context"] = df["context"].apply(lambda x: x[:needle_index] + tokenized_needle + x[needle_index:]) | ||
| # detokenize the context | ||
| df["context"] = ( | ||
| "This is a very long story book: <book> " | ||
| + df["context"].apply(lambda x: tokenizer.decode(x, skip_special_tokens=True)) | ||
| + " </book>." | ||
| ) | ||
| return df |
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
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
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.