Transformer、KV Cache、GPU Basics 面试指南:MLE / AI Infra 怎么讲清楚
Transformer面试KV CacheGPU面试ML Engineer面试AI Infra面试Model Serving

Transformer、KV Cache、GPU Basics 面试指南:MLE / AI Infra 怎么讲清楚

面向 ML Engineer、AI Engineer、AI Infra 候选人的 Transformer / KV Cache / GPU basics 面试指南,拆解 attention、prefill、decode、batching、latency、throughput、memory bandwidth、model serving 和常见追问。

Sam · · 13 分钟阅读

AI infra 和 MLE 面试里,Transformer、KV cache、GPU basics、model serving latency 变得越来越常见。尤其是 NVIDIA、OpenAI、Anthropic、Meta AI、xAI、Databricks、Scale AI、AI infra startup 这类岗位,面试官不一定要求你推完整论文公式,但会看你是否理解 LLM inference 的工程瓶颈。

很多候选人会背“Transformer 是 self-attention”,但一问到 KV cache 为什么加速、prefill 和 decode 有什么区别、batch size 怎么影响 latency、GPU memory bandwidth 为什么重要,就开始回答散。本文把这些高频点整理成面试可用的解释框架。

快速答案

如果面试官问 Transformer / KV cache / GPU basics,你可以先给出这条主线:

“Transformer inference has two phases: prefill and decode. Prefill processes the prompt in parallel and is compute-heavy. Decode generates one token at a time and repeatedly attends to previous tokens, so KV cache stores past keys and values to avoid recomputing them. Serving performance depends on sequence length, batch size, model size, memory bandwidth, GPU utilization, and scheduling.”

这段话不需要很花哨,但方向是对的。

Transformer 面试先讲结构,不要只背 attention

一个 decoder-only Transformer 常见模块:

  • token embedding
  • positional encoding 或 RoPE
  • multi-head self-attention
  • feed-forward network
  • layer norm
  • residual connection
  • output projection to vocabulary

面试里不要只说“attention learns relationships”。更好的解释:

“For each token, self-attention computes query, key, and value vectors. The query of the current token is compared with keys from previous tokens to decide which values to mix. In decoder-only generation, a causal mask prevents the model from attending to future tokens.”

这能自然引出 KV cache。

Prefill vs Decode

LLM inference 可以拆成两个阶段。

Prefill

Prefill 处理用户输入的 prompt。比如 prompt 有 2,000 tokens,模型会一次性计算这些 tokens 的 hidden states,并生成第一步需要的 KV cache。

特点:

  • 可以并行处理 prompt tokens。
  • 计算量大。
  • 对 GPU compute 更敏感。
  • prompt 越长,prefill 越慢。

Decode

Decode 是逐 token 生成。每生成一个 token,就把新 token 的 K/V 加到 cache 里,然后预测下一个 token。

特点:

  • 严格自回归,一次生成一个 token。
  • 对 memory bandwidth 很敏感。
  • batch scheduling 很重要。
  • output 越长,decode 时间越长。

面试中可以说:

“Long prompts hurt prefill latency, long outputs hurt decode latency.”

这句话非常实用。

KV Cache 为什么能加速

没有 KV cache 时,每生成一个新 token,都要重新计算之前所有 tokens 的 key 和 value。这样会浪费大量计算。

KV cache 的核心思想:

  • 对已经处理过的 tokens,把 key 和 value 存起来。
  • 下一个 token 只需要计算自己的 Q/K/V。
  • attention 时,用当前 query 去 attend cached keys and values。

所以 KV cache 主要节省的是重复计算 past tokens 的 K/V。

可以这样解释:

“KV cache does not remove attention over previous tokens. It avoids recomputing keys and values for previous tokens. The model still needs to read cached K/V, so memory bandwidth and cache size become important.”

这句能避免一个常见误区:以为 KV cache 让 attention 完全免费。

KV Cache 的代价

KV cache 加速计算,但会占 GPU memory。

cache 大小和这些因素相关:

  • batch size
  • number of layers
  • number of attention heads
  • head dimension
  • sequence length
  • precision:FP16、BF16、INT8 等

简单理解:

KV cache memory grows roughly linearly with:
batch_size * sequence_length * num_layers * hidden_size

所以当 batch size 或 context length 增大时,KV cache 会快速吃掉显存。

面试追问常见:

  • 为什么长上下文 serving 难?
  • 为什么 high throughput 会影响 latency?
  • 为什么 batch size 不能无限增大?
  • 为什么有些系统要做 paged attention?

回答方向:

  • 长上下文需要更大的 KV cache。
  • 大 batch 提高 GPU utilization,但增加排队和 memory pressure。
  • decode 阶段经常 memory-bound。
  • cache 管理需要避免碎片和浪费。

GPU Basics:面试需要讲到什么程度

大多数 MLE 面试不会让你写 CUDA kernel,但会问基础概念。

你至少要能解释:

  • GPU 擅长大量并行矩阵计算。
  • CPU 适合控制逻辑,GPU 适合 tensor operations。
  • GPU memory 和 CPU memory 之间拷贝很贵。
  • batch size 会影响 GPU utilization。
  • latency 和 throughput 是不同目标。
  • memory bandwidth 可能成为 bottleneck。

