DEV Community

Cover image for đŸ”„ LLM Interview Series(3): Transformers Explained — Attention Is All You Need
jackma
jackma

Posted on

đŸ”„ LLM Interview Series(3): Transformers Explained — Attention Is All You Need

1. What problem were Transformers designed to solve compared to RNNs and LSTMs?

Key Concept: Parallelization & Long-Range Dependency Modeling

Standard Answer:
Before Transformers, sequence models such as RNNs and LSTMs dominated NLP tasks. While effective for short sequences, they suffered two major limitations that became more pronounced as datasets and tasks grew: poor parallelization and difficulty capturing long-range dependencies.

RNNs process tokens sequentially—token t cannot be computed until token t-1 is done. This creates a long chain of computations, preventing modern GPUs from fully parallelizing training. As datasets grew to billions of tokens, this approach became a bottleneck. Even LSTMs, designed to mitigate vanishing gradients with gating mechanisms, remained fundamentally sequential and slow.

Transformers addressed this by replacing recurrence entirely with self-attention, a mechanism that allows a model to access information from any position in a sequence at any time. This shift introduced O(1) sequential operations, enabling full GPU parallelization. Now, all tokens are processed simultaneously, and the attention mechanism determines how each token interacts with every other token.

Another fundamental advantage is how Transformers model long-range relationships. RNNs gradually “forget” earlier information as it moves through the hidden state, even with LSTM gating. Transformers, by contrast, use direct token-to-token interactions through attention weights. A token 200 positions away can directly influence the representation of the current token with no loss in gradient flow.

This explains why Transformers rapidly replaced LSTMs and RNNs across NLP, speech, vision, and multimodal tasks. Their parallelism enables training on massive corpora, their architecture captures both local and global structures effectively, and their scalability continues to support the development of today’s frontier LLMs.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. How exactly does vanishing gradient affect long-range modeling in LSTMs?
  2. Why is self-attention considered more expressive than recurrence?
  3. Could RNNs be competitive again with enough optimizations?

2. How does the Self-Attention mechanism work?

Key Concept: Query, Key, Value (Q/K/V) Computation

Standard Answer:
Self-attention is the core mathematical mechanism behind Transformers. Its purpose is to determine how much each token should pay attention to every other token in the same sequence. This is computed using Queries (Q), Keys (K), and Values (V), which are all learned linear projections of the input embeddings.

For each token, the model constructs:

  • Query vector (Q): “What am I looking for?”
  • Key vector (K): “What do I contain that others may want?”
  • Value vector (V): “What information should I deliver if I'm relevant?”

The attention score between two tokens is calculated by:

attention_score = softmax((Q · K^T) / √d_k)
output = attention_score · V
Enter fullscreen mode Exit fullscreen mode

The scaling by √d_k stabilizes gradients, preventing overly large values from dominating the softmax distribution. The softmax ensures that all attention scores sum to 1, creating a probability distribution over tokens.

The brilliance of this approach is that each token can directly reference any other token’s information, regardless of distance. This drastically improves long-range dependency modeling. Additionally, self-attention is fully parallelizable; all Q/K/V matrices can be computed simultaneously for all tokens.

A single attention head learns one kind of relationship—syntactic, semantic, positional, etc. To increase representational capacity, Transformers use multi-head attention, which runs multiple attention operations in parallel and concatenates their outputs. This allows the model to learn several complementary patterns at once.

Self-attention’s ability to compute global context with constant sequential operations (O(1) depth) and quadratic interactions (O(nÂČ) FLOPs) is why it remains unmatched in modern LLMs, despite emerging efforts in linear attention, sparsity, and state-space models.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. What role does softmax normalization play in self-attention?
  2. Why do we need multiple attention heads?
  3. How would you explain Q/K/V to a non-technical audience?

3. What is Multi-Head Attention and why is it important?

Key Concept: Parallel Representation Subspaces

Standard Answer:
Multi-Head Attention (MHA) expands the capacity and flexibility of the self-attention mechanism by allowing the model to learn multiple distinct types of relationships simultaneously. Instead of computing one attention function on a high-dimensional space, the Transformer splits the embedding dimension into several smaller subspaces and computes attention independently on each.

