Rate this Page
★ ★ ★ ★ ★

Event#

class torch.Event(device=None, *, enable_timing=False, blocking=False, interprocess=False)#

Query and record Stream status to identify or control dependencies across Stream and measure timing.

Parameters:
  • device (torch.device, optional) – the desired device for the Event. If not given, the current accelerator type will be used.

  • enable_timing (bool, optional) – indicates if the event should measure time (default: False)

  • blocking (bool, optional) – if True, wait() will be blocking (default: False)

  • interprocess (bool) – if True, the event can be shared between processes (default: False)

Returns:

An torch.Event object.

Return type:

Event

Example:

>>> event = torch.Event()
>>> e_cuda = torch.Event(device='cuda')
elapsed_time(end_event) → float#

Returns the elapsed time in milliseconds between when this event and the end_event are each recorded via torch.Stream.record_event().

Parameters:

end_event (torch.Event) – The ending event has been recorded.

Returns:

Time between starting and ending event in milliseconds.

Return type:

float

Example:

>>> 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)
from_ipc_handle(device, ipc_handle) → Event#

Class method. Reconstruct an event from an IPC handle on the given device.

Parameters:
Returns:

an event reconstructed from the IPC handle.

Return type:

Event

Example:

>>> e_cuda = torch.Event(device='cuda', interprocess=True)
>>> e_cuda.record()
>>> ipc_handle = e_cuda.ipc_handle()
>>> # pass ipc handle to another process and reconstruct the event with it there
>>> ipc_event = torch.Event.from_ipc_handle(torch.device('cuda'), ipc_handle)
ipc_handle() → bytes#

Return an IPC handle of this event.

The event must have been constructed with interprocess=True. If not yet recorded, the event is eagerly initialized on the current device. .. note:: The event reconstructed with from_ipc_handle() cannot be re-exported via ipc_handle().

Returns:

an opaque byte string that can be passed to from_ipc_handle()

in another process to reconstruct this event.

Return type:

bytes

Example:

>>> e_cuda = torch.Event(device='cuda', interprocess=True)
>>> ipc_handle = e_cuda.ipc_handle()
query() → bool#

Check if the stream where this event was recorded already moved past the point where the event was recorded. Always returns True if the Event was not recorded.

Returns:

A boolean indicating if all work currently captured by event has completed.

Return type:

bool

Example:

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

Record the event in a given stream. The stream’s device must match the event’s device. This function is equivalent to stream.record_event(self).

Parameters:

stream (torch.Stream, optional) – A stream to be recorded. If not given, the current stream will be used.

Example:

>>> e_cuda = torch.Event(device='cuda')
>>> e_cuda.record()
synchronize() → None#

Wait for the event to complete. This prevents the CPU thread from proceeding until the event completes.

Example:

>>> s_cuda = torch.Stream(device='cuda')
>>> e_cuda = s_cuda.record_event()
>>> e_cuda.synchronize()
wait(stream=None) → None#

Make all future work submitted to the given stream wait for this event.

Parameters:

stream (torch.Stream, optional) – A stream to synchronize. If not given, the current stream will be used.

Example:

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