
    קgX                        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mZmZ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% d d
l&m'Z' d dl(m)Z) d dlm*Z*m+Z+ ddl,m-Z- g dZ. G d de	          Z/da0ed             Z1	 dCdeej2        ej3        f         dej4        j5        de6de/de7f
dZ8 G d dej4        j5                  Z9 G d de j:                  Z; G d dej4        j5                  Z<	 dDd e!d!ee;         d"e<fd#Z=d$ej        j>        d"dfd%Z?d& Z@d'e6d(e6d"e6fd)ZAd*ej4        j5        d+ej4        j5        fd,ZBd-ej4        j5        d"e6fd.ZCd-ej4        j5        d"ej        jD        fd/ZEd-ej4        j5        d"ej        jD        fd0ZFd1ej4        j5        de6fd2ZGd1ej4        j5        de6d3ej4        j5        fd4ZH G d5 d6          ZId7ej        j>        d8e<fd9ZJ	 dEd;ej4        j5        d<ee6eKf         d=e6fd>ZLd ej4        j5        d?ee6ee6         f         d@ee6         fdAZMdB ZNdS )F    N)defaultdict)contextmanager)deepcopy)Enum)AnycastDictListOptionalSetTupleUnion)FakeScriptObject)reorder_kwargs)ConstantArgumentExportedProgram	InputKindModuleCallSignatureSymIntArgumentTensorArgument)is_fx_tracing_print_readable)
GetAttrKeySequenceKey   )_remove_effect_tokens)InterpreterModuleUnflattenedModule	unflattenFlatArgsAdapterc                       e Zd ZdZdZdZdS )	_AttrKind	parameterbufferconstantN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANT     R/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/export/unflatten.pyr#   r#   "   s        IFHHHr.   r#   Tc               #   8   K   t           } da 	 d V  | a d S # | a w xY w)NF)RUN_WITH_INTERPRETER)old_flags    r/   _disable_interpreterr3   +   s?       $H ('x''''s    from_obj	to_moduletarget	attr_kind
persistentc                    |                     d          ^ }}|D ]F}t          ||d           }|/t          j                                        }t          |||           |}G|t          j        k    r9t          | t          j        j	                  sJ |
                    ||            d S |t          j        k    r6t          | t          j                  sJ |                    || |           d S |t          j        k    rZt          | t                    r
J d            t          | t          j        t          j        f          sJ t          |||            d S d S )N.)r8   z2FakeScriptObject should only exist during tracing.)splitgetattrtorchnnModulesetattrr#   r*   
isinstance	Parameterregister_parameterr+   Tensorregister_bufferr,   r   ScriptObject)	r4   r5   r6   r7   r8   prefixfielditemts	            r/   _assign_attrrK   8   s    \\#&&NVU  ItT**9!!AItQ'''		I'''(EH$677777$$UH55555	i&	&	&(EL11111!!%j!IIIII	i(	(	(&
 
 	@ 	@?	@ 	@ 	@ "
 
 	
 	
 	
 		5(+++++ 
)	(r.   c                   X     e Zd ZdZdej        j        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.
    graphc                     t                                                       || _        | | j        _        t          | _        d S N)super__init__rM   owning_moduler1   _run_with_interpeter)selfrM   	__class__s     r/   rQ   zInterpreterModule.__init__b   s:     	
#'
 $8!!!r.   c                    | j         
J d            t                      sHt          j                                        s| j        s# t          | j                   j        | g|R i |S |rt          |          }| j	        t          |          d          }|D ]!}||v r|                    ||                    "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 InterpreterModulerM   enable_io_processingF)graph_moduler   r=   compileris_dynamo_compilingrS   typeforwardlist	arg_nameslenappendtuplefxInterpreterrM   run)rT   argskwargsarg_listkwarg_names
kwarg_names         r/   r]   zInterpreterModule.forwardk   sX    ,,.V,,, 	N..00	8<8Q	 34)**24I$III&III '
  ::"nS]]__="- < <J!V++ z(:;;;
 ;''3v;;66668}}DN(;(;;;;;X8''DJ'??C,1  r.   c                    t           j                            | | j                  | j        d<   | j                                         g | _        | j        j        D ],}|j        dk    r| j        	                    |j
                   -d S )NrY   placeholder)r=   rc   GraphModulerM   __dict__lintr_   nodesopra   r6   rT   nodes     r/   finalizezInterpreterModule.finalize   s     ).(<(<T4:(N(Nn%
 J$ 	3 	3Dw-''%%dk222	3 	3r.   TFc                 *    t          | d||||          S )Nr   r   rT   print_outputinclude_strideinclude_devicecoloreds        r/   print_readablez InterpreterModule.print_readable   (     
 
 	
r.   TFFF)r'   r(   r)   __doc__r=   rc   GraphrQ   r]   rt   r{   __classcell__rU   s   @r/   r   r   \   s         
9x~9 9 9 9 9 9  B3 3 3& 
 
 
 
 
 
 
 
r.   r   c            
       p    e Zd ZdZej        dej        dej        dee	         dee	         fd            Z
dS )r!   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    target_spec
input_spec
input_argsreturnc                     dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr-   )rT   r   r   r   s       r/   adaptzFlatArgsAdapter.adapt   s	     	r.   N)r'   r(   r)   r~   abcabstractmethodpytreeTreeSpecr
   r   r   r-   r.   r/   r!   r!      st          	_ O I	
 
