+
    –°³j$&  ã                   ó  € R t ^ RIt^ RIt^ RIHtHt ^ RIt^ RIHt ^RI	H
t
 ^RIHt ^RIHt ^RIHt ^RIHtHt ^R	IHt ^R
IHtHtHt ]'       d   ^ RIHt  ! R R]4      tR R ltR R lt ! R R]4      t ! R R]4      t R# )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)ÚGetSetÚMethodÚVariableTracker)ÚInstructionTranslatorBasec                   óˆ   a a€ ] tR t^)t oRtV3R lV 3R lltV3R lR lt]V3R lR l4       tV3R lR	 lt	R
 t
RtVtV ;t# )Ú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.
c                ó*   <€ V ^8„  d   QhRS[ RS[ RR/# )é   ÚvalueÚkwargsÚreturnN)r   )ÚformatÚ__classdict__s   "€Úd/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/_dynamo/variables/distributed.pyÚ__annotate__Ú DistributedVariable.__annotate__4   s"   ø€ ÷ ñ ™cð ©Sð °Tñ ó    c                óˆ   <€ \         SV `  ! R/ VB  \        P                  4       '       g   \	        R RRR.R7       Wn        R# )z+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ÚhintsN© )ÚsuperÚ__init__r   Úis_availabler   r   )Úselfr   r   Ú	__class__s   &&,€r   r&   ÚDistributedVariable.__init__4   sB   ø€ Ü‰ÒÑ"˜6Ò"Ü"×/Ñ/×1Ò1ÜØEØØnà[ðõ	ð Ž
r   c                ó    <€ V ^8„  d   QhRS[ /# ©r   r   ©Útype)r   r   s   "€r   r   r   A   ó   ø€ ÷  ñ  ™Tñ  r   c                ó,   € \        V P                  4      # ©N©r.   r   ©r(   s   &r   Úpython_typeÚDistributedVariable.python_typeA   ó   € Ü�D—J‘JÓÐr   c                ó    <€ V ^8„  d   QhRS[ /# r,   )Úbool)r   r   s   "€r   r   r   E   s   ø€ ÷ 0ñ 0™$ñ 0r   c                 ó>   € \         P                  P                  4       # r1   )ÚtorchÚdistributedr'   r$   r   r   r'   Ú DistributedVariable.is_availableD   s   € ô × Ñ ×-Ñ-Ó/Ð/r   c                ó:   <€ V ^8„  d   QhRRRS[ S[S[3,          /# ©r   Útxr   r   )ÚtupleÚintr8   )r   r   s   "€r   r   r   I   s%   ø€ ÷ 'ñ 'Ð7ð '¹EÁ#ÁtÀ)Õ<Lñ 'r   c                ó0   € \        V P                  4      R 3# )F)Úhashr   )r(   r?   s   &&r   Ú	hash_implÚDistributedVariable.hash_implI   s   € Ü�D—J‘JÓ Ð&Ð&r   c                ó    € ^RI Hp V! WW#4      # )é   )Úobject_richcompare)Úobject_protocolrH   )r(   r?   ÚotherÚoprH   s   &&&& r   Útp_richcompare_implÚ'DistributedVariable.tp_richcompare_implL   s   € Ý7á! $¨EÓ6Ð6r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r&   r4   Ústaticmethodr'   rD   rL   Ú__static_attributes__Ú__classdictcell__Ú__classcell__©r)   r   s   @@r   r   r   )   sF   ù‡ € ñ÷ó ÷ ð  ð ÷0ó ð0÷'ð '÷7ò 7r   r   c                ó0   € V ^8„  d   QhR\         R\        /# ©r   r   r   ©Úobjectr8   )r   s   "r   r   r   R   s   € ÷ Eñ Eœð E¤Dñ Er   c                 óœ   € \         P                  4       '       g   R # ^ RIHp \        P
                  ! V 4      ;'       d    WP                  J # )F)ÚDTensor)r   r'   Útorch.distributed.tensorr]   ÚinspectÚ
isfunctionÚ
from_local)r   r]   s   & r   Úis_from_localrb   R   s9   € Ü×+Ñ+×-Ò-ÙÝ0ä×Ò˜eÓ$×DÐD¨×2DÑ2DÐ)DÐDr   c                ó0   € V ^8„  d   QhR\         R\        /# rY   rZ   )r   s   "r   r   r   Z   s   € ÷ Rñ R¤Fð R¬tñ Rr   c                 ó¨   € \         P                  4       '       g   R # ^ RIHpHpHpHpHp VVVVV.p\        P                  ! V 4      ;'       d    W9   # )F)Ú_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_c10dre   rf   rg   rh   ri   r_   r`   )r   re   rf   rg   rh   ri   Úconstant_processgroup_functionss   &      r   Úis_constant_pg_functionsrl   Z   sU   € Ü×+Ñ+×-Ò-Ù÷õ ð 	 ØØØØ,ð'Ð#ô ×Ò˜eÓ$×QÐQ¨Ñ)QÐQr   c                   óž   a € ] tR t^qt o Rt]V 3R lR l4       tV 3R lR ltV 3R lR ltV 3R lR	 lt	R
]
! ]R4      R]
! ]	R4      /tRtV tR# )ÚWorldMetaClassVariablezt
Tracks torch.distributed.GroupMember and torch.distributed.group, which are
instances of the metaclass _WorldMeta.
c                ó&   <€ V ^8„  d   QhRS[ RS[/# rY   rZ   )r   r   s   "€r   r   Ú#WorldMetaClassVariable.__annotate__x   s   ø€ ÷ )ñ )©ð )±Dñ )r   c                óX   € V P                  4       '       g   R # ^ RIHp \        V4      VJ # )F)Ú
_WorldMeta)r'   rj   rr   r.   )Úclsr   rr   s   && r   Úis_group_member_typeÚ+WorldMetaClassVariable.is_group_member_typew   s&   € à×Ñ×!Ò!ÙåAä�E‹{˜jÐ(Ð(r   c                ó    <€ V ^8„  d   QhRS[ /# r,   r-   )r   r   s   "€r   r   rp   €   r/   r   c                ó,   € \        V P                  4      # r1   r2   r3   s   &r   r4   Ú"WorldMetaClassVariable.python_type€   r6   r   c                ó$   <€ V ^8„  d   QhRRRS[ /# r>   ©r   )r   r   s   "€r   r   rp   …   s    ø€ ÷ Cñ CÐ ;ð CÁñ Cr   c                ó  € V P                   '       g   \        R 4      h\        V P                   RR7      p\        VP	                  \
        P                  4      4       \        P                  ! WP                  P                  V4      # )z<WorldMetaClassVariable requires a source for WORLD attributeÚWORLD©ÚbaseÚmember)ÚsourceÚAssertionErrorr   r
   Ú
make_guardr	   ÚID_MATCHr   Úbuildr   r|   ©r(   r?   r€   s   && r   Ú_world_getsetÚ$WorldMetaClassVariable._world_getset…   sc   € Ø�{�{ˆ{Ü ØNóð ô  §¡°WÔ=ˆÜ�f×'Ñ'¬×(=Ñ(=Ó>Ô?Ü×$Ò$ R¯©×)9Ñ)9¸6ÓBÐBr   c                ó$   <€ V ^8„  d   QhRRRS[ /# r>   rz   )r   r   s   "€r   r   rp   Ž   s!   ø€ ÷ 	Nñ 	NØ-ð	Ná	ñ	Nr   c                ó  € V P                   '       g   \        R 4      h\        V P                   RR7      p\        VP	                  \
        P                  4      4       \        P                  ! WP                  P                  V4      # )zGWorldMetaClassVariable requires a source for NON_GROUP_MEMBER attributeÚNON_GROUP_MEMBERr}   )r€   r�   r   r
   r‚   r	   rƒ   r   r„   r   rŠ   r…   s   && r   Ú_non_group_member_getsetÚ/WorldMetaClassVariable._non_group_member_getsetŽ   sf   € ð �{�{ˆ{Ü ØYóð ô  §¡Ð5GÔHˆÜ�f×'Ñ'¬×(=Ñ(=Ó>Ô?Ü×$Ò$ R¯©×)DÑ)DÀfÓMÐMr   r|   NrŠ   r$   )rN   rO   rP   rQ   rR   Úclassmethodrt   r4   r†   r‹   r   Ú	tp_getsetrT   rU   )r   s   @r   rn   rn   q   sb   ø‡ € ñð
 ÷)ó ð)÷ ð  ÷
Cð C÷	Nð 	Nð 	‘˜ tÓ,Ø™FÐ#;¸TÓBð†Ir   rn   c                   óÜ   a a€ ] tR t^Ÿt oRt]V3R lR l4       tV3R lV 3R lltV3R lR ltV3R lR	 lt	V3R
 lR lt
V3R lR ltR]! ]
4      R]! ]4      /tV3R lR ltRtVtV ;t# )ÚBackwardHookVariablezI
Handles torch.utils.hooks.BackwardHook for module-level backward
hooks.
c          
      ó4   <€ V ^8„  d   QhRRRS[ RS[ RS[ RR/# )r   r?   r   ÚmoduleÚ
user_hooksÚuser_pre_hooksr   r�   rz   )r   r   s   "€r   r   Ú!BackwardHookVariable.__annotate__¦   sB   ø€ ÷ 9Oñ 9OØ'ð9Oáð9Oñ $ð9Oñ (ð	9Oð
 
 ñ9Or   c                óú  aa	a
€ \         P                  '       g   \        R RRR.R7       R VV	V
3R llpV P                  P	                  VR4      w  opV P                  P	                  V4      w  o
pV P                  P	                  V4      w  o	pV P                  P                  RVV3/ 4      p\        P                  P                  P                  RR
R
4      VP                  P                  R	&   \        WqW#4      # )z7Module-level backwards hooks require compiled autograd.r   zREnable compiled autograd by setting torch._dynamo.config.compiled_autograd = True.r   c                ól   € V ^8„  d   QhR\         R\        P                  P                  P                  /# )r   Úbw_stater   )r   r:   ÚutilsÚhooksÚBackwardHook)r   s   "r   r   Ú1BackwardHookVariable.create.<locals>.__annotate__¶   s,   € ÷ 	ñ 	Ü#ð	ä�[‰[×Ñ×+Ñ+ñ	r   c                óâ   <€ \         P                  P                  P                  R\        P
                  ! \        \        V SSR7      3\        P
                  ! \        \        V SSR7      34      # )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)r:   r™   rš   r›   Ú	functoolsÚpartialr   r   )r˜   r    Úuser_hooks_nameÚuser_pre_hooks_names   &€€€r   Ú_in_graph_bw_hooksÚ7BackwardHookVariable.create.<locals>._in_graph_bw_hooks¶   sk   ø€ ô —;‘;×$Ñ$×1Ñ1Øä×%Ò%Ü%Ü@Ø!)Ø#2Ø$/ôðô ×%Ò%Ü%Ü@Ø!)Ø#6Ø$/ôðóð r   ÚmodÚcall_functionNÚexample_valuer$   )r   Úcompiled_autograd_enabledr   ÚoutputÚadd_backward_state_hookÚcreate_proxyr:   r™   rš   r›   ÚnodeÚmetar�   )r?   r’   r“   r”   r¥   Úbw_state_proxyÚ_Úproxyr    r£   r¤   s   &&&&    @@@r   ÚcreateÚBackwardHookVariable.create¥   sä   ú€ ô !×:×:Ð:ÜØQØØàhðõ	÷	ò 	ð> ')§i¡i×&GÑ&GÈÐPUÓ&VÑ#ˆ�^Ø!#§¡×!BÑ!BÀ>Ó!RÑÐ˜QØŸY™Y×>Ñ>¸zÓJÑˆ˜Ø—	‘	×&Ñ&ØØØÐØó	
ˆô ,1¯;©;×+<Ñ+<×+IÑ+IÈ$ÐPRÐTVÓ+Wˆ�
‰
�‰˜Ñ(Ü# E°:ÓNÐNr   c                ód   <€ V ^8„  d   QhRS[ P                  P                  RS[RS[RS[RS[RR/# )r   r²   r’   r“   r”   Úoptionsr   N)r:   ÚfxÚProxyr   r   )r   r   s   "€r   r   r•   á   sO   ø€ ÷ -ñ -á�x‰x�~‰~ð-ñ  ð-ñ $ð	-ñ
 (ð-ñ ð-ð 
ñ-r   c                óX   <€ \         SV `  ! R/ VB  Wn        W n        W0n        W@n        R # )Nr$   )r%   r&   r²   r’   r“   r”   )r(   r²   r’   r“   r”   r¶   r)   s   &&&&&,€r   r&   ÚBackwardHookVariable.__init__á   s*   ø€ ô 	‰ÒÑ#˜7Ò#ØŒ
ØŒØ$ŒØ,Ör   c                óH   <€ V ^8„  d   QhRS[ P                  P                  /# r,   )r:   r·   r¸   )r   r   s   "€r   r   r•   ï   s   ø€ ÷ ñ ™%Ÿ(™(Ÿ.™.ñ r   c                ó   € V P                   # r1   )r²   r3   s   &r   Úas_proxyÚBackwardHookVariable.as_proxyï   s   € Ø�z‰zÐr   c                ó    <€ V ^8„  d   QhRS[ /# r,   r-   )r   r   s   "€r   r   r•   ò   s   ø€ ÷ .ñ .™Tñ .r   c                óJ   € \         P                  P                  P                  # r1   )r:   r™   rš   r›   r3   s   &r   r4   Ú BackwardHookVariable.python_typeò   s   € Ü�{‰{× Ñ ×-Ñ-Ð-r   c                óV   <€ V ^8„  d   QhRRRS[ S[,          RS[S[S[3,          RS[/# ©r   r?   r   Úargsr   r   ©Úlistr   ÚdictÚstr)r   r   s   "€r   r   r•   õ   sH   ø€ ÷ Iñ Ià'ðIñ ‘?Õ#ðIñ ‘S™/Ð)Õ*ð	Iñ
 
ñIr   c                ó0   € V P                   ! VR .VO5/ VB # )Úsetup_input_hook©Ú_setup_hook©r(   r?   rÄ   r   s   &&&&r   rÊ   Ú%BackwardHookVariable.setup_input_hookõ   s"   € ð ×Ò Ð$6ÐH¸ÒHÀÑHÐHr   c                óV   <€ V ^8„  d   QhRRRS[ S[,          RS[S[S[3,          RS[/# rÃ   rÅ   )r   r   s   "€r   r   r•   ý   sH   ø€ ÷ Jñ Jà'ðJñ ‘?Õ#ðJñ ‘S™/Ð)Õ*ð	Jñ
 
ñJr   c                ó0   € V P                   ! VR .VO5/ VB # )Úsetup_output_hookrË   rÍ   s   &&&&r   rÑ   Ú&BackwardHookVariable.setup_output_hooký   s"   € ð ×Ò Ð$7ÐI¸$ÒIÀ&ÑIÐIr   rÊ   rÑ   c                ó0   <€ V ^8„  d   QhRRRS[ RS[RS[/# )r   r?   r   Úhook_method_namerÄ   r   )rÈ   r   )r   r   s   "€r   r   r•   
  s3   ø€ ÷ 
ñ 
à'ð
ñ ð
ñ ð	
ñ
 
ñ
r   c           
     ó”   € ^RI Hp V! VVP                  P                  RVV P	                  4       VP	                  4       3/ 4      4      # )rG   )Úwrap_fx_proxyÚcall_method)ÚbuilderrÖ   r«   r­   r½   )r(   r?   rÔ   rÄ   rÖ   s   &&&& r   rÌ   Ú BackwardHookVariable._setup_hook
  sF   € õ 	+áØØ�I‰I×"Ñ"ØØ Ø—‘“ $§-¡-£/Ð2Øó	ó
ð 	
r   )r’   r²   r“   r”   )rN   rO   rP   rQ   rR   rS   r³   r&   r½   r4   rÊ   rÑ   r   Ú
tp_methodsrÌ   rT   rU   rV   rW   s   @@r   r�   r�   Ÿ   s…   ù‡ € ñð
 ÷9Oó ð9O÷v-ó -÷ð ÷.ð .÷Ið I÷Jð Jð 	™FÐ#3Ó4Ø™VÐ$5Ó6ð€J÷

÷ 
ð 
r   r�   )!rR   r¡   r_   Útypingr   r   r:   Ú%torch.fx.experimental._backward_stater   r   r   Ú_trace_wrapped_higher_order_opr   Úexcr   Úexternal_utilsr   Úguardsr	   r
   r€   r   r~   r   r   r   Útorch._dynamo.symbolic_convertr   r   rb   rl   rn   r�   r$   r   r   Ú<module>râ      sp   ðñó( Û ß %ã Ý ?å  Ý :Ý Ý Bß 0Ý ß 1Ñ 1÷ ÝHô&7˜/ô &7õREõRô.+Ð0ô +ô\{
˜?ö {
r   