快捷方式

事件

class torch.Event(device, *, enable_timing)

查询和记录 Stream 状态,以识别或控制 Stream 之间的依赖关系并测量时间。

参数
  • 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) – 已记录的结束事件。

返回值

开始和结束事件之间的时间,以毫秒为单位。

返回类型

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 未被记录,则始终返回 True

返回值

一个布尔值,指示事件当前捕获的所有工作是否已完成。

返回类型

bool

示例

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

在给定的流中记录事件。流的设备必须与事件的设备匹配。此函数等效于 stream.record_event(self)

参数
  • stream (torch.Stream, 可选) – 要记录的流。

  • given (如果未) –

  • used. (将使用当前流) –

示例

>>> 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, 可选) – 要同步的流。

  • given (如果未) –

  • used. (将使用当前流) –

示例

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

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源