Special Tokens › Token Type IDs and Segment Embeddings

Michael BrenndoerferApril 18, 20258 min read
Language AI HandbookAll chapters
Part 5 of 67Subword Tokenization
Chapter 34Special Tokens › Token Type IDs and Segment Embeddings
Reading section 5 of 12 · Token Type IDs and Segment Embeddings · 8 minSection 5 of 12 · 8 min

Book contents

Section 5 of 12 · 8 min read

Special Tokens › Token Type IDs and Segment EmbeddingsOne section · Sections in this chapter5 / 12 ↓
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

Beyond the tokens themselves, BERT introduces another mechanism for handling multi-segment inputs: token type IDs (also called segment IDs). Token type IDs answer a question that the token sequence alone cannot: for each position in the input, which sentence does it belong to? This question matters for tasks like natural language inference, question answering, and textual entailment, where the model needs to reason about the relationship between two distinct texts.

Token Type IDs

Integers assigned to each input token indicating which segment of the input it belongs to. In BERT, sentence A tokens receive type ID 0 and sentence B tokens receive type ID 1. These IDs index into learned segment embeddings that are added to the token embeddings at the input layer. Token type IDs allow a single model to represent multi-sentence inputs without architectural changes.

Think of token type IDs as jersey numbers in a two-team game: players wearing jersey 0 belong to team A, and players wearing jersey 1 belong to team B. The number doesn't describe what the player does, but it tells the referee (the model) which team's goals the player is working toward. Without this signal, the model has no reliable way to know which part of a two-sentence input is the premise and which is the hypothesis in an inference task.

In BERT's embedding layer, the final embedding for each token is the sum of three components. Before we write the formula, it helps to understand what each component contributes. The token embedding encodes what the token is: its linguistic identity and semantic content. The positional embedding encodes where the token is: its position in the sequence. The segment embedding encodes which sentence the token belongs to: its structural role in the multi-sentence input.

ei=eitoken+eiposition+eisegment\mathbf{e}_i = \mathbf{e}^{\text{token}}_i + \mathbf{e}^{\text{position}}_i + \mathbf{e}^{\text{segment}}_i

where:

  • eitoken\mathbf{e}^{\text{token}}_i: the token embedding for the ii-th input token, looked up from a vocabulary embedding matrix of shape [V×d][V \times d], where VV is the vocabulary size (30,522 for BERT-base) and dd is the hidden dimension (768 for BERT-base)
  • eiposition\mathbf{e}^{\text{position}}_i: the positional embedding for position ii, looked up from a position embedding matrix of shape [L×d][L \times d], where LL is the maximum sequence length (512 for BERT-base)
  • eisegment\mathbf{e}^{\text{segment}}_i: the segment embedding for token ii, looked up from a segment embedding matrix of shape [2×d][2 \times d], with row 0 for sentence A tokens and row 1 for sentence B tokens

Why does this formula make sense? Notice that all three embedding vectors have the same dimension dd, so their element-wise sum produces a single embedding vector ei\mathbf{e}_i of dimension dd. This sum encodes three orthogonal kinds of information simultaneously: what the token is, where it is, and which segment it belongs to. The model can potentially disentangle these signals across different dimensions of the embedding space. The combined embedding is then passed into the first transformer layer, carrying all three kinds of structural information into every subsequent attention computation.

The segment embedding lookup table has only two rows: one for segment A (type ID 0) and one for segment B (type ID 1). The model learns these two vectors during pre-training as part of the next-sentence prediction objective. Unlike positional embeddings (which have one vector per position up to the maximum sequence length) or token embeddings (which have one vector per vocabulary entry), segment embeddings are an extremely compact table: just two learned vectors that modulate the entire input based on segment membership.

For single-sentence inputs, all tokens receive type ID 0. The segment B embedding is irrelevant but still part of the model. For sentence-pair inputs, the type IDs switch from 0 to 1 at the second segment:

[CLS] tokens_A [SEP] tokens_B [SEP] 0 0 0 1 1

Modern models like RoBERTa dropped segment embeddings entirely. Since RoBERTa discarded next-sentence prediction during pre-training, there was no objective that rewarded the model for learning meaningful segment embeddings. Removing segment embeddings simplifies the architecture without hurting performance. Interestingly, even models that handle sentence pairs for tasks like Natural Language Inference work fine without explicit segment embeddings, relying instead on the [SEP] tokens and positional context to infer segment boundaries. This empirical finding suggests that [SEP]'s boundary signal may already be sufficient, and segment embeddings are redundant rather than necessary.

Worked Example: BERT Input Construction

Let's trace through the complete construction of a BERT input for a sentence-pair task, step by step, to make the three-component embedding concrete. Suppose the task is Natural Language Inference, and the input is:

  • Premise: "The cat sat on the mat."
  • Hypothesis: "A cat was resting."

Step 1: WordPiece tokenization. The tokenizer first splits each sentence into subword tokens. "Resting" might become ["rest", "##ing"] if "resting" is not a vocabulary entry but "rest" and the suffix "##ing" are.

Step 2: Special token insertion. The tokenizer prepends [CLS] to the entire sequence, inserts [SEP] after sentence A, appends sentence B's tokens, and adds a final [SEP]:

Token: [CLS] The cat sat on the mat . [SEP] A cat was rest ##ing . [SEP] Type ID: 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 Pos ID: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Step 3: Embedding summation. For each position ii, three vectors are looked up and summed: the token embedding for the token at position ii, the positional embedding for position ii, and the segment embedding for the type ID at position ii (either row 0 or row 1 of the segment embedding table).

Step 4: Transformer processing. The summed embeddings enter the first transformer layer. All 16 positions attend to each other (with full bidirectional attention). After all 12 layers, each position has a contextualized representation that encodes information from the entire sequence.

Step 5: Classification. The [CLS] representation at position 0 is passed through a classification head with three outputs: entailment, contradiction, neutral. The class with the highest softmax probability is the predicted label.

The segment boundary created by the [SEP] at position 8 and the jump in type IDs from 0 to 1 gives the model two complementary signals about where sentence A ends and sentence B begins. The attention mechanism can learn to use either signal: some heads may track the [SEP] boundary while others track the type ID boundary. Having both signals makes the boundary information resilient to noise in either one.

The figure below visualizes this three-layer structure: token identity, positional index, and segment membership all encoded in parallel for the same sequence.

Out[4]:
Visualization
Three-row grid showing BERT token values, position IDs, and segment type IDs across 12 positions.
BERT input structure for a sentence pair, showing token IDs, positional IDs, and token type IDs stacked for each position. Special tokens [CLS] and [SEP] appear at boundaries (shown in red), sentence A tokens are blue (type ID 0), and sentence B tokens are orange (type ID 1). The type ID switches from 0 to 1 after the first [SEP], marking the sentence A/B boundary.