流#
- class torch.Stream(device, *, priority)#
一個按先入先出 (FIFO) 順序非同步執行相應任務的有序佇列。它可以控制或同步其他 Stream 的執行,或者阻塞當前主機執行緒以確保正確的任務排序。它支援 with 語句作為上下文管理器,以確保 with 塊內的操作在相應的流上執行。
有關適用於所有裝置的精確語義的詳細資訊,請參閱 CUDA 語義 中對 CUDA 行為的深入描述。
- 引數
device (
torch.device, optional) – Stream 的目標裝置。如果未給出,將使用當前的 加速器 型別。priority (int, optional) – 流的優先順序,應為 0 或負數,其中負數表示更高的優先順序。預設情況下,流的優先順序為 0。
- 返回
一個 torch.Stream 物件。
- 返回型別
示例
>>> with torch.Stream(device='cuda') as s_cuda: >>> a = torch.randn(10, 5, device='cuda') >>> b = torch.randn(5, 10, device='cuda') >>> c = torch.mm(a, b)
- query() bool#
檢查所有提交的工作是否已完成。
- 返回
一個布林值,指示此流中的所有核心是否已完成。
- 返回型別
示例
>>> s_cuda = torch.Stream(device='cuda') >>> s_cuda.query() True
- record_event(event) Event#
記錄一個事件。將其加入到 Stream 中,以便在 FIFO 佇列中的當前點進行進一步的同步。
- 引數
event (
torch.Event, optional) – 要記錄的事件。如果未給出,將分配一個新的事件。- 返回
記錄的事件。
- 返回型別
示例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event()
- synchronize() None#
等待此流中的所有核心完成。
示例
>>> s_cuda = torch.Stream(device='cuda') >>> s_cuda.synchronize()
- wait_event(event) None#
使提交到此流的所有未來工作等待一個事件。
- 引數
event (
torch.Event) – 要等待的事件。
示例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record_event() >>> s2_cuda.wait_event(e_cuda)
- wait_stream(stream) None#
與另一個流同步。提交到此流的所有未來工作將等待直到給定流中已提交的所有核心完成。
- 引數
stream (
torch.Stream) – 要同步的流。
示例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> s2_cuda.wait_stream(s1_cuda)