a
    ¤þdi  ã                   @   sZ   d dl Z ddlmZmZ d dlZg d¢Zdd„ Zdd„ Zdd	d
„Zddd„Z	ddd„Z
dS )é    Né   )ÚScatterÚGather)ÚscatterÚscatter_kwargsÚgatherc                 C   s   t  d¡ t| ƒS )NzAis_namedtuple is deprecated, please use the python checks instead)ÚwarningsÚwarnÚ_is_namedtuple©Úobj© r   úi/var/www/html/stable-diffusion-webui/venv/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
   c                    s,   ‡ ‡‡fdd„‰zˆ| ƒ}W d‰nd‰0 |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>   ó    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   #   r   c                    s   g | ]}ˆ ‘qS r   r   )r   Útargetsr   r   r   r   $   r   )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   r      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>7   r   z!scatter_kwargs.<locals>.<genexpr>c                 s   s   | ]
}i V  qd S ©Nr   r-   r   r   r   r/   9   r   )r   r$   ÚextendÚranger   )r+   Úkwargsr*   r(   r   r   r   r   2   s    &$r   c                    s,   ‡ ‡‡fdd„‰zˆ| ƒ}W d‰nd‰0 |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ˆˆgˆ¢R Ž S ˆ d u r4d S t ˆ tƒrxt‡ fdd„ˆD ƒƒs\t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 r0   )r$   ©r   Úd)Úoutr   r   r/   K   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   r4   ©Úkr   r   r   M   r   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r   )Ú
gather_mapÚoutputsr7   r   r/   M   s   ÿ)r   r   r    r   r!   r%   ÚallÚ
ValueErrorr   r
   Ú_maker#   r"   )r:   ©r(   r9   Útarget_device)r6   r:   r   r9   D   s    
ÿzgather.<locals>.gather_mapNr   )r:   r?   r(   r,   r   r>   r   r   ?   s
    
r   )r   )r   )r   )r   Z
_functionsr   r   r   Ú__all__r   r
   r   r   r   r   r   r   r   Ú<module>   s   

