1. pytorch本身的profiler,1.8后新的profiler工具torch.profiler

https://pytorch.org/blog/introducing-pytorch-profiler-the-new-and-improved-performance-tool/

https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html

https://www.cnblogs.com/kaituorensheng/p/4453953.html

可以像tensorflow那样生成trace.json文件用chrome chrome://tracing/打开查看。

2. NVIDIA Nsight Systems

常用的nvprof只能看到kernel和拷贝等耗时,没有timeline,而nsys可以可视化看到timeline,更加直观。框架不依赖。

参考https://zhuanlan.zhihu.com/p/132582159 

https://developer.nvidia.com/nsight-systems

安装:sh NsightSystems-linux-public-2021.2.1.58-642947b.run --accept --quiet

同时在windows主机上安装对应版本。

使用样例:

nsys nvprof python xx.py  

nsys nvprof -o nsys_cuda1  python xx.py  

--force-overwrite true

会生成report.qdrep文件,用windows版本打开即可查看timeline,可以选中放大或者ctrl+鼠标中键缩放:

Nsight Systems - Analyze application algorithm system-wide

Nsight Compute - Debug/optimize CUDA kernel

Nsight Graphics - Debug/optimize graphics workloads

https://indico.cern.ch/event/962112/contributions/4047370/attachments/2159916/3643963/Nsight%20Systems%20-%20x86%20Introduction%20-%20CERN.pdf

https://docs.nvidia.com/nsight-systems/UserGuide/index.html

3. python profiling

https://docs.python.org/zh-cn/3/library/profile.html

python cprofile

python -m cProfile -s tottime xxx.py

sort_stats支持以下参数:'calls', 'cumtime', 'cumulative', 'filename', 'line', 'module', 'name', 'ncalls', 'nfl', 'pcalls', 'stdname', 'time', 'tottime'

输出内容字段简析:

1、ncalls : 是指相应代码 / 函数被调用的次数;

2、tottime: 是指对应代码 / 函数总共执行所需要的时间(注意,并不包括它调用的其他代码 / 函数的执行时间);

3、percall: 就是上述两者相除的结果,也就是 tottime / ncalls;

4、cumtime:则是指对应代码 / 函数总共执行所需要的时间,这里包括了它调用的其他代码 / 函数的执行时间;

5、percall: 则是 cumtime 和 ncalls 相除的平均结果。

https://docs.python.org/zh-cn/3.9/library/profile.html#pstats.Stats.sort_stats

https://blog.csdn.net/weixin_44648216/article/details/104072223

更多推荐