Search

Search papers and lessons

Text-to-Text Framework

Level 4 · 9 min read

Text-to-text casts every language task as text in and text out, so one encoder-decoder model and one loss can handle translation, summarising and classification alike.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Architectures
  • Pre-training
  • Sign in to see your progress

TL;DR

Most language tasks look different on the surface. Translation maps a sentence to a sentence, summarising maps a document to a shorter text, and classification maps a sentence to a label. The text-to-text framework treats all of them as one problem: the input is a string, the output is a string, and a single model learns the mapping between them.

Because every task shares the same input and output format, one model architecture, one training objective and one set of settings can be reused across tasks. A task is named by a short text prefix in the input, so switching tasks means changing the words you send rather than the network.

Where you'll see it: in the T5 family of models, in multi-task training mixes where one model answers many tasks, and in the instruction-following style of prompting that later large language models adopted.

Intuition

One interface for every task

Imagine a translation office where every request arrives on the same form. The form has a box for the request and a box for the answer, and the clerk never needs a different desk for each kind of job. The clerk reads the request, which says what job it is, and writes the answer in the second box. Adding a new kind of job means writing a new request format, not building a new desk.

Before this framework, most systems had a separate output layer for each task. A classifier had a head that produced a label distribution. A tagger had another. A regression model had a number output. Each head was trained from scratch for its task, so the knowledge from pretraining reached only the shared body of the network. The text-to-text idea removes the special heads. Every answer is generated token by token, the same way a language model writes text.

The flow looks like this. A short prefix in the input names the task, the encoder-decoder reads the whole input, and the decoder writes the output string.

flowchart LR
  A["translate English to German: That is good."] --> M[Encoder-decoder model]
  B["summarize: a long news article"] --> M
  C["sst2 sentence: a lovely film"] --> M
  M --> D["Das ist gut."]
  M --> E["a short summary"]
  M --> F["positive"]

Classification is no longer special. The label "positive" is just a short string, and the model learns to spell it out. The same is true for a score, which is written as digits.

The math

Definition: Text-to-text task
A task is a set of pairs (x,y)(x, y) of strings, where xx is the input text (including any task prefix) and yy is the target text. A model is a conditional distribution p(y∣x)p(y \mid x) over strings, built as an encoder-decoder Transformer.

The objective

The decoder writes the target one token at a time, so the probability of the whole target factors into a product of next-token probabilities:

p(y∣x)=∏t=1Tp(yt∣y<t,x).p(y \mid x) = \prod_{t=1}^{T} p(y_t \mid y_{\lt t}, x).

Here yty_t is the tt-th target token, y<ty_{\lt t} is every target token before it, and TT is the target length. Training minimises the cross-entropy of the true target, which is the negative log-likelihood:

L(x,y)=−∑t=1Tlog⁡p(yt∣y<t,x).\mathcal{L}(x, y) = -\sum_{t=1}^{T} \log p(y_t \mid y_{\lt t}, x).

Every task uses this same loss, so mixing tasks in one training run needs no special handling beyond sampling the pairs.

Task prefixes

A prefix τ\tau is a short string such as "translate English to German" or "summarize". The model input is the concatenation

x′=τ :  x,x' = \tau \,\texttt{:}\; x,

so two tasks that share the same raw text but different prefixes ask for different outputs. The prefix is what lets one network know which job it is doing.

Denoising by span corruption

Text-to-text models are usually pretrained on a fill-in-the-blank task rather than a plain next-token task. The original T5 work corrupted about 15 percent of the input tokens, grouped into spans with an average length of about three. Each dropped span is replaced in the input by a unique placeholder marker, and the target lists the placeholder followed by the missing tokens. The encoder reads the corrupted text, and the decoder reconstructs the dropped spans. Only the target tokens enter the loss.

Worked examples

Example
Span corruption by hand

Start from the sentence "Thank you for inviting me to your party last week."

  1. Choose two spans to drop: "for inviting" and "last".
  2. Replace each span in the input with a placeholder, numbered X and Y for the first two. The input becomes Thank you X me to your party Y week.
  3. Write the target: the placeholder, the dropped tokens, then the next placeholder. The target is X for inviting Y last Z, where Z marks the end.
  4. Count the loss tokens. The target has 6 items for 2 dropped spans (3 dropped words and 3 placeholders), while the corrupted input has 9 items. The decoder predicts all 6 target items, but the target is still short because it holds only the dropped words and markers, not the whole sentence.

Interpretation: the model never sees the masked words, so it must use the words around each gap to fill them in. That is the same pressure that masked language modeling applies, now written as text in and text out.

Example
Computing the loss for a short target

