How Sinusoidal Functions Inject Sequence Order into the Permutation-Invariant Transformer
To process language simultaneously rather than sequentially, the original Transformer architecture required a mathematical workaround to understand word order. By adding intersecting sine and cosine waves to word embeddings, the model functions like a high-dimensional clock, allowing it to calculate the exact distance between any two tokens.
- Relative Encoding Innovators
- Maintain that models only need to understand the relative distance between tokens, advocating for newer methods like RoPE that apply rotations directly in the attention layers.
- Absolute Encoding Theorists
- Argue that injecting a fixed mathematical signature at the input layer is the most computationally efficient way to establish sequence order.
Perspectives this story doesn't cover
- Hardware optimization engineers
- Neuroscience researchers mapping AI to human cognition
In December 2017, the publication of the "Attention Is All You Need" paper introduced a structural paradox to artificial intelligence. The new Transformer architecture abandoned the slow, word-by-word processing of older recurrent neural networks in favor of reading entire sequences simultaneously. This parallel processing created an immediate problem: the self-attention mechanism is inherently permutation-invariant.[1][5]
To the raw attention layers, the sentence "the dog bit the man" is mathematically indistinguishable from "the man bit the dog." Without a built-in sense of sequence, the model required a way to inject positional information directly into the data before the processing began. The solution the researchers engineered relied on fundamental trigonometry: sinusoidal functions.[1][3]
Positional encoding works by generating a unique mathematical signature for every position in a sequence and adding that signature to the word's underlying meaning vector, or embedding. A naive approach might simply assign an integer to each word—1 for the first word, 2 for the second, and 500 for the five-hundredth. However, as sequences grow longer, these absolute numbers become massive, overwhelming the delicate decimal values of the word embeddings themselves.[3]
Alternatively, normalizing the positions to a fraction between 0 and 1 creates a different issue. In a 10-word sentence, the step between words is 0.1, but in a 1,000-word document, the step shrinks to 0.001. If this method were used, the model would have to constantly adjust its understanding of distance based on the total document length, making it impossible to generalize across different text sizes.
The sinusoidal approach solves both problems by utilizing intersecting sine and cosine waves of varying frequencies. Imagine a clock with hundreds of hands, each spinning at a drastically different speed. The fastest hand completes a full rotation every few seconds, while the slowest hand might take years to inch forward.[4][5]
The sinusoidal approach solves both problems by utilizing intersecting sine and cosine waves of varying frequencies.
In the original Transformer, each dimension of the 512-dimensional positional encoding corresponds to one of these hands. The first dimension uses a high-frequency sine wave that oscillates rapidly from token to token. As the dimensions increase, the frequency of the waves decreases exponentially, scaled by a factor of 10,000.[1]
By the 512th dimension, the wave stretches out so far that it barely moves across a standard paragraph of text. When the model looks at a specific position, it reads the exact height of all 512 waves at that precise moment. Because the frequencies are staggered, no two positions in the sequence will ever produce the exact same combination of wave heights.[4]
Crucially, this trigonometric approach allows the model to easily calculate relative distances. Because of the mathematical properties of sine and cosine addition, the encoding for any position plus a fixed offset can be represented as a linear transformation of the original position.[2]
As researcher Suvash Sedhain notes in his analysis of the architecture, "The sinusoidal functions allow the model to easily learn to attend by relative positions, since for any fixed offset k, PE(pos+k) can be represented as a linear function of PE(pos)." This means the Transformer does not need to memorize absolute positions.[2]
Instead, it simply learns the geometric rotation required to move from one word to another, allowing it to understand that the distance between word 5 and word 10 is identical to the distance between word 105 and word 110. The bounded nature of sine and cosine functions—which always output a value between -1 and 1—ensures that the positional signal never overpowers the semantic meaning of the word itself.[3][5]
While modern frontier models have largely transitioned to Rotary Positional Embeddings (RoPE), which apply the rotation directly within the attention mechanism rather than adding it at the input stage, the foundational principle remains unchanged. The geometry of intersecting waves remains the mechanism that allows AI to read in parallel while still understanding the flow of time.[2][5]
As context windows expand from thousands to millions of tokens, the mathematical challenge of tracking sequence order remains the primary bottleneck determining how much text a single model can comprehend at once.[5]
Key points
- The original Transformer processes all words simultaneously, meaning it inherently lacks a sense of word order.
- Researchers used sine and cosine waves to create a unique mathematical signature for every position in a sequence.
- These trigonometric functions keep the positional values small (between -1 and 1) so they don't overpower the word's meaning.
- The staggered frequencies allow the model to easily calculate the relative distance between any two words.
Why this matters
Without a mechanism to track word order, large language models would treat sentences as a scrambled bag of words, rendering them incapable of understanding grammar, syntax, or context. The sinusoidal encoding breakthrough paved the way for the massive parallel processing that makes modern AI possible.
Key terms
- Permutation-invariant
- A property of a system where changing the order of the inputs does not change the output. Without positional encoding, the Transformer's attention mechanism is permutation-invariant.
- Word Embedding
- A high-dimensional list of decimal numbers that represents the semantic meaning of a word in a way a neural network can process.
- Self-attention
- The core mechanism of a Transformer that allows it to look at every word in a sentence simultaneously to understand context.
- Linear transformation
- A mathematical operation that preserves straight lines and ratios of distances, allowing the model to easily calculate the relative distance between two positions.
Frequently asked
Why can't the model just use numbers like 1, 2, and 3 for word order?
As sequences get longer, raw integers become massive numbers. When added to the delicate decimal values of a word embedding, these large numbers would completely drown out the actual meaning of the word.
Does the AI learn these sine waves during training?
No. In the original Transformer architecture, the sinusoidal functions are fixed mathematical formulas injected into the data before training even begins. They require zero parameters to learn.
How does this help the model handle text longer than its training data?
Because the waves are continuous mathematical functions, the model can theoretically extrapolate the pattern to sequence lengths it has never seen before, calculating the relative distance between tokens at any scale.
Sources
[1]Oxford Protein Informatics GroupAbsolute Encoding TheoristsUnderstanding positional encoding in Transformers
Read on Oxford Protein Informatics Group →
[2]Suvash SedhainRelative Encoding InnovatorsPositional Encodings for LLMs: From Sinusoidal to RoPE
Read on Suvash Sedhain →
[3]DataCampAbsolute Encoding TheoristsPositional Encoding Explained: Giving Transformers a Sense of Order
Read on DataCamp →
[4]Sifal KliouiRelative Encoding InnovatorsThe Hidden Beauty of Sinusoidal Positional Encodings in Transformers
Read on Sifal Klioui →
[5]Factlen Editorial TeamSynthesis by Factlen editorial team
Read on Factlen Editorial Team →
Comments
More in Artificial Intelligence
See all →Labor Economics
How the Task-Based Model Decomposes Jobs into Tasks to Predict AI's Labor Impact
6 sources
Neural Networks
How Batch Normalization Accelerates Deep Network Convergence
7 sources
Algorithm Mechanics
How Monte Carlo Tree Search Balances Exploration and Exploitation Using the UCB1 Formula
3 sources
Natural Language Programming
How Natural Language Became the New Programming Language
4 sources
Every angle. Every day.
Get Artificial Intelligence stories with full source coverage and perspective breakdowns delivered to your inbox.




