The part of the Transformer that interests me most is its interface to context. Attention turns a query into a weighted read over stored representations. That is a useful engineering lens, provided we do not mistake a learned read for a verified lookup.
What the original paper actually establishes
Attention Is All You Need introduces an encoder-decoder Transformer that replaces recurrent and convolutional sequence processing with attention and position-wise transformations. Its experiments concern machine translation and parsing under specified training conditions. I would not read those experiments as a claim that attention alone solves reasoning, factual reliability or every sequence task. The architecture still includes feed-forward layers, residual connections, normalization and positional information. The memorable title is not a license to erase the rest of the computational system.
My interpretation starts with a narrower question: what interface does one token have to information at other positions? A recurrent representation forces much of that information through a sequence of state updates. An attention layer exposes a collection of representations that can be addressed by a learned query. Thinking in terms of reads makes several design consequences easier to see. The choice of address function, the content of the values, the visibility mask and the physical movement of those values all influence what this interface can do.
References: [1] Attention Is All You Need
Work through one small read
Consider an illustrative attention head with a two-dimensional query, three keys and three scalar values. Let the query be [1, 0], the keys be [1, 0], [0, 1] and [-1, 0], and the values be 10, 20 and 40. Scaled dot products are approximately 0.707, 0 and -0.707. Applying softmax gives weights of roughly 0.576, 0.284 and 0.140. The returned scalar is therefore about 17.04. These numbers are a hand-sized example, not a trained model’s activations.
The result is neither the value at the closest key nor an identifier for a source. It is a mixture. If the values represented incompatible alternatives, averaging could produce something that corresponds to none of them. In a model, subsequent transformations can use that mixture productively, but the interface does not promise a database-style exact match. This is why I find the memory analogy useful and incomplete: it explains addressing and retrieval, while also making the absence of discrete referential guarantees conspicuous.
from math import exp, sqrt
query = [1.0, 0.0]
keys = [[1.0, 0.0], [0.0, 1.0], [-1.0, 0.0]]
values = [10.0, 20.0, 40.0]
scores = [sum(q*k for q, k in zip(query, key))/sqrt(2) for key in keys]
weights = [exp(score - max(scores)) for score in scores]
total = sum(weights)
result = sum(w*v for w, v in zip(weights, values))/total
print(round(result, 2))Separate the address from the payload
Keys and values need not represent the same information. In a conventional data structure, a search key helps locate a record while the record carries the useful payload. A learned system has a similar opportunity: construct features that make a location easy to find, then return different features from that location. I would resist explaining attention as merely deciding which words are important. Importance is too vague. The mechanism computes a particular relationship for a particular query in a particular representation space.
An engineering consequence is that inspecting weights alone gives an incomplete account of the computation. A large weight on a small or cancelling value may contribute less to the output than its visual prominence suggests. Later residual paths and transformations can further change the effect. To investigate a failure, I would inspect the resulting vectors and test interventions, not just display an attractive heatmap. Removing a candidate location, changing its payload or perturbing a relation can reveal dependencies that a picture of normalized scores cannot establish.
The visibility rule is part of the algorithm
The original decoder uses a causal mask so a prediction cannot read future output positions during training. Multi-head attention performs several learned projections and combines their results. Those are architectural facts from the paper; my systems conclusion is that visibility should be treated as a contract rather than a formatting choice. A mask error can allow a model to exploit information that will never exist at inference time, producing an evaluation that looks excellent until the training shortcut disappears in deployment.
The same reasoning applies when packing several examples into one batch. Padding masks, sequence boundaries and position identifiers must agree. If an implementation inadvertently lets one example read another, the result is not merely a slightly different kernel. It is a different information policy. I would test this with deliberately distinctive neighboring sequences and verify that changing one cannot alter the other’s outputs where isolation is promised. Numerical comparisons on ordinary random inputs are useful, but boundary-shaped tests expose the contract more directly.
References: [1] Attention Is All You Need
Position supplies relations that content cannot
Two identical tokens can occupy different grammatical or causal roles because they occur at different positions. The relative-position attention paper by Shaw and colleagues modifies attention to incorporate distances between sequence elements. Its reported translation experiments are evidence for that particular design in those settings. I take a more general lesson from it: an addressing mechanism needs a representation of the relations that the application expects it to distinguish. Content similarity alone cannot provide a relation that was never represented in the inputs.
For a hypothetical event model, absolute timestamps, elapsed time and event order answer different questions. A position counter can say which event came first without saying whether the gap was a millisecond or a month. Conversely, equal timestamps do not necessarily establish causal order. I would choose relation features by writing the questions the model must answer and identifying the invariances it should preserve. Shifting every timestamp should leave some tasks unchanged; reversing two dependent events should not. Those expectations are testable before debating a particular positional encoding.
References: [2] Self-Attention with Relative Position Representations
Count the representation you are materializing
For an illustrative sequence length of 4,096, a dense matrix containing one score for every ordered pair of positions has 16,777,216 entries. At two bytes per entry, one such matrix occupies 32 MiB before accounting for multiple heads, layers, gradients or other intermediates. Doubling the sequence length quadruples that matrix. This arithmetic is independent of a particular accelerator. It explains why a mathematically compact expression can create a large physical object when implemented by straightforward tensor operations.
However, an equation does not force every intermediate to be written to external memory. FlashAttention demonstrates an exact attention algorithm organized around tiling and memory movement. The important distinction is between the mathematical operation and the materialization strategy used to execute it. My own implementation review would therefore ask which tensors exist at each stage, where they reside and how long they remain live. Counting arithmetic alone misses the cost of repeatedly moving a representation that is much larger than the fast memory available to the kernel.
References: [3] FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
A larger memory does not guarantee better recall
Giving a model more context increases the material it may consult, but it also increases the opportunity to retrieve irrelevant or conflicting information. Suppose an application inserts ten nearly identical policy fragments with different effective dates. The attention mechanism can represent relationships among them, yet the application still needs a rule for which version governs the answer. Expanding the context window does not automatically create that rule. I would first fix document identity, versioning and relevance before treating more capacity as the default remedy.
There is a counterargument: the memory-interface description risks making a learned distributed representation sound more discrete and inspectable than it really is. That objection is fair. A head is not a table with stable human-readable records, and a token’s representation can encode many interacting features. I use the analogy to ask engineering questions, not to claim a complete explanation of model behavior. It is most valuable when it reveals costs and missing guarantees, and least valuable when it tempts us to label every head with a simple semantic job.
What I would test before trusting an implementation
I would begin with tiny examples whose expected outputs can be calculated directly, including the three-key case above. Then I would test causal boundaries, padding, isolated packed sequences and extreme score magnitudes. Subtracting the largest score before exponentiation is a numerical stabilization, not a change in the mathematical softmax distribution. Implementations should also define behavior for invalid rows rather than silently allowing an all-masked row to generate non-finite values that contaminate later layers and obscure the original error.
After correctness, I would measure the intended workload: prompt lengths, output lengths, batch shapes, precision and memory limits. A faster kernel on a convenient fixed shape is not automatically a better serving system. Finally, I would keep architectural claims separate from application claims. Attention provides a powerful learned interface to context. Whether that context is relevant, whether its boundaries are correct, and whether a generated statement follows from it remain additional questions. The memory lens is useful precisely because it leaves those questions visible instead of hiding them behind a single operation.
Sources and further reading
- Attention Is All You Need
Original Transformer architecture, scaled attention, decoder masking and multi-head construction.
- Self-Attention with Relative Position Representations
Primary research on representing relative distances within self-attention.
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Primary research separating exact attention mathematics from a memory-efficient execution strategy.