§
    Ÿ²Åj~°  ã                   óê  — d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZ d dlZd dlmZ d dlmc mZ d dlmZ d dlmZ ddlmZ 	 d dlmZ n# e$ r	 d dlmZ Y nw xY w ej        ¦   «         rd„ Zn&	 d d	l m!Z n# e"$ r  ej#        d
¦  «         d„ ZY nw xY w	 	 	 eee$         eee$                  ej%        eede$f         e&f         Z'	 d„ Z(dsdej)        de$de'de&fd„Z*dsdej)        de&de'de&fd„Z+	 dsdej)        de$de'de&fd„Z,	 dsdej)        de$de'de&fd„Z-	 dsdej)        de&de$de'de&f
d„Z.	 dsdej)        de&de$de'de&f
d„Z/	 dsdeej)                 de&de'de&deej)                 f
d„Z0	 dsdeej)                 de'de&deej)                 fd„Z1	 dsdeej)                 de&dee$         de'de&deej)                 fd „Z2d!„ Z3	 dsdej)        d"eee$                  d#eee$                  de'de&dej)        fd$„Z4	 dsdej)        d"eee$                  d#eee$                  de'de&dej)        fd%„Z5	 dsdej)        d&ee$         de'de&dej)        f
d'„Z6 G d(„ d)ej)        ¦  «        Z7	 dsde'de&dee&ee$         e$f         fd*„Z8dsde'de&de&fd+„Z9 G d,„ d-ej:        j;        ¦  «        Z<de=fd.„Z>dej)        fd/„Z?e j@        dtd1e=fd2„¦   «         ZAd3„ ZBd4„ ZCd5„ ZDd6„ ZEd7„ ZFd8„ ZGd9„ ZHd:„ ZId;„ ZJd<„ ZKd=„ ZLd>„ ZMd?„ ZNd@„ ZOdA„ ZPdB„ ZQdC„ ZR ej        ¦   «         �sXejS         T                    dDdE¦  «        ZUeU V                    dFeDdG¦  «         eU V                    dHeIdG¦  «         eU V                    dIeHdG¦  «         eU V                    dJeKdG¦  «         eU V                    dKeEdG¦  «         eU V                    dLeNdG¦  «         eU V                    dMeOdG¦  «         eU V                    dNePdG¦  «         eU V                    dOeQdG¦  «         eU V                    dPeRdG¦  «         eU V                    dQeMdG¦  «         eU V                    dReCdG¦  «         eU V                    dSeJdG¦  «         ejW        jX         Y                    ejZ        j[        j(        j\        ¦  «         ejW        jX         Y                    ejZ        j[        j(        ¦  «         ejS         T                    dTdU¦  «        Z]ejS         T                    dTdE¦  «        Z^g dV¢Z_ej`        ea         Zbe_D ]fZcecd ec d                    dW¦  «        …         Ze efedXee› �¦  «        Zge] h                    eceji        jj        ¬Y¦  «         e^ V                    eeegdZ¦  «         Œgn ej#        d[¦  «         	 	 	 	 dud]ej)        d^ej)        d_e=de&de$f
d`„Zk	 	 	 	 	 dvdbej)        dcej)        dde&d_e=de$de&fde„Zlejm        jn        daejm        jo        dfejm        jp        dgejm        jq        dhejm        jr        diejm        js        djejm        jt        dkejm        ju        dliZv	 	 	 	 dwdmej)        dde&d_e=de&fdn„Zw	 	 	 	 	 dxdbej)        dcej)        de&fdo„Zx	 	 	 dydpeej)                 dmej)        de&fdq„Zyd drlmzZ{m|Z}m~Zm€Z�m+Z‚m4Zƒm.Z„ e�eke„ele‚eweƒexeeye}ele{ekiZ…dS )zé    N)ÚAnyÚcastÚListÚOptionalÚTupleÚTypeÚTYPE_CHECKINGÚUnion)Ú
DeviceMesh)Úget_proxy_modeé   )Ú_functional_collectives_impl)Útree_map_onlyc                  ó   — dS )z9Can't import torchdynamo in torchdeploy builds currently.F© r   ó    úZC:\Projects\Historical\venv\Lib\site-packages\torch/distributed/_functional_collectives.pyÚis_torchdynamo_compilingr      s   € àˆur   )Úis_dynamo_compilingzdUnable to import torchdynamo util `is_torchdynamo_compiling`, so won't support torchdynamo correctlyc                  ó   — dS )NFr   r   r   r   r   r   $   s   € Ø�5r   zdist.tensor.DeviceMeshc                 óJ   — t           j        j                             | ¦  «        S )z¡
    Wait on a tensor returned by the collectives ops.

    Waiting follows device semantics, which means blocking on CPU and synchronizing streams on CUDA.
    )ÚtorchÚopsÚ_c10d_functionalÚwait_tensor)Útensors    r   r   r   ‡   s   € õ Œ9Ô%×1Ò1°&Ñ9Ô9Ð9r   Ú ÚselfÚsrcÚgroupÚtagc                 óŒ   — t          ||¦  «        }t          j        j                             | ||¦  «        }t          |¦  «        S )a  
    Broadcasts the tensor to all processes in the given process group.

    Args:
        src (int): Source rank
        group (ProcessGroup or List[int]): The process group to work on.
        tag (str, optional): A unique identifier for the collective. Default: empty string
    )Ú_resolve_group_namer   r   r   Ú	broadcastÚ_maybe_wrap_tensor)r   r   r    r!   Ú
group_namer   s         r   r$   r$   �   s=   € õ % U¨CÑ0Ô0€JÝŒYÔ'×1Ò1°$¸¸ZÑHÔH€FÝ˜fÑ%Ô%Ð%r   ÚreduceOpc                 ó°   — t          ||¦  «        }t          j        j                             | |                     ¦   «         |¦  «        }t          |¦  «        S )aø  
    Reduces the tensor data across all machines in such a way that all get
    the final result.

    The input tensor is left unmodified.

    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    )r#   r   r   r   Ú
all_reduceÚlowerr%   )r   r'   r    r!   r&   r   s         r   r)   r)   ž   sG   € õ" % U¨CÑ0Ô0€JÝŒYÔ'×2Ò2°4¸¿ºÑ9IÔ9IÈ:ÑVÔV€FÝ˜fÑ%Ô%Ð%r   Ú
gather_dimc                 ó˜  — |                       ¦   «         sJ ‚t          ||¦  «        }t          j        |¦  «        }t          j        j                             | ||¦  «        }t          |¦  «        }|dk    rTt          |t          ¦  «        r|                     ¦   «         }t	          j        t	          j        ||d¬¦  «        |¬¦  «        }|S )a%  
    Gather tensor data across from all machines and concatenate over ``gather_dim``.

    Note that it currently only supports gather_dim = 0.

    The input tensor is left unmodified.
    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    r   ©Údim)Úis_contiguousr#   Úc10dÚ_get_group_size_by_namer   r   r   Úall_gather_into_tensorr%   Ú
isinstanceÚAsyncCollectiveTensorÚwaitÚcatÚchunk©r   r+   r    r!   r&   Ú
group_sizer   Úress           r   Úall_gather_tensorr;   ´   sÀ   € ð, ×ÒÑÔÐÐÐÝ$ U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€JÝŒYÔ'×>Ò>Øˆj˜*ñô €Fõ ˜VÑ
$Ô
$€Cà�Q‚€õ �cÕ0Ñ1Ô1ð 	Ø—(’(‘*”*ˆCÝŒi�œ C¨¸Ð;Ñ;Ô;ÀÐLÑLÔLˆØ€Jr   c                 ó‚  — t          ||¦  «        }t          j        |¦  «        }t          j        j                             | ||¦  «        }t                               |¦  «        }|dk    rTt          |t          ¦  «        r|                     ¦   «         }t          j        t          j        ||d¬¦  «        |¬¦  «        }|S )a<  
    Gather tensor data across from all machines and concatenate over ``gather_dim``.

    Note that it currently only supports gather_dim = 0.

    This function is the same as all_gather_tensor but will propagate the
    backwards gradient across workers.

    See all_gather_tensor for more details on usage.
    r   r-   )r#   r0   r1   r   r   Ú_c10d_functional_autogradr2   Ú_FromTorchTensorÚapplyr3   r4   r5   r6   r7   r8   s           r   Úall_gather_tensor_autogradr@   Û   s®   € õ  % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€JåŒYÔ0×GÒGØˆj˜*ñô €Fõ ×
 Ò
  Ñ
(Ô
(€Cà�Q‚€õ �cÕ0Ñ1Ô1ð 	Ø—(’(‘*”*ˆCÝŒi�œ C¨¸Ð;Ñ;Ô;ÀÐLÑLÔLˆØ€Jr   Úscatter_dimc                 ó¾  — t          ||¦  «        }t          j        |¦  «        }|                      |¦  «        |z  dk    s#J d|                      d¦  «        › d|› �¦   «         ‚|dk    r+t	          j        | ||¬¦  «        }t	          j        |¦  «        } t          j        j         	                    | | 
                    ¦   «         ||¦  «        }t          |¦  «        }	|	S )a(  
    Reduces the tensor data across all machines in such a way that all get
    the final result, then scatter the results to corresponding ranks.


    The input tensor is left unmodified.
    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh
    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    r   úinput dimension 0 (ú" must be a multiple of group_size r-   )r#   r0   r1   Úsizer   r7   r6   r   r   Úreduce_scatter_tensorr*   r%   ©
r   r'   rA   r    r!   r&   r9   Útensor_listr   r:   s
             r   rF   rF   ü   sà   € õ, % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€Jð 	�	Š	�+ÑÔ Ñ+¨qÒ0Ð0Ð0ØY˜TŸYšY q™\œ\ÐYÐYÈZÐYÐYñ 	1Ô0Ð0à�aÒÐÝ”k $¨
¸ÐDÑDÔDˆÝŒy˜Ñ%Ô%ˆåŒYÔ'×=Ò=ØØ�ŠÑÔØØñ	ô €Fõ ˜VÑ
$Ô
$€CØ€Jr   c                 óÔ  — t          ||¦  «        }t          j        |¦  «        }|                      |¦  «        |z  dk    s#J d|                      d¦  «        › d|› �¦   «         ‚|dk    r+t	          j        | ||¬¦  «        }t	          j        |¦  «        } t          j        j         	                    | | 
                    ¦   «         ||¦  «        }t                               |¦  «        }	|	S )a|  
    Reduces the tensor data across all machines in such a way that all get
    the final result, then scatter the results to corresponding ranks.

    This function is the same as reduce_scatter_tensor but will propagate the
    backwards gradient across workers.

    Currently only the "sum" reduceOp is supported.

    See reduce_scatter_tensor for more details on usage.
    r   rC   rD   r-   )r#   r0   r1   rE   r   r7   r6   r   r=   rF   r*   r>   r?   rG   s
             r   Úreduce_scatter_tensor_autogradrJ   &  sæ   € õ& % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€Jð 	�	Š	�+ÑÔ Ñ+¨qÒ0Ð0Ð0ØY˜TŸYšY q™\œ\ÐYÐYÈZÐYÐYñ 	1Ô0Ð0à�aÒÐÝ”k $¨
¸ÐDÑDÔDˆÝŒy˜Ñ%Ô%ˆåŒYÔ0×FÒFØØ�ŠÑÔØØñ	ô €Fõ ×
 Ò
  Ñ
(Ô
(€CØ€Jr   Úreturnc                 óÖ   — t          ||¦  «        }t          j        j                             | |                     ¦   «         |¦  «        }t          t          t          |¦  «        ¦  «        S )a  
    Reduces a list of tensors across all machines in such a way that all get
    the final result.

    The all tensors in the input list are left unmodified.

    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    )	r#   r   r   r   Úall_reduce_coalescedr*   ÚlistÚmapr%   )r   r'   r    r!   r&   rH   s         r   rM   rM   M  s[   € õ& % U¨CÑ0Ô0€JÝ”)Ô,×AÒAØØ�ŠÑÔØñô €Kõ
 •Õ&¨Ñ4Ô4Ñ5Ô5Ð5r   c                 óÚ   — t          ||¦  «        }t          j        |¦  «        }t          j        j                             | ||¦  «        }t          t          t          |¦  «        ¦  «        S )a  
    Gather a list of tensors across from all machines.

    Note that it currently only supports gather_dim = 0.

    The input tensor is left unmodified.
    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    )
r#   r0   r1   r   r   r   Ú all_gather_into_tensor_coalescedrN   rO   r%   )r   r    r!   r&   r9   rH   s         r   rQ   rQ   i  sa   € õ& % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€JÝ”)Ô,×MÒMØØØñô €Kõ
 •Õ&¨Ñ4Ô4Ñ5Ô5Ð5r   Úinputsc           
      ó‚  — t          ||¦  «        }t          j        |¦  «        }t          |¦  «        t          | ¦  «        k    sJ ‚t	          t          || ¦  «        ¦  «        D ]�\  }\  }}	|	                     |¦  «        |z  dk    s)J d|› d|	                     |¦  «        › d|› d|› �¦   «         ‚|dk    r.t          j        |	||¬¦  «        }
t          j	        |
¦  «        | |<   Œ‚t          j
        j                             | |                     ¦   «         ||¦  «        }
t          t          t           |
¦  «        ¦  «        S )a,  
    Reduces a list of tensors across all machines in such a way that all get
    the final result, then scatter the results to corresponding ranks.

    The input tensors are left unmodified.
    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    r   zinput dimension z (rD   z for tensor at index r-   )r#   r0   r1   ÚlenÚ	enumerateÚziprE   r   r7   r6   r   r   Úreduce_scatter_tensor_coalescedr*   rN   rO   r%   )rR   r'   rA   r    r!   r&   r9   Úidxr.   r   rH   s              r   rW   rW   †  sE  € õ, % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€Jåˆ{ÑÔ�s 6™{œ{Ò*Ð*Ð*Ð*Ý'­¨K¸Ñ(@Ô(@ÑAÔAð 1ð 1Ñˆ‰]ˆc�6à�KŠK˜ÑÔ˜zÑ)¨QÒ.Ð.Ð.Ø˜cÐÐ V§[¢[°Ñ%5Ô%5ÐÐÐYcÐÐÐz}ÐÐñ /Ô.Ð.à�!Š8ˆ8Ýœ+ f¨j¸cÐBÑBÔBˆKÝœ) KÑ0Ô0ˆF�3‰Køå”)Ô,×LÒLØØ�ŠÑÔØØñ	ô €Kõ •Õ&¨Ñ4Ô4Ñ5Ô5Ð5r   c                 óÌ   — t          | t          j        j        ¦  «        sJ ‚| j        }t          |j        ¦  «        dk    r#|j        d         }|j        d uo|j        j         S d S ©Nr   )	r3   r   Ú_opsÚ
OpOverloadÚ_schemarT   Ú	argumentsÚ
alias_infoÚis_write)ÚtgtÚschemaÚ	first_args      r   Ú_is_view_oprd   ´  sk   € Ý�c�5œ:Ô0Ñ1Ô1Ð1Ð1Ð1ØŒ[€FÝ
ˆ6ÔÑÔ˜qÒ Ð ØÔ$ QÔ'ˆ	àÔ#¨4Ð/ÐU¸	Ô8LÔ8UÐ4UÐUð !Ð r   Úoutput_split_sizesÚinput_split_sizesc                 óš  — |�#t          d„ |D ¦   «         ¦  «        s
J |¦   «         ‚|�#t          d„ |D ¦   «         ¦  «        s
J |¦   «         ‚t          ||¦  «        }t          j        |¦  «        }|�|€$|€|�
J d¦   «         ‚| j        d         |z  g|z  }|}t
          j        j                             | |||¦  «        }t          |¦  «        S )aC  
    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.

    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one dimension of the DeviceMesh

    :: N.B. If you pass a PG or a 1D list to perform a MPMD collective, the compiler won't be able to recover
    that information and perform collective algebraic optimization. Use other forms of input for that.
    Nc              3   óX   K  — | ]%}t          |t          t          j        f¦  «        V — Œ&d S ©N©r3   Úintr   ÚSymInt©Ú.0rE   s     r   ú	<genexpr>z$all_to_all_single.<locals>.<genexpr>Ô  óD   è è € ð 
ð 
Ø6:�J�t�c¥5¤<Ð0Ñ1Ô1ð
ð 
ð 
ð 
ð 
ð 
r   c              3   óX   K  — | ]%}t          |t          t          j        f¦  «        V — Œ&d S ri   rj   rm   s     r   ro   z$all_to_all_single.<locals>.<genexpr>Ø  rp   r   ú^output_split_sizes and input_split_sizes must either be specified together or both set to Noner   )
Úallr#   r0   r1   Úshaper   r   r   Úall_to_all_singler%   ©r   re   rf   r    r!   r&   r9   r   s           r   ru   ru   ½  s@  € ð, Ð%Ýð 
ð 
Ø>Pð
ñ 
ô 
ñ 
ô 
ð 	ð 	àñ	ô 	ð 
ð Ð$Ýð 
ð 
Ø>Oð
ñ 
ô 
ñ 
ô 
ð 	ð 	àñ	ô 	ð 
õ % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€JØÐ!Ð%6Ð%>Ø!Ð)Ð.?Ð.GÐ.Gð5ñ /HÔ.GÐGð #œj¨œm¨zÑ9Ð:¸ZÑGÐØ.ÐÝŒYÔ'×9Ò9ØØØØñ	ô €Fõ ˜fÑ%Ô%Ð%r   c                 ó°  — |�#t          d„ |D ¦   «         ¦  «        s
J |¦   «         ‚|�#t          d„ |D ¦   «         ¦  «        s
J |¦   «         ‚t          ||¦  «        }t          j        |¦  «        }|�|€$|€|�
J d¦   «         ‚| j        d         |z  g|z  }|}t
          j        j                             | |||¦  «        }t           
                    |¦  «        S )z:
    Same as all_to_all_single but supports autograd.
    Nc              3   óX   K  — | ]%}t          |t          t          j        f¦  «        V — Œ&d S ri   rj   rm   s     r   ro   z-all_to_all_single_autograd.<locals>.<genexpr>ø  rp   r   c              3   óX   K  — | ]%}t          |t          t          j        f¦  «        V — Œ&d S ri   rj   rm   s     r   ro   z-all_to_all_single_autograd.<locals>.<genexpr>ü  rp   r   rr   r   )rs   r#   r0   r1   rt   r   r   r=   ru   r>   r?   rv   s           r   Úall_to_all_single_autogradrz   í  sF  € ð Ð%Ýð 
ð 
Ø>Pð
ñ 
ô 
ñ 
ô 
ð 	ð 	àñ	ô 	ð 
ð Ð$Ýð 
ð 
Ø>Oð
ñ 
ô 
ñ 
ô 
ð 	ð 	àñ	ô 	ð 
õ % U¨CÑ0Ô0€JÝÔ-¨jÑ9Ô9€JØÐ!Ð%6Ð%>Ø!Ð)Ð.?Ð.GÐ.Gð5ñ /HÔ.GÐGð #œj¨œm¨zÑ9Ð:¸ZÑGÐØ.ÐÝŒYÔ0×BÒBØØØØñ	ô €Fõ ×!Ò! &Ñ)Ô)Ð)r   Úsrc_dstc                 óz  — t          ||¦  «        \  }}}t          j        |||¦  «        }dg|z  }dg|z  }	t          |¦  «        D ]c\  }
}|
t	          j        |¦  «        k    r|                      ¦   «         |	|<   |t	          j        |¦  «        k    r|                      ¦   «         ||
<   Œdt          | ||	||¦  «        S )a"  
    Permutes the elements of the tensor according to the given source/destination pairs. `src_dst` should
    be defined such that src_dst[m] == n means m sends to n.

    Group can be one of:
        List[int]: ranks participating in the collective.
        List[List[int]]: 2D mesh of ranks taking part of this collective in MPMD.
        ProcessGroup: Will perform a collective using the ranks and tag of the PG.
        DeviceMesh: Do a SPMD collective over all ranks of the mesh
        (DeviceMesh, int): Do a MPMD collective over one
    r   )Ú_expand_groupr0   Ú#_find_or_create_pg_by_ranks_and_tagrU   ÚdistÚget_rankÚnumelru   )r   r{   r    r!   ÚtÚranksetr9   Úlocal_pgre   rf   r   Údsts               r   Úpermute_tensorr†     sÍ   € õ" +¨5°#Ñ6Ô6Ñ€A€w�
ÝÔ7¸¸7ÀJÑOÔO€Hà˜˜zÑ)ÐØ˜˜jÑ(ÐÝ˜gÑ&Ô&ð 3ð 3‰ˆˆSØ•$”- Ñ)Ô)Ò)Ð)Ø%)§Z¢Z¡\¤\Ð˜cÑ"Ø•$”- Ñ)Ô)Ò)Ð)Ø&*§j¢j¡l¤lÐ˜sÑ#øå˜TÐ#5Ð7HÈ%ÐQTÑUÔUÐUr   c                   óî   — e Zd ZU dZej        ed<   eed<   ddgZe	dej        fd„¦   «         Z
d„ Zd„ Ze	d„ ¦   «         Z	 dd	ed
ee         fd„Zdefd„Zd„ Zdej        fd„Zd„ Zedd„¦   «         Zd„ ZdS )r4   a¸  
    A Tensor wrapper subclass that is used to trigger a call to wait
    prior to first use of the underlying tensor.
    Use it inside functional collective pytorch wrappers like the following:
    def functional_collective(self, group, tag):
        tag, rankset, group_size = _expand_group(group, tag)
        tensor = torch.ops.c10d_functional.{collective}(self, tag, rankset, group_size)
        return _maybe_wrap_tensor(tensor)
    ÚelemÚ	completedc           
      ó  — t           j                             | |                     ¦   «         |                     ¦   «         |                     ¦   «         |j        |j        |j        |j	        ¬¦  «        }||_
        d|_        |S )N)ÚstridesÚstorage_offsetÚdtypeÚlayoutÚdeviceÚrequires_gradF)r   ÚTensorÚ_make_wrapper_subclassrE   ÚstriderŒ   r�   rŽ   r�   r�   rˆ   r‰   )Úclsrˆ   Úrs      r   Ú__new__zAsyncCollectiveTensor.__new__@  sp   € åŒL×/Ò/ØØ�IŠI‰KŒKØ—K’K‘M”MØ×.Ò.Ñ0Ô0Ø”*Ø”;Ø”;ØÔ,ð 0ñ 	
ô 	
ˆð ˆŒØˆŒØˆr   c                 ó   — dgd fS ©Nrˆ   r   ©r   s    r   Ú__tensor_flatten__z(AsyncCollectiveTensor.__tensor_flatten__P  s   € Øˆx˜ˆ~Ðr   c                 óN   — |                       ¦   «                              ¦   «         S ri   )Útrigger_waitÚtolistr™   s    r   r�   zAsyncCollectiveTensor.tolistS  s    € Ø× Ò Ñ"Ô"×)Ò)Ñ+Ô+Ð+r   c                 ó8   — |�J ‚| d         }t          |¦  «        S r˜   )r4   )Úinner_tensorsÚmetaÚ
outer_sizeÚouter_striderˆ   s        r   Ú__tensor_unflatten__z*AsyncCollectiveTensor.__tensor_unflatten__V  s#   € àˆ|ˆ|ˆ|Ø˜VÔ$ˆÝ$ TÑ*Ô*Ð*r   NÚexpected_metadataÚexpected_typec                 óJ   — |t           j        urd S |                      ¦   «         S ri   )r   r‘   rœ   )r   r¤   r¥   s      r   Ú#__coerce_same_metadata_as_tangent__z9AsyncCollectiveTensor.__coerce_same_metadata_as_tangent__\  s(   € ð ¥¤Ð,Ð,Ø�4à× Ò Ñ"Ô"Ð"r   rK   c                 ó2   — d|                       ¦   «         › d�S )NzAsyncCollectiveTensor(ú))rœ   r™   s    r   Ú__repr__zAsyncCollectiveTensor.__repr__d  s   € Ø>¨×(9Ò(9Ñ(;Ô(;Ð>Ð>Ð>Ð>r   c                 óX   — | j         st          | j        ¦  «        }d| _         |S | j        S ©NT)r‰   r   rˆ   )r   Úouts     r   rœ   z"AsyncCollectiveTensor.trigger_waitg  s/   € ØŒ~ð 	Ý˜dœiÑ(Ô(ˆCØ!ˆDŒNØˆJà”9Ðr   c                 ó*   — t          | j        ¦  «        S ri   )r   rˆ   r™   s    r   r5   zAsyncCollectiveTensor.waito  s   € Ý˜4œ9Ñ%Ô%Ð%r   c                 ó   — | j         S )zOThis method enables  _functional_collectives_impl to test if a tensor is an ACS)rˆ   r™   s    r   Ú_get_acs_underlying_tensorz0AsyncCollectiveTensor._get_acs_underlying_tensorr  s
   € àŒyÐr   r   c                 óœ  ‡— |t           j        j        j        j        k    r. ||d         j        |d         ¦  «        }t          |¦  «        }|S t          |¦  «        Šdt          fˆfd„}dt           j        fd„}t          t          ||¦  «        }	t          t          ||¦  «        }
 ||	i |
¤Ž}‰rt          t           j        ||¦  «        }|S )Nr   r   Úec                 ó>   •— ‰s|                       ¦   «         S | j        S ri   )rœ   rˆ   )r²   Ú
is_view_ops    €r   Úunwrapz8AsyncCollectiveTensor.__torch_dispatch__.<locals>.unwrap�  s"   ø€ àð (Ø—~’~Ñ'Ô'Ð'Ø”6ˆMr   c                 óR   — t          | t          ¦  «        rJ ‚t          | ¦  «        }|S ri   )r3   r4   )r²   r:   s     r   Úwrapz6AsyncCollectiveTensor.__torch_dispatch__.<locals>.wrap‡  s+   € å! !Õ%:Ñ;Ô;Ð;Ð;Ð;Ý'¨Ñ*Ô*ˆCØˆJr   )
r   r   ÚatenÚviewÚdefaultrˆ   r4   rd   r‘   r   )r”   ÚfuncÚtypesÚargsÚkwargsr:   Úwrapper_resrµ   r·   Úunwrapped_argsÚunwrapped_kwargsr­   r´   s               @r   Ú__torch_dispatch__z(AsyncCollectiveTensor.__torch_dispatch__v  sø   ø€ à•5”9”>Ô&Ô.Ò.Ð.ð �$�t˜A”w”| T¨!¤WÑ-Ô-ˆCÝ/°Ñ4Ô4ˆKØÐå  Ñ&Ô&ˆ
ð	Õ+ð 	ð 	ð 	ð 	ð 	ð 	ð	•E”Lð 	ð 	ð 	ð 	õ 'Õ'<¸fÀdÑKÔKˆÝ(Õ)>ÀÈÑOÔOÐð ˆd�NÐ7Ð&6Ð7Ð7ˆð ð 	9Ý¥¤¨d°CÑ8Ô8ˆCàˆ
r   c                 óN   — |                       ¦   «                              ¦   «         S ri   )r5   Únumpyr™   s    r   rÄ   zAsyncCollectiveTensor.numpy™  s   € Ø�yŠy‰{Œ{× Ò Ñ"Ô"Ð"r   ri   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r‘   Ú__annotations__ÚboolÚ	__slots__Ústaticmethodr–   rš   r�   r£   r   r   r   r§   Ústrrª   rœ   r5   r°   ÚclassmethodrÂ   rÄ   r   r   r   r4   r4   1  s]  € € € € € € ðð ð Œ,ÐÐÑØ€O€O�Oà˜Ð%€Iàð˜5œ<ð ð ð ñ „\ððð ð ð,ð ,ð ,ð ð+ð +ñ „\ð+ð GKð#ð #Ø!$ð#Ø5=¸d´^ð#ð #ð #ð #ð?˜#ð ?ð ?ð ?ð ?ðð ð ð&�e”lð &ð &ð &ð &ðð ð ð ð ð  ð  ñ „[ð ðD#ð #ð #ð #ð #r   r4   c           	      óN  — t           rd„ }d„ }nd„ }d„ }t          | t          ¦  «        r¬t          | d         t          ¦  «        ru || ¦  «        }g }d}|D ]a}|                     |¦  «         |dk    r5|t	          |¦  «        k    r"t          d|› dt	          |¦  «        › �¦  «        ‚t	          |¦  «        }Œb�nh || ¦  «        }t	          |¦  «        }�nLt          | t          j        ¦  «        r:t          j        | ¦  «        }t	          |¦  «        }|pt          j
        | ¦  «        }nøt          | t          ¦  «        r6| j        d	k    s
J d
¦   «         ‚| j        d         \  }}}t	          |¦  «        }n­t          | t          ¦  «        r‰t	          | ¦  «        dk    rgt          | d         t          ¦  «        rLt          | d	         t          ¦  «        r1| d         }	| d	         }
|	j        |
         \  }}}t	          |¦  «        }nt          d¦  «        ‚t          d¦  «        ‚|||fS )a5  
    _expand_group desugars the different RANK_TYPES types into a canonical format that is traceable.

    By having this be part of the explicit eager codepath, we avoid having to specialize behavior inside
    torchdynamo and can still interoperate with processgroup objects or other untraceable forms.
    c                 óX   — t          t          t          t                            | ¦  «        S ri   ©r   r   rk   ©Úxs    r   Úcast_listlistintz'_expand_group.<locals>.cast_listlistint®  s   € Ý��T¥#œYœ¨Ñ+Ô+Ð+r   c                 óB   — t          t          t                   | ¦  «        S ri   rÑ   rÒ   s    r   Úcast_listintz#_expand_group.<locals>.cast_listint±  s   € Ý��Sœ	 1Ñ%Ô%Ð%r   c                 ó   — | S ri   r   rÒ   s    r   rÔ   z'_expand_group.<locals>.cast_listlistint¸  ó   € ØˆHr   c                 ó   — | S ri   r   rÒ   s    r   rÖ   z#_expand_group.<locals>.cast_listint»  rØ   r   r   éÿÿÿÿz$group sizes must be identical found z and r   úJOnly 1D mesh is supported, pass in (DeviceMesh, int) together if mesh > 1Dé   ú1Invalid tuple for group must be (DeviceMesh, int)z[Invalid type for group, must be one of List, Processgroup, DeviceMesh or (DeviceMesh, int).)r	   r3   rN   ÚextendrT   Ú
ValueErrorr   ÚProcessGroupÚget_process_group_ranksr0   Ú_get_group_tagr   ÚndimÚ_dim_group_infosÚtuplerk   )r    r!   rÔ   rÖ   Únested_listrƒ   r9   ÚrsÚ_Údmeshr.   s              r   r}   r}   ¢  sv  € õ ð ð	,ð 	,ð 	,ð	&ð 	&ð 	&ð 	&ð	ð 	ð 	ð	ð 	ð 	õ �%�ÑÔð )
Ý�e˜A”h¥Ñ%Ô%ð 	&Ø*Ð*¨5Ñ1Ô1ˆKØˆGØˆJØ!ð %ð %�Ø—’˜rÑ"Ô"Ð"Ø Ò#Ð#¨
µc¸"±g´gÒ(=Ð(=Ý$ØY¸zÐYÐYÕPSÐTVÑPWÔPWÐYÐYñô ð õ ! ™WœW�
�
ñ%ð #�l 5Ñ)Ô)ˆGÝ˜W™œˆJ‰JÝ	�E�4Ô,Ñ	-Ô	-ð 
ÝÔ.¨uÑ5Ô5ˆÝ˜‘\”\ˆ
ØÐ/•TÔ(¨Ñ/Ô/ˆˆÝ	�E�:Ñ	&Ô	&ð 
àŒJ˜!ŠOˆOˆOØWñ ŒOˆOð  Ô0°Ô3‰ˆˆW�aÝ˜‘\”\ˆ
ˆ
Ý	�E�5Ñ	!Ô	!ð 
å�‰JŒJ˜!ŠOˆOÝ˜5 œ8¥ZÑ0Ô0ð å˜5 œ8¥SÑ)Ô)ð ð ˜!”HˆEØ˜”(ˆCØ#Ô4°SÔ9‰OˆC�˜!Ý˜W™œˆJˆJåÐPÑQÔQÐQåØiñ
ô 
ð 	
ð �˜*Ð%Ð%r   c                 ó4  — t          | t          j        ¦  «        r| j        S t          | t          ¦  «        r| S t          | t
          ¦  «        r(| j        dk    s
J d¦   «         ‚| j        d         d         S t          | t          ¦  «        r{t          | ¦  «        dk    rYt          | d         t
          ¦  «        r>t          | d         t          ¦  «        r#| d         }| d         }|j        |         d         S t          d¦  «        ‚t          | t          ¦  «        r]t          ¦   «         st          j        dt           d¬¦  «         t#          j        t'          t(          t                   | ¦  «        |¦  «        S t          d	t+          | ¦  «        › d
| › �¦  «        ‚)z;
    Given group in RANK_TYPES, return the group name.
    r   rÛ   r   rÜ   rÝ   z—The combination of ranks + tag as process group identifier has been deprecated. Please switch to using ProcessGroup, DeviceMesh, or group name instead.é   )Ú
stacklevelzUnsupported group type: z, )r3   r   rà   r&   rÍ   r   rã   rä   rå   rT   rk   rß   rN   r   ÚwarningsÚwarnÚFutureWarningr0   Ú$_resolve_group_name_by_ranks_and_tagr   r   Útype)r    r!   ré   r.   s       r   r#   r#   í  s�  € õ �%�Ô*Ñ+Ô+ð LØÔÐÝ	�E�3Ñ	Ô	ð LØˆÝ	�E�:Ñ	&Ô	&ð LàŒJ˜!ŠOˆOˆOØWñ ŒOˆOàÔ% aÔ(¨Ô+Ð+Ý	�E�5Ñ	!Ô	!ð Lå�‰JŒJ˜!ŠOˆOÝ˜5 œ8¥ZÑ0Ô0ð å˜5 œ8¥SÑ)Ô)ð ð ˜!”HˆEØ˜”(ˆCØÔ)¨#Ô.¨qÔ1Ð1åÐPÑQÔQÐQÝ	�E�4Ñ	 Ô	 ð LÝ'Ñ)Ô)ð 	ÝŒMðIõ Øðñ ô ð õ Ô8½½dÅ3¼iÈÑ9OÔ9OÐQTÑUÔUÐUåÐJµD¸±K´KÐJÐJÀ5ÐJÐJÑKÔKÐKr   c                   óz   — e Zd ZdZedej        dej        fd„¦   «         Zedej        dej        fd„¦   «         ZdS )r>   zm
    _FromTorchTensor allows autograd to propagate from a normal Tensor to an
    AsyncCollectiveTensor.
    ÚinputrK   c                 ó    — t          |¦  «        S ri   )r%   )Úctxró   s     r   Úforwardz_FromTorchTensor.forward  s   € õ
 " %Ñ(Ô(Ð(r   Úgrad_outputc                 ó   — |S ri   r   )rõ   r÷   s     r   Úbackwardz_FromTorchTensor.backward"  s   € àÐr   N)	rÅ   rÆ   rÇ   rÈ   rÌ   r   r‘   rö   rù   r   r   r   r>   r>     s†   € € € € € ðð ð
 ð)àŒ|ð)ð 
Œð)ð )ð )ñ „\ð)ð ð 5¤<ð °E´Lð ð ð ñ „\ðð ð r   r>   c                  ó¬   — t          ¦   «         rdS t          j                             t          j        j        j        ¦  «        �dS t          ¦   «         d uS r¬   )r   r   Ú_CÚ_get_dispatch_modeÚ_TorchDispatchModeKeyÚFAKEr   r   r   r   Ú_are_we_tracingrÿ   '  sI   € ÝÑ!Ô!ð Øˆtå„x×"Ò"¥5¤8Ô#AÔ#FÑGÔGÐSØˆtÝÑÔ 4Ð'Ð'r   c                 óŽ   — t          ¦   «         rt          | ¦  «        S t          | ¦  «        }t          t          j        |¦  «        S ri   )rÿ   r   r4   r   r   r‘   )r   r:   s     r   r%   r%   0  s=   € ÝÑÔð !Ý˜4Ñ Ô Ð Ý
 Ñ
%Ô
%€CÝ•”˜cÑ"Ô"Ð"r   TÚvaluec              #   ó<  K  — t           j        j                             ¦   «         }	 t           j        j                             | ¦  «         dV — t           j        j                             |¦  «         dS # t           j        j                             |¦  «         w xY w)aA  
    Context manager to temporarily set whether inflight collectives are allowed as torch.compile graph inputs.
    Common use case is when the collective is issued in eager (with `async_op=True`) but waited in compiled region:
    ```
    def all_reduce_eager(x):
        y = x * x
        req = dist.all_reduce(y, op=dist.ReduceOp.SUM, async_op=True)
        return y

    @torch.compile(fullgraph=True)
    def all_reduce_wait_compiled(y):
        torch.ops.c10d_functional.wait_tensor(y)
        return y * y

    x = torch.ones(1280, 1280, device="cuda") + self.rank
    # the context manager ensures that `wait_tensor(y)` will wait on the correct work object
    with allow_inflight_collective_as_graph_input_ctx():
        y = all_reduce_eager(x)
        z = all_reduce_wait_compiled(y)
    ```
    With this context manager, when a collective is called, under the hood the work object of the collective
    will be registered in the work registry, and the wait_tensor() in compiled region called on
    the output tensor of the collective will wait on the correct work object.
    N)r   rû   Ú_distributed_c10dÚ)_allow_inflight_collective_as_graph_inputÚ-_set_allow_inflight_collective_as_graph_input)r  Úpreviouss     r   Ú,allow_inflight_collective_as_graph_input_ctxr  7  s˜   è è € õ4 ŒxÔ)×SÒSÑUÔU€Hð
ÝŒÔ"×PÒPÐQVÑWÔWÐWØˆˆˆåŒÔ"×PÒPØñ	
ô 	
ð 	
ð 	
ð 	
ø�ŒÔ"×PÒPØñ	
ô 	
ð 	
ð 	
øøøs   §(A5 Á5&Bc                 ó,   ‡‡— ˆfd„Šˆfd„| D ¦   «         S )Nc                 ó”   •— t          |                      ¦   «         ¦  «        }|dxx         ‰z  cc<   |                      |¦  «        }|S rZ   ©rN   rE   Ú	new_empty)ÚshardÚout_sizeÚ
out_tensorr9   s      €r   Úmk_out_tensorz=_all_gather_into_tensor_coalesced_meta.<locals>.mk_out_tensor]  sE   ø€ Ý˜Ÿ
š
™œÑ%Ô%ˆØ�ˆˆŒ�zÑ!ˆˆ‰Ø—_’_ XÑ.Ô.ˆ
ØÐr   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   ©rn   r‚   r  s     €r   ú
<listcomp>z:_all_gather_into_tensor_coalesced_meta.<locals>.<listcomp>c  s#   ø€ Ð+Ð+Ð+ ˆMˆM˜!ÑÔÐ+Ð+Ð+r   r   )r   r!   rƒ   r9   r  s      `@r   Ú&_all_gather_into_tensor_coalesced_metar  \  s9   øø€ ðð ð ð ð ð ,Ð+Ð+Ð+ dÐ+Ñ+Ô+Ð+r   c                 ó*   — t          j        | ¦  «        S ri   ©r   Ú
empty_like©r   r½   s     r   Ú_broadcast_metar  g  ó   € ÝÔ˜DÑ!Ô!Ð!r   c                 ó*   — t          j        | ¦  «        S ri   r  r  s     r   Ú_all_reduce_metar  k  r  r   c                 ó*   — t          j        | ¦  «        S ri   r  r  s     r   Ú_wait_tensor_metar  o  r  r   c                 óŽ   — t          |                      ¦   «         ¦  «        }|dxx         |z  cc<   |                      |¦  «        S rZ   r
  )r  r!   rƒ   r9   r  s        r   Ú_all_gather_into_tensor_metar  s  s?   € Ý�E—J’J‘L”LÑ!Ô!€HØˆQ€K€K„K�:Ñ€K€K�KØ�?Š?˜8Ñ$Ô$Ð$r   c                 óŽ   — t          |                      ¦   «         ¦  «        }|dxx         |z  cc<   |                      |¦  «        S rZ   r
  )ró   Ú	reduce_opr!   rƒ   r9   r  s         r   Ú_reduce_scatter_tensor_metar"  y  s?   € Ý�E—J’J‘L”LÑ!Ô!€HØˆQ€K€K„K�JÑ€K€K�KØ�?Š?˜8Ñ$Ô$Ð$r   c                 ó   — d„ | D ¦   «         S )Nc                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r   r  )rn   r‚   s     r   r  z._all_reduce_coalesced_meta.<locals>.<listcomp>€  s#   € Ð.Ð.Ð. A�EÔ˜QÑÔÐ.Ð.Ð.r   r   r  s     r   Ú_all_reduce_coalesced_metar%    s   € Ø.Ð.¨Ð.Ñ.Ô.Ð.r   c                 ó   — | S ri   r   ©Úinpr½   s     r   Ú_all_reduce__metar)  ƒ  ó   € Ø€Jr   c                 ó   — | S ri   r   r'  s     r   Ú_broadcast__metar,  ‡  r*  r   c                 ó   — | S ri   r   )rR   r½   s     r   Ú_all_reduce_coalesced__metar.  ‹  s   € Ø€Mr   c                 ó,   ‡‡— ˆfd„Šˆfd„| D ¦   «         S )Nc                 ó”   •— t          |                      ¦   «         ¦  «        }|dxx         ‰z  cc<   |                      |¦  «        }|S rZ   r
  )ró   r  r  r9   s      €r   r  z<_reduce_scatter_tensor_coalesced_meta.<locals>.mk_out_tensor�  sE   ø€ Ý˜Ÿ
