GitHub - workdd/LLM_Foreign_Block: LLM 모델의 외국어 토큰 생성을 막는 코드 구현
Service

GitHub - workdd/LLM_Foreign_Block: LLM 모델의 외국어 토큰 생성을 막는 코드 구현

workdd
2025.03.22
·GitHub·by Anonymous
#LLM#Token Generation#Logit Processing#Language Model

Key Points

  • 1This repository introduces a method to prevent Large Language Models (LLMs) from generating specific foreign language tokens by adjusting logit values during inference.
  • 2The approach identifies tokens corresponding to languages like Chinese, Japanese, and Russian based on their Unicode ranges and then sets their generation probabilities to negative infinity.
  • 3Implementations are provided for Transformers and vLLM, with a noted performance impact of significantly slowing down the first token generation (TTFT), suggesting a warm-up phase is beneficial.

This paper presents a method and implementation to prevent Large Language Models (LLMs) from generating tokens corresponding to specific foreign languages by adjusting their logit values during inference. The core objective is to restrict LLM output to desired languages, specifically demonstrating the exclusion of Chinese, Japanese, and Russian.

The methodology involves three main steps:

  1. Tokenization: The input text is tokenized using the model's tokenizer to obtain token IDs.
  2. Foreign Token Identification: Specific Unicode ranges are defined for the target foreign languages. For instance:
    • Chinese (CJK Unified Ideographs and extensions): [0x4E00,0x9FFF][0x4E00, 0x9FFF], [0x3400,0x4DBF][0x3400, 0x4DBF], [0x20000,0x2A6DF][0x20000, 0x2A6DF], [0xF900,0xFAFF][0xF900, 0xFAFF].
    • Japanese (Hiragana, Katakana, Extensions): [0x3040,0x309F][0x3040, 0x309F], [0x30A0,0x30FF][0x30A0, 0x30FF], [0x31F0,0x31FF][0x31F0, 0x31FF].
    • Russian (Cyrillic): [0x0400,0x04FF][0x0400, 0x04FF], [0x0500,0x052F][0x0500, 0x052F].
Tokens whose corresponding Unicode characters fall within these defined ranges are identified as foreign.
  1. Logit Processing: For each identified foreign token, its raw prediction score (logit) is set to negative infinity (-\infty).
In the context of a softmax function, which converts logits (lil_i) into probabilities (pip_i) via the formula:
pi=elij=1Neljp_i = \frac{e^{l_i}}{\sum_{j=1}^N e^{l_j}}
setting lk=l_k = -\infty for a foreign token kk causes elke^{l_k} to become e=0e^{-\infty} = 0. This effectively makes the probability pk=0p_k = 0, ensuring that the model will not select or generate that specific foreign token. This logit manipulation is typically implemented as a custom LogitProcessor within the LLM inference pipeline.

The implementation includes:

  • blocker_torch.py: A Torch Tensor-based implementation for identifying and blocking foreign tokens based on the defined Unicode ranges.
  • transformers_logit_processed.py: Demonstrates the application of this LogitProcessor within the Hugging Face Transformers library inference framework.
  • vllm_logit_processed.py: Shows integration with the vLLM serving framework for optimized inference.

Performance analysis indicates that the LogitProcessor introduces a noticeable overhead. Specifically, for a Qwen2.5-7B-Instruct model, the Time To First Token (TTFT) significantly increases for the *initial* generate call (e.g., from ~100ms to ~1500ms). This is attributed to the one-time processing of all potential tokens to build the foreign token mask. Subsequent token generations or generate calls do not incur this initial overhead, maintaining high tokens per second (TPS). The paper suggests a "warm-up" phase is advisable when deploying this solution to mitigate the initial TTFT latency.

Experimental results demonstrate the effectiveness of the approach. When prompted with requests to generate Chinese text, an LLM with the LogitProcessor enabled instead produces responses in Korean or explains its inability to generate Chinese. Conversely, without the LogitProcessor, the LLM readily generates Chinese text.