
    Vh>                     f   d Z ddlZddlZddlmZ ddlZddlmZ ddlm	Z	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 ddlmZ ddlmZmZ erddlmZ  G d de      Zd Z d Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z& G d d e      Z'y)!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)TYPE_CHECKING)BackwardState   )compiled_autograd	variables)trace_wrapped)unimplemented_v2)%call_module_hooks_from_backward_state)GuardBuilderinstall_guard)
AttrSource)istype   )VariableTracker)ConstantVariableEnumVariable)InstructionTranslatorc                   :     e Zd ZdZd fdZd Ze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 existance
    and hold the tracking value for the corresponding distributed object.
    c                 z    t        |   di | t        j                         st	        ddddg       || _        y )Nz+torch.distributed package is not available! zOThe PyTorch package doesn't include torch.distributed when builing from source.zESet USE_DISTRIBUTED=1 to enable it when building PyTorch from source.gb_typecontextexplanationhints )super__init__r   is_availabler	   value)selfr!   kwargs	__class__s      S/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/variables/distributed.pyr   zDistributedVariable.__init__8   sA    "6""//1Em[	 
    c                 ,    t        | j                        S N)typer!   r"   s    r%   python_typezDistributedVariable.python_typeE   s    DJJr&   c                  >    t         j                  j                         S r(   )torchdistributedr    r   r&   r%   r    z DistributedVariable.is_availableH   s       --//r&   )returnN)	__name__
__module____qualname____doc__r   r+   staticmethodr    __classcell__r$   s   @r%   r   r   -   s&      0 0r&   r   c                     t         j                         syddlm} t	        j
                  |       xr | |j                  u S )NFr   )DTensor)r   r    torch.distributed.tensorr8   inspect
isfunction
from_local)r!   r8   s     r%   is_from_localr=   N   s5    ++-0e$D'2D2D)DDr&   c                     t         j                         syddlm}m}m}m}m} |||||g}t        j                  |       xr | |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_c10dr?   r@   rA   rB   rC   r:   r;   )r!   r?   r@   rA   rB   rC   constant_processgroup_functionss          r%   is_constant_pg_functionsrF   V   sR    ++-  	 ,'# e$Q2Q)QQr&   c                   @     e Zd ZdZe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.
    c                 J    | j                         syddlm} t        |      |u S )NFr   )
_WorldMeta)r    rD   rJ   r)   )clsr!   rJ   s      r%   is_group_member_typez+WorldMetaClassVariable.is_group_member_types   s#    !AE{j((r&   txr   namer/   c                    |dk(  r^t        | j                  d      }t        |j                  t        j
                               t        | j                  j                        S |dk(  r^t        | j                  d      }t        |j                  t        j
                               t        | j                  j                        S t        | 1  ||      S )NWORLD)basememberNON_GROUP_MEMBER)r   sourcer   
make_guardr   ID_MATCHProcessGroupVariabler!   rP   r   rS   r   var_getattr)r"   rM   rN   rT   r$   s       r%   rX   z"WorldMetaClassVariable.var_getattr|   s    7?T[[AF&++L,A,ABC'

(8(899''T[[9KLF&++L,A,ABC

 ; ;<<w"2t,,r&   )
r0   r1   r2   r3   classmethodrL   strr   rX   r5   r6   s   @r%   rH   rH   m   s<    
 ) )	-5 	-S 	-_ 	- 	-r&   rH   c                   F     e Zd Zed        Zd Z	 	 	 	 	 	 	 	 d fdZ xZS )PlacementClassVariablec                 v    t         j                         syddlm} t	        |       t        u xr t        | |      S NFr   )	Placement)r   r    (torch.distributed.tensor.placement_typesr_   r)   
issubclassr!   r_   s     r%   is_placement_typez(PlacementClassVariable.is_placement_type   s1     #//1FE{d"Cz%'CCr&   c                     | j                   S r(   r!   r*   s    r%   as_python_constantz)PlacementClassVariable.as_python_constant       zzr&   c                 d   t        j                  | j                  dd       t        j                  fv rm| j
                  rat        j	                  | j                        }t        |      }t        j                  | j                  dd       r|j                  |d||       |S t        | %  |||      S )N__new__r   )
r:   getattr_staticr!   objectri   rT   PlacementVariablecall_methodr   call_function)r"   rM   argsr#   new_objvarr$   s         r%   rn   z$PlacementClassVariable.call_function   s     ""4::y$?FNNCTT nnTZZ0G#G,C%%djj*dCJf=
w$Rv66r&   )rM   r   ro   list[VariableTracker]r#   dict[str, VariableTracker]r/   r   )r0   r1   r2   r4   rc   rf   rn   r5   r6   s   @r%   r\   r\      sL    D D7#7 &7 -	7
 
7 7r&   r\   c                   Z     e Zd Zed        Zd Zdddedef fdZ	 	 	 	 	 	 d	 fdZ	 xZ
S )
rl   c                 P    t         j                         syddlm} t	        | |      S r^   )r   r    r`   r_   
isinstancerb   s     r%   is_placementzPlacementVariable.is_placement   s#     #//1F%++r&   c                     | j                   S r(   re   r*   s    r%   rf   z$PlacementVariable.as_python_constant   rg   r&   rM   r   rN   r/   c                     |dk(  r)t        j                  | j                  j                        S t        |   ||      S )Ndim)r   creater!   rz   r   rX   r"   rM   rN   r$   s      r%   rX   zPlacementVariable.var_getattr   s5    5=#**4::>>::w"2t,,r&   c                    ddl m} g d}||v r	 t        | j                        }t	        j
                  |dd       J d       t	        j
                  ||      }|t        j                  u r |j                  d       S |D 	cg c]  }	|	j                          }}	|j                         D 
ci c]  \  }
}|
|j                          }}
}|dk(  r || j                  g|i | | S  || j                  g|i |} |j                  |      S t        | 5  ||||      S # t        $ r d }Y w xY wc c}	w c c}}
w )Nr   )r   )r   __setattr__is_shard
is_partialis_replicate__getattr__zno custom getattr allowed!r~   )r   r   r)   r!   r:   rj   AttributeErrorrk   r   r{   rf   itemsr   rm   )r"   rM   rN   ro   r#   r   constant_fold_functions
value_typemethodxkvconstant_valr$   s                r%   rm   zPlacementVariable.call_method   sP    	'#
 **!$**-
**:}dKS0/0S //
DA (.'..t44489qA((*9D9<BLLNKDAqa--//KFK}$tzz3D3F3!$**>t>v>L*#**<88w"2tT6:: " 
 :Ks   A	D$ D5/D:$D21D2ro   rr   r#   rs   r/   r   )r0   r1   r2   r4   rw   rf   rZ   r   rX   rm   r5   r6   s   @r%   rl   rl      s]    , ,-5 -S -_ -
*; &	*;
 -*; 
*; *;r&   rl   c                   Z     e Zd Zed        Zd Zdddedef fdZ	 	 	 	 	 	 d	 fdZ	 xZ
S )
DeviceMeshVariablec                 P    t         j                         syddlm} t	        | |      S )NFr   )
DeviceMesh)r   r    torch.distributed.device_meshr   r   )r!   r   s     r%   is_device_meshz!DeviceMeshVariable.is_device_mesh   s#     #//1<eZ((r&   c                     | j                   S r(   re   r*   s    r%   rf   z%DeviceMeshVariable.as_python_constant   rg   r&   rM   r   rN   r/   c                     |dk(  r)t        j                  | j                  j                        S |dk(  r)t        j                  | j                  j                        S t
        |   ||      S )Nndimdevice_type)r   r{   r!   r   r   r   rX   r|   s      r%   rX   zDeviceMeshVariable.var_getattr   sX    6>#**4::??;;= #**4::+A+ABBw"2t,,r&   c                    |dk(  r}|D cg c]  }|j                          }}|j                         D ci c]  \  }}||j                          }	}}t        j                   | j                  j
                  |i |	      S |dk(  r-t        j                  | j                  j                               S |dk(  rs|D cg c]  }|j                          }}|j                         D ci c]  \  }}||j                          }	}}t         | j                  j                  |i |	      S |dk(  r#t        | j                  j                               S t        
| -  ||||      S c c}w c c}}w c c}w c c}}w )Nsizeget_coordinate	get_group_get_or_create_default_group)rf   r   r   r{   r!   r   r   rW   r   r   r   rm   )r"   rM   rN   ro   r#   r   
const_argsr   r   const_kwargsr$   s             r%   rm   zDeviceMeshVariable.call_method   s_    6>:>?Q!..0?J?BH,,.Q$!QAq3355QLQ#**?4::??J+W,+WXX###**4::+D+D+FGG;:>?Q!..0?J?BH,,.Q$!QAq3355QLQ'$

$$jALA  11'

(O(O(QRRw"2tT6:: @Q
 @Qs   E(E-?E3+E8r   )r0   r1   r2   r4   r   rf   rZ   r   rX   rm   r5   r6   s   @r%   r   r      s]    ) )-5 -S -_ -; &	;
 -; 
; ;r&   r   c                   R     e Zd ZdZd Z	 	 	 	 	 	 d fdZd fdZed        Z xZ	S )rW   a  
    We don't want a ProcessGroup object to end up in our output graph.

    But it's common for dynamo to intercept a PG that is then used to get info like
    rank() or world_size(), as well as passed to utility functions in distributed_c10d
    which desugar it into plain types like a ranklist and tag.

    For convenience and proper guarding, we construct a variable type.

    TODO: make it possible to use ProcessGroupVariable as input to simple functions
          like _expand_group without dynamo complaining about making a proxy for it.
          It is not a tensor-like type, and we don't want a proxy- but dynamo assumes
          torch library functions are dealing with tensor-like types and would have proxies
          for their args.
    TODO: should we make this inherit VT instead of UDOV? Do we want any of the default behaviors
          or just graph-break whenever one of our special cases is not hit?
    c                     | j                   S r(   re   r*   s    r%   rf   z'ProcessGroupVariable.as_python_constant+  rg   r&   c                    |dk(  r7t         j                  j                  | j                  j	                               S |dk(  r7t         j                  j                  | j                  j                               S |dk(  r7t         j                  j                  | j                  j                               S t        | !  ||||      S )Nrankr   _get_backend_name)	r   r   r{   r!   r   r   r   r   rm   r"   rM   rN   ro   r#   r$   s        r%   rm   z ProcessGroupVariable.call_method.  s     6>--44TZZ__5FGG6>--44TZZ__5FGG&&--44TZZ5Q5Q5STTw"2tT6::r&   c                      dk(  r3t         j                  j                   j                  j                        S dv rt        j
                   fd      S t                 S )N
group_name)r   r   c                  ,    j                  | |      S r(   )rm   )ro   r#   rN   r"   rM   s     r%   <lambda>z2ProcessGroupVariable.var_getattr.<locals>.<lambda>C  s    (8(8T4(P r&   )r   r   r{   r!   r   LambdaVariabler   rX   r|   s   ```r%   rX   z ProcessGroupVariable.var_getattr>  s_    <--44TZZ5J5JKK##++P  w"2t,,r&   c                 `    t         j                         syddlm} ddlm} t        | ||f      S )NFr   )ProcessGroup)FakeProcessGroup)r   r    torch._C._distributed_c10dr   +torch.testing._internal.distributed.fake_pgr   r   )r!   r   r   s      r%   is_process_groupz%ProcessGroupVariable.is_process_groupH  s,     #//1;Pel,<=>>r&   r   rM   r   )
r0   r1   r2   r3   rf   rm   rX   r4   r   r5   r6   s   @r%   rW   rW     sI    $; &	;
 -; 
; - ? ?r&   rW   c            
            e Zd ZdZedededefd       Zdej                  j                  dedededdf
 fd	Z
d
 Zdee   deeef   def fdZddZ xZS )BackwardHookVariablezU
    Handles torch.utils.hooks.BackwardHook for module-level backward
    hooks.
    module
user_hooksuser_pre_hooksc                   	
 t         j                  st        ddddg       dt        f	
fd}| j                  j                  |d      \  }| j                  j                  |      \  
}| j                  j                  |      \  	}| j                  j                  d||fi       }t        j                  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_statec                     t         j                  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)fnr   
hooks_namemodule_name)r-   utilshooksBackwardHook	functoolspartialr   r
   )r   r   user_hooks_nameuser_pre_hooks_names    r%   _in_graph_bw_hooksz7BackwardHookVariable.create.<locals>._in_graph_bw_hooksj  sk     ;;$$11%%%@!)#2$/ %%%@!)#6$/ r&   modrn   r   example_value)r   compiled_autograd_enabledr	   r   outputadd_backward_state_hookcreate_proxyr-   r   r   r   nodemetar   )rM   r   r   r   r   bw_state_proxy_proxyr   r   r   s           @@@r%   r{   zBackwardHookVariable.createY  s     !::Qh		 	: ')ii&G&GPU&V#^!#!B!B>!RQYY>>zJ		&&	
 ,1;;+<+<+I+I$PRTV+W

(#E6:~NNr&   r   r/   Nc                 \    t        |   di | || _        || _        || _        || _        y )Nr   )r   r   r   r   r   r   )r"   r   r   r   r   optionsr$   s         r%   r   zBackwardHookVariable.__init__  s2     	#7#
$,r&   c                     | j                   S r(   )r   r*   s    r%   as_proxyzBackwardHookVariable.as_proxy  rg   r&   ro   r#   c                 ^    |dv r | j                   ||g|i |S t        | 	  ||||      S )N)setup_input_hooksetup_output_hook)_setup_hookr   rm   r   s        r%   rm   z BackwardHookVariable.call_method  sC     <<#4##B>t>v>>w"2tT6::r&   c           
          ddl m}  |||j                  j                  d|| j	                         |j	                         fi             S )Nr   )wrap_fx_proxyrm   )builderr   r   r   r   )r"   rM   hook_method_namero   r   s        r%   r   z BackwardHookVariable._setup_hook  sD    *II"" $--/2	
 	
r&   r   )r0   r1   r2   r3   r4   r   r{   r-   fxProxyr   r   listdictrZ   rm   r   r5   r6   s   @r%   r   r   S  s    
 7O7O $7O (	7O 7Or-xx~~-  - $	-
 (- 
-	; ?#		;
 S/)*	; 
	;
r&   r   )(r3   r   r:   typingr   r-   %torch.fx.experimental._backward_stater   r   r   r   _trace_wrapped_higher_order_opr   excr	   external_utilsr
   guardsr   r   rT   r   r   r   rQ   r   constantr   r   torch._dynamo.symbolic_convertr   r   r=   rF   rH   r\   rl   r   rW   r   r   r&   r%   <module>r      s   (      ? + : " B 0   ! 4 D0/ 0BER.-0 -6 70  7F=;+ =;@*;, *;Z8?. 8?vg
? g
r&   