分析您的 PyTorch 模块

原文:https://pytorch.org/tutorials/beginner/profiler.html

作者: Suraj Subramanian

PyTorch 包含一个探查器 API,可用于识别代码中各种 PyTorch 操作的时间和内存成本。 Profiler 可以轻松集成到您的代码中,结果可以打印为表格或在 JSON 跟踪文件中显示。

注意

Profiler 支持多线程模型。 Profiler 与该操作在同一线程中运行,但它还将对可能在另一个线程中运行的子运算符进行概要分析。 同时运行的探查器的作用域将限制在其自己的线程中,以防止结果混淆。

转到此秘籍,可以更快地了解 Profiler API 的用法。


  1. import torch
  2. import numpy as np
  3. from torch import nn
  4. import torch.autograd.profiler as profiler

使用 Profiler 的性能调试

Profiler 有助于识别模型中的性能瓶颈。 在此示例中,我们构建了一个自定义模块,该模块执行两个子任务:

  • 输入的线性变换,以及
  • 使用转换结果来获取遮罩张量上的索引。

我们使用profiler.record_function("label")将每个子任务的代码包装在单独的带标签的上下文管理器中。 在事件探查器输出中,子任务中所有操作的综合性能指标将显示在其相应的标签下。

请注意,使用 Profiler 会产生一些开销,并且最好仅用于调查代码。 如果要对运行时进行基准测试,请记住将其删除。

  1. class MyModule(nn.Module):
  2. def __init__(self, in_features: int, out_features: int, bias: bool = True):
  3. super(MyModule, self).__init__()
  4. self.linear = nn.Linear(in_features, out_features, bias)
  5. def forward(self, input, mask):
  6. with profiler.record_function("LINEAR PASS"):
  7. out = self.linear(input)
  8. with profiler.record_function("MASK INDICES"):
  9. threshold = out.sum(axis=1).mean().item()
  10. hi_idx = np.argwhere(mask.cpu().numpy() > threshold)
  11. hi_idx = torch.from_numpy(hi_idx).cuda()
  12. return out, hi_idx

分析正向传播

我们初始化随机输入和蒙版张量以及模型。

在运行探查器之前,我们需要对 CUDA 进行预热,以确保进行准确的性能基准测试。 我们将模块的正向传播包装在profiler.profile上下文管理器中。 with_stack=True参数在跟踪中附加操作的文件和行号。

警告

with_stack=True会产生额外的开销,并且更适合于研究代码。 如果要对性能进行基准测试,请记住将其删除。

  1. model = MyModule(500, 10).cuda()
  2. input = torch.rand(128, 500).cuda()
  3. mask = torch.rand((500, 500, 500), dtype=torch.double).cuda()
  4. # warm-up
  5. model(input, mask)
  6. with profiler.profile(with_stack=True, profile_memory=True) as prof:
  7. out, idx = model(input, mask)

打印分析器结果

最后,我们打印分析器结果。 profiler.key_averages通过运算符名称,以及可选地通过输入形状和/或栈跟踪事件来聚合结果。 按输入形状分组有助于识别模型使用哪些张量形状。

在这里,我们使用group_by_stack_n=5通过操作及其回溯(截断为最近的 5 个事件)聚合运行时,并按事件注册的顺序显示事件。 还可以通过传递sort_by参数对表进行排序(有关有效的排序键,请参阅文档)。

注意

