什么是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))