
    Ǆg                        d dl mZ d dlmZ d dl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)GraphModule)_format_import_block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    c/home/mcse/projects/flask_80/flask-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:nn6M6M23x("01G1G1I0J K+K,?,?ARARS"TYY.##$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.fxr   torch.fx.graph_moduler   r   r   torch.packager   r	   _compatibilityr   r   r   r   boolr   r   r    r   r   r   r   <module>ra      s    %   
 8 ) $ %* " e,3  -3" e,	,t 	,  -	, e,L -L 04 - e,x!{ x! -x!r   