• 教程 >
  • 使用 Instrumentation and Tracing Technology (ITT) API 分析 PyTorch 工作负载
快捷方式

使用 Instrumentation and Tracing Technology (ITT) API 分析 PyTorch 工作负载

创建于:2022 年 10 月 27 日 | 最后更新:2024 年 10 月 24 日 | 最后验证:未验证

在本食谱中,你将学习

  • 什么是 Intel® VTune™ Profiler

  • 什么是 Instrumentation and Tracing Technology (ITT) API

  • 如何在 Intel® VTune™ Profiler 中可视化 PyTorch 模型层级结构

  • 一个展示如何使用 PyTorch ITT API 的简短代码示例

要求

  • PyTorch 1.13 或更高版本

  • Intel® VTune™ Profiler

安装 PyTorch 的说明可在 pytorch.org 上找到。

什么是 Intel® VTune™ Profiler

Intel® VTune™ Profiler 是一款用于串行和多线程应用程序的性能分析工具。对于熟悉 Intel 架构的用户,Intel® VTune™ Profiler 提供了丰富的指标,帮助用户了解应用程序在 Intel 平台上的执行情况,从而了解性能瓶颈所在。

更多详细信息,包括入门指南,可在 Intel 网站 上找到。

什么是 Instrumentation and Tracing Technology (ITT) API

Intel® VTune™ Profiler 提供的 Instrumentation and Tracing Technology API (ITT API) 使目标应用程序能够在执行期间生成和控制跟踪数据的收集。

ITT 功能的优势在于在 Intel® VTune™ Profiler GUI 上标记单个 PyTorch 运算符以及自定义区域的时间跨度。当用户发现任何异常时,这将非常有助于定位哪个运算符行为异常。

注意

自 1.13 版本以来,ITT API 已集成到 PyTorch 中。用户无需调用原始的 ITT C/C++ API,只需调用 PyTorch 中的 Python API 即可。更多详细信息请参见 PyTorch 文档

如何在 Intel® VTune™ Profiler 中可视化 PyTorch 模型层级结构

PyTorch 中提供了两种使用类型

  1. 隐式调用:默认情况下,所有通过遵循 PyTorch 运算符注册机制注册的运算符,在其功能启用时都将自动通过 ITT 功能进行标记。

  2. 显式调用:如果需要自定义标记,用户可以使用 PyTorch 文档 中提到的 API 显式标记所需的范围。

要启用显式调用,预期被标记的代码应在 torch.autograd.profiler.emit_itt() 作用域下调用。例如

with torch.autograd.profiler.emit_itt():
  <code-to-be-profiled...>

启动 Intel® VTune™ Profiler

要验证功能,你需要启动一个 Intel® VTune™ Profiler 实例。请查看 Intel® VTune™ Profiler 用户指南,了解启动 Intel® VTune™ Profiler 的步骤。

注意

用户还可以按照 Intel® VTune™ Profiler Web Server UI 指南 使用 web-server-ui,例如:vtune-backend –web-port=8080 –allow-remote-access –enable-server-profiling

一旦你启动了 Intel® VTune™ Profiler GUI,你应该看到如下用户界面

../_images/vtune_start.png

左侧导航栏的 sample (matrix) 项目下提供了三个示例结果。如果你不希望分析结果出现在此默认示例项目中,你可以通过蓝色 Configure Analysis… 按钮下的 New Project… 按钮创建一个新项目。要开始新的分析,请单击蓝色 Configure Analysis… 按钮以启动分析配置。

配置 CPU 分析

单击 Configure Analysis… 按钮后,你应该看到以下屏幕

../_images/vtune_config.png

窗口的右侧分为 3 个部分:WHERE(左上)、WHAT(左下)和 HOW(右)。使用 WHERE,你可以分配要在其上运行分析的机器。使用 WHAT,你可以设置要分析的应用程序的路径。要分析 PyTorch 脚本,建议将所有手动步骤(包括激活 Python 环境和设置所需的环境变量)包装到一个 bash 脚本中,然后分析此 bash 脚本。在上面的屏幕截图中,我们将所有步骤包装到 launch.sh bash 脚本中,并分析 bash,参数为 <path_of_launch.sh>。在右侧的 HOW 中,你可以选择你想要分析的类型。Intel® VTune™ Profiler 提供了许多你可以选择的分析类型。详细信息请参见 Intel® VTune™ Profiler 用户指南

配置 XPU 分析

选择 GPU Offload Profiling Type 而不是 Hotspots,并按照与 CPU 相同的说明启动应用程序。

../_images/vtune_xpu_config.png

读取分析结果

通过使用 ITT 成功进行分析后,你可以打开分析结果的 Platform 选项卡,以在 Intel® VTune™ Profiler 时间线中查看标签。

../_images/vtune_timeline.png

时间线在顶部将主线程显示为 python 线程,并在下方显示各个 OpenMP 线程。标记的 PyTorch 运算符和自定义区域显示在主线程行中。所有以 aten:: 开头的运算符都是由 PyTorch 中的 ITT 功能隐式标记的运算符。标签 iteration_N 是使用特定 API torch.profiler.itt.range_push()torch.profiler.itt.range_pop()torch.profiler.itt.range() 作用域显式标记的。请查看下一节中的示例代码以了解详细信息。

注意

标有 convolutionreorder 的红色框来自 Intel® oneAPI Deep Neural Network Library (oneDNN)。

如右侧导航栏所示,时间线行中的棕色部分显示了各个线程的 CPU 使用率。线程行高度的百分比与棕色部分在时间戳处所占的百分比与该时间戳处该线程中的 CPU 使用率对齐。因此,从这个时间线可以直观地理解以下内容

  1. 每个线程上的 CPU 内核利用率如何。

  2. 所有线程上的 CPU 内核利用率是否平衡。所有线程都有良好的 CPU 使用率吗?

  3. OpenMP 线程的同步性如何。启动 OpenMP 线程或 OpenMP 线程完成时是否存在抖动。

当然,Intel® VTune™ Profiler 提供了更丰富的分析功能集,以帮助你理解性能问题。当你理解性能问题的根本原因时,你就可以修复它。更多详细的使用说明可在 Intel® VTune™ Profiler 用户指南 中找到。

读取 XPU 分析结果

通过使用 ITT 成功进行分析后,你可以打开分析结果的 Platform 选项卡,以在 Intel® VTune™ Profiler 时间线中查看标签。

../_images/vtune_xpu_timeline.png

时间线在顶部将主线程显示为 python 线程。标记的 PyTorch 运算符和自定义区域显示在主线程行中。所有以 aten:: 开头的运算符都是由 PyTorch 中的 ITT 功能隐式标记的运算符。时间线还在顶部显示了 GPU Computing Queue,用户可以看到分派到 GPU Queue 中的不同 XPU Kernels。

一个展示如何使用 PyTorch ITT API 的简短代码示例

下面的示例代码是用于上面屏幕截图中的分析的脚本。

拓扑由两个运算符 Conv2dLinear 组成。执行了三次推理迭代。每次迭代都由 PyTorch ITT API 标记为文本字符串 iteration_Ntorch.profile.itt.range_pushtorch.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

评价本教程

© Copyright 2024, PyTorch。

使用 Sphinx 构建,主题由 theme 提供,由 Read the Docs 托管。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源