torch.profiler¶
概述¶
PyTorch Profiler 是一种工具,允许在训练和推理期间收集性能指标。 Profiler 的上下文管理器 API 可用于更好地了解哪些模型运算符最昂贵。 检查它们的输入形状和堆栈跟踪,研究设备内核活动并可视化执行跟踪。
API 参考¶
-
class (*, activities=None, schedule=None, on_trace_ready=None, record_shapes=False、profile_memory=False、with_stack=False、with_flops=False、with_modules=False、use_cuda=没有)[来源]
torch.profiler.
profile
¶ Profiler 上下文管理器。
- 参数
activities (iterable) – 用于分析的活动组 (CPU、CUDA) 列表,支持的值: , . 默认值:ProfilerActivity.CPU 和 ProfilerActivity.CUDA(如果可用)。
torch.profiler.ProfilerActivity.CPU
torch.profiler.ProfilerActivity.CUDA
schedule (callable) – 可调用,它将步骤 (int) 作为单个参数,并返回指定要在每个步骤中执行的 Profiler 操作的值。
ProfilerAction
on_trace_ready (callable) – 在分析期间返回时在每个步骤中调用的可调用。
schedule
ProfilerAction.RECORD_AND_SAVE
record_shapes (bool) – 保存有关运算符输入形状的信息。
profile_memory (bool) - 跟踪张量内存分配/释放。
with_stack (bool) – 记录运算的源信息 (文件和行号)。
with_flops (bool) – 使用公式估计特定运算符的 FLOPs (浮点运算) (矩阵乘法和 2D 卷积)。
with_modules (bool) – 记录模块层次结构(包括函数名称) 对应于 op 的 callstack。例如,如果模块 A 的 forward 调用的 模块 B 的 forward 包含一个 aten::add 操作, 那么 aten::add 的模块层次结构是 A.B 请注意,目前仅对 TorchScript 模型提供此支持 而不是 Eager Mode 模型。
use_cuda (布尔值) –
1.8.1 版后已移除: use instead.
activities
注意
on_trace_ready=torch.profiler.tensorboard_trace_handler(dir_name)
分析后,可以在指定的目录中找到结果文件。使用命令:
tensorboard --logdir dir_name
以查看 TensorBoard 中的结果。 有关更多信息,请参阅 PyTorch Profiler TensorBoard 插件
注意
启用 shape 和 stack 跟踪会导致额外的开销。 指定 record_shapes=True 时,分析器将暂时保存对张量的引用; 这可能会进一步阻止某些依赖于引用计数的优化,并引入 额外的 Tensor 副本。
例子:
with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ] ) as p: code_to_profile() print(p.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1))
使用 Profiler 的 和 函数:
schedule
on_trace_ready
step
# Non-default profiler schedule allows user to turn profiler on and off # on different iterations of the training loop; # trace_handler is called every time a new trace becomes available def trace_handler(prof): print(prof.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) # prof.export_chrome_trace("/tmp/test_trace_" + str(prof.step_num) + ".json") with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ], # In this example with wait=1, warmup=1, active=2, # profiler will skip the first step/iteration, # start warming up on the second, record # the third and the forth iterations, # after which the trace will become available # and on_trace_ready (when set) is called; # the cycle repeats starting with the next step schedule=torch.profiler.schedule( wait=1, warmup=1, active=2), on_trace_ready=trace_handler # on_trace_ready=torch.profiler.tensorboard_trace_handler('./log') # used when outputting for tensorboard ) as p: for iter in range(N): code_iteration_to_profile(iter) # send a signal to the profiler that the next iteration has started p.step()
-
export_stacks
(path, metric='self_cpu_time_total')[来源]¶ 将堆栈跟踪保存在适合可视化的格式的文件中。
注意
使用 FlameGraph 工具的示例:
cd FlameGraph
./flamegraph.pl –title “CPU time” –countname “us.” profiler.stacks > perf_viz.svg