Transformer-XL (2019) › The Solution

Michael BrenndoerferJune 21, 20252 min read
History of Language AIAll chapters
Part 5 of 9Transformers & Pretraining
Chapter 64Transformer-XL (2019) › The Solution
Reading section 3 of 7 · The Solution · 2 minSection 3 of 7 · 2 min

Book contents

Section 3 of 7 · 2 min read

Transformer-XL (2019) › The SolutionOne section · Sections in this chapter3 / 7 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let hτn\mathbf{h}_{\tau}^{n} denote the hidden states at layer nn for segment τ\tau. When processing the next segment, layer nn receives a memory made from earlier layer activations and the current segment:

h~τ+1n−1=[SG⁡ ⁣(hτn−1) ∥ hτ+1n−1],\widetilde{\mathbf{h}}_{\tau+1}^{n-1} = \left[ \operatorname{SG}\!\left(\mathbf{h}_{\tau}^{n-1}\right) \,\Vert\, \mathbf{h}_{\tau+1}^{n-1} \right],

where SG⁡\operatorname{SG} stops gradients through the cached segment and ∥\Vert denotes concatenation. Queries come from the current segment. Keys and values come from the concatenated memory, so a current token can attend to earlier cached states.

Stopping the gradient is an important boundary. Information from the cache affects the forward pass, but backpropagation does not continue through the entire document. Training remains segment-based rather than becoming full-document backpropagation through time.

The memory length is a configuration choice. With current length LL and memory length MM, attention for one layer is proportional to L(L+M)L(L+M). Reusing cached states avoids recomputing them, but attending to a larger memory still costs time and storage. Production implementations normally keep a fixed-size cache and discard older states.

Relative Attention

Transformer-XL decomposes an attention score into content and position terms. The position term depends on the distance i−ji-j between a current query at ii and a key at jj. It uses a sinusoidal relative-position vector together with learned projection and bias parameters. It is not a learned lookup embedding for every possible distance.

This formulation solves the cache mismatch because a stored state has no single new absolute index. Its positional contribution is recomputed from its distance to each current query. The model can also evaluate a longer attention length than the segment length used during training, although performance at unseen distances remains an empirical question rather than a guarantee.

Reported Results

The paper reported new results on five language-modeling datasets, including perplexity 18.3 on WikiText-103 and 21.8 on One Billion Word. Its relative effective context length was measured as 450% longer than the vanilla Transformer baseline in the paper's setup.

The reported evaluation speedup of up to 1,800 times compared Transformer-XL's cached generation with a vanilla Transformer that recomputed a long context for each prediction. It was not a claim that Transformer-XL attention was 1,800 times cheaper for every workload.