o
    h                     @   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mZ d dlZg dZ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   )ScatterGather)scatterscatter_kwargsgatherobjreturnc                 C   s   t d t| S )NzAis_namedtuple is deprecated, please use the python checks instead)warningswarn_is_namedtupler    r   V/var/www/html/ai/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 Nr   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 )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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>-       z0scatter.<locals>.scatter_map.<locals>.<listcomp>r   c                 S   s   g | ]}t |qS r   )listr)   ir   r   r   r+   1       c                    s   g | ]}t  |qS r   r'   r.   r   r   r   r+   3   r,   c                    s   g | ]} qS r   r   r)   _r   r   r   r+   4   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 dictionaryc                 s   s    | ]}d V  qdS )r   Nr   r1   r   r   r   	<genexpr>L       z!scatter_kwargs.<locals>.<genexpr>c                 s   s    | ]}i V  qd S r$   r   r1   r   r   r   r?   N   r@   )r   r8   extendranger   )r    r>   r!   r"   scattered_inputsscattered_kwargsr   r   r   r   B   s   &$r   outputstarget_devicec                    r&   )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 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?   ^   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+   `   r0   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r)   )
gather_maprE   rJ   r   r?   `   s    ")r   r3   r4   r   r5   r9   all
ValueErrorr(   r   _maker7   r6   )rE   r"   rL   rF   )rI   rE   r   rL   W   s   
zgather.<locals>.gather_mapNr   )rE   rF   r"   r=   r   rP   r   r   R   s   
r   ).)r   )r3   typingr   r   r   r   r   r   r   r	   r
   
_functionsr   r   r   __all__boolr   r   r9   r-   r   r   r4   intdevicer   strr   r   r   r   r   r   <module>   sF    ,0
#
"
*