ubuntu

Ubuntu上PyTorch的性能测试方法

小樊
35
2025-10-27 06:36:49
栏目: 智能运维

Ubuntu上PyTorch性能测试方法

1. 基础性能基准测试

工具/方法:使用PyTorch内置的torch.utils.bottleneck工具,快速定位代码性能瓶颈。
步骤

2. CUDA设备性能测试

工具/方法:通过自定义脚本测试CUDA设备的计算性能(单卡/多卡)。
单卡测试示例

import torch
import time

def cuda_benchmark(device_id, N=1000000):
    torch.cuda.set_device(device_id)
    data = torch.ones(N).cuda()
    torch.cuda.synchronize()  # 确保CUDA操作完成
    start = time.time()
    for _ in range(10000):
        data += 1
    torch.cuda.synchronize()
    end = time.time()
    print(f"Execution time: {end - start:.4f} seconds")

cuda_benchmark(0)  # 测试第一块GPU

多卡并行测试:使用torch.nn.DataParallelDistributedDataParallel(DDP)扩展至多卡,测试多卡协同计算的性能提升。
用途:验证CUDA设备是否正常工作,评估单卡/多卡的算力性能。

3. PyTorch Profiler性能分析

工具/方法:使用torch.autograd.profilertorch.profiler(PyTorch 1.8+)分析模型各操作的耗时、内存占用及执行流程。
代码示例

import torch
import torch.nn as nn
from torch.profiler import profile, record_function, ProfilerActivity

model = nn.Sequential(nn.Linear(1000, 1000)).cuda()
inputs = torch.randn(64, 1000).cuda()

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("profile.json"),
    record_shapes=True,
    profile_memory=True
) as prof:
    for _ in range(5):
        with record_function("model_inference"):
            outputs = model(inputs)
            torch.cuda.synchronize()

prof.key_averages().table(sort_by="cuda_time_total", row_limit=10)

可视化:通过prof.export_chrome_trace("profile.json")导出Chrome Trace文件,在Chrome浏览器中打开chrome://tracing查看可视化性能图。
用途:深入分析模型内部操作的耗时(如卷积层、矩阵乘法),识别性能热点(如耗时操作、内存瓶颈)。

4. 实时系统资源监控

工具/方法:使用Ubuntu系统工具监控PyTorch运行时的硬件资源占用。

5. 模型推理性能测试

步骤

import torch
from torchvision import models, transforms
from PIL import Image

# 加载预训练模型
model = models.resnet50(pretrained=True).cuda()
model.eval()

# 数据预处理
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# 加载测试图像
image = Image.open("test.jpg").convert("RGB")
input_tensor = transform(image).unsqueeze(0).cuda()

# 推理并计时
start = time.time()
with torch.no_grad():
    output = model(input_tensor)
end = time.time()
print(f"Inference time: {end - start:.4f} seconds")

用途:评估模型在实际部署中的推理性能(如延迟、吞吐量),验证模型是否满足应用需求。

6. 混合精度训练性能测试

工具/方法:使用torch.cuda.amp(自动混合精度)测试混合精度训练的性能提升(减少显存占用、加速计算)。
代码示例

from torch.cuda.amp import autocast, GradScaler

scaler = GradScaler()
for inputs, targets in dataloader:
    inputs, targets = inputs.cuda(), targets.cuda()
    
    optimizer.zero_grad()
    with autocast():  # 自动混合精度计算
        outputs = model(inputs)
        loss = criterion(outputs, targets)
    
    scaler.scale(loss).backward()  # 缩放梯度防止溢出
    scaler.step(optimizer)         # 更新参数
    scaler.update()                # 调整缩放因子

用途:验证混合精度训练对性能的影响(如显存占用减少、训练速度提升),适用于需要大batch size的场景。

0
看了该问题的人还看了