U
    Mh                     @   s|  d dl Z 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mZmZ dddgZed	ed
eedddZeedddZedeeeZe
de jee	ee jf  eee jdf dddZe
deee	ee jf  eee dddZdddZdeedf eeeef  ee	ee jf  eeeedf eeeef df f dddZdee	ee jf eedddZdS )    N)	AnyDictListOptionalSequenceTupleTypeVarUnionoverload)
deprecated   )ScatterGatherscatterscatter_kwargsgatherzC`is_namedtuple` is deprecated, please use the python checks instead)category)objreturnc                 C   s   t | S N)_is_namedtupler    r   R/var/www/html/venv/lib/python3.8/site-packages/torch/nn/parallel/scatter_gather.pyis_namedtuple
   s    r   c                 C   s   t | tot| dot| dS )N_asdict_fields)
isinstancetuplehasattrr   r   r   r   r      s    r   T.)inputstarget_gpusdimr   c                 C   s   d S r   r   r!   r"   r#   r   r   r   r      s    c                 C   s   d S r   r   r$   r   r   r   r   $   s    c                    s(    fddz| }W 5 dX |S )zSlice tensors into approximately equal chunks and distributes them across given GPUs.

    Duplicates references to objects that are not tensors.
    c                    s   t  tjrtd  S t r@ fddtt  D S t  trht	 dkrht
tt  S t  t
rt	 dkrdd tt  D S t  trt	 dkṙ fddtt   D S  fddD S )Nc                    s   g | ]}t  | qS r   type).0argsr   r   r   
<listcomp>1   s     z0scatter.<locals>.scatter_map.<locals>.<listcomp>r   c                 S   s   g | ]}t |qS r   )listr'   ir   r   r   r)   5   s     c                    s   g | ]}t  |qS r   r%   r+   r   r   r   r)   7   s     c                    s   g | ]} qS r   r   r'   _r   r   r   r)   8   s     )r   torchTensorr   applyr   zipmapr   lenr*   dictitemsr   r#   scatter_mapr"   r   r   r8   -   s     zscatter.<locals>.scatter_mapNr   )r!   r"   r#   resr   r7   r   r   (   s
    )r!   kwargsr"   r#   r   c                 C   s   | rt | ||ng }|r$t |||ng }t|t|k r^|dd tt|t| D  n4t|t| k r|dd tt|t| D  t|t|fS )z+Scatter with support for kwargs dictionary.c                 s   s   | ]
}d V  qdS )r   Nr   r-   r   r   r   	<genexpr>P   s     z!scatter_kwargs.<locals>.<genexpr>c                 s   s   | ]
}i V  qd S r   r   r-   r   r   r   r;   R   s     )r   r4   extendranger   )r!   r:   r"   r#   Zscattered_inputsZscattered_kwargsr   r   r   r   F   s    &$)outputstarget_devicer#   r   c                    s(    fddz| }W 5 dX |S )zuGather tensors from different GPUs on a specified device.

    Use 'cpu' for CPU to avoid a deprecation warning.
    c                    s   d  t  tjr&tjf S  d kr2d S t  trvt fddD sZtdt fdd D S t	 rt 
tt S t tt S )Nr   c                 3   s   | ]}t  t |kV  qd S r   )r4   r'   d)outr   r   r;   b   s     z-gather.<locals>.gather_map.<locals>.<genexpr>z+All dicts must have the same number of keysc                 3   s(   | ]    fd dD fV  qdS )c                    s   g | ]}|  qS r   r   r@   kr   r   r)   d   s     z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r'   )
gather_mapr>   rC   r   r;   d   s   )r   r/   r0   r   r1   r5   all
ValueErrorr&   r   _maker3   r2   )r>   r#   rE   r?   )rB   r>   r   rE   [   s    
zgather.<locals>.gather_mapNr   )r>   r?   r#   r9   r   rI   r   r   V   s
    ).).)r   )r   )r   ) r/   typingr   r   r   r   r   r   r   r	   r
   typing_extensionsr   Z
_functionsr   r   __all__FutureWarningboolr   r   r5   r*   r   r    r0   intZdevicer   strr   r   r   r   r   r   <module>   s>   ,
 *
" 
"