快速鍵

Event

class torch.Event(device, *, enable_timing)

查詢並記錄 Stream 的狀態,以識別或控制 Stream 之間的依賴關係並測量時間。

參數
  • device (torch.device, optional) – Event 期望使用的裝置。如果未給定,則將使用目前的 加速器 類型。

  • enable_timing (bool, optional) – 指示 event 是否應該測量時間 (預設值:False)。

傳回

一個 torch.Event 物件。

傳回類型

Event

範例

>>> e_cuda = torch.Event(device='cuda')
elapsed_time(end_event) float

傳回此 event 與 end_event 各自透過 torch.Stream.record_event() 記錄時之間經過的時間 (以毫秒為單位)。

參數

end_event (torch.Event) – 已記錄的結束 event。

傳回

開始和結束 event 之間的時間,以毫秒為單位。

傳回類型

float

範例

>>> s_cuda = torch.Stream(device='cuda')
>>> e1_cuda = s_cuda.record_event()
>>> e2_cuda = s_cuda.record_event()
>>> ms = e1_cuda.elapsed_time(e2_cuda)
query() bool

檢查記錄此 event 的 stream 是否已通過記錄 event 的點。如果未記錄 Event,則始終傳回 True

傳回

一個布林值,表示 event 目前捕獲的所有工作是否已完成。

傳回類型

bool

範例

>>> s_cuda = torch.Stream(device='cuda')
>>> e_cuda = s_cuda.record_event()
>>> e_cuda.query()
True
record(stream) None

在給定的 stream 中記錄 event。stream 的裝置必須與 event 的裝置相符。此函式等效於 stream.record_event(self)

參數
  • stream (torch.Stream, optional) – 要記錄的 stream。

  • given (如果沒有) –

  • used. (將使用目前的 stream) –

範例

>>> e_cuda = torch.Event(device='cuda')
>>> e_cuda.record()
synchronize() None

等待 event 完成。這可防止 CPU 執行緒在 event 完成之前繼續執行。

範例

>>> s_cuda = torch.Stream(device='cuda')
>>> e_cuda = s_cuda.record_event()
>>> e_cuda.synchronize()
wait(stream) None

使提交到給定 stream 的所有未來工作等待此 event。

參數
  • stream (torch.Stream, optional) – 要同步的 stream。

  • given (如果沒有) –

  • used. (將使用目前的 stream) –

範例

>>> s1_cuda = torch.Stream(device='cuda')
>>> s2_cuda = torch.Stream(device='cuda')
>>> e_cuda = s1_cuda.record()
>>> e_cuda.wait(s2)

文件

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