[Bugfix][Core] Fix tekken edge case for mistral tokenizer (#8640)

This commit is contained in:
Patrick von Platen
2024-09-20 23:33:03 +02:00
committed by GitHub
parent 2874bac618
commit b4e4eda92e
2 changed files with 54 additions and 4 deletions

View File

@@ -175,10 +175,29 @@ class MistralTokenizer:
def convert_tokens_to_string(self, tokens: List[str]) -> str:
if isinstance(self.tokenizer, Tekkenizer):
return "".join(t for t in tokens
if t not in self.tokenizer._all_special_tokens)
tokens = [
t for t in tokens
if t not in self.tokenizer._all_special_tokens
]
if any(isinstance(t, bytes) for t in tokens):
# we need to encode and decode all tokens again
shift = self.tokenizer.num_special_tokens
byte_tokens = [
t.encode("utf-8") if not isinstance(t, bytes) else t
for t in tokens
]
ids = [
self.tokenizer._tekken_token2id_nospecial[t] + shift
for t in byte_tokens
]
decoded = self.tokenizer.decode(ids)
else:
decoded = "".join(tokens)
else:
return self.tokenizer.decode(tokens) # type: ignore[arg-type]
decoded = self.tokenizer.decode(tokens) # type: ignore[arg-type]
return decoded
def decode(self, ids: Union[List[int], int]) -> str:
if isinstance(ids, int):
@@ -200,4 +219,11 @@ class MistralTokenizer:
self.tokenizer)
tokens = [self.tokenizer.id_to_piece(id) for id in ids]
if any(t.strip() == "<EFBFBD>" for t in tokens):
# if any stripped decoded token is undefined
# because it's invalid unicode then pass bytes
# See: https://github.com/vllm-project/vllm/pull/8640
tokens = [self.tokenizer.id_to_byte_piece(id) for id in ids]
return tokens