C.W.K.
Stream
Lesson 01 of 05 · published

What Makes Transformers Powerful

~14 min · transformer, attention, foundations

Level 0Observer
0 XP0/50 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Self-attention is a learned database lookup

Self-attention's superpower is that every token can look at every other token and decide, per-query, how much to weight each one. That's a flexibility no previous sequence architecture offered. RNNs forced information through a single hidden state — a fixed-size bottleneck that lost long-range detail. Convolutions had a fixed receptive field — local by construction. Attention let the model build pairwise relationships at any distance, with weights computed at runtime from the actual content of the tokens.

Mechanically it's a three-tensor dance. For each token you compute a query Q ("what am I looking for?"), a key K ("what do I contain?"), and a value V ("what do I output?"). The pairwise score Q·Kᵀ/√d_k tells you how relevant every token is to every other token. Softmax turns those scores into a distribution. Multiplying by V produces a context-aware output for every position. The whole thing is one matrix multiply, one elementwise softmax, one more matrix multiply.

Why it stuck

Three properties made attention dominant once compute caught up. First, it's fully parallelizable across the sequence dimension during training — unlike RNNs which had to step through tokens sequentially. Second, multi-head attention learns multiple relationship patterns at the same time — different heads end up specializing in syntax, coreference, position, semantics. Third, it composes cleanly with residual streams and layer norms into a stack that scales: 2017's 65M-parameter Transformer and 2025's 2T-parameter mixture-of-experts use the same fundamental block.

This is the architecture you're competing against. Every alternative in this quest gives up something attention has — usually exact recall over the full context — to get something attention doesn't — usually subquadratic compute or constant inference memory. Knowing what attention is good at is the first step to knowing when to leave it behind.

Code

Scaled dot-product attention — the core operation·python
import torch
import torch.nn.functional as F

def attention(Q, K, V, mask=None):
    # Q, K, V: (batch, heads, seq, d_k)
    d_k = Q.size(-1)
    scores = (Q @ K.transpose(-2, -1)) / (d_k ** 0.5)  # (B, H, n, n)
    if mask is not None:
        scores = scores.masked_fill(mask == 0, float('-inf'))
    weights = F.softmax(scores, dim=-1)                 # (B, H, n, n)
    return weights @ V                                  # (B, H, n, d_k)

External links

Exercise

Implement scaled dot-product attention from scratch in PyTorch and verify it matches torch.nn.functional.scaled_dot_product_attention on a random (4, 8, 64, 32) Q/K/V batch. Then time both on a sequence length of 4096 and report the ratio. The point is to feel where PyTorch's fused kernel buys you wall-clock time even when the math is identical.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.