ó
    Eñi>  ã                   óš  • S SK r S SKJr  S SKJr  S SKJrJr  \R                  4S jr	S \R                  4S jr
S \R                  4S jr\R                  \R                  4S jr\R                  \R                  4S jr\R                  4S	 jr\R                  4S
 jr\R                  4S jrSS\R                  4S jr\R                  \R                  4S jr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S  S!\5      rg)"é    N)ÚFunction)ÚgroupÚReduceOpc                 ó.   • [         R                  XU 5      $ )a�  
Broadcasts the tensor to the whole group.

``tensor`` must have the same number of elements in all processes
participating in the collective.

Arguments:
    tensor (Tensor): Data to be sent if ``src`` is the rank of current
        process.
    src (int): Source rank.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    Tensor: Received tensor from the broadcast op.

)Ú
_BroadcastÚapply)ÚtensorÚsrcr   s      Ú\/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/distributed/nn/functional.pyÚ	broadcastr      s   € ô" ×Ñ˜C¨Ó/Ð/ó    c                 ó.   • [         R                  XU 5      $ )a4  
Gathers a list of tensors in a single process.

Arguments:
    tensor (Tensor): Input tensor.
    dst (int, optional): Destination rank (default is 0).
    group (ProcessGroup, optional): The process group to work on.

