a
    NSicj  ã                
   @   sL  d dl Z d dlZd dlZd dlmZmZmZ g d¢Zd Z	dd„ Z
dd„ Zdd	„ Zd
d„ Zeejeej f ddœdd„Zde	ddfdd„Zdd e	ddfddœeej eeejeej f  eeeeejj  eeej  ddœdd„Zdeej eddœdd„Zdeej eej ddœdd„Ze	ddfeej eej eddœdd„ZdS ) é    N)ÚOptionalÚSequenceÚUnion)Ú
all_reduceÚreduceÚ	broadcastÚ
all_gatherÚreduce_scatterc                 C   st   t tjdƒst d¡ dS tƒ }| D ]J}|jr4 dS | ¡ sB dS |jsN dS | 	¡ }||v rd dS | 
|¡ q$dS )NÚ_nccl_all_reducez)PyTorch is not compiled with NCCL supportFT)ÚhasattrÚtorchÚ_CÚwarningsÚwarnÚsetÚ	is_sparseÚis_contiguousÚis_cudaÚ
get_deviceÚadd)ÚtensorsÚdevicesÚtensorÚdevice© r   úK/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/cuda/nccl.pyÚis_available   s     
r   c                  C   s0   t j ¡ } | d? }| d? d@ }| d@ }|||fS )Né    é   iÿÿ  )r   r   Ú_nccl_version)ÚverÚmajorÚminorÚpatchr   r   r   Úversion"   s
    
r$   c                   C   s
   t j ¡ S ©N)r   r   Ú_nccl_unique_idr   r   r   r   Ú	unique_id*   s    r'   c                 C   s   t j | ||¡S r%   )r   r   Ú_nccl_init_rank)Z	num_ranksÚuidÚrankr   r   r   Ú	init_rank.   s    r+   )ÚinputsÚreturnc                 C   s&   t | tjjƒrt | tjƒr"tdƒ‚d S )Nz(Inputs should be a collection of tensors)Ú
isinstanceÚcollectionsÚabcÚ	Containerr   ÚTensorÚ	TypeError)r,   r   r   r   Ú_check_sequence_type2   s    r4   c                 C   s4   t | ƒ |d u r| }t |ƒ tj | ||||¡ d S r%   )r4   r   r   r
   ©r,   ÚoutputsÚopÚstreamsÚcommsr   r   r   r   7   s
    r   )r6   )r,   ÚoutputÚrootr7   r8   r6   r-   c                C   s’   t | ƒ |d ur6|d ur"tdƒ‚qxt d¡ || }nBt|tjƒsdt|tjj	ƒrdt d¡ || }n|d u rt| | n|}tj
 | |||||¡ d S )Nzä'output' and 'outputs' can not be both specified. 'outputs' is deprecated in favor of 'output', taking in a single output tensor. The signature of reduce is: reduce(inputs, output=None, root=0, op=SUM, streams=None, comms=None).zƒnccl.reduce with an output tensor list is deprecated. Please specify a single output tensor with argument 'output' instead instead.z\nccl.reduce with an output tensor list is deprecated. Please specify a single output tensor.)r4   Ú
ValueErrorr   r   r.   r   r2   r/   r0   r   r   Ú_nccl_reduce)r,   r:   r;   r7   r8   r9   r6   Ú_outputr   r   r   r   A   s"    ÿÿ
ÿ
r   )r,   r;   r-   c                 C   s   t | ƒ tj | |||¡ d S r%   )r4   r   r   Ú_nccl_broadcast)r,   r;   r8   r9   r   r   r   r   `   s    r   )r,   r6   r-   c                 C   s&   t | ƒ t |ƒ tj | |||¡ d S r%   )r4   r   r   Ú_nccl_all_gather)r,   r6   r8   r9   r   r   r   r   e   s    r   )r,   r6   r7   r-   c                 C   s(   t | ƒ t |ƒ tj | ||||¡ d S r%   )r4   r   r   Ú_nccl_reduce_scatterr5   r   r   r   r	   k   s    r	   )r   NN)NN)r/   r   Ú
torch.cudar   Útypingr   r   r   Ú__all__ÚSUMr   r$   r'   r+   r2   r4   r   ÚintÚcudaÚStreamr   r   r   r	   r   r   r   r   Ú<module>   sD    ûúú ý
ý