Book a demo →

Transformer Architecture

Definition

The transformer architecture (transformer model) uses self-attention to relate every token to every other. Parts, variants, costs and limits, with dates.

In short

The transformer architecture processes text as a sequence of tokens and lets each token weigh every other token through self-attention. Vaswani et al. introduced it on 12 June 2017. It processes the tokens of a sequence in parallel during training and generates output token by token. IBM calls it the architecture “most prominently associated with large language models”.

What the transformer architecture is

The transformer is a neural network architecture that handles text, or any other sequence, as a series of tokens and lets each token draw on every other token through a mechanism called self-attention. IBM Think (published 28 March 2025) calls it “a type of neural network architecture that excels at processing sequential data, most prominently associated with large language models (LLMs)”. NVIDIA's blog (25 March 2022) describes what that achieves: “A transformer model is a neural network that learns context and thus meaning by tracking relationships in sequential data like the words in this sentence.”

“Transformer architecture” and “transformer model” name the same design, and this entry treats them as one term. NVIDIA separates the term from “the shape-shifting toy robots on TV” and “the trash-can-sized tubs on telephone poles”. The word also names Hugging Face's software library, which its documentation (retrieved 5 December 2026) calls “the model-definition framework for machine learning models in text, computer vision, audio, video, and multimodal tasks”.

Where it comes from

Bahdanau, Cho and Bengio (first posted 1 September 2014) wrote that the neural machine translation models proposed recently “often belong to a family of encoder–decoders”: one network, the encoder, reads the source sentence, and a second, the decoder, writes the translation. They saw the weak point in the single vector that passed between the two: “we conjecture that the use of a fixed-length vector is a bottleneck in improving the performance of this basic encoder–decoder architecture”. Their model looked back at the source sentence for the parts relevant to each word it wrote. That idea is attention. Luong, Pham and Manning opened their paper of 17 August 2015 with: “An attentional mechanism has been used in neural machine translation (NMT) to selectively focus on parts of the source sentence during translation.”

These models still ran on recurrent neural networks (RNNs), which process one position after the other. Vaswani et al. named the cost of that in “Attention Is All You Need”, first posted on arXiv on 12 June 2017: “This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths”. They removed the recurrence and kept attention. Google's research blog presented the result on 31 August 2017: “we introduce the Transformer, a novel neural network architecture based on a self-attention mechanism”. The name came from inside the team. NVIDIA reports: “Jakob Uszkoreit, a senior software engineer on the team, came up with the name Transformer.”

Paper and dateWhat it added
Bahdanau, Cho, Bengio (1 Sep 2014)Attention over the source sentence in an encoder-decoder translation model
Luong, Pham, Manning (17 Aug 2015)Global and local attention for translation
Vaswani et al. (12 Jun 2017)An encoder-decoder built on attention alone, without recurrence

How it works

The original transformer has two halves. The encoder reads the input, the decoder writes the output, and each half is a stack of identical layers. The paper states: “The encoder is composed of a stack of N = 6 identical layers.” An encoder layer has two sub-layers, self-attention and a feed-forward network. A decoder layer adds a third that attends to the encoder's output. Each sub-layer is wrapped in a residual connection and a layer normalization. Before the first layer, every token becomes a vector of 512 numbers, its embedding.

Attention is the central operation. The paper defines it: “An attention function can be described as mapping a query and a set of key-value pairs to an output”. Every token computes three vectors from its embedding: a query, a key and a value. IBM describes the query as the information a token is “seeking”, the keys as “the information that each token contains”, and the value as what “returns” the information from each key. To decide how much one token takes from another, the model compares the first token's query with the second token's key. In the paper's words: “We compute the dot products of the query with all keys, divide each by √dk, and apply a softmax function to obtain the weights on the values.” Here dk is the length of the key vectors, 64 in the original model. Each token's output is a weighted mix of all values in the sequence.

The transformer runs several of these calculations side by side. “Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.” The original model uses “h = 8 parallel attention layers, or heads”. Ashish Vaswani, who led the work, put the idea this way to NVIDIA: “Meaning is a result of relationships between things, and self-attention is a general way of learning relationships”.