Returns:
    tuple[Tensor]: List of appropriately-sized tensors with the gathered data.
)Ú_Gatherr   )r	   Údstr   s      r   Úgatherr       s   € ô �=‰=˜ VÓ,Ð,r   c                 ó0   • [         R                  " X/U Q76 $ )aÊ  
Scatters a list of tensors to all processes in a group.

Each process will receive exactly one tensor and store its data in the
``tensor`` argument.

Arguments:
    tensors (list[Tensor]): List of tensors to scatter on the source rank.
        Receivers must pass ``None`.
    src (int, optional): Source rank (default is 0).
    group (ProcessGroup, optional): The process group to work on.

Returns:
    Tensor: Output tensor from the scatter operation.

)Ú_Scatterr   )Útensorsr
   r   s      r   Úscatterr   /   s   € ô" �>Š>˜#Ð/ wÒ/Ð/r   c                 ó.   • [         R                  XX05      $ )aß  
Reduces the tensor data across all machines.

Only the process with rank ``dst`` is going to receive the final result.

Arguments:
    tensor (Tensor): Input of the collective.
    dst (int): Destination rank.
    op (optional): One of the values from
        ``torch.distributed.ReduceOp``
        enum.  Specifies an operation used for element-wise reductions.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    Tensor: Output of the collective.

)Ú_Reducer   )r	   r   Úopr   s       r   Úreducer   C   s   € ô$ �=‰=˜ %Ó0Ð0r   c                 ó2   • [         R                  " X#U /UQ76 $ )aÉ  
Reduces, then scatters a list of tensors to all processes in a group.

Arguments:
    output (Tensor): Output tensor.
    input_list (list[Tensor]): List of tensors to reduce and scatter.
    op (optional): One of the values from
        ``torch.distributed.ReduceOp``
        enum.  Specifies an operation used for element-wise reductions.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    Tensor: Output of the collective.

)Ú_Reduce_Scatterr   )ÚoutputÚ
input_listr   r   s       r   Úreduce_scatterr   X   s   € ô  × Ò  ¨FÐ@°ZÒ@Ð@r   c                 ó,   • [         R                  X5      $ )zû
Gathers tensors from the whole group in a list.

Arguments:
    tensor (Tensor): Tensor to be broadcast from current process.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    tuple([Tensor]): Output of the collective.

)Ú
_AllGatherr   )r	   r   s     r   Ú
all_gatherr!   k   s   € ô ×Ñ˜EÓ*Ð*r   c                 ó.   • [         R                  XU5      $ )av  
Single tensor all gather. Gathers a single tensor from all ranks, and puts them in a single output tensor.

Args:
    output_tensor (Tensor): Output tensor. It should contain
        correctly-sized tensors to be used for output of the collective.
    input_tensor (Tensor): Tensor to be broadcast from current process.
    group (ProcessGroup, optional): The process group to work on. If None,
        the default process group will be used.

Examples:
    >>> # All tensors below are of torch.int64 dtype.
    >>> # We have 2 process groups, 2 ranks.
    >>> # xdoctest: +SKIP("incorrect want text")
    >>> output_tensor = torch.zeros(2, dtype=torch.int64)
    >>> output_tensor
    [tensor([0, 0])] # Rank 0 and 1
    >>> tensor = torch.arange(1, dtype=torch.int64) + 1 + rank
    >>> tensor
    tensor([1]) # Rank 0
    tensor([2]) # Rank 1
    >>> dist.all_gather_base(output_tensor, tensor)
    >>> output_tensor
    tensor([1,2]) # Rank 0
    tensor([1,2]) # Rank 1

.. warning::
    `_all_gather_base` is experimental and subject to change.
    It is the caller's responsibility to ensure the output_tensor
    is correctly sized.

)Ú_AllGatherBaser   )Úoutput_tensorÚinput_tensorr   s      r   Ú_all_gather_baser&   z   s   € ôB ×Ñ ¸UÓCÐCr   c                 ó0   • [         R                  " X /UQ76 $ )a£  
Each process scatters list of input tensors to all processes in a group and return gathered list of tensors in output list.

Arguments:
    output_tensor_list (list[Tensor]): list of tensors to gather one per rank.
    input_tensor_list (list[Tensor]): List of tensors to scatter one per rank.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    tuple([Tensor]): Output of the collective.

)Ú	_AlltoAllr   )Úoutput_tensor_listÚinput_tensor_listr   s      r   Ú
all_to_allr+   ž   s   € ô �?Š?˜5ÐIÐ7HÒIÐIr   c                 ó0   • [         R                  X@X#U5      $ )aá  
Each process splits input tensor and then scatters the split list to all processes in a group.

Then concatenate the received tensors from all the processes in the group and return single output tensor.

Arguments:
    output (Tensor): Gathered concatenated output tensor.
    input (Tensor): Input tensor to scatter.
    output_split_sizes: (list[Int], optional): Output split sizes for dim 0
        if specified None or empty, dim 0 of ``output`` tensor must divide
        equally by ``world_size``.
    input_split_sizes: (list[Int], optional): Input split sizes for dim 0
        if specified None or empty, dim 0 of ``input`` tensor must divide
        equally by ``world_size``.

Returns:
    Tensor: Output of the collective.

)Ú_AlltoAllSingler   )r   ÚinputÚoutput_split_sizesÚinput_split_sizesr   s        r   Úall_to_all_singler1   ®   s   € ô4 × Ñ ØÐ)¸eóð r   c                 ó.   • [         R                  XU 5      $ )aö  
Reduces the tensor data across all machines in such a way that all get the final result.

After the call the returned tensor is going to be bitwise
identical in all processes.

Arguments:
    tensor (Tensor): Input of the collective.
    op (optional): One of the values from
        ``torch.distributed.ReduceOp``
        enum.  Specifies an operation used for element-wise reductions.
    group (ProcessGroup, optional): The process group to work on.

Returns:
    Tensor: Output of the collective

)Ú
_AllReducer   )r	   r   r   s      r   Ú
all_reducer4   Í   s   € ô$ ×Ñ˜B vÓ.Ð.r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r   éâ   c                 óš   • Xl         X l        [        R                  " US9U l        UR                  5       n[        R                  " X1US9  U$ ©N©r   )r
   r   ÚdistÚget_rankÚrankÚcloner   )Úctxr
   r   r	   s       r   ÚforwardÚ_Broadcast.forwardã   s>   € ð ŒØŒ	Ü—=’= uÑ-ˆŒð —‘“ˆÜ�Š�v¨%Ò0Øˆr   c                 óÔ   • [         R                  U R                  [        R                  U R
                  U5      nU R                  U R                  :w  a  UR                  5         S S U4$ ©N)r   r   r
   r   ÚSUMr   r<   Úzero_)r>   Úgrad_outputÚgxs      r   ÚbackwardÚ_Broadcast.backwardï   sJ   € ô �]‰]˜3Ÿ7™7¤H§L¡L°#·)±)¸[ÓIˆØ�7‰7�c—h‘hÓØ�H‰HŒJØ�d˜BÐÐr   © N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ústaticmethodr?   rG   Ú__static_attributes__rI   r   r   r   r   â   s(   † Øñó ðð ñ ó ó r   r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r   éø   c                 ód  • Xl         X l        [        [        R                  " US95       Vs/ s H  n[
        R                  " U5      PM     nnUR                  5       n[        R                  " US9U:X  a  [        R                  " X5XS9  O[        R                  " US XS9  [        U5      $ s  snf r8   )r   r   Úranger:   Úget_world_sizeÚtorchÚ
zeros_likeÚ
contiguousr;   r   Útuple)r>   r   r   r	   ÚiÚtensor_lists         r   r?   Ú_Gather.forwardù   s—   € ð ŒØŒ	ô /4´D×4GÒ4GÈeÑ4TÔ.Uó
Ú.U¨ŒE×Ò˜VÖ$Ñ.Uð 	ð 
ð ×"Ñ"Ó$ˆÜ�=Š=˜uÑ%¨Ó,Ü�KŠK˜¨SÓ>ä�KŠK˜  cÒ7Ü�[Ó!Ð!ùò
s   ¬ B-c                 ób   • S[         R                  " U R                  U R                  /UQ76 4-   $ ©N©NN)r   r   r   r   )r>   Úgrad_outputss     r   rG   Ú_Gather.backward  s(   € àœxŸ~š~¨c¯g©g°s·y±yÐPÀ<ÒPÐRÑRÐRr   rI   NrJ   rI   r   r   r   r   ø   s*   † Øñ"ó ð"ð$ ñSó óSr   r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r   i  c                 ó(  ^• Xl         X l        [        U4S jT 5       5      (       d   e[        R                  " TS   5      n[
        R                  " US9U:X  a!  [
        R                  " U[        T5      XS9  U$ [
        R                  " US XS9  U$ )Nc              3   ój   >#   • U  H(  oR                  5       TS    R                  5       :H  v •  M*     g7f)r   N)Úsize)Ú.0Útr   s     €r   Ú	<genexpr>Ú#_Scatter.forward.<locals>.<genexpr>  s%   øé € ÐBº'°Q—6‘6“8˜w q™zŸ™Ó0Ö0º'ùs   ƒ03r   r9   )	r
   r   ÚallrV   rW   r:   r;   r   Úlist)r>   r
   r   r   r   s      ` r   r?   Ú_Scatter.forward  s}   ø€ ð ŒØŒ	ÜÔB¹'ÓB×BÑBÐBÐBÜ×!Ò! '¨!¡*Ó-ˆÜ�=Š=˜uÑ%¨Ó,Ü�LŠL˜¤ g£°ÒAð ˆô �LŠL˜  sÒ8Øˆr   c                 ó^   • S[         R                  U R                  U R                  U5      -   $ r^   )r   r   r
   r   ©r>   rE   s     r   rG   Ú_Scatter.backward   s$   € ð œgŸm™m¨C¯G©G°S·Y±YÀÓLÑLÐLr   rI   NrJ   rI   r   r   r   r     s*   † Øñ	ó ð	ð ñMó óMr   r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r   i&  c                 óh   • Xl         X0l        UR                  5       n[        R                  " XAX#S9  U$ )N©r   r   )r
   r   r=   r:   r   )r>   r
   r   r   r	   s        r   r?   Ú_Reduce.forward'  s,   € ð ŒØŒ	Ø—‘“ˆÜ�Š�F BÒ4Øˆr   c                 ó`   • S[         R                  U R                  U R                  U5      4-   $ ©N)NNN)r   r   r
   r   rn   s     r   rG   Ú_Reduce.backward0  s)   € ð "¤Z×%5Ñ%5°c·g±g¸s¿y¹yÈ+Ó%VÐ$XÑXÐXr   rI   NrJ   rI   r   r   r   r   &  s*   † Øñó ðð ñYó óYr   r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r   i6  c                 ó”   • X l         UR                  5       n[        S U 5       5      n[        R                  " U[        U5      XS9  U$ )Nc              3   ó@   #   • U  H  oR                  5       v •  M     g 7frB   ©rX   ©rf   rg   s     r   rh   Ú*_Reduce_Scatter.forward.<locals>.<genexpr>=  s   é € Ð!LÒ:K°Q§,¡,§. .Ò:Kùó   ‚rr   )r   rX   rY   r:   r   rk   )r>   r   r   r	   r*   s        r   r?   Ú_Reduce_Scatter.forward7  sF   € ð Œ	à×"Ñ"Ó$ˆÜ!Ñ!LÑ:KÓ!LÓLÐÜ×Ò˜F¤DÐ):Ó$;ÀÒPØˆr   c                 óH   • S[         R                  U R                  U5      -   $ ru   )r    r   r   rn   s     r   rG   Ú_Reduce_Scatter.backwardA  s    € ð "¤J×$4Ñ$4°S·Y±YÀÓ$LÑLÐLr   rI   NrJ   rI   r   r   r   r   6  s*   † Øñó ðð ñMó óMr   r   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r    iG  c                 óú   • UR                  5       nXl        [        [        R                  " US95       Vs/ s H  n[
        R                  " U5      PM     nn[        R                  " XBUS9  [        U5      $ s  snf r8   )	rX   r   rT   r:   rU   rV   Ú
