Source-linked AI summary

On the Emergence of Position Bias in Transformers

Xinyi Wu, Yifei Wang, Stefanie Jegelka, Ali Jadbabaie

arXiv:2502.01951v4cs.LG

TL;DR

Transformers exhibit position biases whose mechanisms are not fully understood, especially those arising from attention masks and positional encodings. This paper develops a graph-theoretic analysis of multi-layer attention and finds that causal masking favors earlier positions while interacting with relative positional encodings through competing decay and accumulation effects.

  • Problem

    Position bias affects transformer and LLM behavior, but how attention masks and positional encodings produce these biases remains insufficiently understood.

  • Method

    The paper models attention masks as directed graphs and analyzes attention rollout theoretically across multi-layer attention.

  • Results

    The analysis finds that causal masking increasingly favors earlier positions with depth, while relative positional encodings create a non-monotonic balance between distance-based decay and cumulative influence from earlier positions.

  • Takeaways & Limitations

    The framework provides a principled basis for understanding how masking, positional encoding, and network depth interact in transformer position bias.

  • Takeaways & Limitations

    The experiments indicate that token-embedding geometry can affect task difficulty and may influence how positional bias emerges.

Abstract

from arXiv · show

Recent studies have revealed various manifestations of position bias in transformer architectures, from the "lost-in-the-middle" phenomenon to attention sinks, yet a comprehensive theoretical understanding of how attention masks and positional encodings shape these biases remains elusive. This paper presents a graph-theoretic framework for analyzing position bias in multi-layer attention. Modeling attention masks as directed graphs, we quantify how tokens interact with contextual information based on their sequential positions. We uncover two key insights: First, causal masking inherently biases attention toward earlier positions, as tokens in deeper layers attend to increasingly more contextualized representations of earlier tokens. Second, we characterize the competing effects of the causal mask and relative positional encodings, such as the decay mask and rotary positional encoding (RoPE): while both mechanisms introduce distance-based decay within individual attention maps, their aggregate effect across multiple attention layers$\unicode{x2013}$coupled with the causal mask$\unicode{x2013}$leads to a trade-off between the long-term decay effects and the cumulative importance of early sequence positions. Through controlled numerical experiments, we not only validate our theoretical findings but also reproduce position biases observed in real-world LLMs. Our framework offers a principled foundation for understanding positional biases in transformers, shedding light on the complex interplay of attention mechanism components and guiding more informed architectural design.

1. Introduction

Position bias is a central unresolved issue in transformers: positional mechanisms can produce systematic preferences for input regions independently of semantic content. The paper asks how attention masks and positional encodings shape these biases and proposes a graph-theoretic framework for multi-layer attention.

  • Motivation: Position bias can substantially affect transformer and LLM performance and reliability, including degraded retrieval for information in the middle of sequences.The introduction also notes that changing the order of in-context examples can affect performance.
  • Motivation: Attention masks and positional encodings encode positional information but can also introduce systematic positional biases largely independent of semantic content.
  • Research question: The paper asks how attention masks and positional encodings shape position bias in transformers.
  • Approach: The proposed graph-theoretic framework models attention masks as directed graphs to analyze attention score distributions and information flow across multiple layers.It quantifies how each token’s contextual representation is composed from information at different sequence positions.
  • Contributions: Causal masking biases deep-network attention toward earlier positions because tokens increasingly attend to contextualized representations of earlier tokens.The paper also derives analogous results for sliding-window and prefix masks.
  • Contributions: Relative positional encodings and causal masking create a trade-off between local distance decay and the cumulative importance of early sequence positions across layers.

2. Related Work

Prior work documents position-dependent behavior in retrieval, ranking, and in-context learning, while separate studies examine masking and positional encodings from function-approximation, rank-collapse, attention-sink, and length-generalization perspectives.

  • Position bias: Position bias has been associated with systematic performance degradation in information retrieval and ranking tasks.
  • Position bias: In-context learning performance can vary substantially when the order of otherwise comparable examples changes.
  • Masks and positional encodings: Prior studies analyze attention masks through function approximation, rank collapse, and attention sinks.
  • Masks and positional encodings: Other work studies positional encodings in relation to length generalization.

