o
    Sio                     @   s   d Z ddlZddlZdd Zdd Zdd Zejjj	fd	ej
fd
dZdd Zdejej
 fddZddejej
 defddZdS )zTorch distributed utilities.    Nc                   C      t j r
t j S dS )Nr   )torchdistributedis_initializedget_rank r   r   C/home/ubuntu/.local/lib/python3.10/site-packages/encodec/distrib.pyrank      

r	   c                   C   r   N   )r   r   r   get_world_sizer   r   r   r   
world_size   r
   r   c                   C   s
   t  dkS r   )r   r   r   r   r   is_distributed   s   
r   tensorc                 C   s   t  r
tj| |S d S N)r   r   r   
all_reduce)r   opr   r   r   r       s   r   c                 C   s   t | p	t | S r   )r   is_floating_point
is_complex)r   r   r   r   _is_complex_or_float%   s   r   paramsc                 C   sb   t  r| sd S tjt| g| d jtjd}t| | t| t  kr/t	dt|  dd S )Nr   )devicedtypez&Mismatch in number of params: ours is z*, at least one worker has a different one.)
r   r   r   lenr   longr   itemr   RuntimeError)r   r   r   r   r   _check_number_of_params)   s   
r   tensorssrcc                 C   sb   t  sdS dd | D } t|  g }| D ]}tjj|j|dd}|| q|D ]}|  q(dS )zBroadcast the tensors from the given parameters to all workers.
    This can be used to ensure that all workers have the same model to start with.
    Nc                 S   s   g | ]}t |r|qS r   )r   ).0r   r   r   r   
<listcomp>=   s    z%broadcast_tensors.<locals>.<listcomp>T)r    async_op)r   r   r   r   	broadcastdataappendwait)r   r    handlesr   handler   r   r   broadcast_tensors7   s   
r*   )r   )__doc__typingtpr   r	   r   r   r   ReduceOpSUMTensorr   r   Listr   Iterableintr*   r   r   r   r   <module>   s    