
    3j"                        S r SSKrSSKrSSKJrJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  \(       a  SSKJr   " S S\5      rS\S\4S jrS\S\4S jr " S S\5      r  " S S\5      r!g)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LiteralTYPE_CHECKING)BackwardState   )compiled_autograd)trace_wrapped)unimplemented)%call_module_hooks_from_backward_state)GuardBuilderinstall_guard)
AttrSource   )VariableTracker)InstructionTranslatorc                      ^  \ rS rSrSrS\S\SS4U 4S jjrS\4S jr\	S\
4S	 j5       rS\S
   4S jrS\4S jrS\S\
4S jrSrU =r$ )DistributedVariable)   a_  
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U ]  " S0 UD6  [        R                  5       (       d  [	        SSSS/S9  Xl        g )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      ]/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_dynamo/variables/distributed.pyr!   DistributedVariable.__init__4   sB    "6""//11En[	 
    c                 ,    [        U R                  5      $ Ntyper   r#   s    r%   python_typeDistributedVariable.python_typeA       DJJr'   c                  >    [         R                  R                  5       $ r)   )torchdistributedr"   r   r'   r%   r"    DistributedVariable.is_availableD   s       --//r'   Tc                     g)NTr   r,   s    r%   is_python_hashable&DistributedVariable.is_python_hashableI   s    r'   c                 ,    [        U R                  5      $ r)   )hashr   r,   s    r%   get_python_hash#DistributedVariable.get_python_hashL   r/   r'   otherc                 r    [        U[        5      =(       a!    U R                  5       UR                  5       :H  $ r)   )
isinstancer   as_python_constant)r#   r;   s     r%   is_python_equal#DistributedVariable.is_python_equalO   s1    uo. H'')U-E-E-GG	
r'   )r   )__name__
__module____qualname____firstlineno____doc__r   r!   r+   r-   staticmethodboolr"   r   r5   intr9   objectr?   __static_attributes____classcell__r$   s   @r%   r   r   )   s}    c S T  T   0$ 0 0GDM    
V 
 
 
r'   r   r   r   c                     [         R                  5       (       d  gSSKJn  [        R
                  " U 5      =(       a    XR                  L $ )NFr   )DTensor)r   r"   torch.distributed.tensorrN   inspect
isfunction
from_local)r   rN   s     r%   is_from_localrS   V   s6    ++--0e$D2D2D)DDr'   c                     [         R                  5       (       d  gSSKJnJnJnJnJn  UUUUU/n[        R                  " U 5      =(       a    X;   $ )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_c10drU   rV   rW   rX   rY   rP   rQ   )r   rU   rV   rW   rX   rY   constant_processgroup_functionss          r%   is_constant_pg_functionsr\   ^   sR    ++--  	 ,'# e$Q)QQr'   c                   f   ^  \ rS rSrSr\S\S\4S j5       rS\	4S jr
SSS	\S\4U 4S
 jjrSrU =r$ )WorldMetaClassVariableu   zt
Tracks torch.distributed.GroupMember and torch.distributed.group, which are
instances of the metaclass _WorldMeta.
r   r   c                 T    U R                  5       (       d  gSSKJn  [        U5      UL $ )NFr   )
_WorldMeta)r"   rZ   ra   r+   )clsr   ra   s      r%   is_group_member_type+WorldMetaClassVariable.is_group_member_type{   s&    !!AE{j((r'   c                 ,    [        U R                  5      $ r)   r*   r,   s    r%   r-   "WorldMetaClassVariable.python_type   r/   r'   txr   namec                    > US:X  az  SSK Jn  U R                  (       d   e[        U R                  SS9n[	        UR                  [        R                  5      5        UR                  XR                  R                  5      $ US:X  ay  U R                  (       d   e[        U R                  SS9n[	        UR                  [        R                  5      5        [        R                  " XR                  R                  5      $ [        TU ]=  X5      $ )NWORLDr   )SourcelessBuilder)basememberNON_GROUP_MEMBER)builderrk   sourcer   r   
make_guardr   ID_MATCHcreater   rj   r   buildrn   r    var_getattr)r#   rg   rh   rk   rp   r$   s        r%   ru   "WorldMetaClassVariable.var_getattr   s    7?2;;;T[[AF&++L,A,ABC$++B

