
    rh	                    f    d dl mZ d dlZd dlmZ d dlmZ  ej                  e	      Z
dddZddZy)	    )annotationsN)loggingc                   t        j                         rt        j                         r|r*t        j                  j
                  j                  |       }nnt        j                         }t        |      D cg c]  }t        j                  |        }}t        j                  ||        t        j                         }| ||<   t        j                  |d      S d}t        j                  |       | S c c}w )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.
    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.)distis_availableis_initializedtorchdistributednn
all_gatherget_world_sizerange
zeros_likeget_rankcatloggerwarning_once)tensor	with_gradgathered_tensors
world_size_
local_rankwarnings          y/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/util/distributed.pyr   r      s     t224$0033>>vF,,.JBG
BSTQ 0 0 8TT OO,f5 J+1Z(yy)q11	S 
  M#  Us   6C3c                    t        | 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   s    r   all_gather_with_gradr   2   s     f--    )F)r   torch.Tensorr   boolreturnr    )r   r    r"   r    )
__future__r   r
   torch.distributedr   r   transformers.utilsr   
get_logger__name__r   r   r    r   r   <module>r)      s0    "    & 
		H	%$N.r   