What does the dot product q . k actually measure inside an attention layer?
q dot k is the unnormalized affinity score between a query and a key, combining direction (cosine of the angle) with vector magnitude.
Picture two people at a job fair pointing flashlights at each other. The query is your flashlight, the key is theirs. When you both shine straight at each other, the beams overlap a lot, strong match. When you face perpendicular directions, the beams barely cross, weak match.When you face away, the beams point apart, negative match. The dot product is just a number that says how much the two beams overlap. The model later turns that overlap into a probability and uses it to decide how much of the key's payload to pay attention to.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Define the dot product geometrically, name the three alignment regimes, explain why magnitude also matters, motivate the sqrt(d_k) scaling via variance, and connect to softmax turning scores into weights.
Real products, models, and research that use this idea.
- Every transformer-based LLM in production (GPT-5.5, Claude Opus 4.7, Llama 4 Maverick, Gemini 3.1 Pro, Qwen 3.5) computes attention via batched `Q K^T` dot products.
- FlashAttention-2 and FlashAttention-3 fuse the `Q K^T` dot product with the softmax and the value matmul to avoid materializing the full score matrix in HBM.
- Vector database lookups (Pinecone, Weaviate, pgvector) often default to dot-product or inner-product scoring for the same composability and hardware-acceleration reasons.
What an interviewer would ask next. Try answering before peeking at the approach.
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Conflating the raw dot product with cosine similarity. They share direction information, but the dot product also scales with vector magnitudes, that magnitude effect matters.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.