For example, with an embedding dimension of 512 and 8 attention heads:

  • Each head operates on a 64-dimensional subspace.
  • Each head learns different patterns—syntax, semantics, positional cues, punctuation structure, etc.
  • Their outputs are concatenated and projected back to 512 dimensions.

This creates a richer representation than a single-head system could produce. Each head captures a different facet of the sentence, similar to how multiple experts might analyze a paragraph from different angles.

multi_head_output = concat(head_1, ..., head_h) · W_o
Enter fullscreen mode Exit fullscreen mode

If only a single attention head were used, the model would be forced into one representational perspective, reducing expressiveness. MHA avoids this bottleneck and supports better generalization and emergent behaviors.

Empirical observations from interpretability studies confirm that certain heads consistently specialize in:

  • Coreference resolution (e.g., matching “he” to the correct noun)
  • Dependency relations
  • Long-range entity tracking
  • Numerical reasoning patterns

The diversity of these emergent roles is a key factor behind Transformers' success. It also explains why pruning some attention heads often has minimal performance impact—redundancy emerges naturally in large models.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. What happens if we dramatically increase or reduce the number of heads?
  2. Why do we split embeddings before attention instead of after?
  3. Can Multi-Head Attention be replaced with learned routing or mixture-of-experts?

4. What is Positional Encoding and why do Transformers need it?

Key Concept: Injecting Order Information

Standard Answer:
Unlike RNNs and CNNs, Transformers have no inherent notion of sequence order. Self-attention treats all tokens as a set rather than a sequence. Without positional information, the model would interpret “Dog bites man” identically to “Man bites dog.”

To address this, Transformers inject positional encodings into token embeddings before feeding them into the attention layers. These encodings provide a numerical indication of each token’s location in the sequence.

The original Transformer used sinusoidal positional encodings, defined as:

PE(pos, 2i)   = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
Enter fullscreen mode Exit fullscreen mode

These functions have useful properties:

  • They allow the model to generalize to longer sequences beyond training length.
  • Relative positions can be inferred because sinusoids of different frequencies create predictable phase differences.
  • No learned parameters are required.

However, modern LLMs often use learned positional embeddings or rotary positional embeddings (RoPE), which embed relative positioning directly into Q/K vectors. RoPE is particularly powerful because it supports extrapolation with rotary transformations and preserves distance relationships elegantly.

Positional encoding is critical because attention alone can’t model order. Adding positional signals transforms the model from a static bag-of-words processor into a system capable of handling complex linguistic patterns, syntax, and temporal sequences.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. Why do many state-of-the-art models prefer RoPE over learned absolute embeddings?
  2. How does positional encoding help Transformers generalize to longer sequences?
  3. What happens if we remove positional encoding entirely?

5. What are Transformer Encoder and Decoder layers, and how do they differ?

Key Concept: Encoder–Decoder Architecture

Standard Answer:
The original Transformer architecture consists of an encoder and a decoder, each with a stack of identical layers. They collaborate in tasks like machine translation, where the encoder processes the source sentence and the decoder generates the target sentence.

Encoder:

Each encoder layer contains:

  • Multi-Head Self-Attention
  • Feed-Forward Neural Network (FFN)
  • Residual connections + LayerNorm

The encoder focuses solely on understanding the input sequence. Its self-attention is bidirectional, meaning each token can attend to all others.

Decoder:

Each decoder layer includes:

  • Masked Multi-Head Self-Attention
  • Cross-Attention (attending to encoder outputs)
  • Feed-Forward Neural Network
  • Residual connections + LayerNorm

The masking in decoder self-attention ensures that the model cannot “peek ahead”—a requirement for autoregressive generation.

The cross-attention mechanism allows the decoder to align output tokens with encoder representations. This is vital in tasks like translation where output position t may need information from any input position.

Modern LLMs like GPT remove the encoder entirely and use only the decoder stack, relying on causal masking for generation. By contrast, models like BERT use only the encoder stack, enabling deep bidirectional understanding.