š
™œÑ%Ô%ˆØ�ˆˆŒ˜
Ñ"ˆˆ‰Ø—_’_ XÑ.Ô.ˆ
ØÐr   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   r  s     €r   r  z9_reduce_scatter_tensor_coalesced_meta.<locals>.<listcomp>–  s#   ø€ Ð-Ð-Ð- ˆMˆM˜!ÑÔÐ-Ð-Ð-r   r   )rR   r'   r!   rƒ   r9   r  s       `@r   Ú%_reduce_scatter_tensor_coalesced_metar2  �  s9   øø€ ðð ð ð ð ð .Ð-Ð-Ð- fÐ-Ñ-Ô-Ð-r   c                 ó  — |€'|                       |                      ¦   «         ¦  «        S |D ]}t          j        |¦  «         Œt	          |                      ¦   «         ¦  «        }t          |¦  «        |d<   |                       |¦  «        S rZ   )r  rE   r   Ú_check_is_sizerN   Úsum)ró   re   rf   r½   r¾   Úsr  s          r   Ú_all_to_all_single_metar7  ž  s€   € ð Ð!Ø�Š˜uŸzšz™|œ|Ñ,Ô,Ð,à#ð 	$ð 	$ˆAÝÔ  Ñ#Ô#Ð#Ð#Ý˜Ÿ
š
™œÑ%Ô%ˆÝÐ,Ñ-Ô-ˆ�‰Ø�Š˜xÑ(Ô(Ð(r   c                óŽ   — t          |                      ¦   «         ¦  «        }|dxx         |z  cc<   |                      |¦  «        S rZ   r
  )ró   r9   r&   r­   rt   s        r   Ú'_all_gather_into_tensor_out_native_metar9  «  ó?   € Ý�—’‘”ÑÔ€EØ	ˆ!€H€H„H�
Ñ€H€H�HØ�?Š?˜5Ñ!Ô!Ð!r   c                 óŽ   — t          |                      ¦   «         ¦  «        }|dxx         |z  cc<   |                      |¦  «        S rZ   r
  )ró   r9   r&   rt   s       r   Ú#_all_gather_into_tensor_native_metar<  ±  r:  r   c                 ó$   ‡‡— ˆˆfd„| D ¦   «         S )Nc                 ó2   •— g | ]}t          |‰‰¦  «        ‘ŒS r   )r<  )rn   ró   r&   r9   s     €€r   r  zA_all_gather_into_tensor_coalesced_native_meta.<locals>.<listcomp>¸  s5   ø€ ð ð ð àõ 	,¨E°:¸zÑJÔJðð ð r   r   )rR   r9   r&   s    ``r   Ú-_all_gather_into_tensor_coalesced_native_metar?  ·  s4   øø€ ðð ð ð ð àðñ ô ð r   c                 óŽ   — t          |                      ¦   «         ¦  «        }|dxx         |z  cc<   |                      |¦  «        S rZ   r
  )r(  r!  r9   r&   rt   s        r   Ú"_reduce_scatter_tensor_native_metarA  ¾  s?   € Ý�—’‘”ÑÔ€EØ	ˆ!€H€H„H�Ñ€H€H�HØ�=Š=˜ÑÔÐr   c                 ó(   ‡‡‡— ˆˆˆfd„| D ¦   «         S )Nc                 ó4   •— g | ]}t          |‰‰‰¦  «        ‘ŒS r   )rA  )rn   r(  r&   r9   r!  s     €€€r   r  z@_reduce_scatter_tensor_coalesced_native_meta.<locals>.<listcomp>Ç  s7   ø€ ð ð ð àõ 	+¨3°	¸:ÀzÑRÔRðð ð r   r   )rR   r!  r9   r&   s    ```r   Ú,_reduce_scatter_tensor_coalesced_native_metarD  Ä  s:   øøø€ ðð ð ð ð ð àðñ ô ð r   r   ÚIMPLr)   ÚMetaÚall_reduce_rM   Úall_reduce_coalesced_r   Úall_gather_into_tensor_outr2   rQ   rF   rW   ru   r$   Ú
broadcast_Úc10d_functionalÚDEF)	zObroadcast(Tensor self, int src, str tag, int[] ranks, int group_size) -> TensorzUall_reduce(Tensor self, str reduceOp, str tag, int[] ranks, int group_size) -> Tensorzcall_reduce_coalesced(Tensor[] self, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor[]z"wait_tensor(Tensor self) -> TensorzTall_gather_into_tensor(Tensor shard, str tag, int[] ranks, int group_size) -> Tensorzball_gather_into_tensor_coalesced(Tensor[] input, str tag, int[] ranks, int group_size) -> Tensor[]zareduce_scatter_tensor(Tensor input, str reduceOp, str tag, int[] ranks, int group_size) -> Tensorzpreduce_scatter_tensor_coalesced(Tensor[] inputs, str reduceOp, str tag, int[] ranks, int group_size) -> Tensor[]zŠall_to_all_single(Tensor input, SymInt[]? output_split_sizes, SymInt[]? input_split_sizes, str tag, int[] ranks, int group_size) -> Tensorú(rè   )ÚtagsÚCompositeImplicitAutogradzJPyTorch Distributed functional collectives do not work with torch::deploy.FÚoutput_tensorÚinput_tensorÚasync_opc                 ó’   — |r
J d¦   «         ‚|pt           j        j        }|€J ‚|                      t	          ||||¦  «        ¦  «        S ©Nú@Can't remap async version of inplace op to functional collective)r   r    ÚWORLDÚcopy_r;   )rP  rQ  r    rR  r!   r+   s         r   Úall_gather_tensor_inplacerX    sf   € ð ðJð JàIñJô JØð Ð%•T”ZÔ%€EØÐÐÐà×ÒÕ0°¸zÈ5ÐRUÑVÔVÑWÔWÐWr   r5  Úoutputró   Úopc           	      ó”   — |r
J d¦   «         ‚|pt           j        j        }|€J ‚|                      t	          |||||¦  «        ¦  «        S rT  )r   r    rV  rW  rF   )rY  ró   rZ  r    rR  rA   r!   s          r   Úreduce_scatter_tensor_inplacer\  (  se   € ð ðJð JàIñJô JØð Ð%•T”ZÔ%€EØÐÐÐà�<Š<Õ-¨e°R¸ÀeÈSÑQÔQÑRÔRÐRr   ÚavgÚproductÚminÚmaxÚbandÚborÚbxorr   c                 ó’   — |r
J d¦   «         ‚|pt           j        j        }|€J ‚|                      t	          | |||¦  «        ¦  «        S rT  )r   r    rV  rW  r)   )r   rZ  r    rR  r!   s        r   Úall_reduce_inplacere  G  sb   € ð ðJð JàIñJô JØð Ð%•T”ZÔ%€EØÐÐÐà�<Š<�
 6¨2¨u°cÑ:Ô:Ñ;Ô;Ð;r   c           	      ó”   — |r
