Chapter 11

调试与常见错误

📌 commit af33f76跟 PyTorch 主分支跑的代价

TorchTitan 跟着 PyTorch 主分支走,常碰到 nightly 引入的 regression。本章列高频问题。

11.1启动 / 环境

现象处理
"DTensor not found"torch < 2.4;升 nightly
"AttributeError: no attribute 'pipelining'"torch 2.5+ 才有 stable PP
"context_parallel not found"torch 2.6+ 才有 CP
"torchao import 错"装 nightly torchao
tokenizer.model 找不到下载 Llama-3 tokenizer 放 ./tokenizer/

11.2OOM

位置处理
第 0 step OOM开 ac=full;增 tp_degree;减 seq_len / batch_size
第 N step OOM显存碎片化;PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
compile 后 OOMcompile 增加一些 staging tensor;关 compile 验证
float8 OOMfp8 buffer 有额外开销;改 rowwise(更省)

11.3NaN / loss spike

现象处理
0 step NaN用 bf16 而非 fp16;init_method_std 默认 0.02
compile 后 NaN关 compile 看是否消失;可能 inductor 算子有 bug
float8 训练 NaNrowwise 比 tensorwise 稳;降 lr 0.5×
async TP 时 NaNasync TP 通信切块会引入数值差异;关掉验证

11.4NCCL / 多机

现象处理
NCCL hang查 NCCL_SOCKET_IFNAME;多机要 IB
PP send/recv hangschedule 配错,某个 stage 没正确收发;检查 microbatch 数
NCCL allreduce hang不同 rank 走了不同分支(if rank==0 ...)
HSDP 不工作dp_replicate × dp_shard ≠ dp 总 GPU 数

11.5性能

现象处理
MFU 低于 40%没开 bf16 / compile / float8;先开这三个
compile 第一步慢 10×正常,编译期
seq_len 改了 compile 重新跑关 compile.dynamic_shapes 或固定 seq_len
多机比单机慢得多查 IB;开 HSDP;调小 dp_shard

11.6compile 兼容性

TorchTitan 是 PyTorch compile 测试场。常见问题:

11.7调试技巧

# nightly torch 引入 regression 时
pip install --pre torch==2.7.0.devXXXXXXX --index-url ...
# 锁版本验证

# 看 verbose
TORCH_LOGS=+dynamo,recompiles torchrun ...      # compile debug
NCCL_DEBUG=INFO

# memory snapshot
[memory_estimation]
enabled = true
disable_fake_mode = true

11.8这章你需要带走的