
    }Yh                       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 d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ d dlZd dlmc mZ d dlmc mZ d dlm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(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/ d dlm0Z0m1Z1 ddl2m3Z3  ej4        e5          Z6g dZ7 G d de          Z8da9ed             Z:	 dWdeej;        ej<        ej=        j>        f         dej=        j>        de?de8de@f
dZA G d d          ZB G d deBej=        j>                  ZC G d d eBej=        j>                  ZD G d! d"e jE                  ZF G d# d$ej=        j>                  ZG	 dXd%e"d&eeF         d'eGfd(ZHd)ej        jI        d*e#d'dfd+ZJd, ZKd-e?d.e?d'e?fd/ZLd0ej=        j>        d1ej=        j>        fd2ZMd3ej=        j>        d'e?fd4ZNd3ej        jO        d'ej        jP        fd5ZQd3eej        jO        eCeGf         d'ej        jP        fd6ZRd3eej        jO        eCeGf         d'ej        jP        fd7ZSd8ej=        j>        de?fd9ZT	 dXd8ej=        j>        de?d:ej=        j>        d;eee?gej=        j>        f                  fd<ZUd=e?d>eVd'e?fd?ZWd@e?d=e?d'e@fdAZX G dB dC          ZYe G dD dE                      ZZdFej        jI        dGeGfdHZ[	 dYdJej=        j>        dKe\e?eVf         dLe?fdMZ] G dN dO          Z^d3ej        jO        dGeGdPe\e?e_e?         f         fdQZ`dR Za	 dXd%ej=        j>        dSe\e?ebe?         f         dTebe?         dUee\eVe_e?         f                  fdVZcdS )Z    N)defaultdict)contextmanager)deepcopy)	dataclass)Enum)AnyCallablecastOptionalUnion)FakeScriptObject)reorder_kwargs)	ConstantArgumentExportedProgramExportGraphSignature	InputKindModuleCallSignatureSymBoolArgumentSymFloatArgumentSymIntArgumentTensorArgument)is_fx_tracing)	_get_attr_get_attr_via_attr_list_print_readable)
GetAttrKeySequenceKey   )_remove_effect_tokens)FlatArgsAdapterInterpreterModuleInterpreterModuleDispatcherUnflattenedModule	unflattenc                       e Zd ZdZdZdZdZdS )	_AttrKind	parameterbufferconstantmoduleN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANTMODULE     h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/export/unflatten.pyr&   r&   1   s"        IFHFFFr3   r&   Tc               #   8   K   t           } da 	 d V  | a d S # | a w xY wNF)RUN_WITH_INTERPRETER)old_flags    r4   _disable_interpreterr9   ;   s?       $H ('x''''s    from_obj	to_moduletarget	attr_kind
persistentc                   	 |                     d          ^ }}|h}|D ]	t                      }|D ]w}t          |	          s-t          |	t          j                                                   |                    	fd|j        	                                D                        x|}|D ];}|t          j        k    r8t          | t          j        j                  sJ |                    ||            K|t          j        k    r5t          | t          j                  sJ |                    || |           |t          j        k    rYt          | t&                    r
J d            t          | t          j        t          j        f          sJ t          |||            |t          j        k    r2t          | t          j        j                  sJ t          |||            =d S )N.c              3   B   K   | ]\  }}t          |          |V  d S N)_is_call_name).0kt_callitems      r4   	<genexpr>z_assign_attr.<locals>.<genexpr>\   sK        Av D))     r3   )r>   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrtorchnnModuleupdate_modulesitemsr&   r.   
isinstance	Parameterregister_parameterr/   Tensorregister_bufferr0   r   ScriptObjectr1   )
r:   r;   r<   r=   r>   prefixfield
to_modulestsrG   s
            @r4   _assign_attrr]   H   s-    \\#&&NVU J 
 
#&55# 	 	I9d++ <	4):):;;;II    !*!3!9!9!;!;      
 

 0 0		+++h(:;;;;;((9999)***h55555%%eX*%MMMM),,,!(,<==  D   L&     Iuh////)***h88888Iuh///+0 0r3   c                   >    e Zd ZU ee         ed<   dee         fdZdS )_SubmoduleBase_tyreturnc                     | j         S rB   )r`   selfs    r4   	type_namez_SubmoduleBase.type_name~   s	    xr3   N)r+   r,   r-   r   str__annotations__re   r2   r3   r4   r_   r_   {   sE         	#8C=      r3   r_   c                        e Zd ZU dZeej        j                 ed<   	 ddej        j	        dee
         f fdZd Zd Z	 	 	 	 ddZ xZS )r!   zA module that uses torch.fx.Interpreter to execute instead of the usual
    codegen that GraphModule uses. This provides better stack trace information
    and makes it easier to debug execution.
    graph_moduleNgraphtyc                     t                                                       || _        || _        | | j        _        t
          | _        d S rB   )super__init__rj   r`   owning_moduler7   _run_with_interpreter)rd   rj   rk   	__class__s      r4   rn   zInterpreterModule.__init__   sA    
 	
#'
 %9"""r3   c                 ~   | j         
J d            t                      sHt          j                                        s| j        s# t          | j                   j        | g|R i S rt          |          }| j	        t          |          d          }|                    fd|D                        t          |          t                    k    sJ t          |          t          | j	                  k    sJ t          |          }t          j                            | | j                  j        |ddiS )Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]}|v |         V  d S rB   r2   )rD   
kwarg_namekwargss     r4   rH   z,InterpreterModule.forward.<locals>.<genexpr>   s?          "!V++ :&++++   r3   rj   enable_io_processingF)ri   r   rM   compileris_dynamo_compilingrp   typeforwardlist	arg_nameslenextendtuplefxInterpreterrj   run)rd   argsru   arg_listkwarg_namess     `  r4   r{   zInterpreterModule.forward   sc    ,,.V,,, 	N..00	8<8R	 34)**24I$III&III '
  ::"nS]]__=        &1         ;''3v;;66668}}DN(;(;;;;;X8''DJ'??C,1  r3   c                    t           j                            | | j                  | j        d<   | j                                         g | _        | j        j        D ],}|j        dk    r| j        	                    |j
                   -d S )Nri   placeholder)rM   r   GraphModulerj   __dict__lintr}   nodesopappendr<   rd   nodes     r4   finalizezInterpreterModule.finalize   s     ).(<(<T4:(N(Nn%
 J$ 	3 	3Dw-''%%dk222	3 	3r3   TFc                 *    t          | d||||          S )Nr!   r   rd   print_outputinclude_strideinclude_devicecoloreds        r4   print_readablez InterpreterModule.print_readable   (     
 
 	
r3   rB   TFFF)r+   r,   r-   __doc__r   rM   r   r   rg   Graphrf   rn   r{   r   r   __classcell__rq   s   @r4   r!   r!      s          
 58/0000
 !	: 	:x~	: SM	: 	: 	: 	: 	: 	:! ! !F3 3 3& 
 
 
 
 
 
 
 
r3   r!   c                   `     e Zd ZdZdee         dee         f fdZd Z	d Z
	 	 	 	 d
d	Z xZS )r"   z
    A module that carries a sequence of InterpreterModules corresponding to
    a sequence of calls of that module. Each call to the module dispatches
    to the next InterpreterModule, and wraps back around after the last.
    attrscall_modulesc           	         t                                                       |sJ |d         j        | _        |D ]'}t          | |t	          |d         |                     (|d         j        | _        || _        d| _        d S Nr   )rm   rn   rQ   rL   getattrr`   _call_modules
_num_calls)rd   r   r   accessorrq   s       r4   rn   z$InterpreterModuleDispatcher.__init__   s    $Q0 	H 	HHD(GLOX$F$FGGGG?&)r3   c                     | j         | j                 }| j        dz   t          | j                   z  | _        	  ||i |S # t          $ r	 d| _         w xY w)Nr   r   )r   r   r~   	Exception)rd   r   ru   call_modules       r4   r{   z#InterpreterModuleDispatcher.forward   sn    (9?Q.#d6H2I2II	;//// 	 	 	DO	s   A   Ac                     | j         S rB   )r   rc   s    r4   r   z(InterpreterModuleDispatcher.call_modules   s    !!r3   TFc                 `    fd| j         D             }d                    |          S )Nc                 @    g | ]}|                               S r2   )r   )rD   modr   r   r   r   s     r4   
<listcomp>z>InterpreterModuleDispatcher.print_readable.<locals>.<listcomp>   sG     
 
 
  	 
 
 
r3   
)r   join)rd   r   r   r   r   outputss    ```` r4   r   z*InterpreterModuleDispatcher.print_readable   sV    
 
 
 
 
 
 
 )
 
 
 yy!!!r3   r   )r+   r,   r-   r   rJ   rf   r|   r!   rn   r{   r   r   r   r   s   @r4   r"   r"      s         c#h d;L6M        " " "
 " " " " " " " "r3   r"   c                       e Zd ZdZej        	 	 d
dej        dej        dee	         de
eee	f                  de
e	         dee	         fd	            ZdS )r    zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    Ntarget_spec
input_spec
input_argsmetadataobjra   c                     dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr2   )rd   r   r   r   r   r   s         r4   adaptzFlatArgsAdapter.adapt  s	     	r3   )NN)r+   r,   r-   r   abcabstractmethodpytreeTreeSpecr|   r   r   dictrf   r   r2   r3   r4   r    r    
  s          	 .2!	 	_	 O	 I		
 4S>*	 c]	 
