a
    –¥»af  ã                
   @   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ÚsetZ	is_sparseZis_contiguousZis_cudaZ
get_deviceÚadd)ZtensorsZdevicesZtensorZdevice© r   úI/home/yuhw/wc/pochoir/venv/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   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ƒrt | tjƒr tdƒ‚d S )Nz(Inputs should be a collection of tensors)Ú
isinstanceÚcollectionsÚ	Containerr   ÚTensorÚ	TypeError)r    r   r   r   Ú_check_sequence_type2   s    r'   c                 C   s4   t | ƒ |d u r| }t |ƒ tj | ||||¡ d S r   )r'   r   r   r
   ©r    ÚoutputsÚopÚstreamsÚcommsr   r   r   r   7   s
    r   )r)   )r    ÚoutputÚrootr*   r+   r)   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.)r'   Ú
ValueErrorr   r   r"   r   r%   r#   Úabcr   r   Z_nccl_reduce)r    r-   r.   r*   r+   r,   r)   Ú_outputr   r   r   r   A   s"    ÿÿ
ÿ
r   )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)   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*   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	   )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>   sD    ûúú ý
ý