Skip to content

Commit 2edfb81

Browse files
authored
Fix cleanup space (ml-explore#1347)
* fix clean_up_tokenization_spaces * remove cleanup for bpe tokenizer
1 parent df1d3f3 commit 2edfb81

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

mlx_lm/tokenizer_utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class NaiveStreamingDetokenizer(StreamingDetokenizer):
6969
def __init__(self, tokenizer):
7070
self._tokenizer = tokenizer
7171
self._tokenizer.decode([0])
72+
probe = tokenizer.encode("a ,b", add_special_tokens=False)
73+
self._clean_spaces = " ," not in tokenizer.decode(probe)
7274
self.reset()
7375

7476
def reset(self):
@@ -92,7 +94,7 @@ def text(self):
9294
if self._current_tokens:
9395
self._current_text = self._tokenizer.decode(self._current_tokens)
9496
if self._current_text.endswith("\ufffd") or (
95-
self._tokenizer.clean_up_tokenization_spaces
97+
self._clean_spaces
9698
and len(self._current_text) > 0
9799
and self._current_text[-1] == " "
98100
):
@@ -160,11 +162,8 @@ class BPEStreamingDetokenizer(StreamingDetokenizer):
160162
"""
161163

162164
_byte_decoder = None
163-
_space_matches = (".", "?", "!", ",", "n't", "'m", "'s", "'ve", "'re")
164165

165166
def __init__(self, tokenizer):
166-
self.clean_spaces = tokenizer.clean_up_tokenization_spaces
167-
168167
# Extract the tokens in a list from id to text
169168
self.tokenmap = [None] * len(tokenizer.vocab)
170169
for value, tokenid in tokenizer.vocab.items():
@@ -199,8 +198,6 @@ def _maybe_trim_space(self, current_text):
199198
return current_text
200199
elif not self.text:
201200
return current_text[1:]
202-
elif self.clean_spaces and current_text[1:].startswith(self._space_matches):
203-
return current_text[1:]
204201
return current_text
205202

206203
def add_token(self, token):

0 commit comments

Comments
 (0)