Generative AI Fundamentals · 8 min read

Transformers Architecture

Learn why attention and transformers became the foundation of most modern language models.

Transformer architecture used to explain sequence processing and generation. Source: Wikimedia Commons · dvgodoy · CC BY 4.0

Why architecture matters

A model architecture is the arrangement of computations that turns input into output. It affects what patterns a model can learn, how efficiently it trains, and how it behaves at inference time. Transformers became important because they handle relationships across a sequence efficiently and can be scaled with large datasets and hardware. You do not need matrix algebra to understand the main intuition: a transformer repeatedly mixes information, highlights relevant context, and transforms that information into a prediction.

Tokens become vectors

A transformer does not read words as humans do. A tokenizer first breaks text into tokens, which may be words, word pieces, punctuation, or spaces. Each token is mapped to a list of numbers called an embedding. Similar uses can develop related representations, while surrounding context can change how a token is interpreted. For example, “bank” in a sentence about rivers and “bank” in a sentence about savings starts from the same token but can be processed differently after context is added.

Scaled dot-product attentionjavascript
const scores = (query, keys) =>
  keys.map(key => query.reduce((sum, value, i) => sum + value * key[i], 0));

const query = [1, 0];
const keys = [[1, 0], [0, 1]];
const values = ['relevant context', 'other context'];
const bestIndex = scores(query, keys).indexOf(Math.max(...scores(query, keys)));

console.log(values[bestIndex]); // relevant context

Position in a sequence

Attention can compare tokens, but comparison alone does not tell the model whether a word came first or last. Transformers therefore add positional information to token representations. This lets the network distinguish “dog chased cat” from “cat chased dog,” even though both sentences contain the same words. Different model families encode position in different ways. The practical lesson is that order is part of meaning, and long inputs require an architecture that represents position reliably across the available context.

Self-attention intuition

Self-attention lets each token look at other tokens and assign them different relevance weights. In “The child dropped the glass because it was slippery,” the model must use nearby and distant clues to interpret “it.” Each attention head can focus on a different relationship, such as subject, location, or word agreement. The result is a context-aware representation. Attention is not human attention or consciousness; it is a learned numerical operation that routes information between positions.

Queries, keys, and values

A helpful technical picture describes attention with queries, keys, and values. A token’s query asks what information it needs, other tokens provide keys describing what they contain, and matching scores decide which values to combine. The model learns the transformations that create these vectors. Multiple heads perform this process in parallel, allowing different relationship patterns to be considered. The combined result passes onward through more layers, where the network refines its interpretation.

Feed-forward layers

After attention mixes information across positions, a feed-forward layer processes each position through learned transformations. It can expand the representation, apply a nonlinear function, and compress it again. These layers help the model build richer features and are a large part of its capacity. Residual connections carry earlier information around each block, while normalization helps keep numerical behavior stable. Repeating attention and feed-forward blocks gives the model many opportunities to build increasingly abstract representations.

Training and inference

Many language models are trained with next-token prediction: hide or stop before the next token, ask the network to assign probabilities, and adjust weights when the prediction differs from the example. Training processes many sequences in parallel. During inference, the model receives a real prompt and generates one token at a time, often reusing cached intermediate information. This explains a key trade-off: training is expensive but parallelizable, while generation is sequential and can become slower for long responses.

Scale and limits

Larger transformers can represent more patterns, but size alone does not guarantee useful behavior. Data quality, training objectives, context length, fine-tuning, tool access, and evaluation all matter. Attention also becomes more expensive as context grows, and the model can lose track of details in a crowded prompt. A transformer predicts based on learned signals; it does not independently verify claims. Responsible applications add retrieval, validation, permissions, and human review around the architecture.