TechCoder.io / AI & Machine Learning

Transformers & Attention

The architecture that changed the world. Master Self-Attention, Multi-Head Attention, Flash Attention, KV-Cache, Rotary Positional Encodings, and why 'Attention Is All You Need'.

By TechCoder TeamLast updated: 2026-07-23
In a Nutshell

The architecture that changed the world. Master Self-Attention, Multi-Head Attention, Flash Attention, KV-Cache, Rotary Positional Encodings, and why 'Attention Is All You Need'. This hands-on tutorial focuses on practical implementation of transformers & attention concepts.

Transformers & Attention

In 2017, Google researchers published a paper titled "Attention Is All You Need". It introduced the Transformer architecture, which eliminated RNNs and became the foundation for every modern LLM (GPT, BERT, Claude, LLaMA).

[!TIP] Read the original paper β€” "Attention Is All You Need" (Vaswani et al., 2017). One of the most important papers in AI history.

1. The Problem with RNNs 🐒

  • Sequential: Must process word 1, then word 2, then word 3. Slow! Cannot be parallelized on modern GPUs.
  • Long-term Memory: Struggled with very long contexts β€” the gradient signal vanishes over many steps.
  • Bottleneck: The entire sequence was compressed into a fixed-size context vector, losing information.

2. The Solution: Self-Attention πŸ’‘

Transformers process the entire sentence at once (parallelization). The core mechanism is Self-Attention β€” it allows every token to look at every other token to figure out context.

  • Sentence: "The animal didn't cross the street because it was too tired."
  • Attention: When the model processes "it", it pays high attention to "animal" and low attention to "street".

The Math of Self-Attention

Each token gets projected into three vectors:

  • Query (Q): "What information am I looking for?"
  • Key (K): "What information do I have?"
  • Value (V): "What is the actual content I'll share?"

The attention formula:

Attention(Q, K, V) = softmax(QK^T / √d_k) Γ— V
  • QK^T β€” Dot product gives a score: how much does each query "match" each key?
  • / √d_k β€” Scaling prevents the scores from getting too large (causes softmax to saturate)
  • softmax(...) β€” Converts scores to probabilities (0 to 1, sum to 1)
  • Γ— V β€” Weight the value vectors by attention scores
PYTHON PLAYGROUND
⏳ Loading editor…

3. Multi-Head Attention (MHA) 🎯

Instead of running attention once, we run it h times in parallel (multiple "heads"), each learning different types of relationships:

  • Head 1: Might learn syntactic relationships (subject-verb agreement)
  • Head 2: Might learn coreference (pronoun β†’ noun it refers to)
  • Head 3: Might learn semantic similarity (synonyms, antonyms)
MultiHead(Q, K, V) = Concat(head_1, ..., head_h) Γ— W_O
where head_i = Attention(Q Γ— W_Q_i, K Γ— W_K_i, V Γ— W_V_i)

GPT-3 uses 96 attention heads with 128-dimensional each (96 Γ— 128 = 12,288 total model dimension).

4. Architecture: Encoder-Decoder πŸ—οΈ

The original Transformer had two parts:

  1. Encoder: Reads the input and builds rich contextual representations.
  2. Decoder: Generates the output, attending to both its own previous outputs and the encoder's output.
  • BERT (Encoder-only): Good for understanding (Classification, NER, Search). Can see all tokens bidirectionally.
  • GPT (Decoder-only): Good for generation (Chatbots, Writing). Uses causal masking β€” each token can only attend to previous tokens.
  • T5/BART (Encoder-Decoder): Good for transformation (Translation, Summarization).

5. Positional Encoding πŸ“

Since Transformers process all words at once, they don't inherently know the order of words. We must inject position information.

Absolute Positional Encodings (Original Transformer)

Add a sinusoidal vector based on position number:

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))

Rotary Position Embeddings (RoPE) β€” Used by LLaMA, GPT-NeoX

RoPE encodes position by rotating the query and key vectors. This naturally captures relative positions rather than absolute ones.

  • Advantage: Better generalization to sequence lengths not seen during training.
  • Used by: LLaMA 2/3, Mistral, Falcon, GPT-NeoX, Phi.

πŸ“š Official Resources

6. KV-Cache: The Secret to Fast Inference ⚑

During generation, the model processes the same past tokens repeatedly. KV-Cache stores the Key and Value matrices from previous steps so they don't need to be recomputed:

PYTHON PLAYGROUND
⏳ Loading editor…

7. Flash Attention: Making It Practical at Scale ⚑

The bottleneck in standard attention is memory bandwidth β€” writing and reading the large NΓ—N attention matrix from GPU HBM (high-bandwidth memory).

Flash Attention (Tri Dao et al., 2022) reorganizes the computation to keep intermediate results in the fast SRAM (on-chip cache):

  • 2-4x faster training
  • 10-20x less memory for the attention matrix
  • Enables long contexts (32K, 128K, 1M tokens) that weren't previously possible

[!IMPORTANT] Flash Attention is now used by virtually all major LLM training runs. It's the reason models can have 128K+ token context windows.

πŸ“š Official Resources

Interactive Challenge: Attention Matrix

Imagine we have 3 words. The Attention Matrix shows how much each word focuses on others.

PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 4

Why are Transformers faster to train than RNNs?

They are smaller
They process all tokens in parallel rather than sequentially
They don't use GPUs

Key Takeaways

βœ… Self-Attention uses Q, K, V projections to let every token attend to every other token.
βœ… Multi-Head Attention runs multiple attention operations in parallel, each learning different linguistic patterns.
βœ… KV-Cache makes autoregressive generation efficient by storing past computations.
βœ… Flash Attention tiles the attention computation to use fast on-chip memory, enabling 128K+ context windows.
βœ… RoPE positional encodings (used in LLaMA, Mistral) generalize better than absolute sinusoidal encodings.

Official Resources

What's Next?

We have the building blocks. Now let's scale it up. Way up. Next Chapter: Large Language Models (LLMs) β€” Deep Dive.