Qwen Architecture › Tokenization Strategy
Michael BrenndoerferUpdated August 7, 20256 min read
Language AI HandbookAll chapters Part 29 of 67Modern Decoder Models
Qwen Architecture › Tokenization StrategyOne section · Sections in this chapter5 / 11 ↓
Sections in this chapter11
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
One of Qwen's most distinctive features is its tokenization approach, optimized for multilingual text with particular emphasis on Chinese.
Large Multilingual Vocabulary
Out[14]:
Visualization
Qwen uses a vocabulary of 151,936 tokens, nearly 5× larger than LLaMA's 32,000. This design decision changes the interpretation:
Advantages of larger vocabulary:
- Better Chinese representation: Many Chinese characters receive their own tokens rather than being split into byte-level pieces
- Fewer tokens per text: Chinese text requires materially fewer tokens, improving both efficiency and context utilization
- Improved multilingual coverage: Better handling of Korean and Japanese alongside other Asian languages
Trade-offs:
- Larger embedding matrices: Increases model size by approximately 0.5B parameters for Qwen-7B
- Sparser token distributions: Each token appears less frequently during training
- Memory overhead: Larger vocabulary requires more memory for softmax computation
In[15]:
Code# Simulated tokenization comparison
# (Actual tokenization would require the Qwen tokenizer)
def estimate_tokens_chinese(text: str, vocab_type: str) -> int:
"""Estimate token count for Chinese text"""
char_count = len(text)
if vocab_type == "llama":
# LLaMA often splits Chinese chars into 2-3 tokens
return int(char_count * 2.5)
elif vocab_type == "qwen":
# Qwen has dedicated Chinese character tokens
return int(char_count * 1.2)
return char_count
def estimate_tokens_english(text: str, vocab_type: str) -> int:
"""Estimate token count for English text"""
word_count = len(text.split())
# Both handle English similarly
if vocab_type in ["llama", "qwen"]:
return int(word_count * 1.3)
return word_count
# Example texts
chinese_text = "人工智能正在改变我们的生活方式和工作模式"
english_text = "Artificial intelligence is transforming how we live and work"
# Estimate token counts
results = {
"Chinese": {
"LLaMA": estimate_tokens_chinese(chinese_text, "llama"),
"Qwen": estimate_tokens_chinese(chinese_text, "qwen"),
},
"English": {
"LLaMA": estimate_tokens_english(english_text, "llama"),
"Qwen": estimate_tokens_english(english_text, "qwen"),
},
}Out[16]:
ConsoleTokenization Efficiency Comparison (Estimated) ======================================================= Chinese text: 人工智能正在改变我们的生活方式和工作模式 Characters: 20 LLaMA tokens: ~50 Qwen tokens: ~24 Efficiency gain: 2.1x fewer tokens English text: Artificial intelligence is transforming how we live and work Words: 9 LLaMA tokens: ~11 Qwen tokens: ~11 Key insight: Qwen's vocabulary is optimized for Chinese, using ~2x fewer tokens for Chinese text while maintaining similar efficiency for English.
Out[17]:
Visualization
Byte-Level BPE with Special Handling
Qwen uses Byte-Pair Encoding (BPE) at the byte level, similar to GPT-4, but with additional handling for CJK (Chinese, Japanese, Korean) characters:
Out[18]:
Visualization
The tokenization strategy ensures:
- No unknown tokens: Byte-level fallback handles any input
- Efficient CJK: Common characters get dedicated tokens
- Balanced encoding: Similar information density across languages