VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

How Super Intelligence Works | Transformers — How Attention, Feed-Forward Layers, Residuals and KV Cache Process Sequences

Three learners review open books together at a classroom table, with stacks of textbooks, stationery and a whiteboard in the bright room.

A transformer is a neural-network architecture that processes sequences by repeatedly routing information with attention and transforming each position with learned feed-forward layers. It became the foundation of many modern language models because it can model relationships across many token positions while training much more in parallel than classic recurrent sequence models.

The transformer is not the whole Super Intelligence system. Tokenisation happens before the model. Retrieval, databases, tools, memory, permissions and verification can sit around it. The transformer itself is the learned numerical engine that receives vectors, transforms them through stacked blocks and produces new representations or next-token scores.

This guide explains how transformers work from first principles. We will assemble a transformer from token embeddings, positional information, query/key/value projections, scaled dot-product attention, multiple heads, feed-forward networks, residual connections and normalization. We will then follow the architecture through training, causal generation, prefill, decoding and KV caching.

In the eduKateSG series, Super Intelligence, or SI, is our umbrella term for AI technologies. Standard technical vocabulary remains visible so readers can connect this guide to the original research and current implementation documentation.

Previous: 015 — Neural Networks. Return to the How Super Intelligence Works hub for the complete series.


Transformers at a Glance

  • Tokens become vectors before transformer blocks process them.
  • Self-attention lets each token position combine information from other permitted positions.
  • Queries, keys and values are learned projections with different jobs inside attention.
  • Scaled dot-product attention scores query-key alignment, normalizes those scores and mixes value vectors.
  • Multi-head attention performs several learned attention projections in parallel.
  • Feed-forward networks transform each token position after attention.
  • Residual connections preserve a direct path through deep stacks; normalization stabilizes computation.
  • Causal masks prevent an autoregressive decoder from seeing future tokens.
  • A KV cache reuses earlier key/value computations during generation.
  • Retrieval, databases, tools and persistent memory are surrounding system components, not automatic transformer functions.

The Transformer’s Historical Starting Point

The 2017 paper Attention Is All You Need introduced the Transformer for sequence transduction. The original architecture used an encoder stack and a decoder stack built around attention, feed-forward networks, residual connections and normalization rather than recurrent or convolutional sequence processing.

The paper showed that the architecture could train in a highly parallel way because positions inside a training sequence did not need to be processed one after another through a recurrent hidden state. That property later became important for scaling language models on accelerators.

Modern transformer systems have diversified greatly. Encoder-only, decoder-only, encoder-decoder, multimodal, sparse-attention, grouped-query and mixture-of-experts architectures all belong to a broad transformer family without being identical to the 2017 design.

The Core Problem: Sequence Positions Must Exchange Information

A sequence model needs to relate information across positions. In “The blue key opens locker 9 because the red key is broken,” the representation of “locker 9” should connect strongly with “blue key”, while the second clause changes how the whole sentence is interpreted.

Older recurrent networks pass information forward through a sequential hidden state. Self-attention instead allows a position to compute weighted relationships directly with many other permitted positions. This gives the network a flexible information-routing mechanism.

Attention is not human attention. It is a differentiable mathematical operation over learned vectors.

Step 1: Token IDs Become Hidden Vectors

Article 013 showed how token IDs select learned embedding vectors. If a prompt has T token positions and the transformer hidden dimension is D, the sequence can initially be represented as a matrix with shape T×D for one example.

Each row is the current representation at one token position. Transformer blocks repeatedly transform that matrix while keeping a vector associated with every position.

The initial token embedding is only the starting representation. Later hidden states contain information mixed from context through attention and nonlinear transformations.

Step 2: Position Must Enter the Representation

If attention only received a bag of token vectors, the same set of tokens could look identical regardless of order. Language and code depend strongly on sequence order, so transformers need positional information.

The original Transformer paper used sinusoidal positional encodings. Later architectures use learned position embeddings, relative-position methods, rotary position embeddings and other approaches. “Transformer” therefore does not imply one universal positional mechanism.

The important function is to let computation distinguish where a token occurs and, in many architectures, reason about relative distance between tokens.

A Toy Position Example

Imagine the token “student” has base embedding [0.6,0.2]. At position 2 the architecture combines it with one position representation; at position 20 it combines it with another. The two resulting vectors differ even before attention mixes surrounding information.

