Repository navigation
Fix Chinese/Japanese output in LLMLingua-2: spurious spaces (mBERT) and empty prompts (XLM-R) - #266
Cis Labs (cislabs-hub) wants to merge 2 commits into
Conversation
…oft#131) LLMLingua-2 builds the compressed text with tokenizer.convert_tokens_to_string(keep_words), which joins words with a space. The mBERT tokenizer splits every CJK ideograph into its own word, so Chinese and Japanese outputs became "東 京 会 議". The chunk strings are also rebuilt with convert_tokens_to_string, so the original spacing is gone by the time the output is built, and the rate=1 / use_token_level_filter=False path returned the same spaced text. With XLM-RoBERTa, text after a forced token such as "\n" gets a "▁" prefix, which became a space ("\n 新闻"). Record, from the tokenizer offset mapping of the original context, whether each token is preceded by whitespace, and when joining kept words do not insert a space if the original had no whitespace between them (including any dropped words in between) and one side of the junction is a character of a script written without spaces (CJK ideographs, kana, CJK/fullwidth punctuation, Thai, Lao, Myanmar, Khmer). Hangul is excluded because Korean uses spaces. If no word is merged, or offsets are unavailable (slow tokenizers), the output is exactly what it was before, so space-delimited languages are unchanged. Word labels (return_word_label) are unchanged. Update the LongBench expectation (21 spurious spaces removed) and add tests for the word grouping and an end-to-end mBERT test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the XLM-RoBERTa models, a word starts only at a token with the
SentencePiece "▁" marker (or ASCII punctuation / a force token). "▁"
only follows whitespace, so a Chinese or Japanese sentence becomes one
or two "words". Compression then keeps or drops whole sentences: a
prompt that is a single word gets threshold == its probability and is
dropped by the strict `word_prob > threshold` test, so the compressed
prompt is empty, and a Chinese context followed by an English question
loses the whole context.
Start a new word at token boundaries next to characters of scripts
written without spaces, except right after a bare "▁" token
(is_begin_of_new_word gets an optional prev_token, passed by
__merge_token_to_word and __get_word_spaces so word boundaries and
spacing flags stay aligned). In merge_words_without_space, keep a
dropped gap of such characters from fusing the neighbouring kept words
of "▁"-marked models: add "▁" if the gap had whitespace, or if two
letters/digits would be joined ("2024" + "3" when "年" is dropped).
The "▁"-model check is shared (is_sentencepiece_model) and covers the
same model names as is_begin_of_new_word. Without offsets, "▁" is used
as the whitespace flag for these models.
Text without such characters gets the same words, labels and output as
before. Regenerate the LongBench (Chinese) expectation and add tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I've added a second commit (efdaeac) that fixes a related Chinese/Japanese bug in the XLM-RoBERTa models, which return an empty compressed prompt for text such as |
|
Cis Labs (@cislabs-hub) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Fixes #131
This PR fixes two bugs in how LLMLingua-2 handles text in scripts written without spaces (Chinese, Japanese and similar), one per commit:
Output for text without such characters is unchanged.
1. Spurious spaces (mBERT)
The extra spaces also cost tokens. With
rate=0.5, the mBERT model returned 67.5% of the original Chinese tokens. Withrate=1.0, the output was 1.5x longer than the input.Root cause (
prompt_compressor.pyonmain):convert_tokens_to_string(keep_words)(L2463). That call joins words with a space, and the mBERT tokenizer makes every CJK ideograph its own word.convert_tokens_to_string(L2272, L2281), so the original spacing is already lost at that point. The no-compression path returns these strings as they are (L2365)."\n"gets a▁prefix that becomes a space (\n 新闻).Fix:
__chunk_contextrecords, from the fast tokenizer's offset mapping on the original text, whether each token follows whitespace.merge_words_without_space), no space is inserted if both of these hold:2. Empty prompts (XLM-RoBERTa)
When a Chinese context is followed by an English question, the whole context is dropped and only the question is kept.
Root cause:
is_begin_of_new_word(utils.pyL90–99) starts a word only at a▁-marked token, an ASCII punctuation token, or a force token.▁only follows whitespace, so a whole Chinese/Japanese sentence becomes one "word".prompt_compressor.pyL2440) equals that word's probability. The strictword_prob > thresholdtest (L2448) then drops it.Fix:
is_begin_of_new_wordgets an optionalprev_token. Next to characters of these scripts, every SentencePiece piece starts a word (東京,会議, …), except right after a bare▁token.__merge_token_to_wordand__get_word_spacesboth passprev_token, so the spacing flags stay aligned with the words.▁-model words, a▁is added in two cases:年from2024年3月would produce20243.▁-model check covers the same model names asis_begin_of_new_word.Before / after
mainrate=0.5東 京 都 議 会 は2024 年 度 予 算 案 …(79 tok, 77.5%)東京都議会は2024年度予算案審議活用化推進論\n予算3. 5 %増子育て支援重点(49, 48.0%)rate=0.6)前 有 可 爱 的 小 姑 娘 …(169 tok, 84.5%)前有可爱的小姑娘谁喜欢她奶奶要什么 奶奶送给…(119, 59.5%)rate=1.0''会議来年度予算教育医療支出を増やす意見大阪新しい空港建設計画発表(43)''明年公共交通预算提高十二,市民投诉郊公交车拥挤(27)\n Question how much budget increase?(7)决定明年公共交通预算提高十二,市民投诉公交车拥挤\n Question budget increase?(33)In the mBERT Chinese example, the space after
什么is correct: the original has。 一次there.Space-delimited languages are unchanged
I compared
mainand this branch on four models:Prompts covered English, German, Russian, Arabic, Hindi, Korean, Thai, Chinese and Japanese, plus multi-chunk prompts. Each was run with six configurations, all with
return_word_label=True:rate0.5 / 0.33 / 1.0drop_consecutive=Trueforce_reserve_digit=Trueuse_context_level_filter=TrueResults (364 runs):
compressed_prompt,fn_labeled_original_prompt, token counts, rate).fn_labeled_original_promptis identical in every language (192/192).compressed_promptis identical in 22/24 runs. The two differences are Hanja atrate=1.0with mBERT:( 會 議 )→(會議).main, all Chinese/Japanese with XLM-R; 0 with this PR.tests/test_llmlingua2.py: the English, GSM8K and MeetingBank expectations are byte-identical.2007 10 21至24日).compressed_tokensgoes from 870 to 917 (target 1,000).Tests
tests/test_llmlingua2_spacing.py:merge_words_without_space.rate=1.0returns zh/ja unchanged, and the English output equalsmain.tests/test_llmlingua2_xlmr_cjk.py:main.mainand pass here.pytest tests/test_llmlingua2.py tests/test_llmlingua2_spacing.py tests/test_llmlingua2_xlmr_cjk.py: 19 passed on CPU (transformers 4.46.3, torch 2.5.1).Notes
convert_tokens_to_string(GPT - 4,\n Question) is unchanged on purpose, to keep space-delimited output identical.▁models, the▁marker is used instead of offsets.keep_str. That also removes spaces present in the original text, such as Korean word spacing (서울시는 예산안을→서울시는예산안을). It does not cover therate=1or XLM-R cases.🤖 Generated with Claude Code