
    Χgw                        d dl mZmZmZmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlmZmZ g dZ ede          ded	efd
            Zded	efdZ edeee          Ze	 ddej        dee	eej        f                  ded	eej        df         fd            Ze	 ddedee	eej        f                  ded	ee         fd            ZddZ	 ddeedf         deeeef                  dee	eej        f                  ded	eeedf         eeeef         df         f         f
dZddede	eej        f         ded	efdZdS )    )	AnyDictListOptionaloverloadSequenceTupleTypeVarUnion)
deprecatedN)GatherScatter)scatterscatter_kwargsgatherzC`is_namedtuple` is deprecated, please use the python checks instead)categoryobjreturnc                      t          |           S N)_is_namedtupler   s    \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/nn/parallel/scatter_gather.pyis_namedtupler      s     #    c                 l    t          | t                    ot          | d          ot          | d          S )N_asdict_fields)
isinstancetuplehasattrr   s    r   r   r      s4     	3V73	#:#:VwsI?V?Vr   T.inputstarget_gpusdimc                     d S r    r#   r$   r%   s      r   r   r       	     Cr   c                     d S r   r'   r(   s      r   r   r   )   r)   r   c                 F    fd	  |           }dn# dw xY w|S )zSlice tensors into approximately equal chunks and distributes them across given GPUs.

    Duplicates references to objects that are not tensors.
    c                     t           t          j                  rt          j        d            S t                     r# fdt          t                      D             S t           t                    r7t                     dk    r$t          t          t                                S t           t                    r4t                     dk    r!d t          t                      D             S t           t                    rHt                     dk    r5 fdt          t                                                      D             S  fdD             S )Nc                 4    g | ]} t                    | S r'   type).0argsr   s     r   
<listcomp>z0scatter.<locals>.scatter_map.<locals>.<listcomp><   s&    MMMIDIIt$MMMr   r   c                 ,    g | ]}t          |          S r'   )list)r0   is     r   r2   z0scatter.<locals>.scatter_map.<locals>.<listcomp>@   s    AAADGGAAAr   c                 @    g | ]} t                    |          S r'   r.   )r0   r5   r   s     r   r2   z0scatter.<locals>.scatter_map.<locals>.<listcomp>B   s'    NNNQIDIIaLLNNNr   c                     g | ]}S r'   r'   )r0   _r   s     r   r2   z0scatter.<locals>.scatter_map.<locals>.<listcomp>C   s    ))))))r   )r   torchTensorr   applyr   zipmapr    lenr4   dictitems)r   r%   scatter_mapr$   s   `r   rA   zscatter.<locals>.scatter_map8   sU   c5<(( 	>=dC===# 	NMMMMc+s6K6K1LMMMMc5!! 	5c#hhllSc223444c4   	BSXX\\AAS#k3*?*?%@AAAAc4   	OSXX\\NNNN#s;		/L/L*MNNNN))))[))))r   Nr'   )r#   r$   r%   resrA   s    `` @r   r   r   2   sY    * * * * * * *$k&!!dJ    kwargsc           	      8   | rt          | ||          ng }|rt          |||          ng }t          |          t          |          k     rJ|                    d t          t          |          t          |          z
            D                        nit          |          t          |           k     rI|                    d t          t          |          t          |          z
            D                        t	          |          t	          |          fS )z+Scatter with support for kwargs dictionary.c              3      K   | ]}d V  dS )r'   Nr'   r0   r8   s     r   	<genexpr>z!scatter_kwargs.<locals>.<genexpr>[   3        
  
B 
  
  
  
  
  
r   c              3      K   | ]}i V  d S r   r'   rG   s     r   rH   z!scatter_kwargs.<locals>.<genexpr>_   rI   r   )r   r>   extendranger    )r#   rD   r$   r%   scattered_inputsscattered_kwargss         r   r   r   Q   s@    =CJwv{C888<BJwv{C888
s#34444  
  
c"233c:J6K6KKLL 
  
  
 	
 	
 	
 	
 
		V	,	,  
  
c"233c:J6K6KKLL 
  
  
 	
 	
 	
 !""E*:$;$;;;r   outputstarget_devicec                 F    fd	  |           }dn# dw xY w|S )a  Gather tensors from different GPUs on a specified device.

    This function is useful for gathering the results of a distributed computation.
    It takes a sequence of objects, one for each GPU, and returns a single object
    on the specified device.

    Args:
        outputs (Any): A sequence of objects (potentially tensors) to gather.
        target_device (Union[int, torch.device]): The device to gather the tensors to.
            Use 'cpu' for CPU to avoid a deprecation warning.
        dim (int, optional): The dimension along which to gather. Default: 0.

    Returns:
        Any: A gathered object (potentially tensor) on the specified device.
    c                 (     d         t          t          j                  rt          j        g R  S d S t          t
                    rOt          fd D                       st          d           t                     fdD                       S t                    r7t                    
                    t          t                                S  t                    t          t                                S )Nr   c              3   X   K   | ]$}t                    t          |          k    V  %d S r   )r>   )r0   douts     r   rH   z-gather.<locals>.gather_map.<locals>.<genexpr>}   s4      ;;as3xx3q66);;;;;;r   z+All dicts must have the same number of keysc              3   L   K   | ] fd D                       fV  dS )c                      g | ]
}|         S r'   r'   )r0   rT   ks     r   r2   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>   s    ,C,C,CaQqT,C,C,Cr   Nr'   )r0   rX   
gather_maprO   s    @r   rH   z-gather.<locals>.gather_map.<locals>.<genexpr>   sH      SS!a,C,C,C,C7,C,C,C!D!DESSSSSSr   )r   r9   r:   r   r;   r?   all
ValueErrorr/   r   _maker=   r<   )rO   rU   r%   rY   rP   s   `@r   rY   zgather.<locals>.gather_mapv   s   ajc5<(( 	><s=W====;4c4   	T;;;;7;;;;; P !NOOO499SSSSSsSSSSSS# 	C99??3z3=#A#ABBBtCyyZg77888r   Nr'   )rO   rP   r%   rB   rY   s    `` @r   r   r   e   sY    "9 9 9 9 9 9 9 j!!

T
JrC   ).)r   ) typingr   r   r   r   r   r   r	   r
   r   typing_extensionsr   r9   torch.nn.parallel._functionsr   r   __all__FutureWarningboolr   r   r?   r4   r    r"   r:   intdevicer   strr   r   r'   r   r   <module>rf      s   W W W W W W W W W W W W W W W W W W W W W W ( ( ( ( ( (  8 8 8 8 8 8 8 8 2
1
1 I  s t   	 
      GCtU## 
  L%U\ 123 
 5<	   
 
  %U\ 123 
 
!W	   
   F 	< <#s(O<T#s(^$< %U\ 123< 
	<
 5c?E$sCx.#"5667< < < <(% %C %c5<.?(@ %s %SV % % % % % %r   