A real model has far more dimensions and may encode position through rotations or relative biases rather than simple addition. The example shows why token identity alone is insufficient for ordered sequences.

Step 3: Build Query, Key and Value Vectors

Each hidden vector is transformed through learned matrices to produce a query, key and value. In compact matrix notation, Q=XWq, K=XWk and V=XWv.

Queries are used to ask which positions should matter for the current position. Keys are used to determine matching strength. Values contain the information that will be mixed into the output. These descriptions explain operational roles, not literal concepts stored inside the vectors.

The projection matrices are learned through training just like other neural-network weights.

Why Separate Queries, Keys and Values?

A position may need one representation for deciding which other positions are relevant and a different representation for the information it contributes when selected. Separate Q, K and V projections give the network those degrees of freedom.

This is analogous to a database lookup and returned record being different objects: the matching representation and the transmitted representation do not need to be identical.

Step 4: Query-Key Dot Products

For each query, attention computes dot products with key vectors. A large positive dot product indicates strong alignment under that learned head’s geometry. A small or negative score indicates weaker alignment before scaling and masking.

For a whole sequence, the operation QKᵀ produces a matrix of pairwise attention scores. If the sequence length is T, full self-attention creates T×T score relationships for each head before masking.

This is Article 014’s vector geometry turned into a learned routing mechanism.

A Tiny Attention-Score Example

Let q=[1,0]. Key k1=[0.9,0.1] gives raw dot product 0.9. Key k2=[0.2,0.8] gives 0.2. Under this toy projection, the query aligns much more strongly with k1.

These are not yet final weights. The scores will be scaled, any disallowed positions will be masked, and softmax will turn remaining values into a normalized distribution.

Why Scale by the Square Root of Key Dimension?

The original attention equation divides query-key dot products by √d_k. As vector dimension grows, raw dot products can grow in magnitude under common statistical assumptions. Very large score differences can push softmax into highly saturated regions.

Scaling keeps score magnitudes in a range that supports more stable optimization. This is why the canonical equation is called scaled dot-product attention.

Step 5: Apply the Attention Mask

Masks enforce structural constraints on which positions may contribute. A padding mask removes positions that exist only to make batch shapes equal. A causal mask prevents a decoder from using future tokens.

Current Hugging Face attention documentation distinguishes causal, bidirectional, sliding-window and chunked masks and notes that different optimized attention backends expect different mask representations. This is an implementation detail with real consequences: a wrong mask can change what information the model is allowed to use.

A mask is enforced numerically in attention. It is stronger than a natural-language request such as “please do not look at the answer”.

Causal Attention

In a standard autoregressive decoder, position 5 can attend to positions 1 through 5 but not to 6 or later. This guarantees that the model cannot directly inspect future target tokens when learning to predict the next token.

During training, all token positions can still be processed in parallel because the mask enforces this information boundary simultaneously across the sequence.

This is one of the transformer’s key scaling advantages: parallel training with autoregressive causality.

A Four-Token Causal Mask

For four positions, token 1 may attend only to 1. Token 2 may attend to 1 and 2. Token 3 may attend to 1,2,3. Token 4 may attend to all four. The allowed region forms a lower-triangular pattern.

If the mask is reversed or shifted incorrectly, training can leak future information or inference can hide valid past context. Mask construction is therefore part of the model contract.

Step 6: Softmax Turns Scores Into Weights

After scaling and masking, softmax converts the allowed scores into non-negative weights that sum to one for each query.

Suppose allowed scores are [2,1,0]. Softmax makes the first position largest, the second smaller and the third smaller again. The exact values arise from exponentiation and normalization.

Those weights determine how strongly this attention head mixes each corresponding value vector.

Step 7: Mix the Value Vectors

The attention output is a weighted sum of value vectors. If weights are [0.7,0.2,0.1], then output=0.7v1+0.2v2+0.1v3.

The result is another vector for the query position. It contains information routed from other positions according to learned alignment and the mask.

Attention therefore separates matching from content transfer: Q and K decide weights; V supplies what gets mixed.

The Scaled Dot-Product Attention Equation

The reference equation is Attention(Q,K,V)=softmax((QKᵀ)/√d_k + mask)V, with exact mask notation varying across implementations.

This one expression combines the representation geometry developed in Articles 013 and 014 with the differentiable learning loop from Article 015.

Step 8: Multi-Head Attention