Understanding encoder-decoder differences is essential in interviews, as many interview questions implicitly assume you can compare LLMs like GPT (decoder-only) with foundational models like BERT (encoder-only).

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. Why is causal masking necessary in decoder-only models?
  2. What are the trade-offs between encoder-only vs decoder-only architectures?
  3. How does cross-attention differ from self-attention?

6. What is the role of the Feed-Forward Network (FFN) inside each Transformer block?

Key Concept: Token-wise Nonlinear Transformation

Standard Answer:
While attention helps tokens exchange information, the Feed-Forward Network (FFN) allows each token to undergo independent nonlinear transformation. The FFN is applied identically to every token, making it akin to a position-wise MLP.

The typical FFN structure is:

FFN(x) = W2 · GELU(W1 · x + b1) + b2
Enter fullscreen mode Exit fullscreen mode

Two important things happen here:

  1. Dimensional Expansion
    The intermediate layer is typically 4× the embedding dimension (e.g., 4096 hidden size for a 1024-dim embedding).
    This expansion allows the FFN to learn complex abstract representations.

  2. Nonlinearity via GELU
    GELU performs smoother gating than ReLU and empirically works better for LLMs.
    It creates a nonlinear mapping that enhances the model’s expressive capacity beyond linear attention mixing.

The FFN is crucial because attention alone is linear. Without FFNs, Transformers would collapse into a shallow linear system with limited representational power.

Additionally, the FFN handles token-wise transformations in parallel, making it extremely efficient on GPUs. In practice, FFNs account for the majority of FLOPs in large models—often 60–70%.

While attention determines what information should be shared across tokens, the FFN decides how each token’s representation should evolve. This dual pathway is part of what gives Transformers their depth and flexibility.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. Why is GELU preferred over ReLU in modern LLMs?
  2. Could attention layers replace FFNs entirely?
  3. What happens if we reduce the FFN dimensional expansion factor?

7. How do Residual Connections and Layer Normalization stabilize Transformer training?

Key Concept: Gradient Flow & Representation Stability

Standard Answer:
Transformers are deep networks with dozens—even hundreds—of layers. Without stabilization mechanisms, training such deep architectures would be nearly impossible due to exploding or vanishing gradients. Residual connections and layer normalization serve as core stabilizers.

Residual Connections

Residuals allow each sub-layer (attention or FFN) to learn modifications on top of the input instead of mapping from scratch.
Formally:

x_residual = x + sublayer(x)
Enter fullscreen mode Exit fullscreen mode

This shortcut:

  • Improves gradient flow
  • Prevents information loss
  • Helps layers learn incremental refinements rather than full transformations

Residual connections are so important that removing them typically collapses training entirely.

Layer Normalization

LayerNorm normalizes activations across the feature dimension:

LN(x) = (x - mean(x)) / sqrt(var(x) + Δ)
Enter fullscreen mode Exit fullscreen mode

This stabilizes the distribution of intermediate representations, preventing divergence and accelerating convergence.

Recent research introduced RMSNorm and Pre-LN Transformers, where normalization appears before attention and FFN instead of after. Pre-LN is widely used in GPT-like architectures because it improves gradient flow and removes the need for warm-up schedules.

Together, LayerNorm + Residuals provide the foundation that allows Transformers to scale to billions of parameters.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. What is the difference between Pre-LN and Post-LN Transformers?
  2. How do residuals reduce training instability?
  3. Why is RMSNorm becoming more common than LayerNorm?

8. Why is “attention” computationally expensive, and what optimizations exist?

Key Concept: O(nÂČ) Memory & Compute Cost

Standard Answer:
Self-attention scales quadratically with sequence length. For a sequence of length n, attention requires computing an n × n matrix of token interactions. This leads to:

  • O(nÂČ) memory cost
  • O(nÂČ) compute cost

For long sequences (e.g., 10K tokens), this becomes prohibitively expensive. A 10K × 10K matrix contains 100 million elements for a single head—multiplied across heads and layers, the cost becomes massive.

Optimizations

