torch.profiler

什么是torch.profiler

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

性能指标:例如内存使用、CPU 和 GPU 使用、操作时间等。

profiler.profile

torch.profiler.profile(*, activities=None, schedule=None, on_trace_ready=None, record_shapes=False, profile_memory=False, with_stack=False, with_flops=False, with_modules=False, experimental_config=None, use_cuda=None)

API 的参数如下:

  • activities:要使用的活动组列表。支持的值为 torch.profiler.ProfilerActivity.CPU 和 torch.profiler.ProfilerActivity.CUDA。默认值为 ProfilerActivity.CPU 和 (如果可用) ProfilerActivity.CUDA。
  • schedule:一个可调用对象,它以步数 (int) 作为单个参数,并返回 ProfilerAction 值,该值指定在每个步骤执行的 profiler 操作。
  • on_trace_ready:一个可调用对象,它在 schedule 在 profiling 期间返回 - ProfilerAction.RECORD_AND_SAVE 时,会在每个步骤被调用。
  • record_shapes:是否保存操作的输入形状信息。
  • profile_memory:是否跟踪张量内存分配/释放。
  • with_stack:是否记录操作的源信息 (文件和行号)。
  • with_flops:是否使用公式估计特定操作 (矩阵乘法和 2D 卷积) 的 FLOPs (浮点操作数)。
  • with_modules:是否记录操作的调用堆栈中对应的模块层次结构 (包括函数名)。例如,如果模块 A 的 forward 调用了模块 B 的 forward,其中包含一个 aten::add 操作,那么 aten::add 的模块层次结构为 A.B。请注意,此功能目前仅支持 TorchScript 模型,不支持 eager 模式模型。
  • experimental_config:Kineto 库功能使用的一组实验性选项。请注意,不保证向后兼容性。
  • use_cuda:是否使用 CUDA。如果为 None,则会根据可用性自动选择使用 CUDA 或 CPU。

示例

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))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值