KatInterview

LLM Core Mechanics: Attention, Decoding Strategies, and LoRA

Machine LearningModel answer
MediumModel answerMachine Learningtransformersdecodingfine-tuning

Last updated

Walk through how Transformers process sequences, how beam search compares to other decoding methods, and how LoRA cuts fine-tuning costs.

Modern large language models rest on a small number of foundational ideas. Being able to explain those ideas precisely — including the reasoning behind specific design choices — is a reliable signal of depth in an ML interview.

Answer all three parts in order. For each part, go far enough that you could field follow-up questions on the details.

Assume a decoder-only architecture (the dominant design in current LLMs) unless a part explicitly calls for something else.


Part 1 — How a Transformer processes a sequence

Describe what happens inside a single Transformer block and across the full forward pass. Your answer should cover:

  • How self-attention is computed: the role of the query, key, and value projections; why the dot products are scaled before the softmax; and what the output represents
  • What multi-head attention adds over a single attention head
  • How the model knows the order of tokens given that attention itself is position-agnostic
  • What the feed-forward sublayer, residual connections, and layer normalization each contribute
  • How a causal mask makes autoregressive generation possible, and what that implies for inference cost as sequence length grows

Part 2 — Decoding strategies: beam search and its alternatives

Explain how beam search turns a trained model into a text generator. Your answer should cover:

  • The algorithm step by step: what a "beam" is, how candidates are scored and pruned at each step, and how generation ends
  • Why raw cumulative log-probabilities favor short outputs and how that is corrected
  • How beam search compares to greedy decoding and to sampling-based methods in terms of output quality, diversity, and cost
  • The kinds of tasks where beam search is a strong choice and the kinds where it works against you

Part 3 — LoRA: efficient fine-tuning via low-rank adaptation

Explain the LoRA method and the practical decisions that come with using it. Your answer should cover:

  • The mathematical idea behind the low-rank weight update and why the initialization is set up the way it is
  • A concrete worked example showing how many trainable parameters LoRA introduces compared to full fine-tuning
  • The key hyperparameters (rank, scaling factor, which modules to target) and the intuition behind choosing them
  • How a LoRA-adapted model is deployed, including the trade-offs between merging the adapter into the base weights versus keeping them separate
  • The main failure modes and limitations to be aware of

Hints

Hint 1

For Part 1, work through the attention formula step by step before explaining why each piece is there — the scaling factor has a specific numerical motivation tied to the variance of dot products in high dimensions.

Hint 2

For Part 2, think about what 'greedy' decoding is doing as a degenerate case of beam search with width 1, then reason about what you gain and what you pay as you widen the beam.

Hint 3

For Part 3, start with the shapes: if a weight matrix is d × d, a rank-r factorization has two matrices of shapes d × r and r × d. Counting those parameters for a realistic d makes the savings concrete.