import sys
import os
import trace
tracer = trace.Trace(count=False, trace=True,ignoredirs=[sys.prefix, sys.exec_prefix, os.path.dirname(os.__file__)])
class TestModule(nn.Module):
def __init__(self):
super(TestModule,self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(16,8,3,1,1),
nn.ReLU(inplace=False)
)
self.layer2 = nn.Sequential(
nn.Linear(8192,10)
)
def __del__(self):
print("销毁中")
def forward(self,x):
x = self.layer1(x)
x = x.reshape(1, -1)
x = self.layer2(x)
return x
model = TestModule()
model.train()
register_module_trace(model,flush=True)
s = torch.from_numpy(np.random.rand(1, 16, 32, 32)).to(torch.float32)
out = model(s)
out = out.sum()
out.backward()
# class Runner():
# def fun(self):
model = TestModule()
model.train()
register_module_trace(model)
s = torch.from_numpy(np.random.rand(1, 16, 32, 32)).to(torch.float32)
out = model(s)
out = out.sum()
tracer.run('out.backward()')
tracer.run('out.backward()'), 通过改行命令来让执行测试程序,最后会逐行打印出运行日志,直到最终卡主的位置。
对于一些无法找出运行逻辑的过程,可以借助该方法查看最终代码运行到了什么位置。