Repository navigation
[LLM:Bugfix] Buffer split Unicode tokenizer output - #4789
Conversation
|
Thanks for digging into this — the diagnosis is correct on both counts, and both are real latent bugs:
I have one blocking concern before this can be merged, plus two smaller notes. Blocking: the buffer is unbounded and never flushedI replayed the new
The last row is a regression. Once an ID that can never form valid UTF-8 enters the buffer (a stray continuation byte, a genuine Row 3 is also not quite clean: while a pending ID sits in the buffer, a following well-formed token is emitted without flushing the pending bytes, so they linger and can later concatenate with unrelated IDs. A UTF-8 scalar is at most 4 bytes, so capping the buffer is enough: def id_to_str(self, token_id):
token_id = int(token_id)
try:
word = self.tokenizer.decode(token_id)
except Exception:
word = self.tokenizer.convert_tokens_to_string(
self.tokenizer._convert_id_to_token(token_id))
if '\uFFFD' not in word:
if self.decode_buffer:
# pending bytes can never complete; emit them instead of dropping
word = self._flush_decode_buffer() + word
return word
# Smollm-style tokenizers can emit half of a CJK character.
self.decode_buffer.append(token_id)
buffer_txt = self.tokenizer.decode(self.decode_buffer)
if '\uFFFD' not in buffer_txt or len(self.decode_buffer) >= 4:
self.decode_buffer.clear()
return buffer_txt
return ''Note 1: keep the retry on the same API as the fast pathThe primary path uses Note 2: the new test will not run anywhere
Also, the mock's Either wire the case into an existing CI stage (and add the Minor
Could you add the buffer cap and the pending-flush, and switch the retry to |
Description
Decode partial tokenizer output through the same tokenizer
decode()API as the normal path. Keep at most three pending token IDs after each call, emit the oldest undecodable token when the bound is reached, and retain the suffix needed to finish a four-byte UTF-8 character.Flush pending output before a normal decoded word and at both stop-token and maximum-length exits. Reset the buffer at the start of the next response. Literal replacement characters and malformed bytes can no longer hold later output indefinitely.
The unwired mocked tokenizer test has been removed as requested in review.
Module
LLM
Type
Tests
python -m compileall -q transformers/llm/export/utils/tokenizer.py transformers/llm/export/llmexport.pypython -m tabnanny transformers/llm/export/utils/tokenizer.py transformers/llm/export/llmexport.pygit diff --checkChecklist