Linux上PyTorch内存管理实战指南

一 内存构成与监控要点
二 GPU显存优化策略
三 系统层面与主机内存管理
四 实战代码模板
import torch, gcfrom torch.cuda.amp import autocast, GradScalermodel = model.cuda()optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)scaler = GradScaler()accum_steps = 4for epoch in range(epochs):for i, (inputs, targets) in enumerate(train_loader):inputs, targets = inputs.cuda(), targets.cuda()with autocast():outputs = model(inputs)loss = criterion(outputs, targets) / accum_stepsscaler.scale(loss).backward()if (i + 1) % accum_steps == 0:scaler.step(optimizer)scaler.update()optimizer.zero_grad()# 释放本轮临时张量del outputs, loss, inputs, targetsif (i + 1) % 50 == 0:# 适度清理,避免频繁gc.collect()torch.cuda.empty_cache()def find_max_batch(model, input_shape, max_mem=8*1024**3, start=1):bsz = startwhile True:try:inp = torch.randn(*input_shape, device='cuda')with torch.cuda.amp.autocast():_ = model(inp[:bsz])used = torch.cuda.max_memory_allocated()if used > 0.9 * max_mem:return max(1, bsz - 1)bsz *= 2except RuntimeError:return max(1, bsz // 2)# GPUprint(f"Alloc: {torch.cuda.memory_allocated()/1024**2:.1f}MB"f"Reserved: {torch.cuda.memory_reserved()/1024**2:.1f}MB")print(torch.cuda.memory_summary())# 系统# watch -n 1 'free -h'sudo fallocate -l 8G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfileecho '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab以上模板可与梯度检查点、FSDP、更轻量优化器等按需组合,以在有限硬件上获得更稳健的训练流程。