How Positional Embeddings Teach LLMs Word Order
Transformers can't tell word order on their own. Learn how learned, sinusoidal, and RoPE positional embeddings fix that.
“The dog hunted the fox.” “The fox hunted the dog.” They are the same words, but the meanings are totally different. You can note the difference at once, but a transformer can’t.
This is the problem that positional embeddings solve. They inform a language model about the position of each token in the sentence. Without them, a model sees a prompt merely as a collection of words.
We will explore these notions starting from basic facts. First, we will find out why transformers are unable to understand word order. Then we will look at three important types of positional embeddings: learned, sinusoidal, and RoPE.
Why Do Transformers Need Positional Embeddings?
The central concept of a transformer is attention. Every token looks at all other tokens to figure out what is relevant. However, attention operates through the dot product of vectors. Since the dot product doesn’t depend on the origin of the vectors, it loses track of which vectors came first.
For instance, think of a group chat without timestamps. You can see all the messages sent, but you have no idea about the order in which they were sent. That is how attention works without position information.
Let’s demonstrate this with some code. We will take two sentences, swap some words in them, and perform self-attention.
import numpy as np
np.random.seed(0)
emb = {w: np.random.randn(4) for w in ["the", "fox", "chased", "dog"]}
def attention(x):
scores = x @ x.T / np.sqrt(x.shape[1])
weights = np.exp(scores) / np.exp(scores).sum(axis=1, keepdims=True)
return weights @ x
s1 = np.stack([emb[w] for w in ["fox", "chased", "dog"]])
s2 = np.stack([emb[w] for w in ["dog", "chased", "fox"]])
out1, out2 = attention(s1), attention(s2)
print(np.allclose(out1[0], out2[2])) # True
The result is True. “Fox” gets the same representation in both sentences. The model does not know whether the fox is chasing or being chased.
When positional embeddings are absent, the fox produces the same output regardless of where it appears.

But doesn’t the causal mask help?
Somewhat, yes. GPT-style models employ a causal mask, so each token can see only the preceding tokens. This creates some order information.
However, the signal is not clear. It cannot differentiate between a one-token gap and a ten-token gap. Therefore, many models add explicitly coded position data on top of it.
Approach 1: Learned Absolute Positional Embeddings
The simplest solution is the one used in GPT-2, where every position is assigned a unique vector. These vectors are learned during training, much as word embeddings are learned.
The position vector is then added to the token vector.
- For example, “fox” at position 3:
embedding("fox") + position_vector[3] - Yet at position 7:
embedding("fox") + position_vector[7] - In this way, the two “fox” tokens look different to the attention mechanism.
A good analogy is numbered theater seats.
Unfortunately, GPT-2 learns only 1,024 position vectors, so position number 1,025 simply does not exist. You therefore have to choose the context length before training begins.
The cost also grows very fast. These are the numbers observed for GPT-2 Medium:
| Context length | Position weights as a share of the model |
|---|---|
| 1,024 tokens | About 0.3% |
| Roughly 100,000 tokens | More than 22% |
That is a lot of parameters to store and train.
Approach 2: Sinusoidal Positional Encoding
The original Transformer paper from 2017 took a different approach. Rather than learning positional vectors, it generated them by combining sine and cosine waves. There are no learned parameters to consider.
Different pairs of features oscillate at different speeds: some oscillate rapidly, while others barely oscillate at all.
As an analogy, take a clock with three hands. The second hand moves quickly, the minute hand moves more slowly, and the hour hand barely moves. By reading all three hands together, we know what time it is.
import numpy as np
def sinusoidal_encoding(num_positions, d_model):
pos = np.arange(num_positions)[:, None]
i = np.arange(0, d_model, 2)[None, :] # even feature indices
freqs = 1 / (10000 ** (i / d_model)) # fast to slow "clock hands"
pe = np.zeros((num_positions, d_model))
pe[:, 0::2] = np.sin(pos * freqs)
pe[:, 1::2] = np.cos(pos * freqs)
return pe
pe = sinusoidal_encoding(num_positions=50, d_model=8)
print(np.round(pe[:3], 3))
Consider the output rows. In the first column, the value bounces quickly from one position to another, while the other columns remain almost unchanged. This combination of speeds makes every position uniquely identifiable.

The sinusoidal positional encoding can be visualized using fast, moderate, and slow waves, along with a heatmap of values for 64 positions.
The upper chart shows three of the waves. The lower heatmap combines all 64 features. In the heatmap, you can read the individual fingerprint of a position from top to bottom.
Two favorable effects can be observed:
- Positional vectors can be computed for any position, even for sequences longer than those used in training.
- A fixed offset between positions becomes a simple linear transformation.
The design was intended to help models understand relative distances, and in practice it was only partially effective.
Approach 3: Rotary Position Embeddings (RoPE)
RoPE (Rotary Position Embeddings) emerged in 2021 with the RoFormer work and has since become the mainstream technique in modern LLMs. The concept itself is very simple.
RoPE doesn’t add anything to the token embeddings. Instead, it rotates the query and key vectors in the attention mechanism according to the position of the token. This is where RoPE distinguishes itself from the previous methods. Earlier techniques introduced position once, when the tokens were embedded. RoPE applies it at every attention layer.

Let’s illustrate this with a picture. Consider each pair of dimensional features as an arrow in a 2-dimensional space. The token at position 1 rotates its arrow a bit, while the token at position 5 rotates it five times as much. The length of the arrow remains the same; only its direction changes.
The interesting part is that attention compares the query and key arrows with a dot product, and that dot product depends only on the angle between them.
How does rotation encode relative position?
Take an illustrative example in which one pair of features is rotated by 20 degrees for each position. Start with both query and key set to the vector (1, 5), which gives a score of 26 with no rotation. Now comes the “aha” moment. Position 5 and position 2 are 3 positions apart, and so are position 105 and position 102. Both situations yield a score of 13.

Rotating RoPE: at positions 5 and 2, the query and key vectors form a 60-degree angle between them; at positions 105 and 102, they also form a 60-degree angle.
The vectors look quite different in the two cases, but the angle between them stays at 60°, so the score does not change.
If we move the key one position closer, to position 3, the score changes. It becomes 19.92, which means a smaller gap leads to a larger score. The model can now “sense” distance directly.
Here is the whole example:
| Query position | Key position | Gap | Angle between them | Attention score |
|---|---|---|---|---|
| No rotation | No rotation | n/a | 0° | 26.00 |
| 5 | 2 | 3 | 60° | 13.00 |
| 105 | 102 | 3 | 60° | 13.00 |
| 5 | 3 | 2 | 40° | 19.92 |
Why does this matter? Suppose a system prompt places 100 tokens of instructions in a prefix. Everything after it shifts to a new position, but with RoPE the relationships between your later tokens do not change.
What about the other feature pairs?
The attention head works with many features, for example 128. RoPE splits them into 64 pairs, and each pair is rotated at its own speed.
The result mirrors the clock-hand idea from the sinusoidal approach. Fast-rotating pairs are sensitive to small differences in distance, while slow-rotating pairs track much longer ranges. Together they let the model read how far apart two tokens are, rather than just where each one sits.
Conclusion
Transformers cannot tell word order on their own, because attention treats its inputs as an unordered set. Learned embeddings fix this by giving each position its own trained vector, but they cap the context length and grow costly as it increases. Sinusoidal encoding removes those learned parameters and extends to any position, though it captures relative distance only partially. RoPE rotates queries and keys at every attention layer, so the attention score depends directly on the gap between tokens. That is why it has become the mainstream choice in modern LLMs.