o
    kih                     @   s  d dl mZ d dl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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jB  ded
eejdf fddZe	ddedeeejB  ded
ee fddZd ddZ	 d deedf deeef dB deeejB  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jB ded
efddZdS )!    )Sequence)AnyoverloadTypeVar)
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   j/var/www/addictedbytheproject.nl/epg/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	   !      r	   c                 C   r   r   r   r   r   r   r   r	   )   r    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diD S t  tr:t	 dkr:t
tt ddiS t  t
rTt	 dkrTdd tt ddiD S t  trrt	 dkrr fddtt  ddi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>strictFr   c                 S   s   g | ]}t |qS r   )listr$   ir   r   r   r&   E       c                    s   g | ]}t  |qS r   r"   r*   r   r   r   r&   G   r'   c                    s   g | ]} qS r   r   r$   _r   r   r   r&   L   s    )r   torchTensorr   applyr   zipmapr   lenr)   dictitemsr   r   scatter_mapr   r   r   r8   7   s   

zscatter.<locals>.scatter_mapNr   )r   r   r   resr   r7   r   r	   1   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   r-   r   r   r   	<genexpr>d       
z!scatter_kwargs.<locals>.<genexpr>c                 s   s    | ]}i V  qd S r   r   r-   r   r   r   r;   h   r<   )r	   r4   extendranger   )r   r:   r   r   scattered_inputsscattered_kwargsr   r   r   r
   Z   s   


r
   outputstarget_devicec                    r!   )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 rd S t  tr<t fddD s.tdt fdd D S t	 rOt 
ttddiS t ttddiS )Nr   c                 3   s     | ]}t  t |kV  qd S r   )r4   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   rC   kr   r   r&      r,   z8gather.<locals>.gather_map.<locals>.<genexpr>.<listcomp>Nr   )r$   )
gather_maprA   rF   r   r;      s   ( r(   T)r   r/   r0   r   r1   r5   all
ValueErrorr#   r   _maker3   r2   )rA   r   rH   rB   )rE   rA   r   rH      s   
zgather.<locals>.gather_mapNr   )rA   rB   r   r9   r   rL   r   r   n   s   
r   ).)r   )collections.abcr   typingr   r   r   typing_extensionsr   r/   torch.nn.parallel._functionsr   r   __all__FutureWarningboolr   r   r5   r)   r   r   r0   intdevicer	   strr
   r   r   r   r   r   <module>   sb   
-
"
&