empty_liker!   rY   )r>   r   r	   Ú_Úout_tensor_lists        r   r?   Ú_AllGather.forwardH  so   € ð ×"Ñ"Ó$ˆàŒ	ä.3´D×4GÒ4GÈeÑ4TÔ.Uó
Ú.U¨ŒE×Ò˜VÖ$Ñ.Uð 	ð 
ô 	�Š˜°uÒ=Ü�_Ó%Ð%ùò
s   ¶ A8c                 ót  • [         R                  " U R                  S9[         R                  R                  [         R                  R
                  4;   ak  [         R                  " U R                  S9n[        R                  " X   5      n[        R                  " [        R                  U R                  U/UQ76 nS U4$ U Vs/ s H  n[        R                  " U5      PM     nn[        R                  " U R                  U/UQ76 n[        R                  " [        R                  " U5      SS9nS U4$ s  snf )Nr9   r   )Údim)r:   Úget_backendr   ÚBackendÚNCCLÚXCCLr;   rV   rƒ   r   r   r   rC   r(   ÚsumÚstack)r>   r`   r<   rF   r	   r[   Úgxss          r   rG   Ú_AllGather.backwardV  sé   € ä×Ò #§)¡)Ñ,´·±×1BÑ1BÄDÇLÁL×DUÑDUÐ0VÓVÜ—=’= s§y¡yÑ1ˆDÜ×!Ò! ,Ñ"4Ó5ˆBÜ ×&Ò&¤x§|¡|°S·Y±YÀÐRÀ\ÒRˆBð �bˆzÐñ COÓOÂ,¸œ5×+Ò+¨FÖ3Á,ˆKÐOÜ—/’/ #§)¡)¨[ÐH¸<ÒHˆCÜ—’œ5Ÿ;š; sÓ+°Ñ3ˆBØ�bˆzÐùò Ps   Ã D5rI   NrJ   rI   r   r   r    r    G  s(   † Øñ
&ó ð
&ð ñó ór   r    c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r#   ie  c                 óX   • X0l         [        R                  " XR                  5       US9  U$ r8   )r   r:   r&   rX   )r>   r$   r%   r   s       r   r?   Ú_AllGatherBase.forwardf  s(   € ð Œ	Ü×Ò˜m×-DÑ-DÓ-FÈeÒTØÐr   c                 ót  • [         R                  " U R                  S9[         R                  R                  [         R                  R
                  4;   aÕ  [         R                  " U R                  S9n[        UR                  5       5      nUS   U-  S:w  a  [        SU SU 35      eUS   [         R                  " U R                  S9-  US'   [        R                  " X1R                  UR                  S9n[         R                  " XA[        R                   U R                  5        O[        S5      eS US 4$ )Nr9   r   zTensor with dimensions: z8 does not have first dimension divisible by world_size: ©ÚdeviceÚdtypezBackend not supported!)r:   r‰   r   rŠ   r‹   rŒ   rU   rk   re   ÚRuntimeErrorrV   Úemptyr–   r—   Ú_reduce_scatter_baser   rC   )r>   rE   Ú
world_sizeÚout_sizerF   s        r   rG   Ú_AllGatherBase.backwardm  s  € ô ×Ò #§)¡)Ñ,´·±×1BÑ1BÄDÇLÁL×DUÑDUÐ0VÓVÜ×,Ò,°3·9±9Ñ=ˆJÜ˜K×,Ñ,Ó.Ó/ˆHØ˜‰{˜ZÑ'¨1Ó,Ü"Ø.¨x¨jð 9IØISÈðVóð ð # 1™+¬×)<Ò)<À3Ç9Á9Ñ)MÑMˆH�Q‰KÜ—’Ø×!3Ñ!3¸;×;LÑ;LñˆBô ×%Ò% b´x·|±|ÀSÇYÁYÕOäÐ7Ó8Ð8Ø�b˜$ÐÐr   rI   NrJ   rI   r   r   r#   r#   e  s(   † Øñó ðð
 ñ ó ó r   r#   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r(   i‚  c                 ó:  • Xl         [        [        R                  " US95       Vs/ s H  oCU   R	                  5       PM     snU l        [        R                  " US9n[        S U 5       5      n[        R                  " US9[        R                  R                  L aO  [        [        R                  " US95       H-  nS nXE:X  a  [        U5      n[        R                  " X$   XdUS9  M/     O[        R                  " U[        U5      US9  [        U5      $ s  snf )Nr9   c              3   ó@   #   • U  H  oR                  5       v •  M     g 7frB   rz   r{   s     r   rh   Ú$_AlltoAll.forward.<locals>.<genexpr>‹  s   é € Ð8²¨1Ÿ™Ÿ˜²ùr}   )r   rT   r:   rU   re   Úinput_tensor_size_listr;   rY   r‰   rŠ   ÚGLOOrk   r   r+   )r>   r   r…   r   rZ   Úmy_rankÚto_sends          r   r?   Ú_AlltoAll.forwardƒ  sé   € ð Œ	ä',¬T×-@Ò-@ÀuÑ-MÔ'Nó&
Ú'N !�A‰J�O‰OÖÑ'Nñ&
ˆÔ"ô —-’- eÑ,ˆÜÑ8±Ó8Ó8ˆä×Ò %Ñ(¬D¯L©L×,=Ñ,=Ò=Üœ4×.Ò.°UÑ;Ö<�Ø�Ø“<Ü" 7›m�GÜ—’˜_Ñ/°À5ÔIò	 =ô �OŠOØÜ�W“Øòô
 �_Ó%Ð%ùò%&
s   ¦Dc           	      óè   • U R                    Vs/ s H2  n[        R                  " X!S   R                  US   R                  S9PM4     nnS[
        R                  " U R                  U/UQ76 -   $ s  snf )Nr   r•   r_   )r¢   rV   r™   r–   r—   r(   r   r   )r>   r`   re   r[   s       r   rG   Ú_AlltoAll.backward›  sv   € ð ×2Ò2ó	
ò 3�ô �KŠKØ¨!™_×3Ñ3¸<È¹?×;PÑ;Pôñ 3ð	 	ð 
ð œiŸošo¨c¯i©i¸ÐTÀ|ÒTÑTÐTùò
s   �9A/rI   NrJ   rI   r   r   r(   r(   ‚  s*   † Øñ&ó ð&ð, ñUó óUr   r(   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r-   i¦  c                 ó„   • Xl         UR                  5       U l        X@l        X0l        [
        R                  " UUUUUS9  U$ )N)r/   r0   r   )r   re   Ú
input_sizer/   r0   r:   r1   )r>   r   r   r/   r0   r.   s         r   r?   Ú_AlltoAllSingle.forward§  sF   € ð Œ	ØŸ™›ˆŒØ!2ÔØ 2ÔÜ×ÒØØØ1Ø/Øò	
ð ˆr   c           	      óü   • [         R                  " U R                  UR                  UR                  S9nS[
        R                  U R                  UU R                  U R                  UR                  5       5      4-   $ )Nr•   )NNNN)rV   r™   r«   r–   r—   r-   r   r   r/   r0   rX   )r>   rE   r	   s      r   rG   Ú_AlltoAllSingle.backward·  ss   € ô —’Ø�N‰N ;×#5Ñ#5¸[×=NÑ=Nñ
ˆð (Ü×!Ñ!Ø—	‘	ØØ×&Ñ&Ø×%Ñ%Ø×&Ñ&Ó(óð+
ñ 
ð 	
r   rI   NrJ   rI   r   r   r-   r-   ¦  s(   † Øñó ðð ñ
ó ó
r   r-   c                   ó4   • \ rS rSr\S 5       r\S 5       rSrg)r3   iÈ  c                 ó‚   • X l         Xl        UR                  [        R                  S9n[
        R                  " X1US9  U$ )N)Úmemory_formatrr   )r   r   r=   rV   Úcontiguous_formatr:   r4   )r>   r   r   r	   s       r   r?   Ú_AllReduce.forwardÉ  s7   € ð Œ	ØŒØ—‘¬E×,CÑ,C�ÐDˆÜ�Š˜¨UÒ3Øˆr   c                 ó`   • S[         R                  U R                  U R                  U5      4-   $ r^   )r3   r   r   r   rn   s     r   rG   Ú_AllReduce.backwardÒ  s)   € ð œz×/Ñ/°·±¸¿	¹	À;ÓOÐQÑQÐQr   rI   NrJ   rI   r   r   r3   r3   È  s*   † Øñó ðð ñRó óRr   r3   )rV   Útorch.distributedÚdistributedr:   Útorch.autogradr   r   r   ÚWORLDr   r   r   rC   r   r   r!   r&   r+   r1   r4   r   r   r   r   r   r    r#   r(   r-   r3   rI   r   r   Ú<module>rº      sF  ðã Ý  Ý #÷
 .ð "'§¡ô 0ð(  §¡ô -ð  %§+¡+ô 0ð( $Ÿ<™<¨u¯{©{ô 1ð* +3¯,©,¸e¿k¹kô Að& #Ÿ[™[ô +ð 9>¿¹ô !DðH =B¿K¹Kô Jð& ØØ
�+‰+ôð> #Ÿ,™,¨e¯k©kô /ô* �ô  ô,Sˆhô Sô4Mˆxô Mô(Yˆhô Yô M�hô Mô"�ô ô< �Xô  ô:!U�ô !UôH
�hô 
ôDR�õ Rr   