c     r.   r!   c                   T     e Zd Z	 d
dedee         f f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	        
                                | _        t          |j                  | _        || _        d| _        t          | _        t          || j                   t!          ||            |j        | _        g | _        |j        }t)                      }i }| j        j        D ]}||         }	t-          |	          |vrHt          j                            |	                                |	j                  |t-          |	          <   t7          |t-          |	                   | |t8          j                   |                    |           t)          | j        j                  }
t)                      }i }| j        j         D ]}||
v rd}|j!        |         }n
d}||         }t-          |          |vr&|                                |f|t-          |          <   t7          |t-          |                   d         | |t8          j"        |	           |                    |           |#                                D ]\  }}||v s||v rd}t-          |          |v stI          |t          j        j                  sd}|r]t-          |          |vr|df|t-          |          <   t7          |t-          |                   d         | |t8          j"        d	           t-          |          |vr||t-          |          <   t7          |t-          |                   | |t8          j                   i }|j!        #                                D ]\  }}t-          |          |vr@tI          |t          j%                  r|                                }||t-          |          <   |t-          |                   }t7          || |t8          j&                   tO          tP                    "t)                      }"fd
}t)                      }| j        j)        D ]}|j*        tV          j        k    s|j*        tV          j"        k    r|j,        rt[          |j.        d          sJ tI          |j/        t`                    sJ  |t-          |j        |j/                           |j.        j1        |j/                   |                    |j/                   |                    |j/                   |j*        tV          j"        k    r|j,        r*|j*        tV          j2        k    s|j*        tV          j3        k    rt[          |j.        d          sJ tI          |j/        t`                    sJ  |t-          |j!        |j/                           |j.        j1        |j/                   |                    |j/                   |j!        #                                D ]v\  }}||vrmt-          |          "v s
J d            "t-          |                   d         \  }} |t-          |          ||           |                    |j/                   w|j        #                                D ]S\  }}||vrJt-          |          "vr"t-          |                   d         \  }} |t-          |          ||           Ti #"4                                D ]}d |D             }|D ]
\  }}|#|<   tk          | #g            !#fd! !| g            d | j        j6        D             | _7        d| _8        d ts          |          D             } | :                    d          D ]\  }}|| vrtw          |           | |<   ty          | |            d | :                    d          D             tQ          | =                                          k    sJ | j        >                                 d S )Nz%Unflattening on JointExportModule NYIc                     g | ]	}|j         
S r-   )fqn.0entrys     r/   
<listcomp>z.UnflattenedModule.__init__.<locals>.<listcomp>   s    KKK%EIKKKr.   r    F)requires_grad)r7   T)r7   r8   c                 F    |          }|                     ||f           d S rO   )ra   )obj_id	node_nametarget_name	name_list
consts_maps       r/   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_mapG  s,    "6*Ii566666r.   namez?Constants should be either aliased or appear in graph signaturec                     g | ]
}|d          S r   r-   )r   rJ   s     r/   r   z.UnflattenedModule.__init__.<locals>.<listcomp>{  s    111qt111r.   c                 t   t          | d          rj| j        j        D ]]}|j        dk    rP|j        v rGt          fd|j                 D                       r!t          |j         d d| j                   ^|                                 D ]&\  }}                    |            |           'd S )NrM   rl   c              3   t   K   | ]2}|                     d           dt                             k    V  3dS )r:   N)r;   r`   )r   r   scopes     r/   	<genexpr>zJUnflattenedModule.__init__.<locals>.check_module_inputs.<locals>.<genexpr>  sU           #  IIcNN<SZZ<8EA           r.   z was not sunk into the module z which has the graph: )	hasattrrM   rp   rq   r   anyAssertionErrornamed_childrenra   )moduler   rs   r   submodcheck_module_inputsinputs_to_states    `   r/   r   z7UnflattenedModule.__init__.<locals>.check_module_inputs  s   vw'' "L.  D =00 I88        '6ty'A        9 -#yssssekeqss   !' 5 5 7 7 3 3fT"""##FE22223 3r.   c                 (    g | ]}|j         d k    |S rl   rq   r   rs   s     r/   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s+     #
 #
 #
