Event¶
- class torch.Event(device, *, enable_timing)¶
查詢和記錄 Stream 狀態,以識別或控制跨 Stream 的依賴關係並測量時間。
- 引數
device (
torch.device, 可選) – Event 所需的裝置。如果未指定,將使用當前 accelerator 型別。enable_timing (bool, 可選) – 指示事件是否應測量時間(預設值:
False)。
- 返回
一個 torch.Event 物件。
- 返回型別
示例
>>> e_cuda = torch.Event(device='cuda')
- elapsed_time(end_event) float¶
返回此事件與
end_event分別透過torch.Stream.record_event()記錄之間經過的時間(以毫秒為單位)。- 引數
end_event (
torch.Event) – 已被記錄的結束事件。- 返回
開始事件與結束事件之間的時間(以毫秒為單位)。
- 返回型別
示例
>>> 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¶
檢查記錄此事件的 Stream 是否已透過事件記錄點。如果 Event 未記錄,則始終返回
True。- 返回
一個布林值,指示事件當前捕獲的所有工作是否已完成。
- 返回型別
示例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.query() True
- record(stream) None¶
在給定的 stream 中記錄事件。stream 的裝置必須與事件的裝置匹配。此函式等同於
stream.record_event(self)。- 引數
stream (
torch.Stream, 可選) – 要記錄的 stream。如果未指定,
將使用當前 stream。
示例
>>> e_cuda = torch.Event(device='cuda') >>> e_cuda.record()
- synchronize() None¶
等待事件完成。這會阻止 CPU 執行緒繼續執行,直到事件完成。
示例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.synchronize()
- wait(stream) None¶
使提交給給定 stream 的所有未來工作等待此事件。
- 引數
stream (
torch.Stream, 可選) – 要同步的 stream。如果未指定,
將使用當前 stream。
示例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record() >>> e_cuda.wait(s2)