Attention by itself carries no word order. The paper notes that the model “contains no recurrence and no convolution”, so “we must inject some information about the relative or absolute position of the tokens in the sequence”. The original adds a positional encoding to each embedding, built from sine and cosine waves of different wavelengths.

After attention, each position passes on its own through the same two-layer network, the feed-forward sub-layer. Its input and output have 512 dimensions, and “the inner-layer has dimension 2048”.

The decoder must not see tokens it has not written yet. The paper: “We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.” In its extra sub-layer, the queries come from the decoder and the keys and values from the encoder's output, so each word of the translation can draw on the whole source sentence.

Both sides repeat one block six times. The decoder adds a mask and a second attention step that reads the encoder's output. Source: Vaswani et al., “Attention Is All You Need”, arXiv, 12 June 2017 (v1, Section 3 and Figure 1).

Three body plans

Encoder only. Google's BERT (first posted 11 October 2018) spells out the name: “BERT, which stands for Bidirectional Encoder Representations from Transformers”. BERT has no decoder, and in every layer each token sees the input on both sides, to its left and to its right. IBM notes that BERT “remains the basis of most modern word embedding applications, from modern vector databases to Google search”.

Decoder only. OpenAI's first GPT model, described on 11 June 2018, used the masked half alone: “We trained a 12-layer decoder-only transformer with masked self-attention heads (768 dimensional states and 12 attention heads).” IBM puts GPT-3 in this group: “Autoregressive decoder-only LLMs, such as the GPT-3 (short for Generative Pre-trained Transformer) model that powered the launch of OpenAI’s ChatGPT, catalyzed the modern era of generative AI”.

Encoder and decoder. This is the original layout, built for translation, where one text becomes another. Google's T5 (first posted 23 October 2019) uses it for a framework “that converts every language problem into a text-to-text format”.

The same blocks also work on images. The Vision Transformer paper (first posted 22 October 2020) found that “a pure transformer applied directly to sequences of image patches can perform very well on image classification tasks”. A transformer can also serve as the backbone of a diffusion model; the entry on diffusion models gives the paper and its date. See also vision-language models.

Body planExample with date
Encoder and decoderOriginal transformer (12 Jun 2017), T5 (23 Oct 2019)
Encoder onlyBERT (11 Oct 2018)
Decoder onlyGPT (11 Jun 2018), GPT-3 (28 May 2020)
Encoder on image patchesVision Transformer (22 Oct 2020)

Examples with numbers and dates

The 2017 paper tested the transformer on translation and reported the result as BLEU scores. In the first arXiv version: “Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task”. The authors “trained our models on one machine with 8 NVIDIA P100 GPUs”. The base model took “100,000 steps or 12 hours”, and the big model “Training took 3.5 days on 8 P100 GPUs.”

The English-to-French figure depends on the version of the paper. The first arXiv version (12 June 2017) reports a BLEU score of 41.0. The abstract in the NeurIPS 2017 proceedings gives 41.1, and for English-to-German 27.5 for a “single model with 165 million parameters”. Version 7 on arXiv (2 August 2023) gives 41.8 in its abstract and in Table 2, while its body text still reads 41.0. The 28.4 for English-to-German stands in both arXiv versions.

Model and dateFigure from the source
Transformer, big (12 Jun 2017)28.4 BLEU English-German; 3.5 days on 8 P100 GPUs (arXiv v1)
GPT (11 Jun 2018)12 layers, 768-dimensional states, 12 attention heads (OpenAI paper)
GPT-3 (28 May 2020)175 billion parameters (arXiv abstract)
Llama 3 (31 Jul 2024)Largest model 405 billion parameters, context window up to 128K tokens (arXiv abstract)

Meta's Llama 3 report (31 July 2024) still uses the same basic block: “Our largest model is a dense Transformer with 405B parameters and a context window of up to 128K tokens.”

Reading is parallel, writing is not

“Parallel” applies to training and to the layers. Wikipedia (retrieved 5 December 2026): “Transformers allow parallel processing of tokens during training”. During training a causal attention mask “prevents each position from attending to later tokens, allowing the sequence to be processed in parallel while the loss is computed for target tokens”. Writing works differently. The paper says the model is auto-regressive at each step, “consuming the previously generated symbols as additional input when generating the next.” Wikipedia: “During inference, tokens are generated one or more at a time and appended to the context.” The entry on the KV cache covers the key and value vectors an inference system keeps from earlier tokens while the next ones are generated. The entry on AI inference explains what running a trained model means in the sources.