在笔记本中运行 Profiler 时,您可能会在栈跟踪中看到<ipython-input-18-193a910735e8>(13): forward之类的条目,而不是文件名。 这些对应于<notebook-cell>(line number): calling-function

  1. print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))
  2. """
  3. (Some columns are omitted)
  4. ------------- ------------ ------------ ------------ ---------------------------------
  5. Name Self CPU % Self CPU Self CPU Mem Source Location
  6. ------------- ------------ ------------ ------------ ---------------------------------
  7. MASK INDICES 87.88% 5.212s -953.67 Mb /mnt/xarfuse/.../torch/au
  8. <ipython-input-...>(10): forward
  9. /mnt/xarfuse/.../torch/nn
  10. <ipython-input-...>(9): <module>
  11. /mnt/xarfuse/.../IPython/
  12. aten::copy_ 12.07% 715.848ms 0 b <ipython-input-...>(12): forward
  13. /mnt/xarfuse/.../torch/nn
  14. <ipython-input-...>(9): <module>
  15. /mnt/xarfuse/.../IPython/
  16. /mnt/xarfuse/.../IPython/
  17. LINEAR PASS 0.01% 350.151us -20 b /mnt/xarfuse/.../torch/au
  18. <ipython-input-...>(7): forward
  19. /mnt/xarfuse/.../torch/nn
  20. <ipython-input-...>(9): <module>
  21. /mnt/xarfuse/.../IPython/
  22. aten::addmm 0.00% 293.342us 0 b /mnt/xarfuse/.../torch/nn
  23. /mnt/xarfuse/.../torch/nn
  24. /mnt/xarfuse/.../torch/nn
  25. <ipython-input-...>(8): forward
  26. /mnt/xarfuse/.../torch/nn
  27. aten::mean 0.00% 235.095us 0 b <ipython-input-...>(11): forward
  28. /mnt/xarfuse/.../torch/nn
  29. <ipython-input-...>(9): <module>
  30. /mnt/xarfuse/.../IPython/
  31. /mnt/xarfuse/.../IPython/
  32. ----------------------------- ------------ ---------- ----------------------------------
  33. Self CPU time total: 5.931s
  34. """

提高内存性能

请注意,就内存和时间而言,最昂贵的操作位于forward (10),代表掩码索引中的操作。 让我们尝试先解决内存消耗问题。 我们可以看到第 12 行的.to()操作消耗 953.67 Mb。 该操作将mask复制到 CPU。 mask使用torch.double数据类型初始化。 我们可以通过将其转换为torch.float来减少内存占用吗?

  1. model = MyModule(500, 10).cuda()
  2. input = torch.rand(128, 500).cuda()
  3. mask = torch.rand((500, 500, 500), dtype=torch.float).cuda()
  4. # warm-up
  5. model(input, mask)
  6. with profiler.profile(with_stack=True, profile_memory=True) as prof:
  7. out, idx = model(input, mask)
  8. print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))
  9. """
  10. (Some columns are omitted)
  11. ----------------- ------------ ------------ ------------ --------------------------------
  12. Name Self CPU % Self CPU Self CPU Mem Source Location
  13. ----------------- ------------ ------------ ------------ --------------------------------
  14. MASK INDICES 93.61% 5.006s -476.84 Mb /mnt/xarfuse/.../torch/au
  15. <ipython-input-...>(10): forward
  16. /mnt/xarfuse/ /torch/nn
  17. <ipython-input-...>(9): <module>
  18. /mnt/xarfuse/.../IPython/
  19. aten::copy_ 6.34% 338.759ms 0 b <ipython-input-...>(12): forward
  20. /mnt/xarfuse/.../torch/nn
  21. <ipython-input-...>(9): <module>
  22. /mnt/xarfuse/.../IPython/
  23. /mnt/xarfuse/.../IPython/
  24. aten::as_strided 0.01% 281.808us 0 b <ipython-input-...>(11): forward
  25. /mnt/xarfuse/.../torch/nn
  26. <ipython-input-...>(9): <module>
  27. /mnt/xarfuse/.../IPython/
  28. /mnt/xarfuse/.../IPython/
  29. aten::addmm 0.01% 275.721us 0 b /mnt/xarfuse/.../torch/nn
  30. /mnt/xarfuse/.../torch/nn
  31. /mnt/xarfuse/.../torch/nn
  32. <ipython-input-...>(8): forward
  33. /mnt/xarfuse/.../torch/nn
  34. aten::_local 0.01% 268.650us 0 b <ipython-input-...>(11): forward
  35. _scalar_dense /mnt/xarfuse/.../torch/nn
  36. <ipython-input-...>(9): <module>
  37. /mnt/xarfuse/.../IPython/
  38. /mnt/xarfuse/.../IPython/
  39. ----------------- ------------ ------------ ------------ --------------------------------
  40. Self CPU time total: 5.347s
  41. """

