Honor the model's generation_config.eos_token_id in the transformers backend#1279
Open
waqaskhan137 wants to merge 1 commit into
Open
Honor the model's generation_config.eos_token_id in the transformers backend#1279waqaskhan137 wants to merge 1 commit into
waqaskhan137 wants to merge 1 commit into
Conversation
…backend For generative tasks the transformers backend overrode eos_token_id with tokenizer.eos_token_id, discarding the terminators the model itself declares. Chat models whose turn terminator is not the tokenizer eos (e.g. Gemma ends turns with token 106 while tokenizer.eos is 1, generation_config declares [1, 106, 50]) therefore never stopped and padded every generation to max_new_tokens, wasting up to ~95% of generated tokens and corrupting generation-length measurements. Prefer model.generation_config.eos_token_id when set, falling back to the tokenizer's. Measured on Gemma MMLU CoT: 7168 -> 2654 tokens, 145s -> 55s per item, extracted answer unchanged. Fixes huggingface#1278
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 #1278.
What
For generative tasks,
_generate_padded'sgeneration_config.update(...)setseos_token_id=self.tokenizer.eos_token_id, overriding the terminators the model itself declares ingeneration_config.eos_token_id. Chat models whose turn terminator is not the tokenizer's eos never stop: they emit their turn-end token, it is ignored, and every generation runs tomax_new_tokens.Concrete case in the issue: Gemma ends chat turns with token 106 while
tokenizer.eos_tokenis<eos>(id 1); the model declaresgeneration_config.eos_token_id = [1, 106, 50]. With the override, an MMLU chain-of-thought task padded every generation to the cap, with 63-95% of returned tokens being token 106 repeated.Fix
Prefer
self.model.generation_config.eos_token_idwhen set, falling back toself.tokenizer.eos_token_id(one line, plus a comment). Models whose declared eos already equals the tokenizer's are unaffected.Validation
Measured on
google/gemma-4-E2B-it, MMLU CoT item, RTX PRO 6000, bf16 (details in #1278):ruff checkandruff format --checkpass on the touched file.