§
    ŠŠtjì$  ã                   ó  — d Z ddlZddlZddlmZmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ erddlmZ  G d„ de¦  «        Zdedefd„Zdedefd„Z G d„ de¦  «        Z G d„ de¦  «        Z dS )a«  
Distributed computing variable tracking classes for PyTorch Dynamo.

This module implements variable tracking for distributed computing components:
- Process Groups (for collective communication)
- Device Meshes (for distributed tensor sharding)
- Placement Types (for specifying distribution strategies)
- Distributed Tensors and their operations
- Backward hooks for distributed module operations

These classes are responsible for tracking distributed operations during graph
compilation while maintaining proper guards and handling distributed-specific
behaviors. They ensure correct handling of distributed components like process
groups, device meshes, and placement strategies while preserving proper semantics
for distributed tensor operations in the compiled code.

The implementation provides special handling for distributed package availability
checks and proper tracking of distributed state and operations across processes.
é    N)ÚAnyÚTYPE_CHECKING)ÚBackwardStateé   )Úcompiled_autograd)Útrace_wrapped)Úunimplemented)Ú%call_module_hooks_from_backward_state)ÚGuardBuilderÚinstall_guard)Ú
AttrSourceé   )ÚVariableTracker)ÚInstructionTranslatorBasec                   óŽ   ‡ — e Zd ZdZdededdfˆ fd„Zdefd„Zede	fd„¦   «         Z
d	edeee	f         fd
„Zd„ Zdede	fd„Zˆ xZS )ÚDistributedVariablea{  
    The base distributed variable that encapsulates common methods
    for the distributed objects (i.e. ProcessGroup, DeviceMesh, etc.).
    Concrete distributed objects could inherit this class and add object
    specific logic.

    i.e. It provides the check on the distributed package existence
    and hold the tracking value for the corresponding distributed object.
    ÚvalueÚkwargsÚreturnNc                 ó¢   •—  t          ¦   «         j        di |¤Ž t                               ¦   «         st	          ddddg¬¦  «         || _        d S )Nz+torch.distributed package is not available!Ú zPThe PyTorch package doesn't include torch.distributed when building from source.zESet USE_DISTRIBUTED=1 to enable it when building PyTorch from source.©Úgb_typeÚcontextÚexplanationÚhints© )ÚsuperÚ__init__r   Úis_availabler	   r   )Úselfr   r   Ú	__class__s      €úa/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_dynamo/variables/distributed.pyr   zDistributedVariable.__init__4   si   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý"×/Ò/Ñ1Ô1ð 	ÝØEØØnà[ðð	ñ ô ð ð ˆŒ
ˆ
ˆ
ó    c                 ó*   — t          | j        ¦  «        S ©N©Útyper   ©r!   s    r#   Úpython_typezDistributedVariable.python_typeA   ó   € Ý�D”JÑÔÐr$   c                  ó>   — t           j                             ¦   «         S r&   )ÚtorchÚdistributedr    r   r$   r#   r    z DistributedVariable.is_availableD   s   € õ Ô ×-Ò-Ñ/Ô/Ð/r$   Útxc                 ó.   — t          | j        ¦  «        dfS )NF)Úhashr   )r!   r/   s     r#   Ú	hash_implzDistributedVariable.hash_implI   s   € Ý�D”JÑÔ Ð&Ð&r$   c                 ó*   — ddl m}  || |||¦  «        S )Nr   )Úobject_richcompare)Úobject_protocolr4   )r!   r/   ÚotherÚopr4   s        r#   Úrichcompare_implz$DistributedVariable.richcompare_implL   s+   € Ø7Ð7Ð7Ð7Ð7Ð7à!Ð! $¨¨E°2Ñ6Ô6Ð6r$   r6   c                 ó€   — t          |t          ¦  «        o)|                      ¦   «         |                     ¦   «         k    S r&   )Ú
isinstancer   Úas_python_constant)r!   r6   s     r#   Úis_python_equalz#DistributedVariable.is_python_equalQ   s:   € å�u�oÑ.Ô.ð HØ×'Ò'Ñ)Ô)¨U×-EÒ-EÑ-GÔ-GÒGð	
r$   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r*   ÚstaticmethodÚboolr    ÚtupleÚintr2   r8   Úobjectr<   Ú__classcell__©r"   s   @r#   r   r   )   sú   ø€ € € € € ðð ð˜cð ¨Sð °Tð ð ð ð ð ð ð ˜Tð  ð  ð  ð  ð ð0˜$ð 0ð 0ð 0ñ „\ð0ð'˜Cð ' E¨#¨t¨)Ô$4ð 'ð 'ð 'ð 'ð7ð 7ð 7ð

 Vð 
°ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r$   r   r   r   c                 ó~   — t                                ¦   «         sdS ddlm} t	          j        | ¦  «        o| |j        u S )NFr   )ÚDTensor)r   r    Útorch.distributed.tensorrI   ÚinspectÚ
isfunctionÚ
from_local)r   rI   s     r#   Úis_from_localrN   X   sK   € Ý×+Ò+Ñ-Ô-ð ØˆuØ0Ð0Ð0Ð0Ð0Ð0åÔ˜eÑ$Ô$ÐD¨°'Ô2DÐ)DÐDr$   c                 ó’   — t                                ¦   «         sdS ddlm}m}m}m}m} |||||g}t          j	        | ¦  «        o| |v S )NFr   )Ú_get_group_size_by_nameÚ_get_group_tagÚ_rank_not_in_groupÚ$_resolve_group_name_by_ranks_and_tagÚget_process_group_ranks)
r   r    Ú"torch.distributed.distributed_c10drP   rQ   rR   rS   rT   rK   rL   )r   rP   rQ   rR   rS   rT   Úconstant_processgroup_functionss          r#   Úis_constant_pg_functionsrW   `   s˜   € Ý×+Ò+Ñ-Ô-ð Øˆuðð ð ð ð ð ð ð ð ð ð ð ð ð ð 	 ØØØØ,ð'Ð#õ Ô˜eÑ$Ô$ÐQ¨Ð2QÐ)QÐQr$   c                   ó\   ‡ — e Zd ZdZededefd„¦   «         Zdefd„Z	ddde
defˆ fd	„Zˆ xZS )
ÚWorldMetaClassVariablez€
    Tracks torch.distributed.GroupMember and torch.distributed.group, which are
    instances of the metaclass _WorldMeta.
    r   r   c                 ó\   — |                       ¦   «         sdS ddlm} t          |¦  «        |u S )NFr   )Ú
_WorldMeta)r    rU   r[   r(   )Úclsr   r[   s      r#   Úis_group_member_typez+WorldMetaClassVariable.is_group_member_type}   s>   € à×ÒÑ!Ô!ð 	Ø�5àAÐAÐAÐAÐAÐAå�E‰{Œ{˜jÐ(Ð(r$   c                 ó*   — t          | j        ¦  «        S r&   r'   r)   s    r#   r*   z"WorldMetaClassVariable.python_type†   r+   r$   r/   r   Únamec                 ó@  •— |dk    rx| j         st          d¦  «        ‚t          | j         d¬¦  «        }t          |                     t
          j        ¦  «        ¦  «         t          j        || j	        j
        |¦  «        S |dk    rx| j         st          d¦  «        ‚t          | j         d¬¦  «        }t          |                     t
          j        ¦  «        ¦  «         t          j        || j	        j        |¦  «        S t          ¦   «                              ||¦  «        S )NÚWORLDz<WorldMetaClassVariable requires a source for WORLD attribute)ÚbaseÚmemberÚNON_GROUP_MEMBERzGWorldMetaClassVariable requires a source for NON_GROUP_MEMBER attribute)ÚsourceÚAssertionErrorr   r   Ú
