Lecture 10: The Machinery Beyond Vanilla Transformers

Effective AI use in applied machine-learning projects
Outline
A. Efficiency
B. REINFORCE
C. PPO, GRPO, RLVR
D. Prompting and search at inference
attention mechanism
a head maps the sequence of \(x\)'s to a sequence of context-aware \(z\)'s, in two steps:
1. learned (query, key, value) projection
2. attention mechanism
Recall from 6.390:
attention mechanism
To
date
the
cleverest
softmax
To
date
the
cleverest
dot-product magnitude tend to grow with \(d_k\); without the scaling they saturate the softmax, and gradients get tiny
e.g., softmax\((1, 2, 3, 4) \approx (0.03, 0.09, 0.24, 0.64)\), but softmax\((10, 20, 30, 40) \approx (0, 0, 0, 1)\)
softmax
To
date
the
cleverest
softmax'd attention scores: mixing percentages that sum to 1 (100%)
\(a_{12}\): how much \(x_1\) attends to \(x_2\) when updating \(x_1\)'s representation
softmax
To
date
the
cleverest
\(\in \mathbb{R}^{d_k}\)
dot-product similarity scores
causal mask \(M\)
masked scores
causal attention
matrix \(A\)
\(Z = AV\), same as before
each \(z_i\) now mixes only \(v_1, \dots, v_i\)
Each new token recomputes every earlier row of K and V
3 tokens
\[ K = X W_k = \begin{bmatrix} k_1^\top \\ k_2^\top \\ k_3^\top \end{bmatrix} \qquad V = X W_v = \begin{bmatrix} v_1^\top \\ v_2^\top \\ v_3^\top \end{bmatrix} \qquad \text{masked scores} = \begin{bmatrix} q_1^\top k_1 & -\infty & -\infty \\ q_2^\top k_1 & q_2^\top k_2 & -\infty \\ q_3^\top k_1 & q_3^\top k_2 & q_3^\top k_3 \end{bmatrix} \]
4 tokens
\[ K = X W_k = \begin{bmatrix} {\color{#cc0000}{k_1^\top}} \\ {\color{#cc0000}{k_2^\top}} \\ {\color{#cc0000}{k_3^\top}} \\ k_4^\top \end{bmatrix} \qquad V = X W_v = \begin{bmatrix} {\color{#cc0000}{v_1^\top}} \\ {\color{#cc0000}{v_2^\top}} \\ {\color{#cc0000}{v_3^\top}} \\ v_4^\top \end{bmatrix} \qquad \text{masked scores} = \begin{bmatrix} {\color{#cc0000}{q_1^\top k_1}} & {\color{#cc0000}{-\infty}} & {\color{#cc0000}{-\infty}} & {\color{#cc0000}{-\infty}} \\ {\color{#cc0000}{q_2^\top k_1}} & {\color{#cc0000}{q_2^\top k_2}} & {\color{#cc0000}{-\infty}} & {\color{#cc0000}{-\infty}} \\ {\color{#cc0000}{q_3^\top k_1}} & {\color{#cc0000}{q_3^\top k_2}} & {\color{#cc0000}{q_3^\top k_3}} & {\color{#cc0000}{-\infty}} \\ q_4^\top k_1 & q_4^\top k_2 & q_4^\top k_3 & q_4^\top k_4 \end{bmatrix} \]
The red rows repeat the step before.
Because of the mask, row \(i\) depends only on tokens 1 to \(i\).
Step \(t\) rebuilds a \(t \times t\) score matrix, so generating \(T\) tokens costs on the order of \(T^3\).
The KV cache keeps the rows of previous keys and values
3 tokens
\[ K = X W_k = \begin{bmatrix} {\color{#aaaaaa}{k_1^\top}} \\ {\color{#aaaaaa}{k_2^\top}} \\ k_3^\top \end{bmatrix} \qquad V = X W_v = \begin{bmatrix} {\color{#aaaaaa}{v_1^\top}} \\ {\color{#aaaaaa}{v_2^\top}} \\ v_3^\top \end{bmatrix} \qquad \text{masked scores} = \begin{bmatrix} {\color{#aaaaaa}{q_1^\top k_1}} & {\color{#aaaaaa}{-\infty}} & {\color{#aaaaaa}{-\infty}} \\ {\color{#aaaaaa}{q_2^\top k_1}} & {\color{#aaaaaa}{q_2^\top k_2}} & {\color{#aaaaaa}{-\infty}} \\ q_3^\top k_1 & q_3^\top k_2 & q_3^\top k_3 \end{bmatrix} \]
4 tokens
\[ K = X W_k = \begin{bmatrix} {\color{#aaaaaa}{k_1^\top}} \\ {\color{#aaaaaa}{k_2^\top}} \\ {\color{#aaaaaa}{k_3^\top}} \\ k_4^\top \end{bmatrix} \qquad V = X W_v = \begin{bmatrix} {\color{#aaaaaa}{v_1^\top}} \\ {\color{#aaaaaa}{v_2^\top}} \\ {\color{#aaaaaa}{v_3^\top}} \\ v_4^\top \end{bmatrix} \qquad \text{masked scores} = \begin{bmatrix} {\color{#aaaaaa}{q_1^\top k_1}} & {\color{#aaaaaa}{-\infty}} & {\color{#aaaaaa}{-\infty}} & {\color{#aaaaaa}{-\infty}} \\ {\color{#aaaaaa}{q_2^\top k_1}} & {\color{#aaaaaa}{q_2^\top k_2}} & {\color{#aaaaaa}{-\infty}} & {\color{#aaaaaa}{-\infty}} \\ {\color{#aaaaaa}{q_3^\top k_1}} & {\color{#aaaaaa}{q_3^\top k_2}} & {\color{#aaaaaa}{q_3^\top k_3}} & {\color{#aaaaaa}{-\infty}} \\ q_4^\top k_1 & q_4^\top k_2 & q_4^\top k_3 & q_4^\top k_4 \end{bmatrix} \]
Gray is not recomputed. Gray rows of \(K\) and \(V\) are read from the cache.
Gray score rows are skipped: row \(i\) only predicted token \(i+1\), which is already chosen.
Step \(t\) computes \(q_t\), \(k_t\), \(v_t\) and one row of \(t\) scores, so \(T\) tokens cost on the order of \(T^2\).
The price is memory: \(K\) and \(V\) for every layer, one row per token.
At 128K tokens, one user’s KV cache takes about 40 GB

Llama 3 70B is an open-weight LLM. Its weights are loaded once and shared by every user. The cache is kept per user.
Linear attention folds the cache into one running sum
Softmax attention, token 4
\(\displaystyle z_4^\top = \dfrac{\sum_{i \le 4} \exp(q_4^\top k_i)\, v_i^\top}{\sum_{i \le 4} \exp(q_4^\top k_i)} \)
Replace \(\exp(q^\top k)\) with \(\phi(q)^\top \phi(k)\), \(\phi\) fixed
\(\displaystyle z_4^\top = \dfrac{\sum_{i \le 4} \phi(q_4)^\top \phi(k_i)\, v_i^\top}{\sum_{i \le 4} \phi(q_4)^\top \phi(k_i)} \)
\(\phi(q_4)\) does not depend on \(i\), so it leaves the sum
\(\displaystyle \phantom{z_4^\top} = \dfrac{\phi(q_4)^\top\, \colorbox{transparent}{$\sum_{i \le 4} \phi(k_i)\, v_i^\top$}}{\phi(q_4)^\top\, \colorbox{transparent}{$\sum_{i \le 4} \phi(k_i)$}} = \dfrac{\phi(q_4)^\top\, \colorbox{transparent}{$S_4$}}{\phi(q_4)^\top\, \colorbox{transparent}{$c_4$}} \)
Each token adds one term. Gray is stored.
\[ \colorbox{transparent}{$S_4$} = {\color{#aaaaaa}{S_3}} + \phi(k_4)\, v_4^\top \qquad \colorbox{transparent}{$c_4$} = {\color{#aaaaaa}{c_3}} + \phi(k_4) \]
The KV cache stores a \(k\) and a \(v\) per token. Linear attention stores one \(S\) and one \(c\) in total.
The price: \(S\) is a sum over all earlier tokens, so attention can no longer pick out one token on its own.
Prompt and response caching

An agent re-sends its whole conversation on every turn. The provider keeps what it computed, and a request that starts with the same tokens reuses it.
The match must be exact from the first token, as the mask requires: row \(i\) of \(K\) and \(V\) depends on every token before it.
A response cache is a different thing: it stores finished answers to repeated questions, like an FAQ database.
At a small batch, the chip waits on memory, not on arithmetic
Generating a token for each of \(B\) requests is one pass over \(N\) weights, 2 bytes each.
Memory time: every weight moves from memory to the compute cores once per pass.
Compute time: each weight does roughly one multiply and one add, 2 FLOPs, per request.
\[ \text{memory time} = \frac{2N \text{ bytes}}{\text{memory bandwidth}} \qquad\qquad \text{compute time} = \frac{2N \cdot B \text{ FLOPs}}{\text{peak FLOPs per second}} \]
The paper’s chip does \(275 \times 10^{12}\) FLOPs per second and moves \(1200 \times 10^{9}\) bytes per second, so it can supply about 229 FLOPs per byte.
\[ \frac{\text{compute time}}{\text{memory time}} = \frac{B}{229} \]
Below \(B \approx 229\) this chip is memory-bound: a pass takes the memory time, whatever \(B\) is. Above, it is compute-bound.
A faster stream costs more because the batch gets smaller

\(C\): number of chips. \(B\): requests in the batch. \(t\): milliseconds per forward pass.
\[ \text{cost per token} = \frac{C\, t}{B} \ \text{ chip-milliseconds} \]
While memory-bound, \(t\) stays at the memory time, so the cost per token falls as \(1/B\).
The price ladder reflects this
Standard

Claude Opus 5.5 at three speeds, in dollars per million tokens.
Fast mode doubles the standard price.
The Batch API halves it, for requests that can wait.
Fast mode


Batch

A mixture of experts activates a subset of experts
\[ y = \sum_{i=0}^{n-1} \mathrm{Softmax}\big(\mathrm{Top2}(x \cdot W_g)\big)_i \cdot \mathrm{SwiGLU}_i(x) \]

\(x\): one token’s hidden vector.
\(W_g\): the gate, the figure’s router. It gives one logit per expert.
Top2 keeps the two largest logits and sets the rest to \(-\infty\), so six of the \(n = 8\) experts are never computed.
\(\mathrm{SwiGLU}_i\): expert \(i\), a feed-forward block with its own weights.
Gain: Mixtral holds 47B parameters and does the arithmetic of 13B per token.
Cost: each expert sees a quarter of the batch, so its arithmetic intensity is only \(B/4\).
Quantization rounds each layer’s weights to a few bits
Store each weight in 4 bits instead of 16, and keep each layer’s output close:
\[ \arg\min_{\widehat{W}} \; \big\| W X - \widehat{W} X \big\|_2^2 \]
\(W\): one layer’s weights. \(X\): its inputs on a small calibration set. \(\widehat{W}\): the rounded weights.
Round-to-nearest (RTN) ignores \(X\). GPTQ rounds one column of weights at a time and shifts the columns not yet rounded to cancel the error.

Perplexity of the OPT models on WikiText2, lower is better. 16 bits is full precision.
In 2025, gpt-oss-120b shipped with its expert weights at 4.25 bits each, to fit on one 80 GB GPU.
Bit quantization reduction couples with batch size
A 100B-weight LLM on the same chip. Rough time for one forward pass, in milliseconds:
Move the weights
Do the arithmetic
One pass
Batch 1, 16-bit
167
0.7
167
Batch 1, 4-bit
42
0.7
42
Batch 1000, 16-bit
167
727
894
Batch 1000, 4-bit
42
727
769
Only the weights are stored in 4 bits. The arithmetic still runs at 16 bits, at the same speed.
Both batches save the same 125 ms: 75% of the pass at batch 1, and 14% at batch 1000.
The smaller weights also free memory at any batch, for more requests or longer contexts.
Computed from the chip figures in Efficiently Scaling Transformer Inference, MLSys 2023; the KV cache is left out
Kimi K3 stacks these methods

Mixture of experts: the rows Routed Experts and Experts Active per Token.
Linear attention: KDA layers keep a state of fixed size. MLA layers are softmax attention, and their KV cache grows with length.
Quantization, not in the table: the expert weights are stored in 4 bits.
Outline
A. Efficiency
B. REINFORCE
C. PPO, GRPO, RLVR
D. Prompting and search at inference
🌭

current prediction \(g=\text{softmax}(\cdot)\)



To reduce the loss, \(\theta\) needs to change so \(g_{\text{hotdog}}\) can go up.
This signal (from the true label \(y\)) flows back to \(\theta\) through \(-\!\log\) and softmax, so gradient descent can lower the loss.
Policy gradient is similar to supervised learning. Recall from 6.390:
For the hot dog, a gradient step is the label minus the prediction
Taking \(\theta\) to be the three logits, one gradient step is:
\[ \theta \leftarrow \theta - \eta\, \nabla_\theta \mathcal{L}_{\mathrm{nllm}} = \theta + \eta\, \nabla_\theta \log g_{\text{hotdog}} = \theta + \eta\, \colorbox{transparent}{$(y - g)$} \]

\( y = [1,\, 0,\, 0] \)
\( - \)

\( g = [0.1,\, 0.2,\, 0.7] \)
\( = \)

\( y - g = [+0.9,\, -0.2,\, -0.7] \)
The step raises the hot dog logit and lowers the other two.

🤷 ↑ ? ↓


2. Run \(\pi_\theta\) for a while. Keep track of the return.
Policy gradients:
1. Parametrize the policy \(\pi\) with \(\theta.\)
3. Update \(\theta\) so “good” trajectories are more likely to happen.
A bandit is the simplest case: one decision, then a reward
A bandit problem considers a bunch of slot machines.
We pull one arm, see a payout, and go again.
It is a single-state MDP: nothing we do changes the state.
An LLM answering a multiple-choice question with three options can be modeled as a bandit with three arms. Each arm is an answer; its payout is the score, or reward.
How to train?
1. Parametrize the policy: \(\theta\) holds one logit per answer, and \(\pi_\theta = \mathrm{softmax}(\theta)\) (6.390’s \(g\)).
Say \(\pi_\theta = [\tfrac{1}{3}, \tfrac{1}{3}, \tfrac{1}{3}]\).
2. Sample one answer from \(\pi_\theta\) and get its score \(r\). Say the second answer, with \(r = 1\).
A sampled answer stands in for the label, scaled by its score
3. Update \(\theta\) so a well-scored answer becomes more likely.
Supervised: the hot dog step:
\[ \theta \leftarrow \theta + \eta\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta$}(\colorbox{transparent}{label}) \]
Bandit: there is no label, so the sampled answer takes its place, scaled by its score:
\[ \theta \leftarrow \theta + \eta\, \colorbox{transparent}{$r$}\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta$}(\colorbox{transparent}{sampled}) \]
\(\colorbox{transparent}{$r$}\) the score · \(\colorbox{transparent}{$\nabla_\theta \log \pi_\theta$}\) the push toward an answer · \(\colorbox{transparent}{label}\) which answer is pushed
If the second answer is sampled with \(r = 1\), \(\theta\) moves along sample minus prediction:

\( \mathrm{onehot}(2) = [0,\, 1,\, 0] \)
\( - \)

\( \pi_\theta = [\tfrac{1}{3},\, \tfrac{1}{3},\, \tfrac{1}{3}] \)
\( = \)
\( [-\tfrac{1}{3},\, +\tfrac{2}{3},\, -\tfrac{1}{3}] \)
Had it scored \(r = 0\), nothing would move.
Why it works: the update climbs the expected score
\(\theta\) only sets how often each answer is sampled, so the goal is a high expected score:
\[ \mathbb{E}_{y \sim \pi_\theta}\big[ \colorbox{transparent}{$r(y)$} \big] = \sum_{y \in \{1, 2, 3\}} \pi_\theta(y)\, \colorbox{transparent}{$r(y)$} \]
\(y\) an answer · \(\pi_\theta(y)\) how often the LLM samples it · \(\colorbox{transparent}{$r(y)$}\) its score
\[ \begin{aligned} \nabla_\theta\, \mathbb{E}_{y \sim \pi_\theta}\big[ \colorbox{transparent}{$r(y)$} \big] &= \sum_{y \in \{1, 2, 3\}} \colorbox{transparent}{$r(y)$}\, \nabla_\theta \pi_\theta(y) \\[4pt] &= \sum_{y \in \{1, 2, 3\}} \pi_\theta(y)\, \colorbox{transparent}{$r(y)$}\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y)$} \\[4pt] &= \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{transparent}{$r(y)$}\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y)$} \,\big] \end{aligned} \]
The second line uses \(\nabla_\theta \pi_\theta(y) = \pi_\theta(y)\, \nabla_\theta \log \pi_\theta(y)\).
So the bandit update samples this gradient, and on average climbs the expected score.
\(r\) is never differentiated, so the score can come from a person, a reward model, or a test suite.
A baseline pushes failures down and steadies each step
With pass/fail scores, a failed answer has \(r = 0\) and moves nothing; only passes push.
Subtract a baseline \(b\), any number that does not depend on the sampled answer, such as the average score:
\[ \nabla_\theta\, \mathbb{E}_{y \sim \pi_\theta}\big[ \colorbox{transparent}{$r(y)$} \big] = \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{transparent}{$r(y) - b$}\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y)$} \,\big] \]
This holds because
\[ \mathbb{E}_{y \sim \pi_\theta}\big[\, b\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y)$} \,\big] = b\, \nabla_\theta \sum_{y \in \{1, 2, 3\}} \pi_\theta(y) = b\, \nabla_\theta 1 = 0 \]
Now a failure has \(\colorbox{transparent}{$r - b$} < 0\) and is pushed down. \(A = r - b\) is called the advantage.
It also steadies each step. If every answer scores 9 or 10, each sample pushes its own answer up by about 10, whichever answer it is. With \(b = 9.5\) the pushes are \(\pm 0.5\): the same average step, much less noise.
In general, an LLM’s answer is a trajectory, built one token at a time
\(x\) is the prompt and \(y\) the answer. The state is the prompt plus the tokens so far, and each action appends one token, so \(y\) is a trajectory, like one game of Pong.
Treat the whole answer as one action, and the one-step result holds unchanged:
\[ \nabla_\theta\, \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{transparent}{$r(x, y)$} \,\big] = \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{transparent}{$r(x, y)$}\, \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y \mid x)$} \,\big] \]
An answer’s probability is the product of its token choices, so its log is a sum:
\[ \log \pi_\theta(y \mid x) = \sum_t \log \pi_\theta(y_t \mid x, y_{<t}) \]
Together, one score for the whole answer scales every token’s term:
\[ \nabla_\theta\, \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{transparent}{$r(x, y)$} \,\big] = \mathbb{E}_{y \sim \pi_\theta}\Big[\, \colorbox{transparent}{$r(x, y)$} \sum_t \colorbox{transparent}{$\nabla_\theta \log \pi_\theta(y_t \mid x, y_{<t})$} \,\Big] \]
The baseline carries over unchanged: \(r(x, y) - b\) takes the place of \(r(x, y)\).
Outline
A. Efficiency
B. REINFORCE
C. PPO, GRPO, RLVR
D. Prompting and search at inference
A reward can be a score or a comparison, designed or discovered
Designed: we write it
Discovered: we measure it
A score: a number per answer
A checker. The tests pass, or the answer matches.
A measured outcome, such as an A/B test or a clinical trial.
A comparison: which answer is better
An order the designer writes down, as in learning to rank.
Human preferences between two answers. RLHF fits a reward model to these.
Recall from Lec 8: a KL penalty keeps the LLM near where it started
On reward alone, the LLM drifts toward answers that fool the reward model.
So each answer’s reward is docked for moving:
\[ r^\prime(x,y) = \colorbox{transparent}{$r(x,y)$} - \colorbox{transparent}{$\beta \log \dfrac{\pi_\theta(y \mid x)}{\pi_{\mathrm{ref}}(y \mid x)}$} \]
Blue grows as the LLM’s answers move away from \( \pi_{\mathrm{ref}} \), the LLM it started as. \( \beta \) is a hyperparameter that sets the leash strength.
REINFORCE runs as before, on \( r^\prime \). Blue is not a baseline: it depends on the answer \(y\), so it changes which answers win. Averaged over answers, blue is the KL divergence.
A step that goes too far spoils the next batch of answers
In supervised learning, a bad step gives a worse model, but the next batch of data is the same as ever.
In policy gradient, the LLM samples its own training data:
1. A step that goes too far gives a worse LLM.
2. The next batch is that LLM’s answers. They may all score 0, so nothing corrects it.
3. Training may not recover.
The KL penalty guards the whole run, not each step. So also keep each update close to \(\pi_{\mathrm{old}}\), the LLM that sampled the batch.

