Search

Search papers and lessons

Permutation Language Modeling

Level 4 · 14 min read

Permutation language modeling trains an autoregressive model on many random orders of the same sentence, so across those orders each word is predicted from context on both sides.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Pre-training
  • ReadReady to readNot yet

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".

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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 x=(x1,…,xT)\mathbf{x} = (x_1, \dots, x_T) be a sequence of TT tokens. Let ZT\mathcal{Z}_T be the set of all T!T! orders of the positions {1,…,T}\{1, \dots, T\}. For one order z=(z1,…,zT)\mathbf{z} = (z_1, \dots, z_T), the position ztz_t is the one revealed at step tt, so xztx_{z_t} is the word predicted at that step. The words revealed before step tt are written xz<t=(xz1,…,xzt−1)\mathbf{x}_{\mathbf{z}_{\lt t}} = (x_{z_1}, \dots, x_{z_{t-1}}).

Definition: Permutation language modeling objective

For a sequence x\mathbf{x} and a sampled order z\mathbf{z} 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:

max⁡θ  Ez∼ZT[∑t=1Tlog⁡pθ(xzt∣xz<t)].\max_\theta \; \mathbb{E}_{\mathbf{z} \sim \mathcal{Z}_T}\Big[ \sum_{t=1}^{T} \log p_\theta\big(x_{z_t} \mid \mathbf{x}_{\mathbf{z}_{\lt t}}\big) \Big].

Here θ\theta are the model's parameters, log⁡\log is the natural logarithm, and E\mathbb{E} 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 z\mathbf{z},

p(x)=∏t=1Tp(xzt∣xz<t).p(\mathbf{x}) = \prod_{t=1}^{T} p\big(x_{z_t} \mid \mathbf{x}_{\mathbf{z}_{\lt t}}\big).

For two words, the orders (1,2)(1,2) and (2,1)(2,1) both give p(x1,x2)p(x_1, x_2), because p(x1) p(x2∣x1)=p(x2) p(x1∣x2)p(x_1)\,p(x_2 \mid x_1) = p(x_2)\,p(x_1 \mid x_2). 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 z\mathbf{z} drawn uniformly at random from ZT\mathcal{Z}_T. There are T!T! orders, which grows fast: 4!=244! = 24 for four words and 10!=3,628,80010! = 3{,}628{,}800 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 rankz(i)\mathrm{rank}_{\mathbf{z}}(i) for the step at which position ii is revealed. The query stream described in Step 4 at position ii may read position jj only when

rankz(j)<rankz(i).\mathrm{rank}_{\mathbf{z}}(j) \lt \mathrm{rank}_{\mathbf{z}}(i).

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 hh includes the word at its own position, and is used as the context that later predictions read. The query stream gg knows the position of the target but not its word. Let mm index the layers, and let z≤t\mathbf{z}_{\le t} mean the positions revealed up to and including step tt. Attention(Q,K,V)\mathrm{Attention}(Q, K, V) is the usual operation from Attention Mechanism: each query QQ is compared with the keys KK, and the result is a weighted sum of the values VV. The two streams are updated as

hzt(m)←Attention(Q=hzt(m−1),  K,V=hz≤t(m−1)),h^{(m)}_{z_t} \leftarrow \mathrm{Attention}\big(Q = h^{(m-1)}_{z_t},\; K, V = h^{(m-1)}_{\mathbf{z}_{\le t}}\big), gzt(m)←Attention(Q=gzt(m−1),  K,V=hz<t(m−1)).g^{(m)}_{z_t} \leftarrow \mathrm{Attention}\big(Q = g^{(m-1)}_{z_t},\; K, V = h^{(m-1)}_{\mathbf{z}_{\lt t}}\big).

The query at ztz_t 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 e(v)e(v) of each candidate word vv:

pθ(Xzt=v∣xz<t)=exp⁡(e(v)⊤gzt)∑uexp⁡(e(u)⊤gzt).p_\theta\big(X_{z_t} = v \mid \mathbf{x}_{\mathbf{z}_{\lt t}}\big) = \frac{\exp\big(e(v)^\top g_{z_t}\big)}{\sum_{u} \exp\big(e(u)^\top g_{z_t}\big)}.

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 cc and replace the full sum of the Definition with a sum over only the later steps:

∑t=c+1Tlog⁡pθ(xzt∣xz<t).\sum_{t=c+1}^{T} \log p_\theta\big(x_{z_t} \mid \mathbf{x}_{\mathbf{z}_{\lt t}}\big).