c	 	 	 	 	 	r3   r    c                   l     e Zd Z	 ddedee         f fdZd Zd Zd Z	d Z
d	 Zd
 Z	 	 	 	 ddZ xZS )r#   Nexport_moduleflat_args_adapterc                   $% t                                                       |j        j        t	          d          d |j        D             }|d         dk    sJ t          |j                  }t          |j                  | _        t          j	        
                                | _        | | j        _        t          |j                  | _        || _        |j        j        | _        | | j        d<   d| _        t           | _        t%          || j                   t'                      | _        t+          ||           \  }}| j                            |                                           t1          |j        | |           |j        | _        g | _        |j        }t;                      }i }	| j        j        D ]}
||
         }t?          |          |	vrHt          j         !                    |"                                |j#                  |	t?          |          <   tI          |	t?          |                   | |
tJ          j&                   |'                    |
           t;          | j        j(                  }t;                      }i }| j        j)        D ]}
|
|v rd}|j*        |
         }n
d	}||
         }t?          |          |vr&|"                                |f|t?          |          <   tI          |t?          |                   d         | |
tJ          j+        |
           |'                    |
           |,                                D ]\  }
}|
|v s|
|v rd}t?          |          |v st[          |t          j         j!                  sd	}|r]t?          |          |vr|d	f|t?          |          <   tI          |t?          |                   d         | |
tJ          j+        d	
           t?          |          |	vr||	t?          |          <   tI          |	t?          |                   | |
tJ          j&                   i }|j*        ,                                D ]\  }}t?          |          |vr@t[          |t          j.                  r|"                                }||t?          |          <   |t?          |                   }tI          || |tJ          j/                   ta          tb                    $t;                      }$fd}t;                      }| j        j2        D ]}|j3        th          j&        k    s|j3        th          j+        k    r|j5        rtm          |j7        d          sJ t[          |j8        tr                    sJ  |t?          |j        |j8                           |j7        j:        |j8                   |'                    |j8                   |'                    |j8                   |j3        th          j+        k    r|j5        r*|j3        th          j;        k    s|j3        th          j<        k    rtm          |j7        d          sJ t[          |j8        tr                    sJ  |t?          |j*        |j8                           |j7        j:        |j8                   |'                    |j8                   |j*        ,                                D ]v\  }}||vrmt?          |          $v s
J d            $t?          |                   d         \  }} |t?          |          ||           |'                    |j8                   w|j        ,                                D ]S\  }}||vrJt?          |          $vr$t?          |                   d         \  }} |t?          |          ||           Ti }$                                D ]} d | D             }!| D ]
\  }"}|!||"<   t{          | |g            t}          |                                          %%fd|D             }| ?                    %|           d |D             }d | j        j@        D             | _A        d	| _B        d t          |          D             }#| D                    d          D ]\  }
}|
|#vrt          |#          |#|
<   t          | |#           | j        G                                 | H                                 d S )Nz%Unflattening on JointExportModule NYIc                     g | ]	}|j         
S r2   )fqnrD   entrys     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>&  s    KKK%EIKKKr3   r    unflattened_moduleF)requires_grad)r=   T)r=   r>   c                 F    |          }|                     ||f           d S rB   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r4   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s,    "6*Ii566666r3   namez?Constants should be either aliased or appear in graph signaturec                     g | ]
}|d          S r   r2   )rD   ts     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    111qt111r3   c                     g | ]}|v|	S r2   r2   )rD   r   redirected_call_indicess     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s$    RRRCs:Q/Q/QC/Q/Q/Qr3   c                     g | ]}d |v|	S )@r2   )rD   r   s     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    >>>Cs#~~C~~~r3   c                 (    g | ]}|j         d k    |S r   r   rD   r   s     r4   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s+     #
 #
 #
