
    Ph
                        U d Z ddlmZ ddlmZmZmZmZmZ ddl	Z	ddl	m
Z
 ddlmZmZmZmZmZmZ g dZee   ed	<   ee
   Z G d
 d      Z G d de	j.                  j0                        Z G d de	j.                  j0                        Zy)zlAutograd functions for stream-aware CUDA copy.

It is used to overlap copy and computation on the same GPU.
    )deque)DequeListOptionalTupleSequenceN)Tensor   )AbstractStreamcurrent_stream
get_devicerecord_stream
use_streamwait_stream)ContextCopyWait__all__c                   "    e Zd ZU eed<   eed<   y)r   prev_streamnext_streamN)__name__
__module____qualname__r   __annotations__     oC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/pipeline/sync/copy.pyr   r      s    r   r   c            	       `    e Zd ZdZededededefd       Zedede	de
ee	   df   fd	       Zy
)r   z#Copies tensors on specific streams.ctxr   r   returnc                    || _         || _        g }t        t        |            }t	        |      5  t	        |      5  |D ]n  }t        j                  |      rF|j                  t        |      d      }|j                  |       t        ||       t        ||       ^|j                  |       p 	 d d d        d d d        t        |      S # 1 sw Y   xY w# 1 sw Y   t        |      S xY w)NTnon_blocking)r   r   r   r   r   torch	is_tensortoappendr   tuple)r    r   r   inputoutputoutput_streamxys           r   forwardzCopy.forward"   s     &%&z+'>?$j&=??1%Z44HAMM!$ "![1 "!]3MM!$  '>$ V} '>&=$ V}s#   CA4C1CC	CC+grad_output.c                    | j                   }| j                  }t        t        |            }t	        t        |            }t        |      5  t        |      5  t        |      D ]G  }|j                  t        |      d      }|j                  |       t        ||       t        ||       I 	 d d d        d d d        d}|t        |      z   S # 1 sw Y   !xY w# 1 sw Y   %xY w)N)maxlenTr#   NN)r   r   r   lenr   r   r   reversedr'   
appendleftr   r)   )	r    r0   r   r   
grad_inputinput_streamr-   r.   grad_streamss	            r   backwardzCopy.backward;   s    oooo$)[1A$B
%j&=>$j&=k*DDK0tDD%%a( a- a. + '>$ 6BeJ/// '>&=$$s%   CAC0CC	CC%Nr   r   r   __doc__staticmethodr   r   Tensorsr/   r	   r   r   r:   r   r   r   r   r      sm    -W >  dk  . 0g 0V 0x?OQT?T9U 0 0r   r   c            	       `    e Zd ZdZededededefd       Zedede	de
ee	   df   fd	       Zy
)r   zSynchronizes a stream to another stream.

    Place it just before you want to start an operation on the next stream,
    provided that all operations on the previous stream are done.

    r    r   r   r!   c                 Z    || _         || _        t        ||       t        d |D              S )Nc              3   j   K   | ]+  }t        j                  |      r|j                         n| - y w)N)r%   r&   detach).0r-   s     r   	<genexpr>zWait.forward.<locals>.<genexpr>b   s'     Le5??1#5QXXZ1<es   13)r   r   r   r)   )r    r   r   r*   s       r   r/   zWait.forwardZ   s-     &%K-LeLLLr   r7   .c                 X    | j                   }| j                  }t        ||       d}||z   S )Nr3   )r   r   r   )r    r7   r   r   r9   s        r   r:   zWait.backwardd   s.    ooooK-5Aj((r   Nr;   r   r   r   r   r   R   sw     MW M> M Mcj M M )g )F )hv>NPS>S8T ) )r   r   )r<   collectionsr   typingr   r   r   r   r   r%   r	   streamr   r   r   r   r   r   r   strr   r>   r   autogradFunctionr   r   r   r   r   <module>rL      sr     9 9   f f0c 0 6
   
005>>"" 00f)5>>"" )r   