A transformer is a neural network that builds a representation of a sequence from attention alone, with no recurrence and no convolution. So the short answer to “what is a transformer in machine learning” is this: in every layer, each token looks at every other token in the sequence, scores how relevant each one is, and mixes in their information, all in one parallel step. Stack that operation with a position-wise feed-forward network, wrap both in residual connections and layer normalisation, and add positional encodings for word order. Transformers underpin GPT, BERT and vision transformers, and current large language models are mainly built on them.
Google researchers proposed the architecture in the 2017 paper “Attention Is All You Need”, and the original work focused on machine translation.
What is a transformer in machine learning, compared with an RNN or a CNN?
A transformer differs from a recurrent network in how information moves between positions. A recurrent model computes its hidden state at position t from the state at t−1, so it has to walk the sequence one step at a time. The original paper notes that this sequential dependency “precludes parallelization within training examples”, which becomes critical at longer sequence lengths, because memory limits batching across examples. RNNs and LSTMs also squeeze all past context into a fixed-size hidden state, which the survey “Attention mechanisms in neural networks” describes as an information bottleneck that limits long-range dependencies.
Google’s announcement of the transformer gives a concrete example. In “I arrived at the bank after crossing the river”, an RNN can only decide that “bank” means a riverbank after reading every word between “bank” and “river” in order, while a transformer can attend straight from “bank” to “river” in a single step. The same post points out that sequential processing fits GPUs and TPUs poorly, because that hardware is built for parallel work.
Convolutional layers also need only a constant number of sequential operations per layer, with a per-layer cost of O(k·n·d²).
Layer types side by side
The original paper compares the three layer types on three criteria: compute per layer, how much of that compute can run in parallel, and how far a signal travels between two distant positions. Here n is sequence length, d the representation dimension and k the convolution kernel size.
| Layer type | Complexity per layer | Minimum sequential operations | Maximum path length |
|---|---|---|---|
| Self-attention | O(n²·d) | O(1) | O(1) |
| Recurrent | O(n·d²) | O(n) | O(n) |
| Convolutional | O(k·n·d²) | O(1) | - |
Path length matters because shorter paths between positions make long-range dependencies easier to learn. The table also shows the price self-attention pays: its compute grows with the square of sequence length. According to the authors, self-attention is still faster than recurrence when n is smaller than d, which is the usual case for sentence-length inputs.
How self-attention lets a transformer read a whole sequence in parallel
Self-attention relates different positions of one sequence to each other to build a representation of that sequence. Each token is projected into three vectors. The query says what the token is looking for, the key says what it offers, and the value carries the content that gets passed on. The output for a token is a weighted sum of all the values, and each weight comes from how well that token’s query matches the corresponding key.
The transformer computes this as scaled dot-product attention. It takes the dot product of each query with every key, divides by √d_k, applies a softmax to get weights, and uses those weights to average the values. If the components of queries and keys have unit variance, their dot product has variance d_k, so raw scores grow with the head dimension. Left unscaled, large scores push the softmax into regions where gradients nearly vanish. Dividing by √d_k keeps the score variance roughly constant whatever the dimension.
With a single attention head, averaging reduces the effective resolution of attention. Multi-head attention instead projects queries, keys and values h times with different learned projections, runs attention on each projection in parallel, then concatenates and projects the results. This lets the model attend to different representation subspaces at once. In the paper’s ablations, which varied the head count while holding computation constant, a single head scored 0.9 BLEU below the best setting, and quality also fell off when there were too many heads. The authors also observed individual heads learning different tasks, many appearing related to syntactic and semantic structure. That interpretability is limited, though: the same survey cautions that attention patterns in deeper layers are often hard to read meaningfully.
Language generation needs one change. In masked (causal) self-attention, position i cannot attend to any later position, so the model cannot use future tokens. The paper “LLMs as High-Dimensional Nonlinear Autoregressive Models with Attention” puts it neatly: transformers are “autoregressive in the distribution they model, but parallel in how they compute”. A transformer computes the hidden states for every position in the context window in parallel. During generation, the model predicts one token after another, with the KV cache compressing the history.
Inside a transformer layer: attention, feed-forward network and normalisation
A transformer is a stack of layers, each built from the same block. The original encoder stacks N = 6 identical layers, each with two sub-layers: multi-head self-attention, then a position-wise feed-forward network. One encoder layer runs in this order:
- Self-attention mixes information across positions. Each token’s vector is replaced by an attention-weighted blend of every token’s values, computed across all heads.
- A residual connection and layer normalisation wrap the sub-layer. The sub-layer’s output is added back to its input, so the original paper computes LayerNorm(x + Sublayer(x)). The residual path counters vanishing gradients and stabilises training, and normalisation is needed for numerical stability and convergence. Layer normalisation controls the mean and variance of each activation vector.
- The feed-forward network transforms each position on its own. It is two linear transformations with a ReLU between them, applied identically and independently to every position. The original model used d_model = 512, and the inner layer expands to four times the model width before projecting back.
- A second residual connection and normalisation close the layer, and the output becomes the input to the next identical layer.
The two halves of the layer have separate jobs. Attention moves information between positions. The feed-forward network adds non-linearity and capacity within each position, and without that non-linearity a stack of linear layers would collapse into one linear map. People often picture the transformer as mostly attention, but the feed-forward layers hold most of a transformer’s parameters.
Existing large language models keep this anatomy and change the details. The original transformer placed normalisation after the residual addition (post-norm), which was hard to train and needed a learning-rate warm-up. Many recent large models put it before the sub-layer instead (pre-norm), GPT-3 and PaLM among them, because pre-norm makes very deep networks easier to train. Some models use RMSNorm, a cheaper variant of layer normalisation that Llama adopts. Others swap ReLU in the feed-forward network for GeLU or SwiGLU.
Why transformers need positional encoding
Without position information, a transformer has no notion of word order. Self-attention is permutation-equivariant: shuffle the input tokens and the outputs shuffle in exactly the same way, so the architecture has no built-in notion of sequence order. Positional information is what makes “man bites dog” come out differently from “dog bites man”.
The original paper added a positional encoding vector to each input embedding at the bottom of the encoder and decoder stacks. Those vectors were built from sine and cosine functions of different frequencies. The authors also tried learned positional embeddings and found almost identical results. They kept the sinusoidal version because it might extrapolate to sequences longer than those seen in training.
Later models went in different directions. GPT-1 used learned position embeddings. Rotary Position Embeddings (RoPE) rotate query and key vectors so that attention scores depend on the relative distance between tokens, and nothing is added to the input. ALiBi applies a fixed, untrained distance penalty to attention scores, and in the reported results it extrapolated to longer sequences better than sinusoidal encoding or RoPE. Which scheme a model uses matters most when you plan to run it on longer contexts than it was trained on.
Where the quadratic cost of attention shows up
Full attention compares every token with every other token, so compute and memory scale as O(n²·d) in sequence length. “A Survey of Large Language Models” calls this quadratic complexity a burden for long sequences. Sparse variants reduce it by letting each query attend only to a subset of positions, and GPT-3 used locally banded sparse attention. Multi-query and grouped-query attention share key and value projections across heads to speed up inference, at a small cost in quality.
FlashAttention works differently. It reorganises attention into blocks, without trading off model quality, to make better use of the GPU’s fast SRAM over slower HBM. It ships as a fused CUDA kernel integrated into PyTorch, DeepSpeed and Megatron-LM. According to the same survey, FlashAttention-2 roughly doubled the speed of the original by partitioning work across thread blocks and warps more effectively.
Are transformers only used for NLP?
No. Transformers work on images, audio, proteins and mixed inputs, usually after the input has been turned into tokens of some kind. The Vision Transformer paper showed that a pure transformer applied directly to sequences of image patches performs very well on image classification, with no convolutional backbone. The survey “Attention mechanisms in neural networks” adds a condition: vision transformers match or beat CNNs when trained on enough data. If you are weighing a vision transformer against a CNN for image classification, training data volume is one condition to check.
Speech follows the same pattern. The audio becomes a spectrogram, the spectrogram is cut into patches like an image, and the patches are fed in as token embeddings. Attention-based models have also been applied to protein structure prediction with AlphaFold, and to genomics and drug discovery. Multimodal models such as LLaVA and Qwen-VL encode an image with a vision transformer or CLIP-style encoder, project the visual features into the language model’s embedding space, and run them through the same causal transformer blocks as the text.
This range matters for how we read the phrase “generative AI”. Generative AI is broader than large language models. It covers models that generate images, audio and 3D assets, not only text.
How GPT and BERT are built from the same transformer blocks
GPT and BERT are built from the transformer layers described above. They differ in whether they keep the encoder or the decoder and in how they mask attention. The Hugging Face course groups transformer models into three families: GPT-like (auto-regressive), BERT-like (auto-encoding) and T5-like (sequence-to-sequence).
| Variant | Attention | Typical use | Examples |
|---|---|---|---|
| Encoder-only | Bidirectional: every token sees the whole input | Text embeddings and representation learning for downstream tasks | BERT |
| Decoder-only | Masked: each token sees only earlier tokens | Next-token generation, autoregressive or “causal” models | GPT series, Chinchilla |
| Encoder-decoder | Encoder self-attention plus decoder cross-attention over the encoder output | Text generation and instruction following | T5 series, original transformer |
BERT, short for Bidirectional Encoder Representations from Transformers, pre-trains deep representations by conditioning on both left and right context in every layer.
GPT went the other way. OpenAI adapted its language modelling work to the transformer and released GPT-1 in 2018: a generative, decoder-only model trained with unsupervised pre-training followed by supervised fine-tuning. GPT-1 was a 12-layer decoder with masked self-attention, 768-dimensional states, 12 attention heads and 3072-dimensional feed-forward inner states. Its authors tested the architecture choice directly. Swapping the transformer for a single-layer 2048-unit LSTM in the same framework cost an average of 5.6 points across their tasks.
GPT-style large language models are this decoder recipe scaled up. “A Survey of Large Language Models” describes LLMs as transformer language models built from stacked multi-head attention layers, with model size, data and compute increased by orders of magnitude. It credits the transformer’s parallelisability and capacity for making it the de facto backbone that lets language models scale to hundreds of billions of parameters. GPT-3 and PaLM went to 175B and 540B parameters respectively. Those models keep a transformer architecture similar to the 2017 original, with the refinements covered above.
What the architecture decides before you write any code
Encoder-only, decoder-only and encoder-decoder models suit different uses: embeddings, next-token generation, or generating text from an input sequence. For deployment, feed-forward layers hold most parameters, attention cost grows quadratically with context, and the positional scheme affects extrapolation to longer sequences.
Running a transformer within a latency or memory budget depends on attention variants and on kernels such as FlashAttention. For transformer work in generative AI, see TechnoLynx’s Generative & Agentic AI R&D.