pytorch学习笔记

PyTorch 模型性能分析——PyTorch Profiler

2020-09-18  本文已影响0人  侠之大者_7d3f

前言

当深度学习模型完成训练开始部署、推理阶段,模型的推理速度、性能往往受到关注。目前主流DL framework都有各自的性能分析工具,本文主要介绍PyTorch 的性能分析工具——torch.autograd.profiler


测试环境


Profiler 性能分析工具介绍

Profiler 一般指性能分析工具,用于分析APP、模型的执行时间,执行流程,内存消耗等。除了Pytorch,Tensorflow 这样的深度学习框架, 像NVIDIA CUDA, AMD ROCm 等也提供了各自的Profiler性能分析工具,比如 nvprof, rocprofiler

PyTorch Profiler工具

pytroch Profiler位于torch.autograd.profiler, 目前支持的功能:

PyTorch 官网关于Profiler的介绍
https://pytorch.org/docs/master/autograd.html

image.png

Profiler分析CPU、GPU端Op执行时间

torch.autograd.profiler.profile(use_cuda=False...)

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.png

GPU 模式

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

image.png

导入profiler生成的JSON文件:
操作:
按键盘w, a, s, d键,可以对profiler的结果进行缩放和移动

image.png

Profiler 结果分析

上面内容主要是pytorch profiler的用法,我们更关心的是如何分析profiler的数据如何通过profiler发现模型的性能瓶颈,得出结论

模型整体分析

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 区别

relu_ Op为例:(relu_ 是in-place ReLU)
调用关系: relu_ ---->threshold_

relu_ op:


image.png

threshold_ op:


image.png

Op Tensor数据维度分析

PyTorch profiler提供了Op 输入维度


image.png
上一篇下一篇

猜你喜欢

热点阅读