M1I1ID1I1I1Ir3   c                     i | ]\  }}||	S r2   r2   )rD   ir   s      r4   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>  s    >>>3S!>>>r3   remove_duplicate)Irm   rn   graph_signaturebackward_signature
ValueErrormodule_call_graphr   rj   rM   r   r   ro   r   ri   metaadaptedr7   rp   #_inplace_buffer_and_input_mutations_IValsivals_outline_submodulesrP   values_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrJ   
parametersidrN   rT   cloner   r]   r&   r.   addnon_persistent_buffersbuffers	constantsr/   rR   rS   rV   r0   r   r|   input_specskindr   r>   rK   argr<   rf   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   input_placeholderscheck_input_constraints	enumeratenamed_modulesr~   _reorder_submodulesr   r   )'rd   r   r   fqn_listexport_graphseen_modules
seen_attrsr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferr>   r(   tensor	is_bufferid_to_constr   r)   	_constantconsts_targetsr   added_params_bufferss
const_nameconstph_name_inputs_to_statenode_targettargetsn	fqn_orderr   r   rq   s'                                       @@r4   rn   zUnflattenedModule.__init__  sf	   
 	(;GDEEEKK=+JKKK{b     344'(EFFX^^%%
#'
 !)-*I!J!J!2!.3	*.	&' %9"+L$:NOOOXX
#6|T#J#J j 	
,--//000 	-5tZHHH!.!@*,! #-
$'EE57(3 	& 	&Dt$E%yy++).););KKMM1D *< * *BuII& BuII&#-	    %%%%!$T%9%P!Q!Q%(UUCE(0 	' 	'D---"
&06!
#D)&zz--,2LLNNJ+GRZZ(RZZ(+#*%      &&&& ',,.. !	 !	LD&&&$2B*B*BI&zz\))*2 2) !	 vJJl22 0LF,  F,Q/'.#     f::[00.4K6

+6

+'1	     NP*4::<< 	 	MC(||;..h55 0'~~//H,4BxLL)#BxLL1I#,	     8C47H7H
#&55	7 	7 	7 	7 	7 *-%1 	- 	-Av,,,)***q|*quf-----!!(C00000!!}/9::AEJ   ""18,,,$((22229+++AL+6Y6666Y111quf-----!!(C00000!!}.qx89915:qx   ""18,,, "/!8!>!>!@!@ 	3 	3J//%yyJ...U /.. (5		215
!!"U))WjAAA$((222 )399;; 	< 	<KC...f::Z// '6

3A6
!!"V**gs;;; 13%,,.. 	- 	-K11[111G# - -1%,""- 	T?B///"6|7J7J7L7L"M"MRRRR8RRR6GGG>>8>>>
#
 #
!Z-#
 #
 #
 (,$>>)H*=*=>>>	))5)AA 	1 	1GD!9$$"%i..	$D),,,
r3   c                     |                                  D ]_\  }}t          |dz              t          |d          r8t          |j        t
          j        j                  rt          |j                   `d S )N:rj   )r  printrK   rS   rj   rM   r   r   )rd   r   r   s      r4   _print_graphzUnflattenedModule._print_graph  sw    **,, 	! 	!HC#)sG$$ !CIux~)N)N !ci   	! 	!r3   c                 T   | j         d         j        }||j        k    r|S | j        t	          d          | j                            |j        ||| j        |          }t          |          |j        j        k    r,t	          dt          |           d|j        j                   |S )Nr   zeThere is no flat args adapter specified. Are you sure you are calling this with the right arguments? )r   r   r   r   r   z<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )	r   	signaturein_specr   	TypeErrorr   r   r~   
num_leaves)rd   	flat_argsr'  inputr&  s        r4   _adapt_flat_argsz"UnflattenedModule._adapt_flat_args  s    *1-7	i'''!)O  
 .44%-"$ 5  I 9~~!2!===G #IG G(1(9(DG G  
 r3   c                    | j         d         j        }|}|rt          ||j                  }t	          j        ||f          \  }}d |D             }t                      r|S ||j        k    rP| j        s+t          d| dd|j                    t          d           | 	                    |||          }d| _        | j
        r5ddlm} | j        du rd	 |D             }	n|}	 || j        |	| j                   |S )
Nr   c                     g | ]
}|d          S r   r2   rD   xs     r4   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>-  s    777aQqT777r3   zGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: z5Adapting flat arg to match exported module's treespecT)"_check_input_constraints_for_graphc                 R    g | ]$}t          d           t          d          f|f%S )r   )idxz<unknown location>r   )r   r   rD   r   s     r4   r   z<UnflattenedModule.process_forward_inputs.<locals>.<listcomp>F  sJ     + + + "a(((*:N*O*O*OPRUV+ + +r3   )r   r&  r   r'  r   tree_flatten_with_pathr   r   r#  r,  r  torch._export.utilsr1  r  r   )
rd   r   ru   r&  reordered_kwargsflat_args_with_pathr'  r*  r1  new_flat_args_with_paths
             r4   process_forward_inputsz(UnflattenedModule.process_forward_inputs#  s~   *1-7	! 	I-fi6GHH'-'D#$(
 (
$W 87#6777	?? 	i'''< O3'.3 3 3D1BDD  
 MNNN--i$GGIDL' 	 ONNNNN|t##+ +(+ + +''
 +>'..')@$BX   r3   c                 |   t          j                            | j        d          |i |}| j        d         j        }t                      rbt          j                            | | j	                  j
        |ddi}t          |t                    rt          |          dk    r|d         S |S t           j                                        s| j        s! t#          | j                  j        | g|R  }n0t          j                            | | j	                  j
        |ddi}t)          j        ||j                  S )Nz*do not trace into preprocessing the inputs)reasonr   rv   rw   Fr   )rM   _dynamodisabler;  r   r&  r   r   r   rj   r   rS   r   r~   rx   ry   rp   rz   ri   r{   r   tree_unflattenout_spec)rd   r   ru   r*  r&  
return_valtree_outs          r4   r{   zUnflattenedModule.forwardS  sY   M))'? * 
 
   	 *1-7	?? 	--d$*-EEI16 J *e,, %ZA1E1E!!}$>--// 	t7Q 	6tD-..6tHiHHHHHx++D
+CCG16 H $Xy/ABBBr3   c                     t           j                            | | j                  | j        d<   | j                                         d S )Nri   )rM   r   r   rj   r   r   rc   s    r4   r   zUnflattenedModule.finalizek  s:    (-(<(<T4:(N(Nn%
r3   c                    t          t                    | j        D ]}|j        r||j        ru|j        }t          | |                    ||                    }d|v r|                    d          n|dg\  }}|                             t          |          |f           t          t                    }|D ]V}	d|	v r5|	                    dd          \  }
}||
                             |           ;|d                             |	           W                                D ]\  }
}d t          |          D             }t          |          dk    rt!          t          |                    D ]Y}t#          |
|dz             }||vr@|                    d          ^ }}t%          | |          j                            |           Z|                     |
t-          ||
         |                     ҈fd}|                     d	          D ]d\  }}t1          |d
          r |||j                   't1          |d          r-|j        D ]%}t1          |d
          sJ  |||j                   &edS )zFor a module whose call signatures are preserved, replace
        multiple modules corresponding to multiple calls to that module
        with a single dispatcher module that tracks which module to call.
        r   0r@   r   r   c                     g | ]\  }}|S r2   r2   )rD   r  r   s      r4   r   z7UnflattenedModule._dispatch_modules.<locals>.<listcomp>  s    KKKFAsCKKKr3   c                     |j         D ]C}|j        dk    r6|j                            d          d         }| r|  d| n|}|v r||_        Dd S )Nr   r   r   r@   )r   r   r<   rI   )rY   rj   r   r   pathcalled_moduless        r4   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  su     * *7m+++++C003C06?f,,s,,,CD~--&)* *r3   Fr   rj   r   N)r   r|   r   r   r&  r   getrI   r   intrJ   rsplitr   rR   sortedr~   range
_call_namer   rQ   popset_submoduler"   r  rK   rj   r   )rd   r   r  r   r   r   baser3  	attrs_mapr<   orig_fqnr   indexed_call_modulesr   r   rY   rK  mod_rJ  s                     @r4   r  z#UnflattenedModule._dispatch_moduleso  s    %T**+ 	= 	=Ey =U_ = i&=&A&A#s&K&KLL.1SjjCIIcNNNsCj	ct$++SXXsO<<<$$	$ 	* 	*Ff}}!'sA!6!6$(#''----"!!&)))) /=.B.B.D.D 	 	*H*KKf5I.J.JKKKL<  1$$s<0011 Q QA$Xq1u55C"999(+		#/f==FJJ4PPP""/	(0C\RR  	* 	* 	* 	* 	* **E*BB 	8 	8HCsG$$ 8""3	2222o.. 8- 8 8D"411111&&sDJ7777	8 	8r3   TFc                 *    t          | d||||          S )Nr#   r   r   s        r4   r   z UnflattenedModule.print_readable  r   r3   rB   r   )r+   r,   r-   r   r   r    rn   r$  r,  r;  r{   r   r  r   r   r   s   @r4   r#   r#     s         8<c c&c $O4c c c c c cJ! ! !  6. . .`C C C0  88 88 88x 
 
 
 
 
 
 
 
r3   r#   r*   r   ra   c                 @    t          |           } t          | |          S )a  Unflatten an ExportedProgram, producing a module with the same module
    hierarchy as the original eager module. This can be useful if you are trying
    to use :mod:`torch.export` with another system that expects a module
    hierachy instead of the flat graph that :mod:`torch.export` usually produces.

    .. note:: The args/kwargs of unflattened modules will not necessarily match
        the eager module, so doing a module swap (e.g. :code:`self.submod =
        new_mod`) will not necessarily work. If you need to swap a module out, you
        need to set the :code:`preserve_module_call_signature` parameter of
        :func:`torch.export.export`.

    Args:
        module (ExportedProgram): The ExportedProgram to unflatten.
        flat_args_adapter (Optional[FlatArgsAdapter]): Adapt flat args if input TreeSpec does not match with exported module's.

    Returns:
        An instance of :class:`UnflattenedModule`, which has the same module
        hierarchy as the original eager module pre-export.
    )r   r#   )r*   r   s     r4   r$   r$     s"    , #6**FV%6777r3   rj   r   c                    t          t          t          | j                                      }|j        dk    rt          |j                  dk    sJ |j        d         }d | j        D             }i }d |j                                        D             fd|j	                                        D             }|
                    |j                   t          |          }|d|         D ]}||j                 }||         }	|                     |          5  |                     dt          j        j        j        j        |	|f          |j                                        D ]\  }
}|j        |
<   	 ddd           n# 1 swxY w Y   |                    fd	           t-          ||d                   }|f|_        dS )
a^  Transform buffer and input mutations from their functionalized form
    into copy_ nodes in the graph.

    Functionalization represents a buffer mutation by passing the buffer as
    an input and output. For example, consider the eager code:
        def forward(self, x):
            self.buffer += x
            return x * x

    This corresponds to a graph that looks like:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            mul = aten.mul(x, x)
            return (mutated_buffer, mul)

    We want to inplace this into something that looks like the original
    eager code:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            buffer.copy_(mutated_buffer)
            mul = aten.mul(x, x)
            return (mul,)

    Input mutations are handled similarly.
    outputr   r   c                 4    i | ]}|j         d k    |j        |S r   )r   r   r   s     r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>  s/        47m3K3K	43K3K3Kr3   c                     i | ]\  }}||	S r2   r2   )rD   rE   
buffer_fqns      r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>  s+          '!Z
A     r3   c                 (    i | ]\  }}||         S r2   r2   )rD   rE   r_  buffer_fqn_to_input_names      r4   r   z7_inplace_buffer_and_input_mutations.<locals>.<dictcomp>  s5     # # #Az 	
#J/# # #r3   Ncall_functionc                     | uS rB   r2   )r0  new_nodes    r4   <lambda>z5_inplace_buffer_and_input_mutations.<locals>.<lambda>  s    1H;L r3   )nextiterreversedr   r   r~   r   inputs_to_buffersrR   buffers_to_mutaterP   user_inputs_to_mutater   inserting_aftercreate_noderM   opsatencopy_defaultr   replace_all_uses_withr   )rj   r   output_nodereturn_argsinput_name_to_nodemutation_name_to_input_namenum_mutationsmutation
input_name
input_noderE   vuser_outputsra  rd  s                @@r4   r   r     sY   : tHU[112233K>X%%#k.>*?*?1*D*D*D*D"1%K $)K   #%   +:+L+R+R+T+T     # # # #,>DDFF# # #
  &&'LMMM344M/ N N0?
'
3
""8,, 	% 	%((!5!=
H?U H !++-- % %1#$a  %	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	&&x1L1L1L1LMMMM ]^^455L%(Ks   AFF
	F
	c                 x    t          |           t          |          k     o|dt          |                    | k    S )z2Check whether `candidate` is a prefix of `target`.N)r~   )	candidater<   s     r4   
_is_prefixr    s4    y>>CKK'QF3CS^^3C,D	,QQr3   
parent_fqn	child_fqnc                    | dk    r|S |                      d          }|                     d          }|d t          |                   |k    rt          d| d|  d          d                    |t          |          d                    S )Nr   r@   zChild module 'z(' is not a descendant of parent module 'za'.This is currently unsupported.Please try to make child module attach to parent module directly.)rI   r~   RuntimeErrorr   )r  r  parent_splitchild_splits       r4   _compute_accessorr  !  s    R##C((L//#&&K &S&&&'<77PY P PPZ P P P
 
 	

 88KL 1 1 3 34555r3   r0  yc                    dt           j        j        dt          fd}t	          | j        t           j        j                  sJ t	          |j        t           j        j                  sJ  || j                   ||j                  k    S )Nrj   ra   c                    g }i dt           ffd}t          | j                  D ]\  }}d t          j        ||j                  D             }|d t          j        ||j                                                  D             z  }|j        dv r|j	        nd}|
                    | d|j         d| d	d
                    |           d           |t          |          <   d                    |          S )Nra   c                     t          | t          j        j                  r%dt	          t          |                              z   S t	          |           S )N%)rS   rM   r   Noderf   r   )r   	nodes_idxs    r4   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump8  sB    #ux}-- 5S2c77!34444s88Or3   c                 ,    g | ]}t          |          S r2   )rf   r5  s     r4   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>>  s    RRRcSRRRr3   c                 "    g | ]\  }}| d | S )=r2   )rD   keyvalues      r4   r   z@_check_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>?  s:       C       r3   )rb  get_attrr   z: [z](z, )r   )rf   r  r   r   tree_mapr   ru   rR   r   r<   r   r   r   )rj   retr  r   r   	args_dumpr<   r  s          @r4   