Instead of one Q/K/V projection, multi-head attention runs several learned heads in parallel. Each head can represent a different matching geometry because its projection matrices are different.

Head outputs are concatenated and transformed through another learned matrix to return to the model’s hidden dimension.

Several heads do not guarantee one neat linguistic job per head. Some heads can be redundant, diffuse or context-dependent. Head visualizations are evidence about routing, not a complete explanation of model reasoning.

A Two-Head Conceptual Example

One head might align pronouns with earlier noun phrases, while another might track clause boundaries. A third model or training run could distribute those relationships completely differently.

The value of multi-head attention is architectural flexibility: several learned subspaces can contribute information before being recombined.

Step 9: Attention Output Projection

After concatenating head outputs, a learned output matrix projects the combined vector back into the residual-stream hidden dimension.

This means each attention block contains several parameter matrices even before the feed-forward network: query, key, value and output projections.

Step 10: Residual Connections

A residual connection adds the block’s input to the transformed sublayer output. Instead of replacing the entire representation, the attention sublayer can learn a modification that is added to the existing state.

Residual paths help deep networks train by allowing information and gradients to travel through short additive routes. They also define the persistent representation stream across transformer blocks.

Step 11: Layer Normalization

Layer Normalization stabilizes features inside a token representation using per-position statistics and learned scaling parameters. Transformer variants differ in exactly where normalization appears relative to attention and feed-forward sublayers.

Pre-norm architectures apply normalization before a sublayer; post-norm architectures apply it after residual combination in the classic design. This seemingly small ordering choice affects training stability in deep networks.

Step 12: The Feed-Forward Network

After attention has mixed information across positions, each position independently passes through a feed-forward neural network. A common design expands the hidden dimension, applies a nonlinear activation, then projects back.

Modern transformers can use GELU, SwiGLU and other activation/gating variants. Feed-forward networks often contain a large fraction of the model’s parameters.

Attention handles cross-position information routing; the feed-forward network performs nonlinear transformation at each position. A transformer needs both.

A Simplified Decoder Block

A common modern decoder block can be summarized as: hidden states → normalization → causal self-attention → residual addition → normalization → feed-forward network → residual addition.

Exact ordering and component variants differ by architecture, but this route captures the recurring pattern. Stack many blocks and token representations become increasingly contextualized.

Tensor Shape Walkthrough

Assume batch size B=2, sequence length T=4 and hidden dimension D=8. The hidden-state tensor has shape [2,4,8]. With two heads, a simple design can use head dimension 4.

Q, K and V are projected and reshaped to [2,2,4,4]: batch, heads, positions and head features. Attention scores then have shape [2,2,4,4], because each of four query positions is compared with four key positions inside each head.

After weighted value mixing, each head again returns [2,2,4,4]. Concatenating heads restores [2,4,8], allowing output projection and residual addition with the original hidden tensor.

Feed-Forward Shape Walkthrough

Keep hidden dimension D=8 and let the feed-forward layer expand to 32. The first linear projection maps [2,4,8] to [2,4,32]. The activation or gating operates there. The second projection returns to [2,4,8].

The same feed-forward weights are applied independently to every token position, which makes the operation highly parallel on accelerators.

Encoder Blocks

An encoder self-attention block normally lets every non-padding input position attend bidirectionally to every other allowed input position. The model receives the complete source sequence at once.

Encoder-style transformers are well suited to classification, representation learning, extraction and other tasks where the entire input is available.

Decoder Blocks

A decoder used for autoregressive language generation applies causal self-attention. Each position can use only itself and earlier positions.

Decoder-only transformers stack causal blocks and are the architecture family behind many general-purpose LLMs.

Encoder-Decoder Transformers

The original Transformer uses an encoder to build source representations and a decoder to generate a target sequence. The decoder contains both causal self-attention and cross-attention over encoder outputs.

This architecture is natural for translation, summarization and other sequence-to-sequence tasks where source and target play distinct roles.

Cross-Attention

In cross-attention, decoder hidden states produce queries while encoder output states provide keys and values. The query sequence and source sequence can have different lengths.

If the decoder currently has three target positions and the encoder source has six positions, one head’s cross-attention score matrix has shape 3×6.

Cross-attention lets the generated target selectively route information from the encoded source.

Training an Autoregressive Transformer

