a
    NSic(                      @   s   d dl Z d dlZddlmZ eejdsHedejjd< edejjd< G dd dejjZG dd	 d	eZ	G d
d dejj
ZdS )    N   )_dummy_type_CudaStreamBase_CudaEventBasec                       s|   e Zd ZdZd fdd	Zdd Zdd	 Zdd
dZ fddZ fddZ	e
dd Z fddZdd Zdd Z  ZS )Streama  Wrapper around a CUDA stream.

    A CUDA stream is a linear sequence of execution that belongs to a specific
    device, independent from other streams.  See :ref:`cuda-semantics` for
    details.

    Args:
        device(torch.device or int, optional): a device on which to allocate
            the stream. If :attr:`device` is ``None`` (default) or a negative
            integer, this will use the current device.
        priority(int, optional): priority of the stream. Can be either
            -1 (high priority) or 0 (low priority). By default, streams have
            priority 0.

    .. note:: Although CUDA versions >= 11 support more than two levels of
        priorities, in PyTorch, we only support two levels of priorities.
    Nr   c                    sL   t j|, tt| j| fd|i|W  d    S 1 s>0    Y  d S )Nprioritytorchcudadevicesuperr   __new__)clsr   r   kwargs	__class__ N/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/cuda/streams.pyr      s    zStream.__new__c                 C   s   | |  dS )a  Makes all future work submitted to the stream wait for an event.

        Args:
            event (torch.cuda.Event): an event to wait for.

        .. note:: This is a wrapper around ``cudaStreamWaitEvent()``: see
           `CUDA Stream documentation`_ for more info.

           This function returns without waiting for :attr:`event`: only future
           operations are affected.

        .. _CUDA Stream documentation:
           https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__STREAM.html
        N)waitselfeventr   r   r   
wait_event#   s    zStream.wait_eventc                 C   s   |  |  dS )a  Synchronizes with another stream.

        All future work submitted to this stream will wait until all kernels
        submitted to a given stream at the time of call complete.

        Args:
            stream (Stream): a stream to synchronize.

        .. note:: This function returns without waiting for currently enqueued
           kernels in :attr:`stream`: only future operations are affected.
        N)r   record_eventr   streamr   r   r   wait_stream4   s    zStream.wait_streamc                 C   s   |du rt  }||  |S )zRecords an event.

        Args:
            event (torch.cuda.Event, optional): event to record. If not given, a new one
                will be allocated.

        Returns:
            Recorded event.
        N)Eventrecordr   r   r   r   r   B   s    