graph_dumpz,_check_graph_equivalence.<locals>.graph_dump4  s1   $&		S 	 	 	 	 	 	
 !-- 	$ 	$GAtRRV_Xty-Q-QRRRI  "(/(DK"H"H"N"N"P"P   I %)G/L$L$LT[[RTFJJ!JJtwJJJJ499Y3G3GJJJKKK"#Ibhhyy~~r3   )rM   r   r   rf   rS   rj   )r0  r  r  s      r4   _check_graph_equivalencer  3  s    %(. S    ( agux~.....agux~.....:ag**QW"5"555r3   gmc                     d}t          | d|           r|dz  }t          | d|           d| }t          | ||           |S )Nr   _spec_r   )rK   rL   )r  specr   r   s       r4   	_add_specr  M  sd    	A
"lqll
#
# 	Q "lqll
#
# A<<DBdKr3   c                     | j                             t          j        |f          }| j                             t          j        |df          }|S r   )rj   rb  r   tree_flattenoperatorgetitem)r  r   flatten	getitem_0s       r4   _generate_flattenr  V  sB    h$$V%84'BBG&&x'7'1FFIr3   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rB   )r  rj   r  rb  	fx_pytreetree_flatten_spec)r  r   r  r   	spec_nodes        r4   _generate_flatten_specr  \  sH     RD!!$''I8!!)"=i?PQQQr3   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rB   )r  rj   r  rb  r   r@  )r  r   r  r   r  s        r4   _generate_unflattenr  d  sH     RD!!$''I8!!&"7%9KLLLr3   r   c                     |                     d          ^ }}|D ]<}t          | |d           }| d S t          |t          j        j                  s d S |} =t          | |d           S )Nr@   )rI   r   rS   rM   rN   rO   )r   r<   rY   rZ   rG   submods         r4   _get_submoduler  l  sy    \\#&&NVU 	 	dD))>44&%(/22 	443t$$$r3   module_to_addcreate_modulec           	         |                     d          ^ }}t          |          D ]\  }}t          | |d           }|[|* |d                    |d |dz                                }nt          j                                        }t          | ||           t          |t          j        j                  s dS |} | 	                    ||           d S )Nr@   r   F)
rI   r  r   r   rM   rN   rO   rL   rS   
add_module)	r   r<   r  r  rY   rZ   r   rG   r  s	            r4   _add_submoduler  }  s     \\#&&NVUV$$  4dD))>(&sxxwQw'@'@AA**Cv&&&&%(/22 	55NN5-(((((r3   rT  r  c                 &    |dk    r| n	|  d|dz
   S )Nr   r   r2   )rT  r  s     r4   rQ  rQ    s(     6644$00Q000r3   	call_namec                 Z    t          j        t          j        |          dz   |           d uS )Nz(@\d+)?$)rematchescape)r  rT  s     r4   rC   rC     s&    8BIdOOk19==TIIr3   c                       e Zd Z	 ddej        j        deej        j        df         deee	e
e	         ef                  dee	ef         de
eej        j        ef                  f
dZd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )_ModuleFrameN
flat_graphr   .module_stackr   r*   c                     | _         | _        | _        | _        | _        | _         _        | _        |	 _        |
 _	        d _
         j        d         \   _        }t           j        |dz              _          |&| _        t          |d          r|j        ni  _        n\ j                             j        t%          t&          j                                                             _        j         _         j        j         _        i  _        i  _        d  _        t5          j         j                  } fd}t7          j        | j        |           j                            |           _         j         j                 r. j         j                 d         }|j        j         j        _         j         j                                     t?           j        j         j        j         j         j        |dz    j                             |
                     j                  }| j        |j         j!        d	k    sJ |j         j"        d         }|j         j"        d         }|j#        J |j#        J  j        $                    d           5   fd