Suppose the target is the two-token string "for inviting", and the model gives probability 0.50.5 to "for" given the input, and 0.80.8 to "inviting" given the input and "for".

  1. Apply the factorisation: p(y∣x)=0.5×0.8=0.4p(y \mid x) = 0.5 \times 0.8 = 0.4.
  2. Take the negative log of each term: −ln⁡0.5=0.693-\ln 0.5 = 0.693 and −ln⁡0.8=0.223-\ln 0.8 = 0.223.
  3. Add them: L=0.693+0.223=0.916\mathcal{L} = 0.693 + 0.223 = 0.916.
  4. Average over the two tokens for a per-token figure: 0.916/2=0.4580.916 / 2 = 0.458.

If the model had given "for" only 0.050.05 instead of 0.50.5, the first term alone would be −ln⁡0.05=2.996-\ln 0.05 = 2.996. A confident wrong token costs far more than a weakly wrong one, which is why cross-entropy trains the decoder to commit to the right tokens.

Example
One model instead of ten

A team needs answers for ten language tasks. Suppose a base-sized encoder-decoder has about 220 million parameters.

  1. With one fine-tuned model per task, the team stores 10×220 million=2.2 billion10 \times 220 \text{ million} = 2.2 \text{ billion} parameters.
  2. With the text-to-text framework, one shared model is fine-tuned on all ten tasks with prefixes. The team stores 220 million220 \text{ million} parameters, a tenth of the first total.
  3. The cost is one set of weights to host and one training pipeline to maintain.

Interpretation: the saving is largest when tasks are small or many. The trade is that a shared model can be pulled toward one task at the expense of another, which the mixing schedule has to manage.

Tradeoffs and failure modes

The framework is convenient, but it moves some difficulty into the text itself.

SituationWhat goes wrongBetter choice
Classification with many labelsEach label must be spelled and decoded, which is slower and can yield invalid stringsConstrain decoding to the label set, or use a classification head for speed
Numeric regressionNumbers are written as digits, so precision and rounding depend on the tokenizerUse a regression head when fine-grained values matter
Tasks with very different lengths in one mixLong inputs dominate the batch and skew the learning signalBalance the mixture or cap lengths per task
Prefixes that look alikeThe model confuses tasks whose prefixes differ by one wordUse distinct, descriptive prefixes and test each one
Output that drifts from the label setThe decoder may write a near-miss string such as a synonymUse constrained or exact-match decoding and check outputs

History

Colin Raffel and colleagues at Google published the framework in 2019 as the T5 model, short for Text-to-Text Transfer Transformer. The paper compared pretraining objectives, datasets and model sizes across a wide range of tasks, and then tested one encoder-decoder model on all of them as text. Their large clean web corpus, called C4, supplied the pretraining text. The models ranged from tens of millions of parameters up to about 11 billion.

The idea grew out of earlier work on fill-in-the-blank pretraining, especially masked language modeling in BERT, which appeared in 2018, and on transfer learning in which a single pretrained model is adapted to many tasks. T5 combined those threads and made the task interface uniform. Later instruction-tuned variants showed that a shared text interface also handles new tasks described in plain language. In the early 2020s most large chat models adopted the same principle in a decoder-only form, where the prompt carries the task.

Check yourself

QuestionWhy does the text-to-text framework use one loss for every task instead of a separate head per task?Show answer

A separate head maps the shared representation to a task-specific output, so each task needs its own trained parameters. When every answer is a string, the same decoder and the same cross-entropy loss apply to all tasks. Pretraining knowledge then flows through every part of the network, and adding a task only requires new text pairs and a prefix.

QuestionA target string has three tokens with probabilities 0.90.9, 0.50.5 and 0.250.25 under the model. Compute its loss L\mathcal{L} and its per-token loss.Show answer

Compute each term: −ln⁡0.9=0.105-\ln 0.9 = 0.105, −ln⁡0.5=0.693-\ln 0.5 = 0.693, and −ln⁡0.25=1.386-\ln 0.25 = 1.386. The sum is 0.105+0.693+1.386=2.1840.105 + 0.693 + 1.386 = 2.184. Dividing by three gives a per-token loss of 0.7280.728. The weakest token, at 0.250.25, contributes the most.

QuestionIn span corruption, why is only the target part of the loss, not the whole input?Show answer

The encoder's job is to build a representation of the corrupted input, and its outputs are not scored directly. The decoder is what writes the missing spans, so the loss is computed on its predictions for the target. Scoring the input would ask the model to reproduce text it was shown, which teaches nothing about filling gaps.

QuestionA team wants a single model that handles translation and sentiment labels. What is one risk of writing the sentiment label as a word, and how would you reduce it?Show answer

The decoder may produce a string that is close to a valid label but not one of the allowed labels, such as a synonym or a misspelling. To reduce it, constrain decoding so only the label strings can be generated, or check each output against the label set and treat anything else as an error.

Sources