U
    9%e                  
   @   sR  d dl Z d dlZd dlmZmZmZ d dl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   N/var/www/html/Darija-Ai-API/env/lib/python3.8/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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   9   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).znccl.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   C   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   j   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   q   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	   |   s    )r   NN)NN)r#   r   typingr   r   r   Z
torch.cudar   __all__ZSUMr   r   r   r   r&   r(   r   intcudaZStreamr   r   r   r	   r   r   r   r   <module>   sf    (      
  