Chapter 08 of 16: Connect positions in a sequence

How does a model connect words across a sentence?

Arun MalikSeriesAIFoundations
Table of Contents
Builds on chapter 7. Specific dependencies are in the concept map.

“Put the cup beside the plate. Fill it.” A reader uses surrounding words to decide what “it” refers to. A language model needs a numerical way to let one position use information from others.

Order belongs in the input

A sequence is an ordered series of items. Text tokens form a sequence. “The cook helps Sam” and “Sam helps the cook” contain the same words but describe different events.

Positional information tells a model about order or relative locations in a sequence. Transformer designs introduce this information in different ways, including position-dependent vectors or transformations. Otherwise, simply comparing an unordered collection of token representations would lose an important clue.

Numbered recipe steps are a useful analogy. Knowing that “bake” comes after “mix” matters even when both steps are present.

SequencePosition changes what the sequence describes. A transformer needs information about order.1 Mix → 2 BakeOrder is part of the meaning1 Bake → 2 MixSame words, different sequence
Position changes what the sequence describes. A transformer needs information about order.

Let one position borrow information

Attention is a calculation that mixes information using weights based on relationships between representations. It does not mean the machine becomes conscious or looks at words as a person does.

The calculation has three useful roles. A query, key, and value are numerical representations used to request, match, and supply information in attention. A query is compared with keys to calculate mixing weights; those weights determine how much of each value contributes.

Picture a cook holding a question card and comparing it with labels on ingredient containers. Matching determines which containers contribute to the mixture. In the actual calculation, the cards and ingredients are vectors, and matching is learned arithmetic.

AttentionIllustrative attention links let the representation at “it” mix information from other positions. Line emphasis is explanatory, not an observed model measurement.cupbesideplateit
Illustrative attention links let the representation at “it” mix information from other positions. Line emphasis is explanatory, not an observed model measurement.

Self-attention draws its queries, keys, and values from representations within the same sequence. A representation at “it” can incorporate information associated with “cup.” Different positions receive different mixtures.

The drawing shows an illustrative connection, not a measured attention map from a model. Even an actual attention map would not by itself explain every cause of the final answer.

Repeat, combine, transform

Multi-head attention computes several attention mixtures using different learned projections and combines their results. It gives the network several ways to relate positions. Heads are not guaranteed to divide neatly into grammar, facts, and other named jobs.

A transformer is a neural-network architecture built around attention and other repeated processing operations. It is a foundational architecture, not a newly invented synonym for an agent.

A transformer block typically combines attention with a feed-forward network, residual connections, and normalization. The feed-forward part transforms each position's values. A residual connection adds an earlier representation back into the path. Normalization controls numerical scale within the computation. These help a stack process information without simply replacing everything at each step.

  • AttentionMix information across permitted positions.
  • Feed-forward transformationTransform values at each position.
  • Residual paths and normalizationCarry earlier values forward and control numerical scale.
Main jobs in a transformer block. Real variants differ in arrangement; residual paths and normalization support the computation.

Exact designs vary. The block picture shows the main jobs, not a wiring diagram for every model.

Reading and writing roles

An encoder builds representations of an input, often using information on both sides of a position. A decoder produces an output sequence, using earlier output and, in encoder-decoder designs, information from the encoder.

An attention mask restricts which positions can contribute. A causal decoder cannot use future answer tokens to predict the next one. During training, known target sequences let many positions be processed together while this restriction prevents peeking. During ordinary step-by-step generation, future output has not been chosen yet.

Encoder-only, decoder-only, and encoder-decoder are useful families, not a complete list of model designs. And not every AI model is a transformer.

MaskA schematic causal attention mask. Each row is a token position; each column is a position it could read. Cells on or below the diagonal are permitted; cells above it are blocked. This restricts access, not the truth of a token.Only current and earlier positions
  • Reading roleAn encoder can build input representations using both directions.
  • Writing roleA causal decoder uses earlier output without reading future answer tokens.
A schematic causal attention mask. Each row is a token position; each column is a position it could read. Cells on or below the diagonal are permitted; cells above it are blocked. This restricts access, not the truth of a token.
Can attention fetch a recipe that was never supplied?

Attention operates on representations available to that computation. Finding a separate recipe document requires another mechanism, such as retrieval. Attention is not an automatic connection to the internet.