
    BNj
                    r    d dl mZ d dlZd dlmZ d dlmZ ddlm	Z	  ej                  e      Zdd	dZd
dZy)    )annotationsN)logging   )is_dist_initializedc                   t               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.)r   torchdistributednn
all_gatherdistget_world_sizerange
zeros_likeget_rankcatloggerwarning_once)tensor	with_gradgathered_tensors
world_size_
local_rankwarnings          w/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/sentence_transformers/util/distributed.pyr   r      s     $0033>>vF,,.JBG
BSTBSQ 0 0 8BST OO,f5 J+1Z(yy)q11	S 
  M#  Us   C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   4   s     f--    )F)r   torch.Tensorr   boolreturnr    )r   r    r"   r    )
__future__r   r	   torch.distributedr
   r   transformers.utilsr   environmentr   
get_logger__name__r   r   r    r   r   <module>r*      s3    "    & , 
		H	%$N.r   