XLNet
Masked language modelling buys its two-sided view of a sentence with two compromises: it writes [MASK] symbols into text that will never contain them, and it guesses each hidden word independently, as if the other blanks were already filled. XLNet keeps the two-sided view and pays neither price.
Permutation language modelling
XLNet predicts tokens one at a time, left to right — but over a random ordering of the positions rather than the written order. That single change does a lot of work:
- Every token becomes a prediction target across sampled orders, so no part of the sentence sits idle.
- Targets are predicted in sequence, so dependencies between them survive — the two halves of New York are not learned in isolation.
- The input is never altered, so pretraining and fine-tuning see the same kind of text.
Making it work
Ordinary self-attention cannot express “predict position five knowing where it is but not what it says”, so XLNet runs two-stream self-attention: a content stream that sees each token, and a query stream that sees only its position. From Transformer-XL it inherits segment recurrence and relative position encodings, giving it a longer working context than BERT's fixed window — which shows on long documents and SQuAD passages.
Why it matters
On matched training data XLNet beat BERT across GLUE, SQuAD and reading-comprehension benchmarks, and it made a point that outlasted the leaderboard: bidirectional context comes from the training objective, not from masking. The xlnet-base-cased checkpoints in Hugging Face Transformers fine-tune like any other encoder, though the two-stream machinery makes them heavier to train than a comparable BERT.
XLNet is a transformer pretrained by permutation language modelling: it predicts each token autoregressively under randomly sampled orderings of the sequence, so every prediction is conditioned on context from both sides without inserting [MASK] tokens. That removes BERT's pretrain–finetune mismatch and its assumption that masked tokens are independent, and the Transformer-XL backbone extends usable context length. It set state-of-the-art results on GLUE, SQuAD and long-document reading comprehension.
Picture two ways of testing whether someone really understands a sentence. In the first, you blank out a few words and ask them to fill the gaps. In the second, you hand them the words in a jumbled order, one at a time, and ask them to predict what comes next — so by the end they have reasoned about every word, using whatever else they have already been shown.
XLNet learns the second way. Shuffling the order lets it use clues from both before and after a word, without ever having to blank anything out. The result is a model that reads sentences — and long passages — with an unusually complete sense of context.