ElemedexA pocket encyclopedia of AI architectures

Glossary (TM) › Language and language models

TM068

Special tokens and chat templates

Also: control tokens, prompt template

In Spanish: Tokens especiales y plantillas de chat

Vocabulary entries that represent functions rather than text: marking beginnings and ends, separating texts, hiding words, or signaling who is speaking in a conversation.

A special token is a vocabulary entry that corresponds to no piece of text but to a function: marking where a sequence begins or ends, separating two texts, padding empty positions, hiding a word, or indicating who is speaking (Alammar & Grootendorst, 2024, p. 47). The tokenizer of BERT has five: [CLS], [SEP], [PAD], [UNK] (for whatever it cannot encode) and [MASK] (2024, pp. 47–48); that of GPT-2 has only one: <|endoftext|> (2024, p. 49).

Tiny example. To decide whether one sentence entails another, BERT receives

[CLS] the dog is asleep [SEP] the animal is resting [SEP]

[SEP] separates the sentences (together with an embedding marking which one each token belongs to), and the final vector of [CLS] summarizes the whole pair for classification (Devlin et al., 2019, pp. 4174–4175). [MASK] raises a curious problem: it appears in pretraining but never in fine-tuning, which is why BERT uses it in only 80% of the positions chosen for hiding (2019, p. 4174) (see masked language modeling).

The assistants of 2023 brought turn tokens: <|user|>, <|assistant|>, <|system|> (Alammar & Grootendorst, 2024, p. 54). Phi-3's chat template wraps each exchange as <s><|user|> … <|end|><|assistant|>. It is the format the model was trained on, and it tells the model who said what and when to stop: for the model, ending a turn means predicting <|end|> (2024, p. 169).

These tokens have no lexical content; their vectors mean whatever training makes them mean. Without fine-tuning, the [CLS] vector is a poor summary of the sentence, worse than averaging GloVe vectors (Reimers & Gurevych, 2019, p. 3982): an extreme case of meaning as functional role, not unlike the auxiliary symbols of a formal language. And the template reveals something uncomfortable: in a chat, "who is speaking" is not an external fact but one more token in the same sequence as everything else. The boundary between the operator's instructions and the user's text is a learned convention, not a barrier (see system prompt and jailbreak).

Sources

  • Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
  • Devlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of deep bidirectional transformers for language understanding. In J. Burstein, C. Doran, & T. Solorio (Eds.), Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers) (pp. 4171–4186). Association for Computational Linguistics. https://doi.org/10.18653/v1/N19-1423 §
  • Reimers, N., & Gurevych, I. (2019). Sentence-BERT: Sentence embeddings using Siamese BERT-networks. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP) (pp. 3982–3992). Association for Computational Linguistics. https://doi.org/10.18653/v1/D19-1410 §