M1I1ID1I1I1Ir.   c                     i | ]\  }}||	S r-   r-   )r   ir   s      r/   
<dictcomp>z.UnflattenedModule.__init__.<locals>.<dictcomp>  s    >>>3S!>>>r.   )remove_duplicatec                     g | ]\  }}|S r-   r-   )r   r   _s      r/   r   z.UnflattenedModule.__init__.<locals>.<listcomp>  s    MMMQMMMr.   )?rP   rQ   graph_signaturebackward_signature
ValueErrormodule_call_graphr   rM   r=   rc   r   r   adaptedr1   rS   _inplace_buffer_mutations_outline_submodulesrange_constraintsequality_constraints
state_dictset
parametersidr>   rB   cloner   rK   r#   r*   addnon_persistent_buffersbuffers	constantsr+   itemsrA   rD   r,   r   r^   input_specskindr   r8   r   argr6   strr   CONSTANT_TENSOR
CUSTOM_OBJvalues_sink_paramsrp   input_placeholderscheck_input_constraints	enumeratenamed_modulesr`   _reorder_submoduleskeysro   )%rT   r   r   fqn_listexport_graphr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferr8   r%   tensor	is_bufferid_to_constr   r&   	_constantconsts_targetsr   added_params_bufferss
const_nameconstph_namer   node_targettargetsn	fqn_orderr   r   r   rU   s%                                    @@@r/   rQ   zUnflattenedModule.__init__   s	   
 	(;GDEEEKK=+JKKK{b     344'(EFFX^^%%
!)-*I!J!J!2$8!!,0DEEEL$///!.!@*,! #-
$'EE57(3 	& 	&Dt$E%yy++).););KKMM1D *< * *BuII& BuII&#-	    %%%%!$T%9%P!Q!Q%(UU  	 (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//uII+++T ,++'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///	3 	3 	3 	3 	3 	34 	D"%%%
#
 #
!Z-#
 #
 #
 (,$>>)H*=*=>>>	))5)AA 	1 	1GD!9$$"%i..	$D),,,MM$"4"4e"4"L"LMMMQUNNR
 R
 
 
 
 
 	
r.   c                     |                                  D ]_\  }}t          |dz              t          |d          r8t          |j        t
          j        j                  rt          |j                   `d S )N:rM   )r   printr   rA   rM   r=   rc   r   )rT   r   mods      r/   _print_graphzUnflattenedModule._print_graph  sw    **,, 	! 	!HC#)sG$$ !CIux~)N)N !ci   	! 	!r.   c                    | j         d         j        }t          ||j                  }t	          j        ||f          \  }}d |D             }t                      rbt          j        	                    | | j
                  j        |ddi}t          |t                    rt          |          dk    r|d         S |S ||j        k    r| j        st!          d| dd	|j                    | j        t%          d
          | j        st!          d           | j                            |j        ||          }d| _        t          |          |j        j        k    r,t%          dt          |           d|j        j                   | j        r5ddlm}	 | j        du rd |D             }
n|}
 |	| j        |
| j                   t          j                                        r/| j        s(t          j                            | | j
                  | }n0t          j        	                    | | j
                  j        |ddi}t	          j        ||j                  S )Nr   c                     g | ]
}|d          S r   r-   )r   xs     r/   r   z-UnflattenedModule.forward.<locals>.<listcomp>  s    777aQqT777r.   rW   rX   Fr   zGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: zeThere is no flat args adapter sepcified. Are you sure you are calling this with the right arguments? z5Adapting flat arg to match exported module's treespec)r   r   r   Tz<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )"_check_input_constraints_for_graphc                 R    g | ]$}t          d           t          d          f|f%S )r   )idxz<unknown location>)r   )r   r   r   r   s     r/   r   z-UnflattenedModule.forward.<locals>.<listcomp>  sJ     + + + "a(((*:N*O*O*OPRUV+ + +r.   ) r   	signaturer   in_specr   tree_flatten_with_pathr   r=   rc   rd   rM   re   rA   rb   r`   r   r   r   	TypeErrorr   
num_leavesr   torch._export.utilsr   r   r   rZ   r[   _run_with_interpreterrm   tree_unflattenout_spec)rT   rf   rg   r   reordered_kwargsflat_args_with_pathr   	flat_args
return_valr   new_flat_args_with_pathtree_outs               r/   r]   zUnflattenedModule.forward  s   *1-7	)&)2CDD'-'D#$(
 (
$W 87#6777	?? 	--d$*-EEI16 J *e,, %ZA1E1E!!}$i'''< 3'.3 3 3D1BDD  
 %-S  
 | SQRRR 288 ) 1&( 9  	
  $y>>Y%6%AAA#K$'	NNK K,5,=,HK K   ' 	 ONNNNN|t##+ +(+ + +''
 +>'..')@$BX   >--// 	8R 	x++D$*==yIHHx++D
