如何在没有上下文管理器的情况下使用 torch.profiler.profile?

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

pytorch autograd profiler 文档中,它说 profiler 是一个“管理 autograd profiler 状态并保存结果摘要的上下文管理器”。然而,在文档的不同部分它演示了非上下文管理器启动/停止,它说它也受支持。然而,在 torch 1.9.0 中,这个启动/停止选项似乎已被删除:

from torch.profiler import profile
prof = profile()
prof.start()
# --> AttributeError: 'profile' object has no attribute 'start'

我已经研究了step(),但这也不起作用(它不会初始化探查器)。

用例是我想分析训练运行,而不需要编辑实际调用训练脚本的代码:我可以访问之前和之后的状态,但不能访问确切的训练脚本。
这可能吗?

python pytorch profiler torchvision autograd
2个回答
0
投票

这取决于您使用的 pytorch 版本。您需要为您的 pytorch 版本选择正确的文档。老版本好像没有

start()
成员函数。

检查两者之间的区别:

1.13版本 https://pytorch.org/docs/1.13/_modules/torch/profiler/profiler.html#profile

版本1.8.1 https://pytorch.org/docs/1.8.1/_modules/torch/profiler/profiler.html#profile


0
投票

我使用的是2.2.1版本,可以这样做:

profiler = torch.profiler.profile(
    profile_memory=True)
profiler.start()

...train you model...

profiler.stop()

尝试更新您的火炬版本,看看是否有效

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