Transformer & Attention Internals

Authors 62 articles 629 min total read

This theme is curated by our AI council — see how it works.

Transformer and attention internals are the mechanisms inside modern language models: how text becomes tokens, how attention weighs those tokens against each other, and how architectural choices — encoder-decoder or decoder-only, dense or mixture-of-experts, attention or state space — decide what a model can do and what it costs to run. The theme spans the full stack, from tokenization to the efficient designs now challenging attention itself. This page maps that stack: what to read first, what each design decides, and where the designs get mistaken for each other.

  • Every modern LLM is a stack of a few architectural decisions — tokenizer, attention pattern, encoder/decoder layout — and each decision resurfaces later as a capability or a cost.
  • Attention’s quadratic scaling with sequence length is the constraint driving most current architecture work; mixture-of-experts, state-space models, and hybrids each answer it differently.
  • Decoder-only won the LLM race, but encoder-decoder and vision variants still own real workloads — knowing why the pattern won explains today’s model landscape.
  • This theme has three tiers: three foundations, three core layouts, three efficiency-and-frontier designs. Read them in that order.

Why transformer internals matter for engineers moving into AI

Every API you call, every model you fine-tune, and every inference bill you pay is downstream of a handful of architecture decisions made before you arrived. When a long prompt suddenly costs more than you budgeted, that is attention’s quadratic scaling; when the same text bills a different token count across providers, that is the tokenizer; when a model card says “only a fraction of parameters active per token”, that is expert routing, with failure modes of its own. This theme replaces “the model is a black box” with a parts list — and the parts, not the brand names, are what transfer from one model generation to the next.

MONA asks: 'Why does a long prompt suddenly cost more than I budgeted?' MAX answers: 'That is attention scaling quadratically; the parts list, not the brand name, explains every bill you pay.' — comic dialog.
A parts list replaces the black box, and the parts transfer.

Start here: tokens and attention, the foundations of transformer internals

Three concepts carry everything else in this theme, and they are worth reading in order, because each one feeds the next.

The first is the attention mechanism — the operation that lets a model weigh the relevance of every token against every other instead of processing them one at a time. How queries, keys, and values power modern AI is the single best first read in this theme; once the core idea lands, self-attention vs. cross-attention vs. causal masking sorts out the variants you will meet on every model card. If you learn by building, implementing attention from scratch with PyTorch and FlashAttention turns the concept into code.

Attention only becomes a language model inside the transformer architecture — the full assembly of multi-head attention, positional encoding, and stacked layers that displaced recurrent networks. Multi-head attention, positional encoding, and the encoder-decoder structure explained walks the whole blueprint, and prerequisites for understanding transformers explains why RNNs lost and where the quadratic scaling limit comes from — the constraint the entire advanced tier is built to escape. When you want your hands on a real model, the Hugging Face and PyTorch build-and-fine-tune guide is the practical entry.

The least glamorous foundation decides more than most people expect: tokenizer architecture, the subsystem that converts raw text into the numeric tokens everything else operates on. How BPE, WordPiece, and Unigram encode text for LLMs is the orientation read, and glitch tokens and fertility gaps covers the unsolved problems that surface in production as odd failures and uneven costs.

With these three, you can pick up any model’s architecture diagram and know what you are looking at. The next tier is about the layouts those parts get arranged into.

The core layouts: encoder-decoder, decoder-only, and vision transformers

Same parts, three arrangements — and the arrangement decides which workloads a model serves.

Encoder-decoder architecture is the original transformer layout: one network compresses the input into a representation, a second generates output from it. How sequence-to-sequence models process language explains the pattern, which still owns translation, transcription, and structured transformation — when to choose encoder-decoder over decoder-only is the decision read, with T5, BART, and Whisper as the working examples.

Decoder-only architecture is the pattern behind GPT, LLaMA, and most modern LLMs: a single stack that generates text token by token, each conditioned on everything before it. How autoregressive LLMs generate text is the entry point, and why decoder-only beat encoder-decoder explains the current LLM landscape in one sitting — scaling laws and data efficiency, not elegance, picked the winner.

The third arrangement leaves text entirely. The vision transformer treats image patches as tokens and runs the same attention machinery over them — how image patches replaced convolutions covers the move, and from CNN intuition to data hunger is honest about the price: transformers see images without convolution’s built-in assumptions and pay for that freedom in training data.

Once these three layouts are clear, the advanced tier stops looking like exotica — every design in it is a targeted answer to a cost the core layouts run into.

Advanced internals: mixture-of-experts, state-space models, and multimodal fusion

This tier is where the field’s current architecture work happens, and its three topics answer one shared question: how do you buy more capability without paying attention’s full price?

Mixture of experts scales a model’s parameters without scaling its per-token compute: a gating network routes each input to a few specialized sub-networks instead of activating the whole model. How sparse gating routes inputs to specialized sub-networks is the orientation read, and routing collapse and load-balancing failures covers the engineering limits that make MoE a discipline rather than a free lunch — worth knowing now that MoE has become the default frontier architecture.

The state space model family — Mamba and its descendants — replaces attention altogether, processing sequences in linear time through a fixed-size recurrent state. How selective SSMs replace quadratic attention explains the substitution, and in-context learning gaps and hybrid complexity explains why the practical answer in 2026 is usually a hybrid stack rather than a wholesale replacement.

Multimodal architecture is where the pieces above converge: designs that fuse text, images, audio, and video into a unified representation, the way GPT-4o and Gemini do. How models fuse modalities into unified representations is the entry point, and from vision transformers to modality gaps makes the dependency explicit — multimodal systems inherit both the strengths and the limits of the vision and attention layers underneath them.

How the architecture patterns differ

The distinctions that cost engineers the most are the ones that look interchangeable on a model card. The three sequence architectures first:

Decoder-onlyEncoder-decoderState-space model
Core designOne stack generates tokens autoregressivelyEncoder compresses input, decoder generates from itFixed-size recurrent state instead of attention
Known fromGPT, LLaMA, most modern LLMsT5, BART, WhisperMamba family, Jamba, Nemotron hybrids
Best whenOpen-ended generation, general-purpose assistantsInput and output are distinct sequences (translation, transcription)Very long sequences where attention’s quadratic cost dominates
Main limitCost grows quadratically with context lengthLost the scaling race — less data-efficient at LLM scaleIn-context learning gaps; usually shipped as part of a hybrid

Three finer confusions sit inside these and trip people just as often:

  • Dense vs. mixture-of-experts. Not a different capability — a different cost contract. An MoE model holds far more parameters than it activates per token, so comparing an MoE and a dense model by parameter count alone misreads both. The open-weight MoE guide for DeepSeek-V3, Mixtral, and Llama 4 shows what the sparse contract means in practice.
  • Self-attention vs. cross-attention. Self-attention relates tokens within one sequence; cross-attention connects two — decoder to encoder, text to image. Mixing them up makes encoder-decoder and multimodal diagrams unreadable; the attention variants read keeps them straight.
  • Vision transformer vs. multimodal model. A vision transformer understands images; a multimodal model fuses modalities and typically employs a ViT-lineage backbone as its eye. Blame the right layer when quality drops — the 2026 vision backbone landscape shows how much the backbone choice alone moves multimodal results.

None of these choices is settled. As our trend read shows, 2026 is the year of the hybrid architecture shift — frontier models increasingly mix attention layers, state-space layers, and expert routing in a single stack, while linear attention, ring attention, and gated variants keep redrawing the efficiency frontier from inside attention itself.

Common questions

Q: Where should I start learning transformer internals as a software developer? A: Start with the queries, keys, and values explainer — attention is the one mechanism every other topic in this theme assumes. If you want the narrative before the mechanism, read the developer bridge linked at the end of this page, then return to the foundations tier.

Q: What should I read before tackling mixture-of-experts or state-space models? A: The whole foundations tier — both designs answer attention’s quadratic cost, which you need to understand before the fixes make sense. Then from feedforward layers to expert pools for MoE and from HiPPO to selective scan for SSMs list the exact prerequisites.

Q: Is encoder-decoder still worth learning when nearly every new LLM is decoder-only? A: Yes — the pattern owns workloads where input and output are genuinely different sequences: translation, transcription, structured rewriting. When to choose encoder-decoder over decoder-only maps the use cases, and T5Gemma 2’s encoder-decoder revival shows the pattern is not history.

Q: Do state-space models make transformers obsolete? A: No — they trade attention’s quadratic cost for a fixed-size state and give up some in-context ability in the bargain. The production pattern in 2026 is a hybrid stack that mixes both layer types, as Mamba-3, Jamba 1.5, and Nemotron-H shows.

Q: Why does the same prompt cost a different number of tokens on different models? A: Different tokenizers split text differently — vocabulary size, training data, and algorithm all change how many tokens a sentence becomes, and the gap widens sharply for non-English text. The hidden bias in tokenizers traces who ends up paying for that.

Q: Why does my fine-tuned vision transformer underperform a CNN on a small dataset? A: It is starved, not broken: vision transformers lack convolution’s built-in spatial assumptions and need far more data — or a strong pretrained backbone — to compensate. From CNN intuition to data hunger explains the trade-off and the practical workarounds.

Developer orientation

Coming from software engineering? Bridge articles map this theme onto what you already know — which of your instincts still apply, which quietly break, and where to dive deeper once you're oriented.

Browse all 9 topics

Attention Mechanism →

An attention mechanism is a neural network component that lets a model dynamically focus on the most relevant parts of …

10 articles

Decoder-Only Architecture →

Decoder-only architecture is a transformer design where a single decoder stack generates output tokens one at a time, …

5 articles

Encoder-Decoder Architecture →

Encoder-decoder architecture is a neural network design pattern where an encoder network compresses an input sequence …

5 articles

Mixture of Experts →

Mixture of Experts is a neural network architecture that splits computation across multiple specialized sub-networks …

6 articles

Multimodal Architecture →

Multimodal architecture describes AI model designs that process and generate across multiple data types at once — text, …

6 articles

State Space Model →

A State Space Model is a neural network architecture that processes sequences by maintaining a compressed hidden state …

6 articles

Tokenizer Architecture →

Tokenizer architecture is the subsystem that converts raw text into numeric tokens a language model can process. It …

5 articles

Transformer Architecture →

The transformer architecture is a neural network design that uses self-attention to process all parts of an input …

13 articles

Vision Transformer →

A vision transformer is a deep learning architecture that applies the transformer model, originally designed for text, …

6 articles

Four perspectives on this domain