python程序应用领域很广,mxnet, tensorflow, keras, pytorch等深度学习框架,都有基于python的接口和架构,当在训练或者inference的时候,如何确定程序中的热点,查找性能瓶颈并改进呢?
像mxnet, tensorflow都有自己的profiler, 但更多的是定位model或graph热点的,利用cProfile,我们可以定义general的热点,类似c++的valgrand.
运行:
python -m cProfile -s cumtime -o profile.status train.py
其中:
-m cProfile: 进行热点分析的选项
-o profile.status: 输出的分析日志文件名,二进制格式
-s cumtime: 指按照消耗时间进行排序
在程序完成后,运行:
python -c "import pstats; p=pstats.Stats('profile.status'); p.print_stats()"
则可显示具体的热点:
Tue Jun 18 18:33:44 2019 profile.status15474520 function calls (15254225 primitive calls) in 693.087 secondsOrdered by: internal timencalls tottime percall cumtime percall filename:lineno(function)295 639.467 2.168 639.467 2.168 {_pywrap_tensorflow_internal.TF_SessionRun_wrapper}1 7.496 7.496 7.496 7.496 {_pywrap_tensorflow_internal.TF_NewSessionRef}1 4.878 4.878 4.878 4.878 {_pywrap_tensorflow_internal.TF_CloseSession}295 2.787 0.009 2.787 0.009 {_pywrap_tensorflow_internal.ExtendSession}33897 2.534 0.000 2.534 0.000 {method 'SerializeToString' of 'google.protobuf.pyext._message.CMessage' objects}3 1.944 0.648 1.944 0.648