TRPO keeps each update within a KL limit of the old LLM
The batch’s answers came from \(\pi_{\mathrm{old}}\). The ratio \(\rho\) is the one inside the KL penalty’s blue log, with \(\pi_{\mathrm{old}}\) in place of \(\pi_{\mathrm{ref}}\). It reweights the answers to score a nearby \(\pi_\theta\):
\[ \mathbb{E}_{y \sim \pi_{\mathrm{old}}}\big[\, \rho\, \colorbox{#aaaaaa}{$A$} \,\big] = \mathbb{E}_{y \sim \pi_\theta}\big[\, \colorbox{#aaaaaa}{$A$} \,\big], \qquad \rho = \frac{\pi_\theta(y)}{\pi_{\mathrm{old}}(y)}, \qquad \colorbox{#aaaaaa}{$A$} = r - b \]
TRPO raises the left side, and limits how far \(\pi_\theta\) may move:
\[ \max_\theta\; \mathbb{E}_{y \sim \pi_{\mathrm{old}}}\big[\, \rho\, \colorbox{#aaaaaa}{$A$} \,\big] \quad \text{subject to} \quad D_{\mathrm{KL}}\big( \pi_{\mathrm{old}} \,\|\, \pi_\theta \big) \le \delta \]
At \(\rho = 1\), the gradient of \(\rho A\) is \(A\, \nabla_\theta \log \pi_\theta(y)\): the baseline update.
A hard limit is costly to enforce. PPO’s first version turned it into a penalty, \(-\beta\, D_{\mathrm{KL}}\). The version used today clips \(\rho\) instead.
PPO clips the ratio, so an answer stops pulling past 1 ± ε
\[ L^{\mathrm{CLIP}} = \mathbb{E}\Big[ \min\big( \rho\, \colorbox{transparent}{$A$},\; \mathrm{clip}(\rho,\, 1-\epsilon,\, 1+\epsilon)\, \colorbox{transparent}{$A$} \big) \Big], \qquad \rho = \frac{\pi_\theta(y)}{\pi_{\mathrm{old}}(y)} \]

The figure’s \(r\) is \(\rho\), not the score. Red dot: the start, \(\rho = 1\).
\(A > 0\), a good answer: raising \(\rho\) helps, but only up to \(1 + \epsilon\). Past that the objective is flat and the gradient is zero.
\(A < 0\), a bad answer: lowering \(\rho\) helps, but only down to \(1 - \epsilon\).
A move the wrong way is never clipped, so a mistake can still be undone.
GRPO scores each answer against the others to the same prompt

\(q\) is one prompt, \(o_1 \dots o_G\) are the LLM’s sampled answers, and \(r_i\) their rewards.
The group’s mean reward is the baseline, so GRPO trains no value model. It keeps PPO’s clipped ratio.
\[ A_i = \frac{r_i - \mathrm{mean}(r_1, \dots, r_G)}{\mathrm{std}(r_1, \dots, r_G)} \]
GRPO drops PPO’s value model, a second network often about the size of the LLM. Each prompt has its own baseline, so passing a hard prompt counts for more than an easy one.
RLVR replaces the reward model with a checker
“RLVR leverages the existing RLHF objective but replaces the reward model with a verification function.”

A checker needs no human raters and no trained reward model.
Each method puts a different weight on the same gradient
The answers come from
Scaling factor on \(\nabla_\theta \log \pi_\theta\)
Keeps each update small
REINFORCE
one sample from the LLM
\(r - b\)
nothing
PPO
a batch from \(\pi_{\mathrm{old}}\), reused for several steps
\(\rho\, A\)
the clip on \(\rho\)
GRPO
\(G\) samples to one prompt
\((r_i - \mathrm{mean}) / \mathrm{std}\)
PPO’s clip
RLVR
as in PPO or GRPO
the same, with \(r\) from a checker
PPO’s clip
All four can also add the KL penalty to \(r\), so the LLM stays near \(\pi_{\mathrm{ref}}\) over the whole run.
Outline
A. Efficiency
B. REINFORCE
C. PPO, GRPO, RLVR
D. Prompting and search at inference
A language model can learn a task from examples in its prompt
A few-shot prompt puts worked examples before the question (left). No weights change; the model only reads.


Right: on a task of removing random symbols from a word, accuracy climbs with the number of examples in the prompt, and far more steeply for the 175B model.
Learning from the prompt can be gradient descent in disguise
A transformer was trained to predict \(y\) for a new \(x\) from a prompt of \((x, y)\) pairs, each prompt a fresh linear regression problem.

Purple: its loss while it is being trained, over many such prompts. It falls to the loss of one gradient-descent step on each prompt’s pairs (green).
Once trained, its weights stay fixed. For each new prompt, the forward pass does the fitting.
On a pretrained LLM, in-context learning and a gradient step change the output differently; still active area of research.
With chain-of-thought, the LLM writes its steps before the answer

A chain-of-thought prompt shows worked steps before the answer (blue). The LLM then writes its own steps (green).
The steps are extra tokens. Every later token attends to them, as to any earlier row of \(K\) and \(V\).
Reasoning models do this by default. The thinking tokens are billed as output.
The likeliest next token does not necessarily give the best answer
Greedy decoding takes the most probable token at every step.
Say there are three two-token answers:
Answer
First token
Second token
Whole answer
A B
0.6
0.5
0.30
A C
0.6
0.5
0.30
D E
0.4
0.9
0.36
Greedy takes A first, at 0.6, and ends at 0.30. The best answer starts with D.
Search looks at more than one path before it commits.
Sample many answers, and let a checker pick
Checking is easier than generating: run the tests, or compare with the answer key.
Best of \(n\): sample \(n\) answers and keep one that passes. If one answer passes with probability \(p\):
\[ P(\text{at least one of } n \text{ passes}) = 1 - (1 - p)^n \]
Samples \(n\)
Chance at least one passes
Tokens
1
20%
1×
10
89%
10×
50
over 99.9%
50×
With \(p = 0.2\). The price is section A’s: \(n\) times the tokens, on every request.
Here no weights change. GRPO is the training version: the same group of samples and the same checker, used to update the weights.
Assumes each sample passes independently at rate \(p\). A prompt the LLM never solves gains nothing.
What the machinery changes in our choices
Keep a long prompt’s opening the same across requests, so the provider’s cache can reuse it; every extra token costs memory per user.
Price follows batch size, experts per token, and bits per weight: a faster stream costs more, and a mixture-of-experts or 4-bit model costs less to run.
Whatever the reward scores, training amplifies. Check what a checker or judge rewards before training on it.
Sampling more answers and checking them buys quality at a price per request. Training on the ones that pass makes them the default.
6.S950 - Agency with AI (Fall 26) - Lecture 10: The Machinery Beyond Vanilla Transformers
By Shen Shen
6.S950 - Agency with AI (Fall 26) - Lecture 10: The Machinery Beyond Vanilla Transformers
Agency with AI, Lecture 10: The Machinery Beyond Vanilla Transformers
- 60