Transformer configurations & tokenization
Overview
Mohit Iyyer compares transformer attention masks, explaining how prefix-LM, sliding-window, and document-packing masks change context access, training behavior, and inference cost. He then motivates byte-pair encoding (BPE) as a data-driven alternative to word- or character-level tokenization, showing how frequent adjacent pairs become vocabulary merges and how language imbalance affects token lengths.
Key takeaways
- Prefix-LM masks give input tokens bidirectional context and output tokens causal context, but switching a causally pre-trained model to this setup can create a harmful distribution shift.
- In multi-turn conversations, prefix-LM attention can require recomputing earlier KV representations when prior outputs become bidirectional context; causal attention instead allows the cache to grow incrementally.
- Sliding-window attention reduces attention cost by limiting each token to nearby positions, but distant information must pass through more layers; hybrid models use occasional global-attention layers to mitigate that bottleneck.
- Document-packing masks save padding computation by concatenating sequences while enforcing hard attention boundaries, so representations cannot exchange information across packed documents.
- BPE creates subword coverage by repeatedly merging frequent adjacent pairs, balancing compact sequences against the unknown-token problems of word-level vocabularies.
- BPE’s corpus-frequency dependence can disadvantage lower-resource languages: fewer merges mean more tokens per sentence and potentially slower generation.
Chapters
- The lecture continues transformer configurations before moving to tokenization.
- The project proposal deadline is extended to Friday while the autograder is brought online.
- Homework covering pre-training and post-training is planned for release after the exam; Wednesday class and office hours are canceled.
- Decoder-only language models use a lower-triangular causal mask so each token attends only to prior tokens and itself.
- Encoder models use unmasked, bidirectional attention across the input.
- Encoder–decoder models combine a bidirectional encoder with a causal decoder connected through cross-attention.
- A prefix-LM represents an input such as a French sentence and its English translation as one concatenated sequence.
- Input tokens receive bidirectional attention, while generated output tokens use causal attention.
- The loss is applied to the response tokens, not the input tokens.
- With a prefix-LM mask, each French input token can incorporate information from the entire sentence, including later words.
- That full context can produce richer input representations for translation than a causal mask, which hides later French tokens from earlier ones.
- Causal masking remains necessary over English output tokens to prevent access to future generated words.
- During next-token pre-training, causal attention supports a prediction loss at every sequence position.
- Bidirectional input positions cannot serve the same next-token objective without exposing the target, reducing useful training predictions.
- Switching a causally pre-trained model to a prefix-LM mask at fine-tuning or inference creates a distribution shift that can outweigh bidirectional-context benefits.
- Causal attention supports an incremental KV cache: new conversation tokens can be appended without recomputing earlier representations.
- Under a prefix-LM mask, prior assistant responses become part of the next turn's bidirectional context, requiring earlier representations to be recomputed.
- Prefix-LM masks remain useful when context is fixed, such as bidirectional image-token attention paired with causal text generation.
- Causal attention has quadratic sequence-length cost because each query can attend to all preceding keys.
- Sliding-window, or local, attention restricts each token to a fixed number of nearby positions, reducing attention computation.
- A narrow window can miss long-distance dependencies, so local attention is often combined with occasional global-attention layers.
- Variable-length documents in a batch often require padding to a shared length, and padded positions waste GPU computation.
- Document packing concatenates multiple short documents into one sequence and uses a block mask to keep their attention separate.
- Unlike overlapping sliding windows, the block mask creates hard boundaries that prevent information from flowing between documents.
- With a window of W tokens, each layer can extend a representation’s receptive field by about W minus one additional positions.
- After L layers, the effective context grows roughly linearly with depth, illustrated with a two-token window across four layers.
- Very deep local-only models force distant information through many layers, resembling recurrent-network information-propagation bottlenecks.
- A packed-document block mask prevents a token such as “Claude” from receiving information from a separate document, even as more layers are added.
- The effective window resets at each document boundary because the mask has no overlap between blocks.
- Training, fine-tuning, and inference should use matching mask configurations because models learn the context available at each layer.
- Whitespace tokenization can map words to vocabulary IDs, but an out-of-vocabulary term such as “LLM” may collapse to an information-losing unknown token.
- A useful multilingual tokenizer aims for shared vocabulary across languages, few unknown tokens, and short token sequences.
- Shorter sequences reduce the number of predictions and attention computation; self-attention cost grows quadratically with sequence length.
- Splitting on spaces and punctuation mishandles cases such as “O’Neal’s” and can turn an unfamiliar term like “GPT8” into an unknown token.
- Character tokenization can represent unfamiliar words when its character inventory covers them, but produces sequences roughly four to six times longer than word-level tokenization.
- Subword tokenization offers a middle ground: frequent words can remain whole while rare words break into smaller pieces, often with byte-level coverage for Unicode text.
- Byte-pair encoding starts with characters as the vocabulary, pre-tokenizes a corpus, and counts adjacent token pairs.
- In the toy corpus, “hug” appears 10 times, “pug” 5, “pun” 12, “bun” 4, and “hugs” 5; the most frequent pair is merged into a new token.
- The tokenizer retokenizes after each merge and repeats until the designer’s target vocabulary size, such as 32,000 or 64,000, is reached.
- Because BPE merges depend on corpus frequency, high-resource languages can receive many whole-word or subword merges while lower-resource languages remain closer to character-level tokenization.
- Less efficient tokenization can make lower-resource-language outputs longer and slower to generate.
- The vocabulary and its embedding rows are finalized after the target number of merges; model training then learns embeddings for those token types.
Transcript excerpt
0:15 Okay, let's get started everyone. So today um we are going to continue where we left off yesterday talking about the uh transformer configurations. Um, we're going to go into a little more detail about different attention masks and what kind of impact they have on the model. Um, and then for the second half of class, we will talk about tokenization, which is the way in which we break up an input to the model into uh tokens that it eventually gets trained on. Um before that, uh note that we extended the project proposal deadline to Friday because we were um a little slacking off on the autograder.
1:05 It should all be working now. Um so please try it out with your proposals and um remember if you pass the AI detection check, you are free to um submit and we will um grade your proposal. uh other things um your first homework we're still uh finishing that up and we will release it after the exam. So it'll cover both uh both some pre-training and post-training related um concepts. So it'll be a good um practice for the second exam where we might might ask some more um implementation related questions. Uh okay. Oh and uh I will not be here on Wednesday. So, we have no class on
1:51 Wednesday. Um, and my office hours will be cancelled as well, but um you can look out for the uh I think one of the TAs will have their office hours tomorrow if you have any last minute questions about your proposal. Okay, any questions about logistics? All right. So let us uh get started then with our um continuing discussion of transformer configurations. [snorts] So so far we've seen the um kind of decoder um config which is the standard uh
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Mohit Iyyer.