tK          |j!                  D             }i }|j#        D ]} j        &                    |          ||<    tO           j        tQ          |          |f|j                   }tS          |j*                  D ]\  }} j        +                    dtX          j-        ||ft]          |t^                    s|j0        nd|           }t]          |t^                    rd|j0         j        v rHtc          j1         j        |j0                 j2                  |_2        | j         j        |j0                 <   	 d d d            n# 1 swxY w Y    j        j        3                     j                  5  g }|j*        D ]}t]          |t^                    r|                    |j4                   2|j0         j        vr|                    d            Vt]          |tj          tl          tn          tp          f          sJ |                     j        9                     j        |j0                                      tu           j        j        ||j                   } j        j        ;                    tX          j-        |df           j        j        ;                    tX          j-        |df           fdtK          |j!                  D             } fd|j#        D             }d d d            n# 1 swxY w Y    j        J tQ          |           j        _<        | j        _=        d S d S d S )NFr   r   rk   c                     j         rj          d|  n| }|j        v rj        |         S t          t          j                                                  }|j        |<   |S )Nr@   r  )r   created_modulesr!   rM   r   r   )r   rI  r  parentrd   rk   s      r4   r  z,_ModuleFrame.__init__.<locals>.create_module  sp    06
C&*,,s,,,4////55*58>>+;+;CCC-3$T*r3   r   )r  parent_moduleparent_call_moduler   call_idxr*      c                 J    g | ]}j                             d |            S )_positional_arg_)rj   r   )rD   r3  rd   s     r4   r   z)_ModuleFrame.__init__.<locals>.<listcomp>  sB        J**+Cc+C+CDD  r3   rb  
_constant_)r   r<   r   r   c                 h    g | ].}j         j                            t          j        |f          /S r2   r  rj   rb  r  r  )rD   r   	args_noderd   s     r4   r   z)_ModuleFrame.__init__.<locals>.<listcomp>9  sF        K%33H4DyRSnUU  r3   c                 j    i | ]/}|j         j                            t          j        |f          0S r2   r  )rD   rE   kwargs_noderd   s     r4   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>=  sO         t{(66 (;*:   r3   )>r  r   
seen_nodesr
  r  r  r  r  	module_idr   verboser   rQ  r  r*   rK   r   rL  r!   rM   r   r   rj   node_mapnode_to_placeholderr  r  r  r   rQ   r   _SubmoduleEntryr'  num_childrenchildren_specscontextrl  rP  r   r  r   r  inputsrm  r  r  rS   r   r   copyr   inserting_beforer  r   r   r   r   remap_inputr  rb  r   ru   ) rd   r  r   r  r
  r  r  r  r  r  r   r*   	num_callsr   r  base_module_framer&  	args_speckwargs_spec	arg_nodeskwarg_nodesr   r*  r3  r   flat_arg_nodeinput_nodesr+  inputs_noder  r  rk   s    `      `                     @@@r4   rn   z_ModuleFrame.__init__  s    %
$($.("!2"&"3B"7"i#DHi!m<<V DK)0)A)AIrDJJ.22!%(.."2"2r::: DK  DJ[&
 =?#% ;?(T^DDH       6=(DKOOO&,l&>&>x&H&HD# 0 I$($5dn$Ea$H!'8'?'H$dn-44#{"&+"4'+'>&];  	 	 	 &))$.99	 T[%<$1Q6666!)8;I#+:1=K$,,,&222++D11     $Y%;<<  	 !'/ E ED(,
(>(>t(D(DK%%2K9%%{3% 	
 !*)*: ; ;  HC$(J$:$:*'/'- $.c3C#D#D4CHH!3c!3!3 %; 	% 	%M "#'788 ! x4?22-1Ytsx7P7U-V-V*) 01JK!              B "33D4KLL * *=?&-  E!%)9:: #**5;77774?::#**40000)! . . / 0	       $** K33DOEJ4OPP    2K&%  !K-;;${A&6 	 #k/==${A&6     "9#9::  	     )0	  K* * * * * * * * * * * * * * *V *666+0+;+;D#(-8D#***k ! %<%<s&   5EPP	P	4E5V55V9<V9c                 X   | j         dk    sJ d| d            |j        | j        u sJ | j                            d           5  | j                            |j        |j                  }d d d            n# 1 swxY w Y   t          j        |j                  |_        || j	        |<   d S )Nr   zCannot add placeholder z to root module)	type_expr)
r   rj   r  r  r   r   rz   r  r   r  )rd   r0  placeholder_nodes      r4   add_placeholderz_ModuleFrame.add_placeholderG  s    x2~~~KKKK~~~w$/))))Z((.. 	P 	P#z55af5OO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P !%	!& 1 1&6 ###s   'A77A;>A;c                 T   t          j        t          j        j        | j        |j                  }t          j        t          j        j        | j        |j                  }| j        	                    |j
        ||          }t          j        |j                  |_        || j        |<   |S rB   )r   tree_map_onlyrM   r   r  r  r   ru   rj   rb  r<   r  r   r  )rd   r0  r   ru   r   s        r4   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_functionR  s}     #EHM43CQVLL%ehmT5EqxPPz''$??Iaf%%	ar3   c                    |j         | j        u sJ || j        v r| j        |         S |                     d| d           || j        v r| j        |         S |j        dk    s| j                            | j                  | 	                    |           | j
        o| j        j                             | j
                  5  | j
                            d| j                            |                     d d d            n# 1 swxY w Y   | j        |         S |j        dk    r|j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        t           j        j        j        j        fv s%t9          |j        d          r2|j        j        dk    r"|                     |           | j        |         S | j                            | j                  | j                             | |          S tC          d|j         d	|           )
Nzremap_input(r  r   r   rb  r,   	_operatorz(Could not run remap_input() on op type: z
 for node )"rj   r  r  r#  r  r   r   rL  r   r  r  r  r  
insert_argr  r<   rM   rn  ro  sym_sizerM  rG   rq  unbindsumdim_IntListviewdiffrK   r,   r  r   readr  )rd   r0  s     r4   r  z_ModuleFrame.remap_input`  s}   w$/))))=##

&!&&&'''(((+A..DM!!T%;%?%?%I%I%Q   ###&2 [&778OPP V V+66q$+:Q:QRS:T:TUUUV V V V V V V V V V V V V V V+A..T_$$H	'+	#+	%)	".	#+	#+  ,// 56H4G;4V4V ''***=###''11= :??4+++N14NN1NN  s    4D  DDc           	           j                              j        d            g } j                             j                  }| j        |j        D ]}t          |t          t          t          t          t          f          rJ|j         j        v r&|                     j        |j                            d|                    d            zt#          d|            fdt%           j        t)          fd|D                       |j                  }t-           j        j         j        |j                  }|}ng } j                                        D ]Q}|j        D ]G}|j         j        vr7|                    |           |                     j        |                     nHR j        }t7          |          dk    r|d         }t          |t8          t:          j        j        f          sJ  j         !                    |           |d S t          |t:          j        j                  r|j"                            d          nd |D             |j"        d<   t7          |          dk    r|| j        j        |d         <   d S tG          |          D ]c\  }	}
|
t:          j        $                    |          |	         j%        }|
j"                            d          |j"        d<   | j        j        |
<   dd S )Nz'Unsupported data type for output node: c                     | d S j         | j                 }|j        v rj        |         S |j        v rj        |         S t	          d|  dj                   )NzCould not find output node z	. Graph: )r  r   r  r  r  rj   )r\  	seen_noderd   s     r4   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    >4 OFK8	--=33$":::3I>>&SfSStzSS  r3   c              3   .   K   | ]} |          V  d S rB   r2   )rD   r\  r  s     r4   rH   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      PP,,V44PPPPPPr3   r   r   valc                 B    g | ]}|j                             d           S )r  )r   rL  )rD   os     r4   r   z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>  s&    ;;;!&**U##;;;r3   )&r  rR  r   r   rL  r  r  r   rS   r   r   r   r   r   r   r  r   r  r  r*   r   rA  r  r  r  keysusersr~   r|   rM   r   r  rj   r\  r   r  Proxyr   )rd   orig_outputsr&  r\  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr  s   `           @r4   finalize_outputsz_ModuleFrame.finalize_outputs  sH     4000*..t~>>	 T[%<#+  &&'((	 	  {do55$++DOFK,HIIII$++D1111&J&JJ       0PPPP<PPPPP" M
 3I"D$;Y=O3 3J HUMMM!]//11  	!*  I ~T_<<$++I666%,,T]9-EFFF	 = 0J=!!Q&& -a 0-$)>?????
-((( F -77<M""5)));;];;; 	 |!!i&74>DK a111"+L"9"9 > >;&!HNN:66q9>	(3(8(<(<U(C(C	u%4=$[11> >r3   c                     |                      d|                                           | j                            || j                  | j        |<   || j        |j        <   d S )Ncopying)r#  format_noderj   	node_copyr  r  r  r   r   s     r4   	copy_nodez_ModuleFrame.copy_node  sW    

9d..00111"j2249IJJd%)	"""r3   c                    t          | j        j                  D ]F\  }}|                     ||j                            d          |                                           Gd}| j        |         }|j        dk    r2|                     |           |dz  }| j        |         }|j        dk    2| 	                    |           | j        j        D ]"}|j        dk    r|                     |           #d S )Nnn_module_stackr   r   r   r\  )
r  r  r   r#  r   rL  r  r   r  run_from)rd   r   r   node_idxs       r4   	run_outerz_ModuleFrame.run_outer  s    !677 	P 	PGAtJJq$)--(9::D<L<L<N<NOOOO z(#g&&NN4   MH:h'D g&&
 	h O) 	% 	%Dw(""t$$$	% 	%r3   c                 0    | j         rt          |i | d S d S rB   )r  r#  )rd   r   ru   s      r4   r#  z_ModuleFrame.print  s/    < 	#4"6"""""	# 	#r3   c                 4   d}|t          | j                  k     r| j        |         }|j        dk    sJ |                                  |                     d||                                           |                     | j                   t          | j                  }|j        dk    r|dk    r|S |                                  |S t          |j                            di                     dk    rt          d|           |j        d         }ddl