Attention also has a cost. Every token is compared with every other token, so the work grows with the square of the sequence length. Table 1 of the 2017 paper gives the complexity per layer as O(n² · d) for self-attention and O(n · d²) for a recurrent layer, where n is the number of tokens and d the length of each vector. Self-attention needs a constant number of sequential steps, a recurrent layer n of them. The authors add that “self-attention layers are faster than recurrent layers when the sequence length n is smaller than the representation dimensionality d”. Long inputs reverse that. The FlashAttention paper (27 May 2022): “Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length.” A longer context window therefore costs more than proportionally.

Two lines of work respond to this. FlashAttention keeps exact attention and reduces memory traffic, which its authors describe as “making attention algorithms IO-aware”. Others replace attention. Gu and Dao (first posted 1 December 2023): “Many subquadratic-time architectures such as linear attention, gated convolution and recurrent models, and structured state space models (SSMs) have been developed to address Transformers' computational inefficiency on long sequences, but they have not performed as well as attention on important modalities such as language.” Their own model, Mamba, is a state space model of that kind. The authors report that “our Mamba-3B model outperforms Transformers of the same size and matches Transformers twice its size” on language modeling.

Large language model. A large language model is a trained model; the transformer is the architecture IBM links most prominently to it.

Tokens and embeddings. A transformer works on tokens, and its first step turns them into embeddings.

Mixture of experts. A variant of the transformer in which the feed-forward sub-layer is split into several experts; see mixture of experts.

Foundation model. NVIDIA (25 March 2022) notes that Stanford researchers called transformers foundation models in an August 2021 paper (Bommasani et al., first posted 16 August 2021). The report's definition covers any model trained on broad data that can be adapted to many tasks and names no architecture; see the entry on foundation models.

Softmax. Each attention layer and the output layer use it to turn scores into weights and next-token probabilities; see softmax.

