Zenaique
Part ofAI Product Manager·Week 1: AI Fundamentals for PMsView roadmap →

Explain scaled dot product attention.

Short answer·Medium·4.8 · 318·~3 min·Asked atDecagonFreshworksMongodb·Relevant atDeepseekMicrosoftRunwayXai
Attempt it

Explain scaled dot product attention as you would in an interview. Include why the scaling factor matters.

Free · 2 AI evals / day
TL;DR

Scaled dot-product attention computes softmax of QK transposed divided by the square root of d_k, then weights V. The scaling keeps the softmax in a stable gradient regime.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine a room full of labelled boxes, each holding some content. You walk in with a question written on a card. You compare your card to every label, decide how relevant each box is, and then take a mix of their contents weighted by how relevant they are. The query is your card, the keys are the labels, the values are the contents, and the mix is the attention output. The square root divisor is a temperature knob that keeps the comparison fair when the cards are very detailed: without it, one label can shout so loudly that all the others go silent, and you would never get a useful mix.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Scaled dot-product attention is one of the most rehearsed pieces of interview content in modern machine learning, and also one of the most commonly misunderstood. Candidates can usually write down the formula. Far fewer can explain why the scale factor exists or why the softmax is normalised along the axis it is.

This section walks through the operation slowly enough that the why behind each step becomes obvious. By the end the formula should feel like the inevitable consequence of three design pressures, not a magic incantation.

Q, K, and V are three views of the same input

In self-attention, the inputs Q, K, and V are all derived from the same source sequence X by three separate linear projections. If X has shape [n, d_model], then Q = X W_Q, K = X W_K, and V = X W_V each have shape [n, d_k] or [n, d_v] depending on which projection. The three matrices W_Q, W_K, W_V are learned parameters.

The metaphor is a content-addressed lookup. Q is what each token is asking. K is how each token advertises itself, in the same vector space as Q so they can be compared by dot product. V is what each token actually contributes if its key wins. The fact that all three come from the same X is what makes self-attention content-addressed: a token can decide which other tokens to look at based purely on what is in the sequence, with no external index.

In cross-attention the only change is that K and V come from a different source than Q. Encoder-decoder models like the original Transformer use this for the decoder to attend to the encoder output.

The three-step computation
Why the square root of d_k
Variants and what stays invariant
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • PyTorch 2.0's `torch.nn.functional.scaled_dot_product_attention` ships the formula as a fused kernel that dispatches to FlashAttention 2 when available.
  • Hugging Face Transformers calls the same operation inside every attention layer of Llama, Qwen, and DeepSeek implementations.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does the softmax need normalisation along the key axis specifically?
A

Trace one row of the score matrix. Each row corresponds to one query and contains its similarity to every key. Softmax along that row turns the row into a distribution over keys, which is what the weighted sum of V requires.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Reciting the formula without justifying the scale factor. The square root of d_k is not cosmetic, it is the variance correction that keeps gradients from vanishing as dimensionality grows.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • State the formula in full, including the scale factor and the softmax position.

  • Explain the role of Q, K, and V as three projections of the input.

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain why transformers replaced RNNs for language modeling.
Short answer·Easy