Fix tokenizer.encode() to respect add_special_tokens=False parameter#821
Open
ved1beta wants to merge 5 commits intoallenai:mainfrom
Open
Fix tokenizer.encode() to respect add_special_tokens=False parameter#821ved1beta wants to merge 5 commits intoallenai:mainfrom
ved1beta wants to merge 5 commits intoallenai:mainfrom
Conversation
dirkgr
requested changes
May 15, 2025
olmo/tokenizer.py
Outdated
| batch_encoding = self.base_tokenizer.encode_batch(inputs) | ||
| # Check if the base tokenizer's encode_batch method supports add_special_tokens parameter | ||
| if 'add_special_tokens' in inspect.signature(self.base_tokenizer.encode_batch).parameters: | ||
| batch_encoding = self.base_tokenizer.encode_batch(inputs, add_special_tokens=False) |
Member
There was a problem hiding this comment.
This hard-codes add_special_tokens to False. Did you mean to pass it through from the arguments?
…Mo into Fix_add_special_tokens=False
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.
Problem
The
add_special_tokens=Falseparameter in the tokenizer'sencode/encode_batchmethods doesn't work as expected. Even when set toFalse, special tokens (like EOS) are still being added to the encoded output.Root Cause
The issue occurs because the
add_special_tokensparameter is not being passed to the base tokenizer'sencode_batchmethod. While our code correctly handles the parameter after encoding, by that point the base tokenizer may have already added special tokens.Solution
This PR adds a check to see if the base tokenizer's
encode_batchmethod supports theadd_special_tokensparameter, and if so, passes it to ensure special tokens are not added by the base tokenizer. This provides backward compatibility with tokenizers that don't support this parameter.Testing
I've verified the fix by testing encodings with and without special tokens:
add_special_tokens=Falsenow correctly returns only content tokens without the EOS tokenadd_special_tokens=Truecontinues to work as before, returning content tokens plus the EOS tokenFixes #765