Repository navigation
Report truncated_tokens_count in tokens on the transformers backend - #1435
Open
isHeSatoshi wants to merge 1 commit into
Open
isHeSatoshi wants to merge 1 commit into
isHeSatoshi wants to merge 1 commit into
Conversation
`_padded_greedy_until` filled `Batch.truncated` with
`len(context) - tokenized["input_ids"].shape[1]`, i.e. the character count of
the context minus the padded token length, while the value is consumed as
`ModelResponse.truncated_tokens_count` ("How many tokens truncated"). Every
generated sample therefore reported a wrong truncation count, and a prompt
that fits entirely in the context window still reported a non-zero number of
dropped tokens.
The greedy-until path lets the tokenizer truncate, so the untruncated token
count of each context has to be measured separately. `_loglikelihood_tokens`
already computes this count in tokens.
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.
Fixes #1198.
What was wrong
ModelResponse.truncated_tokens_countis documented as "How many tokens truncated" (src/lighteval/models/model_output.py), but on the transformers backend every generated sample reported a wrong number. A prompt that fits entirely in the model's context window still reported a non-zero truncation count, and a genuinely truncated prompt reported how many characters were dropped instead of how many tokens.That is what #1198 is about: a 5-shot GSM8K run saved
"padded_tokens_count": 178, "truncated_tokens_count": 2047in its details file even though the prompt fits in the context window.2047islen(prompt) - 178, i.e. the character count of the prompt minus its token count.Root cause
TransformersModel._padded_greedy_untilfilledBatch.truncatedwithcontextsis a list of prompt strings, solen(c)counts characters, whiletokenized["input_ids"].shape[1]is the padded token length. The greedy-until path lets the tokenizer truncate (truncation="longest_first"), so the untruncated token count is not recoverable fromtokenizedafterwards._loglikelihood_tokens(_build_model_inputs) computes the same field in tokens (max(len(orig_tokens) - effective_max_length, 0)), which is why this only shows up in generative evaluations.What changed
src/lighteval/models/transformers/transformers_model.py:_padded_greedy_untilnow measures the untruncated token count of each context before building theBatch, andBatch.truncatediscontext_token_count - padded_token_length. The extra tokenization passesverbose=Falseso it does not add a spurious "Token indices sequence length is longer than the specified maximum" warning. Nothing else changes:padded_tokens_countandinput_tokensare untouched.tests/unit/models/test_transformers_model.py: two regression tests in a newTestTransformersModelTruncatedTokensCountclass.Verification
Environment: Windows 11, Python 3.11.9,
uv venv+uv pip install -e ".[dev]", CPU torch, modelgpt2(context length 1024). Generation is stubbed out soBatch.truncatedcan be read directly;expectedismax(context_token_count - 1024, 0).On
main:With this change:
The two new tests fail on
mainand pass here:Uncertainty a reviewer might raise
_padded_greedy_untilnow tokenizes each batch twice: once to count the untruncated tokens and once (as before) to build the padded tensors. Tokenization is not the bottleneck of this path, but the count could instead be derived from a single untruncated tokenization plus a manual truncate/pad step.truncated_tokens_countis only consumed for the saved per-sample details, so this affects the reported metadata rather than the scores.SummaryTask.truncated/non_truncatedinsrc/lighteval/logging/info_loggers.pyare declared but never incremented by anything; I left those alone because that is a separate change.AI assistance (disclosure): this change was prepared with an AI coding agent (Factory Droid) working in my checkout. Every result quoted above comes from actually running the commands shown, not from the agent's description. Happy to answer questions or rework anything.