Fix IndexError in LogProbTokenNorm when choices_tokens is shorter than choices_logprob#1171
Open
worksbyfriday wants to merge 1 commit intohuggingface:mainfrom
Open
Conversation
…n choices_logprob Fixes issue huggingface#1170: LogProbTokenNorm crashes with IndexError when the number of token sequences in choices_tokens is less than the number of log probabilities in choices_logprob. This can occur during benchmarking when token generation fails for some answer choices. Changes: - Replaced list comprehension with explicit loop to add bounds checking - Check both length and presence of tokens: if ix < len(choices_tokens) and choices_tokens[ix] - Filter out padding tokens marked as -1 when counting token length - Add fallback: if tokens are missing, use log probability without normalization - Avoid division by zero with max(token_count, 1) This handles the edge case defensively without changing the core normalization logic for valid cases. The fix allows the normalization to complete even when some choices lack token data, using the un-normalized log probability as a reasonable fallback. Tested with the scenario from the issue: - Input: 4 choices (4 logprobs, 3 token lists) - Output: 4 normalized values (no IndexError) - For the 4th choice with missing tokens, uses un-normalized logprob
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes issue #1170: LogProbTokenNorm crashes with IndexError when the number of token sequences is less than the number of log probabilities. This occurs during benchmarking when token generation fails for some answer choices.
Problem
When normalizing log probabilities for token-based metrics, the code assumes
choices_tokenshas at least as many elements aschoices_logprob. However, when token generation fails for some choices,choices_tokenscan be shorter, causing an IndexError on line 527:From the issue (#1170):
Solution
Replaced the list comprehension with an explicit loop that:
if ix < len(choices_tokens) and choices_tokens[ix]max(token_count, 1)Testing
Tested with the exact scenario from the issue:
Impact