事件¶
- class torch.mtia.Event(device, *, enable_timing)¶
查詢並記錄 Stream 狀態,以識別或控制 Stream 之間的依賴關係並測量時間。
- 引數
device (
torch.device, 可選) – 事件所需的裝置。如果未給出,則使用當前 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 是否已越過記錄此事件的點。如果事件未記錄,則始終返回
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。如果未
使用。
示例
>>> 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。如果未
使用。
示例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record() >>> e_cuda.wait(s2)