Attention as Content-Addressed Routing
Attention lets each representation ask a learned question of candidate representations, turn compatibility into routing weights, and mix the associated content. Its power is conditional information flow, not awareness or automatic explanation.
TLDR
- Attention is a routing mechanism. A query scores candidate keys, normalised scores gate their values, and the weighted values become the information delivered to the requesting position.
- Its decisive advantage is content-conditioned interaction: the route can change with the current input instead of following one fixed neighbourhood, recurrence step or pooling rule.
- Scaled dot products make routing efficient, while softmax makes it differentiable. The same softness also creates mixtures, collisions and outputs that may correspond to no single source.
- Raw attention weights describe one internal routing distribution. They do not by themselves establish overall feature importance, causal necessity, faithful reasoning or subjective awareness.
- Use attention when a task needs adaptive exchange among separable candidates, then test it against uniform, local and fixed-routing baselines plus key, value and mask interventions.
The evidence wall that was rearranged overnight
A review team works from a wall of one hundred evidence cards. Each card has a small descriptor at the top and a detailed finding below it. The analyst asks, “Which card contains the current leverage covenant?” A mechanical arm reads every descriptor, gives each card a relevance score, and blends the findings from the highest-scoring cards into a working note.
Overnight, a cleaner rearranges the cards. A location-based arm, programmed to inspect card 37, now retrieves an insurance clause. The descriptor-based arm still finds the covenant because its route depends on what the cards say, not where they sit. This is the useful intuition behind content addressing. A request is represented as a query; each candidate exposes a key; associated content travels as a value.
Now change only one fact. Two cards receive the same descriptor, “current leverage covenant”, but one contains 3.5× and the other 5.0×. The arm cannot distinguish them. If it assigns equal weight, it delivers 4.25×, a number present on neither card. The routing mechanism has behaved exactly as specified and still produced a misleading mixture.
The central answer is therefore precise: attention is differentiable, content-addressed routing followed by mixing. It is powerful when similarity between learned requests and learned addresses identifies useful information. It fails when keys do not separate candidates, when values should not be averaged, when order has not been represented, or when a visible routing weight is mistaken for an explanation of the whole system.
Part I: One operation, three roles
The word attention arrived before the Transformer. In neural machine translation, Bahdanau, Cho and Bengio replaced one fixed-length source summary with a context vector recomputed for each target step. Their model scored encoded source annotations against the current decoder state, normalised those scores, and formed a weighted sum. In their English-to-French experiments, the attentional model was more robust to long sentences than the fixed-vector baseline. That result supports the mechanism in that setting; it does not prove that every sequence task needs attention.
At nearly the same time, Graves, Wayne and Danihelka described content-based addressing in Neural Turing Machines. A controller emitted a key, compared it with memory rows, and produced a normalised weighting. Their paper also retained location-based addressing because some tasks require a stable variable address even when its content is arbitrary. The distinction remains essential: “find what resembles this request” and “read the item at this position” solve different problems.
The Transformer gave the operation its now familiar query, key and value form. Vaswani and colleagues defined an attention function as mapping a query and a set of key-value pairs to an output, with each value weighted by a compatibility function of its key and the query. Keys participate in routing; values carry the information routed. Keeping those roles separate prevents several common confusions.
Content-addressed is a mechanism, not a guarantee
The phrase content-addressed borrows an intuition from associative memory: retrieve by resemblance to a request rather than by a fixed slot. It should not be mistaken for database semantics. A database key can be explicit, unique and governed by an integrity constraint. An attention key is usually a learned vector whose useful meaning is established only through the task loss. Two candidates can collide, one candidate can expose different keys in different layers, and a small change elsewhere in the representation can alter the route.
Nor is the key necessarily a description of the value. Training may make one useful for locating the other, but the architecture does not enforce truthfulness, freshness or even human-interpretable correspondence between them. The label “content” means that the score depends on the current candidate representation rather than solely on its ordinal location. It does not mean that the transmitted value is factually correct, authorised for the requester or sufficient for the downstream decision.
Consider a controlled swap. Keep every query and key fixed, but exchange the values attached to two keys. The attention weights remain identical while the output changes because different content travels through the same route. Now restore the values and exchange only the keys. The content has not moved, yet the route changes because the addresses have. This separation test reveals the causal contract: within the allowed candidate set, queries and keys decide the routing weights; values decide what the routed communication contributes.
The address space is also temporary. In ordinary self-attention, keys and values are computed from the current activations for the current forward pass. They are not a durable enterprise memory, a source catalogue or a record of what the system once knew. A model may use the pattern like an associative read, but persistence, provenance, correction and entitlement must be supplied by systems outside the attention equation. The memory analogy helps with routing geometry and fails when it is stretched into operational governance.
A query is a request, not a word
In a trained network, a query is usually a vector produced by a learned projection. It need not be human-readable and it need not represent a complete semantic question. It is better understood operationally: changing the query changes which keys receive weight. In self-attention, every position creates its own query, so every position can request a different mixture from the same candidate field.
A key is likewise not the underlying content. It is the candidate’s address under the current routing scheme. Two values may contain very different information while exposing similar keys, creating a collision. Conversely, two similar values can expose distinct keys because the model has learned different reasons to retrieve them.
The value is what crosses the route. A candidate can receive high attention and still have little effect if its value lies in an unimportant or cancellable direction. A low-weight value can matter greatly if later computation amplifies the direction it contributes. This is one reason that a heat map of weights cannot be treated as a complete causal explanation.
Self-attention and cross-attention
The routing equation does not require queries, keys and values to come from the same place. In cross-attention, queries come from one collection and keys plus values from another. A decoder state can query an encoded source, or a latent array can query visual features. In self-attention, all three are projected from one input matrix X:
Self-attention therefore creates an input-dependent communication graph. Each row can receive information from every unmasked candidate in one layer. That short path length was one motivation for the Transformer, alongside parallel computation across positions. Yet the output of a single head remains a weighted average. The richer model arises because projections, several heads, residual pathways, non-linear feed-forward layers and depth compose many such exchanges. Paper 58 treats the multi-head trade in detail; here one head is enough to expose the mechanism.
The equation as a routing programme
For m queries, n candidate key-value pairs, key width dₖ and value width dᵥ, scaled dot-product attention is:
The compact matrix expression hides four decisions. First, every query is compared with every candidate key. Second, the scores are scaled and optionally masked. Third, softmax turns each row into non-negative weights that sum to one. Fourth, those weights mix the value rows. The matrix of weights is a routing plan; multiplying by V is the routed transfer.
The routing matrix is a temporary programme
Write the normalised weights as A = softmax((QKᵀ/√dₖ) + M). Each row of A is a probability simplex over the currently allowed candidates. The row can be read as a small programme generated for one requester: gather 72.4 per cent of one value, 4.3 per cent of another and 23.3 per cent of a third. A different query compiles a different row from the same candidate field. A different input can change both the requests and the addresses, so the communication pattern is recomputed rather than stored as one fixed graph.
Calling the row a probability distribution is mathematically accurate and semantically dangerous. Its entries are normalised routing coefficients. They are not automatically probabilities that candidates are true, relevant in a calibrated sense or responsible for the final answer. Softmax forces every allowed row to sum to one even when all keys are poor matches. Unless the model has a separate null route or abstention mechanism, the least unsuitable candidate still receives part of a complete unit of mass.
The matrix alone is also under-specified as a causal account. The delivered representation is Y = AV. Double every value and the same route delivers twice the vector. Rotate value space and the same heat map transmits different directions. Conversely, different routing matrices can produce the same Y when value rows are redundant or one value lies inside the convex hull of others. Later layers can then preserve, cancel or amplify the delivered direction. A defensible routing analysis therefore inspects weights, values and downstream consequence together.
This temporary-programme view is useful because it avoids two exaggerations. Attention is more than static feature weighting: its route is conditioned on the current requester and candidate field. Yet it is less than a complete algorithmic trace: one row records one mixing operation inside a larger computation. The right question is not whether a coloured cell looks important, but whether a controlled change to the address or payload changes the information received and, eventually, the task outcome.
Minimal worked example
Take one two-dimensional query q = [2, 0]. Three candidates expose keys [2, 0], [0, 2] and [1.2, 0.8]. Their values are [10, 0], [0, 10] and [6, 4]. After dividing dot products by √2, the scores are 2.828, 0 and 1.697. Softmax converts them to approximately 0.724, 0.043 and 0.233.
| Candidate | Key | Value | Scaled score | Weight | Weighted contribution |
|---|---|---|---|---|---|
| 1 | [2, 0] | [10, 0] | 2.828 | 0.724 | [7.24, 0] |
| 2 | [0, 2] | [0, 10] | 0.000 | 0.043 | [0, 0.43] |
| 3 | [1.2, 0.8] | [6, 4] | 1.697 | 0.233 | [1.40, 0.93] |
The output is approximately [8.638, 1.362]. Candidate 1 dominates, candidate 3 contributes materially, and candidate 2 is not removed. This is soft routing. A hard nearest-neighbour read would return [10, 0]; uniform pooling would return [5.33, 4.67]. Each operation answers a different design question.
The geometry is learned twice
Calling the dot product “similarity” can conceal the learned projections. The model does not normally compare raw token embeddings. It learns WQ and WK, which decide which directions count as compatible. It separately learns WV, which decides what content a compatible candidate contributes. Thus attention does not merely discover an existing semantic geometry; training constructs a routing geometry and a transmission geometry together.
This perspective connects attention to associative memory. Ramsauer and colleagues showed that a modern Hopfield update can be equivalent to the Transformer attention mechanism, supporting an interpretation of attention as associative retrieval under particular energy formulations. The equivalence is mathematically useful, but the operational account requires less metaphysics: queries induce weighted reads over key-value patterns.
flowchart TD A["The evidence wall that was<br/>rearranged overnight"] --> B["Part I, One operation, three roles"] B --> C["The equation as a routing<br/>programme"] C --> D["Part II, What content routing buys"] D -. "re-test" .-> A classDef first fill:#0a1424,stroke:#D95545,stroke-width:2px,color:#e6f3ff classDef step fill:#0a1424,stroke:#3f7da0,stroke-width:1px,color:#e6f3ff class A first class B,C,D step
Part II: What content routing buys
A fixed pooling rule always combines candidates in the same way. A convolution uses a shared local pattern. A recurrent network transports state through a sequence of update steps. Attention instead lets the current representation choose its exchange pattern from the current candidates. The route is conditional on both the requester and the available field.
That conditionality matters when relevant relationships are not tied to one distance or one fixed slot. A pronoun may need a nearby noun in one sentence and a distant noun in another. A visual patch may need another patch across the image. A set element may need to compare itself with every other member. The Set Transformer makes the order-free case explicit: attention can model interactions among set elements while preserving the permutation properties required by set tasks.
Thought experiment: shuffle the archive
Return to the evidence wall. Number the cards from one to one hundred, but keep their descriptors and findings. Ask the arm to retrieve the current covenant. Then apply a random permutation to the cards while moving each descriptor with its finding. A content-addressed mechanism predicts the same output, subject only to numerical round-off. A location pointer predicts a different output unless its address is updated.
Now perform a different intervention: keep each finding in place but randomly exchange the descriptors. The content router follows the wrong descriptors and fails. The result identifies the causal burden carried by the keys. The values can be perfectly correct and still remain unreachable under a bad routing geometry.
This two-intervention design is stronger than staring at a heat map. Paired permutation tests whether the mechanism depends on location. Key shuffling tests whether the learned addresses control the route. Value shuffling tests whether routed content, rather than routing appearance, determines the output. These are small causal experiments.
Why divide by the square root of key width?
Suppose the components of a query and key are independent, have mean zero and variance one. Their dot product sums dₖ products, so its variance grows with dₖ. Larger raw logits push softmax towards saturation, where small probabilities and their gradients can become tiny. Dividing by √dₖ keeps the logit scale more stable under that simplified assumption. Vaswani and colleagues presented this motivation for scaled dot-product attention.
The scaling is a variance-control heuristic, not a law that makes every learned distribution calibrated. Real queries and keys are neither independent nor fixed-variance throughout training. Layer normalisation, parameterisation, temperature, clipping and alternative similarity functions all affect the score distribution. The practical question is whether routing remains trainable and suitably concentrated, not whether one historical scale factor has metaphysical status.
Softmax creates both a gate and a barycentre
Softmax has useful properties. Every route receives a non-negative weight, weights sum to one, and the operation is smooth almost everywhere. A modest score change can alter the mixture without a discrete branch, allowing gradient-based learning. It also makes the output a point in the convex hull of the value vectors for one head. That geometric fact is a boundary.
If a task needs to copy one value exactly, a diffuse distribution introduces averaging error. If several candidates jointly carry evidence, a mixture can be appropriate. If values encode directions that later layers combine productively, the mixture can be more expressive than a literal average suggests. The correct interpretation depends on the value space and downstream computation.
Temperature makes the trade visible. Divide scores by a small positive temperature and the largest score dominates. Increase temperature and the route approaches uniformity. Standard scaled dot-product attention fixes this extra temperature at one after the √dₖ scale, although learned norms can still change effective sharpness. A sharp route is not automatically correct, and a diffuse route is not automatically uncertain in a calibrated sense.
Part III: Where routing breaks
Attention is often introduced through a successful lookup. Its failure modes become clearer when the keys and values are manipulated independently. Three failures matter most at first principles: indistinguishable addresses, destructive mixing and missing structure.
Key collision: two addresses look the same
Give two candidates identical keys and opposite scalar values, 0 and 100. A matching query produces equal scores, hence equal weights, and the output is 50. The mechanism has no basis for choosing. More training can separate the keys if the data and model expose a distinguishing feature. No softmax temperature can separate truly equal logits.
Key separability is a precondition for reliable content routing. In practice, collisions need not be exact. Near-duplicate entities, repeated boilerplate, duplicated documents, delimiter tokens or a dominant generic feature can attract similar routes. The resulting mixture may be harmless in a distributed representation, or it may create a false intermediate value. The only safe conclusion comes from task-level interventions.
Different routes can yield the same output
Let the values be v₁ = [1,0], v₂ = [0,1] and v₃ = [0.5,0.5]. Route A assigns weights [0.5,0.5,0]. Route B assigns [0,0,1]. Both produce [0.5,0.5]. Observing the output does not identify the route; observing the route does not tell us which downstream changes would alter the final prediction.
Brunner and colleagues formalised a related identifiability problem for Transformer attention, showing that for sequences longer than the attention head dimension, attention weights are not identifiable under their analysis. The practical lesson is broader and simpler: when values are redundant or linearly dependent, many weight patterns can carry the same result.
The serious negative control: uniform routing
Suppose the task is to compute the mean sensor reading. Values are 2, 8 and 14. Uniform weights return 8 exactly. Content-conditioned scores add parameters without adding necessary capability. If the preferred attention mechanism only matches this baseline, its routing story is unnecessary for the task.
This is not merely a toy objection. Tay and colleagues’ Synthesizer experiments replaced token-token query-key interactions with generated or random alignment matrices and reported competitive results across several tested tasks and models. Their result does not show that content addressing never matters. It shows that token mixing, positional regularity and the rest of the architecture can carry substantial performance even when pairwise dot-product routing is weakened or removed.
A 2025 preprint by Xue, Moosavi and Aletras deconstructed attention into token mixing, sequence-dependent activations, mathematical form and coupling to hidden states. In their controlled language-model experiments, removing token mixing caused collapse, while the exact attention form and sequence dependence could be relaxed, especially in hybrid models. This is recent preprint evidence, not a universal theorem, but it sharpens the causal question: which part of “attention” is earning the result?
Order is absent unless represented
A bare attention read over key-value pairs is invariant to their paired permutation. Self-attention without positional information is permutation equivariant: reorder input rows and output rows reorder correspondingly. This is desirable for sets and insufficient for sequences where “dog bites man” differs from “man bites dog”. Positional encodings, relative biases, rotary transformations, masks or structured keys inject the missing geometry. Paper 59 develops that dependency; the first-principles point is that content similarity alone does not recover order.
Masks constrain routes, but they are not authority
A causal mask prevents a query at position t from assigning weight to future positions. A padding mask removes irrelevant slots. A local mask restricts neighbourhoods. Mathematically, prohibited scores receive negative infinity before softmax, yielding zero weight.
A mask is a structural control inside a calculation. It does not authenticate a user, establish data entitlement, prove provenance or prevent a prohibited fact from arriving through another allowed representation. In a governed system, identity and authorisation must be enforced outside the model’s stochastic routing. Information-flow shape is not the same object as permission to access or act.
The computational boundary
Dense attention compares every query with every key, producing quadratic pair count in sequence length. That does not mean the full score matrix must always be stored. FlashAttention computes exact attention with an input-output-aware tiled algorithm that reduces memory traffic. It changes implementation and performance while preserving the mathematical result, which is a useful separation between mechanism and kernel.
Approximate, sparse, local, low-rank and linear variants alter either the candidate set, the score function, the normalisation or the order of operations. Their evaluation must therefore ask two questions: how much resource is saved, and which routing behaviours become impossible or inaccurate? “Efficient attention” is not one causal intervention.
Attention is neither awareness nor an automatic explanation
The borrowed cognitive word invites two category errors. First, a numerical weighting operation is not evidence of subjective awareness. It specifies information flow in a model. Present machines may warrant separate functional, behavioural or consciousness questions, but the name of this operator settles none of them.
Second, attention weights are not automatically a faithful explanation of a prediction. They show which value rows one head mixed at one layer for one input. The final output also depends on the values, output projection, residual stream, other heads, feed-forward computation, later layers and decoding. A high weight can travel along a value direction that later cancels. A low weight can carry a decisive feature.
The empirical debate is instructive. Jain and Wallace found weak correlations between attention weights and gradient-based importance in tested NLP models, and constructed different attention distributions with similar predictions. Serrano and Smith reported that high weights sometimes tracked impact but were not fail-safe indicators. Wiegreffe and Pinter argued that the conclusion depends on the definition of explanation and the experimental baseline, proposing uniform-weight, seed-variance, frozen-weight and adversarial tests.
The balanced engineering conclusion is not “never inspect attention”. It is: treat a weight map as a routing diagnostic whose explanatory status must be earned by interventions. Ask whether changing a key changes the route, whether changing the routed value changes the output, whether ablating the route changes the decision, and whether an alternative route produces the same result. Report the model scope and task.
This routing argument no longer justifies attention when the task does not need candidate-dependent interaction, when relevant candidates cannot be separated in key space, when the required operation is discrete or non-convex and value mixing destroys it, when essential order or relation is absent, or when context length makes the chosen attention pattern unaffordable. A different mixer, memory system, retrieval stage or symbolic operation may be the correct mechanism.
stateDiagram-v2 [*] --> Claim state "Content-addressed is a<br/>mechanism, not a guarantee" as Claim state "A query is a request, not a word" as Test state "Self-attention and cross-attention" as Revision Claim --> Test: expose mechanism Test --> Decision: evidence survives Test --> Revision: rival survives Revision --> Claim: narrow or rebuild Decision --> [*]
Part IV: Build the route, then test the consequence
A production-shaped worked scenario
Consider a synthetic credit-review assistant reading an authorised packet with a facility agreement, two amendments, a monitoring report and a covenant calculation. The user asks for the current maximum leverage ratio and its evidence. External retrieval first selects the authorised documents. Tokenisation and encoding create internal representations. Attention then routes among those representations while the model constructs an answer.
The mechanism can help connect “maximum total net leverage” in the question with a clause several pages away and a later amendment. Yet three layers of correctness remain outside raw attention. The retrieval stage must include the controlling amendment. The context must mark effective dates and supersession. The final claim must cite evidence and respect a deterministic calculation or review rule. A model can attend strongly to an obsolete clause because it is lexically perfect.
Attention routes what is present; it does not certify what is true, current or authorised. In this scenario, a robust system keeps retrieval entitlement, temporal validity, calculation and human decision rights as separate controls. Attention is an internal component of representation exchange, not the evidence ledger.
A routing fitness decision instrument
Before adding attention, ask whether the task needs conditional exchange rather than merely broad capacity. The following instrument turns the mechanism into testable architecture questions.
1. Interaction need
Does the relevant neighbour change with the instance? Compare with mean pooling, a fixed window, convolution and a recurrent or state-space baseline.
2. Key separability
Can candidates that require different treatment expose distinguishable keys? Inject duplicates, near-duplicates and misleading descriptors.
3. Value compatibility
Is weighted mixing meaningful in value space? Test copy, contradiction, arithmetic and categorical cases where an average may fabricate an invalid state.
4. Structural geometry
Are order, distance, relation and causality represented? Run paired permutations and position ablations rather than assuming sequence order is inherent.
5. Route constraint
What does the mask prohibit, and what can still leak through other representations? Keep security and entitlement controls outside the model.
6. Causal use
Do key, value and route interventions predict output changes? Compare raw weights with ablations, counterfactuals and independent measures.
7. Resource shape
Can the sequence length and latency afford the candidate graph? Separate exact kernel improvements from approximate changes to the mechanism.
8. Release evidence
Report task-level gain, uncertainty, collision cases, negative controls and failure boundaries. Do not promote a visual pattern into a capability claim.
| Observed task structure | First baseline | When attention earns a trial | Decisive failure test |
|---|---|---|---|
| All items contribute symmetrically | Sum, mean or max pooling | Interactions among items change the required aggregate | Uniform routing matches quality with lower cost |
| Relevant information is local and stationary | Convolution or fixed window | The useful neighbourhood changes by content | Shuffled distant distractors attract the route |
| Long sequence with compact recurrent state | Recurrent or state-space model | Direct pairwise access materially improves the task | Length or memory traffic erases the gain |
| Exact record or rule lookup | Database, retrieval or symbolic index | Fuzzy alignment is required after exact filtering | Key collision blends mutually exclusive values |
| Authorised action or decision | Deterministic policy and typed action | Attention supports evidence interpretation only | Model routing is mistaken for permission or approval |
The executable attention laboratory
The companion programme uses only the Python standard library and synthetic vectors. It implements one scaled dot-product read, then runs six checks: a positive content read, paired permutation, duplicate-key collision, two different routes with the same output, an exact uniform-mean negative control, and a structural mask.
Save the code as attention_from_first_principles.py and run python attention_from_first_principles.py. The assertions are the important part. They force the mechanism to reproduce the numerical example and preserve the failures rather than printing a persuasive diagram.
Complete runnable Python laboratory
"""Attention from first principles: a synthetic routing laboratory.
Python 3.10+. Standard library only.
All vectors, labels and thresholds are synthetic teaching fixtures.
"""
from __future__ import annotations
from math import exp, isclose, sqrt
from typing import Sequence
Vector = list[float]
Matrix = list[Vector]
def dot(a: Sequence[float], b: Sequence[float]) -> float:
"""Return the dot product of equal-length vectors."""
if len(a) != len(b):
raise ValueError("vectors must have equal length")
return sum(x * y for x, y in zip(a, b))
def softmax(logits: Sequence[float]) -> Vector:
"""Stable softmax over at least one finite logit."""
if not logits:
raise ValueError("softmax needs at least one logit")
finite = [x for x in logits if x != float("-inf")]
if not finite:
raise ValueError("at least one route must remain allowed")
peak = max(finite)
numerators = [0.0 if x == float("-inf") else exp(x - peak) for x in logits]
total = sum(numerators)
return [x / total for x in numerators]
def weighted_sum(weights: Sequence[float], values: Matrix) -> Vector:
"""Mix value rows using a probability vector."""
if not values or len(weights) != len(values):
raise ValueError("one weight is required for every value")
width = len(values[0])
if any(len(value) != width for value in values):
raise ValueError("all values must have equal width")
return [
sum(weight * value[column] for weight, value in zip(weights, values))
for column in range(width)
]
def scaled_dot_attention(
query: Sequence[float],
keys: Matrix,
values: Matrix,
*,
temperature: float = 1.0,
allowed: Sequence[bool] | None = None,
) -> tuple[Vector, Vector, Vector]:
"""Compute one scaled dot-product attention read.
`allowed` is a structural mask. False routes receive a score of negative
infinity before softmax. A mask constrains this calculation; it is not an
identity, entitlement or security system.
"""
if temperature <= 0:
raise ValueError("temperature must be positive")
if not keys or len(keys) != len(values):
raise ValueError("keys and values must be non-empty and aligned")
key_width = len(query)
if key_width == 0 or any(len(key) != key_width for key in keys):
raise ValueError("query and keys must share a non-zero width")
if allowed is None:
allowed = [True] * len(keys)
if len(allowed) != len(keys) or not any(allowed):
raise ValueError("mask must leave at least one route allowed")
scale = sqrt(key_width) * temperature
scores = [
dot(query, key) / scale if route_allowed else float("-inf")
for key, route_allowed in zip(keys, allowed)
]
weights = softmax(scores)
output = weighted_sum(weights, values)
return scores, weights, output
def rounded(items: Sequence[float], digits: int = 3) -> list[float]:
return [round(item, digits) for item in items]
def assert_vector_close(a: Sequence[float], b: Sequence[float]) -> None:
if len(a) != len(b) or any(not isclose(x, y, abs_tol=1e-9) for x, y in zip(a, b)):
raise AssertionError(f"vectors differ: {a!r} != {b!r}")
def positive_content_read() -> None:
query = [2.0, 0.0]
keys = [[2.0, 0.0], [0.0, 2.0], [1.2, 0.8]]
values = [[10.0, 0.0], [0.0, 10.0], [6.0, 4.0]]
scores, weights, output = scaled_dot_attention(query, keys, values)
print("positive read")
print(" scores :", rounded(scores))
print(" weights:", rounded(weights))
print(" output :", rounded(output))
assert weights[0] > weights[2] > weights[1]
assert_vector_close(output, [8.638325843083024, 1.3616741569169766])
def permutation_test() -> None:
query = [2.0, 0.0]
keys = [[2.0, 0.0], [0.0, 2.0], [1.2, 0.8]]
values = [[10.0, 0.0], [0.0, 10.0], [6.0, 4.0]]
original = scaled_dot_attention(query, keys, values)[2]
order = [2, 0, 1]
permuted_keys = [keys[index] for index in order]
permuted_values = [values[index] for index in order]
permuted = scaled_dot_attention(query, permuted_keys, permuted_values)[2]
print("paired permutation")
print(" original output:", rounded(original))
print(" permuted output:", rounded(permuted))
assert_vector_close(original, permuted)
def collision_failure() -> None:
query = [1.0, 0.0]
duplicate_keys = [[1.0, 0.0], [1.0, 0.0]]
contradictory_values = [[0.0], [100.0]]
_, weights, output = scaled_dot_attention(query, duplicate_keys, contradictory_values)
print("key collision")
print(" weights:", rounded(weights))
print(" mixed value:", rounded(output))
assert_vector_close(weights, [0.5, 0.5])
assert_vector_close(output, [50.0])
def non_identifiability_test() -> None:
values = [[1.0, 0.0], [0.0, 1.0], [0.5, 0.5]]
route_a = [0.5, 0.5, 0.0]
route_b = [0.0, 0.0, 1.0]
output_a = weighted_sum(route_a, values)
output_b = weighted_sum(route_b, values)
print("different routes, same result")
print(" route A output:", rounded(output_a))
print(" route B output:", rounded(output_b))
assert route_a != route_b
assert_vector_close(output_a, output_b)
def negative_control_mean_task() -> None:
values = [[2.0], [8.0], [14.0]]
uniform_weights = [1.0 / 3.0] * 3
output = weighted_sum(uniform_weights, values)
print("negative control: exact mean without content addressing")
print(" uniform output:", rounded(output))
assert_vector_close(output, [8.0])
def mask_test() -> None:
query = [2.0, 0.0]
keys = [[2.0, 0.0], [0.0, 2.0], [1.2, 0.8]]
values = [[10.0, 0.0], [0.0, 10.0], [6.0, 4.0]]
_, weights, output = scaled_dot_attention(
query,
keys,
values,
allowed=[False, True, True],
)
print("structural mask")
print(" weights:", rounded(weights))
print(" output :", rounded(output))
assert isclose(weights[0], 0.0, abs_tol=1e-12)
def main() -> None:
positive_content_read()
permutation_test()
collision_failure()
non_identifiability_test()
negative_control_mean_task()
mask_test()
print("all attention laboratory checks passed")
if __name__ == "__main__":
main()
Expected output from the verified run
positive read
scores : [2.828, 0.0, 1.697]
weights: [0.724, 0.043, 0.233]
output : [8.638, 1.362]
paired permutation
original output: [8.638, 1.362]
permuted output: [8.638, 1.362]
key collision
weights: [0.5, 0.5]
mixed value: [50.0]
different routes, same result
route A output: [0.5, 0.5]
route B output: [0.5, 0.5]
negative control: exact mean without content addressing
uniform output: [8.0]
structural mask
weights: [0.0, 0.155, 0.845]
output : [5.071, 4.929]
all attention laboratory checks passed
How to extend the experiment
Replace the two-dimensional fixtures with a learned projection and a small classification task, but keep the same controls. Compare a content router with uniform weights and a fixed learned matrix. Train several seeds. Shuffle keys, values and key-value pairs separately. Add near-duplicate keys with contradictory labels. Measure final task error, route entropy and sensitivity to each intervention.
Then test whether the claimed benefit survives a simpler mixer at matched parameter count and compute. Michel, Levy and Neubig found that many trained attention heads could be pruned with little performance loss in their tested models, while some cross-attention layers were more dependent on multiple heads. The correct unit of necessity is therefore not “an attention-looking pattern exists”. It is the marginal task effect of the route under controlled removal.
For an implementation reference beyond the minimal lab, The Annotated Transformer documents a close reproduction of the original architecture. Use such implementations to check tensor shapes and masking, but retain the first-principles tests. Library correctness cannot establish that attention is the right mechanism for a new task.
Compact glossary
- Query
- A learned representation of what the current position requests from the candidate field.
- Key
- A learned address used in compatibility scoring. It controls routing but is not itself the transmitted content.
- Value
- The vector mixed into the output according to the routing weights.
- Self-attention
- Attention whose queries, keys and values are projected from the same input collection.
- Cross-attention
- Attention whose queries come from one collection and keys plus values from another.
- Mask
- A structural restriction applied to scores before normalisation, usually by assigning prohibited routes negative infinity.
- Route entropy
- A measure of how concentrated or diffuse the normalised weights are. It is not automatically calibrated uncertainty.
- Key collision
- A condition in which candidates requiring different treatment expose indistinguishable or insufficiently distinct keys.
The architecture decision changes from “add attention” to “prove the route”
Attention deserves its central place because it turns a collection of representations into a conditional communication field. A query can reach a distant candidate without walking through every intervening position. The candidate can expose one geometry for address and another for transmitted content. The whole operation is differentiable and efficiently expressed through matrix multiplication.
Those virtues do not remove the bargain. Soft routing mixes. Similar keys collide. Bare attention does not know order. Masks do not confer authority. Weight maps do not explain a whole model. Dense candidate graphs consume resources. Several simpler mixers can match attention when the task does not require content-conditioned interaction.
The changed decision is concrete. Begin with the task’s interaction structure, not the prestige of the architecture. Establish a mean, local, fixed-routing or recurrent baseline. Add attention only when candidate-dependent routing predicts a discriminating gain. Then run paired permutation, key shuffle, value ablation, collision and mask tests. Measure the task consequence, not the attractiveness of the heat map.
Attention is best understood as a learned routing proposal whose causal value must be demonstrated. Once that distinction is clear, the mechanism becomes easier to derive, easier to debug and harder to anthropomorphise. The question is no longer where the model “looked”. It is which content moved, under which learned address, through which allowed route, and what changed when that route was broken.
sequenceDiagram participant Q as Question participant M as Mechanism participant E as Evidence participant D as Decision Q->>M: The evidence wall that was rearranged overnight M->>E: Content-addressed is a mechanism, not a guarantee E-->>M: compare rival explanations E->>D: Sources and evidence ledger D-->>Q: revise scope or act
Sources and evidence ledger
- Bahdanau, Cho and Bengio, “Neural Machine Translation by Jointly Learning to Align and Translate”. Primary mechanism and experiment for adaptive weighted context.
- Graves, Wayne and Danihelka, “Neural Turing Machines”. Primary formulation of content and location addressing in a differentiable memory system.
- Sukhbaatar et al., “End-To-End Memory Networks”. Primary evidence for recurrent attention over external memory.
- Vaswani et al., “Attention Is All You Need”. Primary definition of scaled dot-product and multi-head attention in the Transformer.
- Lee et al., “Set Transformer”. Primary result on attention for permutation-invariant set modelling.
- Ramsauer et al., “Hopfield Networks Is All You Need”. Primary analysis connecting modern Hopfield updates and Transformer attention.
- Jain and Wallace, “Attention is not Explanation”. Primary negative evidence on treating attention weights as explanations.
- Wiegreffe and Pinter, “Attention is not not Explanation”. Primary competing interpretation and alternative tests.
- Serrano and Smith, “Is Attention Interpretable?”. Primary intervention evidence on weight magnitude and predictive impact.
- Brunner et al., “On Identifiability in Transformers”. Primary analysis of non-identifiable attention distributions.
- Tay et al., “Synthesizer”. Primary negative control for the necessity of token-token dot-product attention in tested models.
- Michel, Levy and Neubig, “Are Sixteen Heads Really Better than One?”. Primary head-pruning evidence.
- Dao et al., “FlashAttention”. Primary systems result separating exact attention semantics from memory-efficient execution.
- Xue, Moosavi and Aletras, “Deconstructing Attention”. Recent primary preprint; treated as provisional evidence about which design principles matter in tested language models.
- Rush, “The Annotated Transformer”. Reproducibility and implementation source.