DistributedDataParallel 而非 DataParallelmodel = torch.nn.parallel.DistributedDataParallel(
model,
device_ids=[local_rank],
output_device=local_rank
)DP 是单进程多线程,已被淘汰DDP 是多进程,性能更好backenddist.init_process_group(backend="nccl")torch.cuda.set_device(local_rank)NCCL_SOCKET_IFNAME 指定网卡if (step + 1) % accum_steps == 0:
optimizer.step()
optimizer.zero_grad()bucket 优化(DDP 默认开启)DDP(model, bucket_cap_mb=25)torch.cuda.ampscaler = torch.cuda.amp.GradScaler()torch.compile(PyTorch 2.x)model = torch.compile(model)pin_memory + num_workersDataLoader(..., pin_memory=True, num_workers=8)prefetch_factornum_workers=8, prefetch_factor=4from torch.distributed.fsdp import FullyShardedDataParallelWebDataset / MosaicML StreamingDistributedSampler(dataset, shuffle=True)new_lr = base_lr * (global_bs / base_bs)warmup_steps = 5–10% of totaltorch.profilerwith torch.profiler.profile(
activities=[torch.profiler.ProfilerActivity.CUDA]
) as prof:
train_step()| 现象 | 可能原因 |
|---|---|
| GPU 利用率低 | DataLoader 慢 |
| 多机比单机还慢 | 网络 / NCCL 配置 |
| 显存爆 | 未用 FSDP / ZeRO |
| 训练不稳定 | LR 未随 batch 放大 |
| 卡死 | 端口 / 网卡错误 |
✅ 中小模型
✅ 大模型
torch.compile如果你愿意,可以告诉我:
我可以直接给你针对性的调优方案或代码片段。