a
    hw                     @   sv  d dl mZ d dl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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 )     )Sequence)AnyOptionaloverloadTypeVarUnion)
deprecatedN)GatherScatter)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   N/var/www/auris/lib/python3.9/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    r   c                 C   s   d S r   r   r    r   r   r   r   )   s    c                    s,    fddz| }W dnd0 |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>;       z0scatter.<locals>.scatter_map.<locals>.<listcomp>r   c                 S   s   g | ]}t |qS r   )listr#   ir   r   r   r%   ?   r&   c                    s   g | ]}t  |qS r   r!   r(   r   r   r   r%   A   r&   c                    s   g | ]} qS r   r   r#   _r   r   r   r%   B   r&   )r   torchTensorr
   applyr   zipmapr   lenr'   dictitemsr   r   scatter_mapr   r   r   r5   7   s     zscatter.<locals>.scatter_mapNr   )r   r   r   resr   r4   r   r   1   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>Z   s   z!scatter_kwargs.<locals>.<genexpr>c                 s   s   | ]
}i V  qd S r   r   r*   r   r   r   r8   ^   s   )r   r1   extendranger   )r   r7   r   r   Zscattered_inputsZscattered_kwargsr   r   r   r   P   s    


r   )outputstarget_devicer   r   c                    s,    fddz| }W dnd0 |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                    s   d  t  tjr(tjgR  S  d u r4d S t  trxt fddD s\t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   )r1   r#   d)outr   r   r8   |   r&   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%   ~   r&   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r#   )
gather_mapr;   r@   r   r8   ~   r&   )r   r,   r-   r	   r.   r2   all
ValueErrorr"   r   _maker0   r/   )r;   r   rB   r<   )r?   r;   r   rB   u   s    
zgather.<locals>.gather_mapNr   )r;   r<   r   r6   r   rF   r   r   d   s
    
r   ).).)r   )r   )r   )collections.abcr   typingr   r   r   r   r   Ztyping_extensionsr   r,   Ztorch.nn.parallel._functionsr	   r
   __all__FutureWarningboolr   r   r2   r'   r   r   r-   intZdevicer   strr   r   r   r   r   r   <module>   sL     
# 
"