使用仪器和跟踪技术(ITT)API分析PyTorch工作负载¶
Created On: Oct 27, 2022 | Last Updated: Oct 24, 2024 | Last Verified: Not Verified
在此教程中,您将学习:
什么是英特尔®VTune™分析器
什么是仪器和跟踪技术(ITT)API
如何在英特尔®VTune™分析器中可视化PyTorch模型层次结构
一个展示如何使用PyTorch ITT API的短代码示例
要求¶
PyTorch 1.13或更高版本
英特尔®VTune™分析器
PyTorch的安装说明可在`pytorch.org <https://pytorch.org/get-started/locally/>`__上找到。
什么是英特尔®VTune™分析器¶
英特尔®VTune™分析器是一个用于串行和多线程应用的性能分析工具。对于熟悉英特尔架构的开发者,英特尔®VTune™分析器提供了一套丰富的指标,帮助用户了解应用在英特尔平台上的执行情况,从而定位性能瓶颈。
更多详细信息,包括入门指南,请参考`英特尔官方网站 <https://www.intel.com/content/www/us/en/developer/tools/oneapi/vtune-profiler.html>`__。
什么是仪器和跟踪技术(ITT)API¶
`仪器和跟踪技术API(ITT API) <https://www.intel.com/content/www/us/en/develop/documentation/vtune-help/top/api-support/instrumentation-and-tracing-technology-apis.html>`_由英特尔®VTune™分析器提供,用于在目标应用执行过程中生成和控制跟踪数据的收集。
ITT功能的优势在于可以在英特尔®VTune™分析器GUI中标记单个PyTorch操作以及自定义区域的时间跨度。当用户发现任何异常时,这对于定位哪个操作行为异常非常有帮助。
备注
ITT API自PyTorch 1.13版本起已集成。用户无需调用原始的ITT C/C++ API,只需调用PyTorch中的Python API即可。更多详细信息可以参考`PyTorch文档 <https://pytorch.org/docs/stable/profiler.html#intel-instrumentation-and-tracing-technology-apis>`__。
如何在英特尔®VTune™分析器中可视化PyTorch模型层次结构¶
PyTorch提供了两种使用方法:
隐式调用:默认情况下,所有通过PyTorch操作注册机制注册的操作在启用ITT功能时都会被自动标记。
显式调用:如果需要自定义标记,用户可以在`PyTorch文档 <https://pytorch.org/docs/stable/profiler.html#intel-instrumentation-and-tracing-technology-apis>`__中提到的API中显式标记所需的范围。
要启用显式调用,预期被标记的代码应在`torch.autograd.profiler.emit_itt()`范围内调用。例如:
with torch.autograd.profiler.emit_itt():
<code-to-be-profiled...>
启动英特尔®VTune™分析器¶
要验证功能,需要启动一个英特尔®VTune™分析器实例。请查看`英特尔®VTune™分析器用户指南 <https://www.intel.com/content/www/us/en/develop/documentation/vtune-help/top/launch.html>`__了解启动英特尔®VTune™分析器的步骤。
备注
用户还可以通过`英特尔®VTune™分析器Web服务器UI指南 <https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2024-1/web-server-ui.html>`__使用web服务器UI,例如:vtune-backend –web-port=8080 –allow-remote-access –enable-server-profiling
一旦英特尔®VTune™分析器GUI启动,您应该会看到如下的用户界面:
左侧导航栏下的`sample (matrix)`项目中可以看到三个示例结果。如果您不希望分析结果出现在默认的示例项目中,可以通过蓝色的`配置分析…`按钮下的`新建项目…`按钮创建新项目。要开始新的分析,请点击蓝色的`配置分析…`按钮启动分析配置。
配置CPU性能分析¶
点击`配置分析…`按钮后,您应该看到以下界面:
窗口的右侧分为三部分:WHERE`(左上),`WHAT`(左下)和`HOW`(右侧)。通过`WHERE,您可以分配用于运行性能分析的机器。通过`WHAT`,您可以设置要分析的应用程序的路径。为了分析PyTorch脚本,建议将所有手动步骤(包括激活Python环境和设置所需的环境变量)封装到一个bash脚本中,然后分析此bash脚本。在上方的截图中,我们将所有步骤封装到`launch.sh` bash脚本中,并使用参数`<path_of_launch.sh>`分析`bash`。在右侧的`HOW`,您可以选择要分析的类型。Intel® VTune™ Profiler 提供了许多分析类型可供选择。可以在`Intel® VTune™ Profiler用户指南 <https://www.intel.com/content/www/us/en/develop/documentation/vtune-help/top/analyze-performance.html>`__找到详细信息。
读取性能分析结果¶
成功使用ITT分析后,您可以打开分析结果的`Platform`选项卡以在Intel® VTune™ Profiler时间线上查看标签。
时间线显示主线程为顶部的`python`线程,下面是单独的OpenMP线程。标记的PyTorch操作符和自定义区域显示在主线程的一行中。所有以`aten::开头的操作符都是由PyTorch中的ITT功能隐式标记的。标记为`iteration_N`的标签使用特定的API显式标记,例如`torch.profiler.itt.range_push(),`torch.profiler.itt.range_pop()`或`torch.profiler.itt.range()`作用域。有关详细信息,请查看下一节的示例代码。
备注
用红色框标注的`convolution`和`reorder`来自Intel® oneAPI深度神经网络库(oneDNN)的标签。
如右侧导航栏所示,时间线行中的棕色部分表示各线程的CPU使用情况。线程行高度中棕色部分在某个时间点的占比与该时间点线程的CPU使用率一致。因此,直观地来看这条时间线可以了解以下内容:
每个线程上的CPU核心利用率如何。
所有线程上的CPU核心利用是否均衡。所有线程的CPU使用情况是否良好?
OpenMP线程同步的情况如何。当OpenMP线程启动或结束时是否有抖动。
当然,Intel® VTune™ Profiler还提供了更丰富的性能分析功能,帮助您了解性能问题。当了解性能问题的根本原因后,您可以解决它。可以在`Intel® VTune™ Profiler用户指南 <https://www.intel.com/content/www/us/en/develop/documentation/vtune-help/top/analyze-performance.html>`__找到更多详细的使用说明。
一个展示如何使用PyTorch ITT API的短代码示例¶
以下示例代码是上方截图中用于性能分析的脚本。
拓扑由两个操作符`Conv2d`和`Linear`组成。进行了三次推理迭代。每次迭代都使用PyTorch ITT API标记为字符串`iteration_N`。`torch.profile.itt.range_push`和`torch.profile.itt.range_pop`或`torch.profile.itt.range`作用域都可以完成自定义标签功能。
# sample.py
import torch
import torch.nn as nn
class ITTSample(nn.Module):
def __init__(self):
super(ITTSample, self).__init__()
self.conv = nn.Conv2d(3, 5, 3)
self.linear = nn.Linear(292820, 1000)
def forward(self, x):
x = self.conv(x)
x = x.view(x.shape[0], -1)
x = self.linear(x)
return x
def main():
m = ITTSample
# unmark below code for XPU
# m = m.to("xpu")
x = torch.rand(10, 3, 244, 244)
# unmark below code for XPU
# x = x.to("xpu")
with torch.autograd.profiler.emit_itt():
for i in range(3)
# Labeling a region with pair of range_push and range_pop
#torch.profiler.itt.range_push(f'iteration_{i}')
#m(x)
#torch.profiler.itt.range_pop()
# Labeling a region with range scope
with torch.profiler.itt.range(f'iteration_{i}'):
m(x)
if __name__ == '__main__':
main()
截图中Intel® VTune™ Profiler GUI中提到的`launch.sh` bash脚本,用于封装所有手动步骤,见下文。
# launch.sh
#!/bin/bash
# Retrieve the directory path where the path contains both the sample.py and launch.sh so that this bash script can be invoked from any directory
BASEFOLDER=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
<Activate a Python environment>
cd ${BASEFOLDER}
python sample.py