Learn essential Transformer formulas for attention, QKV, RoPE, RMSNorm, SwiGLU, cross-entropy, softmax, gradients, and AdamW training.
Transformer Important Formulas
Transformers are based on several important mathematical operations. These formulas are used in attention, normalization, feed-forward networks, positional encoding, and training.
1. Token Embedding
Each token ID is converted into a vector using an embedding matrix.
X=E[token_ids]
Where:
E = embedding matrix
V = vocabulary size
dmodel = embedding dimension
The embedding matrix has shape:
E∈RV×dmodel
2. Query, Key, and Value
Self-attention creates three projections from the input:
Q=XWQK=XWKV=XWV
Where:
Q = Query
K = Key
V = Value
WQ = Query projection
= Key projection
3. Scaled Dot-Product Attention
The most important Transformer formula is:
Attention(Q,K,V)=softmax(
First, calculate the similarity between queries and keys:
S=QKT
Then scale the scores:
S=dk
Then apply softmax:
A=softmax(dk
Finally, multiply by the values:
Output=AV
4. Softmax
Softmax converts attention scores into probabilities:
softmax(xi)=∑
The probabilities satisfy:
i∑pi=1
5. Causal Attention Mask
In a decoder Transformer, a token cannot look at future tokens.
The mask can be represented as:
Mij={0−∞
The attention becomes:
Attention=softmax(d
6. Multi-Head Attention
Instead of using a single attention operation, Transformers use multiple attention heads.
For each head:
headi=Attention(Qi
The heads are concatenated:
H=Concat(head1,head
Then projected:
MultiHeadAttention=HWO
Complete formula:
MHA(X)=Concat(head1,…,
7. Head Dimension
If the model has h attention heads:
dk=hdmodel
For example, if:
dmodel=768
and:
h=12
then:
dk=12768=64
8. Layer Normalization
LayerNorm first calculates the mean:
μ=n1i=1∑n
Variance:
σ2=n1i=
Normalization:
x^=σ2+ϵ
Final LayerNorm:
LayerNorm(x)=γx^+β
Where γ is the scale parameter and β is the bias parameter.
9. RMSNorm
Modern LLMs often use RMSNorm.
The RMS value is:
RMS(x)=n1
Then:
RMSNorm(x)=RMS(x)x⊙
10. Residual Connection
Transformers use residual connections to help information and gradients flow through the network.
A simplified attention residual is:
X′=X+Attention(X)
The feed-forward residual is:
Output=X′+FFN(X′)
11. Feed-Forward Network
The original Transformer uses:
FFN(x)=ReLU(xW1+b
Modern Transformers often use GELU, SiLU, or SwiGLU.
12. GELU
The GELU activation function is:
GELU(x)=xΦ(x)
A commonly used approximation is:
GELU(x)≈2x[1+tanh
13. SiLU
SiLU is defined as:
SiLU(x)=xσ(x)
where:
σ(x)=1+e−x1
Therefore:
SiLU(x)=1+e−xx
14. SwiGLU
SwiGLU is commonly used in modern LLM architectures.
The gating operation is:
SwiGLU(x)=SiLU(xWg)⊙(x
Then the output projection is:
FFN(x)=SwiGLU(x)Wd
Where:
Wg = gate projection
Wu = up projection
W = down projection
15. RoPE — Rotary Position Embedding
RoPE introduces positional information by rotating pairs of dimensions.
For dimensions 2i and 2i+1:
[
The angle is related to the token position:
θ=pos⋅ωi
RoPE is applied to queries and keys:
Q′=RoPE(Q)K′=RoPE(K)
Then attention uses:
Attention=softmax(d
16. Output Logits
After the final Transformer layer, the hidden states are projected into vocabulary space:
Logits=HWvocab+b
Where:
H = final hidden states
Wvocab = vocabulary projection matrix
V = vocabulary size
The output shape is:
Logits∈RT×V
17. Next Token Probability
The logits are converted into probabilities using softmax:
P(tokeni)=∑
The token with the highest probability can be selected using greedy decoding:
token=argimaxP(tokeni
18. Cross-Entropy Loss
For language-model training, cross-entropy loss is commonly used:
L=−i∑yilog(p
For the correct token:
L=−log(pcorrect)
If the model assigns a high probability to the correct token, the loss is small.