Positional Encoding in Transformers

Search for a command to run...

No comments yet. Be the first to comment.
💡 If you are here just to read about LLM streaming, jump to section 12 From first principles to production-grade architecture.Covers: protocol internals, Express server setup, multi-client forward

Here's a full deep-dive on the SWE-bench paper: PAPER X-RAY ║ Title : SWE-bench: Can Language Models ║ ║ Resolve Real-World GitHub ║

This is a Claude-generated summary! PAPER X-RAY ║ Title : SWE-agent: Agent-Computer ║ ║ Interfaces Enable Automated ║ ║

Paper: Zhang, Kraska & Khattab — MIT CSAIL, January 2026Code: github.com/alexzhang13/rlm TLDR; From first principles — before RLMs, performance degradation over large contexts was a known issue. RLM

The core question is: what makes zero-shot retrieval fail, and what would fix it? Let me build up the intuition step by step.The root problem: A user query like "how do I fix a leaky pipe?" and a docu
Positional Encoding is a technique used in Transformer architectures to encode the order of tokens in a sequence.
Transformers process tokens in parallel, unlike sequential models such as RNNs. Because of this, the model has no inherent understanding of token order. Positional encoding injects information about token positions into token embeddings before they enter the self-attention layers.
The final representation sent to the transformer is:
Input = TokenEmbedding + PositionalEncoding
This allows the model to learn both:
semantic meaning (from embeddings)
sequence order (from positional encoding)
Self-attention processes tokens simultaneously, not sequentially.
Example sentence:
"Nitish killed lion"
"Lion killed Nitish"
Both sentences contain the same tokens:
[Nitish, killed, lion]
If sent to self-attention simultaneously, the model cannot distinguish token order, so both sequences appear identical.
This is a fundamental limitation because word order determines meaning in natural language.
| Model | Order Awareness | Reason |
|---|---|---|
| RNN | Yes | Tokens processed sequentially |
| LSTM | Yes | Hidden state carries time information |
| Transformer | No | Tokens processed in parallel |
Transformers sacrifice sequential processing for parallel efficiency, so positional information must be explicitly added.
To solve the ordering problem, we must encode position information alongside token embeddings.
Token Embedding
Positional Encoding
Self-Attention Layer
Token → Embedding Vector
Position → Positional Encoding Vector
Final Input = Embedding + Positional Encoding
Each token therefore carries:
semantic information + positional information
A good positional encoding must satisfy:
Neural networks train best with values in small ranges (e.g. -1 to 1).
Neural networks prefer smooth functions, not discrete jumps.
The model should infer relationships like:
distance(token_i, token_j)
Each position must have a distinct encoding.
Example:
Sentence: "River Bank"
Tokens: [River, Bank]
Example:
River → embedding vector (d_model)
Bank → embedding vector (d_model)
Example dimension:
d_model = 512
For position pos and dimension i:
PE(pos,2i) = sin(pos / 10000^(2i/d_model))
PE(pos,2i+1) = cos(pos / 10000^(2i/d_model))
Key ideas:
even dimensions → sine
odd dimensions → cosine
InputVector = Embedding + PositionalEncoding
The resulting vector contains both:
semantic meaning + position
This vector becomes the input to the transformer encoder.
Sentence:
"The lion runs"
Token positions:
The → position 0
lion → position 1
runs → position 2
Compute positional encodings:
PE(0)
PE(1)
PE(2)
Then combine:
Embedding(The) + PE(0)
Embedding(lion) + PE(1)
Embedding(runs) + PE(2)
The transformer can now learn relationships such as:
which word comes first
relative distances between words
syntactic dependencies
Transformers process tokens in parallel, losing order information.
Positional encoding injects token position information.
Encodings are generated using sine and cosine functions at multiple frequencies.
Positional vectors have the same dimension as embeddings.
Final input to transformers is:
embedding + positional_encoding