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:
| Mask | Allows | Typical use |
|---|---|---|
| padding mask | ignore nonexistent tokens | variable-length batches |
| causal mask | attend only to previous tokens | autoregressive decoding |
| bidirectional mask | attend to all valid tokens | representation learning |
| block/local mask | attend to restricted windows | long-context efficiency |
| cross mask | connect query tokens to external memory | retrieval 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 Family | Inference Memory | Main Risk |
|---|---|---|
| decoder-only Transformer | grows with context through KV cache | long-context memory cost |
| recurrent language model | fixed recurrent state | compressed-state bottleneck |
| retention model | decayed key-value state | retention horizon and normalization details |
| SSM/selective SSM | scan state or structured cache | state design and retrieval behavior |
| hybrid Transformer-SSM | fewer attention layers need KV cache | allocation 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.