a
    Þñh  ã                
   @   sT  d dl Z 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   úO/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/cuda/nccl.pyÚis_available   s     
r   c                   C   s
   t j ¡ S ©N)r   r   Z_nccl_versionr   r   r   r   Úversion#   s    r   c                   C   s
   t j ¡ S r   )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_type/   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   4   s
    r   )r#   )r   ÚoutputÚrootr$   r%   r#   r   c                C   s”   t | ƒ |d ur6|d ur"tdƒ‚qzt d¡ || }nDt|tjƒsft|tjjj	ƒrft d¡ || }n|d u rv| | 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   Z_sixZcontainer_abcsr   r   Z_nccl_reduce)r   r'   r(   r$   r%   r&   r#   Ú_outputr   r   r   r   >   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   b   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	   h   s    r	   )r   NN)NN)r   r   Z
torch._sixr   Z
torch.cudaÚtypingr   r   r   Ú__all__ZSUMr   r   r   r   r   r!   r   ÚintZcudaZStreamr   r   r   r	   r   r   r   r   Ú<module>   sF    ûúú ý
ý