Ë
    [^(h	  ã                   óž   — 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 erd dlmZ  e j                  e«      ZdgZ G d„ d«      Zy)	é    N)ÚTYPE_CHECKING)Úregister_multi_grad_hook)Úregister_module_forward_hookÚ register_module_forward_pre_hook)Útree_flatten)ÚRemovableHandleÚModuleTrackerc                   ól   — e Zd ZU dZee   ed<   	 dd„Zd„ Ze	d„ «       Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zy)r	   aC  
    ``ModuleTracker`` 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 ModuleTracker() 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))

    ÚparentsNc                 ó”   — dh| _         t        j                  «       | _        t        j                  «       | _        d| _        g | _        y ©NÚGlobalF)r   ÚweakrefÚWeakKeyDictionaryÚ_known_modulesÚWeakSetÚ_seen_modulesÚ_has_callbackÚ_hooks©Úselfs    úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/utils/module_tracker.pyÚ__init__zModuleTracker.__init__?   s:   € Ø �zˆŒÜ9@×9RÑ9RÓ9TˆÔÜ.5¯o©oÓ.?ˆÔØ"ˆÔØ-/ˆ�ó    c                 óž   ‡ — ‰ j                   ry ˆ fd„}t        j                  j                  j                  j                  |«       d‰ _         y )Nc                  ó$   •— dh‰ _         d‰ _        y r   )r   r   r   s   €r   Úcallbackz:ModuleTracker._maybe_set_engine_callback.<locals>.callbackK   s   ø€ Ø$˜:ˆDŒLØ!&ˆDÕr   T)r   ÚtorchÚautogradÚVariableÚ_execution_engineÚqueue_callback)r   r   s   ` r   Ú_maybe_set_engine_callbackz(ModuleTracker._maybe_set_engine_callbackF   s<   ø€ à×ÒØô	'ô 	�‰×Ñ×1Ñ1×@Ñ@ÀÔJØ!ˆÕr   c                 óD   — t         j                  j                  «       dk7  S )z`
        A boolean marking if this is currently running during the backward pass or not
        éÿÿÿÿ)r   Ú_CÚ_current_graph_task_idr   s    r   Úis_bwzModuleTracker.is_bwR   s   € ô
 �x‰x×.Ñ.Ó0°BÑ6Ð6r   c                 óP  — || j                   vr"t        |«      j                  | j                   |<   | j                   |   }|| j                  vrX|j	                  «       D ]*  \  }}|› d|› �| j                   |<   | j                  |«       Œ, | j                  j                  |«       |S )Nú.)r   ÚtypeÚ__name__r   Únamed_childrenÚ_get_mod_nameÚadd)r   ÚmodÚmod_nameÚnameÚsubmods        r   r.   zModuleTracker._get_mod_nameY   s¨   € Ø�d×)Ñ)Ñ)Ü'+¨C£y×'9Ñ'9ˆD×Ñ Ñ$Ø×&Ñ& sÑ+ˆØ�d×(Ñ(Ñ(Ø #× 2Ñ 2Ó 4ò +‘��fØ19°
¸!¸D¸6Ð.B�×#Ñ# FÑ+Ø×"Ñ" 6Õ*ð+ð ×Ñ×"Ñ" 3Ô'Øˆr   c                 ó   ‡ ‡‡— ˆˆˆ fd„}|S )Nc                  ó²   •— ‰r‰j                  «        ‰‰j                  v rt        j                  d‰‰rdnd«       ‰j                  j	                  ‰«       y )NzaThe module hierarchy tracking seems to be broken as this Module was already entered. %s during %sÚbackwardÚforward)r#   r   ÚloggerÚinfor/   ©Úargsr(   r2   r   s    €€€r   Úfnz(ModuleTracker._get_append_fn.<locals>.fne   sK   ø€ ÙØ×/Ñ/Ô1Ø�t—|‘|Ñ#Ü—‘ØwØÙ"'‘J¨Yôð
 �L‰L×Ñ˜TÕ"r   © ©r   r2   r(   r<   s   ``` r   Ú_get_append_fnzModuleTracker._get_append_fnd   s   ú€ ö		#ð ˆ	r   c                 ó   ‡ ‡‡— ˆˆˆ fd„}|S )Nc                  ó�   •— ‰‰j                   v r‰j                   j                  ‰«       y t        j                  d‰‰rdnd«       y )NzhThe Module hierarchy tracking is confused as we're exiting a Module that was never entered. %s during %sr6   r7   )r   Úremover8   r9   r:   s    €€€r   r<   z%ModuleTracker._get_pop_fn.<locals>.fns   s:   ø€ Ø�t—|‘|Ñ#Ø—‘×#Ñ# DÕ)ä—‘Ø~ØÙ"'‘J¨Yõr   r=   r>   s   ``` r   Ú_get_pop_fnzModuleTracker._get_pop_fnr   s   ú€ ö	ð ˆ	r   c           	      óX  — | j                  |«      } | j                  |d«      «        t        |«      \  }}|D �cg c],  }t        |t        j
                  «      sŒ|j                  sŒ+|‘Œ. }}|r6| j                  j                  t        || j                  |d«      «      «       y y c c}w ©NFT)r.   r?   r   Ú
isinstancer   ÚTensorÚrequires_gradr   Úappendr   rC   )r   r0   Úinputr2   r;   Ú_ÚaÚtensorss           r   Ú_fw_pre_hookzModuleTracker._fw_pre_hook   s’   € Ø×!Ñ! #Ó&ˆØ(ˆ×Ñ˜D %Ó(Ô*ä˜uÓ%‰ˆˆaØ"ÖV˜¤j°´E·L±LÕ&AÀaÇoÃo’1ÐVˆÐVÙØ�K‰K×ÑÜ(¨°$×2BÑ2BÀ4ÈÓ2NÓOõð ùò Wó   »B'ÁB'Á(B'c           	      óX  — | j                  |«      } | j                  |d«      «        t        |«      \  }}|D �cg c],  }t        |t        j
                  «      sŒ|j                  sŒ+|‘Œ. }}|r6| j                  j                  t        || j                  |d«      «      «       y y c c}w rE   )r.   rC   r   rF   r   rG   rH   r   rI   r   r?   )	r   r0   rJ   Úoutputr2   r;   rK   rL   rM   s	            r   Ú_fw_post_hookzModuleTracker._fw_post_hookŠ   s’   € Ø×!Ñ! #Ó&ˆØ%ˆ×Ñ˜˜uÓ%Ô'ä˜vÓ&‰ˆˆaØ"ÖV˜¤j°´E·L±LÕ&AÀaÇoÃo’1ÐVˆÐVÙØ�K‰K×ÑÜ(¨°$×2EÑ2EÀdÈDÓ2QÓRõð ùò WrO   c                 ón   — t        | j                  «      | _        t        | j                  «      | _        | S ©N)r   rN   Ú_fw_pre_handler   rR   Ú_fw_post_handler   s    r   Ú	__enter__zModuleTracker.__enter__•   s-   € Ü>¸t×?PÑ?PÓQˆÔÜ;¸D×<NÑ<NÓOˆÔØˆr   c                 óâ   — | j                   j                  «        | j                  j                  «        | j                  D ]  }|j                  «        Œ | j                  j	                  «        y rT   )rU   rB   rV   r   Úclear)r   r;   Úhooks      r   Ú__exit__zModuleTracker.__exit__š   sR   € Ø×Ñ×"Ñ"Ô$Ø×Ñ×#Ñ#Ô%Ø—K‘Kò 	ˆDØ�K‰K�Mð	à�‰×ÑÕr   )ÚreturnN)r,   Ú
__module__Ú__qualname__Ú__doc__ÚsetÚstrÚ__annotations__r   r#   Úpropertyr(   r.   r?   rC   rN   rR   rW   r[   r=   r   r   r	   r	      sZ   … ñð@ �‰XÓðó0ò
"ð ñ7ó ð7ò	òòò	ò	òó
r   )Úloggingr   Útypingr   r   Útorch.autograd.graphr   Útorch.nn.modules.moduler   r   Útorch.utils._pytreer   Útorch.utils.hooksr   Ú	getLoggerr,   r8   Ú__all__r	   r=   r   r   ú<module>rl      sP   ðã Û Ý  ã Ý 9÷õ -ñ Ý1ð 
ˆ×	Ñ	˜8Ó	$€ð Ð
€÷Fò Fr   