
    Ch	                    l    S SK Jr  S SKrS SKJr  S SKJr  \R                  " \	5      r
SSS jjrSS jrg)	    )annotationsN)loggingc                   [         R                  " 5       (       a  [         R                  " 5       (       a  U(       a*  [        R                  R
                  R                  U 5      nOr[         R                  " 5       n[        U5       Vs/ sH  n[        R                  " U 5      PM     nn[         R                  " X 5        [         R                  " 5       nXU'   [        R                  " USS9$ Sn[        R                  U5        U $ s  snf )aL  
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          ^/var/www/html/shao/venv/lib/python3.13/site-packages/sentence_transformers/util/distributed.pyr   r      s     t2244$0033>>vF,,.JBG
BSTBSQ 0 0 8BST OO,5 J+1Z(yy)q11	S 
  M#  Us   Dc                    [        U SS9$ )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)      s.    "    & 
		H	%$N.r   