Ë
    f^(hö  ã                   óú   — d dl mZ d dlmZmZmZmZ d dlmZm	Z	 ddl
mZ dada ed¬«      ed„ «       «       Z ed¬«      ed	efd
„«       «       Z ed¬«      d„ «       Zddœd„Z ed¬«       G d„ de«      «       Zy)é    )Úcontextmanager)Ú_format_import_blockÚGraphModuleÚreduce_graph_moduleÚreduce_package_graph_module)ÚPackageExporterÚsys_importeré   )ÚcompatibilityF)Úis_backward_compatiblec               #   ó8   K  — 	 t         } da d–— | a y#  a w xY w­w)a@  
    Skip using lazy graph module disregarding the setting of _use_lazy_graph_module.
    Use to skip _LazyGraphModule when testing inductor torchscript related backend.

    torch.jit.script a _LazyGraphModule results in following error:
        https://gist.github.com/shunting314/5143654c8084aed84ecd19b818258a69
    TN)Ú"_force_skip_lazy_graph_module_flag)Úpriors    úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/_lazy_graph_module.pyÚ_force_skip_lazy_graph_moduler      s%   è ø€ ð3ä2ˆØ-1Ð*Ûà-2Ñ*ø¨UÑ*üs   ‚„ �“—Ú
should_usec              #   óJ   K  — 	 t         }| xr t         a d –— |a y # a w xY w­w©N)Ú_use_lazy_graph_module_flagr   )r   r   s     r   Ú_use_lazy_graph_moduler   &   s2   è ø€ ð,ä+ˆàÒAÔAÐAð 	$ó 	à&+Ñ#ø eÑ#üs   ‚#„ ™#œ  #c                  ó&   — t         rt        S t        S r   )r   Ú_LazyGraphModuler   © ó    r   Ú_get_graph_module_clsr   4   s   € å:ÔÐKÄÐKr   N)Úgraph_module_clsc                 ó*   — | €
t        «       }  | |i |¤ŽS r   )r   )r   ÚargsÚkwargss      r   Ú_make_graph_moduler    9   s!   € ØÐÜ0Ó2Ðá˜TÐ, VÑ,Ð,r   c                   ó´   ‡ — e Zd ZdZedefd„«       Zed„ «       Zd„ Z	ed„ «       Z
d„ ZeZdefd	„Zd
„ Zˆ fd„Zed„ «       Zedefˆ fd„«       Zdefˆ fd„Zˆ xZS )r   a!  
    The main difference between _LazyGraphModule and GraphModule is how recompile happens.
    GraphModule will do a 'recompile' call to generate python code and the forward method when it's
    constructed. Later on if the graph get updated, recompile method can be called again to refresh
    the saved python code and forward method.

    However in some cases especially in inductor, the recompilation can be a waste since we never
    check the python code for the graph module or call its forward method. A few more concreate
    examples regarding pattern matching fx passes in inductor:
    1. some passes will update the graph to be compiled and then call recompile on the GraphModule.
    2. some passes will trace small pattern function to search it in the graph being compiled and
       replace the match with the traced graph of a replacement function. The pattern graph and
       replacement graph are quite small but there are large amount of them. Doing GraphModule.recompile
       for them in GraphModule.__init__ is also a waste of time.

    However simply skip calling GraphModule.recompile in these scenarios is also dangeruous.
    People may want to check the python code or call the GraphModule's forward method for debugging purposes.

    The way _LazyGraphModule solves it is, we override the recompile method to just mark the
    need for recompilation but does not do the actual recompilation. Later on if people really
    access the compiled python code or call the GraphModule's forward method, we do the real
    recompilation.
    Úgmc                 óR   — t        |t        «      r|S t        ||j                  «      S r   )Ú
isinstancer   Úgraph)Úclsr"   s     r   Úfrom_graphmodulez!_LazyGraphModule.from_graphmoduleZ   s#   € ä�bÔ*Ô+ØˆIä# B¨¯©Ó1Ð1r   c                 óF   — t        | t        «      r| j                  «        yy)zë
        Sometimes we need force a recompile as a workaround
        - we want to do the real recompilation before symbolic_trace to avoid error:
            https://gist.github.com/shunting314/75549c2e82ae07ac1139c94a3583d259
        N)r$   r   Úreal_recompile)r"   s    r   Úforce_recompilez _LazyGraphModule.force_recompilea   s    € ô �bÔ*Ô+Ø×ÑÕð ,r   c                 óF   — | j                  «       r| j                  «        y y r   )Ú_needs_recompileÚ_real_recompile)Úselfs    r   r)   z_LazyGraphModule.real_recompilek   s   € Ø× Ñ Ô"Ø× Ñ Õ"ð #r   c                 ó2   — | j                   | j                  u S r   )ÚforwardÚ_lazy_forward©r&   s    r   r,   z!_LazyGraphModule._needs_recompileo   s   € à�{‰{˜c×/Ñ/Ð/Ð/r   c                 óV   — | j                  «        | j                  «       rJ ‚ | |i |¤ŽS r   )r)   r,   )r.   r   r   s      r   r1   z_LazyGraphModule._lazy_forwards   s4   € ð
 	×ÑÔØ×(Ñ(Ô*Ð*Ð*ñ
 �TÐ$˜VÑ$Ð$r   Úexporterc                 óJ  — | j                  «       }| j                  j                  «       }| j                  j                  |d<   |d= d|j                  «       › �}t        |j                  |j                  «      }|| j                  z   }|j                  ||«       t        ||ffS )úø
        Follow GraphModule.__reduce__ but call 'self._real_recompile' rather
        than 'self.recompile' since for a _LazyGraphModule, self.recompile just
        mark the need of recompilation and does not return the PythonCode object.
        Ú_graphmodule_cls_nameÚ_graphzfx-generated._)r-   Ú__dict__ÚcopyÚ	__class__Ú__name__Úget_unique_idr   ÚglobalsÚimporterÚcodeÚsave_source_stringr   )r.   r4   Úpython_codeÚdict_without_graphÚgenerated_module_nameÚimport_blockÚmodule_codes          r   Ú__reduce_package__z#_LazyGraphModule.__reduce_package__‡   s§   € ð ×*Ñ*Ó,ˆØ!Ÿ]™]×/Ñ/Ó1ÐØ6:·n±n×6MÑ6MÐÐ2Ñ3Ø˜xÐ(à"0°×1GÑ1GÓ1IÐ0JÐ KÐÜ+¨K×,?Ñ,?À×ARÑARÓSˆØ" T§Y¡YÑ.ˆØ×#Ñ#Ð$9¸;ÔGä'ØÐ!6Ð7ð
ð 	
r   c                 ó¤   — | j                  «       }| j                  j                  «       }t        |j                  t
        «      }|d= t        ||ffS )r6   r8   )r-   r9   r:   r   r>   r	   r   )r.   rB   rC   rE   s       r   Ú
__reduce__z_LazyGraphModule.__reduce__›   sP   € ð ×*Ñ*Ó,ˆØ!Ÿ]™]×/Ñ/Ó1ÐÜ+¨K×,?Ñ,?ÄÓNˆØ˜xÐ(Ü#Ð&8¸,Ð%GÐHÐHr   c                 ó    •— t         ‰| �  «       S r   )ÚsuperÚ	recompile©r.   r;   s    €r   r-   z _LazyGraphModule._real_recompile§   s   ø€ Ü‰wÑ Ó"Ð"r   c                 ó&   — | j                   | _        y r   )r1   r0   r2   s    r   rL   z_LazyGraphModule.recompileª   s   € à×'Ñ'ˆ�r   Úreturnc                 ó8   •— | j                  «        t        ‰| �  S r   )r)   rK   r@   rM   s    €r   r@   z_LazyGraphModule.code®   s   ø€ à×ÑÔÜ‰w‰|Ðr   c                 ó@   •— | j                  «        t        ‰| �	  «       S )zˆ
        str(GraphModule) will access the _code attribute. Make sure recompile
        happens so _code attribute is available.
        )r)   rK   Ú__str__rM   s    €r   rR   z_LazyGraphModule.__str__³   s   ø€ ð
 	×ÑÔÜ‰w‰Ó Ð r   )r<   Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r'   Ústaticmethodr*   r)   r,   r1   r0   r   rG   rI   r-   rL   ÚpropertyÚstrr@   rR   Ú__classcell__)r;   s   @r   r   r   @   s³   ø„ ñð0 ð2 +ò 2ó ð2ð ñ ó ð ò#ð ñ0ó ð0ò%ð €Gð
¨?ó 
ò(
Iô#ð ñ(ó ð(ð ð�cô ó ðð!˜÷ !ñ !r   r   )Ú
contextlibr   Útorch.fx.graph_moduler   r   r   r   Útorch.packager   r	   Ú_compatibilityr   r   r   r   Úboolr   r   r    r   r   r   r   ú<module>r`      s¹   ðå %÷ó ÷ 8å )ð $Ð Ø%*Ð "ñ  eÔ,Øñ3ó ó -ð3ñ"  eÔ,Øð	, tò 	,ó ó -ð	,ñ  eÔ,ñLó -ðLð 04ô -ñ  eÔ,ôx!�{ó x!ó -ñx!r   