
    Ph"                     ^   U d Z ddlmZmZmZ ddlZddlmZ ddlmZ	 ddlm
Z
 ddlmZ dd	lmZmZ g Zee   ed
<    G d d      Z G d de	      Z G d dej(                  j*                        Z G d dej(                  j*                        Z G d dej(                  j*                        Zy)uY  Portal keeps a tensor in the pocket plane. The tensor becomes hidden to the
autograd engine. The shared context of three functions (:class:`PortalBlue`,
:class:`PortalOrange`, and :class:`PortalCopy`) out of the computation graph is
one of the most important feature of :mod:`torchpipe.skip`.

The metaphor is inspired by Portal™ from Valve.

    )ListOptionalTupleN)Tensor   )Context)Copy)	get_phony)AbstractStream
get_device__all__c                       e Zd ZdZdee   deddfdZdefdZdedee   fd	Z	d
e
de
dedefdZddZdee   deddfdZdee   fdZdeddfdZdefdZy)PortalzA portal for a tensor.tensortensor_lifereturnNc                 6    | j                  ||       d | _        y N)
put_tensorgradselfr   r   s      vC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/pipeline/sync/skip/portal.py__init__zPortal.__init__   s    ,&*	    c                     | j                         }| t        t        j                  d      d      S t        j                  | |      S )a:  Creates a :class:`PortalBlue` which hides the underlying tensor from
        the autograd engine.

        Join the returning phony to the main lane of the autograd graph to
        assure the correct backpropagation::

            PortalBlue --+
                         |
            ---------- Join --

        cpuFrequires_grad)
use_tensorr
   torchdevice
PortalBlueapplyr   r   s     r   bluezPortal.blue#   s>     ">U\\%0FFf--r   phonyc                     | j                          | j                  | j                         S t        j	                  | |      S )a9  Creates a :class:`PortalOrange` which retrieves the hidden tensor
        without losing ability of backpropagation.

        Give a phony forked from the main lane of an autograd graph::

                +-- PortalOrange --+
                |                  |
            -- Fork --------- f(a, b) --

        )check_tensor_lifer   r    PortalOranger$   )r   r'   s     r   orangezPortal.orange6   s:     	 ;;??$$!!$..r   prev_streamnext_streamc                     | j                    t        t        j                  d      d      S t        j                  | |||      S )zCopies the hidden tensor by a :class:`PortalCopy`.

        Give a phony and use the returning phony to keep backpropagation::

                +-- PortalCopy --+
                |                |
            -- Fork ---------- Join --

        r   Fr   )r   r
   r!   r"   
PortalCopyr$   )r   r,   r-   r'   s       r   copyzPortal.copyH   s;     ;;U\\%0FFk;FFr   c                 8    | j                   dk  rt        d      y )Nr   z!tensor in portal has been removed)r   RuntimeError)r   s    r   r)   zPortal.check_tensor_lifeW   s!    q BCC !r   c                 :    || _         |dkD  r|| _        yd| _        y)z!Stores a tensor into this portal.r   N)r   r   r   s      r   r   zPortal.put_tensor[   s!    4 '? DKDKr   c                     | j                          | j                  }| xj                  dz  c_        | j                  dk  rd| _        |S )zRetrieves the underlying tensor and decreases the tensor  life. When
        the life becomes 0, it the tensor will be removed.
           r   N)r)   r   r   r%   s     r   r    zPortal.use_tensor|   sD     	 Aq DKr   r   c                     || _         y)z#Stores a gradient into this portal.N)r   r   r   s     r   put_gradzPortal.put_grad   s	    	r   c                 Z    | j                   t        d      | j                   }d| _         |S )zaRetrieves and removes the underlying gradient. The gradient is
        always ephemeral.
        Nz,grad in portal has been removed or never set)r   r2   r7   s     r   use_gradzPortal.use_grad   s/     99MNNyy	r   )r   N)__name__
__module____qualname____doc__r   r   intr   r&   r+   r   r0   r)   r   r    r8   r:    r   r   r   r      s     +x/ +c +d +.f .&/F /x'7 /$G G^ GTZ G`f GD&!1   BHV, V  	& 	r   r   c                       e Zd ZU eed<   y)r   portalN)r;   r<   r=   r   __annotations__r@   r   r   r   r      s    Nr   r   c            	       Z    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	def   fd	       Z
y)
r#   z=Hides a tensor from the autograd engine by a :class:`Portal`.ctxrB   r   r   c                 ^    || _         t        |j                  d      }|j                         S NFr   )rB   r
   r"   detach)rE   rB   r   r'   s       r   forwardzPortalBlue.forward   s'     
&--u=||~r   
grad_phonyNc                 >    | j                   j                         }d |fS r   )rB   r:   )rE   rJ   r   s      r   backwardzPortalBlue.backward   s      zz""$Tzr   r;   r<   r=   r>   staticmethodr   r   r   rI   r   rL   r@   r   r   r#   r#      sm    G			 		
 
	 	 g 6 uT6\7J  r   r#   c            	       V    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	d   fd	       Z
y
)r*   z3Retrieves the hidden tensor from a :class:`Portal`.rE   rB   r'   r   c                 X    || _         |j                         }|J |j                         S r   )rB   r    rH   )rE   rB   r'   r   s       r   rI   zPortalOrange.forward   s1     
""$!!!}}r   r   NNc                 :    | j                   j                  |       y)NrQ   )rB   r8   )rE   r   s     r   rL   zPortalOrange.backward   s     	

D!r   NrM   r@   r   r   r*   r*      sa    =W f V    g V j0A  r   r*   c                   ^    e Zd ZdZededededededefd       Z	eded	ede
d
   fd       Zy)r/   zfCopies the hidden tensor in a :class:`Portal`. It replaces the hidden
    tensor with copied one.
    rE   rB   r,   r-   r'   r   c                     || _         |j                  J t        j                  | |||j                        \  |_        t	        t        |      d      }|j                         S rG   )rB   r   r	   rI   r
   r   rH   )rE   rB   r,   r-   r'   s        r   rI   zPortalCopy.forward   sU    
 
}}(((<<[+v}}U*[1G||~r   rJ   NNNNc                     | j                   }|j                  J t        j                  | |j                        \  }}|_        y)NrU   )rB   r   r	   rL   )rE   rJ   rB   _s       r   rL   zPortalCopy.backward   s=     {{&&& MM#v{{;1fk%r   N)r;   r<   r=   r>   rN   r   r   r   r   rI   r   rL   r@   r   r   r/   r/      sz     		$	3A	P^	gm			 	 &g &6 &u=S7T & &r   r/   )r>   typingr   r   r   r!   r   r0   r   CopyContextr	   r'   r
   streamr   r   r   strrC   r   autogradFunctionr#   r*   r/   r@   r   r   <module>r^      s    ) (   )   /c | |Bk (( 05>>** (&(( &r   