活动 ¶
- class torch.Event(设备, *, 启用计时)
查询并记录流状态,以识别或控制流之间的依赖关系并测量计时。
- 参数:
device (
torch.device
,可选) – 指定 Event 的期望设备。如果未提供,则使用当前加速器类型。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
检查记录此事件的流是否已经移动到事件记录点之后。如果事件未被记录,则始终返回
True
。- 返回值:
一个布尔值,表示当前事件捕获的所有工作是否已完成。
- 返回类型:
示例:
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.query() True
- record(stream) → None
记录给定流中的事件。流的设备必须与事件设备匹配。此功能与
stream.record_event(self)
相当。- 参数:
流(
torch.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 (
torch.Stream
,可选) – 要同步的流。给定(如果不)——
使用。 (当前流将是)——
示例:
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record() >>> e_cuda.wait(s2)