Transformer Attention Math, Multi-Head Attention, and Masked vs Causal LM Objectives

Quick Overview

A machine learning fundamentals question on the Transformer: walk through one layer, write scaled dot-product attention with shapes, justify the square-root scaling and the softmax, explain multi-head attention and its cost, and compare masked and causal language modeling as pretraining objectives.

Transformer Attention Math, Multi-Head Attention, and Masked vs Causal LM Objectives

Company: ByteDance

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Technical Screen

Explain the Transformer architecture, focusing on the attention mechanism and the math behind it, and then compare the two self-supervised objectives most often used to pretrain language models. Use $n$ for the sequence length, $d$ for the model width and $h$ for the number of attention heads. ### Clarifying Questions - Should the walkthrough follow the original encoder-decoder model, a decoder-only model as used in current LLMs, or both? - Is a short derivation of the attention scaling factor expected, or an intuitive argument? - Should positional information and the placement of normalization be covered, or only attention? ### Part 1 — The Transformer layer Walk through what happens to a sequence of tokens from the input embeddings through one Transformer layer to the layer's output, and explain how encoder and decoder layers differ. ```hint One job per sublayer Name every sublayer in order and say what each contributes that the others do not. Then ask where encoder and decoder layers differ in what a position is allowed to look at. ``` #### What This Part Should Cover - Token embeddings and why position information must be added - The attention and feed-forward sublayers with residual connections and normalization - Encoder versus decoder layers: bidirectional versus causal attention, and cross-attention ### Part 2 — Scaled dot-product and multi-head attention Write the attention formula in terms of queries, keys and values, with matrix shapes. Explain why the scores are divided by $\sqrt{d_k}$, what the softmax does, and how multi-head attention is computed and why it helps. State the time and memory cost in terms of the sequence length. ```hint Size of a dot product Think about how the typical magnitude of a dot product grows with the vector dimension, and what large inputs do to a softmax and to its gradient. ``` #### What This Part Should Cover - The formula with shapes, the role of the mask, and the softmax as a soft lookup - A precise argument for the scaling factor - Multi-head projections, concatenation and output projection, with the cost compared to one full-width head and the quadratic term in sequence length ### Part 3 — Masked versus causal language modeling Compare masked language modeling and causal language modeling as pretraining objectives: what is predicted, which context each prediction may use, how the attention mask differs, how much training signal each sequence provides, and which models and downstream uses each objective suits. ```hint Count the graded positions Compare how many positions in one training sequence produce a loss under each objective, and what the model is allowed to see at each of them. ``` #### What This Part Should Cover - Both objectives written as losses, with their attention masks - Signal per sequence and the mismatch between pretraining and later use - Which model families and tasks each objective suits, and why current LLMs use causal language modeling ### What a Strong Answer Covers - Correct formulas with consistent dimensions - A derivation of the scaling factor rather than a slogan - Masks used correctly in both the attention and the pretraining discussion - Cost awareness: what is quadratic, what is linear, and where most parameters live - Objectives connected to the models and uses they suit ### Follow-up Questions - Why does a Transformer need positional information at all, and how do learned, sinusoidal and rotary position encodings differ? - Attention is quadratic in sequence length. What would you change to handle much longer contexts? - During generation, what does a key-value cache store, and how do multi-query and grouped-query attention reduce its size? - Could you build a sentence-embedding model from a causal language model? What would you change?

Overview: A machine learning fundamentals question on the Transformer: walk through one layer, write scaled dot-product attention with shapes, justify the square-root scaling and the softmax, explain multi-head attention and its cost, and compare masked and causal language modeling as pretraining objectives.

ByteDance logo
ByteDance
Aug 24, 2025
hardMachine Learning EngineerTechnical ScreenMachine Learning
0
0

Explain the Transformer architecture, focusing on the attention mechanism and the math behind it, and then compare the two self-supervised objectives most often used to pretrain language models. Use nn for the sequence length, dd for the model width and hh for the number of attention heads.

Clarifying Questions Guidance

  • Should the walkthrough follow the original encoder-decoder model, a decoder-only model as used in current LLMs, or both?
  • Is a short derivation of the attention scaling factor expected, or an intuitive argument?
  • Should positional information and the placement of normalization be covered, or only attention?

Part 1 — The Transformer layer

Walk through what happens to a sequence of tokens from the input embeddings through one Transformer layer to the layer's output, and explain how encoder and decoder layers differ.

What This Part Should Cover Guidance

  • Token embeddings and why position information must be added
  • The attention and feed-forward sublayers with residual connections and normalization
  • Encoder versus decoder layers: bidirectional versus causal attention, and cross-attention

Part 2 — Scaled dot-product and multi-head attention

Write the attention formula in terms of queries, keys and values, with matrix shapes. Explain why the scores are divided by dk\sqrt{d_k}, what the softmax does, and how multi-head attention is computed and why it helps. State the time and memory cost in terms of the sequence length.

What This Part Should Cover Guidance

  • The formula with shapes, the role of the mask, and the softmax as a soft lookup
  • A precise argument for the scaling factor
  • Multi-head projections, concatenation and output projection, with the cost compared to one full-width head and the quadratic term in sequence length

Part 3 — Masked versus causal language modeling

Compare masked language modeling and causal language modeling as pretraining objectives: what is predicted, which context each prediction may use, how the attention mask differs, how much training signal each sequence provides, and which models and downstream uses each objective suits.

What This Part Should Cover Guidance

  • Both objectives written as losses, with their attention masks
  • Signal per sequence and the mismatch between pretraining and later use
  • Which model families and tasks each objective suits, and why current LLMs use causal language modeling

What a Strong Answer Covers Guidance

  • Correct formulas with consistent dimensions
  • A derivation of the scaling factor rather than a slogan
  • Masks used correctly in both the attention and the pretraining discussion
  • Cost awareness: what is quadratic, what is linear, and where most parameters live
  • Objectives connected to the models and uses they suit

Follow-up Questions Guidance

  • Why does a Transformer need positional information at all, and how do learned, sinusoidal and rotary position encodings differ?
  • Attention is quadratic in sequence length. What would you change to handle much longer contexts?
  • During generation, what does a key-value cache store, and how do multi-query and grouped-query attention reduce its size?
  • Could you build a sentence-embedding model from a causal language model? What would you change?