目录

torch.profiler

概述

PyTorch Profiler 是一种工具,允许在训练和推理期间收集性能指标。 Profiler 的上下文管理器 API 可用于更好地了解哪些模型运算符最昂贵。 检查它们的输入形状和堆栈跟踪,研究设备内核活动并可视化执行跟踪。

注意

模块中 API 的早期版本被视为旧版,将被弃用。

API 参考

*activities=Nonerecord_shapes=Falseprofile_memory=Falsewith_stack=Falsewith_flops=Falsewith_modules=Falseexperimental_config=None[来源]torch.profiler._KinetoProfile

低级分析器包装 autograd 配置文件

参数
  • activitiesiterable) – 用于分析的活动组 (CPU、CUDA) 列表,支持的值: , . 默认值:ProfilerActivity.CPU 和 ProfilerActivity.CUDA(如果可用)。torch.profiler.ProfilerActivity.CPUtorch.profiler.ProfilerActivity.CUDA

  • record_shapesbool) – 保存有关运算符输入形状的信息。

  • profile_memorybool) - 跟踪张量内存分配/释放。

  • with_stackbool) – 记录运算的源信息 (文件和行号)。

  • with_flopsbool) – 使用公式估算特定运算符的 FLOPS (矩阵乘法和 2D 卷积)。

  • with_modulesbool) – 记录模块层次结构(包括函数名称) 对应于 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 副本。

add_metadata([来源]

添加具有字符串键和字符串值的用户定义元数据 到跟踪文件中

add_metadata_json([来源]

添加用户定义的元数据,其中包含字符串键和有效的 json 值 到跟踪文件中

events()[来源]

返回未聚合的 Profiler 事件列表, 在 trace 回调中使用或在性能分析完成后使用

export_chrome_trace(path[来源]

以 Chrome JSON 格式导出收集的跟踪记录。

export_stacks(pathmetric='self_cpu_time_total'[来源]

将堆栈跟踪保存在适合可视化的格式的文件中。

参数
  • pathstr) – 将堆栈文件保存到此位置;

  • metricstr) – 要使用的度量:“self_cpu_time_total”或“self_cuda_time_total”

注意

使用 FlameGraph 工具的示例:

key_averages(group_by_input_shape=Falsegroup_by_stack_n=0[来源]

对事件进行平均,按运算符名称和(可选)输入形状对事件进行分组,以及 叠。

注意

要使用形状/堆栈功能,请确保将 record_shapes/with_stack 创建 Profiler Context Manager 时。

class *activities=Noneschedule=Noneon_trace_ready=Nonerecord_shapes=Falseprofile_memory=Falsewith_stack=Falsewith_flops=Falsewith_modules=Falseexperimental_config=use_cuda=[来源]torch.profiler.profile

Profiler 上下文管理器。

参数
  • activitiesiterable) – 用于分析的活动组 (CPU、CUDA) 列表,支持的值: , . 默认值:ProfilerActivity.CPU 和 ProfilerActivity.CUDA(如果可用)。torch.profiler.ProfilerActivity.CPUtorch.profiler.ProfilerActivity.CUDA

  • schedulecallable) – 可调用,它将步骤 (int) 作为单个参数,并返回指定要在每个步骤中执行的 Profiler 操作的值。ProfilerAction

  • on_trace_readycallable) – 在分析期间返回时在每个步骤中调用的可调用。scheduleProfilerAction.RECORD_AND_SAVE

  • record_shapesbool) – 保存有关运算符输入形状的信息。

  • profile_memorybool) - 跟踪张量内存分配/释放。

  • with_stackbool) – 记录运算的源信息 (文件和行号)。

  • with_flopsbool) – 使用公式估计特定运算符的 FLOPs (浮点运算) (矩阵乘法和 2D 卷积)。

  • with_modulesbool) – 记录模块层次结构(包括函数名称) 对应于 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

注意

用于生成可调用的计划。 非默认计划在分析长时间训练作业时非常有用 并允许用户在不同迭代时获取多个跟踪 的训练过程。 默认计划只是连续记录 Duration 的上下文管理器。

注意

用于为 TensorBoard 生成结果文件:

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 的 和 函数:scheduleon_trace_readystep

# 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()
step()[来源]

向探查器发出下一个性能分析步骤已启动的信号。

class value[来源]torch.profiler.ProfilerAction

可以按指定间隔执行的 Profiler 操作

torch.profiler.ProfilerActivity

成员:

中央处理器

CUDA 的

财产 name
torch.profiler.schedule(*等待预热活动重复=0skip_first=0[来源]

返回可用作 profiler 参数的可调用对象。分析器将跳过 第一步,然后等待步骤,然后为后续步骤做热身, 然后为后续步骤进行活动录制,然后重复循环 从 步骤开始. 可选的循环数由参数指定,零值表示 这些循环将继续进行,直到性能分析完成。scheduleskip_firstwaitwarmupactivewaitrepeat

torch.profiler.tensorboard_trace_handler(dir_name,worker_name=use_gzip=False[来源]

将跟踪文件输出到 的目录 ,则该目录可以是 作为 logdir 直接交付到 TensorBoard。 对于分布式场景中的每个 worker 应该是唯一的, 默认情况下,它将设置为 '[hostname]_[pid]”。dir_nameworker_name

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源