Why Self-Attention Divides Dot Products by the Square Root of Key Dimension

Last updated: November 29, 2026

Quick Overview

In scaled dot-product attention, scores are computed as $QK^T / \sqrt{d_k}$. Explain mathematically why the scaling is there and what happens without it.

Google
Machine Learning
Machine Learning Engineer
Google
November 29, 2026
Machine Learning Engineer
Technical Screen
Machine Learning
Medium

0

0

0 solved


In scaled dot-product attention, scores are computed as $QK^T / \sqrt{d_k}$. Explain mathematically why the scaling is there and what happens without it.

Google invented the Transformer, and interviewers for Gemini, Search, and Ads modeling roles often probe whether candidates understand the mechanics rather than just the API. This checks variance reasoning and how softmax saturation affects gradients.

What the Interviewer Expects
  • Derive the variance of a dot product of random vectors
  • Explain softmax saturation and vanishing gradients
  • Connect the fix to keeping logits at unit scale
  • Mention alternatives such as QK normalization or temperature
  • Note interaction with initialization and dimension size
Key Topics to Cover
Transformers
Softmax saturation
Variance analysis
Gradient flow
QK normalization
How to Approach This
  1. Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
  2. Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
  3. Feature engineering is often more impactful than model selection.
  4. Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
  5. Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
  • Would the scaling still matter if Q and K were layer-normalized?
  • Why not divide by $d_k$ instead of $\sqrt{d_k}$?
  • How does attention logit growth cause instability in large-model training?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Explore ML Interview Prep
Sample Answer
Clarify

Assume query and key components are roughly independent with mean 0 and variance 1 at initialization, which is what standard init and layer norm aim for....

Approach

Compute the variance of a raw dot product as a function of dkd_k, then show what large-variance logits do to softmax and its gradient....


Submit Your Answer
Markdown supported