Fix tokenizer.encode() to respect add_special_tokens=False parameter #821
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