+CCG16 H $Xy/ABBBr.   TFc                 *    t          | d||||          S )Nr   r   rv   s        r/   r{   z UnflattenedModule.print_readable   r|   r.   rO   r}   )r'   r(   r)   r   r   r!   rQ   r   r]   r{   r   r   s   @r/   r   r      s         8<p p&p $O4p p p p p pd! ! !GC GC GCV 
 
 
 
 
 
 
 
r.   r   r   r   r   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     r/   r    r      s"    , #6**FV%6777r.   rM   c                 B   t          t          t          | j                                      }|j        dk    rt          |j                  dk    sJ |j        d         }|j        }|dt          |                   }d |j        	                                D             }d | j        D             }|D ]}||j
                 }	||	         }
||
         }|                     |          5  |                     dt          j        j        j        ||f          |j        	                                D ]\  }}|j        |<   	 ddd           n# 1 swxY w Y   |                    fd           t'          |t          |          d                   }|f|_        dS )	a  Transform buffer mutations from their functionalized form into a copy_
    node in the graph.

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

    Will become 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,)
    outputr   r   Nc                     i | ]\  }}||	S r-   r-   )r   kvs      r/   r   z-_inplace_buffer_mutations.<locals>.<dictcomp>G  s    TTT$!QATTTr.   c                 4    i | ]}|j         d k    |j        |S r   )rq   r   r   s     r/   r   z-_inplace_buffer_mutations.<locals>.<dictcomp>H  s/        47m3K3K	43K3K3Kr.   call_functionc                     | uS rO   r-   )r   new_nodes    r/   <lambda>z+_inplace_buffer_mutations.<locals>.<lambda>X  s    1H;L r.   )nextiterreversedrp   rq   r`   rf   buffers_to_mutateinputs_to_buffersr   r   inserting_aftercreate_noder=   opsatencopy_metareplace_all_uses_withrb   )rM   r   output_nodereturn_argsmutation_node_to_buffer	mutationsbuffers_to_inputsinput_name_to_nodemutationbuffer_name
input_name
input_noder  r  user_outputsr  s                  @r/   r   r   +  s   , tHU[112233K>X%%#k.>*?*?1*D*D*D*D"1%K-?:c"9:::;ITT/*K*Q*Q*S*STTT $)K    N N-hm<&{3
'
3
""8,, 	% 	%((!5
H7M H !++-- % %1#$a  %		% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	&&x1L1L1L1LMMMM
 C/00223 L &(Ks   *AEE	E	c                 x    t          |           t          |          k     o|dt          |                    | k    S )z2Check whether `candidate` is a prefix of `target`.N)r`   )	candidater6   s     r/   
