
    Ǆg              	       \   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZmZmZmZmZmZm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mZmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z% g d	Z&d
Z' G d d      Z( e(       Z)d/de*dee*ef   fdZ+d/de*dee*ef   fdZ,	 d/de*de*dee*ef   defdZ-de*dedede*fdZ.dee*ef   defdZ/ ed      deeef   de*dej*                  j`                  fd       Z1 ed      dedeeef   de*dej*                  j`                  fd       Z2 ed      dedeeef   de*dej*                  j`                  fd        Z3 G d! d"ej*                  j`                        Z4d/deeef   dej*                  j`                  fd#Z5d$ej*                  j`                  d%ej*                  j`                  d&e*fd'Z6d(ed%ej*                  j`                  d&e*fd)Z7	 	 	 	 d0d*Z8 G d+ d,      Z9 ed       G d- d.ej*                  j`                               Z:y)1    N)Path)AnyCallableDictListOptionalSetTypeUnion)
_addindent)ImporterPackageExporterPackageImportersys_importer   )compatibility)_custom_builtins_is_from_torch_PyTreeCodeGenGraph
PythonCode)reduce_graph_modulereduce_package_graph_modulereduce_deploy_graph_moduleGraphModule_user_preserved_attributesc                   F    e Zd Zd Zd	dedeeef   fdZdee   fdZ	d Z
y)
_EvalCacheLoaderc                      i | _         d| _        y )Nr   )
eval_cachenext_idselfs    ]/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/fx/graph_module.py__init__z_EvalCacheLoader.__init__$   s        Nsrcglobalsc                     | j                         }|r|d|d    d|d    d|d    z  }|| j                  |<   |j                         }||d<   ||d<   | |d	<   t        j                  ||       |S )
aV  Store the source in a private cache, and add a lazy entry in linecache
        that allows the source to be retrieved by 'filename'.

        Args:
            src (str): The module source to cache
            globals (dict): The module globals

        Returns:
            str: The cache key (and dummy filename) generated for src.
        z from co_filename:co_firstlinenoz in co_name__file____name__
__loader__)_get_keyr    copy	linecache	lazycache)r#   r'   r(   	co_fieldskeyglobals_copys         r$   cachez_EvalCacheLoader.cache(   s     mmoVIm45QyAQ7R6SSWXabkXlWmnnC"
 ||~#&Z #&Z %)\"C.
r&   returnc                 >    || j                   v r| j                   |   S y N)r    )r#   module_names     r$   
get_sourcez_EvalCacheLoader.get_sourceF   s     $//)??;//r&   c                 N    d| j                    }| xj                   dz  c_         |S )Nz<eval_with_key>.r   )r!   )r#   r6   s     r$   r1   z_EvalCacheLoader._get_keyK   s%     /
r&   r;   )r/   
__module____qualname__r%   strr   r   r8   r   r=   r1    r&   r$   r   r   #   s7     tCH~ <# 
r&   r   r'   r(   c                 `    t         j                  | ||      }t        t        | |d      |       y )Nexec)_loaderr8   rD   compile)r'   r(   r5   r6   s       r$   _exec_with_sourcerG   T   s'    
--Wi
0Cc6	"G,r&   c                      t        d| ||      S )Nforward)method_namer'   r(   r5   )_method_from_src)r'   r(   r5   s      r$   _forward_from_srcrL   Y   s    39 r&   rJ   r9   c                 P    |j                         }t        |||       ||    }|| = |S r;   )r2   rG   )rJ   r'   r(   r5   r7   fns         r$   rK   rK   _   s2     <<>Lc<3	k	"B[!Ir&   nameobjimporterc                     | t         v rt         |    j                  S t        |       ry|j                  |      \  }}d| d| d|  S )Nzimport torchzfrom z import z as )r   
import_strr   get_name)rO   rP   rQ   r<   	attr_names        r$   _format_import_statementrV   j   sU    %000d%..s3K;-x	{$tf==r&   c           	          | j                         D ch c]  \  }}t        |||       }}}dj                  t        |            S c c}}w )N
)itemsrV   joinsorted)r(   rQ   rO   rP   import_strss        r$   _format_import_blockr]   s   sI    \c\i\i\klytUX5dCJlKl 99VK()) ms   ATis_backward_compatiblebodyimport_blockc                 h    | j                  d      xs | d   }t        ||z   i       }t        ||       S )N_codecode)getrL   _deserialize_graph_module)r`   ra   fn_srcrI   s       r$   r   r   z   s9     XXg.$v,Fv 5r:G$Wd33r&   generated_module_namec                 P    | j                  |      j                  }t        ||      S r;   )import_modulerI   rf   )rQ   r`   rh   rI   s       r$   r   r      s(     $$%:;CCG$Wd33r&   c                     i }| j                   |d<   |j                  d      }|J t        ||z   |      }t        ||      S )N__builtins__rc   )patched_builtinsre   rL   rf   )rQ   r`   ra   nsrg   rI   s         r$   r   r      sQ     
B!22B~XXgFv 5r:G$Wd33r&   c                        e Zd Z fdZ xZS )_CodeOnlyModulec                 0    t         |           || _        y r;   )superr%   __dict__)r#   r`   	__class__s     r$   r%   z_CodeOnlyModule.__init__   s    r&   )r/   r?   r@   r%   __classcell__rt   s   @r$   rp   rp      s     r&   rp   c                    | t         _        |j                  d      }|ddlm} |}|j                  dd      }|} G d d|      }t        |      }|j                  di       }	  |       j
                  |fi |	}
