a
    h	                     @  s^   d dl mZ d dlZd dlmZ d dlmZ ee	Z
ddddddd	Zddd
ddZdS )    )annotationsN)loggingFztorch.Tensorbool)tensor	with_gradreturnc                   s~   t  rlt  rl|r$tjj }n:t  } fddt|D }t |  t 	 } ||< tj
|ddS d}t|  S )ap  
    Gathers a tensor from each distributed rank into a list. Always retains gradients for the local rank's tensor,
    and optionally retains gradients for the gathered tensors if `with_grad` is True.

    Args:
        tensor (torch.Tensor): The tensor to gather from each rank.
        with_grad (bool, optional): If True, the local rank's tensor retains its gradients. Defaults to False.

    Returns:
        torch.Tensor: A tensor containing the gathered tensors from all ranks, concatenated along the first dimension.
        If torch.distributed is not available or not initialized, returns the original tensor.
    c                   s   g | ]}t  qS  )torchZ
zeros_like).0_r   r   b/var/www/html/assistant/venv/lib/python3.9/site-packages/sentence_transformers/util/distributed.py
<listcomp>       zall_gather.<locals>.<listcomp>r   )dimzTrying to gather while torch.distributed is not available or has not been initialized, returning the original (local) tensor. This is expected if you are only using one GPU; consider not using gathering to remove this warning.)distZis_availableZis_initializedr	   distributednn
all_gatherZget_world_sizerangeZget_rankcatloggerZwarning_once)r   r   Zgathered_tensorsZ
world_sizeZ
local_rankwarningr   r   r   r      s    
r   )r   r   c                 C  s   t | ddS )a  
    Gathers a tensor from each distributed rank into a list, retaining gradients for the local rank's tensor.

    Args:
        tensor (torch.Tensor): The tensor to gather from each rank.

    Returns:
        torch.Tensor: A tensor containing the gathered tensors from all ranks, concatenated along the first dimension.
        If torch.distributed is not available or not initialized, returns the original tensor.
    T)r   )r   r   r   r   r   all_gather_with_grad2   s    r   )F)
__future__r   r	   Ztorch.distributedr   r   Ztransformers.utilsr   Z
get_logger__name__r   r   r   r   r   r   r   <module>   s   
'