
    [iF                     H   d dl mZ d dl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j        z           ded
eej        df         fd            Ze	 ddedeeej        z           ded
ee         fd            ZddZ	 ddeedf         deeef         dz  deeej        z           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j        z  ded
efdZdS )    )Sequence)AnyoverloadTypeVar)
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    S/var/www/icac/venv/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
   !   s	    
  #sr   c                     d S r   r"   r#   s      r   r
   r
   )   s	    
 cr   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diD             S t           t                    r:t                     dk    r't          t          t                     ddi          S t           t                    r7t                     dk    r$d t          t                     ddiD             S t           t                    rKt                     dk    r8 fdt          t                                                     ddiD             S  fdD             S )Nc                 4    g | ]} t                    | S r"   type).0argsr   s     r   
<listcomp>z0scatter.<locals>.scatter_map.<locals>.<listcomp><   s8         S		4   r   strictFr   c                 ,    g | ]}t          |          S r"   )list)r*   is     r   r,   z0scatter.<locals>.scatter_map.<locals>.<listcomp>G   s    OOODGGOOOr   c                 @    g | ]} t                    |          S r"   r(   )r*   r0   r   s     r   r,   z0scatter.<locals>.scatter_map.<locals>.<listcomp>J   s9         S		!  r   c                     g | ]}S r"   r"   )r*   _r   s     r   r,   z0scatter.<locals>.scatter_map.<locals>.<listcomp>P   s    ))))))r   )r   torchTensorr	   applyr   zipmapr   lenr/   dictitems)r   r    scatter_mapr   s   `r   r<   zscatter.<locals>.scatter_map7   s   c5<(( 	>=dC===# 	     [#!6!6EuEE	    c5!! 	Cc#hhllSc22A5AABBBc4   	PSXX\\OOS#k3*?*?%N%N%NOOOOc4   	SXX\\    c+syy{{;;JEJJ	    *)))[))))r   Nr"   )r   r   r    resr<   s    `` @r   r
   r
   1   sZ    * * * * * * *@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"   r*   r3   s     r   	<genexpr>z!scatter_kwargs.<locals>.<genexpr>h   3        
  
B 
  
  
  
  
  
r   c              3      K   | ]}i V  d S r   r"   rB   s     r   rC   z!scatter_kwargs.<locals>.<genexpr>l   rD   r   )r
   r9   extendranger   )r   r?   r   r    scattered_inputsscattered_kwargss         r   r   r   ^   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           
      4     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                    r:t                    
                    t          t           ddi                    S  t                    t          t           ddi                    S )Nr   c              3   X   K   | ]$}t                    t          |          k    V  %d S r   )r9   )r*   douts     r   rC   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"   )r*   rO   ks     r   r,   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>   s    ,C,C,CaQqT,C,C,Cr   Nr"   )r*   rS   
gather_maprJ   s    @r   rC   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-   T)r   r4   r5   r   r6   r:   all
ValueErrorr)   r   _maker8   r7   )rJ   rP   r    rT   rK   s   `@r   rT   zgather.<locals>.gather_map   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# 	P99??3z33M3M3M#N#NOOOtCyyZg)Cd)C)CDDEEEr   Nr"   )rJ   rK   r    r=   rT   s    `` @r   r   r   r   s`    "F F F F F F F&j!!

T
Jr>   ).)r   )collections.abcr   typingr   r   r   typing_extensionsr   r4   torch.nn.parallel._functionsr   r	   __all__FutureWarningboolr   r   r:   r/   r   r   r5   intdevicer
   strr   r   r"   r   r   <module>rb      s   $ $ $ $ $ $ ) ) ) ) ) ) ) ) ) ) ( ( ( ( ( (  8 8 8 8 8 8 8 8 2
1
1 I  s t   	 
      GCtU## 
 # #L##,-# 
# 5<	# # # 
# 
  #,- 
 
!W	   
* * * *b 	< <#s(O<cNT!< #,-< 
	<
 5c?E$sCx.#"5667< < < <(( (C (el(: ( (S ( ( ( ( ( (r   