3. Problem Setup

The paper represents masked attention as a directed graph whose edges encode allowable context interactions, then defines masked attention updates and two relative positional mechanisms: decay masking and RoPE.

  • Graph formulation: The attention mask is formalized as a directed graph in which an edge from node j to i means token j directly contexts token i.Reachability represents direct or indirect contextual influence through attention paths.
  • Graph formulation: A center node is a node from which every node in the directed graph is reachable.
  • Masked attention: Raw attention scores are computed from query and key projections, with dQK serving as a temperature term controlling their scale.The analysis assumes dQK = 1 without loss of generality.
  • Masked attention: Masked attention normalizes scores over allowed graph edges and sets attention weights to zero for disallowed interactions.
  • Attention update: The analysis considers single-head masked self-attention and assumes equal representation and value dimensions with a fixed graph across layers.The setup notes that time-varying masks can be handled under regularity conditions.
  • Relative positional encoding: The decay mask adds an explicit distance-based bias favoring more recent tokens, whereas RoPE rotates query and key embeddings by angles proportional to token positions.Under RoPE, the resulting raw scores depend on relative token distances.

4. Main Results

The paper models multi-layer attention as information flow on directed graphs, showing how masks and positional encodings shape position bias. Causal attention increasingly favors early tokens with depth, while local decay and causal accumulation jointly produce non-monotonic distance effects.

  • The framework treats attention rollout as a theoretical quantity for tracking how input tokens contribute to contextual representations across layers.It models masks as directed graphs and interprets paths as multi-layer information flow.
  • Causal mask: Causal masking makes position bias toward earlier tokens intensify with depth, regardless of semantic content.Deeper layers attend to increasingly contextualized earlier representations, allowing early tokens to influence later ones through direct and indirect paths.
  • Causal mask: Causal attention’s early-token bias is not equivalent to simulating a positional encoding and reflects the sequential structure of the mask.The paper reports empirical validation that the causal mask itself favors earlier sequence positions.
  • Architectural trade-off: Increasing attention depth trades greater representational power for stronger positional bias toward initial sequence tokens.The paper frames architectural depth as requiring a balance between representation power and positional bias.
  • Other masks: Smaller sliding-window widths mitigate but do not eliminate convergence toward early tokens, whereas prefix masks distribute influence across the first K tokens.For sliding windows, convergence remains exponential; for prefix masks, the K prefix tokens each retain non-trivial influence.
  • Relative positional effects: The decay mask creates exponential distance decay within each attention layer, but cross-layer accumulation makes the aggregate behavior non-monotonic.Earlier tokens can gain cumulative importance across layers despite stronger within-layer decay.
  • Relative positional effects: For the decay mask, increasing decay strength m decreases the critical distance x*, while increasing depth t increases x*, shifting bias between recent and initial tokens.The critical point is x*=t/(em−1) under Stirling’s approximation.
  • RoPE: RoPE induces a weaker distance-based decay than the decay mask, yet deeper RoPE models also become more biased toward initial tokens under causal masking.Increasing the base angle θ1 strengthens long-term decay and reduces the optimal distance x*, whereas increasing depth increases x*.

5. Experiments

