
    Ph#                         U d Z ddlZddlmZmZmZmZmZmZ ddlZddlm	Z	 ddl
Zg dZee   ed<   ee	   Zee	ef   Zeegeee   e	f   f   Z G d d      Z G d	 d
      ZddZdedee   fdZdee   fdZy)zManipulation of micro-batches.    N)AnyCallableListUnioncastSequence)Tensor)NoChunkBatchcheckscattergather__all__c                   ,    e Zd ZdZdefdZed        Zy)r
   a  
    Wrapper for a Tensor in :meth:`Pipe.forward` indicating that the tensor
    should not be chunked on the batch dimension and instead be replicated
    as-is across all micro-batches. This is useful for tensors which might
    not have any 'batch' semantics for the model.
    inpc                 X    t        j                  |      st        d|       || _        y )Nz+NoChunk only supported for tensors, found: )torch	is_tensor	TypeError_tensor)selfr   s     uC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/pipeline/sync/microbatch.py__init__zNoChunk.__init__   s(    s#I#OPP    c                     | j                   S N)r   r   s    r   tensorzNoChunk.tensor#   s    ||r   N)__name__
__module____qualname____doc__r	   r   propertyr    r   r   r
   r
      s'    F 
  r   r
   c                   :   e Zd ZdZdeee   ef   ddfdZe	defd       Z
e	d        Zd Zd	 Zd
edd fdZdefdZd ZdefdZdefdZej.                  dededdfd       Zej.                  dededdfd       Zdeeef   ddfdZdeddfdZdeddfdZy)r   zC
    An abstraction representing a microbatch in the pipeline.
    valuesreturnNc                     || _         t        j                  |      | _        | j                  s5t	        d | j                   D              st        d| j                          y y )Nc              3   F   K   | ]  }t        j                  |        y wr   r   r   ).0values     r   	<genexpr>z!Batch.__init__.<locals>.<genexpr>3   s     H<%uu-<   !zNo tensors found in batch: )_valuesr   r   atomicanyr   )r   r&   s     r   r   zBatch.__init__-   sV    oof- {{H4<<HH"=dll^ LMM I r   c                 d    | j                   st        d      t        t        | j                        S )z Retrieves the underlying tensor.znot atomic batch)r0   AttributeErrorr   r	   r/   r   s    r   r   zBatch.tensor6   s(     {{ !344FDLL))r   c                     | j                   S )z-Retrieves the underlying values for the batch)r/   r   s    r   r&   zBatch.values=   s     ||r   c                     | j                   ryt        | j                        D ]  \  }}t        j                  |      s|c S  t        d      )z<
        Retrieves the index of first tensor found.
        r   zNo tensor found!)r0   	enumerater/   r   r   r   )r   ir,   s      r   find_tensor_idxzBatch.find_tensor_idxB   sD     ;;!$,,/HAuu% 0 *++r   c                     | j                   r| j                  j                  S | j                  D ]%  }t        j                  |      s|j                  c S  y)z;
        Retrieves the device for this microbatch.
        N)r0   r/   devicer   r   )r   r,   s     r   
get_devicezBatch.get_deviceN   s?     ;;<<&&&\\Eu%||# "r   functionc                     | j                   rt         || j                              S t         || j                         S )zbCalls a function on the microbatch. It also wraps
        the output with :class:`Batch`.
        )r0   r   r/   )r   r<   s     r   callz