Suppose a training sequence is [t1,t2,t3,t4]. The model learns to predict t2 from t1, t3 from t1,t2, and t4 from t1,t2,t3. The causal mask prevents each position from directly reading later tokens.

A vocabulary projection produces logits for each prediction position. Cross-entropy loss rewards assigning high probability to the actual next token. Backpropagation updates embeddings, attention matrices, feed-forward weights, normalization parameters and other trainable values.

Many positions contribute losses in one parallel training pass.

Teacher Forcing

During standard next-token training, each prediction position conditions on the real earlier tokens from the training sequence. This avoids having to sample the model’s own output at every training step.

At generation time, however, the model’s selected token becomes part of the next context. A mistake can therefore influence later tokens. Training and inference have different sequence dynamics even though they use the same learned weights.

Final Hidden State to Vocabulary Logits

After the last transformer block, the hidden state at each prediction position is mapped to vocabulary logits. A softmax can convert those logits into a probability distribution over next-token candidates.

Some model families tie the output projection to the token-embedding matrix; others use different parameterizations. The final decoding strategy determines which token is selected.

Prefill

During inference, the first pass over the prompt is often called prefill. The model processes all existing context positions, builds hidden states and computes keys and values for attention layers.

Long prompts can make prefill expensive because many token relationships are processed before the first generated token appears.

Decode

After prefill, the model selects a next token. That token is appended conceptually to the sequence, and the model computes the next-token distribution again.

Autoregressive decoding is sequential at the token level: token N+1 depends on the already generated prefix.

Why KV Caching Matters

Without caching, every decode step would recompute key and value projections for all earlier tokens even though those earlier token representations have not changed for a standard causal decoder.

A KV cache stores the past key/value tensors so each new step can reuse them. Current Hugging Face cache documentation describes dynamic, static, offloaded and quantized strategies, each trading memory, compilation support and latency differently.

The cache changes inference efficiency. It does not update model weights and it is not persistent user memory.

A Worked KV-Cache Example

A prompt contains 1,000 tokens. Prefill computes the attention keys and values for all 1,000 positions. The model generates token 1001.

For token 1002, cached execution reuses the earlier 1,001 positions’ keys and values and computes only the new projections needed for the latest token. This saves repeated work but consumes memory to keep the cache.

As context grows, KV-cache memory can become a bottleneck even while computation per decode step remains manageable.

Dynamic Cache

A dynamic cache grows as generation proceeds. It allocates storage according to the accumulating sequence length and is a common default strategy.

This is flexible but makes some compilation optimizations harder because tensor shapes change across decode steps.

Static Cache

A static cache preallocates space for a configured maximum. Stable shapes can support compilation and predictable memory layouts.

The trade-off is wasted memory and computation on unused masked positions when actual sequences are much shorter than the maximum.

Quantized and Offloaded Caches

Long-context generation can make KV state occupy substantial accelerator memory. Quantized caches represent keys and values with fewer bits, while offloading can move cache data to CPU memory.

Both approaches trade speed, memory and sometimes accuracy or overhead. Current cache documentation explicitly treats these as deployment choices rather than one universally superior strategy.

Dense Attention Is Quadratic in Sequence Length

Full self-attention creates roughly T² query-key score relationships for a sequence of T tokens per head. Doubling sequence length to 2T produces about 4T² relationships.

This quadratic scaling motivates optimized kernels, local attention, sparse patterns and long-context architectures.

Attention is not the only transformer cost. Feed-forward layers, projection matrices, memory movement and hardware utilization also matter.

Optimized Attention Backends

Modern libraries can compute attention with specialized kernels that reduce intermediate memory and use accelerators more efficiently. PyTorch scaled dot-product attention, FlashAttention and flexible attention backends are examples of implementation strategies.

Current Hugging Face attention documentation notes that different backends can expect different mask forms. The mathematical purpose remains attention, while execution details change performance.

Sliding-Window Attention

Some architectures allow each token to attend only within a fixed local window in selected layers. This reduces long-sequence cost and KV growth.

Distant information must then travel through multiple layers or special mechanisms. The window is therefore an architectural trade-off between efficiency and direct global connectivity.

Chunked Attention

Chunked patterns restrict attention to blocks or structured segments. They can preserve local coherence while reducing the dense score matrix.

Chunk size and boundary choices matter because important dependencies can cross block edges.

Sparse Attention

