Paper 62~11 min readACL 2019worked math + runnable code
Paper Breakdown

Transformer-XL,
explained.

Early transformers had a strange amnesia: they read text in fixed-length chunks and forgot everything the moment a chunk ended. A dependency that reached across the boundary was simply severed. Transformer-XL gave the model a memory — cache the previous chunk's hidden states so the next one can look back — and, because that trick breaks absolute positions, paired it with a relative way to encode distance. The result reached far longer context and evaluated hundreds of times faster. Here's the recurrence, the positions, and the math.

Video breakdown
The animated walkthrough is in production.
Read the full breakdown below in the meantime ↓
01

The fixed-context problem

A standard transformer attends over a fixed-length window — a segment of, say, 512 tokens. To process a long document you chop it into segments and run each one independently. That creates two problems. First, the model can never learn a dependency longer than one segment, because information never crosses a boundary — the paper calls this context fragmentation. Second, at evaluation time, to predict each new token you slide the window forward by one and recompute the whole segment from scratch, which is enormously wasteful.

Both problems come from the same root: each segment is an island. Transformer-XL's fix is to build bridges between the islands — let a segment see the one before it — without paying to reprocess the past. Two mechanisms make that work, and they turn out to depend on each other.

The one-sentence version

Cache the previous segment's hidden states so each layer reaches one segment further back, and use relative positions so the recurring segments don't collide — long context, fast evaluation.

02

Segment recurrence

The first mechanism is segment-level recurrence. When processing a segment, Transformer-XL caches the hidden states it produced for the previous segment, at every layer. The current segment's attention can then read from its own tokens and from those cached states — extending its view one segment into the past. The cache is treated as fixed input (stop-gradient: no backpropagation flows into it), so training stays cheap.

The elegance is how this compounds with depth. Layer 1 of the current segment reaches back into layer 1 of the previous segment. But layer 2 reads layer-1 states that already incorporated the segment before that. So each additional layer extends the reach by roughly another segment — the effective context grows about linearly with the number of layers, not staying pinned at a single segment length. A 12-layer model with 512-token segments can carry information across thousands of tokens.

03

Relative positions

Recurrence breaks something. The original transformer adds an absolute position embedding — position 0, 1, 2, … within the segment. But with recurrence, the same absolute indices reappear in every segment: the cached previous segment also has a "position 3." Add absolute embeddings and two tokens from different segments look identically placed; the model can't tell the recent "position 3" from the older one. Recurrence and absolute positions are incompatible.

The fix is relative positional encoding: encode the distance between a query and a key, not their absolute indices. Distance is well-defined across segment boundaries — a token 5 steps back is 5 steps back whether it's in this segment or the last:

absolute (breaks): pos(i) collides across segments  ·  relative (works): rel(i, j) = i − j is shift-invariant

Because rel(5,3) = rel(105,103) = 2, the encoding is identical no matter where in the stream the pair sits — exactly the property recurrence needs. Transformer-XL folds this relative distance directly into the attention score computation. The runnable version below confirms the shift-invariance and the context growth.

04

The evaluation speedup

Recurrence pays a second dividend at inference. A vanilla transformer, generating token by token, slides its fixed window forward one step at a time and recomputes the entire context for each new token — order L work per token for a segment length L. Transformer-XL already has the previous segment's states cached, so it processes a new segment once and reuses the cache — no re-computation of the past.

Cost to evaluate a long stream
VanillaRe-process the full L-token context for every new token → cost ~ tokens × L.
Transformer-XLProcess each token once, reuse the cache → cost ~ tokens.
SpeedupRoughly faster evaluation — the paper reported over 1,800× on its settings.

So the same cache that extends the context also removes the redundant recomputation that made long-context evaluation painfully slow. Longer reach and faster inference from one idea — carry the past forward instead of rebuilding it.

RUN IT YOURSELF

Context growth and the speedup

Both of Transformer-XL's wins are simple to compute. A vanilla transformer's context is capped at the segment length, but caching one segment per layer makes the effective context grow with depth — 12 layers of 512-token segments reach ~6,144 tokens back. Relative distance is shift-invariant, so rel(5,3) equals rel(105,103), which is what lets recurring segments coexist. And reusing the cache turns per-token recomputation (cost ~ tokens × L) into a single pass (cost ~ tokens), an ~L× evaluation speedup. Change the depth or segment length and watch the reach and speedup move.

CPython · WebAssembly
05

What it achieved

Transformer-XL delivered concrete gains that mattered at the time:

ResultDetail
Much longer dependenciesLearned dependencies far beyond a single segment — reportedly ~4.5× longer than a vanilla transformer of the same segment size.
State-of-the-art language modelingNew bests on long-context benchmarks like WikiText-103 and enwik8 at release.
~1,800× faster evaluationCache reuse removed the per-token recomputation of the vanilla sliding window.
No context fragmentationInformation flows across segment boundaries, so coherence spans the whole document.

It was, for a period, the standard for long-sequence language modeling, and it seeded XLNet (which built its permutation language modeling on the Transformer-XL backbone). Its ideas became reference points for how to think about memory and position in transformers.

06

Its legacy

Two of Transformer-XL's contributions outlived the specific model. Relative positional encoding became a lasting theme — the recognition that transformers should reason about distance rather than absolute index runs directly into RoPE and the relative-bias schemes used across modern models. And the idea of a recurrent memory — carrying compressed past state forward instead of re-attending to raw tokens — recurs in memory-augmented and state-space models aiming at very long context.

The broader arc is instructive. Long context has been attacked from several directions: sparse attention (Longformer), efficient exact attention (FlashAttention), and recurrence/memory (Transformer-XL). Each trades differently, and the field still borrows from all three. Transformer-XL's specific answer — recur across segments, encode position relatively — was an early, influential statement that a transformer's context needn't end where its window does.

Worth knowing

The stop-gradient on the cache is what keeps recurrence affordable: the model attends across many segments at inference, but only backpropagates through the current one during training — long reach, bounded training cost.

Frequently asked

Quick answers

What are Transformer-XL's two ideas?

Segment-level recurrence (cache the previous segment's hidden states so context grows with depth) and relative positional encoding (encode distances, not absolute indices).

What is context fragmentation?

Processing text in independent fixed-length segments, so dependencies crossing a boundary are lost. Transformer-XL removes it by carrying state across segments.

Why relative positions?

Recurrence reuses the same absolute indices in every segment, which would collide. Relative distance (i−j) is shift-invariant, so it stays coherent across boundaries.

Why is evaluation faster?

Cached states are reused instead of recomputing the whole context per new token — turning O(tokens×L) into O(tokens), an ~L× speedup.

Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context · Dai, Yang, Yang, Carbonell, Le, Salakhutdinov · ACL 2019 · read the original paper on arXiv → · Vibe Engines · 2026
Finished this one? 0 / 101 Paper Breakdowns done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns