+
    –°³j(  ã                   ól   € ^ RI t ^ RIt^ RIHt ^ RIt^ RIHt ^ RIHtH	t	 ^ RI
Ht R.t ! R R4      tR# )é    N©ÚCallable)Úregister_multi_grad_hook)Úregister_module_forward_hookÚ register_module_forward_pre_hook)Útree_flattenÚ
ModTrackerc                   óš   a € ] tR t^t o Rt R tR t]R 4       tR t	RV 3R lR llt
R	 tR
 tR tR tR tR tR tR tV 3R ltRtV tR# )r	   aæ  
``ModTracker`` is a context manager that tracks the nn.Module hierarchy during execution
so that other system can query which Module is currently being executed (or its backward is being
executed).

You can access the ``parents`` attribute on this context manager to get the set of all the
Modules currently being executed via their fqn (fully qualified name, also used as the key within
the state_dict).
You can access the ``is_bw`` attribute to know if you are currently running in backward or not.

Note that ``parents`` is never empty and always contains the "Global" key. The ``is_bw`` flag
will remain ``True`` after the forward until another Module is executed. If you need it to be
more accurate, please submit an issue requesting this. Adding a map from fqn to the module instance
is possible but not done yet, please submit an issue requesting this if you need it.

Example usage

.. code-block:: python

    mod = torch.nn.Linear(2, 2)

    with ModTracker() as tracker:
        # Access anything during the forward pass
        def my_linear(m1, m2, bias):
            print(f"Current modules: {tracker.parents}")
            return torch.mm(m1, m2.t()) + bias

        torch.nn.functional.linear = my_linear

        mod(torch.rand(2, 2))

c                óà   € R 0V n         / V n        \        P                  ! 4       V n        \        P
                  ! 4       V n        RV n        . V n        RV n	        RV n
        RV n        RV n        R# ©ÚGlobalFN)ÚparentsÚ_active_module_cntÚweakrefÚWeakKeyDictionaryÚ_known_modulesÚWeakSetÚ_seen_modulesÚ_has_callbackÚ_post_bw_callbacks_to_enqueueÚ_user_pre_fw_hookÚ_user_post_fw_hookÚ_user_pre_bw_hookÚ_user_post_bw_hook©Úselfs   &Úe/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/distributed/_tools/mod_tracker.pyÚ__init__ÚModTracker.__init__9   sc   € Ø �zˆŒØ"$ˆÔÜ9@×9RÒ9RÓ9TˆÔÜ.5¯oªoÓ.?ˆÔØ"ˆÔØ=?ˆÔ*Ø!%ˆÔØ"&ˆÔØ!%ˆÔØ"&ˆÖó    c                ó‚  a € S P                   '       d   R # \        S P                  4       F6  p\        P                  P
                  P                  P                  V4       K8  	  S P                  P                  4        V 3R lp\        P                  P
                  P                  P                  V4       RS n         R # )Nc                  ó&   <€ R 0S n         RS n        R# r   )r   r   r   s   €r   ÚcallbackÚ7ModTracker._maybe_set_engine_callback.<locals>.callbackN   s   ø€ Ø$˜:ˆDŒLØ!&ˆDÖr    T)	r   Úreversedr   ÚtorchÚautogradÚVariableÚ_execution_engineÚqueue_callbackÚclear)r   Úpost_bw_callbackr#   s   f  r   Ú_maybe_set_engine_callbackÚ%ModTracker._maybe_set_engine_callbackE   s‡   ø€ à××ÐÙä (¨×)KÑ)KÖ LÐÜ�N‰N×#Ñ#×5Ñ5×DÑDÐEUÖVñ !Mà×*Ñ*×0Ñ0Ô2õ	'ô 	�‰×Ñ×1Ñ1×@Ñ@ÀÔJØ!ˆÖr    c                óD   € \         P                  P                  4       R8g  # )zP
A boolean marking if this is currently running during the backward pass or not
éÿÿÿÿ)r&   Ú_CÚ_current_graph_task_idr   s   &r   Úis_bwÚModTracker.is_bwU   s   € ô
 �x‰x×.Ñ.Ó0°BÑ6Ð6r    c                ó:   € V P                   P                  VR4      # )z_