一个实用表达:

“For model serving, we want high GPU utilization, but user-facing latency also matters. Larger batches improve throughput, but they can increase queueing delay and KV cache memory pressure.”

Latency vs Throughput

AI infra 面试非常喜欢问 trade-off。

Latency

用户发请求到收到响应的时间。聊天产品、代码补全、voice agent 更重视 latency。

Throughput

单位时间处理多少 tokens 或 requests。离线 batch generation、annotation、document processing 更重视 throughput。

优化 latency 的方向:

  • shorter prompt
  • smaller model
  • streaming output
  • faster tokenizer / pre-processing
  • lower queue time
  • efficient KV cache

优化 throughput 的方向:

  • batching
  • continuous batching
  • quantization
  • optimized kernels
  • better scheduling
  • model parallelism

面试中不要说“batch 越大越好”。正确回答是:“It depends on the latency SLO.”

Continuous Batching 怎么讲

传统 batching 要等一批请求一起进入模型。问题是不同请求的 output length 不同,有些很快结束,有些很慢。

Continuous batching 的思路:

  • decode 每一步都可以把新请求加入 batch。
  • 完成的请求从 batch 中移除。
  • 让 GPU 更持续地保持 busy。

面试说法:

“Continuous batching improves GPU utilization for autoregressive decoding because requests can enter and leave the batch dynamically instead of waiting for a fixed batch to finish.”

可以进一步补:

  • 好处:higher throughput、better utilization。
  • 代价:scheduler 更复杂,KV cache management 更复杂。

Model Serving 常见追问

如果用户抱怨首 token 很慢?

看 prefill:

  • prompt 是否太长
  • retrieval context 是否太多
  • tokenizer / preprocessing 是否慢
  • queue time 是否高
  • model size 是否过大

如果生成速度慢?

看 decode:

  • output length
  • batch size
  • KV cache memory
  • GPU utilization
  • memory bandwidth
  • sampling settings

如果显存不够?

可以考虑:

  • smaller model
  • quantization
  • lower max context length
  • KV cache optimization
  • offload
  • model parallelism
  • request admission control

如果成本太高?

可以考虑:

  • routing easy requests to smaller model
  • cache frequent prompts / embeddings
  • batch offline jobs
  • reduce context length
  • distillation / quantization
  • monitor tokens per request

常见挂法

只背公式,不讲 serving

MLE / AI infra 面试看的是工程落地。公式懂但讲不出 latency、batching、memory,会显得离生产很远。

把 KV cache 讲成万能加速

KV cache 减少重复计算,但增加 memory pressure,并且 attention 仍然需要读取历史 K/V。

混淆 prefill 和 decode

首 token 慢和生成慢是两个不同问题。

不讲 trade-off

AI infra 没有单一最优解。必须围绕 SLO、成本、质量和用户体验做权衡。

英文回答模板

  • “Prefill is compute-heavy because it processes the prompt, while decode is autoregressive and often memory-bandwidth sensitive.”
  • “KV cache stores past keys and values so we do not recompute them for every generated token.”
  • “The trade-off is that KV cache consumes GPU memory proportional to batch size and sequence length.”
  • “Larger batches can improve throughput but may hurt latency due to queueing and memory pressure.”
  • “For user-facing chat, I would optimize time to first token and streaming decode latency. For offline generation, I would optimize throughput.”

一周准备计划

第 1 天:Transformer basics

复习 Q/K/V、causal mask、multi-head attention、FFN、residual、layer norm。

第 2 天:Prefill / Decode

用自己的话解释首 token latency 和 per-token latency。

第 3 天:KV Cache

练 KV cache 的作用、内存代价、长上下文影响。

第 4 天:GPU Basics

补 batch size、memory bandwidth、CPU/GPU transfer、utilization。

第 5 天:Serving Trade-off

练 latency vs throughput、continuous batching、quantization、cache。

第 6 天:Debug Case

准备“首 token 慢”“生成慢”“显存爆”“成本高”的排查路径。

第 7 天:Mock

做一轮 45 分钟 ML infra deep dive,强制用英文解释。

相关阅读

需要 ML Infra / MLE Mock?

Transformer、KV cache、GPU basics 这类面试最怕“概念知道但讲不成工程判断”。Interview Coach Pro 可以按 NVIDIA、OpenAI、Anthropic、Meta AI、xAI、Databricks、AI startup 风格做 ML infra deep dive、model serving system design、PyTorch coding 和英文表达训练。

查看 ML Engineer 面试准备服务
预约一次面试准备咨询

S

关于作者

Sam 是 Interview Coach Pro 的技术面试教练,长期辅导在美国求职的中文候选人准备 SDE、System Design、Behavioral、Data Engineer 和 ML Engineer 面试。

本文基于匿名面试复盘、公开岗位要求和一对一辅导中的高频问题整理,发布前会检查内容结构、术语准确性和可操作性。你也可以查看我们的 辅导团队辅导方法

相关面试辅导

如果你正在准备类似面试,可以直接从下面的专项辅导开始。

准备好拿下下一次面试了吗?

获取针对你的目标岗位和公司的个性化辅导方案。

联系我们