m} t          |          dk    r||v r| j        }n)d	 |j        d                                         D             }|d |         | j        k    rK|                                  |                     d
| j                   |                     | j                   |S |J t          | j        |          r||         }|                     d|           t!          |j        d                                                   |         }	t%          | j        | j        | j        | j        | j        | j        | | j        |gz   |	                    d          d         | j        
  
                            |          }|dz  }|| j        k    sJ |j        dk    r*| j        | j                                     |j                   |                     |           |dz  }|t          | j                  k     d S d S )Nr   r   STEPr\  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYc           	          g | ]=\  }\  }}||r|nd d|v r(t          |                    d          d                   ndf>S )Nr   r  r   )rM  rI   )rD   rE   rI  rk   s       r4   r   z)_ModuleFrame.run_from.<locals>.<listcomp>&  sj     % % % &:D"	 ",14AGGCLL,---q% % %r3   	outliningzCreating new stack frame forr   r  )r~   r   r   r#  r  r  r  r   rL  r  (torch._export.passes._node_metadata_hookr  rR   r   rj   r  r|   r  r  r  r  r
  r  r  rI   r   r  r  r   r<   r  )
rd   r  
module_idxr   depthr  r  node_module_stacknext_modulenext_module_keys
             r4   r  z_ModuleFrame.run_from  sM   
