UUtah CS 6340 NLP | Fall 2026 | Transformers: Part 2/2
Overview
The lecture completes the original 2017 Transformer architecture, explaining positional information, encoder and decoder blocks, causal masking, cross-attention, and next-token prediction. It contrasts learned and sinusoidal position embeddings with modern RoPE, explains why long context is constrained by quadratic attention memory and imperfect length generalization, and connects the encoder-decoder design to today’s common decoder-only LLMs.
Key takeaways
- Learned absolute position embeddings require a preset maximum length and add a position vector to every token embedding, while RoPE injects positional information by rotating queries and keys inside attention.
- RoPE’s ability to compute positions beyond training length does not ensure reliable long-context performance, and standard attention’s quadratic memory cost forces a tradeoff between training length and available GPU memory.
- Teacher forcing shifts the known target sequence by one position, and causal masking sets future-position attention scores to negative infinity before softmax so all next-token losses can be computed in one forward pass.
- An encoder-decoder Transformer uses causal self-attention for target history and unmasked cross-attention, with decoder queries attending to encoder keys and values, to incorporate source information.
- A Transformer decoder’s language-modeling head maps token representations to vocabulary logits, and training minimizes negative log likelihood for the ground-truth next token.
- Decoder-only LLMs became the common architecture because causal attention can process prompts and generate continuations in one framework, while encoder-only and encoder-decoder models remain useful for classification and sequence-to-sequence tasks.
Chapters
- The second practice assignment is for a practicum scheduled for the Monday after fall break, with autograder issues to be fixed after class.
- The next Monday includes an in-person quiz, while the lecture will be shared as a recording because the instructor is attending a language-modeling conference.
- Wednesday’s tutorial covers fine-tuning with the Hugging Face Transformers library and running GPU jobs on the University of Utah Center for High Performance Computing, or CHPC.
- Students are asked to create a CHPC account before the tutorial and post questions on Piazza.
- The original 2017 Transformer, from the paper “Attention Is All You Need,” converts each input token to a vocabulary ID and looks up its embedding.
- The lecture contrasts the original model’s embedding dimensions, such as 512, with contemporary open-weight models using dimensions around 4,000 or 7,000.
- Self-attention contextualizes each token representation, but attention alone does not tell the model whether a token appeared first, second, or later in the sequence.
- A learned position-embedding table assigns a vector with the same dimension as a token embedding to every position up to a preset maximum sequence length.
- For each input position, the model adds its position vector to the corresponding token embedding.
- A fixed table adds parameters, requires a maximum length to be chosen before training, and has no learned embedding for positions beyond that limit.
- Adding position vectors directly to token embeddings mixes positional and semantic information.
- The original Transformer uses fixed sine and cosine functions to fill even and odd dimensions of each position vector, avoiding a learned position table.
- Different frequencies produce periodic patterns intended to help represent relative distances between positions.
- Sine and cosine values remain between minus one and one, and the encoding introduces no additional learned position parameters.
- Sinusoidal encodings are still added to token embeddings, so they retain the drawback of mixing position and token semantics.
- Rotary Position Embeddings, or RoPE, do not add position vectors to input token embeddings; they rotate query and key representations inside self-attention.
- The rotations alter query-key dot products, allowing positional relationships to affect attention weights.
- RoPE is a core positional technique in modern LLMs, often paired with additional methods intended to improve efficiency or long-context behavior.
- RoPE can be computed at positions beyond the training range, but empirical results show that this does not guarantee good performance on longer sequences.
- Standard attention has memory costs that grow quadratically with sequence length; training at 128,000 tokens can require roughly 1,000 times the attention memory of training at 4,000 tokens.
- GPU memory limits force model builders to choose a maximum training sequence length, and performance may degrade when inference sequences exceed it.
- Context windows have grown from hundreds of tokens to hundreds of thousands or around a million, but large codebases and generated text can still exhaust them.
- Some architectures periodically omit positional encoding, creating NoPE layers alongside layers that use RoPE.
- The proposed intuition is that causal attention already reveals which tokens precede a given token, so a model may learn positional structure implicitly.
- Work including Press et al. (2022) studied alternatives such as ALiBi and NoPE; the lecture notes that large-scale evidence for NoPE remains limited.
- The key design requirement is to represent sequence order, while current LLMs commonly use RoPE with modifications intended to improve longer-context behavior.
- After attention, each token representation passes through a feed-forward transformation: a linear layer, a nonlinearity, and a second linear layer.
- The first linear layer typically expands the representation, such as from 512 dimensions to roughly 2,000, before projecting back to the model dimension.
- The lecture contrasts ReLU and GELU with modern gated activations such as SwiGLU.
- Every token receives its own transformed representation, preserving the sequence of token representations.
- A residual connection adds a block’s input representation to its transformed output, preserving earlier information as attention and feed-forward layers change representations.
- Layer normalization stabilizes training by normalizing each token representation; its learned parameters include scale and bias.
- RMSNorm is a simpler alternative that omits the learned bias and is common in contemporary LLMs.
- Modern Transformers often use pre-layer normalization, applying normalization before attention and feed-forward modules, with a final normalization before producing logits.
- An encoder block combines multi-head self-attention, a feed-forward network, residual connections, and normalization.
- Each block outputs a new representation for every source token, and stacking blocks produces increasingly contextualized representations.
- The encoder represents the source sequence but does not itself perform next-token generation.
- The lecture pauses before introducing the decoder, which uses the encoder’s output in encoder-decoder Transformers.
- During inference, a decoder generates tokens sequentially, using each generated token to predict the next.
- During training, teacher forcing supplies the known target sequence shifted right by one position, beginning with a special start token.
- Because the target tokens are already available as decoder input, the model can compute predictions and losses for every position in one forward pass.
- Teacher forcing makes training parallel across sequence positions, unlike step-by-step generation.
- Decoder self-attention must prevent the prediction at position i from using target tokens that occur after position i.
- The causal mask sets attention scores above the main diagonal to negative infinity before softmax, making future-token attention weights zero.
- An alternative is to zero future attention weights after softmax and renormalize, but masking logits before softmax is the recommended implementation.
- Causal masking allows a single training pass to produce next-token losses at all positions without leaking future answers.
- Encoder-decoder models need a second attention mechanism because causal attention only connects decoder positions to earlier decoder positions.
- Cross-attention uses decoder representations as queries and the encoder’s final source-token representations as keys and values.
- Each decoder position can attend to relevant source tokens without a causal mask, since the full source sequence is available.
- Source and target sequence lengths can differ, so the attention matrix relates the target positions to the source positions.
- An encoder-decoder Transformer’s decoder block applies causal self-attention, cross-attention to encoder outputs, and a feed-forward transformation.
- Residual connections and normalization accompany these operations, with the exact ordering depending on choices such as pre-layer or post-layer normalization.
- Stacked decoder blocks repeatedly update each target-token representation before the model predicts output tokens.
- The decoder’s causal attention is required in every block to prevent access to future target tokens during teacher-forced training.
- A linear language-modeling head projects each decoder representation to vocabulary-sized logits, and softmax converts those logits into probabilities.
- Training minimizes negative log likelihood for the ground-truth next token, indexed by its vocabulary ID, rather than selecting the model’s most probable token.
- Weight tying reuses the token-embedding matrix for the output projection, reducing parameters in smaller models.
- Tying may be less important in large models, where the embedding and output matrices are a smaller share of total parameters.
- Encoder-only models became useful for classification tasks: token representations can be pooled and projected to class logits.
- Encoder-decoder models such as T5 support sequence-to-sequence tasks including translation.
- Decoder-only models became the prevalent LLM design, using causal attention for both processing prompts and generating continuations.
- Architecture choices continue to vary, so the rise of decoder-only models does not mean encoder-decoder models have disappeared.
Transcript excerpt
0:00 All right. Uh, good afternoon everyone. I imagine this is set up, but it's not yet. Sorry. Set this. Right. There we go. I'm going to check the Zoom just to make sure. Okay, that's fine. Okay, hopefully everything is set up properly. You don't know the amount of times I have been thinking I'm recording and then I'm not recording. This is why I'm paranoid now. Okay, we have so much to do today. So, we are going to go over some announcements and then uh continue with the transformer. Um I released I've just released the second practice assignment for your second practicum
0:48 that will happen on Monday after the full break. Um, I said I set up the autograder, but I just noticed some errors. So, after this lecture, I'm going to fix the autograder. But I suppose you won't magically solve this alone in one minute. So, you'll be you'll be fine. Um, next week we are going to do things a little bit differently because I will be at the conference of language modeling column. So, on Monday you will come here and do your quiz in person as always at the beginning of the class. But I'll actually share a recording of the lecture and you can watch it at any time or day and anywhere you want. You can use this lecture uh room to you know sit and with your headphones um you know use that time to watch the recording. Uh but
1:35 it's up to you when exactly you do that. But do show up for the quiz. So don't get confused. Uh if you don't follow what I'm saying, just show up on Monday as you would uh uh uh usually. On Wednesday, uh Giant, your TA will uh give you a tutorial on how to use the
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, UofU Data Science.