_is_prefixr/  c  s4    y>>CKK'QF3CS^^3C,D	,QQr.   
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 mldule 'za'.This is currently unsupported.Please try to make child module attach to parent module direclty.)r;   r`   RuntimeErrorjoin)r0  r1  parent_splitchild_splits       r/   _compute_accessorr7  h  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5r.   r   yc                     dt           j        j        dt          fd} || j                   ||j                  k    sJ d S )NrM   r   c                    g }i dt           ffd}t          | j                  D ]\  }}d t          j        ||j                  D             }|d t          j        ||j                                                  D             z  }|j        dk    r|j	        nd}|
                    | d|j         d| d	d
                    |           d           |t          |          <   d                    |          S )Nr   c                     t          | t          j        j                  r%dt	          t          |                              z   S t	          |           S )N%)rA   r=   rc   Noder   r   )r   	nodes_idxs    r/   arg_dumpz?_verify_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  sB    #ux}-- 5S2c77!34444s88Or.   c                 ,    g | ]}t          |          S r-   )r   r   s     r/   r   zA_verify_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s    RRRcSRRRr.   c                 "    g | ]\  }}| d | S )=r-   )r   keyvalues      r/   r   zA_verify_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>  s:       C       r.   r  r   z: [z](z, )
)r   r   rp   r   tree_maprf   rg   r   rq   r6   ra   r4  r   )rM   retr?  r   rs   	args_dumpr6   r>  s          @r/   
graph_dumpz-_verify_graph_equivalence.<locals>.graph_dump{  s/   $&		S 	 	 	 	 	 	
 !-- 	$ 	$GAtRRV_Xty-Q-QRRRI  "(/(DK"H"H"N"N"P"P   I %)G$>$>T[[BFJJ!JJtwJJJJ499Y3G3GJJJKKK"#Ibhhyy~~r.   )r=   rc   r   r   rM   )r   r8  rK  s      r/   _verify_graph_equivalencerL  z  sY    %(. S    ( :ag**QW"5"5555555r.   gmc                     d}t          | d|           r|dz  }t          | d|           d| }t          | ||           |S )Nr   _spec_r   )r   r@   )rM  specr   r   s       r/   	_add_specrQ    sd    	A
"lqll
#
# 	Q "lqll
#
# A<<DBdKr.   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rO   )rQ  rM   get_attrr  	fx_pytreetree_flatten_spec)rM  rs   rP  r   	spec_nodes        r/   _generate_flattenrW    sF    RD!!$''I8!!)"=i?PQQQr.   c                     t          | |          }| j                            |          }| j                            t          j        ||f          S rO   )rQ  rM   rS  r  r   r  )rM  rp   rP  r   rV  s        r/   _generate_unflattenrY    sF    RD!!$''I8!!&"7%9KLLLr.   r   c                     |                     d          ^ }}|D ]<}t          | |d           }| d S t          |t          j        j                  s d S |} =t          | |d           S )Nr:   )r;   r<   rA   r=   r>   r?   )r   r6   rG   rH   rI   r   s         r/   _get_submoduler[    sy    \\#&&NVU 	 	dD))>44&%(/22 	443t$$$r.   module_to_addc                 8   |                     d          ^ }}|D ]h}t          | |d           }|/t          j                                        }t          | ||           t          |t          j        j                  s dS |} i|                     ||           d S )Nr:   F)r;   r<   r=   r>   r?   r@   rA   
add_module)r   r6   r\  rG   rH   rI   r   s          r/   _add_submoduler_    s    \\#&&NVU 
 
dD))>X__&&FCv&&&&%(/22 	55NN5-(((((r.   c                       e Zd Z	 ddej        j        deej        j        df         dee	         de
e	ef         deej        j                 f
dZd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )_ModuleFrameN
flat_graphrp   .module_stackr   r   c
                 ~
    | _         | _        | _        | _        | _        | _        | _        | _        d _         j        d          _	        |	|	 _
        n0t          t          j                                                   _
         j         j        v r j         j                  _        nd  _         j
         j         j        <    j
        j         _        i  _        i  _        d  _        |bt)          |j	         j	                  }
t+          |j
        |
 j         j
        n j                   |j                            |
           _        |                     j	                  }|ĉ j        |j        j        dk    sJ |j        j        d         }|j        j        d         }|j        J |j        J  j                            d           5  g }t;          |j                  D ]2}|                     j                            d|                      3i }|j        D ]} j                            |          ||<    tA           j
        tC          |          |f|j                  }tE          |j#                  D ]\  }} j        $                    dtJ          j&        ||ftO          |tP                    s|j)        nd| 	          }tO          |tP                    rd|j)         j        v rHtU          j*         j        |j)                 j+                  |_+        | j         j        |j)                 <   	 d d d            n# 1 swxY w Y    j        j        ,                     j                  5  g }|j#        D ]}tO          |tP                    r|j-        |                    d            4|j)         j        vr|                    d            XtO          |t\          t^          f          sJ |                     j        0                     j        |j)                                      tc           j        j
        ||j                  } j        j        2                    tJ          j&        |df           j        j        2                    tJ          j&        |df           fd
t;          |j                  D             } fd|j        D             }d d d            n# 1 swxY w Y    j        J tC          |           j        _3        | j        _4        d S d S d S )NF   r   r   _positional_arg_r  
_constant_)rq   r6   rf   r   c                 h    g | ].}j         j                            t          j        |f          /S r-   parentrM   r  operatorgetitem)r   r   	args_noderT   s     r/   r   z)_ModuleFrame.__init__.<locals>.<listcomp>B  sF        K%33H4DyRSnUU  r.   c                 j    i | ]/}|j         j                            t          j        |f          0S r-   rj  )r   r  kwargs_noderT   s     r/   r   z)_ModuleFrame.__init__.<locals>.<dictcomp>F  sO         t{(66 (;*:   r.   )5rb  rp   
seen_nodesseen_modulesrk  rc  	module_idr   verboser   r   r   r=   rc   r   cached_graph_modulerM   node_mapnode_to_placeholderparent_call_moduler7  r_  call_modulegetr   num_childrenchildren_specscontextr  rangera   rl   rW  rb   r   inputsr  rl  rm  rA   r   r   copyr   inserting_beforerD  r   r   remap_inputrY  r  rf   rg   )rT   rb  rp   rq  rr  rk  rc  rs  r   r   accessorr   	args_speckwargs_spec	arg_nodesr   kwarg_nodesr   r  r   flat_arg_nodeinput_nodesinputinputs_nodern  rp  s   `                       @@r/   rQ   z_ModuleFrame.__init__  s    %
$(("!2$R( DKK+EHNN,<,<==DK>T...'+'8'HD$$'+D$04Ddn-[&
 =?#% ;?(TX>>H /7 KK1   '-l&>&>x&H&HD#%))$(33	 T[%<$1Q6666!)8;I#+:1=K$,,,&222++D11 * *	 !788 W WC$$TZ%;%;<Ts<T<T%U%UVVVV '/ E ED(,
(>(>t(D(DK%%-K9%%{3% 	
 !*)*: ; ; * *HC$(J$:$:*'/'- $.c3C#D#D4CHH!3c!3!3 %; 	% 	%M "#'788 ! x4?22-1Ytsx7P7U-V-V* * 0 OCH5!** * * * * * * * * * * * * * *@ "33D4KLL " "=?&- 	 	E!%)9:: u{?R#**400004?::#**40000)%..1QRRRRR#** K33DOEJ4OPP    2K&%  !K-;;${A&6 	 #k/==${A&6     "9#9::  	     )0	  ;" " " " " " " " " " " " " " "F *666+0+;+;D#(-8D#***Y ! %<%<s&   !E,MM!M	E+T  TT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   rM   rb  r  rl   r   r\   r  r   rw  )rT   r   placeholder_nodes      r/   add_placeholderz_ModuleFrame.add_placeholderP  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 rO   )r   tree_map_onlyr=   rc   r=  r  rf   rg   rM   r  r6   r  r   rv  )rT   r   rf   rg   rs   s        r/   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function[  s}     #EHM43CQVLL%ehmT5EqxPPz''$??Iaf%%	ar.   c                 R   |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 t?          d|j         d	|           )
Nzremap_input(rF  rl   r   r  r(   	_operatorz(Could not run remap_input() on op type: z
 for node ) rM   rb  rv  r   rw  rq   r   rz  r   r  rx  rk  r  
insert_argr  r6   r=   r  r  sym_sizeintrI   defaultunbindsumdim_IntListviewdiffr   r(   r  r3  )rT   r   s     r/   r  z_ModuleFrame.remap_inputi  sM   w$/))))=##

&!&&&'''(((+A..DM!!%))$(33;   ###&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 ''***=##N14NN1NN  s    4D  DDc                     g } j                              j                  }| j        |j        D ]y}t          |t          t          f          rJ|j         j	        v r&|
                     j	        |j                            R|
                    d            ht          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        }t+          |          dk    r|d         }t          |t,          t.          j        j        f          sJ  j                            |           |d S t          |t.          j        j                  r|j                            d          nd |D             |j        d<   t+          |          dk    r|| j        j        |d         <   nst;          |          D ]c\  }	}
|
t.          j                            |          |	         j        }|
j                            d          |j        d<   | j        j        |
<   d j         tC           j          j                   d S d 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: )rq  r   rv  rw  r3  rM   )r  	seen_noderT   s     r/   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_node  sx    >4 OFK8	--=33$":::3I>>&SfSStzSS  r.   c              3   .   K   | ]} |          V  d S rO   r-   )r   r  r  s     r/   r   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      PP,,V44PPPPPPr.   r   r   valc                 B    g | ]}|j                             d           S )r  )r   rz  )r   os     r/   r   z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>  s&    ;;;!&**U##;;;r.   )"r   rz  r   rk  outputsrA   r   r   r   rq  ra   r3  rY  r   rb   r  rW  rx  rv  r   usersr`   r^   r=   rc   r=  rM   r  r   r   Proxyrs   ru  rL  )rT   orig_outputsr   r  tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr  s   `           @r/   finalize_outputsz_ModuleFrame.finalize_outputs  s8   *..tx88	 T[%<#+ 	 	f~~&FGG {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
 3D"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"+L"9"9 > >;&!HNN:66q9>	(3(8(<(<U(C(C	u%4=$[11#/%d&>LLLLL 0/r.   c                     |                      d|                                           | j                            || j                  | j        |<   || j        |j        <   d S )Ncopying)r   format_noderM   	node_copyr  rv  rq  r   rr   s     r/   	copy_nodez_ModuleFrame.copy_node  sW    

9d..00111"j2249IJJd%)	"""r.   c                    d}| j         j        D ]H}|                     ||j                            d          |                                           |dz  }Id}| j        |         }|j        dk    r2|                     |           |dz  }| j        |         }|j        dk    2|                     |           | j         j        D ]"}|j        dk    r|                     |           #d S )Nr   nn_module_stackr   rl   r  )	rb  rp   r   r   rz  r  rq   r  run_from)rT   r   rs   node_idxs       r/   	run_outerz_ModuleFrame.run_outer  s   O) 	 	DJJq$)--(9::D<L<L<N<NOOOFAA z(#g&&NN4   MH:h'D g&&
 	h O) 	% 	%Dw(""t$$$	% 	%r.   c                 0    | j         rt          |i | d S d S rO   )rt  r   )rT   rf   rg   s      r/   r   z_ModuleFrame.print  s/    < 	#4"6"""""	# 	#r.   c                    d}|t          | j                  k     r| j        |         }|j        dk    sJ |                                  |                     d||                                           |                     | j                   |j        dk    r0t          | j                  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 t          | j                           | j        k    rK|                                  |                     d
| j                   |                     | j                   |S |J t          | j        |          r|t          | j                           }|                     d|           t!          | j        | j        | j        | j        | | j        |gz   t)          |j        d                                                   t          | j                           | j                                      |          }|dz  }|| j        k    sJ |                     |           |dz  }|t          | j                  k     d S d S )Nr   rl   STEPr  r   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYc                     g | ]\  }}|S r-   r-   )r   pathtys      r/   r   z)_ModuleFrame.run_from.<locals>.<listcomp>%  s)     % % %%T2D% % %r.   	outliningzCreating new stack frame for)r`   rp   rq   r   r  rc  r  r   rz  r3  (torch._export.passes._node_metadata_hookr  r   r   rM   r/  ra  rb  rq  rr  r^   r   r   r  r  )rT   r  
module_idxrs   r  r  node_module_stacknext_modules           r/   r  z_ModuleFrame.run_from  s   
TZ((:h'D7m++++JJLLLJJvx)9)9););<<<JJt()))w(""t())Q.. $O %%'''49==!2B7788A=="#Td#T#TUUU"i(9:O     
 O$$))./AA %)$5!!% %)-3D)E)L)L)N)N% % %! !!93t'8#9#9!9:d>OOO %%'''