TZ((:h'D7m++++JJLLLJJvx)9)9););<<<JJt())))**Ew(""A:: $O %%'''49==!2B7788A=="#Td#T#TUUU"i(9:O     
 O$$))./AA %)$5!!% % *.3D)E)K)K)M)M% % %! !%(D,=== %%'''

;111

4:&&&$000$+->??  06

9;GGG #'ty1B'C'H'H'J'J"K"KE"R'OJO%O(%5#))#..q1*  (8$$  a
 %(99999w*$$/33DK@@@NN4   MHq TZ((((((r3   rB   )r+   r,   r-   rM   r   r   r   r  r|   rf   r   rM  r   r   r   r   r#   rn   r  r  r  r  r  r  r#  r  r2   r3   r4   r  r    s8        LPb9 b9HNb9 UX]C'(b9 5hsmS!89:b9  %8 89b9 ux35FFGHb9 b9 b9 b9H	7 	7 	7  ) ) )VX> X> X>t* * *
% % %&# # #[ [ [ [ [r3   r  c                       e Zd ZU eed<   ej        j        ed<   ej        j	        ed<   eed<   e
ed<   ej        j        ed<   dS )r  r  r  r  r   r  r*   N)r+   r,   r-   rf   rg   rM   rN   rO   r   r  rM  r2   r3   r4   r  r  _  s]         OOO8?"""%%%	HHHMMMHOr3   r  
orig_graphroot_modulec                     i }t          t                    }t          t                    }i }t          | t	          | j                  ||||d dgdd |j        D             |                                           ||fS )N)r   Nr   r   c                 6    i | ]}|j         	|j        |j         S r2   )r&  r   r   s     r4   r   z'_outline_submodules.<locals>.<dictcomp>x  s6     	
 	
 	
	
Iu	
 	
 	
r3   )r*   )r   r|   rJ   r  r   r   r   r  )r%  r&  r  r
  r  r  s         r4   r   r   i  s    +-J5@5F5FL&1#&6&6J24Oj	
	
 	
$6	
 	
 	

     ikkk##r3   r   r  r   rY   c                    |dk    rit          |                                          dd          D ]?}t          | |          -t          | |t          j                                                   @g }t          | j                                                  D ]i\  }}|||z   }t          |||
                    d          d         dz              t          | |           |                    ||         ||f           j|                    t          j        d                     |D ]\  }}}|                     ||           d S )Nr   r   r   r   r@   )rY   )r  )r|   r  r  r  rM   rN   rO   rQ   rR   r  rI   delattrr   sortr  
itemgetterregister_module)r  r   rY   r   childrenr   childr  s           r4   r  r    sU    ||	(())!""- 	? 	?Cfc**2vsEHOO,=,=>>>HFO113344 7 7e=tmE9SYYs^^A5F5LMMMM3u56666MMh)!,,M---" , ,4tU++++, ,r3   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r   aY  
    Collect the intermediate values of mutations in a graph.

    Example: in the following graph, suppose that buf_in and buf_out
    are the input and output values of a buffer.

        buf_in = placeholder()
        ...
        ival1 = f0(buf_in, ...)  # inside self.n0(...)
        ...
        ival2 = f1(ival1, ...)  # inside self.n1(...)
        ...
        buf_out = f2(ival2, ...)  # inside self.n2(...)
        return buf_out, ...

    Here ival1 and ival2 are intermediate values created inside
    calls to n0 and n1 respectively, and used inside calls to
    n1 and n2 respectively.
    c                 8    t          t                    | _        d S rB   )r   rJ   node_names_by_fqnrc   s    r4   rn   z_IVals.__init__  s    !,S!1!1r3   c                 \    t          |t          j        j                  r|j        j        S dS r6   )rS   rM   _ops
OpOverload_schema
is_mutable)rd   r<   s     r4   _is_mutablez_IVals._is_mutable  s)    fej344 	->,,ur3   c                    |j         dk    sJ |                     |j                  }t          d|j        |           |sat	          t          |j        d                                                             \  }}| j        |         	                    |j
                   |                    |j        d                   S )zI
        Read state corresponding to a given intermediate value.
        rb  zChecking mutabilityr  r   )r   r8  r<   r#  rf  rh  r   r   r2  r   r   r  r   )rd   mfr   br   r  s         r4   r  z_IVals.read  s    
 w/))))T[))#T[!444 	7 (49->#?#F#F#H#HIIJJFC"3'++DI666~~dil+++r3   c           
         |D ]}|D ]}|j         j        }| j        |j                 fd|j        D             }|D ]}|                    |          5  |                    dt          j        j	        j
        j        |j        d         |f          }t          j        |j                  |_        ddd           n# 1 swxY w Y   dS )zT
        Update states corresponding to intermediate values that were read.
        c                 &    g | ]}|j         v |S r2   r4  )rD   r  
node_namess     r4   r   z!_IVals.update.<locals>.<listcomp>  s%    HHHq16Z3G3G3G3G3Gr3   rb  r   N)r*   rj   r2  r   r   rl  rm  rM   rn  ro  rp  rq  r   r  r   )	rd   
partitionsshared_submodulesr   rj   r   r   rd  r>  s	           @r4   rP   z_IVals.update  s/    ", 	= 	=* = =*!3EI>
HHHHEKHHH! 	= 	=D ..t44 = =#(#4#4+!IN08!Yq\40$ $
 )-	$)(<(<= = = = = = = = = = = = = = =	=	=	= 	=s   AB<	<C C N)r+   r,   r-   r   rn   r8  r  rP   r2   r3   r4   r   r     sZ         (2 2 2  
, , ,= = = = =r3   r   r  c                     |                                 D ]?\  }}|rt          ||          n|}|D ]#}t          | |          }t          |||           $@d S rB   )rR   r   r   rL   )r  r&  r  r  namesr*   r   r  s           r4   r   r     s    
 ',,.. ' '	56?P;	222[ 	' 	'D"d##CFD#&&&&	'' 'r3   c                 f   i }| D ]*}t          |          D ]\  }}t          |j        |j                  }t	          |j        |          }d}|d |         D ]}t          |j        |j                  r|j        }	|j        |j        k    r|	                    d          ^ }
}t          |	|
          j                            |           t          |j        |j                  }t	          |j        |          }||j        _        |||<    n |s|	                    ||j                   d}Ր,|S )NFr@   T)r  rQ  r   r  r  r  r  r*   r  rI   r   rQ   rR  r  r<   rS  )r?  r   r@  r   r   r  r<   deduplicatedseenr  rY   r   seen_child_fqnseen_targets                 r4   r  r    s]    ' $, $,!"344 #	, #	,HAu"59en==I&u'7CCF L)"1"- , ,+DKFF ,"0F x59,,
 )/S(9(9/??HLLTRRR)3DHdm)L)L&7!,n' ' ;F07=K/	:) , ,,VT[AAA'+G#	,J #"r3   r  scopemodule_id_to_inputs_removedc                 $   t          t                    t          |           v r$t          |           t          |                    iS | j                                        D ]^\  }t          t          d          |||gz             }|                                D ] \  }}|                             |           !_t          | dd          }|t          |j
                  dk    rS t          |t          j        j                  sJ t          t!          d |j
                            }	t          |	          dk    rdn|	d         }
t!          d |j
                  }|D ]U}t#          | |j                  <t                    v r+t'          t!          fd|j                            |_        Vi }|	D ]d}|j        |vrd}||j                 D ]@}|                    d	          }|dt          |                   d
 |D             k    r|} nA|_|||<   et                      }|                                D ]	\  }}t          |j                  dk    r|t          |          d         }t1          | |          }t          |t          j        t          j        f          sJ |                    |
          5  |                    dd	                    |                    }ddd           n# 1 swxY w Y   |                    |d           |                    |           |                     |j                   t          | tB                    r| "                                 t          |           |iS )a  Sink params, buffers, and constants from graph inputs into get_attr nodes.

    Exported modules are purely functional, so they pass their parameters and
    buffers in as inputs to the graph.

    To replicate eager's semantics, we need to get them from the module state
    via get_attr instead.

    module: GraphModule, potentially containing nested submodules.
    inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
    scope: tracks where we are in the module hierarchy, so that we can emit the
        right `getattr(self, "foo.bar")` calls, etc.
    module_id_to_inputs_removed: records inputs removed by child modules, mapping
        the module object id to the list of placeholder node names in the child module
        that were removed.
    Nztorch.nn.Modulerj   r   c                     | j         dk    S )Nr   r   r  s    r4   re  z_sink_params.<locals>.<lambda>@  s    14=#8 r3   r  c                     | j         dk    S )Nr   r   rL  s    r4   re  z_sink_params.<locals>.<lambda>D  s    )> r3   c                 <    | j         t                             vS rB   )r   r   )r  rI  	submodules    r4   re  z_sink_params.<locals>.<lambda>L  s    af,G9,VV r3   r@   c                 D    g | ]}|                     d           d         S )r   r   )rI   r/  s     r4   r   z _sink_params.<locals>.<listcomp>Z  s&    )I)I)Ia!''#,,q/)I)I)Ir3   r  T)propagate_meta)#r   rJ   r   rQ   rR   r   r
   rP   r   r~   r   rS   rM   r   r   r|   filterr   r<   r   r   r   rI   r  r   rV   rX   rl  rm  r   rr  
erase_noder   r!   r   )r*   r  rH  rI  r   submod_id_to_inputs_removedrE   r{  rj   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrrd  rO  s      `                 @r4   r   r     s$   , #*&1#&6&6#	&zz0006

76

CDD "?0022 5 5i&2"I..TFN'	'
 '
# 05577 	5 	5DAq'*11!4444	5 FGT**E}EK((A--**eUX^,,,,,&88%+FFGGF [[A--TT6":N >>LL! 
 
fdk22	  R	]]6Q%Q%QVVVVVI  DI @B 4 49O++
!$), 	 	Bxx}}H#e**%)I)I5)I)I)III%
 J )3 &&  #uuN4::<< & &jtz??Q"3u::<<0I0CCJj5<9K*LMMMMM &&~66 N N ,,Z)9L9LMMN N N N N N N N N N N N N N N &&x&EEE49%%%%&+,, vJJ''s   *LL		L		)TrB   )r   )dr   r  loggingr  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   r   rM   torch.fx._pytreer   _pytreer  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   r   r   _remove_effect_tokens_passr   	getLoggerr+   log__all__r&   r7   r9   rV   rX   rN   rO   rf   boolr]   r_   r!   r"   ABCr    r#   r$   r   r   r  r  r  r  r   r  r  r  r  r  r  rM  rQ  rC   r  r  r   r   r  r   rJ   r   r  r|   r   r2   r3   r4   <module>rs     s   



    				 # # # # # # % % % % % %       ! ! ! ! ! !       7 7 7 7 7 7 7 7 7 7 7 7 7 7  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ? ? ? ? ? ? 3 3 3 3 3 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 3 2 2 2 2 2 U U U U U U U U U U 7 7 7 7 7 7 7 7 = = = = = = g!!            ( ( (" 00 00EL%"4ehoEF00x00 00 	00
 00 00 00 00f       U
 U
 U
 U
 U
 U
 U
 U
p-" -" -" -" -".%(/ -" -" -"`    cg   $[
 [
 [
 [
 [
 [
 [
 [
~ MQ8 88080I88 8 8 84E)8>E))E) 
E) E) E) E)PR R R
6# 6# 6# 6 6 6 6$6 6EHO 6 6 6 64%(/ C    %(.     Reh"$57HHIR
X]R R R RMeh"$57HHIM
X]M M M M% % % % % %* AE	) )	)) 8?) HcUEHO%;<=	) ) ) )41S 1S 1S 1 1 1 1JS J J J J J J
z z z z z z z zz        $EHN $AR $ $ $ $4 GI, ,HO,(,S#X,@C, , , ,,?= ?= ?= ?= ?= ?= ?= ?=D	'	'"	' S#c(]#	' 	' 	' 	'(# (# (#^ BF	p( p(HOp(#tCy.)p( 9p( "*$sCH}*=!>	p( p( p( p( p( p(r3   