make_guardr   ÚID_MATCHr   Úbuildr   ra   rd   r   Úvar_getattr)r!   r/   r_   re   r"   s       €r#   rj   z"WorldMetaClassVariable.var_getattr‰   s  ø€ ð �7Š?ˆ?Ø”;ð Ý$ØRñô ð õ   T¤[¸ÐAÑAÔAˆFÝ˜&×+Ò+­LÔ,AÑBÔBÑCÔCÐCÝ"Ô(¨¨T¬ZÔ-=¸vÑFÔFÐFØÐ'Ò'Ð'Ø”;ð Ý$Ø]ñô ð õ   T¤[Ð9KÐLÑLÔLˆFÝ˜&×+Ò+­LÔ,AÑBÔBÑCÔCÐCÝ"Ô(¨¨T¬ZÔ-HÈ&ÑQÔQÐQÝ‰wŒw×"Ò" 2 tÑ,Ô,Ð,r$   )r=   r>   r?   r@   ÚclassmethodrE   rB   r]   r(   r*   Ústrr   rj   rF   rG   s   @r#   rY   rY   w   s¨   ø€ € € € € ðð ð
 ð)¨ð )°Dð )ð )ð )ñ „[ð)ð ˜Tð  ð  ð  ð  ð-Ø-ð-Ø58ð-à	ð-ð -ð -ð -ð -ð -ð -ð -ð -ð -r$   rY   c                   óü   ‡ — e Zd ZdZedddedededd f
d„¦   «         Zd	ej        j	        dededed
e
ddfˆ fd„Zdej        j	        fd„Zdefd„Zdddedee         deeef         def
ˆ fd„Zdddededefd„Zˆ xZS )ÚBackwardHookVariablezU
    Handles torch.utils.hooks.BackwardHook for module-level backward
    hooks.
    r/   r   ÚmoduleÚ
user_hooksÚuser_pre_hooksr   c                 ó  ‡‡	‡
— t           j        st          ddddg¬¦  «         dt          dt          j        j        j        fˆˆ	ˆ
fd„}| j         	                    |d¦  «        \  Š}| j         	                    |¦  «        \  Š
}| j         	                    |¦  «        \  Š	}| j         
                    d	||fi ¦  «        }t          j        j                             d d
d
¦  «        |j        j        d<   t          ||||¦  «        S )Nz7Module-level backwards hooks require compiled autograd.r   zREnable compiled autograd by setting torch._dynamo.config.compiled_autograd = True.r   Úbw_stater   c                 óØ   •— t           j        j                             dt	          j        t          t          | ‰‰¬¦  «        ft	          j        t          t          | ‰‰¬¦  «        f¦  «        S )a  
            Rather than installing the user hooks in the graph (which
            don't survive AotAutograd), we install hooks that will call
            trace_wrapped in the backward pass that CompiledAutograd
            can turn into actual hook calls.
            N)Úfnrs   Ú
hooks_nameÚmodule_name)r-   ÚutilsÚhooksÚBackwardHookÚ	functoolsÚpartialr   r
   )rs   rw   Úuser_hooks_nameÚuser_pre_hooks_names    €€€r#   Ú_in_graph_bw_hooksz7BackwardHookVariable.create.<locals>._in_graph_bw_hooks¶   sy   ø€ õ ”;Ô$×1Ò1ØåÔ%Ý%Ý@Ø!)Ø#2Ø$/ðñ ô ðõ Ô%Ý%Ý@Ø!)Ø#6Ø$/ðñ ô ðñô ð r$   ÚmodÚcall_functionr   Úexample_value)r   Úcompiled_autograd_enabledr	   r   r-   rx   ry   rz   ÚoutputÚadd_backward_state_hookÚcreate_proxyÚnodeÚmetarn   )r/   ro   rp   rq   r   Úbw_state_proxyÚ_Úproxyrw   r}   r~   s           @@@r#   ÚcreatezBackwardHookVariable.create¥   s1  øøø€ õ !Ô:ð 	ÝØQØØàhðð	ñ ô ð ð	Ý#ð	åŒ[ÔÔ+ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð> ')¤i×&GÒ&GÈÐPUÑ&VÔ&VÑ#ˆ�^Ø!#¤×!BÒ!BÀ>Ñ!RÔ!RÑÐ˜QØœY×>Ò>¸zÑJÔJÑˆ˜Ø”	×&Ò&ØØØÐØñ	
