Why doesn't the transformer use positional encoding in every layer?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The original Transformer ("Attention is All You Need") adds positional encoding only at the input layer — not at every layer. This works because self-attention preserves positional information through residual connections. Each layer's output includes a residual connection from its input, which carries the positional signal forward. Adding positional encoding at every layer is unnecessary (the information persists) and potentially harmful (it can overweight position relative to content). Some later architectures like DeBERTa and ALiBi do inject position at every layer, but with relative position representations rather than repeating absolute sinusoidal encodings.
How Positional Encoding Works in the Original Transformer
The positional encoding is added to token embeddings before the first encoder/decoder layer. After this point, positional information propagates through the network via residual connections.
Why Residual Connections Preserve Position
Each Transformer layer has the form:
Because each layer adds its transformation to the input (rather than replacing it), the original positional encoding is never lost. It gets blended with learned representations but remains part of the signal.
Why Adding Position at Every Layer Would Be Redundant
Problems with this approach:
- Double-counting: Position is already in the signal from residual connections. Adding it again amplifies positional bias relative to semantic content.
- Interfering with learned representations: By layer 6+, the network has learned high-level abstractions. Re-injecting raw positional encoding disrupts these representations.
- No empirical benefit: Experiments show no improvement from repeating absolute sinusoidal encodings at every layer.
Architectures That Do Inject Position at Every Layer
Some newer architectures inject positional information per layer, but with relative (not absolute) position:
These approaches work because they inject relative position information (how far apart tokens are) directly into the attention computation, rather than adding absolute position to the token representations.
Absolute vs Relative Position Encoding
| Approach | Where Injected | Type | Used By |
| Sinusoidal | Input only | Absolute | Original Transformer |
| Learned embedding | Input only | Absolute | BERT, GPT-2 |
| RoPE | Every layer (Q, K) | Relative | LLaMA, GPT-NeoX |
| ALiBi | Every layer (attention bias) | Relative | BLOOM, MPT |
| DeBERTa | Every layer (disentangled) | Relative | DeBERTa |
Experimental Evidence
Research has shown:
- Repeating absolute sinusoidal encodings at every layer provides no measurable improvement on standard benchmarks (WMT translation, GLUE).
- Relative position methods (RoPE, ALiBi) applied per layer improve length generalization — the model handles sequences longer than those seen during training.
- Learned absolute positional embeddings at the input only (BERT-style) perform comparably to sinusoidal encodings.
Common Pitfalls
- Assuming positional information is lost in deep layers: Residual connections preserve the positional signal throughout the network. Probing experiments confirm that position can be decoded from any layer's representations, even the final layer. The information does not need to be re-injected.
- Confusing absolute and relative position injection: Adding absolute sinusoidal encodings at every layer is redundant and harmful. Injecting relative position (RoPE, ALiBi) at every layer is different — it modifies the attention pattern, not the token representations, and provides fresh positional context for each layer's attention computation.
- Thinking sinusoidal encoding is the only option: Modern LLMs (LLaMA, GPT-NeoX, BLOOM) use RoPE or ALiBi, which are injected per-layer and handle long sequences better. Sinusoidal encoding was a starting point, not the final answer.
- Ignoring the interaction between position and content: In the original Transformer, positional encoding is added to content embeddings, entangling position and meaning in the same vector. DeBERTa explicitly separates content and position into disentangled representations, which improves performance on tasks where position matters differently from content.
- Applying position encoding after layer normalization: Layer normalization rescales the representation, which can dilute the positional signal if encoding is added afterward. The original Transformer adds positional encoding before any layer processing, preserving the full signal magnitude.
Summary
- The original Transformer adds positional encoding only at the input, not at every layer
- Residual connections propagate positional information through all layers without re-injection
- Adding absolute positional encoding at every layer overweights position and degrades learned representations
- Modern architectures (RoPE, ALiBi, DeBERTa) inject relative position at every layer through attention mechanisms, which is fundamentally different from repeating absolute encodings
- Relative position methods improve length generalization and are now standard in large language models