J d¦   «         ‚|pt           j        j        }|€J ‚|                      t	          |||||¦  «        ¦  «        S rT  )r   r    rV  rW  ru   )rY  ró   re   rf   r    rR  r!   s          r   Úall_to_all_inplacerg  X  st   € ð ðJð JàIñJô JØð Ð%•T”ZÔ%€EØÐÐÐà�<Š<ÝØØØØØñ	
ô 	
ñô ð r   rH   c           	      óº  ‡— |r
J d¦   «         ‚t          ˆfd„| D ¦   «         ¦  «        s
J d¦   «         ‚|pt          j        j        }|€J ‚t	          ‰d||¦  «        }g }d}| D ]M}|                     ||||                     d¦  «        z   …         ¦  «         ||                     d¦  «        z  }ŒNt          | |¦  «        D ]\  }	}
|	                     |
¦  «         Œ| S )NrU  c              3   óp   •K  — | ]0}|                      d ¦  «        ‰                      d ¦  «        k    V — Œ1dS )r   N)rE   )rn   r‚   r   s     €r   ro   z%all_gather_inplace.<locals>.<genexpr>}  sM   øè è € ð ð Ø()ˆ�Šˆq‰	Œ	�V—[’[ ‘^”^Ò#ðð ð ð ð ð r   z7Remapping variable size all_gather is not yet supportedr   )	rs   r   r    rV  r;   ÚappendrE   rV   rW  )rH   r   r    rR  r!   rY  Úoutput_splitsÚoffsetr‚   r…   r   s    `         r   Úall_gather_inplacerm  s  s;  ø€ ð ðJð JàIñJô JØåð ð ð ð Ø-8ðñ ô ñ ô ð Að Aà@ñAô Að ð Ð%•T”ZÔ%€EØÐÐÐå˜v q¨%°Ñ5Ô5€Fð €MØ€FØð ð ˆØ×Ò˜V F¨V°a·f²f¸Q±i´iÑ-?Ð$?Ô@ÑAÔAÐAØ�!—&’&˜‘)”)ÑˆˆÝ˜ ]Ñ3Ô3ð ð ‰ˆˆSØ�	Š	�#‰ŒˆˆØÐr   )Ú_all_gather_baseÚ_reduce_scatter_baseÚ
all_gatherr2   r)   ru   rF   )r   )T)Fr   r   )r5  NFr   r   )r5  NFr   )NNNFr   )NFr   )†Ú
contextlibÚsysrí   Útypingr   r   r   r   r   r   r	   r
   r   Útorch.distributedÚdistributedr   Ú"torch.distributed.distributed_c10dÚdistributed_c10dr0   Útorch.distributed.device_meshr   Ú"torch.fx.experimental.proxy_tensorr   r   r   Úfun_col_implÚtorch.utils._cxx_pytreer   ÚImportErrorÚtorch.utils._pytreeÚ_running_with_deployr   Útorch.compilerr   Ú	Exceptionrî   rk   rà   rÍ   Ú