zStream.record_eventc                    s   t t|  S )zChecks if all the work submitted has been completed.

        Returns:
            A boolean indicating if all kernels in this stream are completed.)r   r   queryr   r   r   r   r   Q   s    zStream.queryc                    s   t t|   dS )zWait for all the kernels in this stream to complete.

        .. note:: This is a wrapper around ``cudaStreamSynchronize()``: see
           `CUDA Stream documentation`_ for more info.
        N)r   r   synchronizer    r   r   r   r!   X   s    zStream.synchronizec                 C   s   t | jS N)ctypesc_void_pcuda_streamr    r   r   r   _as_parameter_`   s    zStream._as_parameter_c                    s   t |trtt| |S dS )NF)
isinstancer   r   __eq__)r   or   r   r   r(   d   s    
zStream.__eq__c                 C   s   t | j| jfS r"   )hashr%   r   r    r   r   r   __hash__i   s    zStream.__hash__c                 C   s   d | j| jS )Nz1<torch.cuda.Stream device={0} cuda_stream={1:#x}>)formatr   r%   r    r   r   r   __repr__l   s    zStream.__repr__)Nr   )N)__name__
__module____qualname____doc__r   r   r   r   r   r!   propertyr&   r(   r+   r-   __classcell__r   r   r   r   r      s   

r   c                       s"   e Zd ZdZd fdd	Z  ZS )ExternalStreama  Wrapper around an externally allocated CUDA stream.

    This class is used to wrap streams allocated in other libraries in order
    to facilitate data exchange and multi-library interactions.

    .. note:: This class doesn't manage the stream life-cycle, it is the user
       responsibility to keep the referenced stream alive while this class is
       being used.

    Args:
        stream_ptr(int): Integer representation of the `cudaStream_t` value.
            allocated externally.
        device(torch.device or int, optional): the device where the stream
            was originally allocated. if device is specified incorrectly,
            subsequent launches using this stream may fail.
    Nc                    sL   t j|, tt| j| fd|i|W  d    S 1 s>0    Y  d S )N
stream_ptrr   )r   r5   r   r   r   r   r   r      s    zExternalStream.__new__)N)r.   r/   r0   r1   r   r3   r   r   r   r   r4   q   s   r4   c                       s   e Zd ZdZd fdd	Ze fddZd fdd		Zd fd
d	Z fddZ	 fddZ
 fddZ fddZedd Zdd Z  ZS )r   a  Wrapper around a CUDA event.

    CUDA events are synchronization markers that can be used to monitor the
    device's progress, to accurately measure timing, and to synchronize CUDA
    streams.

    The underlying CUDA events are lazily initialized when the event is first
    recorded or exported to another process. After creation, only streams on the
    same device may record the event. However, streams on any device can wait on
    the event.

    Args:
        enable_timing (bool, optional): indicates if the event should measure time
            (default: ``False``)
        blocking (bool, optional): if ``True``, :meth:`wait` will be blocking (default: ``False``)
        interprocess (bool): if ``True``, the event can be shared between processes
            (default: ``False``)

    .. _CUDA Event Documentation:
       https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html
    Fc                    s   t t| j| |||dS )N)enable_timingblockinginterprocess)r   r   r   )r   r6   r7   r8   r   r   r   r      s    
zEvent.__new__c                    s   t t| ||S )z<Reconstruct an event from an IPC handle on the given device.)r   r   from_ipc_handle)r   r   handler   r   r   r9      s    zEvent.from_ipc_handleNc                    s&   |du rt j }tt| | dS )zRecords the event in a given stream.

        Uses ``torch.cuda.current_stream()`` if no stream is specified. The
        stream's device must match the event's device.N)r	   r
   current_streamr   r   r   r   r   r   r   r      s    
zEvent.recordc                    s&   |du rt j }tt| | dS )a#  Makes all future work submitted to the given stream wait for this
        event.

        Use ``torch.cuda.current_stream()`` if no stream is specified.

        .. note:: This is a wrapper around ``cudaStreamWaitEvent()``: see
            `CUDA Event documentation`_ for more info.
        N)r	   r
   r;   r   r   r   r   r   r   r   r      s    	
z
Event.waitc                    s   t t|  S )zChecks if all work currently captured by event has completed.

        Returns:
            A boolean indicating if all work currently captured by event has
            completed.
        )r   r   r   r    r   r   r   r      s    zEvent.queryc                    s   t t| |S )z}Returns the time elapsed in milliseconds after the event was
        recorded and before the end_event was recorded.
        )r   r   elapsed_time)r   	end_eventr   r   r   r<      s    zEvent.elapsed_timec                    s   t t|   dS )aO  Waits for the event to complete.

        Waits until the completion of all work currently captured in this event.
        This prevents the CPU thread from proceeding until the event completes.

         .. note:: This is a wrapper around ``cudaEventSynchronize()``: see
            `CUDA Event documentation`_ for more info.
        N)r   r   r!   r    r   r   r   r!      s    	zEvent.synchronizec                    s   t t|  S )ziReturns an IPC handle of this event. If not recorded yet, the event
        will use the current device. )r   r   
ipc_handler    r   r   r   r>      s    zEvent.ipc_handlec                 C   s   t | jS r"   )r#   r$   
cuda_eventr    r   r   r   r&      s    zEvent._as_parameter_c                 C   s   | j rd| jjS dS d S )Nz<torch.cuda.Event {0:#x}>z <torch.cuda.Event uninitialized>)r?   r,   r&   valuer    r   r   r   r-      s    zEvent.__repr__)FFF)N)N)r.   r/   r0   r1   r   classmethodr9   r   r   r   r<   r!   r>   r2   r&   r-   r3   r   r   r   r   r      s   		
r   )r#   r	   _utilsr   hasattr_C__dict__r   r   r4   r   r   r   r   r   r   <module>   s   e