;111

4:&&&$000$+->??  0D4E0F0FG

9;GGG (OJO%%5#45::<<==c$BS>T>TU*	 	 (8$$  a
 %(99999NN4   MHU TZ((((((r.   rO   )r'   r(   r)   r=   rc   r   r   r=  r
   r   r	   r   r   r>   r?   rQ   r  r  r  r  r  r  r   r  r-   r.   r/   ra  ra    s        -1C9 C9HNC9 UX]C'(C9 3iC9  %8 89C9 )C9 C9 C9 C9J	7 	7 	7  & & &PPM PM PMd* * *
% % %*# # #M M M M Mr.   ra  
orig_graphroot_modulec                     i }i }t          | t          | j                  ||d dgdd |j        D             |	  	                                         d S )Nr   c                 6    i | ]}|j         	|j        |j         S r-   )r   r   r   s     r/   r   z'_outline_submodules.<locals>.<dictcomp>\  s6     	
 	
 	
	
Iu	
 	
 	
r.   )r   )ra  rb   rp   r   r  )r  r  rq  rr  s       r/   r   r   Q  su    +-J/1Lj	
	
 	
$6	
 	
 	

    ikkkkkr.   r   rk  r   rG   c                 d   |dk    rit          |                                          dd          D ]?}t          | |          -t          | |t          j                                                   @g }t          | j                                                  D ]P\  }}|||z   }t          |||dz              t          | |           |                    ||         ||f           Q|                    t          j        d                     |D ]\  }}}|                     ||           d S )Nr   r   r:   )rG   r   )rC  )r^   r   r[  r_  r=   r>   r?   _modulesr   r   delattrra   sortrl  
itemgetterregister_module)rk  r   rG   r   childrenr   childr   s           r/   r   r   e  sE    ||	(())!""- 	? 	?Cfc**2vsEHOO,=,=>>>HFO113344 7 7e=tmE9S3Y????3u56666MMh)!,,M---" , ,4tU++++, ,r.   r   r   c                 8   t          t                    | j                                        D ]l\  }t	          t          t          j        j                  |||gz             }|                                D ] \  }}|         	                    |           !mt          | d          sS | j        }t          t          d |j                            }|d         }	t          d |j                  }
|
D ]h}t          | |j                            d                    <t#                    v r+t%          t          fd|j                            |_        ii }|D ]Z}|j        |vrd}||j                 D ]6}|                    d          }|dt+          |                   |k    r|} n7|U|||<   [g }|                                D ]	\  }}t+          |j                  dk    r|t+          |          d         }t          | |          }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/          | t@                    r| !                                 t#          |           |iS )aT  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 containining 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.
    rM   c                     | j         dk    S )Nrl   r   r   s    r/   r  z_sink_params.<locals>.<lambda>  s    14=#8 r.   re  c                     | j         dk    S )Nry  r   r  s    r/   r  z_sink_params.<locals>.<lambda>  s    )> r.   r:   Nc                 <    | j         t                             vS rO   )r   r   )r   module_id_to_inputs_removed	submodules    r/   r  z_sink_params.<locals>.<lambda>  s    af,G9,VV r.   r   rS  T)propagate_meta)"r   r^   r  r   r   r   r=   r>   r?   extendr   rM   filterrp   _recursive_getattrr6   r;   r   rb   rf   r   r`   r  rA   rD   rF   r  r  r4  r!  
