Transformer

Transformers use attention to model relationships across tokens. They are common in language models, protein sequence models, molecular sequence models, and agent systems.

A pre-norm Transformer block with multi-head self-attention is:

Here is the token matrix, is a padding or causal mask, is the number of heads, and is the feed-forward nonlinearity. Decoder-only Transformers use a causal ; encoder-style models usually use bidirectional attention with only padding masks. The block combines tokenization, embeddings, linear layers, softmax, normalization, residual connections, and a token-wise feed-forward network.

Shape Contract

For a batch of token embeddings:

each head uses projections:

and produces:

The attention score tensor has shape:

For self-attention, . For cross-attention, query tokens and key/value tokens may come from different sources.

Mask Semantics

The mask defines what information is available:

MaskAllowsTypical use
padding maskignore nonexistent tokensvariable-length batches
causal maskattend only to previous tokensautoregressive decoding
bidirectional maskattend to all valid tokensrepresentation learning
block/local maskattend to restricted windowslong-context efficiency
cross maskconnect query tokens to external memoryretrieval or encoder-decoder models

Mask choice is part of the model claim. A representation model, a generator, and a retrieved-context agent can all use Transformers while having different information boundaries.

Complexity

Full self-attention scales quadratically in sequence length:

and attention logits require:

memory before optimization. This is why long-context systems use caching, local attention, sparse attention, state-space models, retrieval, or chunking.

Transformer vs Recurrent Inference

Decoder-only Transformers usually avoid recomputing the full prefix by keeping a KV cache:

Recurrent or retention-based alternatives such as RWKV and RetNet aim for a compact per-layer state:

RetNet frames this as retention state:

This does not make recurrence automatically better. It changes the bottleneck: attention keeps explicit past keys and values, while recurrence or retention compresses the prefix into state.

Model FamilyInference MemoryMain Risk
decoder-only Transformergrows with context through KV cachelong-context memory cost
recurrent language modelfixed recurrent statecompressed-state bottleneck
retention modeldecayed key-value stateretention horizon and normalization details
SSM/selective SSMscan state or structured cachestate design and retrieval behavior
hybrid Transformer-SSMfewer attention layers need KV cacheallocation and implementation details matter

Hybrid models such as Jamba keep some attention layers for explicit token-token retrieval while using Mamba-style layers to reduce long-context cache pressure:

This is not a free replacement for attention. It is an architecture allocation choice.

Key Ideas

  • Self-attention lets each token mix information from other tokens according to learned relevance.
  • Feed-forward networks, normalization, residual connections, and positional encodings are part of the core pattern.
  • Encoder-only Transformer, Decoder-only Transformer, and encoder-decoder variants serve different training and inference workflows.
  • Causal masking supports autoregressive generation; bidirectional attention supports representation learning.
  • Transformers can process text, protein sequences, molecular strings, retrieved context, and multimodal tokens when inputs are tokenized.

Practical Checks

  • Identify the variant: encoder-only, decoder-only, or encoder-decoder.
  • Check context length, attention pattern, positional encoding, and masking.
  • Track what each token represents: wordpiece, residue, atom token, graph node, image patch, tool call, or retrieved chunk.
  • For scientific tasks, separate model architecture from data split, leakage, calibration, and downstream evaluation.
  • Check whether reported context length is training length, inference length, or extrapolated length.
  • Check whether memory optimization changes exact attention, approximation, or only implementation.