Transformers and Attention Questions
The transformer architecture that underlies modern language and multimodal models. Covers self-attention and multi-head attention, positional encoding, encoder/decoder structures, and why transformers scale better than recurrent alternatives. Focuses on the architectural intuition behind contemporary foundation models.
Design positional encoding schemes for 2D image patches used in ViT. Compare flattened 1D positional embeddings, separable 2D embeddings (row and column), and learned 2D sin/cos embeddings. Discuss implications for translation invariance, ability to generalize to larger images, and parameter overhead.
Sample Answer
Approach: I’ll describe three practical 2D positional schemes for Vision Transformer (ViT) patches, give their properties, and compare them on translation invariance, extrapolation to larger images, and parameter overhead.
- Flattened 1D learned positional embeddings
- Description: Learn a distinct embedding vector for each patch index after flattening (Hp × Wp patches → N positions).
- Pros: Simple, directly usable with standard ViT code; can learn arbitrary position-specific priors.
- Translation invariance: Low — embeddings are absolute; model will not be translation equivariant unless trained on many translations or augmented heavily.
- Generalization to larger images: Poor — fixed-size lookup prevents straightforward extrapolation; resizing requires interpolation or re-training.
- Parameter overhead: O(N · D) where N = Hp·Wp, can be large for high-resolution patch grids.
- Separable 2D learned embeddings (row + column)
- Description: Learn two sets of embeddings: one for row indices (Hr) and one for column indices (Wr); patch embedding = row[r] + col[c].
- Pros: Factorized, fewer parameters; captures grid structure and relative relations along axes.
- Translation invariance: Still absolute, but factorization encourages sharing across rows/cols which can help learning translational patterns.
- Generalization: Better than 1D flattening — you can extend to larger Hr/Wr by reusing or interpolating row/col embeddings; parameter count grows linearly with dimension, not area.
- Parameter overhead: O((Hr+Wr)·D), much smaller than flattened when grid is large.
- Learned 2D sin/cos (or fixed sin/cos 2D) embeddings
- Description: Extend transformer sinusoidal PE to 2D: use positional functions of x,y (e.g., sin/cos of different wavelengths) or learnable frequency coefficients.
- Pros: Encodes continuous coordinates and relative offsets; no per-position params (if fixed) or small param set (if learnable frequencies).
- Translation invariance: Better at modeling relative position — sin/cos preserve relative differences and can help downstream attention learn translation-equivariant patterns.
- Generalization: Strong — naturally extrapolates to larger images because functions are defined for any coordinate; interpolation-free.
- Parameter overhead: Minimal (O(D) for fixed frequencies; O(K·D) if learning K frequencies), lowest among options.
Trade-offs and practical recommendations:
- If model must handle multiple resolutions or larger images than training: prefer 2D sin/cos or separable embeddings.
- If dataset is fixed-size and capacity to memorize spatial priors is desired: flattened 1D may yield best empirical performance but at cost of parameters and poor generalization.
- Separable embeddings are a pragmatic middle ground: low overhead and easier extension (interpolation), but still rely on absolute positions.
- For translation robustness use architectural choices (local windowing, relative positional bias in attention) alongside PE; relative positional encodings combined with sin/cos give best balance of invariance and generalization.
Architect a training pipeline to train a 100B-parameter Transformer across multi-node GPU clusters. Cover data ingestion (sharding, streaming, deduplication), parallelism strategy (tensor + pipeline + data), optimizer state management (ZeRO), checkpointing, failure recovery, and cost/performance trade-offs. Explain choices to minimize wall-clock time while preserving numerical stability.
Sample Answer
Requirements and constraints:
- Train a 100B-parameter Transformer with minimal wall-clock time, stable numerics, tolerable cost; target multi-node GPU cluster (A100/H100), 100–1000 GPUs, 100Gb+ interconnect; tolerate preemption/failures; reproducible checkpoints.
High-level architecture:
- Data platform (sharded, deduped, streaming) → Training orchestration (Kubernetes + job manager) → Distributed runtime (PyTorch + DeepSpeed/Megatron-LM) → Storage (S3-like object store + parallel FS for checkpoints).
Data ingestion:
- Single canonical dataset layer: ingest raw sources into append-only parquet shards with metadata (source, hash, provenance).
- Deduplication: content-addressed dedupe using MinHash + exact fingerprint (BLAKE3) at ingestion; keep provenance to avoid leakage.
- Sharding/streaming: produce many small shards (>> number of workers) enabling streaming read and elastic scaling. Use record-level indexes and byte-range reads (HTTP range) so each process streams its shard.
- Preprocess once (tokenize, pack, cache byte offsets) and store compressed TFRecord/Parquet for high throughput.
Parallelism strategy:
- Model-parallel core: combine tensor (1D/2D) parallelism for large matrix slices + pipeline parallelism across transformer stages to fit activations/params.
- Data parallelism outer loop for throughput. Example: with 256 GPUs, configure tensor parallelism TP=8, pipeline PP=4, data parallel DP=8 (TPPPDP=256).
- Use micro-batching + gradient accumulation to maximize GPU utilization and keep global batch size for optimizer stability.
Optimizer state management (ZeRO):
- Use DeepSpeed ZeRO-3 (partition optimizer, gradients, and parameters) to fit 100B on available memory.
- Offload optimizer states to host NVMe when needed (ZeRO-Offload) to trade cost vs speed.
- Maintain mixed-precision (bfloat16/FP16) with an FP32 master copy for parameters or use ZeRO-3 to store FP32 master shards.
- Use dynamic loss scaling and maintain fp32 copies for moment estimates (Adam) to preserve numerical stability.
Checkpointing & failure recovery:
- Incremental, asynchronous checkpointing:
- Regular lightweight checkpoints of optimizer/ZeRO shards + model metadata to object storage (consistent snapshot via barrier).
- Full checkpoint via aggregated write staged through local NVMe then parallel upload to S3 to avoid NFS bottlenecks.
- Checkpoint format stores sharded parameter + optimizer state aligned with ZeRO partitioning to allow partial recovery and rank rebalancing.
- Fast restarts: use elastic rendezvous to remap shards; support topology changes by re-sharding or readjusting ZeRO partitioning.
- Periodic validation checkpoints (every N hours) stored redundantly.
Numerical stability and wall-clock trade-offs:
- Use mixed-precision to accelerate; keep FP32 masters for optimizer to avoid drift.
- Larger global batch sizes reduce wall-clock but can harm generalization—use LARS/AdamW tweaks, linear warmup + cosine decay, and normalize batch-size-dependent lr scaling.
- Offloading ZeRO reduces memory pressure but increases IO latency; prefer in-GPU ZeRO when interconnect is excellent (NVLink/NCCL) to minimize wall-clock; offload to NVMe when GPUs constrained and accept added wall-clock for fewer nodes.
- Pipeline stage balancing and operator fusion to reduce bubble time; overlap communication with compute (async NCCL) and prefetch next micro-batch.
Cost/performance considerations:
- Minimize nodes by maximizing TP/PP fit, but avoid time lost from excessive pipeline bubbles—use profiler to tune TP/PP/DP.
- Use spot/preemptible instances for data-parallel shards with frequent lightweight checkpoints; keep primary nodes for pipeline stages on stable instances.
- Autoscaling data loaders and sharded prefetch to ensure GPUs are fed—IO starvation costs more than extra storage.
Metrics to optimize:
- Throughput (tokens/sec), time-to-accuracy, cost-per-token, checkpoint/recovery time.
- Instrument per-stage utilization, NCCL bandwidth, NVMe throughput; iterate configuration for best wall-clock under budget.
This design balances maximal in-GPU parallelism and ZeRO-3 memory savings to minimize wall-clock while enforcing fp32 master copies and careful checkpointing to preserve numerical stability and fast failure recovery.
Provide a quantitative explanation for why the dot-product of random Q and K vectors grows with dimension and why dividing by sqrt(d_k) stabilizes gradients and softmax outputs. Include a short derivation assuming components of Q and K are independent with zero mean and variance 1/d_k.
Sample Answer
Situation: We want a concise quantitative argument why the raw dot-product Q·K grows with dimension and why dividing by sqrt(d_k) is needed.
Derivation:
Let Q_i,K_i be independent, zero-mean, Var(Q_i)=Var(K_i)=σ^2. The dot product S = ∑{i=1}^{d_k} Q_i K_i. E[S]=0. Because terms are independent,
Var(S)=∑{i=1}^{d_k} Var(Q_i K_i).
For zero-mean independent Q_i,K_i, Var(Q_i K_i)=E[Q_i^2]E[K_i^2] = σ^4, so Var(S)=d_k σ^4 and std(S)=σ^2 sqrt(d_k).
Two common parameterizations:
- If σ^2=1 (components unit variance), std(S)=sqrt(d_k) — dot-products grow like sqrt(d_k).
- To keep std(S)=O(1) we set σ^2=1/√d_k for each entry? More standard: set Var(Q_i)=Var(K_i)=1/d_k gives Var(S)=d_k*(1/d_k)^2 = 1/d_k and std~1/√d_k — but the practical convention used in Transformers scales S by 1/√d_k (equivalently balancing if inputs have unit variance).
Cleaner typical setup: if Q_i,K_i ~ N(0,1) then S has std = sqrt(d_k). Dividing S by sqrt(d_k) yields a normalized logit with O(1) variance.
Why this matters for softmax and gradients:
Softmax probabilities p_j = exp(S_j)/∑exp(S). If logits S_j have std ~ sqrt(d_k), larger d_k makes logits spread larger → softmax becomes extremely peaked (close to one-hot). Gradients of softmax-cross-entropy involve p_j(1−p_j) or p_j−y_j: when logits are large, p_j saturates near 0/1 and gradients vanish or become numerically unstable. Scaling S ← S/√d_k keeps logits O(1), preventing saturation, preserving meaningful gradients and stable training.
Takeaway: divide by √d_k to keep dot-product scale independent of dimension so softmax outputs and gradients remain well-conditioned as d_k changes.
Provide a technical explanation for why multi-head attention often outperforms single-head attention with the same total dimensionality. Discuss representation subspaces, the ability to capture multiple relations in parallel, and propose empirical experiments you would run to validate the hypothesis.
Sample Answer
Multi-head attention outperforms single-head (with equal total dimensionality) because it partitions the model's representational capacity into multiple lower-dimensional subspaces that can specialize and run in parallel, rather than forcing a single projection to encode all relations.
Why this helps:
- Representation subspaces: Each head has its own Q/K/V projections, learning distinct linear subspaces of the embedding space. Lower-dimensional heads can focus on features or relational patterns that are easier to linearly separate in those subspaces (e.g., syntax, coreference, local vs. global context).
- Parallel capture of multiple relations: Different tokens have multiple simultaneous relations (positional, semantic, syntactic). Multiple heads let the model attend to different relations concurrently; a single high-dim head must multiplex those signals, leading to interference and harder optimization.
- Optimization and specialization: Independent heads reduce competition during gradient updates. Heads can specialize (sparse or focused attention) reducing variance in gradient signals and improving convergence. Empirically, attention maps show decorrelated patterns across heads.
- Implicit ensembles and robustness: Multiple heads act like an ensemble of attention patterns; the model is more robust to noise or one head failing.
Proposed experiments to validate:
- Equal-parameter comparison: Train Transformer encoders where total d_model and parameter count are fixed, varying number of heads (e.g., 1, 4, 8, 16). Evaluate on language modeling, translation, and probing tasks (syntactic/semantic probes). Expect multi-head variants to outperform single-head.
- Synthetic multi-relation tasks: Create tasks requiring simultaneous orthogonal relations (e.g., copy with permutation + parity check). Measure sample efficiency and final accuracy across head counts.
- Head ablation and redundancy: Mask or zero individual heads post-training to measure performance drop; multi-head should show graceful degradation and specialized importance distribution.
- Representational analysis: Compute pairwise CKA/CCA between heads' key/query/value subspaces to quantify decorrelation; higher decorrelation correlates with better performance.
- Orthogonality regularization: Add a penalty encouraging head projection orthogonality; check if this improves single-head-to-multi-head gap—if so, supports subspace separation hypothesis.
- Gradient signal analysis: Track gradient norms and variance per-head vs single-head during training to show reduced competition.
Measured outcomes: task metrics, convergence speed, CKA/CCA similarity, attention sparsity, head importance distribution, and statistical significance across seeds. Together these tests isolate whether gains come from representational partitioning, parallel relation capture, or optimization dynamics.
Propose a design to shard a quantized 70B-parameter Transformer model across heterogeneous devices (multiple GPUs and CPU nodes) to serve high-throughput, low-latency requests. Address scheduling of requests, memory placement of shards, communication minimization, token-level latency optimization, and failover strategies.
Sample Answer
Requirements & constraints:
- Serve a quantized 70B Transformer with high throughput and sub-100ms token-latency targets for single-token and streaming requests.
- Heterogeneous cluster: multiple GPUs (mixed memory/compute), several CPU nodes with RAM/NVMe.
- Minimize inter-device communication, support per-request scheduling, and robust failover.
High-level approach:
- Hybrid sharding: combine tensor-parallel (TP) within racks for compute-heavy linear layers, pipeline-parallel (PP) across stage-capable devices for layer sequence, and memory offload to CPU/NVMe for inactive weights/KV cache. Use GPTQ / 4-bit or 8-bit quantization (bitsandbytes/GPTQ) to reduce memory footprint.
Components & responsibilities:
-
Shard planner (static+adaptive)
- Precompute an initial partitioning: group consecutive layers into pipeline stages sized by GPU memory/FP throughput; within a stage, split large matmuls with TP across multiple GPUs.
- Use a cost model (memory, FLOPS, PCIe/NVLink bandwidth, latency) and run bin-packing to assign shards.
- Produce placement maps and fallback plans.
-
Runtime scheduler
- Accepts incoming requests and classifies: single-token streaming vs batched completions.
- Two execution modes:
a) Low-latency mode: prioritize small micro-batches, route to reserved GPU slots with warm KV cache; use token-level pipelining to overlap compute and comms.
b) Throughput mode: accumulate larger batches and schedule across more GPUs. - Scheduler also performs request coalescing within a target latency budget (e.g., up to 8–16ms wait).
-
Communication layer
- Use NCCL for intra-node/NVLink all-reduce and sharded all-gather; use RDMA/gRPC with binary protocol for inter-node transfers.
- Implement CUDA IPC and GPUDirect RDMA to avoid CPU copies.
- Support fused kernels (attention + layernorm) to reduce intermediate transfers.
Memory placement & KV handling:
- Store quantized static weights on GPU where possible; overflow shards on CPU RAM via pinned memory with mmap+NVMe-backed swap for very large models.
- Keep active KV cache co-located with the GPU executing a pipeline stage to avoid cross-device KV transfers. For streaming requests, reserve hot slots with pinned KV caches; for cold requests, use lazy fetch from CPU.
- Use memory-mapped files for quantized weights so multiple processes can share read-only pages.
Communication minimization techniques:
- Operator fusion to reduce temporary tensors.
- Shard layout that minimizes cross-stage dependencies: colocate layers with heavy attention-head communication on same device group.
- Reduce precision on inter-shard transfers (e.g., quantize inter-node activations to int8 with stochastic rounding when safe).
- Asynchronous, chunked KV transfers and gather-scatter to start downstream computation while remaining chunks stream.
Token-level latency optimization:
- Token-pipelining: stream partial outputs; while stage N computes token t+1, stage N+1 consumes token t immediately.
- Micro-batching with adaptive waiting: if no coalescing opportunity, process single token on reserved low-latency path.
- Pre-warming: maintain a pool of warm contexts (weights, KV) for the top-N tenants or recent requests.
- Priority queuing: prioritize last-token or interactive sessions to minimize tail latency.
Scheduling example flow:
- New interactive request arrives → scheduler checks warm pool → if hit, route to low-latency pipeline, allocate reserved token slot, start token-pipelined execution.
- If miss, start cold load: stream quantized shards from CPU to GPU in background, start stage where ready, fall back to CPU execution for earliest tokens if needed.
Failover & reliability:
- Replicated critical shards: for low-latency paths keep 1 active + 1 warm replica on other GPU/node; maintain consistent placement map.
- Checkpoint and immutable shard snapshots on NVMe; atomic switch-over using consistent routing tables.
- Health monitoring + fast re-route: when a device fails, scheduler reroutes requests to replicas or degrades to CPU-only execution with adaptive quality reductions (smaller beam, lower precision).
- Graceful degradation: progressively reduce batch sizes, drop speculative layers (if using early-exit heads) or serve smaller-context model until recovery.
Trade-offs & considerations:
- More replication → lower tail risk but higher memory cost.
- Aggressive offloading reduces GPU memory pressure but increases PCIe/RDMA latency.
- Quantizing inter-stage activations saves bandwidth but can slightly lower quality; validate with calibration.
Metrics & validation:
- Measure p50/p95/p99 token latency, throughput (tokens/sec), GPU utilization, and cross-node bandwidth.
- Run A/B tests to validate quantization/activation compression does not materially degrade output quality.
Technologies to implement:
- PyTorch with FSDP-inspired sharding + custom pipeline scheduler, NCCL, GPUDirect RDMA, bitsandbytes/GPTQ for quantization, Redis/gRPC for control plane, Prometheus/Jaeger for observability.
This design balances memory locality, minimized communication, token-level pipelining, and robust failover for heterogeneous clusters serving a quantized 70B Transformer.
Unlock Full Question Bank
Get access to all Transformers and Attention interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.