erase_nodera   r   rt   )r   r   r   r   submod_id_to_inputs_removedr  r  rM   r  the_last_inputcall_module_nodesrs   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrr  r  r  s                       @@r/   r   r   {  s   * 9DD8I8I "?0022 5 5i&2),,ouv~'
 '
# 05577 	5 	5DAq'*11!4444	5 67## +**LE&88%+FFGGFBZN >>LL! 
 
&vt{/@/@/E/EFF	  R	]]6Q%Q%QVVVVVI  DI @B 4 49O++
!$), 	 	Bxx}}H#e**%..%
 / )3 && !#N4::<< ) )jtz??Q"3u::<<0I+FI>>J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di((((&+,, vJJ''s   *JJ	J	c                 V    |D ]%}t          | |          s d S t          | |          } &| S rO   )r   r<   )objr  attrs      r/   r  r    s@     ! !sD!! 	44c4  Jr.   )TrO   )r   )Or   r  rl  collectionsr   
contextlibr   r   enumr   typingr   r   r	   r
   r   r   r   r   r=   torch.fx._pytreerc   _pytreerT  torch.utils._pytreeutilsr   "torch._library.fake_class_registryr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   torch.fx._symbolic_tracer   torch.fx.graph_moduler   r   r   _remove_effect_tokens_passr   __all__r#   r1   r3   rD   rF   r>   r?   r   boolrK   r   ABCr!   r   r    r   r   r/  r7  rL  rQ  r=  rW  rY  r[  r_  ra  r   r  r   r   r  r-   r.   r/   <module>r     s   



   # # # # # # % % % % % %             E E E E E E E E E E E E E E E E E E E E  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ? ? ? ? ? ? 3 3 3 3 3 3                3 2 2 2 2 2 1 1 1 1 1 1 7 7 7 7 7 7 7 7 = = = = = = U
T
T          ( ( (" !, !,EL%"445!,x!, !, 	!,
 !, !, !, !,HO
 O
 O
 O
 O
 O
 O
 O
d    cg    P
 P
 P
 P
 P
 P
 P
 P
h
 MQ8 88080I88 8 8 845)UX^ 5) 5) 5) 5) 5)pR R R
6# 6# 6# 6 6 6 6$6 6UX_ 6 6 6 60%(/ C    R%(/ R%(- R R R RMEHO MUX] M M M M% % % % % %") ) )UX_ ) ) ) )$D D D D D D D DNEHN AR    * GI, ,HO,(,S#X,@C, , , ,,f(HOf(#tCy.)f( 9f( f( f( f(R    r.   