记录函数 ¶
- 类 torch.autograd.profiler.record_function(name, args=None)[source][source] ¶
自动微分分析器运行时,为代码块/函数添加标签的上下文管理器/函数装饰器。标签仅在启用 CPU 活动跟踪时显示。
追踪代码性能时很有用。
- 参数:
name (str) – 分配给代码块的标签。
node_id (int) – 节点 ID,用于分布式分析。非分布式(non-distributed)情况下未设置。
情况。(非分布式)
示例
>>> 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"): # label the block ... z = y ** 3 ... y.backward() ... >>> # NOTE: some columns were removed for brevity >>> print(prof.key_averages().table(sort_by="self_cpu_time_total")) ----------------------------------- --------------- --------------- --------------- Name Self CPU total % CPU time avg Number of Calls ----------------------------------- --------------- --------------- --------------- 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 ----------------------------------- --------------- --------------- --------------- Self CPU time total: 234.344us CUDA time total: 0.000us