Return the fqn for the given module if it is known to the ``ModTracker``, otherwise ``None``.
N)r   Úget)r   Úmods   &&r   Úget_known_fqnÚModTracker.get_known_fqn\   s   € ð ×"Ñ"×&Ñ& s¨DÓ1Ð1r    Nc          	      ój   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          /# )é   Úpre_fw_hookNÚpost_fw_hookÚpre_bw_hookÚpost_bw_hookr   )ÚformatÚ__classdict__s   "€r   Ú__annotate__ÚModTracker.__annotate__b   sC   ø€ ÷ 3
ñ 3
á •_ð3
ñ  •oð3
ñ  •_ð	3
ñ
  •oñ3
r    c                óÌ   € R pV! WP                   R4      V n         V! W P                  R4      V n        V! W0P                  R4      V n        V! W@P                  R4      V n        R# )a…  
Registers user-specified hooks to be called before/after the forward/backward pass for each
module tracked by the ``ModTracker``. One or more can be ``None``.
Args:
    pre_fw_hook (Callable, optional): A hook to be called before the forward pass for the
        module. It should have the following signature:
        pre_fw_hook (module, input) -> None
    post_fw_hook (Callable, optional): A hook to be called after the forward pass for the
        module. It should have the following signature:
        post_fw_hook (module, input, output) -> None
    pre_bw_hook (Callable, optional): A multi-grad hook to be called on all the outputs of
        the module that require gradients. It should have the following signature:
        pre_bw_hook (module, grad_output) -> None
    post_bw_hook (Callable, optional): A multi-grad hook to be called on all the inputs of
        the module that require gradients. It should have the following signature:
        post_bw_hook (module, grad_input) -> None
Raises:
    AssertionError: If a new hook is provided when one is already registered.
Note:
    If the module is not alive during the backward pass, the pre_bw_hook and post_bw_hook will
    receive None as the module argument.
    The module fqn will be present in the ``parents`` attribute when each of the hooks is called.
    Hooks are intended to be used as markers only not to modify the inputs/outputs.
c                 ó4   € V e   Ve   \        RV R24      hV # )Nz	Only one zq can be registered at a time Clear the existing hook by calling ``clear_user_hooks`` before registering a new one)ÚAssertionError)ÚhookÚ	user_hookÚ	hook_names   &&&r   Úset_hookÚ0ModTracker.register_user_hooks.<locals>.set_hook‚   s4   € ØÒ IÒ$9Ü$Ø 	˜{ð +lð móð ð ˆKr    r<   r=   r>   r?   N©r   r   r   r   )r   r<   r=   r>   r?   rJ   s   &&&&& r   Úregister_user_hooksÚModTracker.register_user_hooksb   sp   € ò@	ñ "*Ø×/Ñ/°ó"
ˆÔñ #+Ø×1Ñ1°>ó#
ˆÔñ "*Ø×/Ñ/°ó"
ˆÔñ #+Ø×1Ñ1°>ó#
ˆÖr    c                ó>   € RV n         RV n        RV n        RV n        R# )zI
Clears the user specified hooks registered with ``register_user_hooks``
NrL   r   s   &r   Úclear_user_hooksÚModTracker.clear_user_hooks—   s$   € ð "&ˆÔØ"&ˆÔØ!%ˆÔØ"&ˆÖr    c                ób  € WP                   9  d#   \        V4      P                  V P                   V&   V P                   V,          pWP                  9  dZ   VP	                  4        F*  w  r4V R V 2V P                   V&   V P                  V4       K,  	  V P                  P                  V4       V# )Ú.)r   ÚtypeÚ__name__r   Únamed_childrenÚ_get_mod_nameÚadd)r   r7   Úmod_nameÚnameÚsubmods   &&   r   rW   ÚModTracker._get_mod_name    s�   € Ø×)Ñ)Ô)Ü'+¨C£y×'9Ñ'9ˆD×Ñ Ñ$Ø×&Ñ& sÕ+ˆØ×(Ñ(Ô(Ø #× 2Ñ 2Ö 4‘�Ø19°
¸!¸D¸6Ð.B�×#Ñ# FÑ+Ø×"Ñ" 6Ö*ñ !5ð ×Ñ×"Ñ" 3Ô'Øˆr    c                ó    a aaa€ VVV V3R  lpV# )c                  óæ  <€ S'       d   SP                  4        SSP                  9   d;   SP                  '       g)   RR lpV\        n        \        P
                  ! R^R7       SSP                  9  d,   ^SP                  S&   SP                  P                  S4       MSP                  S;;,          ^,          uu&   SP                  e$   S'       d   SP                  S! 4       V 4       R # R # R # )Nc                 ó2   € V R V RVP                    RV  R2# )Ú:z: z 
)rU   )ÚmsgÚcategoryÚfilenameÚlinenoÚlines   &&&&&r   Úcustom_formatwarningÚCModTracker._get_append_fn.<locals>.fn.<locals>.custom_formatwarning±   s(   € Ø&˜Z q¨¨°°8×3DÑ3DÐ2EÀRÈÀuÈCÐPÐPr    zbThe module hierarchy tracking maybe be messed up. Please file a bug to PyTorch, if it is the case.)Ú
stacklevel©N)	r-   r   r3   ÚwarningsÚformatwarningÚwarnr   rX   r   )Úargsrf   r3   rZ   r   Úw_mods   * €€€€r   ÚfnÚ%ModTracker._get_append_fn.<locals>.fn¬   s¹   ø€ ßØ×/Ñ/Ô1Ø�t—|‘|Ô#¨D¯J¯J¨JôQð *>”Ô&Ü—’ðHà õð
 ˜4Ÿ<™<Ô'Ø01�×'Ñ'¨Ñ-Ø—‘× Ñ  Õ&à×'Ñ'¨×-°Õ2Ó-à×%Ñ%Ò1·eØ×&Ñ&¡u£w°Ö5ñ 7<Ñ1r    © ©r   rn   rZ   r3   ro   s   ffff r   Ú_get_append_fnÚModTracker._get_append_fn«   s   û€ ÷	6ð 	6ð0 ˆ	r    c                ó    a aaa€ VVV V3R  lpV# )c                  ój  <€ SP                   e    S'       d   SP                  S! 4       V 4       SSP                  9   dW   SP                  S;;,          ^,          uu&   SP                  S,          ^ 8X  d   SP                  P                  S4       R # R # SP                  '       g   \        R4      hR # )Nz?The Module hierarchy tracking is wrong. Report a bug to PyTorch)r   r   r   Úremover3   ÚRuntimeError)rm   r3   rZ   r   rn   s   *€€€€r   ro   Ú"ModTracker._get_pop_fn.<locals>.fnÇ   s�   ø€ Ø×&Ñ&Ò2·uØ×'Ñ'©«°Ô6Ø�t—|‘|Ô#Ø×'Ñ'¨×-°Õ2Ó-Ø×*Ñ*¨4Õ0°AÔ5Ø—L‘L×'Ñ'¨Ö-ñ 6à—Z—Z�Zô #ØUóð ñ  r    rq   rr   s   ffff r   Ú_get_pop_fnÚModTracker._get_pop_fnÆ   s   û€ ÷	ð 	ð ˆ	r    c                óš  € \         P                  P                  P                  4       '       d   R # V P	                  V4      p\
        P                  ! V4      pV P                  WCR4      ! 4        V P                  e   V P                  W4       \        V4      w  rVV Uu. uF;  p\        V\         P                  4      '       g   K%  VP                  '       g   K9  VNK=  	  ppV P                  '       gS   V'       d   \        W€P                  WCR4      4       R # V P                   P#                  V P                  WCR4      4       R # R # u upi )NFT)r&   Ú_dynamoÚ
