U
    Jºc  ã                   @   s
  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„ dƒZG dd„ deƒZG dd„ dej ƒZ!e!ƒ Z"eeed dœdd„ƒZ#edœdd„Z$dS )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                   @   sh   e Zd ZdZddœdd„Zeeeee	 ddœdd„Z
eeeee	 d	œd
d„Zeeeeeddœdd„ZdS )Ú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.

    N©Úreturnc                 C   s
   i | _ d S ©N©Útensors©Úself© r   úP/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/pipeline/sync/skip/tracker.pyÚ__init__%   s    zSkipTracker.__init__©ÚbatchÚnsÚnameÚtensorr   c                 C   s   || j ||f< d S r   r   )r   r    r!   r"   r#   r   r   r   Úsave(   s    zSkipTracker.save©r    r!   r"   r   c                 C   s   | j  ||f¡S r   )r   Úpop)r   r    r!   r"   r   r   r   Úload+   s    zSkipTracker.load©r    Úprev_streamÚnext_streamr!   r"   r   c                 C   s   t dƒ‚d S )Nz1copy is not supported for non-portal skip tensors)Ú	TypeError)r   r    r)   r*   r!   r"   r   r   r   Úcopy.   s    zSkipTracker.copy)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ústrr   r   r$   r'   r   r,   r   r   r   r   r      s       þr   c                       sz   e Zd ZdZeddœ‡ fdd„Zeeee	e
 ddœ‡ fdd„Zeeee	e
 d	œ‡ fd
d„Zeeeeeddœdd„Z‡  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`.

    N)Úskip_layoutr   c                    s   t ƒ  ¡  || _i | _d S r   )Úsuperr   r3   Úportals)r   r3   ©Ú	__class__r   r   r   =   s    
z!SkipTrackerThroughPotals.__init__r   c           	         s    | j  ||¡s$tƒ  ||||¡ dS ||f| jkr\tƒ r>d}nd}t||ƒ}|| j||f< n| j||f }d}| ||¡ | ¡ }| 	¡ }t
|| |ƒ||< dS )z…Saves the stashed skip tensor in a portal. The portal is then
        connected to the given micro-batch with :class:`Join`.
        Né   r	   r   )r3   Úrequires_copyr4   r$   r5   r
   r   Z
put_tensorZblueÚfind_tensor_idxr   )	r   r    r!   r"   r#   Ztensor_lifeÚportalÚphonyÚ
tensor_idxr6   r   r   r$   B   s    
zSkipTrackerThroughPotals.saver%   c                    sZ   | j  ||¡s"tƒ  |||¡}|S | j||f }| ¡ }t|| ƒ\||< }| |¡}|S )zLoads a skip tensor from the corresponding portal to pop. The given
        micro-batch is connected to the portal with :class:`Fork`.
        )r3   r9   r4   r'   r5   r:   r   Zorange)r   r    r!   r"   r#   r;   r=   r<   r6   r   r   r'   q   s    
zSkipTrackerThroughPotals.loadr(   c           	      C   s`   | j  ||¡st‚| ¡ }t|| ƒ\||< }| j||f }| |||¡}t|| |ƒ||< dS )z¤Copies the skip tensor in the corresponding portal. The given
        micro-batch and the portal will be tied with :class:`Fork` and
        :class:`Join`.
        N)r3   r9   ÚAssertionErrorr:   r   r5   r,   r   )	r   r    r)   r*   r!   r"   r=   r<   r;   r   r   r   r,      s    zSkipTrackerThroughPotals.copy)r-   r.   r/   r0   r   r   r   r   r1   r   r   r$   r'   r   r,   Ú__classcell__r   r   r6   r   r2   4   s   /    þr2   c                   @   s   e Zd Zddœdd„ZdS )ÚThreadLocalNr   c                 C   s
   d | _ d S r   ©Úskip_trackerr   r   r   r   r   ’   s    zThreadLocal.__init__)r-   r.   r/   r   r   r   r   r   r@   ‘   s   r@   )NNN)rB   r   c                 c   s$   t j}| t _z
dV  W 5 |t _X dS )z”Registers the given skip tracker on the current thread within a
    context::

        with use_skip_tracker(my_skip_tracker):
            ...

    N)Úthread_localrB   )rB   Úorigr   r   r   Úuse_skip_tracker™   s
    	
rE   r   c                  C   s   t j} | dkrtƒ } | t _| S )z,Gets the skip tracker on the current thread.N)rC   rB   r   rA   r   r   r   Úcurrent_skip_tracker¬   s
    rF   )%r0   Ú
contextlibr   Ú	threadingÚtypingr   r   r   r   r   Ztorchr   Ú
checkpointr
   Ú
dependencyr   r   Z
microbatchr   Ústreamr   Zlayoutr   Ú	namespacer   r;   r   r   r1   Ú__annotations__r   r2   Úlocalr@   rC   rE   rF   r   r   r   r   Ú<module>   s&   ]