RANK_TYPESr   r‘   r$   r)   r;   r@   rF   rJ   rM   rQ   rW   rd   ru   rz   r†   r4   r}   r#   ÚautogradÚFunctionr>   rÊ   rÿ   r%   Úcontextmanagerr  r  r  r  r  r  r"  r%  r)  r,  r.  r2  r7  r9  r<  r?  rA  rD  ÚlibraryÚLibraryÚlib_implÚimplÚfxÚnodeÚhas_side_effectr   r   rº   Ú
legacy_libÚlegacy_lib_implÚops_defsÚmodulesrÅ   Ú	my_moduleÚop_defÚindexÚop_nameÚgetattrÚbackend_implÚdefineÚTagÚpt2_compliant_tagrX  r\  ÚReduceOpÚSUMÚAVGÚPRODUCTÚMINÚMAXÚBANDÚBORÚBXORÚREDUCE_OP_TO_STRre  rg  rm  rn  Úlegacy_all_gather_basero  Úlegacy_reduce_scatter_baserp  Úlegacy_all_gatherr2   Úlegacy_allgatherÚlegacy_allreduceÚlegacy_all_to_all_singleÚlegacy_reducescatterÚtraceable_collective_remapsr   r   r   ú<module>r«     s”  ðà Ð Ð Ð Ø 
€
€
€
Ø €€€Ø OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ Oà €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø =Ð =Ð =Ð =Ð =Ð =à :Ð :Ð :Ð :Ð :Ð :ð2Ø5Ð5Ð5Ð5Ð5Ð5Ð5øØð 2ð 2ð 2Ø1Ð1Ð1Ð1Ð1Ð1Ð1Ð1ð2øøøð €5ÔÑÔð ðð ð ð ð
ØRÐRÐRÐRÐRÐRÐRøØð ð ð ØˆŒØrñ	
ô 	
ð 	
ð	ð 	ð 	ð 	ð 	ðøøøðð#ðJð
 Øˆ„IØˆˆcŒ„OØÔØØ	Ð
