Multilingual Tokenization and Vocabulary Allocation › Shared and exclusive tokens
Language AI HandbookAll chapters How much of the vocabulary used by each held-out language is shared? Shared IDs make the same embedding entry available to several languages in a model trained with that tokenizer. Token overlap is a property of an encoding sample. It does not reveal how a trained model transfers knowledge.
Collect the set of token IDs used by each language within one tokenizer and divide the size of the intersection by the size of the union. This Jaccard overlap is zero for disjoint sets with a nonempty union and one for identical nonempty sets. The code returns zero when both sets are empty. Each distinct ID counts once, including IDs for whitespace and individual UTF-8 bytes.
PythonIn [24]Expand
def used_token_sets(tokenizer):
"""Set of token IDs each language actually uses on the sample corpus."""
return {
code: {
token_id
for sentence in sentences
for token_id in tokenizer.encode(sentence)
}
for code, sentences in test_by_language.items()
}
lang_codes = list(test_by_language)
def jaccard_matrix(tokenizer):
"""Pairwise Jaccard overlap of used token sets."""
sets = used_token_sets(tokenizer)
matrix = np.zeros((len(lang_codes), len(lang_codes)))
for i, a in enumerate(lang_codes):
for j, b in enumerate(lang_codes):
union = sets[a] | sets[b]
matrix[i, j] = len(sets[a] & sets[b]) / len(union) if union else 0.0
return matrix
overlap_english = jaccard_matrix(tok_english)
overlap_balanced = jaccard_matrix(tok_balanced)ConsoleOut [25]Expand
language tokens used used only here shared with another language
Korean 61 37 24
Thai 42 35 7
Hindi 39 32 7
Chinese 48 30 18
Arabic 32 29 3
English 43 6 37
Spanish 38 5 33
Finnish 37 5 32
Swahili 38 5 33The first heatmap shows pairwise overlap for the English-only tokenizer. Each ID refers to the same tokenizer on both axes. Raw byte pieces can be shared by code points in otherwise different scripts.
VisualizationOut [27]Expand

The second heatmap uses the same language order and definition for the mixed vocabulary.
VisualizationOut [28]Expand

The Hindi–Thai overlap declines under the mixed fit, while English–Spanish overlap rises. The examples illustrate that overlap can change in either direction. The English-only model spells much of the non-Latin held-out text with byte pieces. An ID for a continuation byte can occur in unrelated scripts, so a shared byte does not imply a shared morpheme. Common punctuation and digits can also contribute overlap without establishing transfer.
To see which tokens drive an overlap score, inspect the intersection with the tokenizer's ID-to-piece lookup. Count raw byte pieces separately if the question is about learned pieces. For the question asked here, both kinds count because either can occupy a deployed input position. For a vocabulary redesign, you could also weight by how often the tokens occur. Then a rarely used shared piece would contribute less than a shared piece that occurs in most messages. Keep both the weighting choice and the unit of input explicit when comparing overlap across designs.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!