Choosing cc so that only a fraction of the tokens are predicted trades some training targets for stronger ones.

Worked examples

Example
A two-word sentence, both orders
  1. The sentence is "New York", so x1x_1 is "New" and x2x_2 is "York". There are two orders, (1,2)(1,2) and (2,1)(2,1), each with probability 1/21/2.
  2. The model's table gives p(New)=0.4p(\text{New}) = 0.4, p(York∣New)=0.5p(\text{York} \mid \text{New}) = 0.5, p(York)=0.25p(\text{York}) = 0.25 and p(New∣York)=0.8p(\text{New} \mid \text{York}) = 0.8.
  3. Order (1,2)(1,2): the product is 0.4×0.5=0.20.4 \times 0.5 = 0.2. Its log is ln⁡0.4+ln⁡0.5=−0.9163−0.6931=−1.6094\ln 0.4 + \ln 0.5 = -0.9163 - 0.6931 = -1.6094.
  4. Order (2,1)(2,1): the product is 0.25×0.8=0.20.25 \times 0.8 = 0.2. Its log is ln⁡0.25+ln⁡0.8=−1.3863−0.2231=−1.6094\ln 0.25 + \ln 0.8 = -1.3863 - 0.2231 = -1.6094.
  5. The average over the two equally likely orders is (−1.6094−1.6094)/2=−1.6094(-1.6094 - 1.6094)/2 = -1.6094.
  6. Both orders give the same joint probability, 0.2, because the table is consistent: 0.4×0.5=0.25×0.80.4 \times 0.5 = 0.25 \times 0.8. 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.
Example
One sampled order for four words

Take "New York is big" with the sampled order z=(3,1,4,2)\mathbf{z} = (3, 1, 4, 2). The model gives p(is)=0.1p(\text{is}) = 0.1 with nothing visible, p(New∣is)=0.2p(\text{New} \mid \text{is}) = 0.2, p(big∣is,New)=0.5p(\text{big} \mid \text{is}, \text{New}) = 0.5, and p(York∣is,New,big)=0.4p(\text{York} \mid \text{is}, \text{New}, \text{big}) = 0.4.

  1. Step 1 reveals position 3, "is", with nothing visible. The factor is 0.10.1, and its log is −2.3026-2.3026.
  2. Step 2 reveals "New" given "is". The factor is 0.20.2, and its log is −1.6094-1.6094.
  3. Step 3 predicts "big" given "is" and "New". The factor is 0.50.5, and its log is −0.6931-0.6931.
  4. Step 4 predicts "York" given all three other words. The factor is 0.40.4, and its log is −0.9163-0.9163.
  5. Multiply the factors: 0.1×0.2×0.5×0.4=0.0040.1 \times 0.2 \times 0.5 \times 0.4 = 0.004.
  6. Add the logs: −2.3026−1.6094−0.6931−0.9163=−5.5214-2.3026 - 1.6094 - 0.6931 - 0.9163 = -5.5214, which matches ln⁡0.004=−5.5215\ln 0.004 = -5.5215 up to rounding.
  7. The score for this order is about −5.52-5.52 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.
Example
Realistic: which orders let York see exactly two words

Take the sentence "New York is a city", with positions 1 New, 2 York, 3 is, 4 a, 5 city. There are 5!=1205! = 120 orders, and all are equally likely.

  1. York is predicted last in 4!=244! = 24 orders. In those orders it sees all four other words, which is the strongest context the sentence can give. That is 24/120=1/524/120 = 1/5 of the orders.
  2. In other orders York sees fewer words. Take the order (3,1,5,2,4)(3, 1, 5, 2, 4). York is predicted fourth, after "is", "New" and "city". It sees words on both sides but not "a".
  3. 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 2×2=42 \times 2 = 4 orders.
  4. Those 4 orders are out of 120, so the probability is 4/120=1/304/120 = 1/30.

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 2×2=42 \times 2 = 4 orders out of 120, or 1/301/30. 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 p(x2)=0.5p(x_2) = 0.5, p(x3∣x2)=0.2p(x_3 \mid x_2) = 0.2 and p(x1∣x2,x3)=0.4p(x_1 \mid x_2, x_3) = 0.4. What is the probability of this factorisation, and what is its natural log?Show answer

The probability is 0.5×0.2×0.4=0.040.5 \times 0.2 \times 0.4 = 0.04. Its natural log is ln⁡0.04≈−3.22\ln 0.04 \approx -3.22 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.

Sources