Key Moments

Stanford CS229 Machine Learning | Spring 2026 | Lecture 16: Basic Concept in RL, Policy Gradient

Stanford OnlineStanford Online
Education5 min read74 min video
Jul 31, 2026|428 views|22|1
Save to Pod

Want to know something specific about what's covered?

We've already dissected every moment. Ask and we will deliver (with timestamps).

TL;DR

Transformer attention mechanisms face memory limitations due to the KV cache, leading to Grouped-Query Attention (GQA) which shares keys/values across query heads to reduce memory usage by 10-20%.

Key Insights

1

The KV cache for attention mechanisms requires significant GPU memory, limiting batch sizes and reducing computational utilization.

2

Grouped-Query Attention (GQA) reduces the number of key and value heads compared to query heads, decreasing memory footprint by sharing K/V sets across multiple query heads.

3

Sliding window attention reduces the quadratic O(T^2) computation of attention to O(T*W) by limiting attention to a window of W recent tokens, though this can lead to forgetting long-term context.

4

Mixture-of-Experts (MoE) models allow for a large number of total parameters (e.g., 30B) while activating only a subset (e.g., 3B) during inference, disentangling memory from compute.

5

In-context learning allows large language models to perform new tasks with few or zero examples provided in the prompt, without updating model parameters.

6

Instruction tuning (a form of supervised fine-tuning) trains models on datasets of task descriptions and desired outputs to improve their ability to follow instructions and generalize.

Attention's KV cache memory bottleneck and Grouped-Query Attention

The core of transformer attention relies on computing scores between queries (Q) and keys (K), then applying these scores to values (V). During inference, especially in autoregressive models, all keys and values from previous steps must be stored in a 'KV cache' to maintain context. This cache grows linearly with sequence length (T) and head dimension, leading to substantial memory requirements on GPUs. This memory constraint limits the batch size, meaning fewer sequences can be processed in parallel, which in turn underutilizes the GPU's computational power, a phenomenon known as being memory-bound. To address this, Grouped-Query Attention (GQA) was introduced. GQA reduces the number of key and value heads compared to query heads. Instead of each query head having its own set of keys and values, multiple query heads share a single set of keys and values. This significantly reduces the size of the KV cache, typically by 10-20%, while maintaining performance. For instance, if there are 100 query heads and only 8 key/value heads, the memory usage is drastically reduced.

Sliding window attention for computational efficiency

While GQA tackles memory, the quadratic O(T^2) computational complexity of self-attention is another major challenge, especially for very long sequences (e.g., T = 1 million). Sliding window attention offers a solution by restricting each query to attend only to a fixed-size window (W) of preceding tokens. This reduces the computation from O(T^2) to O(T*W), making it linear with sequence length. However, a key limitation of this approach is that attention is localized to recent context, potentially leading to a loss of information from the distant past. While multiple layers can extend the effective receptive field, there's still a theoretical limit to how far back the model can 'see' based on the number of layers and window size (L*W). This method prioritizes computational speed over long-range dependency retention.

Mixture-of-Experts (MoE) for parameter scaling

Mixture-of-Experts (MoE) models offer a way to scale the number of parameters significantly without proportionally increasing computational cost during inference. Instead of a single dense MLP, an MoE layer consists of multiple 'expert' networks and a routing mechanism. For each token, a router determines which subset of experts (e.g., 2 out of 128) should process it. This allows models to have a vast number of total parameters (e.g., 30 billion) while activating only a fraction (e.g., 3 billion) for any given computation. This disentangles model size (memory) from compute cost. The intuition is that different experts can specialize in different types of data or tasks (e.g., math, coding, general language). While specialization isn't explicitly trained, it can emerge naturally as the most efficient way to minimize the loss function. The routing is often handled by a small gating network that projects the input to a vector, and the top-K scoring experts are selected and their outputs are weighted and combined.

In-context learning and zero-shot capabilities

A remarkable finding with large language models is their ability to perform new tasks with minimal or no task-specific training, a phenomenon known as in-context learning. In few-shot learning, a few examples of the desired task (e.g., input-output pairs) are provided directly in the prompt. The model learns from these examples 'on the fly' without any parameter updates. Zero-shot learning takes this further by providing only a description of the task, without any examples. The model infers the task from the description and generates the output. This capability has revolutionized how LLMs are applied, moving from deploying specialized models per task/company to using a single, general-purpose model customized via prompts. The examples in the prompt can range from simple arithmetic problems to complex classification tasks, demonstrating the model's ability to generalize and infer definitions or patterns from context.

Instruction tuning to enhance model controllability

While in-context and zero-shot learning are powerful, their performance can be further enhanced through instruction tuning. This is a form of supervised fine-tuning where the model is trained on a dataset of explicit instructions (tasks) and their corresponding desired outputs. By continuing to train a pre-trained model on these instruction-answer pairs, the model becomes better at understanding and following instructions. This process reinforces the model's ability to generalize and perform a wide range of tasks as specified by the user. The training objective is typically to minimize the negative log-likelihood of generating the correct output sequence given the instruction. This method is crucial for making LLMs more reliable and aligned with user intentions.

Normalization and practical considerations

During attention calculations, a normalization constant (often sqrt(DH), where DH is the head dimension) is used to scale the inner products between queries and keys before applying the softmax. This heuristic helps stabilize training and maintain consistent performance across different model dimensions. Empirically, this scaling factor prevents the inner products from becoming too large or too small. Additionally, techniques like RMSNorm are sometimes applied to queries and keys before the inner product to further ensure normalization. For MoE models, challenges include ensuring uniform expert utilization during training to avoid wasting parameters and optimizing GPU allocation. Dynamic provisioning of GPUs based on usage patterns or language context is one advanced strategy.

Common Questions

Queries (Q), Keys (K), and Values (V) are core components of the attention mechanism in transformers. Queries represent the current information being processed, Keys represent what information is available, and Values hold the actual content associated with the Keys. Attention calculates the relevance of Queries to Keys and uses this to weight the Values.

Topics

Mentioned in this video

More from Stanford Online

View all 106 summaries

Ask anything from this episode.

Save it, chat with it, and connect it to Claude or ChatGPT. Get cited answers from the actual content — and build your own knowledge base of every podcast and video you care about.

Get Started Free