v7:拼成完整配置、跑快、出问题怎么查

面试官给你一个模型和一堆卡,问的是 TP、PP、CP、EP、DP 每种开多大。配对了也可能只跑到 20% 的 MFU,或者中途 hang 住。这一课讲怎么拼配置、怎么跑快、出了问题怎么查。

面试会这样问Given a model and a cluster, how do you choose TP, PP and DP sizes? · Design a distributed training system for a trillion-parameter language model. · If GPU utilization is low during training, how would you debug it?

考过NVIDIA Senior SWE, LLM Applications 2025-09 等("Design a distributed training system for a trillion-parameter language model.",Aced 用户提交,NVIDIA 问过 3 次)· NVIDIA SWE 2025-12("If GPU utilization is low during training, how would you debug and benchmark performance?",Aced 用户提交)· NVIDIA High Performance LLM Training Engineer(Glassdoor RVW105730158)· NVIDIA DL Engineer 慕尼黑 2026("Can you explain the details of how distributed training works",Glassdoor RVW104512401)· NVIDIA DL Engineer HM 轮("parallel training methods",Glassdoor RVW98332074)· 小红书 NVIDIA 面经 VO 的 ML System Design(DP、PP、checkpoint、fault tolerance,岗位未写,存档)· 快手("hang都有可能因为什么,分别要如何排查";"DP、TP-SP的计算通信重叠原理")· 网易("FP8格式差异(E4M3高精度窄范围 vs E5M2宽动态范围)")· Tower("Debug Oscillating GPU Utilization During Training")· Meta("Design a Scalable MoE Pretraining Pipeline")

配方:先算 memory,再按通信从重到轻往外套

  1. 算 memory:16Ψ + activation,除以 80 GB,得出一份模型至少要几张卡
  2. TP 放机内,≤ 8:每层 4 次同步 all-reduce,必须走 NVLink
  3. 还放不下,跨机加 PP:取放得下的最小 p,micro-batch 数 m ≥ 4p,用 interleaved 1F1B
  4. 按需加:长序列加 CP,MoE 加 EP
  5. 剩下的卡全给 DP,并开 distributed optimizer(≈ ZeRO-1)

通信越重、越频繁,就放在越快的链路上。PP 只在段边界传一次 activation,DP 每个 step 才同步一次 gradient,还能藏在 backward 后面,走 IB 也来得及。

总卡数 = TP × PP × CP × DP。EP 不额外乘,MoE 层里沿 DP(× CP)方向原本存着相同 expert 的卡,改成各存不同的 expert(Megatron 叫 MoE Parallel Folding),attention 照样每卡一份。

三个例子

配置GPUTPPPCPDP每卡 model stateMFU
70B,seq 8K(Megatron 指南)64442270 GB → 约 39 GB–
Llama 3 405B,seq 8K16384816112850.6 GB,再沿 DP 切41%
Llama 3 405B,seq 128K16384816168同上38%

例 1:70B 放 64 卡(h = 8192、80 层、seq 8192、micro-batch 1、global batch 512)

例 2:Llama 3 405B。TP 8 × PP 16 = 128 张卡一份,每卡 50.6 GB,再用 FSDP 沿 DP 切 optimizer state。seq 从 8K 涨到 128K 时卡数不变,CP 1 → 16,每卡又是 8K token,DP 128 → 8。从内到外排成 [TP, CP, PP, DP]。

Trillion-parameter 设计题怎么答

按配方走,每一步给数字。先问清 dense 还是 MoE、多少卡、多少 token、seq 多长,下面设 dense、8192 张 H100、2T tokens、seq 4K。

  1. memory:1T × 16 bytes = 16 TB,光放 model state 就要 16 TB ÷ 80 GB = 200 张卡
  2. 切法:TP 8 → PP 16 → 128 张卡一份,每卡 125 GB 还是放不下 → 剩下 64 份 DP,开 distributed optimizer:7.8B × (6 + 12/64) ≈ 48 GB,余下约 32 GB 给 activation(SP + recompute)
  3. bubble:global batch 4096 条(16M tokens)→ 每条 pipeline m = 64 = 4p,bubble 15/64 ≈ 23%;interleaved v = 4 → 约 6%
  4. 时长:6ND = 1.2e25 FLOPs ÷(8192 × 989T × 40%)≈ 43 天,每秒要吃约 54 万 token
  5. checkpoint:fp32 master weight + m + v 共 12 bytes × 1T = 12 TB,每卡各写自己那片(约 1.5 GB)。异步写,还要能换 TP/PP 重新切分加载(Megatron 的 dist_checkpointing)
  6. 容错:Llama 3 在 16K 卡上 54 天中断 466 次,约 2.8 小时一次,所以要自动换坏节点、从最近的 checkpoint 重启
  7. data pipeline:提前 tokenize、分片、prefetch,data loader 读到哪也存进 checkpoint
  8. 监控:MFU、loss spike、gradient norm、每个 rank 的 step time(找 straggler)

跑快:overlap、FP8、kernel fusion

Overlap:通信放在单独的 CUDA stream 上,和不依赖它的计算同时跑,step time 只多出没被盖住的那部分。

