§
    +�’jš  ã                  óŒ   — 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„Zdd	„Zddd„Zdd„Z	 ddd„ZdS )é    )ÚannotationsN)Úloggingé   )Úis_dist_initializedÚreturnÚintc                 óH   — t          ¦   «         rt          j        ¦   «         S dS )zXThe rank of the current process in the distributed group, or ``0`` when not distributed.r   )r   ÚdistÚget_rank© ó    úTC:\Projects\DSCons\.venv\Lib\site-packages\sentence_transformers\util\distributed.pyr   r      s!   € åÑÔð ÝŒ}‰ŒÐØˆ1r   c                 óH   — t          ¦   «         rt          j        ¦   «         S dS )zPThe number of processes in the distributed group, or ``1`` when not distributed.r   )r   r
   Úget_world_sizer   r   r   r   r      s$   € åÑÔð %ÝÔ"Ñ$Ô$Ð$Øˆ1r   FÚtensorútorch.TensorÚ	with_gradÚboolc                óŒ  ‡ — t          ¦   «         r˜|r%t          j        j                             ‰ ¦  «        }n[t          j        ¦   «         }ˆ fd„t          |¦  «        D ¦   «         }t          j        |‰ ¦  «         t          j        ¦   «         }‰ ||<   t          j	        |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                ó8   •— g | ]}t          j        ‰¦  «        ‘ŒS r   )ÚtorchÚ
zeros_like)Ú.0Ú_r   s     €r   ú
<listcomp>zall_gather.<locals>.<listcomp>.   s$   ø€ ÐTÐTÐT¸Q¥Ô 0°Ñ 8Ô 8ÐTÐTÐTr   r   )ÚdimzâTrying 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   r   ÚdistributedÚnnÚ
all_gatherr
   r   Úranger   ÚcatÚloggerÚwarning_once)r   r   Úgathered_tensorsÚ
world_sizeÚ
local_rankÚwarnings   `     r   r   r      sÑ   ø€ õ ÑÔð 2Øð 	2Ý$Ô0Ô3×>Ò>¸vÑFÔFÐÐåÔ,Ñ.Ô.ˆJØTÐTÐTÐTÅ%È
ÑBSÔBSÐTÑTÔTÐõ ŒOÐ,¨fÑ5Ô5Ð5õ œ™œˆJØ+1Ð˜ZÑ(ÝŒyÐ)¨qÐ1Ñ1Ô1Ð1ð	Sð õ
 ×Ò˜Ñ Ô Ð Ø€Mr   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*   B   s   € õ �f¨Ð-Ñ-Ô-Ð-r   Úmaskú!tuple[torch.Tensor, torch.Tensor]c           
     ór  — t          ¦   «         �r	t          j        |                      d¦  «        | j        ¬¦  «        }t          j        |t
          j        j        ¬¦  «         t          | 
                    ¦   «         ¦  «        }|                      d¦  «        |k     r|t          j        j                             | ddd||                      d¦  «        z
  f¦  «        } t          j        j                             |d||                     d¦  «        z
  f¦  «        }t          | |¬¦  «        t          |¦  «        fS )af  All-gather a ``(B, T, D)`` token-embedding tensor and its ``(B, T)`` mask across ranks, padding
    the token axis to the cross-rank max ``T`` first.

    ``all_gather`` requires every rank to contribute an identically-shaped tensor, but multi-vector
    batches pad each column to its own per-rank batch-longest ``T``, which differs across ranks. This
    reduces the global max ``T``, pads both the embeddings and the mask up to it (keeping them
    aligned), then gathers. Embeddings are gathered with ``with_grad``. The mask never carries a
    gradient.

    Args:
        tensor (torch.Tensor): ``(B, T, D)`` token embeddings to gather.
        mask (torch.Tensor): ``(B, T)`` mask to gather, padded with ``False`` to match ``tensor``.
        with_grad (bool, optional): Retain gradients for the embeddings (see :func:`all_gather`).

    Returns:
        tuple[torch.Tensor, torch.Tensor]: Gathered ``(sum(B), T_max, D)`` embeddings and
        ``(sum(B), T_max)`` mask. Without an initialised process group this only forwards to
        :func:`all_gather` (no padding needed, all_gather returns the local tensor).
    r   )Údevice)Úopr   r)   )r   r   r   Úsizer.   r
   Ú
all_reduceÚReduceOpÚMAXr   Úitemr   Ú
functionalÚpadr   )r   r+   r   Ú	local_maxÚT_maxs        r   Úall_gather_paddedr9   P   sú   € õ, ÑÔñ LÝ”L §¢¨Q¡¤¸¼ÐFÑFÔFˆ	ÝŒ˜	¥d¤mÔ&7Ð8Ñ8Ô8Ð8Ý�I—N’NÑ$Ô$Ñ%Ô%ˆØ�;Š;�q‰>Œ>˜EÒ!Ð!Ý”XÔ(×,Ò,¨V°a¸¸A¸uÀvÇ{Â{ÐSTÁ~Ä~Ñ?UÐ5VÑWÔWˆFÝ”8Ô&×*Ò*¨4°!°U¸T¿YºYÀq¹\¼\Ñ5IÐ1JÑKÔKˆDÝ�f¨	Ð2Ñ2Ô2µJ¸tÑ4DÔ4DÐDÐDr   )r   r   )F)r   r   r   r   r   r   )r   r   r   r   )r   r   r+   r   r   r   r   r,   )Ú
__future__r   r   Útorch.distributedr   r
   Útransformers.utilsr   Úenvironmentr   Ú
get_loggerÚ__name__r"   r   r   r   r*   r9   r   r   r   ú<module>r@      sñ   ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø &Ð &Ð &Ð &Ð &Ð &à ,Ð ,Ð ,Ð ,Ð ,Ð ,ð 
ˆÔ	˜HÑ	%Ô	%€ðð ð ð ðð ð ð ð$ð $ð $ð $ð $ðN.ð .ð .ð .ð AFðEð Eð Eð Eð Eð Eð Er   