O&O ConsultingBook a free call

Home / Insights / AI Foundations

How Transformers Work: Attention Without the Mathematics Overload

The architecture behind modern language models, explained through the idea of attention.

O&O Consulting · · 2 min read

Programming code representing a model architecture

The transformer was introduced in a 2017 paper titled "Attention Is All You Need". It was designed for translation and became the basis for most large language models.

The problem it solved

Earlier recurrent networks read text one step at a time, carrying a summary forward. Information from the beginning of a long passage could fade, and the step-by-step order prevented efficient parallel computation. Attention mechanisms, introduced for translation by Bahdanau and colleagues, let a model look back at relevant words. The transformer removed recurrence and kept attention.

The main stages of a transformerIllustrative diagram, not data
Probabilities for the next tokenFeed-forward layers transformSelf-attention mixes contextPosition information addedTokens turned into vectors
Probabilities for the next tokenFeed-forward layers transformSelf-attention mixes contextPosition information addedTokens turned into vectors

Diagram by O&O Consulting.

Self-attention in plain terms

Text is split into tokens, pieces of words, and each becomes a vector. For every token the model forms three vectors: a query (what am I looking for?), a key (what do I offer?) and a value (the information to pass on). Each token's query is compared with all the keys, producing weights. The token's new representation is a weighted mix of the values.

In the sentence "the trophy did not fit in the case because it was too large", attention gives the model a means of linking "it" to the relevant noun. Several attention "heads" run side by side, each free to track a different relationship.

The rest of the block

Attention is followed by a small feed-forward network applied to each position, with shortcut connections and normalisation. Dozens of such blocks are stacked. Because attention itself ignores order, information about position is added to the tokens.

A model that generates text is only allowed to attend to earlier tokens. It outputs a probability for each possible next token, one is chosen, and the process repeats.

Limitations and open questions

Standard attention compares every token with every other, so doubling the input length roughly quadruples that part of the work. Faster kernels and alternative architectures, including state space models, aim to reduce the cost. Attention weights are also not a dependable explanation of why a model produced an answer.

References

This article is general information, not financial, legal or investment advice. AI research and rules change quickly; check the cited sources for the current position.