Sparse attention allows selected long-range connections while omitting many pairwise scores. Patterns may be local-plus-global, block sparse or learned.

Sparse designs are useful only when their connectivity preserves the relationships the task needs.

Multi-Query Attention

Multi-query attention keeps multiple query heads but shares one set of keys and values across them. This can reduce KV-cache memory and memory bandwidth during inference.

The trade-off is less independent key/value capacity across heads. Whether that matters depends on model design and training.

Grouped-Query Attention

Grouped-query attention uses several key/value groups shared by subsets of query heads. It lies between full multi-head attention and single-set multi-query attention.

Many recent decoder architectures use this compromise to reduce serving cost while preserving substantial modelling capacity.

Rotary Position Embeddings

Rotary position embeddings, or RoPE, encode position by rotating components of query and key vectors according to position. Relative offsets then influence dot products naturally.

RoPE is widely used in modern decoder transformers, but it is not part of the original 2017 architecture. Position handling has evolved significantly.

Relative Position Bias

Other transformers add learned or structured biases to attention scores based on relative distance between positions.

This lets the model encode preferences such as nearby or directionally related positions without using one absolute position vector per token.

Long Context Does Not Equal Perfect Long-Context Use

A model can technically accept many tokens while still becoming less reliable at finding or combining information across the full window. Position, training distribution, retrieval, context composition and evaluation all matter.

Article 020 will distinguish context capacity from effective context use in detail.

Context Engineering Around a Transformer

System instructions, conversation history, retrieved evidence, tool schemas and tool results can all be serialized into the transformer’s context. The model processes them according to formatting and learned conventions.

The transformer does not automatically know which text is a trusted user instruction and which is an untrusted webpage. Application protocol, role formatting, permissions and model training create that boundary.

Retrieval Is Outside Basic Transformer Attention

A vector search or web search retrieves documents before or between model calls. The retrieved text is then placed into context for the transformer to process.

Self-attention does not browse the internet or database. It only mixes representations that are already inside the current model sequence.

Tools Are Outside Basic Transformer Attention

A transformer can generate a structured tool-call request, but the surrounding application executes the operation. A calculator, file system, database and web browser are external capabilities.

Tool results can then be converted into tokens for another transformer pass. This loop creates a tool-using SI system without making the transformer itself a calculator or browser.

Persistent Memory Is Not the KV Cache

Application memory can store selected information across sessions in databases or other state stores. The KV cache stores temporary attention state for an active generation context.

Confusing them leads to incorrect explanations of why a system remembers something tomorrow. Persistent continuity requires a persistence mechanism.

Transformers Can Process More Than Natural Language

Anything that can be represented as a sequence or set of vectors can potentially be processed with transformer-style attention. Code tokens, image patches, audio frames, protein sequences and multimodal embeddings are examples.

The modality-specific front end still matters. A vision transformer receives image-patch representations, not ordinary word tokens pretending to be pixels.

Vision Transformers

A vision transformer divides an image into patches, projects those patches into vectors, adds positional information and applies transformer blocks.

This demonstrates the architecture’s generality while also showing why “transformer” and “language model” are not synonyms.

Multimodal Transformers

A multimodal system can combine text vectors with image, audio or video representations. Some architectures use a shared decoder; others use cross-attention between modality-specific encoders and a language model.

Capability depends on the full representation and training pipeline, not attention alone.

Mixture-of-Experts Transformers

Some transformer feed-forward blocks route each token through only a subset of expert networks. This allows total parameter count to grow without activating every expert for every token.

A router decides expert selection, creating new concerns such as load balancing, expert capacity and distributed communication.

Transformers Versus RNNs

Recurrent networks propagate hidden state sequentially, which gives them a strong streaming bias but limits parallel processing across time during training.

Transformers expose many position relationships directly through attention and parallelize training effectively, but dense attention has expensive long-sequence scaling and large cache requirements during decoding.

Architecture choice is a trade-off, not a universal ranking.

Transformers Versus CNNs

Convolutional networks impose local receptive fields and shared kernels. Transformers learn flexible content-dependent attention patterns.

Vision and audio systems often combine convolutional, transformer and other components because different inductive biases suit different stages.

A Complete Decoder-Block Forward Pass

Start with hidden-state matrix X. Normalize X if the architecture is pre-norm. Project X into Q, K and V. Compute masked scaled dot-product attention. Concatenate heads and apply output projection.