哪一维要藏的通信和什么 overlapMegatron 开关
DP(DDP)gradient all-reduce分桶(PyTorch 默认 25 MiB),一桶满就发,同时算剩下的 backward--overlap-grad-reduce
FSDP / ZeRO-3parameter all-gatherprefetch 下一层的参数,同时算当前层--overlap-param-gather
TP + SPall-gather / reduce-scatter把 GEMM 切块,和同一个 GEMM 的其他块流水--tp-comm-overlap
PPsend / recv1F1B 稳态里另一个 micro-batch 的计算overlap_p2p_comm(需 interleaved)

FP8:H100 的 FP8 Tensor Core 峰值约 1979 TFLOPS,是 BF16 的 2 倍。

Kernel fusion:elementwise 操作是 memory-bound 的(第 0 课)。bias → GeLU → dropout 分 3 个 kernel 要读写 HBM 3 次,融成 1 个快约 3 倍。Megatron 默认融了 bias + GeLU、bias + dropout + add、masked softmax、RoPE。

目标 MFU 约 40%:Llama 3 405B 是 38–43%。MFU = tokens/s × 6Ψ ÷(卡数 × 989T)。

去查一下你 TikTok 任务的 tokens/s,代进去算 MFU。8B 在 32 张 H100 上,40% MFU 对应约 26 万 tokens/s(只算 LLM 部分,粗估)。按本课的配方,8B、32 卡用 ZeRO 就放得下,所以 TP = PP = CP = 1,卡全给 DP,就是课表里的 v3。另外准备一个你遇到过的慢、OOM 或 hang 的例子,第 10 课要用。

慢了怎么查

  1. 先量化:step time、tokens/s、MFU。nvidia-smi 的 utilization 只表示有 kernel 在跑,很小的 kernel 一直跑也显示 100%
  2. 再 profile:用 torch profiler 或 Nsight Systems 录几步 trace,看 GPU timeline 上的空白和这时 CPU 在干什么
  3. 分四类对症下药(下表)
  4. 控制变量:单卡 → 单机 → 多机看 scaling;换 synthetic data 排除 data loader;nccl-tests 测网络 bandwidth
类别trace 上看到怎么修
data loading每个 step 开头 GPU 空等加 num_workers、开 pin_memory、预处理离线做
commNCCL kernel 很长,旁边没有计算开 overlap;TP 留在机内;nccl-tests 查网络
computekernel 之间密集的小空隙(launch-bound);.item() 后的空隙;kernel 本身 memory-boundCUDA Graphs、kernel fusion、加大 micro-batch、别每步同步
straggler某个 rank 总是最后进 collective比较各 rank 的 step time,换节点、平衡数据长度

hang 了怎么查

collective 要求所有 rank 都调用,顺序和 shape 一致,少一个 rank 其余的就无限期干等。排查就是找出谁没进同一个 collective。

export NCCL_DEBUG=INFO
export TORCH_NCCL_TRACE_BUFFER_SIZE=2000   # 打开 flight recorder
export TORCH_NCCL_DUMP_ON_TIMEOUT=true     # 超时时 dump
py-spy dump --pid <PID>                    # 每个 rank 各跑一次

例:8 卡在 step 1200 卡住。py-spy 显示 rank 1–7 停在 DDP 的 all-reduce,rank 0 停在 save_checkpoint() 里的 all_gather,因为它写在 if rank == 0: 里。修法:让所有 rank 都调用它。

30 秒英文回答

I size memory first: 16 bytes per parameter plus activations tells me how many GPUs one replica needs. Then I place parallelism by communication cost: tensor parallel inside a node, up to 8; pipeline parallel across nodes with at least four micro-batches per stage; context parallel for long sequences and expert parallel for MoE; the rest goes to data parallel with a distributed optimizer. Then I overlap communication with compute, use FP8, and aim for about 40% MFU. If it's slow, I profile and split time into data loading, exposed communication, compute and stragglers. If it hangs, I find the rank that isn't in the same collective.

自测

Design a distributed training system for a trillion-parameter model.(NVIDIA 问过 3 次)

先问清 dense 还是 MoE、卡数、token 数、seq 长度。再按配方给数字(16 TB → TP 8、PP 16、DP 64,每卡约 48 GB,约 43 天)。最后讲 checkpoint、容错、data pipeline、监控。

If GPU utilization is low during training, how would you debug and benchmark performance?(NVIDIA SWE 2025-12 原题)

先量化 step time、tokens/s、MFU。录 trace 找 GPU 空白,归到 data loading、通信、compute、straggler 四类。再控制变量,每修一处重测 MFU。

训练每次都在 step N hang 住,怎么查?

每次同一步,多半是代码或数据。比如第 N 步的 checkpoint、eval、logging 里有 collective 只被部分 rank 调用,或者第 N 个 batch 特别长,某个 rank OOM 退出。用 py-spy 和 flight recorder 找出缺席的 rank。不固定在某一步的 hang 更可能是硬件或网络。

为什么 DP 放在最外层?

DP 通信最轻,每个 step 才同步一次 gradient,还能藏在计算后面。Megatron 默认的 tp-cp-ep-dp-pp 把 PP 放最外,因为 PP 同样很轻。要点是重的在内、轻的在外。