Controlled experiments isolate architectural and data-driven sources of position bias. Deeper causal attention favors earlier tokens, while relative positional mechanisms and training-data bias shape competing recency and endpoint effects.

  • Experimental setup: The synthetic retrieval setup isolates positional effects using alternating item-label sequences, novel classes, and controlled answer positions.The model uses n = 8 items embedded in d = 64 dimensions, with an attention-only network and classifier.
  • Experimental setup: The positional-preference gap compares [a, b] with [b, a] when identical vectors occupy the two positions, removing semantic-content differences.Three test-set pairs compare first, middle, and last positions, each containing 10,000 sequences.
  • Architectural effects: Increasing depth consistently amplifies bias toward earlier sequence positions without residual connections, regardless of positional encoding.The middle-versus-last gap is smaller than comparisons involving the first position.
  • Architectural effects: Decay masks and RoPE reduce causal beginning bias by favoring recent tokens, but the decay mask produces substantially stronger distance-based decay.Residual connections make the depth relationship non-monotonic and dependent on positional encoding and depth regime.
  • Data and positional encoding: With endpoint-biased training data, sin PE and RoPE capture biases at both ends, whereas a causal mask without PE captures only the first-position bias.The causal mask therefore introduces positional dependence but does not generally implement positional encoding across arbitrary locations.
  • Data and positional encoding: The lost-in-the-middle pattern emerges with training bias toward both sequence ends, but not with unbiased training data except in a deep-layer RoPE regime.This indicates that training-data positional bias contributes to how models prioritize sequence positions.

6. Conclusion

The paper concludes that graph-theoretic analysis explains how causal masking and relative positional encodings jointly create position bias across attention layers. It identifies a trade-off between cumulative early-position influence and distance-based decay, while extending the analysis to other masks.

  • Conclusion: The framework quantifies how positional information shapes context construction across multi-layer attention using probabilistic and graph-theoretic analysis.It tracks how attention information flows and accumulates through successive layers.
  • Conclusion: Causal masking inherently favors earlier tokens because deeper layers attend to increasingly contextualized representations of those positions.The same analysis derives analogous results for sliding-window and prefix masks.
  • Conclusion: Causal masking and relative positional encodings produce a non-monotonic balance between distance-based decay and cumulative influence from earlier positions.The conclusion frames this balance as a design trade-off for attention-based architectures.
  • Conclusion: The framework is presented as a basis for studying positional bias and informing designs that mitigate or strategically exploit it.The paper mentions bias-free transformers and applications emphasizing early or recent positions as future directions.

B.2. Proof of Theorem 4.2

The proof analyzes products of transformed attention matrices over grouped steps. It combines a sliding-window connectivity bound with the main theorem to obtain geometric control of aggregate influence.

  • Proof construction: The proof defines the grouped product ˜P(k) as the product of transformed attention matrices ˜A(k) through ˜A(0).This product represents the multi-step attention propagation analyzed in the theorem.
  • Geometric bound: Using Lemma B.1 and Theorem 4.1, the proof obtains constants 0 < C < 1 and 0 < c < 1 controlling entries of ˜P(k).The stated bound is ˜P(k)_ij ≤ C(1 − (j − 1)c)^k for j ≤ i.

C. Proof of Theorem 4.3

The proof analyzes causal-mask connectivity through directed paths and combines path counting with bounds on attention terms. It establishes how depth and RoPE parameters affect the optimal interaction distance and positional bias.

  • C. Proof of Theorem 4.3: Causal-graph paths from token j to token i have non-decreasing token indices along every intermediate step.This connectivity property underlies the path-counting argument used in the theorem.
  • C. Proof of Theorem 4.3: The proof uses Stirling’s approximation and implicit differentiation to characterize how the critical distance changes with depth and the base rotational angle.The resulting monotonicity statement is reported in the theorem analysis.
  • C. Proof of Theorem 4.3: RoPE analysis reparameterizes the base rotational angles as θ_i = α_iθ_1 and assumes each two-dimensional segment contributes nontrivially.The effective number of slowly rotating feature segments may be small in LLMs.

I.1. Results

The results section extends the causal-mask analysis to higher-dimensional RoPE settings under explicit norm, angle, and depth conditions. Its lemmas and theorem provide bounded attention estimates and a generalized positional-bias result.

  • I.1. Results: Lemma I.1 establishes constants controlling rotated query-key interactions under the causal mask and the stated assumptions.The lemma introduces positive constants needed for the subsequent theorem.
  • I.1. Results: Theorem I.2 extends the bound to all depths t ≤ T under the same conditions.The theorem fixes a finite depth range and supplies a uniform constant over that range.
  • I.1. Results: The generalized theorem is obtained as a direct corollary of Lemma 4.6 and Theorem 4.7.The proof section presents auxiliary trigonometric and Cauchy–Schwarz steps before invoking the earlier results.

