【发布时间】:2021-02-25 11:24:03
【问题描述】:
我正在使用 PyTorch 分析一些代码。我知道 CUDA 通常有一些异步执行(请参阅PyTorch docs),但我相信从 GPU 传输到 CPU 通常会强制同步。
出于这个原因,我决定天真地使用cProfile 进行分析,但我注意到Profile.enable() ... Profile.disable() 报告的时间与time.time() 记录的时间不同(作为增量)。
以下是高级代码的外观:
gpu = torch.device("cuda")
cpu = torch.device("cpu")
setup = Setup()
net = make_fcn_resnet50(num_classes=setup.D)
net.eval().to(gpu)
rgb_tensor = setup.sample(device=cpu)
pr = profile.Profile()
pr.enable()
t_start = time.time()
rgb_tensor = rgb_tensor.to(gpu)
y = net(rgb_tensor)
dd_tensor = y["out"]
dd_mean = torch.mean(dd_tensor[[0]]).to(cpu).numpy()
assert dd_mean is not None
dt = time.time() - t_start
pr.disable()
stats = pstats.Stats(pr)
stats.print_stats(5)
print(f"dt: {dt:.4f}s")
这是我看到的差异:
2925 function calls (2734 primitive calls) in 0.009 seconds
...
dt: 0.0355s
我希望 cProfile 报告大约 35 毫秒(与 dt 相同),但它报告了大约 10 毫秒。
为什么会这样?
完整代码 + 复制在这里:
https://github.com/EricCousineau-TRI/repro/tree/bdef8a14/python/cprofile_with_torch
【问题讨论】:
标签: python pytorch python-3.6 ubuntu-18.04