U
    ‰dj  ã                
   @   sR  d dl Z d dlZd dlZd dlmZmZmZ dddddg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 | 	¡ }||krd dS | 
|¡ q$dS )NÚ_nccl_all_reducez)PyTorch is not compiled with NCCL supportFT)ÚhasattrÚtorchÚ_CÚwarningsÚwarnÚsetZ	is_sparseZis_contiguousZis_cudaZ
get_deviceÚadd)ZtensorsZdevicesZtensorZdevice© r   ú3/tmp/pip-unpacked-wheel-ua33x9lu/torch/cuda/nccl.pyÚis_available   s     
r   c                  C   s0   t j ¡ } | d? }| d? d@ }| d@ }|||fS )Né    é   iÿÿ  )r   r   Z_nccl_version)ÚverÚmajorÚminorÚpatchr   r   r   Úversion"   s
    
r   c                   C   s
   t j ¡ S ©N)r   r   Z_nccl_unique_idr   r   r   r   Ú	unique_id*   s    r   c                 C   s   t j | ||¡S r   )r   r   Z_nccl_init_rank)Z	num_ranksÚuidZ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    r(   c                 C   s4   t | ƒ |d kr| }t |ƒ tj | ||||¡ d S r   )r(   r   r   r
   ©r    ÚoutputsÚopÚstreamsÚcommsr   r   r   r   7   s
    )r*   )r    ÚoutputÚrootr+   r,   r*   r!   c                C   s’   t | ƒ |d k	r6|d k	r"tdƒ‚qxt d¡ || }nBt|tjƒsdt|tjj	ƒrdt d¡ || }n|d k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.)r(   Ú
ValueErrorr   r   r"   r   r&   r#   r$   r   r   Z_nccl_reduce)r    r.   r/   r+   r,   r-   r*   Ú_outputr   r   r   r   A   s"    ÿÿ
ÿ
)r    r/   r!   c                 C   s   t | ƒ tj | |||¡ d S r   )r(   r   r   Z_nccl_broadcast)r    r/   r,   r-   r   r   r   r   `   s    )r    r*   r!   c                 C   s&   t | ƒ t |ƒ tj | |||¡ d S r   )r(   r   r   Z_nccl_all_gather)r    r*   r,   r-   r   r   r   r   e   s    )r    r*   r+   r!   c                 C   s(   t | ƒ t |ƒ tj | ||||¡ d S r   )r(   r   r   Z_nccl_reduce_scatterr)   r   r   r   r	   k   s    )r   NN)NN)r#   r   Z
torch.cudar   Útypingr   r   r   Ú__all__ZSUMr   r   r   r   r&   r(   r   ÚintZcudaZStreamr   r   r   r	   r   r   r   r   Ú<module>   sH    ûú ú  ý
ý