" CÐ
'Ô(Øð	ô€
ðð::ð :ð :ð&ð &�E”Lð & sð &°:ð &ÀCð &ð &ð &ð &ð&ð &�U”\ð &¨Sð &¸ð &È#ð &ð &ð &ð &ð4 ð	$ð $Ø
Œ,ð$àð$ð ð$ð 
ð	$ð $ð $ð $ðV ð	ð Ø
Œ,ðàðð ðð 
ð	ð ð ð ðL ð'ð 'Ø
Œ,ð'àð'ð ð'ð ð	'ð
 
ð'ð 'ð 'ð 'ð^ ð$ð $Ø
Œ,ð$àð$ð ð$ð ð	$ð
 
ð$ð $ð $ð $ðP LNð6ð 6Ø
ˆuŒ|Ô
ð6Ø(+ð6Ø4>ð6ØEHð6à	ˆ%Œ,Ôð6ð 6ð 6ð 6ð: =?ð6ð 6Ø
ˆuŒ|Ô
ð6Ø%/ð6Ø69ð6à	ˆ%Œ,Ôð6ð 6ð 6ð 6ðD ð)6ð )6Ø�”Ôð)6àð)6ð �c”ð)6ð ð	)6ð
 
ð)6ð 
ˆ%Œ,Ôð)6ð )6ð )6ð )6ð\Vð Vð Vð ð-&ð -&Ø
Œ,ð-&à   c¤Ô+ð-&ð    S¤	Ô*ð-&ð ð	-&ð
 
