o
    ,i                     @   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 g dZededd	ed
efddZd	ed
efd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dZe
ddedee	ee jf  ded
ee fddZd 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dZd dede	ee jf ded
ef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   ^/home/ubuntu/SoloSpeech/.venv/lib/python3.10/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dimc                 C      d S r   r   r!   r"   r#   r   r   r   r      s   r   c                 C   r$   r   r   r%   r   r   r   r   $   s   c                    *    fddz	| }W d|S dw )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r4t	 dkr4t
tt  S t  t
rKt	 dkrKdd tt  D S t  trft	 dkrf 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       z0scatter.<locals>.scatter_map.<locals>.<listcomp>r   c                 S   s   g | ]}t |qS r   )listr)   ir   r   r   r+   5       c                    s   g | ]}t  |qS r   r'   r.   r   r   r   r+   7   r,   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   r<   -   s    zscatter.<locals>.scatter_mapNr   )r!   r"   r#   resr   r;   r   r   (   s   
kwargsc                 C   s   | rt | ||ng }|rt |||ng }t|t|k r/|dd tt|t| D  nt|t| k rI|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   r1   r   r   r   	<genexpr>P       z!scatter_kwargs.<locals>.<genexpr>c                 s   s    | ]}i V  qd S r   r   r1   r   r   r   r?   R   r@   )r   r8   extendranger   )r!   r>   r"   r#   scattered_inputsscattered_kwargsr   r   r   r   F   s   &$r   outputstarget_devicec                    r&   )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gR  S  d u rd S t  tr<t fddD s.tdt fdd D S t	 rLt 
tt S t tt S )Nr   c                 3   s     | ]}t  t |kV  qd S r   )r8   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   rG   kr   r   r+   d   r0   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r)   )
gather_maprE   rJ   r   r?   d   s    ")r   r3   r4   r   r5   r9   all
ValueErrorr(   r   _maker7   r6   )rE   r#   rL   rF   )rI   rE   r   rL   [   s   
zgather.<locals>.gather_mapNr   )rE   rF   r#   r=   r   rP   r   r   V   s   
r   ).)r   ) r3   typingr   r   r   r   r   r   r   r	   r
   typing_extensionsr   
_functionsr   r   __all__FutureWarningboolr   r   r9   r-   r   r    r4   intdevicer   strr   r   r   r   r   r   <module>   sN   ,0
"
"
*