此操作的 CPU 内存占用量减少了一半。

提高时间表现

虽然所消耗的时间也有所减少,但仍然太高。 原来,将矩阵从 CUDA 复制到 CPU 非常昂贵! forward (12)中的aten::copy_运算符将mask复制到 CPU,以便可以使用 NumPy argwhere函数。 forward(13)处的aten::copy_将数组作为张量复制回 CUDA。 如果我们在这里使用torch函数nonzero(),则可以消除这两个方面。

  1. class MyModule(nn.Module):
  2. def __init__(self, in_features: int, out_features: int, bias: bool = True):
  3. super(MyModule, self).__init__()
  4. self.linear = nn.Linear(in_features, out_features, bias)
  5. def forward(self, input, mask):
  6. with profiler.record_function("LINEAR PASS"):
  7. out = self.linear(input)
  8. with profiler.record_function("MASK INDICES"):
  9. threshold = out.sum(axis=1).mean()
  10. hi_idx = (mask > threshold).nonzero(as_tuple=True)
  11. return out, hi_idx
  12. model = MyModule(500, 10).cuda()
  13. input = torch.rand(128, 500).cuda()
  14. mask = torch.rand((500, 500, 500), dtype=torch.float).cuda()
  15. # warm-up
  16. model(input, mask)
  17. with profiler.profile(with_stack=True, profile_memory=True) as prof:
  18. out, idx = model(input, mask)
  19. print(prof.key_averages(group_by_stack_n=5).table(sort_by='self_cpu_time_total', row_limit=5))
  20. """
  21. (Some columns are omitted)
  22. -------------- ------------ ------------ ------------ ---------------------------------
  23. Name Self CPU % Self CPU Self CPU Mem Source Location
  24. -------------- ------------ ------------ ------------ ---------------------------------
  25. aten::gt 57.17% 129.089ms 0 b <ipython-input-...>(12): forward
  26. /mnt/xarfuse/.../torch/nn
  27. <ipython-input-...>(25): <module>
  28. /mnt/xarfuse/.../IPython/
  29. /mnt/xarfuse/.../IPython/
  30. aten::nonzero 37.38% 84.402ms 0 b <ipython-input-...>(12): forward
  31. /mnt/xarfuse/.../torch/nn
  32. <ipython-input-...>(25): <module>
  33. /mnt/xarfuse/.../IPython/
  34. /mnt/xarfuse/.../IPython/
  35. INDEX SCORE 3.32% 7.491ms -119.21 Mb /mnt/xarfuse/.../torch/au
  36. <ipython-input-...>(10): forward
  37. /mnt/xarfuse/.../torch/nn
  38. <ipython-input-...>(25): <module>
  39. /mnt/xarfuse/.../IPython/
  40. aten::as_strided 0.20% 441.587us 0 b <ipython-input-...>(12): forward
  41. /mnt/xarfuse/.../torch/nn
  42. <ipython-input-...>(25): <module>
  43. /mnt/xarfuse/.../IPython/
  44. /mnt/xarfuse/.../IPython/
  45. aten::nonzero
  46. _numpy 0.18% 395.602us 0 b <ipython-input-...>(12): forward
  47. /mnt/xarfuse/.../torch/nn
  48. <ipython-input-...>(25): <module>
  49. /mnt/xarfuse/.../IPython/
  50. /mnt/xarfuse/.../IPython/
  51. -------------- ------------ ------------ ------------ ---------------------------------
  52. Self CPU time total: 225.801ms
  53. """

进一步阅读

我们已经看到了 Profiler 如何用于调查 PyTorch 模型中的时间和内存瓶颈。 在此处阅读有关 Profiler 的更多信息:

脚本的总运行时间:(0 分钟 0.000 秒)

下载 Python 源码:profiler.py

下载 Jupyter 笔记本:profiler.ipynb

由 Sphinx 画廊生成的画廊