ð-&ð „\ð-&ð -&ð -&ð -&ðj ð"*ð "*Ø
Œ,ð"*à   c¤Ô+ð"*ð    S¤	Ô*ð"*ð ð	"*ð
 
ð"*ð „\ð"*ð "*ð "*ð "*ðR ð	Vð VØ
Œ,ðVà�#ŒYðVð ðVð 
ð	Vð
 „\ðVð Vð Vð Vð>i#ð i#ð i#ð i#ð i#˜EœLñ i#ô i#ð i#ðXð
H&ð H&˜ð H&¨#ð H&°u¸SÀ$ÀsÄ)ÈSÐ=PÔ7Qð H&ð H&ð H&ð H&ðV%Lð %L˜zð %L°ð %L¸Sð %Lð %Lð %Lð %LðPð ð ð ð �u”~Ô.ñ ô ð ð$(˜ð (ð (ð (ð (ð# ¤ð #ð #ð #ð #ð Ôð!
ð !
¸ð !
ð !
ð !
ñ Ôð!
ðH,ð ,ð ,ð"ð "ð "ð"ð "ð "ð"ð "ð "ð%ð %ð %ð%ð %ð %ð/ð /ð /ðð ð ðð ð ðð ð ð.ð .ð .ð
)ð 
)ð 
)ð"ð "ð "ð"ð "ð "ðð ð ð ð  ð  ðð ð ð "€uÔ!Ñ#Ô#ñ ;ð Œ}×$Ò$Ð%7¸Ñ@Ô@€HØ‡M‚M�,Ð 0°&Ñ9Ô9Ð9Ø‡M‚M�-Ð!2°FÑ;Ô;Ð;Ø‡M‚MÐ(Ð*DÀfÑMÔMÐMØ‡M‚MÐ)Ð+FÈÑOÔOÐOØ‡M‚M�-Ð!2°FÑ;Ô;Ð;Ø‡M‚MØ$Ð&MÈvñô ð ð ‡M‚MÐ*Ð,OÐQWÑXÔXÐXØ‡M‚MØ*Ø5Øñô ð ð
 ‡M‚MÐ)Ð+MÈvÑVÔVÐVØ‡M‚MØ)Ø4Øñô ð ð
 ‡M‚MÐ%Ð'>ÀÑGÔGÐGØ‡M‚M�+˜°Ñ7Ô7Ð7Ø‡M‚M�,Ð 0°&Ñ9Ô9Ð9ð 