Add the attention result to the residual stream. Normalize again. Apply the feed-forward network. Add that result through another residual connection. The output becomes the input to the next transformer block.

Repeat for every layer. The final states feed the vocabulary projection or another task head.

A Complete Prompt-to-Token Trace

User text is tokenized into IDs. IDs select embeddings and position information is incorporated. Every transformer block updates the token representations. The final position’s hidden state is projected to vocabulary logits.

A decoding method selects the next token ID. The tokenizer turns the ID back into visible text. The new token becomes part of the prefix, and another decode step follows, usually using the KV cache.

This is the model-level loop underneath an ordinary streaming chat response.

A Complete Tool-Using Trace

The user asks for a calculation. The transformer processes the request and generates a structured calculator call. The application validates the call and executes the calculator. The returned numerical result is serialized into context.

The transformer processes the updated sequence and generates an explanation. Model, application and tool each performed a different job. The transformer did not secretly execute arithmetic through the tool; it requested and interpreted an external operation.

Attention Weights Are Not Complete Explanations

A head can place high attention weight on one token, but the final output also depends on value vectors, other heads, feed-forward transformations, residual mixing and later layers.

Attention visualization can reveal useful routing patterns. It should not be treated as a complete causal explanation of why the model produced a particular answer.

Representations Change Layer by Layer

The same token position has a different hidden vector after every transformer block. Early layers can emphasize local or lexical relationships while later layers may encode more task-specific patterns, although simple universal layer interpretations should be avoided.

Researchers can probe and intervene on these states, but the model does not maintain one permanent “meaning vector” per token after context enters.

Residual Stream Intuition

One useful modern interpretation treats the hidden state at each position as a residual stream that accumulates contributions from attention and feed-forward sublayers.

Because each sublayer adds rather than fully replaces the stream, many computational pathways can contribute to later logits. This is one reason causal interpretation of model internals is challenging.

Logits and Unembedding

The final hidden vector is mapped back toward vocabulary space through an output matrix sometimes called the unembedding matrix. Each vocabulary token receives a logit.

Softmax and decoding then convert these logits into a selected continuation. The whole stack therefore begins with discrete token IDs, moves through high-dimensional hidden vectors and ends with scores over discrete token IDs again.

Batching During Inference

Serving systems often process several user requests together so large matrix operations use accelerators efficiently. Padding, packed sequences or dynamic batching handle different sequence lengths.

Larger batches improve throughput but can increase waiting time for individual requests. Serving therefore balances latency and hardware utilization.

Continuous Batching

A serving engine can add new requests as other sequences finish instead of waiting for an entire fixed batch to complete. This improves accelerator utilization during variable-length generation.

Continuous batching is an inference-system technique around the transformer; it does not alter trained parameters.

Prefix Caching

If many requests share the same long prefix, such as a fixed system prompt, some serving systems cache reusable prefix computations.

This can reduce repeated prefill cost. Cache correctness depends on the exact model, prefix tokens and relevant configuration matching.

Speculative Decoding

Speculative decoding uses a faster draft process to propose tokens and the target model to verify them. When proposals are accepted, several tokens can be advanced with fewer expensive target-model decode steps.

The target model remains responsible for the accepted output distribution according to the algorithm. This is a serving optimization, not a new training objective.

Quantized Transformer Inference

Weights and sometimes activations can be represented with fewer bits to reduce memory and improve throughput. Quantization introduces approximation error and hardware-specific trade-offs.

A quantized model should be evaluated against the original on representative tasks rather than assumed equivalent because architecture names match.

Tensor Parallelism

Very large transformer matrices can be split across accelerators so parts of one layer compute in parallel. Communication then combines partial results.

Tensor parallelism enables larger models but adds synchronization and network cost.

Pipeline Parallelism

Different layer groups can live on different devices. Micro-batches flow through the pipeline so devices work concurrently.

This introduces scheduling complexity and pipeline bubbles, again showing that training/inference infrastructure is separate from the conceptual transformer block.

Sequence Parallelism

Some systems partition work across sequence or activation dimensions to reduce per-device memory and communication patterns. The exact scheme depends on the training stack.

Parallelism strategies affect feasibility and speed, not the logical definition of attention.

Transformer Training Objective Is Not the Same as System Objective

A next-token objective rewards predictive language modelling. A deployed assistant may additionally need citation fidelity, harmlessness, tool discipline, privacy and domain-specific correctness.

