Transformers Needs review

XLNet

Year of appearance
2019
Researchers
Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le
Organisation
Carnegie Mellon University, Google Brain
Architecture type
Autoregressive Transformer-XL encoder
Learning mechanism
self-supervised
Tasks
Text classification, Question answering

An autoregressive pre-training model that maximises the expected likelihood over all permutation orders of the sequence, so it gets bidirectional context without a [MASK] token.

Architecture diagram

Displayed configuration: XLNet-base: L = 12, d = 768, 12 heads — A Transformer-XL encoder with two-stream attention.

Layer table (7 nodes)
LayerTypeInputOutputRepetitionParametersFormulaMACs
Tokensinput12810—
Token embedding 32,000 × 768embed11 × 768124,576,000From the reference PyTorch implementation0
Previous segment memory (Transformer-XL)statem × 76810—
Two-stream relative attentionmha1 × 7681 × 768122,954,496From the reference PyTorch implementation503,316,480
FFN with GELU + Add & Normffn1 × 7681 × 768124,723,968From the reference PyTorch implementation603,979,776
Representations / token probability in the permutation orderoutput128 × 768128 × 76810—
Mask vector (mask_emb)token76817681 × 1 × 768—

Configuration figures: XLNet-base: L = 12, d = 768, 12 heads

Parameters (computed from the layers)116,718,336 (≈116.72M)
Parameters (published in the source)Unspecified
MACs13,287,555,072 (≈13.29B)
FLOPs ≈ 2 × MACs26,575,110,144 (≈26.58B)
Input shape
n tokens (+ memory)
Output shape
n × 768
Depth
12 layers
Complexity
O(L·(n·(n+m)·d + nd²)).
MACs note
For a sequence n = 128 without previous memory (content stream only at inference).
Calculation assumptions: XLNetModel from Hugging Face with the xlnet-base-cased configuration; the paper does not state the number of parameters explicitly.

MAC = a multiplication followed by an addition. The figures are for one forward pass of one sample with the stated dimensions; they are an analytical calculation, not a measurement of run time.

Scientific explanation

Historical background

Published at NeurIPS 2019; it outperformed BERT on 20 tasks at the time, and relied on Transformer-XL to handle long contexts.

The problem it addresses

BERT assumes the masked tokens are independent and uses [MASK], which is absent during fine-tuning, while autoregressive models see only one direction.

Core idea

Permutation language modelling: for each random order the token is predicted from the tokens that precede it in that order, with two-stream attention so the model knows the target position without its content.

Architecture description

Transformer-XL (relative attention and segment memory) with a content stream and a query stream that share weights.

Data flow

Tokens → embedding → 12 layers of two-stream relative attention → prediction of the last tokens in the permutation order.

Inputs

A sequence of SentencePiece tokens with optional memory.

Outputs

Contextual representations.

Training procedure

Pre-training on about 33 billion tokens, predicting the last 1/K of the tokens in each order to reduce the cost.

Loss functions

The expected log likelihood over permutations.

Computational complexity

O(L·(n(n+m)d + nd²)) with memory m.

Computational requirements

The large version was trained on 512 TPU v3 chips for about 5.5 days.

Advantages

  • Bidirectional context without [MASK]
  • Long contexts through memory
  • Strong results in 2019

Limitations

  • High implementation complexity
  • Costly training
  • Outperformed by RoBERTa with simpler procedures

Equations

Permutation language modelling objective Permutation language modeling

\max_\theta \; \mathbb{E}_{\mathbf{z} \sim \mathcal{Z}_T}\left[\sum_{t=1}^{T} \log p_\theta\left(x_{z_t} \mid \mathbf{x}_{\mathbf{z}_{\lt t}}\right)\right]

z is a random permutation order of the sequence positions.

Symbols: Z_T: all permutations of length T

Two-stream attention Two-stream attention

\begin{aligned} g_{z_t}^{(m)} &\leftarrow \operatorname{Attention}\big(Q = g_{z_t}^{(m-1)},\, KV = \mathbf{h}_{\mathbf{z}_{\lt t}}^{(m-1)}\big) \\ h_{z_t}^{(m)} &\leftarrow \operatorname{Attention}\big(Q = h_{z_t}^{(m-1)},\, KV = \mathbf{h}_{\mathbf{z}_{\le t}}^{(m-1)}\big) \end{aligned}

The query stream does not see the content of the token it predicts.

Symbols: g: query stream, h: content stream

Applications

Related models

References

  1. Original paper Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. (2019). XLNet: Generalized autoregressive pretraining for language understanding. NeurIPS 2019. arXiv:1906.08237 Verified
Scientific review note: A first scientific draft based on the original paper and reference implementations; it needs review by a specialist before final approval.

Version 1 — last updated 2026-11-15.