To address this bottleneck, several strategies exist:

  1. FlashAttention
    A memory-efficient kernel that tilts the compute-to-memory ratio by fusing operations and avoiding materializing large attention matrices. This dramatically reduces memory usage and speeds up training/inference.

  2. Sparse Attention
    Instead of attending to all tokens, certain patterns limit attention to blocks or neighborhoods (e.g., Longformer, BigBird).
    Complexity can drop to O(n log n) or even O(n).

  3. Linear Attention
    Kernels approximate softmax attention by decomposing it into linear forms:
    softmax(QK^T)V ≈ φ(Q)(φ(K)^T V)
    Reducing complexity to O(n).

  4. Mixture-of-Experts (MoE)
    Not an attention optimization per se, but allows massive model scaling with minimal compute by activating only a small number of expert FFNs.

Despite these advancements, dense quadratic attention remains the most accurate and stable method, which is why frontier LLMs continue using it with optimized kernels like FlashAttention.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. What are the trade-offs between sparse attention and dense attention?
  2. How does FlashAttention achieve memory efficiency?
  3. Why haven’t linear attention methods replaced softmax attention yet?

9. How does causal masking enable autoregressive generation?

Key Concept: Preventing Information Leakage

Standard Answer:
Causal masking (also called triangular masking) ensures that when generating token t, the model cannot access any token beyond t. Without this, the decoder could “cheat” by looking into the future.

The mask is typically implemented as:

mask[i][j] = -∞  if j > i
mask[i][j] = 0    otherwise
Enter fullscreen mode Exit fullscreen mode

Then applied before the softmax:

attention_scores = softmax((QK^T + mask) / √d_k)
Enter fullscreen mode Exit fullscreen mode

The negative infinity forces future positions to have zero probability after softmax.

This ensures:

  • Token 1 can attend only to token 1
  • Token 2 can attend to tokens 1 and 2
  • 

  • Token t can attend only up to token t

This strictly enforces left-to-right generation.

Without causal masking:

  • The model could condition on future context
  • Autoregressive sampling would collapse
  • Training would become inconsistent with inference

Causal masking is what allows GPT-style models to be trained in parallel on entire sequences but behave autoregressively during inference. This dual compatibility—parallel training + sequential inference—is one of the major breakthroughs enabling modern LLM scalability.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. Why can encoder attention be bidirectional but decoder attention cannot?
  2. What would happen if we removed masking entirely?
  3. How does causal masking interact with rotary positional embeddings?

10. Why did the Transformer architecture become the foundation for modern LLMs?

Key Concept: Scalability, Expressiveness & Parallelism

Standard Answer:
Transformers revolutionized NLP by introducing a highly scalable, fully parallel architecture capable of capturing global dependencies efficiently. This marks a fundamental shift away from recurrence-based models whose sequential nature limited their ability to train on massive datasets.

Transformers excel in several dimensions:

1. Parallelism

All tokens are processed simultaneously. This enables training on GPUs/TPUs efficiently, making LLMs with 100B+ parameters feasible.

2. Global Context Modeling

Self-attention allows every token to interact with every other token regardless of distance, enabling:

  • Long-range reasoning
  • Better abstraction
  • Stronger emergent behavior

3. Scalability Laws

Empirical scaling laws show that Transformers improve predictably as:

  • Data increases
  • Model size increases
  • Compute increases

This predictability accelerated the era of large-scale pretraining.

4. Architectural Flexibility

Transformers have adapted to:

  • Text generation
  • Speech recognition
  • Vision transformers (ViT)
  • Multimodal models
  • Reinforcement learning models using attention

This flexibility made them a universal architecture.

5. Stable Optimization

Components like residuals, layer normalization, GELU, and positional encodings create a robust training landscape.

6. Compatibility with massive datasets

Transformers are uniquely suited to ingest hundreds of billions of tokens, allowing the emergence of reasoning, coding, multilingual abilities, and chain-of-thought behaviors.

As a result, the Transformer architecture remains the backbone of almost all frontier LLMs, including GPT-4+, Gemini, Claude, LLaMA, Qwen, and many others.

Possible 3 Follow-up Questions: 👉 ( Want to test your skills? Try a Mock Interview — each question comes with real-time voice insights)

  1. What architectural innovations might replace Transformers in the future?
  2. Do scaling laws continue indefinitely, or are there diminishing returns?
  3. How do Transformers compare to state-space models like Mamba?

Top comments (0)