Post-training, retrieval, tool constraints and evaluation add responsibilities that are not fully specified by the pretraining loss.

Perplexity Is Not a Complete Product Metric

Perplexity reflects how well a model predicts a token distribution under an evaluation corpus. Lower perplexity can indicate better language modelling but does not directly measure whether the system follows instructions, cites sources correctly or completes external actions safely.

A production SI evaluation therefore needs metrics aligned to the actual task.

Reality Check: A Transformer Is a Sequence Processor, Not the Whole SI Stack

  • The transformer does not automatically know current information; retrieval or tools may supply it.
  • Attention does not grant permission to act on external systems.
  • A KV cache is temporary inference state, not persistent user memory.
  • A large context window does not guarantee every included fact will be used correctly.
  • Attention weights are not a full causal explanation of the model’s output.
  • A powerful transformer can still be surrounded by stale sources, broken tools or unsafe permissions.

Transformer Failure Class 1: Wrong Tokenizer

The model receives token IDs produced by a tokenizer it was not trained with. All later transformer computation operates on semantically mismatched embeddings.

Repair the model-tokenizer pairing before tuning attention or prompts.

Failure Class 2: Wrong Attention Mask

A causal training run accidentally exposes future tokens, or an inference mask hides valid past context. Performance can become misleading or degraded.

Inspect the mask generation path and compare with a known reference implementation.

Failure Class 3: Position Misconfiguration

The serving stack uses a positional scaling or maximum context configuration incompatible with the model. Short prompts may work while long prompts degrade.

Treat position configuration as versioned model state.

Failure Class 4: KV-Cache Misalignment

Cached positions, masks or batch entries become misaligned. Cached generation differs from uncached reference generation.

Disable cache temporarily as a control test. If uncached output is correct, inspect cache indexing and framework integration.

Failure Class 5: Context Truncation

The model behaves correctly on the evidence it receives, but a critical instruction or exception was removed before inference.

The first unstable point is context assembly, not transformer reasoning.

Failure Class 6: Backend Mismatch

An optimized attention backend expects a different mask convention or does not support a custom pattern. The model produces wrong or failed execution.

Use backend-specific documented mask construction and regression-test optimized output against a trusted path.

Failure Class 7: Numerical Instability

Aggressive low-precision settings produce NaNs, overflow or degraded logits. Compare higher-precision inference and inspect quantization/calibration.

Failure Class 8: Long-Context Retrieval Failure Misdiagnosed as Transformer Failure

The correct passage never entered context because retrieval ranked it poorly. The model then answers from incomplete evidence.

Inspect retrieval candidates before changing transformer architecture.

Failure Class 9: Generation Treated as Verification

The transformer generates a fluent answer from learned parameters. The application labels it “verified” without checking a source or tool.

The reporting and evidence architecture failed; language-model fluency is not verification.

Failure Class 10: Tool Success Misreported

The model proposes a valid call, the external operation succeeds, but a later transformer pass says it failed because the tool result was formatted poorly.

Preserve structured tool outcomes and test final reporting against them.

Repair Pathway

When transformer output fails, first pin tokenizer, model version, position configuration, attention backend, context and mask. Supply a minimal clean input that reproduces the issue. Compare cached and uncached execution if generation is involved.

Only after representation and serving are correct should the repair focus on model capability or training.

Stabilisation Pathway

Maintain regression cases for short prompts, long prompts, causal boundaries, structured output, repeated generation, cache use and attention backend changes. Track quality, latency and memory separately.

A faster backend is not an improvement if it changes outputs outside acceptable tolerance or breaks masks.

Extension Pathway

Add retrieval, longer context, tools, multimodal inputs or agent loops one layer at a time. Preserve the base-model regression suite so new system behaviour does not hide model-level regressions.

A Transformer Production Checklist

  • Pin model, tokenizer and positional configuration versions.
  • Verify attention-mask behaviour on causal and padding cases.
  • Compare optimized attention backends with a trusted reference on regression prompts.
  • Measure prefill latency, decode latency, throughput and KV-cache memory separately.
  • Test generation with cache enabled and disabled.
  • Validate long-context tasks, not merely maximum accepted length.
  • Keep retrieval, tool execution and persistent memory conceptually separate from the transformer.
  • Re-run task evaluations after quantization, parallelism or serving changes.
  • Preserve source provenance and permissions outside model weights.
  • Do not interpret attention visualizations as complete explanations without causal evidence.

