PyTorch 模型性能分析——PyTorch Profiler
前言
当深度学习模型完成训练开始部署、推理阶段,模型的推理速度、性能往往受到关注。目前主流DL framework都有各自的性能分析工具,本文主要介绍PyTorch 的性能分析工具——torch.autograd.profiler
测试环境
- ubuntu 18.04
- anaconda3 + python 3.7
- NVIDIA GPU/CUDA 10.2 (可选)
- PyTorch 1.6
Profiler 性能分析工具介绍
Profiler 一般指性能分析工具,用于分析APP、模型的执行时间,执行流程,内存消耗等。除了Pytorch,Tensorflow 这样的深度学习框架, 像NVIDIA CUDA, AMD ROCm 等也提供了各自的Profiler性能分析工具,比如 nvprof, rocprofiler。
PyTorch Profiler工具
pytroch Profiler位于torch.autograd.profiler
, 目前支持的功能:
- CPU/GPU 端Op执行时间统计
- CPU/GPU 端Op输入Tensor的维度分析
- Op的内存消耗统计
PyTorch 官网关于Profiler的介绍
https://pytorch.org/docs/master/autograd.html
image.png
Profiler分析CPU、GPU端Op执行时间
torch.autograd.profiler.profile(use_cuda=False...)
- CPU Only: 设置use_cuda=False
- GPU 模式:设置use_cuda=True, 注意:模型 以及输入Tensor 需要事先导入显存
CPU Only 模式
import os
import numpy as np
import torch
from torchvision.models import resnet18
import time
if __name__ == '__main__':
model = resnet18(pretrained=False)
device = torch.device('cpu')
model.eval()
model.to(device)
dump_input = torch.ones(1,3,224,224).to(device)
# Warn-up
for _ in range(5):
start = time.time()
outputs = model(dump_input)
torch.cuda.synchronize()
end = time.time()
print('Time:{}ms'.format((end-start)*1000))
with torch.autograd.profiler.profile(enabled=True, use_cuda=False, record_shapes=False, profile_memory=False) as prof:
outputs = model(dump_input)
print(prof.table())
profiler输出:(CPU Only)
image.pngGPU 模式
import os
import numpy as np
import torch
from torchvision.models import resnet18
import time
if __name__ == '__main__':
model = resnet18(pretrained=False)
device = torch.device('cuda')
model.eval()
model.to(device)
dump_input = torch.ones(1,3,224,224).to(device)
# Warn-up
for _ in range(5):
start = time.time()
outputs = model(dump_input)
torch.cuda.synchronize()
end = time.time()
print('Time:{}ms'.format((end-start)*1000))
with torch.autograd.profiler.profile(enabled=True, use_cuda=True, record_shapes=False, profile_memory=False) as prof:
outputs = model(dump_input)
print(prof.table())
profiler输出:(GPU)
image.png
使用Chrome trace可视化Profiler结果
上面的例子中,profiler的结果直接输出到终端,为了更进一步分析模型Op的执行关系,pytroch profiler支持生成 chrome trace json格式的输出,然后采用chrome 浏览器可视化结果:
只需要在上面的代码最后,加上 prof.export_chrome_trace('./resnet_profile.json')
import os
import numpy as np
import torch
from torchvision.models import resnet18
import time
# def process_event(profiler_events):
if __name__ == '__main__':
model = resnet18(pretrained=False)
device = torch.device('cuda')
model.eval()
model.to(device)
dump_input = torch.ones(1,3,224,224).to(device)
# Warn-up
for _ in range(5):
start = time.time()
outputs = model(dump_input)
torch.cuda.synchronize()
end = time.time()
print('Time:{}ms'.format((end-start)*1000))
with torch.autograd.profiler.profile(enabled=True, use_cuda=True, record_shapes=False, profile_memory=False) as prof:
outputs = model(dump_input)
print(prof.table())
prof.export_chrome_trace('./resnet_profile.json')
生成的JSON 文件
image.png
打开Chrome浏览器,在地址栏输入 chrome://tracing
导入profiler生成的JSON文件:
操作:
按键盘w, a, s, d键,可以对profiler的结果进行缩放和移动
Profiler 结果分析
上面内容主要是pytorch profiler的用法,我们更关心的是如何分析profiler的数据, 如何通过profiler发现模型的性能瓶颈,得出结论
模型整体分析
-
CPU 端的OP List
image.png -
GPU 端的OP List
image.png
CPU 和 GPU Op的关系
CNN/RNN/GAN/Transformer 等模型最终都是由许多Op组成的,在采用GPU设备的情况下,首先CPU端负责Op的调度(schedule),将Op的运算发送到GPU, GPU负责Op的具体运算。 笔者略微了解CUDA编程知识,在CUDA编程中, host(cpu)端调用GPU kernel function, GPU kernel启动之后,CPU与GPU异步执行。
image.png image.png
Op的wall_duration_time, self_time 区别
- wall_duration_time: 此Op的总共执行时间
- self_time: 此Op自身的执行时间,不包含调用其他子Op的执行时间
以relu_ Op为例:(relu_ 是in-place ReLU)
调用关系: relu_ ---->threshold_
- threshold_: wall_dur=154.624us
- relu_: wall_dur=179us, 由于relu_ Op又调用了threshold_ Op,因此relu_的self_time = 179 - 154 = 25us
relu_ op:
image.png
threshold_ op:
image.png
Op Tensor数据维度分析
PyTorch profiler提供了Op 输入维度
image.png