Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow single byte decoding #13988

Merged
merged 1 commit into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transformers/models/byt5/tokenization_byt5.py
Expand Up @@ -237,7 +237,7 @@ def convert_tokens_to_string(self, tokens):
else:
tok_string = bytes([ord(token)])
bstring += tok_string
string = bstring.decode("utf-8")
string = bstring.decode("utf-8", errors="ignore")
return string

# ByT5Tokenizer has no vocab file
Expand Down
16 changes: 16 additions & 0 deletions tests/test_tokenization_byt5.py
Expand Up @@ -290,6 +290,22 @@ def test_special_tokens_initialization_with_non_empty_additional_special_tokens(
),
)

def test_decode_single_bytes(self):
tokenizer_list = []
if self.test_slow_tokenizer:
tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()))

if self.test_rust_tokenizer:
tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()))

for tokenizer_class, tokenizer_utils in tokenizer_list:
with tempfile.TemporaryDirectory() as tmp_dir:
tokenizer_utils.save_pretrained(tmp_dir)

tokenizer = tokenizer_class.from_pretrained(tmp_dir)

self.assertTrue(tokenizer.decode([255]) == "")

# tokenizer can be instantiated without any pretrained files, so no need for pretrained tokenizer list
def test_pretrained_model_lists(self):
pass
Expand Down