Explain the math of scaled dot-product attention and why we divide by sqrt(d)

MediumConcept12 minRERSPro outline

The question

Write the formula for scaled dot-product attention. Explain what each matrix is and what the softmax is doing.

Why is there a division by the square root of the key dimension, and what would go wrong without it?

What the interviewer is looking for

  • Correct formula: softmax(Q K^T / sqrt(d_k)) V with shapes stated
  • The variance argument: if entries of q and k are independent with unit variance, q dot k has variance d_k
  • Large logits push softmax toward one-hot, which shrinks gradients for the other positions
  • Causal masking explained as adding minus infinity before the softmax
  • Cost of attention: quadratic in sequence length for the score matrix

Your answer

Saved in this browser only. Nobody else sees it.

Why this question

Written by BlogLLM in the style of a publicly reported Google DeepMind question type: explain the math behind a core LLM concept.

Reported in: glassdoor.co.in

Practice question written by BlogLLM based on publicly reported interview formats. Not an official Google DeepMind question.