„H„M×!Ò! %¤)Ô"<Ô"HÔ"PÑQÔQÐQØ	„H„M×!Ò! %¤)Ô"<Ô"HÑIÔIÐIð ”×&Ò&Ð'8¸%Ñ@Ô@€JØ”m×+Ò+Ð,=¸vÑFÔF€Oð
ð 
ð 
€Hð ”˜HÔ%€IØð Qð QˆØ˜˜VŸ\š\¨#Ñ.Ô.Ð.Ô/ˆØ�w˜|¨]°¨]¨]Ñ;Ô;ˆØ×Ò˜& u¤yÔ'BÐÑCÔCÐCØ×Ò˜W lÐ4OÑPÔPÐPÐPð	Qð €H„MØTñô ð ð
ð ØØðXð XØ”<ðXà”,ðXð ð	Xð
 
ðXð ðXð Xð Xð Xð* Ø
ØØØðSð SØŒLðSàŒ<ðSð 	ðSð
 ðSð ðSð 
ðSð Sð Sð Sð( 	„MÔ�uØ„MÔ�uØ„MÔ˜9Ø„MÔ�uØ„MÔ�uØ„MÔ˜Ø„MÔ�uØ„MÔ˜ð	Ð ð Ø
ØØð<ð <ØŒLð<àð<ð ð	<ð
 
ð<ð <ð <ð <ð( ØØ
ØØðð ØŒLðàŒ<ðð 
ðð ð ð ð< ØØðð Ø�e”lÔ#ðàŒLðð
 
ðð ð ð ð>ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð Ð/ØÐ7ØÐ(ØÐ0ØÐ)ØÐ =ØÐ5ðÐ Ð Ð s$   ÁA ÁAÁAÁ3A: Á:BÂB