您好,我正在尝试重现官方 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")
注意:我在裸码头图像上复制了它。
非常奇怪的是,当我从脚本中打印表格时,我可以看到痕迹。我给出了我为此使用的确切代码片段,我只是将它们放在上面的代码片段之后。
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 存储库上有一个
简而言之,torch 2.0.0 版本上存在有关分析器的错误。他们的例子更简单:他们尝试分析 2 个张量的加法。他们的调查与我的相同,最终得出相同的结论:分析器未填充堆栈,因为事件具有空的 stacks
当前修复是返回torch 1.13.0等待修复。
编辑
:请参阅 Ben 评论和 Github,要获得所有信息,我们应该添加experimental_config
。我个人对它的使用揭示了一些其他问题,特别是使用 HTA 的 Kineto 迹线。但这些问题不属于这篇 SO 帖子的范围。
感谢在 torch repo 上提出此问题的人,并感谢 torch 的维护者!