Natural Language Processing
Natural language processing
Learn Natural language processing: 40 practice questions and answers with an interactive quiz — from the offline AI Learning app.
Questions & Answers
Tap a question to reveal its answer. Written for self-study and interview preparation.
In standard scaled dot-product self-attention, why is the dot product divided by the square root of the key dimension d_k?
For large d_k the dot products grow large in magnitude (their variance scales with d_k), pushing softmax into saturated regions with extremely small gradients. Dividing by sqrt(d_k) keeps the logit variance around 1, stabilizing gradients and training. Without it attention saturates early and learning slows.
What is the time and memory complexity of vanilla self-attention with respect to sequence length n, and why does it matter for long contexts?
Both compute and memory are O(n^2) because every token attends to every other token, materializing an n-by-n attention matrix. This quadratic scaling is the primary bottleneck for long-context models and motivates FlashAttention (IO-aware tiling) and sparse/linear attention variants.
How do Rotary Position Embeddings (RoPE) encode positional information differently from learned absolute embeddings?
RoPE rotates the query and key vectors by an angle proportional to their absolute position, so the dot product between them depends only on their relative offset. This injects relative position directly into attention, generalizes better to unseen lengths, and enables length-extension tricks like NTK-aware scaling. Learned absolute embeddings instead add a position-indexed vector and extrapolate poorly.
What is the key algorithmic difference between Byte-Pair Encoding (BPE) and the Unigram language model tokenizer used by SentencePiece?
BPE is a greedy bottom-up merge algorithm that iteratively merges the most frequent adjacent symbol pair to build the vocabulary. The Unigram model is top-down and probabilistic: it starts from a large vocabulary and prunes tokens to maximize corpus likelihood under a unigram distribution, allowing multiple segmentations and subword regularization. Unigram can sample alternative tokenizations; BPE is deterministic.
Why do modern LLMs predominantly use Pre-LayerNorm (Pre-LN) rather than Post-LayerNorm (Post-LN) in transformer blocks?
Pre-LN normalizes the input to each sublayer before attention/FFN, keeping a clean identity residual path that yields well-behaved gradients at initialization and removes the need for learning-rate warmup at depth. Post-LN can produce exploding/vanishing gradients in deep stacks and is harder to train. The tradeoff is that Post-LN can slightly outperform when carefully tuned, but Pre-LN is far more stable at scale.
What is the purpose of the KV cache during autoregressive decoding, and what is its memory cost?
The KV cache stores the key and value projections of all previously generated tokens so each new token only computes attention against cached states instead of recomputing them, turning per-step cost from quadratic to linear. Its memory grows linearly with sequence length, layers, heads, and batch size, and it often dominates inference memory for long contexts, motivating MQA/GQA and cache quantization.
How does Grouped-Query Attention (GQA) reduce inference cost compared to standard Multi-Head Attention (MHA)?
GQA lets several query heads share a single key/value head, shrinking the KV cache and memory bandwidth needed at decode time while keeping more expressivity than Multi-Query Attention (one KV head total). It sits between MHA (one KV per query head) and MQA, offering most of MQA's speedup with little quality loss. This is why models like Llama 2/3 70B adopt it.
What is exposure bias in sequence-to-sequence models trained with teacher forcing?
During teacher forcing the model always conditions on ground-truth previous tokens, but at inference it conditions on its own (possibly wrong) predictions, creating a train/test distribution mismatch. Errors then compound over the generated sequence. Mitigations include scheduled sampling, sequence-level training objectives, and RL fine-tuning.
How do nucleus (top-p) sampling and top-k sampling differ when controlling generation diversity?
Top-k restricts sampling to the k highest-probability tokens regardless of how the mass is distributed, while top-p (nucleus) selects the smallest set of tokens whose cumulative probability exceeds p, so the candidate set size adapts to the distribution's sharpness. Top-p handles peaky and flat distributions more gracefully, avoiding cutting off too few or too many candidates.
Why is perplexity equal to the exponential of the average cross-entropy loss, and what does it intuitively measure?
Perplexity = exp(mean per-token negative log-likelihood in nats) = 2^(mean per-token cross-entropy in bits), so it is a monotonic transform of cross-entropy. Intuitively it is the effective branching factor: the average number of equally likely next tokens the model is choosing among. Lower perplexity means the model assigns higher probability to the held-out text.
What does BERTScore measure that BLEU and ROUGE do not capture well?
BERTScore computes token-level similarity using contextual embeddings (cosine similarity, greedily matched) rather than exact n-gram overlap, so it rewards semantic/paraphrastic equivalence that BLEU/ROUGE miss. This makes it more robust to synonyms and word reordering, though it is model-dependent and slower. BLEU/ROUGE remain surface-level lexical-overlap metrics.
What is the core idea of Direct Preference Optimization (DPO) and how does it differ from RLHF with PPO?
DPO reframes preference learning as a simple classification loss on chosen-vs-rejected pairs, using a closed-form relationship between the optimal RLHF policy and the reward, so it directly optimizes the policy without training a separate reward model or running RL. This removes PPO's instability, reward-model overhead, and sampling loop, while implicitly using the reference model as a KL anchor. PPO instead fits a reward model and optimizes it via on-policy RL.
In LoRA fine-tuning, what is actually being trained and why does it save so much memory?
LoRA freezes the pretrained weight matrix W and learns two low-rank matrices A and B such that the update is delta-W = B*A (rank r << dimension), so only the small A,B are trained and stored. This slashes optimizer-state and gradient memory by orders of magnitude and lets adapters be swapped/merged. At inference, B*A can be merged into W with zero added latency.
# delta_W = (alpha/r) * B @ A ; W_eff = W + delta_W
out = x @ W.T + (alpha/r) * (x @ A.T) @ B.TWhat problem does FlashAttention solve, and is it an approximation of attention?
FlashAttention is an exact (not approximate) attention algorithm that is IO-aware: it tiles the computation and uses online softmax so the large n-by-n attention matrix never gets written to slow HBM, only kept in fast SRAM. This reduces memory from O(n^2) to O(n) and speeds up by minimizing memory traffic, while producing outputs numerically equivalent to standard attention.
In a Retrieval-Augmented Generation (RAG) pipeline, why is chunking strategy critical to answer quality?
Chunk size and overlap determine whether a retrieved passage contains a coherent, self-contained answer span: too large dilutes the embedding and wastes context, too small fragments the relevant context and breaks coreference. Good chunking respects semantic/structural boundaries and adds overlap so facts spanning boundaries are retrievable. It directly drives retrieval recall and the grounding the generator sees.
Why is cosine similarity (rather than raw dot product) often preferred for comparing sentence embeddings, and when is dot product better?
Cosine similarity normalizes for vector magnitude, comparing only direction, which is robust when embedding norms vary with token frequency or length. Dot product is preferred when magnitude carries meaningful signal, e.g. models trained with a dot-product objective (like some dense retrievers) where norm encodes confidence/importance. The right choice should match the training objective and whether embeddings are L2-normalized.
What is the role of in-batch negatives in contrastive training of dense retrieval / sentence embedding models?
In-batch negatives treat the other examples' positives in the same batch as negatives for a given query, giving many cheap, hard-ish negatives per step without extra computation. This sharpens the embedding space so relevant pairs score higher than irrelevant ones, and larger batches generally improve representation quality. Adding mined hard negatives further boosts discrimination.
Why can chain-of-thought (CoT) prompting improve reasoning, and what is a key reliability caveat?
CoT elicits intermediate reasoning tokens that let the model allocate more sequential computation and decompose a problem, which empirically improves multi-step arithmetic/logic accuracy in sufficiently large models. The caveat is that the stated reasoning is not guaranteed to be faithful: the model may reach the answer by other means and the verbalized chain can be post-hoc or even misleading. So CoT explanations should not be treated as ground-truth rationales.
Which technique most directly reduces factual hallucination by constraining the model to external evidence?
Grounding generation in retrieved documents (RAG) constrains outputs to provided evidence and enables citation/attribution, directly reducing fabricated facts when retrieval is good. It is complementary to decoding tweaks and fine-tuning but addresses the root cause: missing or parametric-only knowledge. Lower temperature or larger models alone do not guarantee factuality.
How does a Mixture-of-Experts (MoE) layer increase model capacity without proportionally increasing inference FLOPs?
An MoE replaces a dense FFN with many expert FFNs and a router that activates only the top-k experts per token, so total parameters scale up while the per-token compute stays near that of one or two experts (sparse activation). This decouples capacity from FLOPs but adds routing overhead, load-balancing losses, and higher memory/communication costs. Quality depends on balanced expert utilization.
What is speculative decoding and what guarantee does it provide about output quality?
A small draft model proposes several tokens cheaply, and the large target model verifies them in one parallel forward pass, accepting the longest valid prefix via a rejection-sampling correction that matches the target's distribution. This yields a 2-3x speedup while provably producing samples from the target model's exact distribution. Speedup depends on the draft acceptance rate.
What is the difference between pretraining, instruction tuning, and preference optimization in the modern LLM training stack?
Pretraining learns broad language/world knowledge via next-token prediction on massive unlabeled corpora. Instruction (SFT) tuning teaches the model to follow tasks/formats using curated instruction-response pairs. Preference optimization (RLHF/DPO) then aligns outputs with human preferences for helpfulness, safety, and style. Each stage uses progressively smaller, higher-quality data.
What is catastrophic forgetting in the context of fine-tuning LLMs, and name one mitigation.
Catastrophic forgetting is the loss of previously learned capabilities when a model is fine-tuned on a narrow new distribution, because gradient updates overwrite weights important for old tasks. Mitigations include parameter-efficient tuning (LoRA/adapters that freeze base weights), rehearsal/replay of original data, low learning rates, and regularization like EWC. Mixing general data into the fine-tuning set is a common practical fix.
How do GPTQ/AWQ post-training quantization methods preserve accuracy at 4-bit, beyond naive rounding?
They are activation-aware: rather than rounding weights independently, they use calibration data to minimize the impact of quantization on each layer's outputs, protecting salient weight channels (AWQ scales important channels before rounding; GPTQ uses second-order/Hessian information to compensate rounding error). This keeps perplexity close to FP16 at INT4. Naive round-to-nearest ignores which weights matter for activations and degrades more.
How do subword tokenizers eliminate the out-of-vocabulary (OOV) problem that plagued word-level models?
Subword tokenizers build a vocabulary of frequent character sequences so any unseen word is decomposed into known subword units (down to bytes/characters in the worst case), guaranteeing every string is representable. This trades a fixed manageable vocabulary for slightly longer sequences and lets the model share morphology across related words. Byte-level BPE makes this fully closed over any Unicode input.
In sequence labeling (e.g. NER), why might a CRF layer on top of a neural encoder outperform independent per-token softmax classification?
A CRF models dependencies between adjacent labels via transition scores, enforcing valid tag sequences (e.g. an I-PER cannot follow an O or a B-LOC) and performing global Viterbi decoding. Independent softmax decisions can produce inconsistent BIO sequences because they ignore label transitions. The gain is largest when the output structure has strong sequential constraints.
What distinguishes the skip-gram and CBOW training objectives in word2vec, and which suits rare words better?
Skip-gram predicts surrounding context words from the center word, while CBOW predicts the center word from the averaged context. Skip-gram creates more training examples per occurrence and learns better representations for rare words, whereas CBOW is faster and smooths over frequent words. Both are static (one vector per word, no context sensitivity).
Why do transformers handle long-range dependencies better than RNNs despite RNNs having theoretically unbounded memory?
In a transformer any two tokens interact through a single attention hop (constant path length), so gradients and information flow directly regardless of distance, and the whole sequence is processed in parallel. RNNs must propagate signal step-by-step, where the effective path length grows with distance and gradients vanish/explode, limiting practical long-range modeling. The cost is the transformer's quadratic attention.
What is knowledge distillation and how does using soft targets help the student model?
Distillation trains a smaller student to mimic a larger teacher, typically by matching the teacher's softened probability distribution (logits scaled by temperature) in addition to hard labels. The soft targets convey 'dark knowledge', the relative similarities among classes, providing a richer training signal than one-hot labels and improving generalization. This yields compact models that retain much of the teacher's quality.
What is prompt injection and why is it fundamentally hard to fully eliminate in LLM applications?
Prompt injection is when untrusted input (e.g. a retrieved web page or user message) contains instructions that override the developer's intended system prompt, hijacking the model's behavior. It is hard to fully solve because the model processes instructions and data in the same token stream with no robust trust boundary, so any in-band text can be interpreted as a command. Mitigations (input filtering, privilege separation, tool sandboxing, output checks) reduce but do not eliminate risk.
Why is BM25 still competitive with dense retrieval, and how does hybrid search combine them?
BM25 is a strong sparse lexical baseline that excels at exact term and rare-keyword matching (e.g. product codes, names) with no training and great efficiency, where dense embeddings can blur exact tokens. Dense retrieval captures semantic similarity and paraphrase. Hybrid search fuses both scores (e.g. reciprocal rank fusion or weighted sum) to get lexical precision plus semantic recall.
When building sentence embeddings from a transformer, why is mean pooling over token embeddings often preferred over using the raw [CLS] token of a vanilla BERT?
In a vanilla (non-fine-tuned) BERT, the [CLS] token is not trained to be a good sentence representation for similarity, so its embeddings perform poorly on semantic textual similarity. Mean pooling aggregates information across all token embeddings and empirically gives stronger, more isotropic sentence vectors. Sentence-BERT addresses this by fine-tuning with a pooling layer on NLI/STS pairs.
emb = (token_embeddings * mask).sum(1) / mask.sum(1) # mean poolingWhat are the main tradeoffs when increasing a tokenizer's vocabulary size?
A larger vocabulary shortens sequences (fewer tokens per text, lower attention cost and better long-context use) and can capture more whole words/morphemes, but it inflates the embedding and output-softmax parameters/memory and makes rare tokens undertrained. A smaller vocab keeps embeddings compact but lengthens sequences and fragments words. The optimum balances sequence length, compute, and multilingual coverage.
How does gradient checkpointing reduce memory during transformer training, and what is the cost?
Gradient (activation) checkpointing stores only a subset of activations during the forward pass and recomputes the rest on demand during backprop, cutting activation memory from O(L) toward O(sqrt(L)) for L layers. The cost is roughly one extra forward pass (about 20-30% more compute). It enables training larger models or longer sequences on fixed memory.
What does it mean for an LLM's probabilities to be 'miscalibrated', and how can RLHF affect calibration?
A model is calibrated if its predicted confidence matches empirical correctness (e.g. answers stated with 80% confidence are right ~80% of the time). Base pretrained LLMs are often reasonably calibrated on token probabilities, but RLHF/preference tuning tends to make models overconfident and degrade calibration of verbalized and token-level confidence. Temperature scaling and post-hoc calibration can partially restore it.
What is the difference between zero-shot, few-shot, and fine-tuning, and when would you choose few-shot in-context learning?
Zero-shot gives only an instruction; few-shot adds a handful of input-output demonstrations in the prompt; fine-tuning updates model weights on task data. Few-shot in-context learning is preferred when you lack enough labeled data or training infrastructure, need fast iteration, or want one model to serve many tasks without per-task weights. Fine-tuning wins when you have ample data, need lower latency/cost per call, or require consistent specialized behavior.
In multilingual models, what is the 'curse of multilinguality' and how is cross-lingual transfer still beneficial?
The curse of multilinguality is the observation that, at fixed capacity, adding more languages eventually degrades per-language performance as model capacity is spread thinner (interference). Yet shared subword vocabularies and aligned representations enable positive cross-lingual transfer, so low-resource languages benefit from high-resource ones, and the model can do zero-shot cross-lingual tasks. Scaling capacity mitigates the curse.
Why does masked language modeling (BERT) produce better bidirectional representations for understanding tasks than causal LM (GPT), and what is MLM's drawback?
MLM masks random tokens and predicts them using both left and right context, yielding deeply bidirectional encodings ideal for classification/extraction. Causal LM only attends leftward, which is necessary for generation but gives weaker bidirectional understanding. MLM's drawbacks are a pretrain/finetune mismatch (the [MASK] token never appears downstream) and lower sample efficiency since only ~15% of tokens contribute loss per example.
What is the difference between extractive and abstractive summarization, and which is more prone to hallucination?
Extractive summarization selects and copies salient spans/sentences verbatim from the source, while abstractive summarization generates new phrasing that paraphrases the content. Abstractive is more fluent and concise but more prone to hallucinating facts not in the source, whereas extractive is faithful by construction but can be choppy and redundant. Faithfulness metrics and grounding are especially important for abstractive systems.
How does length-normalization in beam search address a known bias of standard beam search?
Standard beam search sums log-probabilities (all negative), so longer sequences accumulate lower total scores, biasing the model toward overly short outputs. Length normalization divides the score by a (possibly exponentiated) length term to make sequences of different lengths comparable. Without it, beam search systematically truncates outputs, especially in translation and summarization.
Related topics in Natural Language Processing
Put it into practice with AI Learning
This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.
- Free
- No account
- Works offline