记录函数¶
- class torch.autograd.profiler.record_function(name, args=None)[源代码]¶
上下文管理器/函数装饰器,在运行自动梯度分析器时为代码块/函数添加标签。
在追踪代码性能时非常有用。
示例
>>> x = torch.randn((1, 1), requires_grad=True) >>> with torch.autograd.profiler.profile() as prof: ... y = x ** 2 ... with torch.autograd.profiler.record_function("label-z"): # 标记块 ... z = y ** 3 ... y.backward() ... >>> # 注意:为了简洁,一些列被删除了 >>> print(prof.key_averages().table(sort_by="self_cpu_time_total")) ----------------------------------- --------------- --------------- --------------- 名称 自我CPU总时间% CPU时间平均值 调用次数 ----------------------------------- --------------- --------------- --------------- pow 60.77% 47.470us 3 mul 21.73% 25.465us 2 PowBackward0 12.03% 121.891us 1 torch::autograd::AccumulateGrad 2.70% 6.324us 1 label-z 2.13% 12.421us 1 torch::autograd::GraphRoot 0.64% 1.503us 1 ----------------------------------- --------------- --------------- --------------- 自我CPU总时间: 234.344us CUDA总时间: 0.000us