Pytorch如何分析部分程序的占用时间
在进行pytorch程序代码的测试时,由于功能的不同,我们往往需要分模块进行, 但是最后给的时间却是一个程序跑下来所需要的时间,如果我们要观察部分程序执行的时间以及占比,这时候,我们只要在函数的前面加上两行代码即可,
with torch.autograd.profiler.profile() as prof: print(prof.key_averages().table(sort_by="self_cpu_time_total"))
如下:
x = torch.randn((1, 1), requires_grad=True)
with torch.autograd.profiler.profile() as prof:
for _ in range(200):
y =x**2
y.backward()
print(prof.key_averages().table(sort_by="self_cpu_time_total"))
~
将会输出以下结果: