Sub topic 11
Special tokens, continuation markers, and whitespace markers
This chapter assumes basic Python, dictionaries, loops, and token frequencies. You should also know that a tokenizer converts text into tokens, and that a language model receives token IDs rather than raw strings.
Subword tokenizers need more than ordinary vocabulary pieces. They also need tokens that describe sequence boundaries, padding, unknown text, masking, or how pieces relate to surrounding text. These symbols are part of the tokenizer's contract with the model.
Special tokens carry structure
A special token is a reserved vocabulary item with a meaning defined by the model or preprocessing pipeline. Common examples include:
| Token | Typical meaning | Common use |
|---|---|---|
| `[CLS]` | Classification-start marker | Put before a sequence for classification models |
| `[SEP]` | Separator or sequence-end marker | Separate two sequences |
| `[PAD]` | Padding marker | Make a batch have equal sequence lengths |
| `[UNK]` | Unknown token | Represent text the tokenizer cannot encode |
| `[MASK]` | Hidden-token marker | Train masked language models |
| `<s>`, `</s>` | Beginning and end markers | Mark sequence boundaries |
The exact spelling and behavior vary by tokenizer. A model trained with <s> cannot automatically treat [CLS] as equivalent. The tokenizer, vocabulary, model configuration, and data pipeline must agree on the token IDs.
Padding illustrates this dependency. A batch may contain sequences of different lengths, so shorter sequences receive [PAD]. The attention mask then marks real tokens with 1 and padding positions with 0. If padding is added without updating the attention mask, the model may process padding as meaningful input.
tokens = ["[CLS]", "cats", "sleep", "[SEP]", "[PAD]"]
special_ids = {"[CLS]": 101, "[SEP]": 102, "[PAD]": 0}
input_ids = [
special_ids["[CLS]"],
9001,
4120,
special_ids["[SEP]"],
special_ids["[PAD]"],
]
attention_mask = [1, 1, 1, 1, 0]
print(input_ids)
print(attention_mask)Continuation markers show piece position
WordPiece commonly uses ## to mark a piece that continues a word. For example:
unaffable -> un ##aff ##ableHere, un begins the word. The pieces ##aff and ##able must attach directly to the preceding piece, without inserting spaces.
The marker is a tokenizer convention, not necessarily text that the language model should display. During decoding, the tokenizer removes ## and joins continuation pieces to the previous piece.
BPE does not have one universal continuation marker. Some implementations store ordinary strings and use tokenization state to determine whether a piece begins a word. Others use prefixes such as Ġ to indicate that a piece follows whitespace. Always inspect the tokenizer's vocabulary and decoder rather than assuming that all BPE systems behave alike.
The SentencePiece whitespace marker
SentencePiece commonly represents a preceding space with the visible marker ▁. For example:
"Hello world" -> ["▁Hello", "▁world"]The marker lets SentencePiece train directly on raw text without first splitting it into words. It preserves where spaces occur, including the boundary before the first word. Decoding converts the markers back into spaces.
This differs from ##. The ▁ marker usually says that a token begins after whitespace. The ## prefix says that a WordPiece continues the current word.
Practical checks
Keep special-token IDs fixed when loading a trained model. Check whether the tokenizer adds beginning or ending markers automatically. Verify that encoding followed by decoding preserves spaces and punctuation. Test empty strings, multiple spaces, unknown characters, and sequences containing padding.
Exercise
Why can replacing every ## with an empty string produce incorrect decoded text?
Show answer
## describes a continuation piece, so it should be joined to the previous piece. Removing it without applying the surrounding tokenizer rules can mishandle word boundaries and special tokens.