ô 
ˆõ ,1¬;Ô+<×+IÒ+IÈ$ÐPRÐTVÑ+WÔ+WˆŒ
Œ˜Ñ(Ý# E¨6°:¸~ÑNÔNÐNr$   r‹   ÚoptionsNc                 ór   •—  t          ¦   «         j        di |¤Ž || _        || _        || _        || _        d S )Nr   )r   r   r‹   ro   rp   rq   )r!   r‹   ro   rp   rq   r�   r"   s         €r#   r   zBackwardHookVariable.__init__á   sE   ø€ ð 	�‰ŒÔÐ#Ð#˜7Ð#Ð#Ð#ØˆŒ
ØˆŒØ$ˆŒØ,ˆÔÐÐr$   c                 ó   — | j         S r&   )r‹   r)   s    r#   Úas_proxyzBackwardHookVariable.as_proxyï   s
   € ØŒzÐr$   c                 ó.   — t           j        j        j        S r&   )r-   rx   ry   rz   r)   s    r#   r*   z BackwardHookVariable.python_typeò   s   € ÝŒ{Ô Ô-Ð-r$   r_   Úargsr   c                 óx   •— |dv r | j         ||g|¢R i |¤ŽS t          ¦   «                              ||||¦  «        S )N)Úsetup_input_hookÚsetup_output_hook)Ú_setup_hookr   Úcall_method)r!   r/   r_   r’   r   r"   s        €r#   r—   z BackwardHookVariable.call_methodõ   sT   ø€ ð Ð<Ð<Ð<Ø#�4Ô# B¨Ð>¨tÐ>Ð>Ð>°vÐ>Ð>Ð>Ý‰wŒw×"Ò" 2 t¨T°6Ñ:Ô:Ð:r$   Úhook_method_namec           
      ó¨   — ddl m}  |||j                             d||                      ¦   «         |                     ¦   «         fi ¦  «        ¦  «        S )Nr   )Úwrap_fx_proxyr—   )Úbuilderrš   r„   r†   r�   )r!   r/   r˜   r’   rš   s        r#   r–   z BackwardHookVariable._setup_hook   sc   € ð 	+Ð*Ð*Ð*Ð*Ð*àˆ}ØØŒI×"Ò"ØØ Ø—’‘” $§-¢-¡/¤/Ð2Øñ	ô ñ
ô 
ð 	
r$   )r=   r>   r?   r@   rA   r   rŒ   r-   ÚfxÚProxyr   r   r�   r(   r*   rl   ÚlistÚdictr—   r–   rF   rG   s   @r#   rn   rn   Ÿ   s®  ø€ € € € € ðð ð
 ð9OØ'ð9Oàð9Oð $ð9Oð (ð	9Oð
 
 ð9Oð 9Oð 9Oñ „\ð9Oðv-àŒxŒ~ð-ð  ð-ð $ð	-ð
 (ð-ð ð-ð 
ð-ð -ð -ð -ð -ð -ð˜%œ(œ.ð ð ð ð ð.˜Tð .ð .ð .ð .ð	;à'ð	;ð ð	;ð �?Ô#ð		;ð
 �S˜/Ð)Ô*ð	;ð 
ð	;ð 	;ð 	;ð 	;ð 	;ð 	;ð
à'ð
ð ð
ð ð	
ð
 
ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r$   rn   )!r@   r{   rK   Útypingr   r   r-   Ú%torch.fx.experimental._backward_stater   r   r   Ú_trace_wrapped_higher_order_opr   Úexcr	   Úexternal_utilsr
   Úguardsr   r   re   r   rb   r   Útorch._dynamo.symbolic_convertr   r   rE   rB   rN   rW   rY   rn   r   r$   r#   ú<module>r§      sß  ððð ð( Ð Ð Ð Ø €€€Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %à €€€Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?à  Ð  Ð  Ð  Ð  Ð  Ø :Ð :Ð :Ð :Ð :Ð :Ø Ð Ð Ð Ð Ð Ø BÐ BÐ BÐ BÐ BÐ BØ 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !ð ð IØHÐHÐHÐHÐHÐHð,
ð ,
ð ,
ð ,
ð ,
˜/ñ ,
ô ,
ð ,
ð^E˜ð E Dð Eð Eð Eð EðR Fð R¨tð Rð Rð Rð Rð.%-ð %-ð %-ð %-ð %-Ð0ñ %-ô %-ð %-ðPq
ð q
ð q
ð q
ð q
˜?ñ q
ô q
ð q
ð q
ð q
r$   