J. Experiments

The experiments use controlled Gaussian-mixture retrieval tasks and vary attention depth, positional encodings, and residual connections. They show that deeper pure-attention models strengthen early-position bias, while decay mechanisms favor recent tokens and residuals make depth effects non-monotonic.

  • J. Experiments: The retrieval task alternates sampled items and labels before a query, with Gaussian-mixture classes and a cross-entropy objective.The experimental setup uses a controlled information-retrieval task with configurable data parameters.
  • J. Experiments: The experiments use γ = 0.75, K = 2048, L = 32, and B = 4 to target a difficult in-weight-learning and easy in-context-learning regime.These values are selected to preserve information-retrieval ability in the controlled setup.
  • J. Experiments: Deeper pure-attention models amplify bias toward earlier tokens regardless of positional encoding, while decay masks and RoPE increase distance-based focus on recent tokens.The decay-mask effect is stronger than the RoPE effect in these experiments.
  • J. Experiments: With residual connections, the relationship between depth and positional bias becomes non-monotonic and depends on the positional encoding and depth regime.Residuals therefore alter how positional effects accumulate across layers.

K.3. The role of training data on positional bias

Controlled experiments separate architectural and training-data contributions to positional bias. Causal masking favors sequence beginnings, positional encodings can preserve biases at other locations, and attention sinks follow mask-specific patterns.

  • K.3. The role of training data on positional bias: Sinusoidal positional encoding and RoPE allow the model to capture positional biases at different locations in the training sequences.The comparison includes training sequences biased toward the first, middle, or last position.
  • K.3. The role of training data on positional bias: Without positional encodings, the causal mask favors the beginning because earlier tokens receive more attention through iterative attention.This effect appears across training-data cases compared with using no mask.
  • K.3. The role of training data on positional bias: The lost-in-the-middle phenomenon emerges when training sequences are biased toward both the beginning and end, with one additional deep-layer RoPE exception.The paper presents this as evidence that training-data bias can shape the learned positional pattern.
  • K.3. The role of training data on positional bias: Attention sinks emerge in simplified, position-unbiased experiments and align with phenomena reported in real-world LLMs.The setup uses causal masking without positional encodings and reports similar attention-sink behavior.
  • K.3. The role of training data on positional bias: Under the causal mask, attention sinks emerge on the absolute first token; under the prefix mask, they emerge across all K prefix tokens.For sliding-window masks, the absolute first token becomes more likely to be a sink as window size increases.

L. Additional Results under a Fixed-Vocabulary Setting

The fixed-vocabulary experiments largely preserve the paper’s main findings, while showing that anisotropic embeddings make the task more challenging. Increasing depth consistently amplifies positional bias even with residual connections.

  • Experimental setting: The fixed-vocabulary setting uses K fixed class vectors assigned to inputs, with anisotropy controlled by λ = 0.75.The shared component makes class vectors non-orthogonal, resembling practical token embeddings.
  • Results: Most findings from the main text continue to hold under the fixed-vocabulary setting.The corresponding results span Figures 13–24.
  • Results: Increasing depth consistently amplifies positional bias even with residual connections.This result is shown in Figure 14.
  • Results: Anisotropic embeddings make the task significantly more challenging than the orthogonal embeddings used in the Gaussian mixture setting.The comparison concerns task difficulty rather than a reported change in positional bias itself.
  • Additional analyses: The experiments examine architectural position bias without positional bias in the training data and include attention-sink behavior under several masking settings.The figures cover pure attention layers, residual connections, and causal, sliding-window, and prefix masks.
  • Limitations and future work: Preliminary λ = 0 results resemble the Gaussian mixture setup, motivating further study of how embedding geometry affects task difficulty and positional bias.The authors identify embedding geometry as a possible factor in how residual connections behave on more difficult tasks.
Loading 2502.01951v4…