
    Ph                     @   U d Z ddlmZ ddlZddlmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZm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 g Zee   ed<    G d d      Z G d de      Z G d dej@                        Z! e!       Z"ededed   fd       Z#defdZ$y)z Tracks skip tensors on a thread.    )contextmanagerN)Dict	GeneratorListOptionalTuple)Tensor   )is_checkpointing)forkjoin)Batch)AbstractStream   )
SkipLayout)	Namespace)Portal__all__c                   x    e Zd ZdZddZdedededee	   ddf
d	Z
dedededee	   fd
ZdedededededdfdZy)SkipTrackera`  Tracks saved skip tensors.

    It will update the given micro-batch in place. This is because when it
    manipulates the underlying skip tensors, the current micro-batch also has
    to be connected with the skip tensors.

    One thread has one skip tracker. Call :func:`current_skip_tracker` to get
    the skip tracker on the current thread.

    returnNc                     i | _         y Ntensorsselfs    wC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/pipeline/sync/skip/tracker.py__init__zSkipTracker.__init__%   s	    FH    batchnsnametensorc                 &    || j                   ||f<   y r   r   )r   r!   r"   r#   r$   s        r   savezSkipTracker.save(   s    #)b$Z r    c                 <    | j                   j                  ||f      S r   )r   pop)r   r!   r"   r#   s       r   loadzSkipTracker.load+   s    ||T
++r    prev_streamnext_streamc                     t        d      )Nz1copy is not supported for non-portal skip tensors)	TypeError)r   r!   r*   r+   r"   r#   s         r   copyzSkipTracker.copy.   s     KLLr    r   N)__name__
__module____qualname____doc__r   r   r   strr   r	   r&   r)   r   r.    r    r   r   r      s    	I*% *Y *c *8FCS *X\ *,% ,Y ,c ,hv>N ,MM)7MFTMZcMknM	Mr    r   c                        e Zd ZdZdeddf fdZdededed	e	e
   ddf
 fd
Zdededede	e
   f fdZdedededededdfdZ xZS )SkipTrackerThroughPotalsa  Tracks saved skip tensors through portals. The skip tensors will be
    hidden in portals so that the autograd engine does not need to track them.

    This tracker is only used when the training or evaluating module is wrapped
    with :class:`torchpipe.Pipe`.

    skip_layoutr   Nc                 >    t         |           || _        i | _        y r   )superr   r8   portals)r   r8   	__class__s     r   r   z!SkipTrackerThroughPotals.__init__=   s    &<>r    r!   r"   r#   r$   c                    | j                   j                  ||      st        	|   ||||       y||f| j                  vr-t               rd}nd}t        ||      }|| j                  ||f<   n%| j                  ||f   }d}|j                  ||       |j                         }|j                         }t        ||   |      ||<   y)zSaves the stashed skip tensor in a portal. The portal is then
        connected to the given micro-batch with :class:`Join`.
        N   r
   r   )r8   requires_copyr:   r&   r;   r   r   
put_tensorbluefind_tensor_idxr   )
r   r!   r"   r#   r$   tensor_lifeportalphony
tensor_idxr<   s
            r   r&   zSkipTrackerThroughPotals.saveB   s     --b$7GLD&1 :T\\)!
  FK0F'-DLL"d$ \\2t*-F Kfk2**,
 z!2E:jr    c                     | j                   j                  ||      st        |   |||      }|S | j                  ||f   }|j                         }t        ||         \  ||<   }|j                  |      }|S )zLoads a skip tensor from the corresponding portal to pop. The given
        micro-batch is connected to the portal with :class:`Fork`.
        )r8   r?   r:   r)   r;   rB   r   orange)	r   r!   r"   r#   r$   rD   rF   rE   r<   s	           r   r)   zSkipTrackerThroughPotals.loadq   s~     --b$7W\%T2FMr4j)**,
#'j(9#: j5u%r    r*   r+   c                     | j                   j                  ||      sJ |j                         }t        ||         \  ||<   }| j                  ||f   }|j                  |||      }t        ||   |      ||<   y)zCopies the skip tensor in the corresponding portal. The given
        micro-batch and the portal will be tied with :class:`Fork` and
        :class:`Join`.
        N)r8   r?   rB   r   r;   r.   r   )	r   r!   r*   r+   r"   r#   rF   rE   rD   s	            r   r.   zSkipTrackerThroughPotals.copy   s     --b$777**,
#'j(9#: j5r4j)Ke< z!2E:jr    )r0   r1   r2   r3   r   r   r   r   r4   r   r	   r&   r)   r   r.   __classcell__)r<   s   @r   r7   r7   4   s    ?J ?4 ?
-;% -;Y -;c -;8FCS -;X\ -;^% Y c hv>N ;;)7;FT;Zc;kn;	;r    r7   c                       e Zd ZddZy)ThreadLocalNc                     d | _         y r   skip_trackerr   s    r   r   zThreadLocal.__init__   s
    37r    r/   )r0   r1   r2   r   r5   r    r   rL   rL      s    8r    rL   rO   r   )NNNc              #      K   t         j                  }| t         _        	 d |t         _        y# |t         _        w xY ww)zRegisters the given skip tracker on the current thread within a
    context::

        with use_skip_tracker(my_skip_tracker):
            ...

    N)thread_localrO   )rO   origs     r   use_skip_trackerrS      s3      $$D ,L)$(!D!s   ?/ ?<?c                  T    t         j                  } | t               } | t         _        | S )z,Gets the skip tracker on the current thread.)rQ   rO   r   rN   s    r   current_skip_trackerrU      s(    ,,L"}$0!r    )%r3   
contextlibr   	threadingtypingr   r   r   r   r   torchr	   
checkpointr   
dependencyr   r   
microbatchr   streamr   layoutr   	namespacer   rD   r   r   r4   __annotations__r   r7   localrL   rQ   rS   rU   r5   r    r   <module>rb      s    ' %  9 9  ) #  #    c M M6Z;{ Z;z8)// 8
 } ); )9=M3N ) )$k r    