PyTorch在Ubuntu上的性能测试方法与工具
在Ubuntu上测试PyTorch性能,需关注以下核心维度:
DataLoader的读写效率,直接影响训练节奏);PyTorch Profiler是官方提供的性能分析工具,可详细记录计算图、内存使用、GPU利用率等信息。结合TensorBoard可视化,能直观识别性能瓶颈:
torch-tb-profiler集成,代码示例如下:import torch
from torch.profiler import profile, record_function, ProfilerActivity
# 定义模型与数据
model = ... # 你的PyTorch模型
data = torch.randn(32, 3, 224, 224).to('cuda')
# 启动Profiler
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3),
on_trace_ready=lambda prof: prof.export_chrome_trace("trace.json"),
record_shapes=True
) as prof:
for _ in range(5):
with record_function("model_inference"):
output = model(data)
torch.cuda.synchronize() # 确保CUDA操作完成
# 启动TensorBoard查看结果
# 终端命令:tensorboard --logdir=./logs
使用Ubuntu自带命令行工具,监控系统级资源占用:
M排序内存,P排序CPU);linux-tools-common):sudo perf record -g -p <PID> # 记录目标进程
sudo perf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flamegraph.svg
iostat -x 1查看读写延迟);PyTorch内置的torch.utils.benchmark模块,用于精确测量代码片段的执行时间(支持CPU/GPU同步):
from torch.utils.benchmark import Timer
# 示例:测量矩阵乘法的GPU耗时
stmt = "torch.mm(a, b)"
setup = "import torch; a=torch.randn(256, 256).cuda(); b=torch.randn(256, 256).cuda()"
timer = Timer(stmt, setup, num_threads=4)
print(timer.timeit(100)) # 运行100次取平均
输出结果包含中位数耗时、四分位距(IQR),帮助排除偶然误差。
PyTorch官方提供的benchmark框架,覆盖训练、推理、多设备场景(如ResNet、Transformer等预置模型):
git clone https://github.com/pytorch/benchmark
cd benchmark && pip install -e . # 可编辑安装
python run.py -d cuda -t train --model resnet50;python run.py -d cuda -t train --profile --profile-devices cpu,gpu resnet50;logs/目录,可通过TensorBoard可视化(如GPU利用率曲线、内存占用趋势)。Volksdep是基于TensorRT的模型部署加速工具,支持PyTorch模型转换为TensorRT格式,并测试推理性能(吞吐量、延迟):
pip install "git+https://github.com/Media-Smart/volksdep.git";from volksdep import benchmark
import torchvision.models as models
# 加载模型并设置设备
model = models.resnet18().eval().cuda()
dummy_input = torch.randn(1, 3, 224, 224).cuda()
# 运行基准测试(默认测推理延迟)
benchmark(model, (1, 3, 224, 224))
对于大规模模型,可使用NCCL后端测试多GPU并行性能:
import torch.distributed as dist
import torch.multiprocessing as mp
def run(rank, world_size):
dist.init_process_group("nccl", rank=rank, world_size=world_size)
model = ... # 模型需支持多GPU(如DataParallel或DistributedDataParallel)
model = model.to(rank)
# 运行训练/推理...
if __name__ == "__main__":
world_size = torch.cuda.device_count()
mp.spawn(run, args=(world_size,), nprocs=world_size)
通过torch.utils.benchmark或官方benchmark框架,对比单GPU与多GPU的性能提升(如加速比)。
以上方法覆盖了PyTorch在Ubuntu上的微观代码优化、宏观系统监控、官方基准测试及第三方部署加速场景,可根据具体需求选择组合使用。