TL;DR
Permutation language modeling is a way to pre-train a language model that keeps the autoregressive rule (predict each word from the words before it) but changes what counts as "before". Each training step picks a random order in which the positions of a sentence are revealed, and the model learns to predict each word from the words revealed earlier in that order. Averaged over many orders, every word gets to be predicted from context on its left and on its right, and the input never contains a special mask token.
Builds on: Autoregressive Model, Masked Language Modeling · Leads to: XLNet: Generalized Autoregressive Pretraining for Language Understanding
Where it's used
Permutation language modeling is a pre-training objective, so you meet it through the models it produced rather than on a product screen.
Pre-training an encoder for understanding tasks. The XLNet paper (Yang and colleagues, 2019) pre-trained a large Transformer on a large collection of books, Wikipedia and web text, then fine-tuned it on question answering (SQuAD), reading comprehension (RACE) and the GLUE benchmark of sentence-level tasks. In the paper's comparison it beat BERT on 20 tasks. These are tasks where the model reads a whole passage and must judge or extract something from it, so context on both sides of a word matters.
Question answering over a passage. Take the question "Who wrote the report?" and a passage that names the author in the middle of a sentence. To answer, the model has to weigh the words before the name and the words after it. A model trained only to read left to right has seen just the first half when it reads the name. Permutation training is one way to give a single network both halves without a masked input.
Choosing an objective for a new model. A team building a text generator usually keeps the plain left-to-right objective, because that is exactly the order in which the text will be produced. A team building an encoder for classification or search has to choose between masked and permutation objectives. The choice depends on whether the extra training cost pays for itself on the team's own tasks.
How it works
Trace one tiny sentence through the process. The sentence is "New York is big", so there are four positions: 1 holds "New", 2 holds "York", 3 holds "is", and 4 holds "big".
- Start with the sentence in its normal order. The input is not shuffled: position 2 still holds "York". Check: reading the four words left to right gives back the sentence.
- Draw a random order of the four positions. Say the draw is 3, 1, 4, 2. The model will reveal position 3 first, then 1, then 4, then 2. Check: every position appears exactly once in the order.
- Predict the first revealed word, position 3 ("is"), with nothing else visible. The model outputs a probability for each word in its vocabulary and is scored on the probability it gave "is". Check: this is the first factor of an ordinary left-to-right model.
- Reveal position 1 and predict "New" given only the already revealed "is". Positions 2 and 4 are still hidden. Check: the visible set is 3.
- Reveal position 4 and predict "big" given "is" and "New". Check: the visible set is 3, and "big" sits to the right of the word it is predicted from.
- Predict position 2 ("York") given "is", "New" and "big". This is the first step where the target word has context on both sides. Check: the visible set is 4.
- Add up the four log-probabilities. That sum is the score for this order. Training averages the score over several random orders of the same sentence, and gradient steps push the model to give the true word more probability in every order.
The random orders are permutations of the positions, which is where the name comes from. Here is the whole loop as a diagram.
flowchart LR A[Sentence in normal order] --> B[Sample a random order of positions] B --> C[Reveal words in that order] C --> D[Predict each word from the words already revealed] D --> E[Sum the log-probabilities for this order] E --> F[Average over orders and update weights]
Two details make step 3 and step 6 work inside a Transformer. The model must know which position it is predicting, otherwise it cannot use the same weights for every order. But it must not see the word at that position, otherwise the prediction is a copy. The model handles both with two streams of representations, described in "The math" below.
The math
Definition
Let be a sequence of tokens. Let be the set of all orders of the positions . For one order , the position is the one revealed at step , so is the word predicted at that step. The words revealed before step are written .
For a sequence and a sampled order of its positions, the model factorises the probability of the sequence with the chain rule in that order, and is trained to maximise the expected log-likelihood over orders:
Here are the model's parameters, is the natural logarithm, and is the average over the random order.
Read the formula in words: for each order, add up the log-probability of each word given the words revealed before it, then average that sum over orders. Inside a single order the sum is exactly the autoregressive factorisation from Autoregressive Model, because the conditional at each step depends only on the earlier steps.
Step 1 in symbols: the chain rule holds for any order
The chain rule says that for any order ,
For two words, the orders and both give , because . The orders disagree only when the model's conditionals are imperfect. The training objective averages over orders so that the model's conditionals agree with each other across orders.
Step 2 in symbols: sampling the order
Each training sequence gets an order drawn uniformly at random from . There are orders, which grows fast: for four words and for ten. The expectation is therefore estimated by sampling one or a few orders per sequence rather than summing over all of them.
Step 3 in symbols: which words a prediction may read
In an ordinary autoregressive Transformer, a fixed triangular mask blocks each position from reading the positions after it. Permutation training uses a mask built from the sampled order. Write for the step at which position is revealed. The query stream described in Step 4 at position may read position only when
Because the mask changes with each order, the same weights are trained to handle all of them.
Step 4 in symbols: two streams
The model keeps two representations for each position. The content stream includes the word at its own position, and is used as the context that later predictions read. The query stream knows the position of the target but not its word. Let index the layers, and let mean the positions revealed up to and including step . is the usual operation from Attention Mechanism: each query is compared with the keys , and the result is a weighted sum of the values . The two streams are updated as
The query at reads the content of earlier positions in the order, but not its own content, which is why the prediction cannot copy the answer. The final prediction uses the query stream and the embedding of each candidate word :
The denominator is the Softmax normaliser over the vocabulary, so the probabilities add to 1.
Step 5 in symbols: predict only the end of each order
Early positions in an order have very little context, so their predictions give a weak training signal. The authors therefore pick a cut point and replace the full sum of the Definition with a sum over only the later steps:
Choosing so that only a fraction of the tokens are predicted trades some training targets for stronger ones.
Worked examples
- The sentence is "New York", so is "New" and is "York". There are two orders, and , each with probability .
- The model's table gives , , and .
- Order : the product is . Its log is .
- Order : the product is . Its log is .
- The average over the two equally likely orders is .
- Both orders give the same joint probability, 0.2, because the table is consistent: . A perfect model gives the same answer for every order, so the average adds nothing; a real model does not, and averaging pushes it toward consistency.
Take "New York is big" with the sampled order . The model gives with nothing visible, , , and .
- Step 1 reveals position 3, "is", with nothing visible. The factor is , and its log is .
- Step 2 reveals "New" given "is". The factor is , and its log is .
- Step 3 predicts "big" given "is" and "New". The factor is , and its log is .
- Step 4 predicts "York" given all three other words. The factor is , and its log is .
- Multiply the factors: .
- Add the logs: , which matches up to rounding.
- The score for this order is about nats. Training averages it with the scores of other sampled orders. The last step had the most context, which is why predicting the end of each order is the useful part.
Take the sentence "New York is a city", with positions 1 New, 2 York, 3 is, 4 a, 5 city. There are orders, and all are equally likely.
- York is predicted last in orders. In those orders it sees all four other words, which is the strongest context the sentence can give. That is of the orders.
- In other orders York sees fewer words. Take the order . York is predicted fourth, after "is", "New" and "city". It sees words on both sides but not "a".
- The question is how often York sees exactly "New" and "is", and nothing else. Those two words must come before York in the order, and "a" and "city" must come after it. Within the first two steps "New" and "is" can come in either order, and "a" and "city" can come in either order after York, which gives orders.
- Those 4 orders are out of 120, so the probability is .
Try it: using the same 120 orders, how many have York seeing exactly "is" and "city", with "New" and "a" hidden? Answer: the two visible words must come before York, and the two hidden words after it, so the count is orders out of 120, or . The count has the same shape as step 3 with the two groups swapped, which shows that a word sees a specific set of neighbours in only a small share of orders.
Common mistakes and tradeoffs
It is tempting to think the model reads the sentence in a random order. In fact the input keeps its normal order and its positional information. Only the order of prediction changes, and a mask decides which words each prediction may read. This is why the Positional Encoding of each word still says where it sits in the sentence.
It is tempting to think the target word is visible when it is predicted. In fact the query stream sees the target's position but not its word. If it could see the word, the model would learn to copy the input and would learn nothing useful.
It is tempting to call permutation training a bidirectional model like BERT. In fact each single conditional is one-directional, because it reads only the words earlier in its own order. The two-sided context emerges only when you look across many orders. A single prediction still sees only part of the sentence.
The method has real costs. It needs two streams of attention, sampled orders and extra masks, so each training step is more expensive than plain autoregressive training. Predicting only the end of each order means fewer training targets per sentence. Its advantage is that the input never contains a special mask token, so the text the model sees in pre-training matches the text it sees in fine-tuning.
Use it when you are pre-training an encoder for understanding tasks, where two-sided context helps and you can afford the extra compute. Do not use it when the model must generate text one word at a time at serving time: a plain autoregressive model with a KV Cache is simpler and does that natively. Masked language modeling is the cheaper alternative for encoders.
History
By 2018 the field had two strong recipes. Autoregressive language models predicted the next word and generated text well, but read only one direction. BERT hid some words and predicted them from both sides, which gave strong representations for understanding tasks. But BERT's mask token never appears in real text, and it predicts the hidden words independently of each other.
In June 2019 Zhilin Yang and colleagues proposed permutation language modeling in the XLNet paper. It was built on Transformer-XL, a Transformer that handles longer context through segment-level recurrence, from the same group. XLNet reported better results than BERT across its benchmark comparison. The idea shows that two-sided context can be learned without a mask token. It did not become the default recipe. Later work mostly kept masked encoders for understanding and left-to-right decoders for generation.
Recap
- An Autoregressive Model factors a sequence into one-step predictions, and the chain rule holds for any order of the positions.
- Permutation language modeling samples a random order of positions and predicts each word from the words revealed before it in that order.
- Averaged over orders, each word is predicted from context on both sides, and the input never contains a mask token.
- The query stream lets the model know a target's position without seeing its word, so the prediction cannot copy the answer.
- Only the end of each order is predicted, and the objective is an expected log-likelihood over orders.
- Read next: XLNet: Generalized Autoregressive Pretraining for Language Understanding, the paper that introduced the method, and BERT: Pre-training of Deep Bidirectional Transformers for the masked alternative it was compared against.
Check yourself
QuestionIn permutation language modeling, is the input sentence shuffled before the model reads it? Explain briefly.Show answer
No. The input keeps its normal order, and positional information still says where each word sits. Only the order in which the words are predicted changes, and an attention mask decides which words each prediction may read.
QuestionA three-word sentence is factorised in the order (2, 3, 1). The model gives , and . What is the probability of this factorisation, and what is its natural log?Show answer
The probability is . Its natural log is nats.
QuestionWhy does the query stream exist alongside the content stream?Show answer
The model must know which position it is predicting, because the same weights are reused for every order. But it must not see the word at that position, otherwise the prediction is a copy of the input. The query stream carries the target position without the target word, while the content stream carries the words.
QuestionA team wants a model that writes a reply to a customer email one word at a time. Would permutation language modeling be their first choice? Explain.Show answer
No. The model generates text in a fixed left-to-right order, and a plain autoregressive model does that directly, with a KV cache for speed. Permutation training is aimed at pre-training encoders for understanding tasks, where two-sided context helps, and it adds training cost that the generator does not need.