火炬分析仪中的空堆栈

问题描述 投票:0回答:1

问题详情

您好,我正在尝试重现官方 Pytorch 教程的探查器示例。我想导出模型前向传递的堆栈。

尽管创建了堆栈文件,但它们是空的。

import torch
from torch import profiler
from torchvision.models import resnet18

model = resnet18().cuda()
inputs = torch.rand(5, 3, 224, 224).cuda()


with profiler.profile(
    activities=[profiler.ProfilerActivity.CPU,
                profiler.ProfilerActivity.CUDA],
    with_stack=True,
)as p:
    model(inputs)

p.export_stacks(
    f"/tmp/profiler/stacks_cpu.txt", "self_cpu_time_total")
p.export_stacks(
    f"/tmp/profiler/stacks_cuda.txt", "self_cuda_time_total")
环境信息
  • 在 Docker 镜像 pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime 上运行
  • 火炬:2.0.0
  • 火炬视觉:0.15.0
  • Python:3.10.9

注意:我在裸码头图像上复制了它。

我尝试了什么

非常奇怪的是,当我从脚本中打印表格时,我可以看到痕迹。我给出了我为此使用的确切代码片段,我只是将它们放在上面的代码片段之后。

print(p.key_averages(group_by_stack_n=5).table(
    sort_by="self_cuda_time_total", row_limit=2))

更新:打印选项也不起作用。它打印表格,但不打印堆栈。通过调试,我可以在模块

_build_table
中看到函数
torch.autograd.profiler_util
。在第 794 行,变量 stacks 是一个空列表。 (
_build_table
在上面代码片段中的
table
方法上被调用)。 另外,在
key_averages
类的
EventList
方法中 - 在
key_averages
类的
profiler
中调用(在代码片段中使用) - 每个事件在第 298 行
 都有一个空的 
stacks
 属性。
那么问题来了,为什么堆栈没有填满那些事件呢?我会进一步调查。

在 pytorch 存储库上有一个
python pytorch profiling
1个回答
5
投票
回答了我帖子的最后一个问题。我让您阅读该问题以了解更多详细信息。

简而言之,torch 2.0.0 版本上存在有关分析器的错误。他们的例子更简单:他们尝试分析 2 个张量的加法。他们的调查与我的相同,最终得出相同的结论:分析器未填充堆栈,因为事件具有空的 stacks

属性。他们的调查更加定位,因为他们比较了 torch 的两个版本(1.13.0 VS 2.0.0),发现事件数量不一样。探查器的跟踪是用 C++ 完成的,因此我无法进一步调查。

当前修复是返回torch 1.13.0等待修复。

编辑

:请参阅 Ben 评论和 Github,要获得所有信息,我们应该添加

experimental_config。我个人对它的使用揭示了一些其他问题,特别是使用 HTA 的 Kineto 迹线。但这些问题不属于这篇 SO 帖子的范围。

感谢在 torch repo 上提出此问题的人,并感谢 torch 的维护者!

© www.soinside.com 2019 - 2024. All rights reserved.