Multilingual Tokenization and Vocabulary Allocation › Shared and exclusive tokens

Michael BrenndoerferNovember 25, 20264 min read
Language AI HandbookAll chapters
Part 26 of 67Multilingual Language Models and Cross-Lingual Transfer
Chapter 184Multilingual Tokenization and Vocabulary Allocation › Shared and exclusive tokens
Reading section 7 of 11 · Shared and exclusive tokens · 4 minSection 7 of 11 · 4 min

Section 7 of 11 · 4 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

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]
CollapseExpand
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]
CollapseExpand
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                            33

The 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]
CollapseExpand
English-only token overlap heatmap has ones on the diagonal and several off-diagonal cells with shared IDs.
Pairwise Jaccard overlap of held-out token ID sets under English-only training. Darker cells indicate higher overlap, including overlap in raw byte pieces.

The second heatmap uses the same language order and definition for the mixed vocabulary.

VisualizationOut [28]
CollapseExpand
Mixed-vocabulary token overlap shows lower Hindi–Thai overlap and higher English–Spanish overlap than in the English-only heatmap.
Pairwise Jaccard overlap under equal-row mixed training. Some pairs share fewer IDs as learned pieces replace raw bytes, while others share more.

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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy