Introduction to Transformers – Motivation for Generative AI
Why generative AI gave up on recurrence and bet everything on attention
Why generative AI gave up on recurrence and bet everything on attention
RNNs and LSTMs process sequences one token at a time, which makes them slow to train and bad at remembering far-away context. Transformers drop recurrence entirely — every token attends directly to every other token in parallel — and that single change is why they became the backbone of essentially all modern generative AI.
Every major generative system you've heard of — GPT-style LLMs, vision Transformers generating or understanding images, audio Transformers like those behind speech generation — is built on this architecture. Understanding why Transformers replaced RNNs is really understanding why generative AI scaled the way it did over the last few years.
Simple shabdo mein: an RNN/LSTM reads a sequence like a person reading one word at a time and updating a mental summary as they go. Two problems fall out of that directly:
t, you need the hidden state at
step t-1 — there's no way to compute step 5 before step 4 finishes. This means training can't be
parallelized across the time dimension, which is brutal when you want to train on huge datasets.The Transformer's answer: throw out recurrence entirely. Use self-attention, where every position in the sequence computes a direct, weighted connection to every other position, in one shot, using matrix multiplications that are fully parallelizable across the whole sequence.
Two architectural changes do all the work:
At a high level, the original Transformer (and most Transformer-based systems) has an encoder-decoder shape: the encoder reads the full input sequence and produces a contextual representation of it; the decoder generates the output sequence token by token, attending to its own previously generated tokens (self-attention) and to the encoder's representations (cross- attention) to stay grounded in the input. Encoder-only models (like BERT-style) and decoder-only models (like GPT-style LLMs) are variations that keep one half of this shape and drop the other, depending on the task.
Q = XW_Q, K = XW_K, V = XW_V;
Attention(Q,K,V) = softmax(QK^T / √d_k) V — the √d_k scaling keeps the dot products from
growing too large as dimensionality increases, which would otherwise push softmax into regions
with tiny gradients.head_i = Attention(QW_Q^i, KW_K^i, VW_V^i);
MultiHead(Q,K,V) = Concat(head_1,...,head_h) W_O — running several smaller attention "heads" in
parallel lets the model attend to different kinds of relationships at once (e.g. syntactic vs
semantic), then recombines them.PE(pos,2i) = sin(pos / 10000^(2i/d_model)),
PE(pos,2i+1) = cos(pos / 10000^(2i/d_model)).Consider the sentence "The trophy didn't fit in the suitcase because it was too big" — resolving what "it" refers to requires connecting a token near the end back to a token near the beginning. An RNN has to carry that link through every intermediate word, with the risk of it fading. A Transformer computes attention weights directly between "it" and every earlier word in one pass; if "trophy" and "suitcase" both get non-trivial attention weight from "it," the model can weigh both candidates based on the full sentence's context, not a decayed running summary.
RNNs bottleneck on sequential computation and decaying long-range signal; Transformers fix both at once by replacing recurrence with self-attention, giving full parallelism and a direct, one-hop connection between any two tokens no matter how far apart.