0@0@AA'';;;T[[9KLF&++L,A,ABC"((ZZ-H-HIIw"2,,r'   r   )rA   rB   rC   rD   rE   classmethodrI   rG   rc   r+   r-   strr   ru   rJ   rK   rL   s   @r%   r^   r^   u   sV    
 ) )D ) ) T  -5 -S -_ - -r'   r^   c                     ^  \ rS rSrSr\SSS\S\S\SS 4
S	 j5       rS
\R                  R                  S\S\S\S\SS4U 4S jjrS\R                  R                  4S jrSSS\S\\   S\\\4   S\4
U 4S jjrSSS\S\S\4S jrSrU =r$ )BackwardHookVariable   zI
Handles torch.utils.hooks.BackwardHook for module-level backward
hooks.
rg   r   module
user_hooksuser_pre_hooksr   c                 D  ^^	^
 [         R                  (       d  [        SSSS/S9  S[        S[        R
                  R                  R                  4UU	U
4S jjnU R                  R                  US5      u  mnU R                  R                  U5      u  m
nU R                  R                  U5      u  m	nU R                  R                  S	UU40 5      n[        R
                  R                  R                  S S
S
5      UR                  R                  S'   [        XqX#5      $ )Nz7Module-level backwards hooks require compiled autograd.r   zREnable compiled autograd by setting torch._dynamo.config.compiled_autograd = True.r   bw_stater   c                    > [         R                  R                  R                  S[        R
                  " [        [        U TTS94[        R
                  " [        [        U TTS945      $ )z
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)fnr   
hooks_namemodule_name)r1   utilshooksBackwardHook	functoolspartialr	   r   )r   r   user_hooks_nameuser_pre_hooks_names    r%   _in_graph_bw_hooks7BackwardHookVariable.create.<locals>._in_graph_bw_hooks   sk     ;;$$11%%%@!)#2$/ %%%@!)#6$/ r'   modcall_functionr   example_value)r   compiled_autograd_enabledr
   r   r1   r   r   r   outputadd_backward_state_hookcreate_proxynodemetarz   )rg   r|   r}   r~   r   bw_state_proxy_proxyr   r   r   s           @@@r%   rs   BackwardHookVariable.create   s    !::Qh		#	[[++	 	> ')ii&G&GPU&V#^!#!B!B>!RQYY>>zJ		&&	
 ,1;;+<+<+I+I$PRTV+W

(#E:NNr'   r   optionsNc                 V   > [         TU ]  " S0 UD6  Xl        X l        X0l        X@l        g )Nr   )r    r!   r   r|   r}   r~   )r#   r   r|   r}   r~   r   r$   s         r%   r!   BackwardHookVariable.__init__   s*     	#7#
$,r'   c                     U R                   $ r)   )r   r,   s    r%   as_proxyBackwardHookVariable.as_proxy   s    zzr'   rh   argsr   c                 \   > US;   a  U R                   " X/UQ70 UD6$ [        TU ]	  XX45      $ )N)setup_input_hooksetup_output_hook)_setup_hookr    call_method)r#   rg   rh   r   r   r$   s        r%   r    BackwardHookVariable.call_method   s;     <<##B>t>v>>w"2T::r'   hook_method_namec           
          SSK Jn  U" UUR                  R                  SUU R	                  5       UR	                  5       40 5      5      $ )Nr   )wrap_fx_proxyr   )ro   r   r   r   r   )r#   rg   r   r   r   s        r%   r    BackwardHookVariable._setup_hook   sF     	+II"" $--/2	
 	
r'   )r|   r   r}   r~   )rA   rB   rC   rD   rE   rF   r   rs   r1   fxProxyr   r!   r   rx   listdictr   r   rJ   rK   rL   s   @r%   rz   rz      s   
 9O#9O9O $9O (	9O
 
 9O 9Ov-xx~~-  - $	-
 (- - 
-%((.. 	;#	; 	; ?#		;
 S/)*	; 
	;
)
=@
HW
	
 
r'   rz   )"rE   r   rP   typingr   r   r   r1   %torch.fx.experimental._backward_stater   r   r   _trace_wrapped_higher_order_opr	   excr
   external_utilsr   guardsr   r   rp   r   rl   r   torch._dynamo.symbolic_convertr   r   rI   rG   rS   r\   r^   rz   r   r'   r%   <module>r      s   (   . .  ?   :  B 0  ! D*
/ *
ZE ED ERF Rt R.-0 -Dk
? k
r'   