Token LabSearch

← Paper library
Attention & efficient inference / 2017

Transformer: turn token compatibility into information flow

Track Q, K and V, interpret a causal attention row, and distinguish parallel training from autoregressive generation.

Attention Is All You Need · Vaswani et al. · arXiv:1706.03762

Original editorial analysis by Token Lab · Source checked 2026-10-25 · Paper claims and synthetic examples are separated.

01 · What the paper establishes

The Transformer replaces recurrence with attention-based encoder and decoder blocks. The paper evaluates translation and parsing. Scaled dot-product attention, multiple heads and positional signals are central components; the original model is not simply today’s decoder-only LLM.

Primary source: paper abstract ↗
Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

  • Understand: state assumptions, shapes/units and a counterexample.
  • Implement: reproduce a numerical example and inspect boundaries.
  • Investigate: compare one alternative under a matched budget.

02 · The mechanism

Text / 原始公式
Q=XW_Q, K=XW_K, V=XW_V
S=QKᵀ/√dₖ + M
A=softmax(S) row-wise
O=AV; Mᵢⱼ=−∞ when j>i (causal case)

A query chooses how to mix values by comparing itself with keys. For N tokens, Q and K have N rows; their product contains N×N compatibility scores. The softmax is applied per query, so each allowed row sums to one. Scaling by √dₖ controls score magnitude under a simple independent-coordinate variance argument. Positional information is necessary because content-only matching does not encode sequence order.

Notation and intuition are teaching explanations. Consult the original method for complete assumptions and implementation details.

Mechanism map & worked derivation

Input token vectors X → Project Q, K and V → Scores → mask → softmax → Mix V → output vectors
Original schematic; read the steps from top to bottom. SVG ↓
Symbols and assumptions
SymbolMeaning
Q, K ∈ R^(N × d_k)Queries and keys: one row per token
V ∈ R^(N × d_v)Values to be mixed
A ∈ ℝᴺˣᴺRow-normalized mixing weights
  1. Check dimensions before numbers: QKᵀ is N×N; multiplying by V returns N×dᵥ. A matrix shape test catches many transposition mistakes.
  2. For logits [2,1,0], subtract the maximum to get [0,−1,−2]. Exponentials are [1,0.367879,0.135335], whose sum is 1.503214. Division yields [0.665241,0.244728,0.090031].
  3. The weighted sum for values [1,3,5] is 0.665241+0.734185+0.450153≈1.849579. Mask forbidden logits before normalization; masking weights afterward without renormalization changes the operation.
Experiment checkpoint

Check causal zeros and future-token perturbation invariance, not only row sums. Handle an all-masked row explicitly to avoid NaNs.

LOCAL / SYNTHETIC / INTERACTIVE

Change one variable. Explain what moves.

Original teaching calculation, not a reproduction of the paper’s benchmark. Bars are relative within this panel; numeric values and units are authoritative.

04 · Work through an example

Original example: focus on the third token with allowed scores [2,1,0] and values [1,3,5]. Softmax produces approximately [0.665,0.245,0.090], giving output 1.850. A fourth, future value must have exactly zero weight in a causal implementation. Adjust the first score below and observe the mixture rather than treating attention as a hard lookup.

05 · Design your experiment

Build a four-token single-head reference implementation. Assert row sums and causal zeros. Perturb a future token and verify that earlier outputs do not change. Compare gradients with finite differences on a tiny input. Only after those checks, add multiple heads and compare batched output with a per-head implementation.

Research deliverable

State one hypothesis; save the configuration and raw results; identify a competing explanation; report the narrowest supported conclusion. Use the linked lab as a local starting point, not a full paper reproduction.

Open related lesson / lab →

06 · Limits & reviewer questions

Editorial caution: an attention weight is a mixing coefficient, not by itself a causal explanation of a model’s decision. Full-sequence training can compute positions in parallel, but autoregressive generation still depends on previously generated tokens. Dense attention also creates a sequence-length scaling problem.

If every row sums to one, why can a mask bug still leak future information?

Normalization checks only probability mass. A row can sum to one while assigning mass to forbidden columns. Test the allowed support and perturbation invariance separately.

07 · Read the original

Section 3, especially scaled dot-product attention, multi-head attention and positional encoding.

Year is the first arXiv submission year. Unversioned links follow revisions; record the exact version in your reproduction report. DeepSeek-R1 here is explicitly pinned to v1.

Continue this topic

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Self-attention: X is N×d_model; W_Q,W_K are d_model×d_k; W_V is d_model×d_v. Scores are N×N and output N×d_v. The variance argument assumes independent, zero-mean, unit-variance coordinates. Cross-attention can have different query/key lengths.

Displayed formula, bilingual explanation and declared slider range. This code reproduces the scalar teaching calculation, not the paper training system.

Before running: predict a boundary case

Build a four-token single-head reference implementation. Assert row sums and causal zeros. Perturb a future token and verify that earlier outputs do not change. Compare gradients with finite differences on a tiny input. Only after those checks, add multiple heads and compare batched output with a per-head implementation.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node transformer-attention.mjs. No third-party runtime dependencies.

import {paperCalc} from './paper-core.mjs';
const result=paperCalc("attention",2);
console.log(result);
Reference output — reveal after predicting
{
  "labels": [
    "Token 1",
    "Token 2",
    "Token 3",
    "Future token (masked)"
  ],
  "values": [
    0.6652409557748218,
    0.24472847105479764,
    0.09003057317038046,
    0
  ],
  "unit": "weight",
  "extra": 1.849579234791117,
  "extraLabel": "Weighted value"
}

Test and transfer

Build a four-token single-head reference implementation. Assert row sums and causal zeros. Perturb a future token and verify that earlier outputs do not change. Compare gradients with finite differences on a tiny input. Only after those checks, add multiple heads and compare batched output with a per-head implementation.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope