Prefix Language Modeling › Comparing Attention Patterns

Michael BrenndoerferUpdated July 14, 20255 min read
Language AI HandbookAll chapters
Part 22 of 67Pre-training Objectives
Chapter 159Prefix Language Modeling › Comparing Attention Patterns
Reading section 5 of 16 · Comparing Attention Patterns · 5 minSection 5 of 16 · 5 min

Book contents

Section 5 of 16 · 5 min read

Prefix Language Modeling › Comparing Attention PatternsOne section · Sections in this chapter5 / 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

To understand prefix LM's unique position, let's compare it with causal and masked LM attention patterns:

Out[8]:
Visualization
Lower triangular attention matrix for causal LM.
Causal LM: Each position attends only to itself and earlier positions, enforcing strict left-to-right information flow.
Full attention matrix for masked LM.
Masked LM: Full bidirectional attention where every position attends to every other position.
L-shaped attention matrix for prefix LM with bidirectional prefix and causal target.
Prefix LM: Hybrid pattern with bidirectional prefix and causal target, capturing context richly while enabling generation.

The visual comparison reveals the key differences:

Comparison of attention patterns across language modeling objectives.
AspectCausal LMMasked LMPrefix LM
Prefix attentionCausalBidirectionalBidirectional
Target attentionCausalBidirectionalCausal
GenerationYesNoYes
Context richnessLimitedFullHybrid
Use casePure generationUnderstandingConditioned generation

Prefix LM occupies the middle ground. It enables generation like causal LM while providing richer prefix representations like masked LM.

Out[9]:
Visualization
Line plot comparing attention span across positions for causal LM, masked LM, and prefix LM, showing prefix LM's hybrid behavior.
Number of positions each token can attend to across different LM objectives. Causal LM (blue) shows linear growth from 1 to n. Masked LM (green) provides full access to all n positions. Prefix LM (orange) has full access for prefix positions, then follows the causal pattern for target positions. The shaded region highlights the advantage Prefix LM gains in the prefix region.

This plot quantifies the representational advantage of prefix LM. In the prefix region (positions 0-7), each token has full access to all 16 positions, just like masked LM. In the target region (positions 8-15), the attention span follows the causal pattern, growing linearly. The shaded area represents the additional context each prefix position gains compared to causal LM, which translates to richer representations that inform target generation.