4.3.3 · D3Pretraining & Fine-Tuning LLMs

Worked examples — T5 and text-to-text framework

2,670 words12 min readBack to topic

Warm-up: the three tools we will reuse

Related tools: Cross-entropy loss, Transfer learning and fine-tuning, Multi-task learning with prefixes.


The scenario matrix

Every T5 example lives in one of these cells. The worked examples below are labelled with the cell they cover, and together they hit all of them.

Cell Case class What's special / the "edge" Example
A Classification, model right short label string, high Ex 1
B Classification, model wrong off-vocabulary / wrong label → still just CE Ex 2
C Regression as text (STS-B) number "3.8" emitted as digit-tokens; rounding to 0.2 Ex 3
D Translation, multi-token target full product over several tokens Ex 4
E Pretraining span corruption sentinels <X>,<Y>, build input+target from raw text Ex 5
F Degenerate input/output empty target = just </s>; limiting case Ex 6
G Multi-task prefix decides task same input string, two prefixes → two answers Ex 7
H Exam twist: greedy vs. beam greedy can pick a locally-good but globally-wrong string Ex 8

We will reuse two tiny fixed "sureness" numbers so the arithmetic is clean and checkable.


Ex 1 — Cell A: classification, model is right

Forecast: guess first — will the loss be closer to , to , or above ? (The model is very sure and correct, so bet on small.)

  1. Write the product of confidences. . Why this step? The chain rule (parent note) factorizes the whole-string probability into one factor per token; multiply them.
  2. Take of each factor and add. . Why this step? of a product = sum of s; this is exactly .
  3. Evaluate. , , so . Why this step? Plug the numbers into the natural log.
  4. Per-token loss. . Why this step? Divides the total pain over the 2 tokens so it's comparable across examples.

Verify: ✓ (same as summing the two logs — a good consistency check that the product/sum identity held). Small loss, as forecast: the model was confident and correct.


Ex 2 — Cell B: classification, model is wrong

Forecast: the model is confidently wrong — expect the loss to be large compared to Ex 1.

  1. Loss on the true token. . Why this step? We always score the probability the model gave to the correct target, no matter what it preferred.
  2. Add the </s> term. , so . Why this step? Sum over all target tokens as before.
  3. Compare. the loss of Ex 1. Why this step? Confidence in the wrong answer is punished hard — that's the behaviour from the warm-up.

Verify: ✓.


Ex 3 — Cell C: regression emitted as text (STS-B)

Forecast: rounding to a multiple of — is it or ? And is the loss moderate (the model is only 60% sure)?

  1. Round to nearest . Compute , round to , multiply back: . Why this step? T5 discretizes the continuous range into a finite vocabulary of targets ( = 26 values) so cross-entropy — a tool for choosing among finitely many options — even applies. A raw real number has no "probability of exactly ".
  2. Check the boundary case. The two candidates are and ; is from and from , so wins. Why this step? Always confirm which side of the midpoint () you're on — the exam trap is rounding the wrong way.
  3. Compute the loss. . Why this step? Identical machinery — the number is just a string.

Verify: rounded target ✓; ✓.


Ex 4 — Cell D: translation, several target tokens

Forecast: four factors each below 1, so the product shrinks — will be around or nearer ?

  1. Multiply confidences. . Why this step? Chain rule: whole-sentence probability is the product of per-token probabilities.
  2. Total loss = sum of . Why this step? Turns the shrinking product into a friendly additive score.
  3. Per-token loss. . Why this step? A 4-token sentence and a 2-token label can now be compared on equal footing.

Verify: ✓ (product-vs-sum agree); per-token ✓.


Ex 5 — Cell E: build a span-corruption training pair

Forecast: how many sentinels appear in the input? in the target? (Hint: one per dropped span, plus a closing sentinel at the very end of the target.)

  1. Replace span 1 with <X>, span 2 with <Y>. Input: Thank you <X> me to your party <Y> week . Why this step? Sentinels are unique pointers so the decoder knows which blank each generated chunk fills.
  2. Target = each sentinel followed by its original text, then a final <Z>. Target: <X> for inviting <Y> last <Z> Why this step? The decoder must generate the missing text (keeping the text-to-text shape), not classify masked positions like BERT does. The trailing <Z> marks end-of-target.
  3. Count target tokens. <X>, for, inviting, <Y>, last, <Z> = tokens. Why this step? is the number of terms in the loss sum — here 6.
Figure — T5 and text-to-text framework

Verify: target token count ✓ (checked in VERIFY by counting the listed tokens).


Ex 6 — Cell F: the degenerate / limiting case

Forecast: with a single token the loss is just — no sum to spread it over. Bigger or smaller than the multi-token cases?

  1. One-term sum. . Why this step? The sum with has exactly one term — the shortest possible target still has a well-defined loss.
  2. Per-token loss = total loss. . Why this step? Dividing by changes nothing; the degenerate case doesn't break the formula.
  3. Limiting sanity check. If the model were sure to stop, and . If it refused to stop, and . Why this step? Confirms both extreme ends behave sensibly — the reader never hits an undefined scenario.

Verify: ✓.


Ex 7 — Cell G: the prefix chooses the task

Forecast: which single change flips the output from a label to a German sentence? (It is not a new head.)

  1. Prepend the task prefix to the encoder input. Why this step? The prefix is ordinary text the encoder reads; it conditions the decoder on which task to perform — this is multi-task-by-prefix.
  2. Decode until </s>; read the string as the answer. Why this step? Same architecture and same cross-entropy for both — only the target string differs.
  3. Loss is identical machinery. For the CoLA target ["unacceptable","</s>"] with : . Why this step? Demonstrates the same loss formula scores a label string, just as it scored digits and German words.

Verify: ✓.


Ex 8 — Cell H: exam twist, greedy vs. beam

Forecast: greedy commits to at step 1, but path Q's second token is much stronger. Guess who wins overall.

  1. Sequence probabilities. ; . Why this step? Chain rule again — a string's score is the product of its tokens, not just the first token.
  2. Losses. ; . Why this step? Lower loss = higher-probability string. wins ().
  3. Conclusion. Greedy chose (higher first token) but is the better sequence. Beam search keeps several partial paths alive, so it can recover . Why this step? Shows the classic exam trap: locally best ≠ globally best. This is a decoding choice at inference; it does not change the training loss.

Verify: , ✓; , , and ✓.


Recall

Recall Did every cell get covered?

Which example handled a wrong classification? ::: Ex 2 (Cell B). How does Ex 3 turn a real number into something cross-entropy can score? ::: Round to nearest 0.2 → one of 26 string labels → CE over its tokens. In Ex 5, why does the target end in <Z>? ::: <Z> is the closing sentinel marking end-of-target. In Ex 6, what is the shortest possible target and its loss when ? ::: ["</s>"], , . Why did greedy lose in Ex 8? ::: It maximized the first token () but path Q had higher total product ().

Connections