eval_frameÚ_is_in_optimized_modulerW   r   Úrefrs   r   r   Ú
isinstanceÚTensorÚrequires_gradr3   r   rz   r   Úappend)	r   r7   ÚinputrZ   rn   rm   Ú_ÚaÚtensorss	   &&&      r   Ú_fw_pre_hookÚModTracker._fw_pre_hook×   sñ   € Ü�=‰=×#Ñ#×;Ñ;×=Ò=Ùà×!Ñ! #Ó&ˆÜ—’˜CÓ ˆØ×Ñ˜E¨Ô/Ô1Ø×!Ñ!Ò-Ø×"Ñ" 3Ô.Ü˜uÓ%‰ˆÙ"ÓV™d˜¤j°´E·L±L×&A”1ÀaÇoÅo—1�1™dˆÐVØ�z�zˆzßÜ(¨×2BÑ2BÀ5ÐPTÓ2UÖVà×2Ñ2×9Ñ9Ø×$Ñ$ U°$Ó7öñ	 ùò Ws   Â"EÃEÃEc                óJ  € \         P                  P                  P                  4       '       d   R # V P	                  V4      p\
        P                  ! V4      pV P                  e   V P                  WV4       V P                  WTR4      ! 4        \        V4      w  rgV Uu. uF;  p\        V\         P                  4      '       g   K%  VP                  '       g   K9  VNK=  	  p	pV P                  '       g*   V	'       d    \        W�P                  WTR4      RR7       R # R # R # u upi )NFTÚany)Úmode)r&   r}   r~   r   rW   r   r€   r   rz   r   r�   r‚   rƒ   r3   r   rs   )
r   r7   r…   ÚoutputrZ   rn   rm   r†   r‡   rˆ   s
   &&&&      r   Ú_fw_post_hookÚModTracker._fw_post_hookê   sÔ   € Ü�=‰=×#Ñ#×;Ñ;×=Ò=Ùà×!Ñ! #Ó&ˆÜ—’˜CÓ ˆØ×"Ñ"Ò.Ø×#Ñ# C°Ô7Ø×Ñ˜ eÔ,Ô.Ü˜vÓ&‰ˆÙ"ÓV™d˜¤j°´E·L±L×&A”1ÀaÇoÅo—1�1™dˆÐVØ�z�zˆzŸgÜ$Ø×,Ñ,¨U¸$Ó?Àe÷ñ &‰zùò Ws   Â "D ÃD ÃD c                ór   € \        V P                  4      V n        \        V P                  R R7      V n        V # )T)Úalways_call)r   r‰   Ú_fw_pre_handler   r�   Ú_fw_post_handler   s   &r   Ú	__enter__ÚModTracker.__enter__ú   s4   € Ü>¸t×?PÑ?PÓQˆÔÜ;Ø×Ñ¨Dô 
ˆÔð ˆr    c                ón   € V P                   P                  4        V P                  P                  4        R # ri   )r“   rw   r”   )r   rm   s   &*r   Ú__exit__ÚModTracker.__exit__  s&   € Ø×Ñ×"Ñ"Ô$Ø×Ñ×#Ñ#Ö%r    c                ó6   <€ V ^8„  d   Qh/ S[ S[,          ;R&   # )r;   r   )ÚsetÚstr)r@   rA   s   "€r   rB   rC      s   ø‡ ‚ ñD ‘�XÑòE r    )r   r”   r“   r   r   r   r   r   r   r   r   r   )NNNN)rU   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r-   Úpropertyr3   r8   rM   rP   rW   rs   rz   r‰   r�   r•   r˜   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)rA   s   @r   r	   r	      sn   ø‡ € ñðDò
'ò"ð  ñ7ó ð7ò2÷3
ò 3
òj'ò	òò6ò"ò&ò ò&÷_ ƒ r    )rj   r   Úcollections.abcr   r&   Útorch.autograd.graphr   Útorch.nn.modules.moduler   r   Útorch.utils._pytreer   Ú__all__r	   rq   r    r   Ú<module>rª      s3   ðã Û Ý $ã Ý 9÷õ -ð ˆ.€÷q&ó q&r    