-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add profiling model doc #4694
add profiling model doc #4694
Conversation
Thanks for your contribution! |
关于paddle.profiler模块的API说明,在API文档的[paddle.profiler](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/profiler/Overview_cn.html)中, 这里主要根据常用使用场景来进行示例说明。 | ||
|
||
1、 将paddle.profiler.Profiler作为Context Manager, 对所包含的代码块进行性能分析 | ||
- 对某一段batch的训练过程进行性能分析,如batch [2,10),前闭后开区间 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对训练过程的某一段迭代过程进行性能分析,通过scheduler=(2, 10),指定开始和结束的迭代序号,则将对第2~9个迭代进行性能分析。
另外用迭代还是batch更好,我也不是很确定,是否需要请模型的同学确认下表达的习惯。
out.backward() | ||
momentum.step() | ||
momentum.clear_grad() | ||
prof.step() # 每迭代一个step(batch), prof也调用step(), 告知Profiler进入了下一个step(batch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释建议:在训练的一个step结束的位置,调用prof.step(),标记Profiler进入下一个step
momentum.step() | ||
momentum.clear_grad() | ||
prof.step() # 每迭代一个step(batch), prof也调用step(), 告知Profiler进入了下一个step(batch) | ||
# 离开with代码块,性能分析结束 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
离开->退出?
prof.summary() # 打印统计表单 | ||
``` | ||
该段代码会对整个训练过程性能数据进行采集(默认的scheduler参数会让Profiler始终保持收集数据的RECORD状态),即batch [0, 20)在CPU和GPU上的性能数据(默认的targets参数会判断是否支持GPU数据的采集,支持则自动开启), 并将收集到的性能数据以chrome tracing timeline的格式保存在profiler_log文件夹(默认的on_trace_ready参数会将日志文件保存到profiler_log文件夹)中,最后对收集到的性能数据进行统计分析打印到终端。在正常使用中不推荐这种方式,因为采集性能数据的batch太多有可能会耗尽所有的内存,并且导出的文件也会非常大,一般采几个batch的性能数据就能够对整个程序的运行情况有个判断了,没必要采集所有数据。 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前半句内容上建议做一些简化,毕竟是使用指南,对于API过多的细节描述,其实应该是有相应文档说明的。括号内的解释是不是最好不要涉及实现层面的内容?比如RECORED状态这样的描述。建议:
该段代码会对整个训练过程性能数据进行采集,统计batch [0, 20)的CPU和GPU性能数据,并将收集到的性能数据以chrome tracing timeline的格式默认保存在profiler_log文件夹,最后对收集到的性能数据进行统计分析打印到终端。通常不推荐使用这种方式,因为采集的batch太多对内存的消耗过大,并且导出的文件也会非常大,一般建议跳过开始的迭代,采样少量batch的性能数据对整个程序的运行情况进行判断。
p.stop() # 打印总的benchmark表单 | ||
``` | ||
这段代码会只开启Profiler的benchmark统计功能,用于输出模型的吞吐量和执行时间信息,而不开启详细性能数据的采集。如果只需要获得ips(iterations per second)的数据, | ||
而不关心各部分的详细性能,可以如上所示设置timer_only=True。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是否要把打印的内容贴上,直观地展示,在timer_only=True时,只会打印出模型性能的统计数据。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一节移动到api文档。
Others - 44.03 / - / - / - / 15.01 0.52 / - / - / - / 3.94 | ||
--------------- ------ ---------------------------------------- ---------------------------------------- | ||
``` | ||
benchmark工具输出的信息如下所示(由于打点位置不同和数据处理上的差异,benchmark输出的Reader Ratio和上面统计表单中Dataloader的比例不完全一致) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实打点的位置是不是都是在__next__里面打的?benchmark的计时功能是统计的全部迭代的平均值,而性能分析采样的是部分区间,所以比例有所不同。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改说明
``` | ||
|
||
从timeline和统计表单中可以看到,dataloader占了执行过程的很大比重,甚至接近了50%。通过分析程序发现,这是由于模型本身比较简单,需要的计算量小,再加上dataloader | ||
准备数据时只用了单线程来读取,使得程序近乎没有并行操作,导致dataloader占比过大。通过对程序做如下修改,将dataloader的num_workers设置为4,使得能有多个线程并行读取数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
描述上应该是单进程和多进程
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已改
可以看到,从dataloader中取数据的时间大大减少,变成了平均只占一个step的1.8%,并且一个step所需要的时间也相应减少了。从benchmark工具给出的信息来看,ips也从平均30增长到了101,程序性能得到了极大的提升。 | ||
通过Profiler工具,您也可以使用RecordEvent对您所想要分析的程序片段进行监控,以此来寻找瓶颈点进行优化。 | ||
|
||
**Note**: 目前Paddle的性能分析工具主要还只提供时间方面的分析,之后会提供更多信息的收集来辅助做更全面的分析,如提供显存分析来监控显存泄漏问题。此外,Paddle的可视化工具VisualDL正在对Profiler的数据展示进行开发,敬请期待。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一点建议,在使用案例方面,按步骤来写可能更清晰一些,而且也需要让用户知道计时功能和分析功能的使用场景。可以根据步骤说明一步步跟着操作。比如:
- 第一步:模型训练过程中,使用Profiler工具计时功能,统计性能数据。(下面的内容展示示例代码,特别注意要设置timer_only=True),打印出模型的step_info和摘要数据。
- 第二步:将timer_only设置为False,在计时的同时对模型进行性能分析。(可以不再重复写示例代码了,直接给出打印的性能表单,给出Profiler分析的结论,同时需要说明在开启了timer_only=False时,由于有额外开销,benchmark计时的ips会偏低,因此如果不需要性能分析时,注意设置timer_only=True)
- 第三步:根据分析结论对模型进行调优,并查看Profiler数据确认提升效果。(给出调整了num_workers后的数据,reader占比下降的说明)
- 第四步:设置timer_only=True,确认最终的模型性能。(相比第一步时,ips提升了多大比例)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改,没有说冗余的提在性能测试前后都单独测一遍benchmark, 因为修改前后开启profiler影响是相同的,有提升的话在profiler的数据下也能体现。但是添加了profiler对程序的影响和单独使用benchmark的情景说明。
@@ -0,0 +1,425 @@ | |||
# 模型性能分析 | |||
Paddle profiler模块是paddle框架自带的低开销性能分析器,辅助用户对模型的运行性能进行调试。 | |||
用户可以通过性能分析器提供的对性能数据进行收集、统计和展示的功能,来对程序的执行瓶颈进行判断分析,识别造成程序运行时间过长或者GPU利用率低的原因,并寻求优化方案来获得性能的提升。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段描述有些部分感觉有点不通顺和重复。以及Paddle应该是一直首字母大写?一点建议:
Paddle Profiler模块是Paddle框架自带的低开销性能分析器,可以对模型的性能数据进行收集、统计和展示。性能分析器提供的数据可以帮助我们定位模型的瓶颈,识别造成程序运行时间过长或者GPU利用率低的原因,从而寻求优化方案来获得性能的提升。
shuffle=True, | ||
batch_size=batch_size, | ||
num_workers=4) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第3步,优化程序后,是不是就可以相应的展示优化后的Profiler数据,看瓶颈是否去除。不然这一步的内容也会显得比较单薄
num_workers=4) | ||
``` | ||
|
||
### 4. 再次进行性能分析,检查优化效果 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在模型分析中,我们的步骤通常是:
第1步,只做模型ips的统计,给出baseline数据(timer_only=True)
第2步,性能分析,找瓶颈(timer_only=False)
第3步,调优,再性能分析确认瓶颈是否去除
第4步,统计调优后的ips,与baseline比较,看提升的幅度。(timer_only=True)
- 优化程序,检查优化效果。 | ||
- 获取优化后模型正常运行时的ips,和baseline比较,确定真实的提升幅度。 | ||
|
||
我们以一个比较简单的示例,来看性能分析工具是如何通过上述四个步骤在调试程序性能中发挥作用。下面是Paddle的应用实践教学中关于[使用神经网络对cifar10进行分类](https://www.paddlepaddle.org.cn/documentation/docs/zh/practices/cv/convnet_image_classification.html)的示例代码,我们加上了启动性能分析的代码。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
链接用相对路径,比较容易维护
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
valid_loader = paddle.io.DataLoader(cifar10_test, batch_size=batch_size) | ||
|
||
# 创建性能分析器相关的代码 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里注意下代码段落,更清楚一点,比如42、44前面加个空行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
valid_loader = paddle.io.DataLoader(cifar10_test, batch_size=batch_size) | ||
|
||
# 创建性能分析器相关的代码 | ||
def my_on_trace_ready(prof): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
38 - 43 涉及 profiler的代码,最好逐行解释功能,增加易理解性,目前看的一脸懵
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| batch_cost | 0.00986 | 0.00798 | 0.00786 | | ||
| ips | 101.41524 | 127.25977 | 125.29320 | | ||
``` | ||
其中ReaderRatio表示数据读取部分占batch迭代过程的时间占比,reader_cost代表数据读取时间,batch_cost代表batch迭代的时间,ips表示每秒能迭代多少次,即跑多少个batch。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句话要放在第一次出现的地方,这个解释太靠后了,第一次出现的时候,就要对应解释了
# 模型性能分析 | ||
Paddle Profiler是Paddle框架自带的低开销性能分析器,可以对模型运行过程的性能数据进行收集、统计和展示。性能分析器提供的数据可以帮助我们定位模型的瓶颈,识别造成程序运行时间过长或者GPU利用率低的原因,从而寻求优化方案来获得性能的提升。 | ||
|
||
在这篇文档中,我们主要介绍如何使用Profiler工具来调试程序性能,以及阐述当前提供的所有功能特性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文中不用第一人称我/我们,一般省略主语,无法省略时,用第二人称你。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| batch_cost | 0.02555 | 0.02381 | 0.02220 | | ||
| ips | 39.13907 | 45.03588 | 41.99930 | | ||
``` | ||
可以看到,此时的ips为39.1,可将这个值作为优化对比的baseline。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
关于 Reader Ratio 等的解释要放在这里
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
### 2. 开启性能分析器,定位性能瓶颈点 | ||
|
||
修改程序,将Profiler的timer_only参数设置为False, 此时代表不只开启benchmark功能,还将开启性能分析器,进行详细的性能分析。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
False, -> False,
统一用中文标点
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
p = profiler.Profiler(scheduler = [3,14], on_trace_ready=my_on_trace_ready, timer_only=False) | ||
``` | ||
|
||
性能分析器会收集程序在第3到14次(不包括14)训练迭代过程中的性能数据,并在profiler_demo文件夹中输出一个json格式的文件,用于展示程序执行过程的timeline,可通过chrome浏览器的[chrome://tracing](chrome://tracing)插件打开这个文件进行观察。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
观察? -> 查看
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Backward 11 21.23 / 1.93 / 2.61 / 1.70 / 7.21 8.14 / 0.74 / 0.74 / 0.74 / 61.51 | ||
Optimization 11 34.74 / 3.16 / 3.65 / 2.41 / 11.79 0.67 / 0.06 / 0.06 / 0.06 / 5.03 | ||
Others - 45.66 / - / - / - / 15.50 0.53 / - / - / - / 3.96 | ||
--------------- ------ ---------------------------------------- ---------------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同样 要解释一下上图中各种名词的概念
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
--------------- ------ ---------------------------------------- ---------------------------------------- | ||
``` | ||
|
||
通过timeline可以看到,dataloader占了执行过程的很大比重,Model Summary显示其甚至接近了50%。分析程序发现,这是由于模型本身比较简单,需要的计算量小,再加上dataloader |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
怎么看出来的呢?用哪个指标
dataloader -> DataLoader 要和表中完全对应
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,449 @@ | |||
# 模型性能分析 | |||
Paddle Profiler是Paddle框架自带的低开销性能分析器,可以对模型运行过程的性能数据进行收集、统计和展示。性能分析器提供的数据可以帮助定位模型的瓶颈,识别造成程序运行时间过长或者GPU利用率低的原因,从而寻求优化方案来获得性能的提升。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Paddle框架 -> 飞桨框架
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里要改一下
|
||
在这篇文档中,主要介绍如何使用Profiler工具来调试程序性能,以及阐述当前提供的所有功能特性。 | ||
|
||
## 内容 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里别用二级标题,直接删除吧,前面加上一句,主要内容如下:
就可以;目前结构不清楚
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
- [更多细节](#gengduoxijie) | ||
|
||
|
||
## 使用Profiler工具调试程序性能 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加上序号 一、
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
- 获取模型正常运行时的ips(iterations per second, 每秒的迭代次数),给出baseline数据。 | ||
- 开启性能分析器,定位性能瓶颈点。 | ||
- 优化程序,检查优化效果。 | ||
- 获取优化后模型正常运行时的ips,和baseline比较,确定真实的提升幅度。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确定真实的提升幅度。 -> 计算真实的提升幅度。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DOne
- 优化程序,检查优化效果。 | ||
- 获取优化后模型正常运行时的ips,和baseline比较,确定真实的提升幅度。 | ||
|
||
我们以一个比较简单的示例,来看性能分析工具是如何通过上述四个步骤在调试程序性能中发挥作用。下面是Paddle的应用实践教学中关于[使用神经网络对cifar10进行分类](../../practices/cv/convnet_image_classification.html)的示例代码,我们加上了启动性能分析的代码。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段话要去掉我们,重新写一下逻辑
@@ -0,0 +1,449 @@ | |||
# 模型性能分析 | |||
Paddle Profiler是Paddle框架自带的低开销性能分析器,可以对模型运行过程的性能数据进行收集、统计和展示。性能分析器提供的数据可以帮助定位模型的瓶颈,识别造成程序运行时间过长或者GPU利用率低的原因,从而寻求优化方案来获得性能的提升。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里要改一下
| batch_cost | 0.02555 | 0.02381 | 0.02220 | | ||
| ips | 39.13907 | 45.03588 | 41.99930 | | ||
``` | ||
其中ReaderRatio表示数据读取部分占训练batch迭代过程的时间占比,reader_cost代表数据读取时间,batch_cost代表batch迭代的时间,ips表示每秒能迭代多少次,即跑多少个batch。可以看到,此时的ips为39.1,可将这个值作为优化对比的baseline。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReaderRatio -> Reader Ratio 要保持一致
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
p = profiler.Profiler(scheduler = [3,14], on_trace_ready=my_on_trace_ready, timer_only=False) | ||
``` | ||
|
||
性能分析器会收集程序在第3到14次(不包括14)训练迭代过程中的性能数据,并在profiler_demo文件夹中输出一个json格式的文件,用于展示程序执行过程的timeline,可通过chrome浏览器的[chrome://tracing](chrome://tracing)插件打开这个文件进行查看。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里要想一下,如果不是 chrome,用户该怎么办~要给一个这种场景的解决办法
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以后会开发VisualDl前端工具展示,目前和竞品一样,都用chrome的插件看
Name Calls CPU Total / Avg / Max / Min / Ratio(%) GPU Total / Avg / Max / Min / Ratio(%) | ||
--------------- ------ ---------------------------------------- ---------------------------------------- | ||
ProfileStep 11 294.53 / 26.78 / 35.28 / 24.56 / 100.00 13.22 / 1.20 / 1.20 / 1.20 / 100.00 | ||
Dataloader 11 141.49 / 12.86 / 17.53 / 10.34 / 48.04 0.00 / 0.00 / 0.00 / 0.00 / 0.00 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里建议修改输出为 DataLoader,和我们的API名字一致,否则会很奇怪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个得之后修改代码了,这些是代码里输出来的
--------------- ------ ---------------------------------------- ---------------------------------------- | ||
``` | ||
其中ProfileStep表示训练batch的迭代step过程,对应代码中每两次调用`p.step()`的间隔时间;Dataloader表示数据读取的时间,即`for batch_id, data in enumerate(train_loader())`的执行时间;Forward表示模型前向的时间,即`logits = model(x_data)`的执行时间,Backward表示反向传播的时间,即`loss.backward()`的执行时间;Optimization表示优化器的时间,即`opt.step()`的执行时间。 | ||
通过timeline可以看到,Dataloader占了执行过程的很大比重,Model Summary显示其甚至接近了50%。分析程序发现,这是由于模型本身比较简单,需要的计算量小,再加上dataloader |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataloader -> Dataloader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
### 3. 优化程序,检查优化效果 | ||
|
||
识别到了问题产生的原因,对程序继续做如下修改,将dataloader的num_workers设置为4,使得能有多个进程并行读取数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataloader -> Dataloader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
当前Profiler提供Timeline、统计表单、benchmark信息共三个方面的展示功能。 | ||
|
||
### Timeline展示 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也加上小标题吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* add profiling model doc * fix doc * fix * fix ref * fix number * fix * fix link * fix format * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix * fix according to review * fix according to review * fix according to review
* add profiling model doc * fix doc * fix * fix ref * fix number * fix * fix link * fix format * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix according to review * fix * fix according to review * fix according to review * fix according to review
加入Profiler的Tutorial文档。