U
    ‰d@  ã                   @   sB   d dl Z ddlmZmZ dd„ Zddd„Zddd	„Zdd
d„ZdS )é    Né   )ÚScatterÚGatherc                 C   s   t | tƒot| dƒot| dƒS )NÚ_asdictÚ_fields)Ú
isinstanceÚtupleÚhasattr©Úobj© r   úD/tmp/pip-unpacked-wheel-ua33x9lu/torch/nn/parallel/scatter_gather.pyÚis_namedtuple   s    ÿr   c                    s(   ‡ ‡‡fdd„‰zˆ| ƒ}W 5 d‰X |S )zŸ
    Slices 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
ƒr–t	ˆ ƒdkr–dd„ ttˆˆ ƒŽ D ƒS t ˆ tƒrÌt	ˆ ƒ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>   s     z0scatter.<locals>.scatter_map.<locals>.<listcomp>r   c                 S   s   g | ]}t |ƒ‘qS r   )Úlist©r   Úir   r   r   r      s     c                    s   g | ]}t ˆ ƒ|ƒ‘qS r   r   r   r
   r   r   r      s     c                    s   g | ]}ˆ ‘qS r   r   )r   Útargetsr
   r   r   r      s     )r   ÚtorchÚTensorr   Úapplyr   ÚzipÚmapr   Úlenr   ÚdictÚitemsr
   ©ÚdimÚscatter_mapÚtarget_gpusr
   r   r"      s     zscatter.<locals>.scatter_mapNr   )Úinputsr#   r!   Úresr   r    r   Úscatter   s
    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 dictionaryc                 s   s   | ]
}d V  qdS )r   Nr   ©r   Ú_r   r   r   Ú	<genexpr>/   s     z!scatter_kwargs.<locals>.<genexpr>c                 s   s   | ]
}i V  qd S ©Nr   r'   r   r   r   r)   1   s     )r&   r   ÚextendÚranger   )r$   Úkwargsr#   r!   r   r   r   Úscatter_kwargs*   s    &$r.   c                    s(   ‡ ‡‡fdd„‰zˆ| ƒ}W 5 d‰X |S )zz
    Gathers 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	ˆ ƒr–tˆ ƒ 
tˆtˆŽ ƒ¡S tˆ ƒtˆtˆŽ ƒƒS )Nr   c                 3   s   | ]}t ˆ ƒt |ƒkV  qd S r*   )r   ©r   Úd)Úoutr   r   r)   C   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   E   s     z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r   )Ú
gather_mapÚoutputsr2   r   r)   E   s   ÿ)r   r   r   r   r   r   ÚallÚ
ValueErrorr   r   Ú_maker   r   )r5   ©r!   r4   Útarget_device)r1   r5   r   r4   <   s    
ÿzgather.<locals>.gather_mapNr   )r5   r:   r!   r%   r   r9   r   Úgather7   s
    r;   )r   )r   )r   )r   Z
_functionsr   r   r   r&   r.   r;   r   r   r   r   Ú<module>   s
   

