Skip to main content
CookLLM logoCookLLM Docs

Position Encoding Basics

Premium

Why Transformers need position information, and the methods and limits of absolute position encoding

Get code access

Permutation Invariance in Transformers

Let’s start with a key fact: standard Self-Attention does not care about input order.

Recall the Attention formula:

Attention(Q,K,V)=softmax(QKTd)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d}}\right)V

Here Q=XWQQ = XW_Q, K=XWKK = XW_K, V=XWVV = XW_V. If we shuffle the tokens in XX, the QQ, KK, and VV vectors for each token do not change, only their positions in the sequence do. Attention computes pairwise dot products between all tokens, which is order-invariant.

In other words, “I like cats” and “Cats like me” look the same to Attention. That is clearly unacceptable because language meaning depends on word order.

That is why we need position encoding: inject position information into the input so the model knows where each token is in the sequence.

Log in to continue reading

This is premium content. Please log in to access the full article.