捷徑

record_function

class torch.autograd.profiler.record_function(name, args=None)[source][source]

當執行 autograd 分析器時,這個上下文管理器/函式裝飾器會為程式碼區塊/函式添加標籤。 只有在啟用 CPU 活動追蹤時,才會顯示標籤。

在追蹤程式碼效能時,這非常有用。

參數
  • name (str) – 指定給程式碼區塊的標籤。

  • node_id (int) – 節點的 ID,用於分散式分析。 在以下情況下未設置

  • cases. (non-distributed) –

範例

>>> x = torch.randn((1, 1), requires_grad=True)
>>> with torch.autograd.profiler.profile() as prof:
...     y = x ** 2
...     with torch.autograd.profiler.record_function("label-z"): # label the block
...         z = y ** 3
...     y.backward()
...
>>> # NOTE: some columns were removed for brevity
>>> print(prof.key_averages().table(sort_by="self_cpu_time_total"))
-----------------------------------  ---------------  ---------------  ---------------
Name                                 Self CPU total %  CPU time avg     Number of Calls
-----------------------------------  ---------------  ---------------  ---------------
pow                                  60.77%           47.470us         3
mul                                  21.73%           25.465us         2
PowBackward0                         12.03%           121.891us        1
torch::autograd::AccumulateGrad      2.70%            6.324us          1
label-z                              2.13%            12.421us         1
torch::autograd::GraphRoot           0.64%            1.503us          1
-----------------------------------  ---------------  ---------------  ---------------
Self CPU time total: 234.344us
CUDA time total: 0.000us

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources