KatInterview

OLS Guarantees and Attention Mechanics in Transformers

Machine LearningModel answer
MediumModel answerMachine Learninglinear-regressiongauss-markovmulticollinearity

Last updated

Explain when OLS is the best linear unbiased estimator, how correlated features inflate variance, and how attention, positional signals and long-context costs work in Transformers.

This is a two-part conceptual question. Answer it as you would in a technical discussion: give precise conditions, explain why each claim holds, and note trade-offs.

Part 1: Ordinary least squares

You fit a linear model by ordinary least squares on a dataset with $n$ rows and $p$ predictors.

  1. Which assumptions about the data and the error term must hold for OLS to be the best linear unbiased estimator (BLUE)? Say which assumptions are not needed.
  2. Suppose several predictors are strongly correlated with one another. What happens to the variance of the estimated coefficients, and why does that make it hard to interpret the effect of any single predictor?

Part 2: Transformers

  1. Walk through the computation of scaled dot-product attention. Why are the query-key scores divided by $\sqrt{d_k}$, and what would go wrong during training without it?
  2. Attention by itself does not know the order of tokens. Why must position information be added, and how do fixed sinusoidal additive encodings compare with learned position embeddings, particularly when sequences at test time are longer than those seen in training?
  3. State the time and memory cost of self-attention as a function of sequence length $L$ and model width $d$. If the context grows to 100,000 tokens, which components become the main bottlenecks, and how can they be mitigated?

Hints

Hint 1

For BLUE, list the conditions on the design matrix and on the error's mean and covariance, then ask which of them is actually used to prove unbiasedness and which to prove minimum variance. Is a Gaussian error needed?

Hint 2

Write the variance of a single coefficient in terms of the R-squared obtained by regressing that predictor on the others, or look at the eigenvalues of X^T X. What happens when one is near zero?

Hint 3

For the scaling factor, assume query and key entries are independent with zero mean and unit variance and compute the variance of their dot product. Then think about the softmax gradient when the output is almost one-hot.

Hint 4

For long context, separately count the cost of the L-by-L score matrix, the arithmetic, and the key/value cache kept during generation. Memory-saving kernels fix some of these but not all.