||
_        dd	lm}  |||
||
      }|j                         D ]  \  }}t        ||      rt        |||       ! |S )a  
    Deserialize a GraphModule given the dictionary of the original module,
    using the code to reconstruct the graph. We delete the actual graph before
    saving the dictionary so that changes to the in-memory graph format do not
    get serialized.
    _tracer_clsr   )Tracer_graphmodule_cls_namer   c                   H    e Zd Zdej                  j
                  dedefdZy)._deserialize_graph_module.<locals>.KeepModules___r9   c                      yNTrB   )r#   r}   r~   s      r$   is_leaf_modulez=_deserialize_graph_module.<locals>.KeepModules.is_leaf_module   s    r&   N)	r/   r?   r@   torchnnModulerA   boolr   rB   r&   r$   KeepModulesr|      s$    	EHHOO 	 	 	r&   r   _tracer_extras_make_graph_module)
class_namegraph_module_cls)rp   rI   re   _symbolic_tracery   tracerx   _lazy_graph_moduler   rY   hasattrsetattr)rI   r`   r   
tracer_clsry   graphmodule_cls_name
cls_tracerr   comtracer_extrasgraphr   gmkvs                  r$   rf   rf      s     &O-(J+
88$;]K !Jj  $
CHH-r2MKM5}5E #E6	C3GZj	kB 

 1r1~B1 Ir&   from_module	to_moduletargetc                    |j                  d      ^ }}|D ]R  }t        | |      }t        ||d       }||u r y |+t        j                  j	                         }t        |||       ||}} T t        | |      }t        |t        j                        r7t        |t        j                  j                        s|j                  ||       y t        |||       y N.
splitgetattrr   r   r   r   
isinstanceTensor	Parameterregister_buffer)	r   r   r   prefixfielditemftorigs	            r$   
_copy_attrr      s    \\#&NVU &K&ItT*6
 9!AItQ'!"AY& ;&D $%juxx?Q?Q.R!!%.	5$'r&   from_objc                 n   |j                  d      ^ }}|D ]>  }t        ||d       }|+t        j                  j	                         }t        |||       |}@ t        | t        j                        r7t        | t        j                  j                        s|j                  ||        y t        |||        y r   r   )r   r   r   r   r   r   r   s          r$   _assign_attrr      s    \\#&NVU ItT*9!AItQ'	 (ELL)*%(($$3 	!!%2	5(+r&   c                    | j                   }|$t        |t        j                  j                        sJ d       |j                  dd|||      }|j                  }|j                  d      }d| d|z   }t        |d      }d	g}	| j                         D ]2  \  }
}t        |d
      s|	j                  t        ||
d|||             4 dj                  |	      }t        |d      }||z   }|rt        ||z          |S )Nz4print_readable must be used on a module with a graphr#   T)root_moduleverboseinclude_strideinclude_devicecoloredrX   zclass z(torch.nn.Module):
    r   F)print_outputr   r   r   )r   r   r   fxr   python_coder'   lstripr   named_childrenr   append_print_readablerZ   print)moduler<   r   r   r   r   r   verbose_python_codemodule_codesubmodule_code_listsubmodule_name	submodulesubmodule_codeoutputs                 r$   r   r     s%    LLEE588>>!BzDzzB++%% ,  &))K$$T*K;-';<{JK[!,K$%+%:%:%< !	9g&&&"!&#1#1#	 YY23N2N>)FkN*+Mr&   c                   R     e Zd Zd Zedej                  defd       Z fdZ	 xZ
S )_WrappedCallc                      || _         || _        y r;   )clscls_call)r#   r   r   s      r$   r%   z_WrappedCall.__init__2  s     r&   frame_summaryr9   c                    | j                   }|J | j                  }|J t        |      }t        j                  | j
                        }t        j                  j                  t        j                               }d| d}dj                  ||dz
  |       }d|z  dz   }dj                  |||dz          }	dj                  |||||	g      S )Nz%Call using an FX-traced Module, line z3 of the traced Module's generated forward function:r      ~z~~~ <--- HERErX   )linenolinelenr3   getlinesfilenamer   _dynamodisable	traceback
format_excrZ   )
r   
err_linenor   err_line_lenall_src_linestb_repr
custom_msg
before_errmarkererr_and_after_errs
             r$   _generate_error_messagez$_WrappedCall._generate_error_message?  s     #))
%%%!!4y!**=+A+AB --''	(<(<=?<  ** 	
 WW]:>JGH
|#o5 IImJa&PQ yy':z6CTUVVr&   c                    	 | j                    | j                   |g|i |S t        | j                  |  |i |S # t        $ r}|j
                  sJ t        j                  j                  t        j                  |j
                              d   }d|j                  v r?t        t        j                  |      t        j                         |j!                  d       |d }~ww xY w)Neval_with_key)file)r   rr   r   __call__	Exception__traceback__r   StackSummaryextractwalk_tbr   r   r   r   sysstderrwith_traceback)r#   rP   argskwargsetopmost_framesummaryrt   s         r$   r   z_WrappedCall.__call__W  s    	}}($t}}S:4:6::TXXs4dEfEE 	??"?&&..y/@/@/QRSUV ! "6"?"?? 889MN &&t,,	s   !> > 	C(BC##C()r/   r?   r@   r%   staticmethodr   FrameSummaryrA   r   r   ru   rv   s   @r$   r   r   1  s>    ! Wy/E/E W# W W. r&   r   c                       e Zd ZdZd) fdZ ed      	 d*deej                  j                  e
eef   f   dedef fd       ZdgZed	efd
       Zej$                  ded	dfd       Z ed      d+deeej(                  f   defd       Z ed      dedej                  j                  d	efd       Z ed      ded	efd       Z ed      d,d       Zed	efd       Z ed      d	efd       ZdefdZdefdZ d Z!d Z"d Z#d  Z$ ed      d-d!       Z%d	ef fd"Z&d# Z'e(jR                  d$        Z*d% Z+d& Z,d' Z-d( Z. xZ/S ).r   a  
    GraphModule is an nn.Module generated from an fx.Graph. Graphmodule has a
    ``graph`` attribute, as well as ``code`` and ``forward`` attributes generated
    from that ``graph``.

    .. warning::

        When ``graph`` is reassigned, ``code`` and ``forward`` will be automatically
        regenerated. However, if you edit the contents of the ``graph`` without reassigning
        the ``graph`` attribute itself, you must call ``recompile()`` to update the generated
        code.
    c                     | j                   D ])  }|j                  j                  d      d   }|dk7  s'|}  n  G d d|       }t        |   |      S )Nr   r   GraphModuleImplc                       e Zd Zy),GraphModule.__new__.<locals>.GraphModuleImplN)r/   r?   r@   rB   r&   r$   r   r     s    r&   )__mro__r@   r   rr   __new__)r   r   r   r   cr   rt   s         r$   r   zGraphModule.__new__z  s_      	A$$S)"-A%%			c 	 w//r&   Tr^   rootr   r   c                    t         	|           || j                  _        t	        |t
        j                  j                        rt        |d      r|j                  | _	        t	        |t              ro|j                         D ]  \  }}t        || |        |j                         D ]  \  }}t        || |        |j                         D ]  \  }}t        || |        |j                  D ]D  }|j                   dv st	        |j"                  t$              sJ t        || |j"                         F nt	        |t&              rg }|j                  D ]  }|j                   dv st	        |j"                  t$              sJ |j"                  |vr*t)        dt%        |      z   dz   |j"                  z   dz         |j+                  |j"                          |j-                  d        |D ]  }t/        ||   | |        nt)        dt%        |      z   d	z         || _        d
| _        | j0                  j2                  r=d| j0                  j2                  j4                  vr| j0                  j2                  | _        i | _        | j0                  j6                  r| j0                  j6                  | _        i | _        d
| _        g | _        g | _        y
)a  
        Construct a GraphModule.

        Args:

            root (Union[torch.nn.Module, Dict[str, Any]):
                ``root`` can either be an nn.Module instance or a Dict mapping strings to any attribute type.
                In the case that ``root`` is a Module, any references to Module-based objects (via qualified
                name) in the Graph's Nodes' ``target`` field will be copied over from the respective place
                within ``root``'s Module hierarchy into the GraphModule's module hierarchy.
                In the case that ``root`` is a dict, the qualified name found in a Node's ``target`` will be
                looked up directly in the dict's keys. The object mapped to by the Dict will be copied
                over into the appropriate place within the GraphModule's module hierarchy.

            graph (Graph): ``graph`` contains the nodes this GraphModule should use for code generation

            class_name (str): ``name`` denotes the name of this GraphModule for debugging purposes. If it's unset, all
                error messages will report as originating from ``GraphModule``. It may be helpful to set this
                to ``root``'s original name or a name that makes sense within the context of your transform.
        training)get_attrcall_modulezNode z referenced target z. but that target was not provided in ``root``!c                 $    | j                  d      S r   )count)r   s    r$   <lambda>z&GraphModule.__init__.<locals>.<lambda>  s    qwws| r&   )r6   zUnsupported type z passed for root!Nz<locals>) rr   r%   rt   r/   r   r   r   r   r   r  rp   r   r   named_buffersnamed_parametersnodesopr   rA   dictRuntimeErrorr   sortr   r   rx   r@   r   meta_replace_hook_create_node_hooks_erase_node_hooks)
r#   r   r   r   r   r}   nodetargets_to_copytarget_to_copyrt   s
            r$   r%   zGraphModule.__init__  s   6 	",dEHHOO,tZ( $ $0 //1 .DAqtT1-. !..0 .DAqtT1-. !113 .DAqtT1-.  87799%dkk3777tT4;;78 d# O 87799%dkk3777{{$.*#!$i(34 #kk* O	O  $**4;;78$   %; <"1 IT.14HI 2SY>ATTUU
  JJ""$**"8"8"E"EE#zz55D ::$$"&**";";D %'	!2413r&   r9   c                     | j                   S )zF
        Return the ``Graph`` underlying this ``GraphModule``
        )_graphr"   s    r$   r   zGraphModule.graph  s    
 {{r&   gNc                     t        |t              sJ dt        |              || _        | |_        | j                          y)z
        Set the underlying ``Graph`` for this ``GraphModule``. This will internally
        recompile the ``GraphModule`` so that the generated ``forward()`` function
        corresponds to ``g``
        z#Expected a Graph instance, but got N)r   r   typer  owning_module	recompile)r#   r  s     r$   r   zGraphModule.graph  s>     !U#T'J4PQ7)%TT#r&   Ffolderr<   c           
      "   t        |      }t        |      j                  d       t        j                  | j	                         |dz         d}dj                  t        j                         D cg c]  }|j                   c}      }d| d| d}d	t        d
t        j                  j                  dt        t           fd}g }| j                         D ]  \  }}	 |||	      }
|
g|| dz  }t        j                  |	|       |j                  |       |	j                         j!                  dd      j!                  dd      }d| d| }
||dz   d| d|
 dz  } | j"                  j%                         D ]9  \  }}|	||dz   d| dt'        |j(                         d|j*                   dz  }; | j,                  j%                         D ]9  \  }}|	||dz   d| dt'        |j(                         d|j*                   dz  }; ||dz   d| dz  }|t/        | j0                  d       dz  }|dz  }|j3                  |       |dz  }|j3                  d        t5        |      d!kD  rt7        j8                  d"|        yyc c}w )#aa  Dumps out module to ``folder`` with ``module_name`` so that it can be
        imported with ``from <folder> import <module_name>``

        Args:

            folder (Union[str, os.PathLike]): The folder to write the code out to

            module_name (str): Top-level name to use for the ``Module`` while
                writing out the code
        T)exist_okzstate_dict.ptz    rX   z
import torch
z

from torch.nn import *
class zF(torch.nn.Module):
    def __init__(self):
        super().__init__()
r<   r   r9   c                    t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  g}t        |      |v r|j                          S y r;   )
r   LinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3dr  __repr__)r<   r   
safe_reprss      r$   _gen_model_reprz.GraphModule.to_folder.<locals>._gen_model_repr#  s[    								J F|z) //+,-r&   Nz.pt ztorch.load(r'z', weights_only=False) # r   zself.z = zself.register_buffer('z', torch.empty(z, dtype=z))
z" = torch.nn.Parameter(torch.empty(z"self.load_state_dict(torch.load(r'z/state_dict.pt'))
r   z	module.pyz__init__.pyzfrom .module import *r   z^Was not able to save the following children modules as reprs -saved as pickled files instead: )r   mkdirr   save
state_dictrZ   r   valuesrS   rA   r   r   r   r   r   r'  replace_buffersrY   listshapedtype_parametersr   rd   
write_textr   warningswarn)r#   r  r<   tabr   custom_builtins	model_strr)  blobified_modulesr   
module_strmodule_filemodule_reprbuffer_namebuffer
param_nameparam	init_files                     r$   	to_folderzGraphModule.to_folder  s    fVD)

4??$f&>?));K;R;R;T$UaQ\\$UV   m 		 	ehhoo 	(SV- 	 #'#6#6#8 		GK(f=J!$+c'::

6;/!((5$oo/77cBJJ4QTU,[M9RS^R_`
CE7%}C
|2FFI		G $(==#6#6#8 	DK~CE7"8_UYZ`ZfZfUgThhpqwq}q}p~  C  D  DI	D
 "&!1!1!7!7!9 	CJ}CE7%
|3UVZ[`[f[fVgUhhpqvq|q|p}  ~B  C  CI	C
 	1ug7x?RS	
	 	
499a0144	{*y)]*	45 !A%MM33D2EG &w %Vs   1Jr   mc                 &   |j                  d      ^ }}| }|D ]d  }t        ||d      }|+t        j                  j	                         }t        |||       t        |t        j                  j                        s y|}f |j                  ||       y)a%  
        Adds the given submodule to ``self``.

        This installs empty Modules where none exist yet if they are
        subpaths of ``target``.

        Args:
            target: The fully-qualified string name of the new submodule
                (See example in ``nn.Module.get_submodule`` for how to
                specify a fully-qualified string.)
            m: The submodule itself; the actual object we want to
                install in the current Module

        Return:
            bool: Whether or not the submodule could be inserted. For
                this method to return True, each object in the chain
                denoted by ``target`` must either a) not exist yet,
                or b) reference an ``nn.Module`` (not a parameter or
                other attribute)
        r   NFT)r   r   r   r   r   r   r   
add_module)r#   r   rF  r   r   modr   submods           r$   add_submodulezGraphModule.add_submoduleY  s    ,  c*# 	DS$-F~*T6*fehhoo6C	 	ua r&   c                 \   |j                  d      }|dd |d   }}| }|D ]B  }t        ||      s yt        ||      }t        |t        j
                  j                        rB y t        ||      syt        t        ||      t        j
                  j                        syt        ||       y)ab  
        Deletes the given submodule from ``self``.

        The module will not be deleted if ``target`` is not a valid
        target.

        Args:
            target: The fully-qualified string name of the new submodule
                (See example in ``nn.Module.get_submodule`` for how to
                specify a fully-qualified string.)

        Returns:
            bool: Whether or not the target string referenced a
                submodule we want to delete. A return value of ``False``
                means that the ``target`` was not a valid reference to
                a submodule.
        r   Nr   FT)r   r   r   r   r   r   r   delattr)r#   r   atomspathtarget_submodrI  r   s          r$   delete_submodulezGraphModule.delete_submodule  s    & S!#CRj%)m#  	D3%#t$Cc588??3	 sM*'#}5uxxG]#r&   c                    g }| j                   j                  D ]  }|j                  dk(  s|j                  dk(  s"|j                  j	                  d      }dt
        dt
        dt
        fd}|j                  t        j                  ||             |j                  dk(  s	 | j                  |j                        }|j                         D ]7  \  }}|dk7  s|j                  dj                  |j                  |g             9  | j                         D cg c]  \  }}||vs| }	}}|	D ]  }| j                  |        y	# t        $ r Y <w xY wc c}}w )
a  
        Deletes all unused submodules from ``self``.

        A Module is considered "used" if any one of the following is
        true:
        1. It has children that are used
        2. Its forward is called directly via a ``call_module`` node
        3. It has a non-Module attribute that is used from a
        ``get_attr`` node

        This method can be called to clean up an ``nn.Module`` without
        manually calling ``delete_submodule`` on each unused submodule.
        r  r  r   xyr9   c                 :    dj                  |r| |g      S | g      S r   )rZ   )rS  rT  s     r$   join_fnz9GraphModule.delete_all_unused_submodules.<locals>.join_fn  s!    88aQF99aS99r&   r   N)r   r	  r
  r   r   rA   extend	itertools
accumulateget_submodulenamed_modulesr   rZ   AttributeErrorrQ  )
r#   usedr  fullpathrV  rJ  submod_namer}   rO   	to_deletes
             r$   delete_all_unused_submodulesz(GraphModule.delete_all_unused_submodules  sQ    JJ$$ !	Dww-'477j+@
  ;;,,S1
:s :s :s : I007CD 77m+	!%!3!3DKK!@.4.B.B.D RNK*b0 $CHHdkk;5O,P QR7!	F *.););)=RgdATAQTR	R 	(D!!$'	( *  
 Ss$   "6E	.E	E)E		EEc                 H    t        | d      st        d      | j                  S )zn
        Return the Python code generated from the ``Graph`` underlying this
        ``GraphModule``.
        rc   z;Code has not been generated! Please report a bug to PyTorch)r   r  rc   r"   s    r$   rd   zGraphModule.code  s*     tW%M  zzr&   c                    t        | j                  j                  t              r^| j                  j                  j                  j
                  | _        | j                  j                  j                  j                  | _        | j                  j                  d      }|j                  | _        |j                  | _        t        |       }t        | j                  d      r| j                  j                  ni }t!        | j                  |j"                  |      |_        dt'        |      v r|j(                  nd}dt'        |      vrt+        ||      |_        d }||_        |S )z
        Recompile this GraphModule from its ``graph`` attribute. This should be
        called after editing the contained ``graph``, otherwise the generated
        code of this ``GraphModule`` will be out of date.
        r#   )r   
_co_fieldsr   N_wrapped_callc                 .     | j                   | g|i |S r;   )re  )r#   r   r   s      r$   call_wrappedz+GraphModule.recompile.<locals>.call_wrapped  s    %4%%d<T<V<<r&   )r   r  _codegenr   pytree_infoin_spec_in_specout_spec	_out_specr   r'   rc   _lineno_mapr  r   rd  rL   r(   rI   varsr   r   re  )r#   r   r   r5   r   rg  s         r$   r  zGraphModule.recompile  s    dkk**N; KK00<<DDDM![[11==FFDNkk--&-A __
&224j.5dkk<.PDKK**VX	'

K4G4GS $.c#:3<<$s)+ ,S( ;C	= $r&   rQ   c                     | j                   j                         }| j                  j                  |d<   |d= | j	                         }t        |j                  |      }t        ||ffS )Nrz   r  )rs   r2   rt   r/   r  r]   r(   r   )r#   rQ   dict_without_graphr   ra   s        r$   __reduce_deploy__zGraphModule.__reduce_deploy__  sb    !]]//16:nn6M6M23x(nn&+K,?,?J*-?,NOOr&   exporterc                 J   | j                   j                         }| j                  j                  |d<   |d= d|j	                          }| j                         }t        |j                  |j                        }|| j                  z   }|j                  ||       t        ||ffS )Nrz   r  zfx-generated._)rs   r2   rt   r/   get_unique_idr  r]   r(   rQ   rd   save_source_stringr   )r#   rs  rq  rh   r   ra   r   s          r$   __reduce_package__zGraphModule.__reduce_package__!  s    !]]//16:nn6M6M23x("01G1G1I0J Knn&+K,?,?ARARS"TYY.##$9;G'!67
 	
r&   c                     | j                   j                         }| j                         }t        |j                  t
              }|d= t        ||ffS )a|  
        Serialization of GraphModule. We serialize only the generated code, not
        the underlying ``Graph``. This is because ``Graph`` does not have on-disk
        backward-compatibility guarantees, whereas Python source code does.
        On the deserialization side, we symbolically trace through the generated
        code to regenerate the underlying ``Graph``
        r  )rs   r2   r  r]   r(   r   r   )r#   rq  r   ra   s       r$   
__reduce__zGraphModule.__reduce__0  sN     "]]//1nn&+K,?,?Nx(#&8,%GHHr&   c                 "    t         j                  S r;   )r   r%   r"   s    r$   _deepcopy_initzGraphModule._deepcopy_init?  s    ###r&   c           	      p   t        |       j                  t        |             }||t        |       <   t        t	        j
                  | j                  |            } | j                         |||j                  d          g d}|D ]?  }|| j                  v st        ||t	        j
                  | j                  |   |             A t	        j
                  t        | di       |      |_
        t        |j                  v r6|j                  t           j                         D ]  \  }}t        |||        |S )Nr  )_state_dict_hooks_load_state_dict_pre_hooks_load_state_dict_post_hooksr  r  r  r  )r  r   idrp   r2   deepcopyrs   r{  r   r   r  _USER_PRESERVED_ATTRIBUTES_KEYrY   )r#   memoresfake_modextra_preserved_attrsattrrU   s          r$   __deepcopy__zGraphModule.__deepcopy__E  s	   4j  d,RX"4==#EFc8X->->x-HI
!
 * 	MDt}}$T4==t1Dd#KL	M ==vr!:DA)SXX5#&88,J#K#Q#Q#S .	4Y-.
r&   c                 \    ddl m}  || | j                        }t        | di       |_        |S )Nr   r   r  )r   r   r   r   r  )r#   r   r  s      r$   __copy__zGraphModule.__copy___  s*    : tzz24,
r&   c                 >    t        | | j                         ||||      S )zh
        Return the Python code generated for current GraphModule and its children GraphModules
        )r   	_get_name)r#   r   r   r   r   s        r$   print_readablezGraphModule.print_readablee  s*    
 NN
 	
r&   c                 `    t         |          }d}dj                  || j                  |g      S )NzD# To see more debug info, please use `graph_module.print_readable()`rX   )rr   __str__rZ   rc   )r#   orig_strprint_readable_reminderrt   s      r$   r  zGraphModule.__str__s  s4    7?$R 	  yy(DJJ0GHIIr&   c                 4    | j                         }d|_        |S r   )r  _is_replica)r#   new_gms     r$   _replicate_for_data_parallelz(GraphModule._replicate_for_data_parallelz  s    !r&   c              #      K   t        |      sJ d       | j                  |c}| _        	 d || _        y# || _        w xY ww)a7  
        Takes a callable which will be called everytime when we replace a node
        to a new node, or change the node's name. Callable takes three arguments:
        the old node we're changing, and NAME of the new node, followed by the
        user node which consumes the old node to be replaced.
        z Replace hook must be a callable.N)callabler  )r#   r   prevs      r$   _set_replace_hookzGraphModule._set_replace_hook  sG      {>>>{#'#5#5q d 	&!%DDs   'A6 A	?Ac                 ^    t        |      sJ d       | j                  j                  |       y)z
        Takes a callable which will be called after we create a new node. The
        callable takes the newly created node as input and returns None.
        $create_node hook must be a callable.N)r  r  r   r#   r   s     r$   _register_create_node_hookz&GraphModule._register_create_node_hook  *    
 {BBB{&&q)r&   c                 ^    t        |      sJ d       | j                  j                  |       y)z
        Takes a callable which was previously registered to be called after we create a node.
        This function will unregister that callable so it is no longer invoked on node creation.
        r  N)r  r  remover  s     r$   _unregister_create_node_hookz(GraphModule._unregister_create_node_hook  r  r&   c                 ^    t        |      sJ d       | j                  j                  |       y)z
        Takes a callable which will be called after we erase a node. The
        callable takes the node that is being erased as input and returns None.
        #erase_node hook must be a callable.N)r  r  r   r  s     r$   _register_erase_node_hookz%GraphModule._register_erase_node_hook  *    
 {AAA{%%a(r&   c                 ^    t        |      sJ d       | j                  j                  |       y)z
        Takes a callable which was previously registered to be called after we erase a node.
        This function will unregister that callable so it is no longer invoked on node erasure.
        r  N)r  r  r  r  s     r$   _unregister_erase_node_hookz'GraphModule._unregister_erase_node_hook  r  r&   )r   zType[GraphModule])r   )FxModule)r9   NTFFF)0r/   r?   r@   __doc__r   r   r   r   r   r   r   rA   r   r   r%   __jit_unused_properties__propertyr   setterosPathLikerE  r   rK  rQ  ra  rd   r   r  r   rr  r   rw  ry  r{  r  r  r  r  r  
contextlibcontextmanagerr  r  r  r  r  ru   rv   s   @r$   r   r   k  s(   0& $/
 (	_4EHHOOT#s(^34_4 _4 	_4 0_4J ")	u   \\	u 	 	 	 %0Nc2;;&6 7 Nc N 1N` $/&C &EHHOO & & 0&P $/(s (t ( 0(T $/6( 06(p 	c 	 	 $/!: ! 0!JP( P
? 
I$4 %0
 1
J J
 & &**))r&   r   r;   r  );r  r2   rX  r3   r  r   r   r7  pathlibr   typingr   r   r   r   r   r	   r
   r   r   torch.nnr   torch.overridestorch.nn.modules.moduler   torch.packager   r   r   r   _compatibilityr   r   r   r   r   r   r   __all__r  r   rE   rA   rG   rL   rK   rV   r]   r   r   r   r   rp   rf   r   r   r   r   r   rB   r&   r$   <module>r     s       	 
    H H H    . R R ) V V "> + +\ 
-3 -c3h -
3 c3h  DH)-c3h>3 >S >H > >*$sCx. *H * d+4d38n 4C 4EHHOO 4 ,4 d+44%)#s(^4LO4
XX__4 ,4 d+44%)#s(^4CF4
XX__4 ,4ehhoo /T#s(^ /W\W_W_WfWf /h(EHHOO ( (QT (8,3 ,588?? ,C ,. *Z8 8t d+@	)%((// @	) ,@	)r&   