Prefix Language Modeling › The Prefix LM Objective

Michael BrenndoerferUpdated July 14, 20253 min read
Language AI HandbookAll chapters
Part 22 of 67Pre-training Objectives
Chapter 159Prefix Language Modeling › The Prefix LM Objective
Reading section 3 of 16 · The Prefix LM Objective · 3 minSection 3 of 16 · 3 min

Book contents

Section 3 of 16 · 3 min read

Prefix Language Modeling › The Prefix LM ObjectiveOne section · Sections in this chapter3 / 16 ↓
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

Now that we understand the intuition behind splitting sequences into prefix and target regions, we need a formal training objective. The key question is: how do we mathematically express what we want the model to learn?

From Intuition to Formalization

Think about what happens when you complete someone's sentence. You take in the context they've provided, then generate a continuation that fits. Prefix LM captures this behavior: the model should fully understand the prefix before generating the target.

This leads to two requirements that our objective must encode:

  1. Full prefix visibility: When predicting any target token, the model should have access to the complete prefix as well as earlier target tokens.
  2. Causal target generation: Target tokens should be generated left-to-right, each depending on previous target tokens but not future ones.

The Mathematical Formulation

Consider a sequence x=(x1,…,xn)x = (x_1, \ldots, x_n) divided at position kk into two parts: the prefix x1:k=(x1,…,xk)x_{1:k} = (x_1, \ldots, x_k) and the target xk+1:n=(xk+1,…,xn)x_{k+1:n} = (x_{k+1}, \ldots, x_n). The training objective minimizes the negative log-likelihood of predicting target tokens:

LPrefix-LM=−∑t=k+1nlog⁡Pθ(xt∣x1:k,xk+1:t−1)\mathcal{L}_{\text{Prefix-LM}} = -\sum_{t=k+1}^{n} \log P_\theta(x_t | x_{1:k}, x_{k+1:t-1})

where:

  • LPrefix-LM\mathcal{L}_{\text{Prefix-LM}}: the prefix language modeling loss to minimize
  • nn: the total sequence length
  • kk: the position where the prefix ends and the target begins (the split point)
  • x1:kx_{1:k}: the prefix tokens (x1,…,xk)(x_1, \ldots, x_k), which are fully visible bidirectionally
  • xk+1:t−1x_{k+1:t-1}: the target tokens generated before position tt, specifically (xk+1,…,xt−1)(x_{k+1}, \ldots, x_{t-1})
  • Pθ(xt∣x1:k,xk+1:t−1)P_\theta(x_t | x_{1:k}, x_{k+1:t-1}): the probability the model with parameters θ\theta assigns to token xtx_t, conditioned on the full prefix and all preceding target tokens
  • The summation ∑t=k+1n\sum_{t=k+1}^{n} iterates only over target positions, from k+1k+1 to nn

Understanding the Conditioning

The heart of this formula lies in the conditioning term x1:k,xk+1:t−1x_{1:k}, x_{k+1:t-1}. Let's unpack what this means for a concrete example.

Suppose we have the sequence "The cat sat on the mat" with k=3k=3 (prefix is "The cat sat"). When predicting the fourth word "on":

  • The model sees: all of "The cat sat" (bidirectionally) + nothing yet from the target
  • When predicting "the": sees "The cat sat" + "on"
  • When predicting "mat": sees "The cat sat" + "on the"

Notice the asymmetry: prefix tokens are always fully visible, while target tokens accumulate causally. This is the mathematical expression of the L-shaped attention mask we'll implement shortly.

Why Loss on Target Only?

The loss summation starts at k+1k+1, not at 1. This means we compute loss only on target tokens, ignoring prefix tokens entirely. Why?

The prefix is given as input at inference time. Asking the model to predict prefix tokens would be like testing whether someone can recite back what you just told them. That's not the skill we want to develop. Instead, we focus all learning signal on the generation task: given this context, what comes next?

This differs from causal LM, where every position contributes to the loss. In prefix LM, the prefix serves purely as conditioning context, its representation is refined through gradient flow, but no direct prediction loss applies to it.

Prefix Language Modeling

A pretraining objective that splits sequences into a prefix with bidirectional attention and a target with causal attention. The model learns to generate the target conditioned on the full bidirectional context of the prefix, combining the representational power of encoder-style models with the generation capability of decoder-style models.