Source

  • IBM Think, “What is a transformer model?” (28 March 2025). Source for the definition, the description of query, key and value and the encoder-only and decoder-only examples (retrieved 2026-12-05): https://www.ibm.com/think/topics/transformer-model
  • NVIDIA Blog, Rick Merritt, “What Is a Transformer Model?” (25 March 2022). Source for the definition, the name, the other meanings of the word, Vaswani's statement and the term foundation model (retrieved 2026-12-05): https://blogs.nvidia.com/blog/what-is-a-transformer-model/
  • Wikipedia, “Transformer (deep learning)”. Source for parallel processing during training, the causal mask and generation at inference (retrieved 2026-12-05): https://en.wikipedia.org/wiki/Transformer_(deep_learning)
  • Hugging Face, Transformers documentation. Source for the library of the same name (retrieved 2026-12-05): https://huggingface.co/docs/transformers/index
  • Bahdanau, Cho and Bengio, “Neural Machine Translation by Jointly Learning to Align and Translate” (arXiv 1409.0473, first version of 1 September 2014). Source for the fixed-length vector and attention in translation (retrieved 2026-12-05): https://arxiv.org/abs/1409.0473
  • Luong, Pham and Manning, “Effective Approaches to Attention-based Neural Machine Translation” (arXiv 1508.04025, first version of 17 August 2015). Source for global and local attention (retrieved 2026-12-05): https://arxiv.org/abs/1508.04025
  • Vaswani et al., “Attention Is All You Need” (arXiv 1706.03762, first version of 12 June 2017). Source for the architecture, all model sizes, training time, BLEU 28.4 and 41.0 and the complexity table (retrieved 2026-12-05): https://arxiv.org/abs/1706.03762v1
  • Vaswani et al., “Attention Is All You Need”, version 7 (2 August 2023). Source for BLEU 41.8 in the abstract and Table 2 (retrieved 2026-12-05): https://arxiv.org/abs/1706.03762v7
  • Vaswani et al., “Attention Is All You Need”, NeurIPS 2017 proceedings abstract. Source for BLEU 27.5 and 41.1 (retrieved 2026-12-05): https://papers.nips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html
  • Google Research Blog, Jakob Uszkoreit, “Transformer: A Novel Neural Network Architecture for Language Understanding” (31 August 2017). Source for the public presentation (retrieved 2026-12-05): https://research.google/blog/transformer-a-novel-neural-network-architecture-for-language-understanding/
  • OpenAI, “Improving Language Understanding by Generative Pre-Training” (11 June 2018). Source for the decoder-only GPT model (retrieved 2026-12-05): https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf
  • Devlin et al., “BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding” (arXiv 1810.04805, first version of 11 October 2018). Source for the encoder-only design (retrieved 2026-12-05): https://arxiv.org/abs/1810.04805
  • Raffel et al., “Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer” (arXiv 1910.10683, first version of 23 October 2019). Source for the text-to-text framework (retrieved 2026-12-05): https://arxiv.org/abs/1910.10683
  • Brown et al., “Language Models are Few-Shot Learners” (arXiv 2005.14165, 28 May 2020). Source for GPT-3 and its size (retrieved 2026-12-05): https://arxiv.org/abs/2005.14165
  • Dosovitskiy et al., “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale” (arXiv 2010.11929, first version of 22 October 2020). Source for the Vision Transformer (retrieved 2026-12-05): https://arxiv.org/abs/2010.11929
  • Bommasani et al., “On the Opportunities and Risks of Foundation Models” (arXiv 2108.07258, 16 August 2021). Source for the term foundation model (retrieved 2026-12-05): https://arxiv.org/abs/2108.07258
  • Dao et al., “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness” (arXiv 2205.14135, 27 May 2022). Source for the quadratic cost and IO-aware attention (retrieved 2026-12-05): https://arxiv.org/abs/2205.14135
  • Gu and Dao, “Mamba: Linear-Time Sequence Modeling with Selective State Spaces” (arXiv 2312.00752, first version of 1 December 2023). Source for the spread of the transformer, subquadratic alternatives and the Mamba-3B result (retrieved 2026-12-05): https://arxiv.org/abs/2312.00752
  • Llama Team, AI @ Meta, “The Llama 3 Herd of Models” (arXiv 2407.21783, first version of 31 July 2024). Source for the size and context window of Llama 3 (retrieved 2026-12-05): https://arxiv.org/abs/2407.21783

Frequently asked questions

What is the transformer architecture and how does it work?

It is a neural network design built from a stack of identical blocks. In each block, self-attention lets every token weigh every other token in the sequence, and a small feed-forward network then processes each position. Positional encodings add word order, and when the model writes text, a mask keeps each position from seeing later ones.

Can you provide a diagram of a transformer?

This entry has one in the section on how it works: tokens, embeddings with positional encoding, the stack of identical layers and the output. The original drawing is Figure 1 of Vaswani et al. (12 June 2017), with the caption “Figure 1: The Transformer - model architecture.”, available on arXiv under 1706.03762.

Is GPT a transformer model?

Yes. GPT stands for Generative Pre-trained Transformer, as IBM spells out. OpenAI's first GPT model, described on 11 June 2018, uses only the decoder half of the original transformer, with masked self-attention, so it can write text one token after another. GPT-3 (28 May 2020) follows the same design at 175 billion parameters.

What is the difference between transformers and LLMs?

The transformer is an architecture; a large language model is a trained model. Gu and Dao wrote in December 2023 that foundation models are “almost universally based on the Transformer architecture”, and Meta describes its largest Llama 3 model (31 July 2024) as a dense transformer with 405 billion parameters. The two terms are not identical, though: Mamba (1 December 2023) is a sequence model built on selective state spaces instead of attention.

Why did transformers replace RNNs?

Vaswani et al. (12 June 2017) gave the reason they moved away from recurrence. A recurrent network computes one position after the other, which prevents parallel training within a sequence. Self-attention connects any two positions in a constant number of steps. The price is a cost that grows with the square of the input length, and writing proceeds token by token.

What is a transformer called?

In machine learning, transformer is the name of this architecture, and networks built on it are called transformer models or transformers. According to NVIDIA, Jakob Uszkoreit chose the name. NVIDIA separates it from “the shape-shifting toy robots on TV” and “the trash-can-sized tubs on telephone poles”. The Hugging Face Transformers library is a software framework of the same name.