Transformers
PyTorch
text2text-generation

Regenerate the tokenizer.json pipeline from spiece.model

#3
by cidcodes - opened

spiece.model keeps whitespace (remove_extra_whitespaces: false) and uses byte fallback, but tokenizer.json collapses repeated spaces and has byte_fallback: false, and transformers v5 additionally rebuilds the T5 pipeline with a whitespace split. Tabs and newlines are lost:

text spiece.model current (transformers v5) with this PR
a\tb ▁a, <0x09>, b ▁a, ▁b ▁a, <0x09>, b
line one\nline two ▁line, ▁one, <0x0A>, line, ▁two ▁line, ▁one, ▁line, ▁two ▁line, ▁one, <0x0A>, line, ▁two
two spaces ▁two, ▁, ▁spaces ▁two, ▁spaces ▁two, ▁, ▁spaces

This PR replaces the normalizer, pre-tokenizer and decoder in tokenizer.json with the ones transformers generates from spiece.model (with legacy=True), turns on byte_fallback, and sets "tokenizer_class": "PreTrainedTokenizerFast" in tokenizer_config.json (TokenizersBackend in transformers v5), so the file is used as-is on v4 and v5, as suggested by the transformers maintainers in huggingface/transformers#51269. The vocabulary, the <extra_id_*> tokens and the post-processor are unchanged, so token ids do not move.

I checked it on 33,013 texts (FLORES-200 sentences in 205 languages, Hindi/Malayalam code-mixed text, and Unicode edge cases): the texts that tokenize differently from spiece.model go from 5,102 to 19, and decoding matches SentencePiece on all of them. </s> is still appended, and the special tokens are unchanged.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment