12.4 T5: span corruption and relative position buckets
Two pretraining objectives are now in place: GPT’s next token and BERT’s masked tokens. BERT’s targets are single tokens at known positions, which an encoder-decoder Transformer cannot use well, because its decoder writes sequences. T5 recast pretraining as text to text: corrupt the input by dropping whole spans and ask the decoder to write them back. It also replaced absolute position embeddings with a small learned table indexed by relative distance, which generalizes past the training length.
Concepts from other sections (9)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- absolute position: , the position of a query row in the whole sequence rather than in the current call. Read more: 22.5 FlashAttention forward in C
- Self-attention: a layer in which each position computes a weighted average of all positions’ values, weighted by query-key similarity. Read more: Chapter 2: Deep Learning
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Upstream gradient: , the gradient arriving at an op’s output. Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs in Mathematics
- RoPE: rotary position embedding, which rotates queries and keys by position to encode relative position. Read more: 5.3 Model landscape and lifecycle
- ALiBi: a per-head bias that falls linearly with query-key distance, used instead of rotations. Read more: 13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi
- scatter-add: adding each upstream contribution into its source position with
np.add.at, so repeated reads accumulate. Read more: 6.3 The op library and gradcheck_all - Fisher-Yates shuffle: the in-place permutation algorithm that swaps each position , from the end, with a uniformly chosen position at or below it. Read more: 6.6 Training loop and the autograd bigram
Neither piece has a call site in the system this book builds, which is why this section is optional. It is still worth an evening. Span corruption is the objective of T5, UL2, and many code models, and relative buckets are the simplest relative position scheme, a good contrast to RoPE and ALiBi in Part 7. If you want to put the pieces to work, a natural side project is to train a small T5 on the book’s corpus with span corruption for pretraining and the bias inside every attention layer.
The code uses only the Tensor, the permute op, and Embedding; it helps to have read categorical sampling and PCG32 in Mathematics. The reference implementation is python/tinyllm/obj/t5.py (noise_span_counts, random_spans_noise_mask, span_corrupt, t5_relative_bucket, T5RelativeBias), with the contract in course/contracts/py/tinyllm/obj/t5.pyi.
Learning objectives
- Compute the number of noise tokens and spans for a text, with half-to-even rounding and both clamps.
- Lay out a span-corruption mask from two random compositions and replay the exact
belowdraws.- Build inputs and targets with sentinels counting down from the top of the vocabulary, and show that they restore the text.
- Compute T5 relative position buckets by hand for bidirectional and causal attention.
- Explain how the bias table is indexed and trained, and what
q_offsetdoes in cached decoding.
Span corruption
Section titled “Span corruption”Here is the notation for this section.
| Symbol | Meaning | Type / shape |
|---|---|---|
| the number of tokens of one text | int | |
noise_density, the fraction to corrupt | float in | |
mean_noise_span, the average span length | float | |
| noise tokens, | int | |
| noise spans, | int | |
sentinel , the id sentinel_start_id | int | |
| relative position of key from query | int | |
buckets per direction (num_buckets, halved when bidirectional) | int | |
max_distance | int |
T5’s random_spans_noise_mask decides how many before where. With noise tokens in spans, it splits into positive span lengths and the kept tokens into positive gap lengths, then lays them out as keep, noise, keep, noise, and so on. The text therefore always starts with kept tokens and ends with a noise span.
Splitting items into positive parts uniformly is a shuffle. Take flags, the first of them true, shuffle them, and start a new part after every true flag. The shuffle is Fisher-Yates from the end, $j = $ rng.below(i + 1) for down to 1, and the noise lengths are drawn first.
Rounding is numpy’s (half to even): , gives , not 3. T5 clamps only from below. On a short text can exceed , and then some gap would be empty, so this book’s implementation also clamps .
Once the mask is fixed, each noise span is replaced in the inputs by sentinel , and the targets are each span preceded by its sentinel. T5’s sentinels are the last ids of the vocabulary counting down (<extra_id_0> is ), so sentinel_start_id is and . Real ids must stay out of the sentinel range, or the targets become ambiguous. Replacing every sentinel of the inputs by what follows it in the targets gives the text back. The objective loses nothing, and the targets are short, about tokens.
Relative position buckets
Section titled “Relative position buckets”Span corruption is T5’s objective; its other idea concerns positions. Self-attention without positions is permutation-equivariant. T5 adds to head ‘s logit for query and key a learned scalar , with . Small offsets matter most and get one bucket each; large offsets share logarithmically wider buckets:
for a distance . The two attention directions use this differently.
- Bidirectional (the encoder): is half of
num_buckets, , and keys after the query () add , so the two directions use disjoint halves. - Causal (the decoder): is all of
num_bucketsand . Every future key falls into bucket 0, which the causal mask hides anyway.
The logarithm is computed in float32 and truncated toward zero, exactly as in the Hugging Face code that T5 checkpoints were trained with; a correct solution agrees with it at every offset in . The bias table is an Embedding(num_buckets, n_heads) named relative_attention_bias, and T5RelativeBias.forward(q_len, k_len, q_offset) returns [n_heads, q_len, k_len], with q_offset the number of tokens already in a decoder’s cache.
Working it by hand
Section titled “Working it by hand”Start with the simplest case, one span. Take tokens 10 to 17 (), , . Then and . One span leaves no choice: 6 kept tokens, then 2 noise tokens. With , the inputs are and the targets .
Now two spans, with scripted draws. Take tokens 0 to 9, , , so and . The table traces both shuffles.
| step | flags before | draw | flags after | lengths |
|---|---|---|---|---|
| noise: 3 flags, first 1 true | T F F | below(3) = 0, swap 2 and 0 | F F T | |
| F F T | below(2) = 1, swap 1 and 1 | F F T | parts after each true flag: 3, 1 | |
| keep: 5 flags, first 1 true | T F F F F | below(5) = 2, below(4) = 0, below(3) = 2, below(2) = 0 | F F F T F | 4, 2 |
The layout is keep 4, noise 3, keep 2, noise 1, so tokens 4, 5, 6, and 9 are noise. The inputs are and the targets .
Finally, buckets. With bidirectional attention, 32 buckets (, exact below 8), and :
| bucket | ||
|---|---|---|
| 3 | 3 | |
| 3 | ||
| 20 | ||
| 200 | ||
| 0 | 0 | 0 |
With causal attention and 32 buckets, gives bucket 0 and gives 5.
Designing the interface
Section titled “Designing the interface”def noise_span_counts(length: int, noise_density: float, mean_noise_span: float) -> tuple[int, int]: ...def random_spans_noise_mask(length, noise_density, mean_noise_span, rng) -> NDArray: ... # bool [L]def span_corrupt(ids, noise_density, mean_noise_span, sentinel_start_id, rng, eos_id=None) -> tuple[NDArray, NDArray]: ...def t5_relative_bucket(rel_pos, bidirectional: bool, num_buckets=32, max_distance=128) -> NDArray: ...class T5RelativeBias(Module): def __init__(self, n_heads, num_buckets=32, max_distance=128, bidirectional=True, rng=None): ... def forward(self, q_len: int, k_len: int, q_offset: int = 0) -> Tensor: ... # [H, q, k]The corruption side splits into counts, mask, and corruption, so each layer of the “how many, then where, then what” decision can be tested on its own. The position side is a pure bucket function plus a module that owns the learned table.
Building it step by step
Section titled “Building it step by step”The reasoning runs as follows.
- An encoder-decoder’s decoder writes sequences, so the corruption should produce a sequence to write. Dropping whole spans and asking for them back does that, and the targets are short: about tokens instead of .
- The decoder must know which hole each span came from, so each span gets its own marker: sentinel in the inputs where span was, and before span in the targets. Putting the targets back into the inputs at the sentinels recovers the text exactly, so nothing is lost.
- The sentinels must not collide with real ids. T5 takes them from the top of the vocabulary counting down (), so the solution refuses any id inside the sentinel range it is about to use.
- “How many” is decided before “where”: and , clamped so every span and every gap has at least one token. In the first worked example , .
- “Where” is two random compositions: into positive parts and into positive parts, interleaved keep, noise, keep, noise. A uniform composition of into positive parts is a choice of of the cut points, which a shuffle of flags makes. Fixing the shuffle (Fisher-Yates from the end, one
belowper swap, noise first) makes a seed replay the batch. - For positions, attention logits get a learned scalar per head that depends only on . Exact buckets for small and logarithmic buckets for large ones spend the table where offsets are distinguishable and stop at
max_distance, beyond which all offsets share the last bucket, so the scheme extends to any length. - To load T5 checkpoints, the bucket function must reproduce Hugging Face’s float32 arithmetic and truncation, bucket for bucket.
Counts
Section titled “Counts”def noise_span_counts(length, noise_density, mean_noise_span): # ... length >= 2, 0 < density < 1, mean span >= 1 ... n_noise = int(np.round(length * noise_density)) # numpy rounds half to even: round(2.5) = 2 n_noise = min(max(n_noise, 1), length - 1) # at least one noise and one kept token n_spans = max(int(np.round(n_noise / mean_noise_span)), 1) # Every span needs at least one token, and so does every gap before one. n_spans = min(n_spans, n_noise, length - n_noise) return n_noise, n_spansRandom composition
Section titled “Random composition”def _segment_lengths(n_items, n_segments, rng): """Split n_items into n_segments positive lengths, uniformly at random.""" # n_items - 1 gaps, n_segments - 1 of them start a new segment; the gaps # are shuffled with Fisher-Yates from the end, one rng.below per swap. starts = [i < n_segments - 1 for i in range(n_items - 1)] for i in range(len(starts) - 1, 0, -1): j = rng.below(i + 1) starts[i], starts[j] = starts[j], starts[i] first = np.array([False] + starts, dtype=bool) # item 0 always opens segment 0 seg = np.cumsum(first) # segment index of every item return np.bincount(seg, minlength=n_segments).astype(np.int64)Trace the two-span example. The noise flags start as T F F; below(3) = 0 swaps positions 2 and 0, and below(2) = 1 swaps 1 with itself, giving F F T. Prepending False and taking the cumulative sum gives segment ids , so the lengths are .
The mask and the corruption
Section titled “The mask and the corruption”def random_spans_noise_mask(length, noise_density, mean_noise_span, rng): n_noise, n_spans = noise_span_counts(length, noise_density, mean_noise_span) noise = _segment_lengths(n_noise, n_spans, rng) # noise lengths are drawn first keep = _segment_lengths(length - n_noise, n_spans, rng) # keep_1, noise_1, keep_2, noise_2, ...: the text starts with kept tokens # and ends with a noise span. mask = np.zeros(length, dtype=bool) pos = 0 for k, x in zip(keep, noise): pos += int(k) mask[pos : pos + int(x)] = True pos += int(x) return maskdef span_corrupt(ids, noise_density, mean_noise_span, sentinel_start_id, rng, eos_id=None): # ... 1-D integer check ... mask = random_spans_noise_mask(a.size, noise_density, mean_noise_span, rng) n_spans = int(np.sum(mask[1:] & ~mask[:-1]) + mask[0]) # count rising edges lowest = sentinel_start_id - n_spans + 1 if lowest < 0: raise ValueError(...) # not enough ids below sentinel_start_id if np.any((a >= lowest) & (a <= sentinel_start_id)): raise ValueError(...) # a real id inside the sentinel range: ambiguous targets inputs, targets = [], [] k = -1 for t in range(a.size): if mask[t]: if t == 0 or not mask[t - 1]: # a new noise span: sentinel k k += 1 inputs.append(sentinel_start_id - k) # counting DOWN from the top targets.append(sentinel_start_id - k) targets.append(int(a[t])) else: inputs.append(int(a[t])) if eos_id is not None: inputs.append(int(eos_id)) targets.append(int(eos_id)) return np.array(inputs, dtype=np.int64), np.array(targets, dtype=np.int64)Buckets
Section titled “Buckets”def t5_relative_bucket(rel_pos, bidirectional, num_buckets=32, max_distance=128): rel = np.asarray(rel_pos, dtype=np.int64) # rel = key - query n = int(num_buckets) out = np.zeros(rel.shape, dtype=np.int64) if bidirectional: n //= 2 out += (rel > 0).astype(np.int64) * n # keys after the query: upper half rel = np.abs(rel) else: rel = -np.minimum(rel, 0) # a causal decoder only sees keys at or before the query max_exact = n // 2 # ... need max_exact >= 1 and max_distance > max_exact ... is_small = rel < max_exact # T5's float32 arithmetic (as torch computes it), truncated toward zero. # Small offsets take the exact branch, so they are lifted to max_exact # here only to keep log away from 0. logr = np.log(np.maximum(rel, max_exact).astype(np.float32) / np.float32(max_exact)) large = max_exact + ( logr / np.float32(math.log(max_distance / max_exact)) * np.float32(n - max_exact) ).astype(np.int64) # astype truncates toward zero large = np.minimum(large, n - 1) # everything past max_distance: last bucket return out + np.where(is_small, rel, large)For (bidirectional, 32 buckets), and max_exact ; , truncated to 2, so the bucket is 10, as in the table.
The bias module
Section titled “The bias module” def forward(self, q_len, k_len, q_offset=0): # ... range checks ... # A query at row r of this call is at absolute position q_offset + r (cached decoding). q = np.arange(q_len, dtype=np.int64)[:, None] + q_offset k = np.arange(k_len, dtype=np.int64)[None, :] buckets = t5_relative_bucket(k - q, self.bidirectional, self.num_buckets, self.max_distance) bias = self.relative_attention_bias(buckets) # [q, k, heads], an embedding gather return F.permute(bias, (2, 0, 1)) # [heads, q, k], added to the attention logitsBecause the lookup is an Embedding, backward scatters the upstream gradient into the bucket rows, summing over every pair that shares a bucket. The full solution is in t5.py.
Why this design
Section titled “Why this design”Corruption costs with fewer than below draws (one per swap of each shuffle). The bias is bucket lookups and an tensor; Hugging Face computes it once in layer 0 and reuses it in every layer.
Count-then-place beats flipping a coin per token: the number of noise tokens and spans is fixed by the length, so target lengths are predictable for padding and the mean span is exactly controlled. Composition by shuffled flags beats drawing cut points with rejection: it is uniform, uses a fixed number of draws, and is easy to replay in another language. Float32 bucket arithmetic is chosen over exact rationals because the buckets must agree with the code pretrained T5 tables were trained with, including where truncation lands. The tradeoff accepted is that all offsets past max_distance share one bucket, so very long-range order is coarse; RoPE and ALiBi make different tradeoffs.
Testing it
Section titled “Testing it”A correct solution has to pass these checks.
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| the one-span example | unit | the worked example, with and without eos_id | everyone agrees on the layout |
| the two-span example with scripted draws | unit | the six scripted draws, in order, give the worked inputs and targets | the shuffle and the sentinel order |
| the bucket table | unit | the worked bucket table, both directions | the bucket formula |
| inputs and targets restore the text | property | 40 random texts: putting spans back gives the ids | the objective loses nothing |
| counts and lengths | property | , (half to even), mask starts kept and ends noisy, sequence lengths | the batch shapes a trainer pads to |
| short texts never make empty spans | boundary | the upper clamp of on 10 and 6 tokens | no empty span or gap |
| draws follow the spec | unit | the exact below calls; same seed, same output | reproducible data |
| sentinels and validation | boundary | sentinels count down; colliding ids, no room, bad densities, raise | unambiguous targets |
| buckets match Hugging Face | golden | five settings over against HF’s _relative_position_bucket | pretrained T5 tables index correctly |
| bucket properties | property | monotone, in range, disjoint halves, exact while small | the coarsening is a coarsening |
| the bias matches Hugging Face | golden | HF compute_bias, encoder and decoder with past tokens | the bias each head adds |
| the bias gradient lands on buckets | property | the gradient is a scatter-add of the upstream gradient by bucket | training the table |
What can go wrong
Section titled “What can go wrong”The common mistakes are sign conventions, rounding, and anything that changes the order of random draws.
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| relative position as query minus key, or a causal decoder that gives future keys their distance | past and future swapped: pretrained biases read the wrong side | rel = k - q; causal uses -min(rel, 0) |
| bidirectional buckets not halved | the two directions overlap; HF disagrees past offset 8 | n //= 2 and keys after the query add n |
| rounding half up; no upper clamp on the span count | 3 noise tokens where T5 has 2; empty spans on short texts | np.round (half to even); n_spans = min(n_spans, n_noise, length - n_noise) |
| sentinels counting up, or targets without them | ids collide with real tokens; the decoder cannot tell spans apart | sentinel_start_id - k written to both inputs and targets |
| a text that starts with a noise span | the first tokens always corrupted, unlike T5 | the layout loop adds a keep length before every noise length |
| the shuffle’s range or draw order changed | a seed no longer replays the batch | Fisher-Yates from the end with below(i + 1), noise lengths drawn before keep lengths |
Warning: Python’s built-in
roundand numpy’snp.roundboth round half to even, but many other languages round half away from zero. If you portnoise_span_counts, port the rounding rule too, or , will give 3 noise tokens instead of T5’s 2.
Where this leads
Section titled “Where this leads”Nothing later in the book calls this code. The ideas, though, recur: RoPE encodes position by rotation, and ALiBi uses a fixed linear penalty with no table, two different answers to the question T5’s buckets answer with a learned table.
In production, T5’s span_corruption preprocessor (t5/data/preprocessors.py) and Hugging Face’s DataCollatorForT5MLM (transformers/examples/flax/language-modeling/run_t5_mlm_flax.py) add packing to fixed input and target lengths, EOS handling, and expand_inputs_and_targets to choose the raw length. UL2’s mixture of denoisers trains short spans, long spans, and prefix LM in one model, chosen by a mode token. Hugging Face’s T5Attention.compute_bias (transformers/models/t5/modeling_t5.py) computes the bias once in layer 0 and reuses it in every layer.
Further reading:
- Raffel et al., “Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer” (2020), sections 2.1, 3.1.3, and 3.3.
- Shaw, Uszkoreit, and Vaswani, “Self-Attention with Relative Position Representations” (2018).
- Tay et al., “UL2: Unifying Language Learning Paradigms” (2022).
Key terms
Section titled “Key terms”- Span corruption: replacing whole spans of the input with sentinels and training the decoder to write the spans back.
- Sentinel: a reserved id marking one dropped span, counting down from the top of the vocabulary.
- Random composition: a uniform split of items into positive parts, made by shuffling cut-point flags.
- Relative position bucket: an index into a learned bias table, exact for small offsets and logarithmic for large ones.
q_offset: the absolute position of the first query in a call, the number of tokens already cached.
Summary
Section titled “Summary”- Span corruption hides about 15% of the tokens in a few whole spans, one sentinel id per span; inputs plus targets hold every original token exactly once.
- The counts are fixed by the length, the density, and the mean span (round half to even), and the mask always starts with kept tokens and ends with a span.
- The random choices are two Fisher-Yates shuffles with one
belowdraw per swap, noise lengths first, so a seed replays the batch. - T5 has no position embeddings: each head adds a learned bias indexed by a bucket of the key’s offset, exact when small and logarithmic up to
max_distance. - A causal decoder folds every future offset into bucket 0 and uses all buckets for the past.
Review questions
Section titled “Review questions”- Why does the mask always start with kept tokens and end with a noise span?
Answer
The layout interleaves $K$ keep lengths and $K$ noise lengths starting with a keep, and every length is positive. That is T5's convention; a mask starting with noise would always corrupt the first tokens. - Why must be clamped to and not just to at least 1?
Answer
Each of the $K$ spans needs at least one noise token and each of the $K$ gaps at least one kept token. On a short text, $\mathrm{round}(N/\mu)$ can exceed $L - N$, and `_segment_lengths` would then need more positive parts than items, producing an empty gap. - Why do sentinels count down from
sentinel_start_idand why doesspan_corruptrefuse ids in that range?Answer
T5's sentinels are the last vocabulary ids, `` $= V - 1$ and so on downward. If a real token had a sentinel's id, the targets could not be split back into spans unambiguously. - In a causal decoder, why can every future offset map to bucket 0?
Answer
The causal mask removes those keys from the softmax, so their bias is never used. Folding them into one bucket frees the whole table for past offsets. - What does
q_offsetfix during cached decoding?Answer
With a cache, the new query is at absolute position `q_offset` while keys run from 0. Without the offset, the query would be treated as position 0 and every relative distance would be wrong.