字节级分词让大模型生成无效UTF-8,根本无法避免。
UTF-8 Plumbing: Byte-level Tokenizers Unavoidably Enable LLMs to Generate Ill-formed UTF-8
- 用字节构建词表,虽减少词汇量但可能生成非法UTF-8序列。
- 证明无论怎么处理,只要词表含非法字节,就必然产生非法输出。
- 揭示真实漏洞,适合做语言模型部署与安全的开发者参考。
子词分词将输入文本按预定义词表分割以输入语言模型;模型则生成由相同词表构成的序列。词表成员可基于字符编码点或字节构建。使用字符编码点能保证词表成员为有效UTF-8字符,但需数千初始成员才能覆盖足够输入;而基于字节的方案仅需256个初始成员即可避免未登录词错误,但词表成员及生成序列未必是合法UTF-8。非法序列会破坏依赖合法UTF-8输入的代码。本文利用幺半群理论形式化分词过程,证明:若词表包含非法UTF-8字节,则分词器必然生成非法序列。我们形式化证明了逐段还原为字符串并解析为UTF-8,与一次性还原结果不一致。该结论预测真实世界中的漏洞:我们评估了多种缓解方案,并对主流基础模型、推理引擎及受限生成系统进行了案例研究。
原文摘要 · Abstract (English)
Subword tokenization segments input text according to a pre-defined vocabulary to feed it into a language model; the language model, in turn, generates a sequence made from this same vocabulary. The members of the vocabulary can be built of code points or bytes. Using code points means that all members of the vocabulary are valid UTF-8 characters. However, it also requires thousands of initial members to achieve acceptable coverage of inputs. Beginning with bytes, on the contrary, avoids out-of-vocabulary errors with only 256 initial members of the vocabulary, but the members of the vocabulary and sequences of them are not guaranteed to be valid UTF-8. Sequences that are not valid UTF-8 break code that assumes its input to be valid UTF-8. Applications of language models must account for the breakage thereby introduced. In this paper, we formalize tokenization using monoid theory and prove that tokenizers whose vocabularies contain tokens that are ill-formed UTF-8 can always produce sequences that are ill-formed UTF-8. We demonstrate formally that attempting to incrementally convert tokens back to a string and interpret the results as UTF-8 gives different results than converting the whole sequence of tokens at once. This formal result predicts real-world bugs: we evaluate mitigations for the problem identified and provide case studies of major foundation models, serving engines, and constrained generation systems.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。