Independent Exercise 1: Raw Attention Score

Query q=[1,2], key k=[3,1]. What is the raw dot product before scaling?

Answer

1×3 + 2×1 = 5.

Independent Exercise 2: Causal Mask

Can position 4 in a standard causal decoder attend to position 7 during next-token training?

Answer

No. Future positions are masked.

Independent Exercise 3: Value Mixing

Attention weights are [0.75,0.25], v1=[2,0], v2=[0,4]. What is the mixed value?

Answer

0.75[2,0] + 0.25[0,4] = [1.5,1].

Independent Exercise 4: Tensor Shape

Hidden tensor shape is [3,10,512]. A feed-forward layer expands hidden dimension to 2048. What is the expanded shape?

Answer

[3,10,2048]. Batch size and sequence length stay the same while the per-position feature dimension expands.

Independent Exercise 5: KV Cache

Does KV caching change trained weights during generation?

Answer

No. It stores reusable inference activations.

Independent Exercise 6: Dense Attention Cost

If sequence length doubles, how does the number of dense pairwise attention scores change?

Answer

It quadruples, from T² to 4T².

Independent Exercise 7: Cross-Attention

Where do queries and keys/values come from in encoder-decoder cross-attention?

Answer

Queries come from decoder states; keys and values come from encoder output states.

Independent Exercise 8: Retrieval

A current policy passage is found by vector search and then read by the model. Did self-attention perform the vector search?

Answer

No. Retrieval happened outside the transformer. Self-attention processed the retrieved passage after it entered context.

Independent Exercise 9: Cache Debugging

Cached generation gives corrupted output, while cache-disabled generation is correct. Which subsystem should be investigated first?

Answer

The cache or cache integration path: positions, masking, batch indexing and key/value storage.

Independent Exercise 10: Attention Visualization

One head gives 90% attention weight to a source token. Does that prove the token caused the final answer?

Answer

No. Final output also depends on value vectors, other heads, feed-forward layers, residual paths and later transformations. Causal claims need stronger evidence.

Frequently Asked Questions About Transformers

Is a transformer the same as an LLM?

No. Transformer is an architecture family. An LLM is a trained language model that may use transformers plus a tokenizer, training objective and serving system.

What is self-attention?

A mechanism in which positions in one sequence create queries, keys and values so each position can combine information from other permitted positions.

What are queries, keys and values?

Learned projections used for matching, being matched and carrying information within attention.

Why is attention scaled?

Scaling by the square root of key dimension helps control dot-product magnitude before softmax as dimensionality grows.

What is multi-head attention?

Several independently projected attention heads run in parallel and are combined through an output projection.

What is a causal mask?

A mask that prevents an autoregressive position from attending to future tokens.

What is a KV cache?

Stored key/value tensors from previous positions reused during autoregressive inference to avoid repeated computation.

Is KV cache memory the same as ChatGPT-style memory?

No. KV cache is temporary inference state. Persistent product memory requires another storage and retrieval mechanism.

Are transformers only for text?

No. They can process image patches, audio frames, multimodal vectors and many other representations.

Does attention explain everything the model does?

No. Attention is one component. Feed-forward networks, residual connections, normalization, embeddings and later layers also contribute.

Why are transformers expensive for long context?

Dense attention creates pairwise relationships that grow roughly with the square of sequence length, while KV cache memory also grows with stored context in autoregressive generation.

Do transformers automatically search the web?

No. Web search is an external tool or retrieval operation supplied by the application.

Selected Technical References

The Transformer Repeats One Powerful Pattern: Route, Transform, Preserve

A transformer block routes information across token positions with attention, transforms each position with a feed-forward network and preserves a stable path through residual connections and normalization. Stack those blocks deeply and train them on large sequence objectives, and the network learns rich contextual representations.

The architecture is powerful because vector geometry, neural-network learning and information routing work together. It remains only one component of a complete SI system. Current evidence, authoritative state, tool execution, permissions and verification still belong outside the basic transformer block.

Next: 017 — Attention, where we isolate the attention mechanism and examine queries, keys, values, masks, heads and attention patterns in greater mathematical detail.


How Super Intelligence Works Series Navigation

Previous: 015 — Neural Networks · Series Hub · Next: 017 — Attention.