From e64f75a1d8bec5547ba81463c1a14f0a437a8ad4 Mon Sep 17 00:00:00 2001 From: egg-west <708829300@qq.com> Date: Mon, 22 Oct 2018 13:56:25 -0700 Subject: [PATCH] fix ZeroDivisionError in utils.bottleneck (#11987) Summary: **ZeroDivisionError** occurs when `cuda_prof_exec_time` is small enough. This situation is normal for a project that has little CUDA work. Or someone does not make his work transferred to CUDA successfully. In this time he profiles the code, this error occurs. Pull Request resolved: https://github.com/pytorch/pytorch/pull/11987 Differential Revision: D10488568 Pulled By: soumith fbshipit-source-id: db8c1e9e88a00943c100958ebef41a1cb56e7e65 --- torch/utils/bottleneck/__main__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/torch/utils/bottleneck/__main__.py b/torch/utils/bottleneck/__main__.py index 31c40d4a145d27..b4661de7510c5c 100644 --- a/torch/utils/bottleneck/__main__.py +++ b/torch/utils/bottleneck/__main__.py @@ -222,10 +222,12 @@ def main(): # Print both the result of the CPU-mode and CUDA-mode autograd profilers # if their execution times are very different. cuda_prof_exec_time = cpu_time_total(autograd_prof_cuda) - cpu_prof_exec_time = cpu_time_total(autograd_prof_cpu) - pct_diff = cuda_prof_exec_time - cpu_prof_exec_time / cuda_prof_exec_time - if abs(pct_diff) > 0.05: - print_autograd_prof_summary(autograd_prof_cpu, 'CPU', autograd_prof_sortby, autograd_prof_topk) + if len(autograd_prof_cpu.function_events) > 0: + cpu_prof_exec_time = cpu_time_total(autograd_prof_cpu) + pct_diff = (cuda_prof_exec_time - cpu_prof_exec_time) / cuda_prof_exec_time + if abs(pct_diff) > 0.05: + print_autograd_prof_summary(autograd_prof_cpu, 'CPU', autograd_prof_sortby, autograd_prof_topk) + print_autograd_prof_summary(autograd_prof_cuda, 'CUDA', autograd_prof_sortby, autograd_prof_topk) if __name__ == '__main__':