Batch.callY   s3     ;;$,,/004<<011r   c                 <    d| j                   d| j                  dS )NzBatch[atomic=z]()r0   r/   r   s    r   __repr__zBatch.__repr__b   s     t{{oR/?qAAr   c              #   n   K   | j                   r| j                   y | j                  E d {    y 7 wr   rA   r   s    r   __iter__zBatch.__iter__e   s$     ;;,,||##s   +535c                 H    | j                   rdS t        | j                        S )N   )r0   lenr/   r   s    r   __len__zBatch.__len__k   s    KKq6S%66r   indexc                 p    | j                   s| j                  |   S |dk7  rt        d      | j                  S )Nr    atomic batch allows index 0 onlyr0   r/   
IndexError)r   rI   s     r   __getitem__zBatch.__getitem__n   s5    {{<<&&A:?@@||r   r,   c                      y r   r$   r   rI   r,   s      r   __setitem__zBatch.__setitem__x       r   c                      y r   r$   rP   s      r   rQ   zBatch.__setitem__|   rR   r   c                 n    t        |t              r| j                  ||       y | j                  ||       y r   )
isinstanceint_setitem_by_index_setitem_by_slicerP   s      r   rQ   zBatch.__setitem__   s,    eS!""5%0""5%0r   c                     | j                   s.|}| j                  d | |fz   | j                  |dz   d  z   | _        y |dk7  rt        d      || _        y )NrF   r   rK   rL   )r   rI   r,   r7   s       r   rW   zBatch._setitem_by_index   sX    {{A<<+uh6a!eg9NNDLA:?@@r   c                     |j                   |j                  cxu r|j                  cxu rt        d       t        d      | j                  s|| _        y t        |      dk7  rt        d      |d   | _        y )Nzonly slice [:] supportedrF   z5atomic batch cannot be replaced with multiple tensorsr   )startstopstepNotImplementedErrorr0   r/   rG   rM   rP   s      r   rX   zBatch._setitem_by_slice   sl    uzz?UZZ?%&@AA @%&@AA{{ DLu:?TUUQxr   )r   r    r!   r"   r   r   r   r	   r   r#   r   r&   r8   r;   Functionr>   strrB   rD   rV   rH   rN   typingoverloadrQ   sliceTensorsrW   rX   r$   r   r   r   r   (   s>   NuT#Y%67 ND N * * *  
,	$2X 2' 2B# B$7 7  __ V    __ w 4  1sEz!2 1d 1	s 	d 	 u    r   r   r'   c                      t        d |D              st        d|       t         fd|D              rt        d      y)z
    Checks whether the input contains at least one tensor and each tensor is
    on the same device as the first partition.

    Raises:
        ValueError: input does not contain at least one tensor

    c              3   F   K   | ]  }t        j                  |        y wr   r*   )r+   inputs     r   r-   zcheck.<locals>.<genexpr>   s     :6%uu%6r.   z inputs do not have any tensors: c              3   j   K   | ]*  }t        j                  |      xr |j                  k7   , y wr   )r   r   r:   )r+   rg   first_devices     r   r-   zcheck.<locals>.<genexpr>   s.     
WPVu5??5!Belll&BBPVs   03z>All inputs should be on the same device as the first partitionN)r1   r   
ValueError)ri   inputss   ` r   r   r      sG     :6:::6(CDD

WPV
WWYZZ Xr   chunksc           	         t        |      dk(  r>t        |d   t              r+|d   j                  |       D cg c]  }t	        |       c}S t        |       D cg c]  }g  }}d}|D ]  }t        j                  |      rq|j                  |       }|dk7  r(|t        |      k7  rt        d| dt        |             t        |      }t        |      D ]  \  }}	||   j                  |	        t        |       D ]E  }t        |t              r||   j                  |j                         2||   j                  |       G  |d| }|D cg c]  }t	        |       c}S c c}w c c}w c c}w )z7Splits an input mini-batch into multiple micro-batches.rF   r   z6Found different number of chunks produced for inputs:  and N)rG   rU   r	   chunkr   ranger   r   RuntimeErrorr6   appendr
   r   )
rl   rk   x_batches
num_chunksrg   tensorsr7   r   s
             r   r   r      sa   
6{aJvay&9"()//&"9:"9Qa"9::&+Fm4m"mG4J??5!kk&)G RJ#g,$>"%[\f[gglmpqxmylz#{||WJ&w/	6
!!&) 0 6]eW-AJ%%ell3AJ%%e, # , kz"G%&gE!Hg&&; ;46 's   E%	E*E/outputsc                 "   | d   j                   r)t        d | D              }t        j                  |      }|S g }t	        t        | d               D ]  }t        | d   |         }g }| D ]D  }|t        ||         k7  rt        d| dt        ||                |j                  ||          F t        j                  | d   |         r%|j                  t        j                  |             |j                  |        t        |      }|S )z4Concatenates output micro-batches into a mini-batch.r   c              3   4   K   | ]  }|j                     y wr   )r   )r+   bs     r   r-   zgather.<locals>.<genexpr>   s     2'Q's   z2Types for microbatch outputs do not match, found: ro   )
r0   tupler   catrq   rG   typer   rs   r   )ry   rx   output
output_bufr7   output_typecurrent_outputsbatchs           r   r   r      s    qz2'227#$ M! !#
s71:'Awqz!}-K O $uQx.0#&XYdXeejkopuvwpxkyjz${||&&uQx0 !
 wqz!}-!!%))O"<=!!/2 ( z"Mr   )r'   N)r"   ra   r   r   r   r   r   r   r   r	   torch.cuda.commr   r`   __annotations__rd   TensorOrTensorsr_   r
   r   r   rV   r   r   r$   r   r   <module>r      s    %  = =   Gc G 6
(_%uT#Y->'??@ "t  t n[  'S  'T%[  'FDK r   