torch.profiler¶
概述¶
PyTorch Profiler 是一种工具,允许在训练和推理期间收集性能指标。 Profiler 的上下文管理器 API 可用于更好地了解哪些模型运算符最昂贵。 检查它们的输入形状和堆栈跟踪,研究设备内核活动并可视化执行跟踪。
API 参考¶
- 类 torch.profiler 中。_KinetoProfile(*, activities=None, record_shapes=False, profile_memory=False, with_stack=False, with_flops=False, with_modules=False, experimental_config=None)[来源]¶
低级分析器包装 autograd 配置文件
- 参数
activities (iterable) – 用于分析的活动组 (CPU、CUDA) 列表,支持的值: , . 默认值:ProfilerActivity.CPU 和 ProfilerActivity.CUDA(如果可用)。
torch.profiler.ProfilerActivity.CPU
torch.profiler.ProfilerActivity.CUDA
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 模型。
experimental_config (_ExperimentalConfig) – 一组实验性选项 由 Kineto 等分析器库使用。请注意,不能保证向后兼容性。
注意
此 API 是实验性的,将来可能会更改。
启用 shape 和 stack 跟踪会导致额外的开销。 指定 record_shapes=True 时,分析器将暂时保存对张量的引用; 这可能会进一步阻止某些依赖于引用计数的优化,并引入 额外的 Tensor 副本。
- export_memory_timeline(path, device=None)[来源]¶
从收集的内存配置文件中提取内存信息 树,并导出一个由 [times, [sizes by category]]],其中 times 是时间戳和大小 是每个类别的内存使用情况。内存时间线图将 保存为 JSON(默认)或 gzip 压缩的 JSON。
输入:(文件路径、设备) 输出:以 JSON 或 gzip 压缩的 JSON 格式写入的文件
- 类 torch.profiler 中。profile(*, activities=无, schedule=无, on_trace_ready=无, record_shapes=False、profile_memory=False、with_stack=False、with_flops=False、with_modules=False、experimental_config=无,use_cuda=无)[来源]¶
Profiler 上下文管理器。
- 参数
activities (iterable) – 用于分析的活动组 (CPU、CUDA) 列表,支持的值: , . 默认值:ProfilerActivity.CPU 和 ProfilerActivity.CUDA(如果可用)。
torch.profiler.ProfilerActivity.CPU
torch.profiler.ProfilerActivity.CUDA
schedule (Callable) – 将步骤 (int) 作为单个参数并返回指定在每个步骤中要执行的分析器操作的值的可调用。
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 模型。
experimental_config (_ExperimentalConfig) – 一组实验性选项 用于 Kineto 库功能。请注意,不能保证向后兼容性。
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, repeat=1, # 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, repeat=1), 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()