
    #hJ                       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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l0m1Z1m2Z2m3Z3 d dlm4Z4m5Z5 ddl6m7Z7  ejp                  e9      Z:g dZ; G d de      Z<da=ed        Z>	 dTdeej~                  ej                  ej                  j                  f   dej                  j                  deCde<deDf
dZE G d dej                  j                        ZF G d dej                  j                        ZG G d d e j                        ZI G d! d"ej                  j                        ZJ	 dUd#e&d$eeI   d%eJfd&ZKd'ej4                  j                  d(e'd%dfd)ZMd* ZNd+eCd,eCd%eCfd-ZOd.ej                  j                  d/ej                  j                  fd0ZPd1ej                  j                  d%eCfd2ZQd1ej4                  j                  d%ej4                  j                  fd3ZTd1eej4                  j                  eFeJf   d%ej4                  j                  fd4ZUd1eej4                  j                  eFeJf   d%ej4                  j                  fd5ZVd6ej                  j                  deCfd7ZW	 dUd6ej                  j                  deCd8ej                  j                  d9eeeCgej                  j                  f      fd:ZXd;eCd<eYd%eCfd=ZZd>eCd;eCd%eDfd?Z[ G d@ dA      Z\e G dB dC             Z]dDej4                  j                  dEeJfdFZ^	 dVdGej                  j                  dHeeCeYf   dIeCfdJZ_ G dK dL      Z`d1ej4                  j                  dEeJdMeeCeeC   f   fdNZadO Zb	 dUd#ej                  j                  dPeeCeeC   f   dQeeC   dReeeYeeC   f      fdSZcy)W    N)defaultdict)contextmanager)deepcopy)	dataclass)Enum)	AnyCallablecastDictListOptionalSetTuple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y)	_AttrKind	parameterbufferconstantmoduleN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANTMODULE     S/var/www/html/sandstorm/venv/lib/python3.12/site-packages/torch/export/unflatten.pyr*   r*   1   s    IFHFr7   r*   Tc               #   8   K   t         } da 	 d  | a y # | a w xY ww)NF)RUN_WITH_INTERPRETER)old_flags    r8   _disable_interpreterr<   ;   s'      $H ('xs   	 from_obj	to_moduletarget	attr_kind
persistentc                   	 |j                  d      ^ }}|h}|D ]|  	t               }|D ]i  }t        |	      s)t        |	t        j
                  j                                |j                  	fd|j                  j                         D               k |}~ |D ]?  }|t        j                  k(  r9t        | t        j
                  j                        sJ |j                  ||        P|t        j                  k(  r1t        | t        j                         sJ |j#                  || |       |t        j$                  k(  rQt        | t&              rJ d       t        | t        j                   t        j(                  f      sJ t        |||        |t        j*                  k(  st        | t        j
                  j                        sJ t        |||        B y )N.c              3   @   K   | ]  \  }}t        |      r|  y wN)_is_call_name).0kt_callitems      r8   	<genexpr>z_assign_attr.<locals>.<genexpr>\   s'      Av D) s   )rA   z2FakeScriptObject should only exist during tracing.)splitsethasattrsetattrtorchnnModuleupdate_modulesitemsr*   r2   
isinstance	Parameterregister_parameterr3   Tensorregister_bufferr4   r   ScriptObjectr5   )
r=   r>   r?   r@   rA   prefixfield
to_modulestsrJ   s
            @r8   _assign_attrr`   H   s    \\#&NVU J 
#&5# 	I9d+	4):;II !*!3!3!9!9!; 	 

   0		+++h(:(:;;;((9)***h555%%eX*%M),,,!* DCD  LL&&   Iuh/)***h888Iuh/+0r7   c                        e Zd ZU dZeej                  j                     ed<   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.
    graph_modulegraphc                 h    t         |           || _        | | j                  _        t        | _        y rE   )super__init__rc   owning_moduler:   _run_with_interpreter)selfrc   	__class__s     r8   rf   zInterpreterModule.__init__   s+     	
#'

 %9"r7   c                 d   | j                   J d       t               sSt        j                  j	                         s| j
                  s) t        | j                         j                  | g|i S rt        |      }| j                  t        |      d  }|j                  fd|D               t        |      t              k(  sJ t        |      t        | j                        k(  sJ t        |      }t        j                  j                  | | j                        j                   |ddiS )Nz&Didn't finalize this InterpreterModulec              3   0   K   | ]  }|v r|     y wrE   r6   )rG   
kwarg_namekwargss     r8   rK   z,InterpreterModule.forward.<locals>.<genexpr>   s&       "!V+ :& s   rc   enable_io_processingF)rb   r   rP   compileris_dynamo_compilingrh   typeforwardlist	arg_nameslenextendtuplefxInterpreterrc   run)ri   argsrn   arg_listkwarg_namess     `  r8   rt   zInterpreterModule.forward   s     ,V.VV,NN..08R8R 34))*224I$I&II
  :"nnS]_=  &1   ;'3v;6668}DNN(;;;;X88''DJJ'?CC,1 r7   c                 T   t         j                  j                  | | j                        | j                  d<   | j                  j                          g | _        | j                  j                  D ]7  }|j                  dk(  s| j                  j                  |j                         9 y )Nrb   placeholder)rP   rz   GraphModulerc   __dict__lintrv   nodesopappendr?   ri   nodes     r8   finalizezInterpreterModule.finalize   sy     ).(<(<T4::(Nn%

 JJ$$ 	3Dww-'%%dkk2	3r7   c                 "    t        | d||||      S )Nr%   r   ri   print_outputinclude_strideinclude_devicecoloreds        r8   print_readablez InterpreterModule.print_readable   #     
 	
r7   TFFF)r/   r0   r1   __doc__r   rP   rz   r   __annotations__Graphrf   rt   r   r   __classcell__rj   s   @r8   r%   r%   {   sR    
 588//00:xx~~:!F3& 
r7   r%   c                   T     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| _        y Nr   )re   rf   rT   rO   getattr_call_modules
_num_calls)ri   r   r   accessorrj   s       r8   rf   z$InterpreterModuleDispatcher.__init__   s^    |$Q00 	HHD(GLOX$FG	H)r7   c                     | j                   | j                     }| j                  dz   t        | j                         z  | _        	  ||i |S # t        $ r	 d| _         w xY w)Nr"   r   )r   r   rw   	Exception)ri   r}   rn   call_modules       r8   rt   z#InterpreterModuleDispatcher.forward   sd    ((9??Q.#d6H6H2II	/// 	DO	s   A Ac                     | j                   S rE   )r   ri   s    r8   r   z(InterpreterModuleDispatcher.call_modules   s    !!!r7   c           	          | j                   D cg c]  }|j                  ||||       }}dj                  |      S c c}w )N
)r   r   join)ri   r   r   r   r   modoutputss          r8   r   z*InterpreterModuleDispatcher.print_readable   sV     ))
  	
 
 yy!!
s   =r   )r/   r0   r1   r   r   strr   r%   rf   rt   r   r   r   r   s   @r8   r&   r&      sB    c#h d;L6M "
 "r7   r&   c            
       z    e Zd ZdZej
                  dej                  dej                  dee	   dee	   fd       Z
y)r$   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    target_spec
input_spec
input_argsreturnc                      y)z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr6   )ri   r   r   r   s       r8   adaptzFlatArgsAdapter.adapt  s     	r7   N)r/   r0   r1   r   abcabstractmethodpytreeTreeSpecr   r   r   r6   r7   r8   r$   r$      sW     	__ OO I	
 
c r7   r$   c                   `     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d	Z xZS )r'   export_moduleflat_args_adapterc                   ) t         *|           |j                  j                  t	        d      |j
                  D cg c]  }|j                   }}|d   dk(  sJ t        |j                        }t        |j                        | _        t        j                  j                         | _        | | j                  _        t        |j
                        | _        || _        d| _        t        | _        t#        || j                         t%               | _        t)        ||       \  }}| j&                  j+                  |j-                         |        t/        |j0                  | |       |j2                  | _        g | _        |j6                  }t9               }	i }
| j                  j:                  D ]  }||   }t=        |      |
vrEt        j>                  jA                  |jC                         |jD                        |
t=        |      <   tG        |
t=        |         | |tH        jJ                         |	jM                  |        t9        | j                  jN                        }t9               }i }| j                  jP                  D ]  }||v rd}|jR                  |   }nd}||   }t=        |      |vr|jC                         |f|t=        |      <   tG        |t=        |         d   | |tH        jT                  |       |jM                  |        |jW                         D ]  \  }}||	v s||v rd}t=        |      |v s$tY        |t        j>                  j@                        sd}|rKt=        |      |vr|df|t=        |      <   tG        |t=        |         d   | |tH        jT                  d       t=        |      |
vr||
t=        |      <   tG        |
t=        |         | |tH        jJ                          i }|jR                  jW                         D ]u  \  }}t=        |      |vr8tY        |t        jZ                        r|jC                         }||t=        |      <   |t=        |         }tG        || |tH        j\                         w t_        t`              )t9               })fd	}t9               }| j                  jb                  D ]  }|jd                  tf        jJ                  k(  s)|jd                  tf        jT                  k(  r|jh                  rtk        |jl                  d
      sJ tY        |jn                  tp              sJ  |t=        |j6                  |jn                           |jl                  jr                  |jn                         |jM                  |jn                         |jM                  |jn                         |jd                  tf        jT                  k(  r|jh                  r<|jd                  tf        jt                  k(  s|jd                  tf        jv                  k(  sbtk        |jl                  d
      sJ tY        |jn                  tp              sJ  |t=        |jR                  |jn                           |jl                  jr                  |jn                         |jM                  |jn                          |jR                  jW                         D ]`  \  }}||vst=        |      )v sJ d       )t=        |         d   \  }} |t=        |      ||       |jM                  jn                         b |j6                  jW                         D ]?  \  }}||vst=        |      )vr)t=        |         d   \  }} |t=        |      ||       A i } )j-                         D ]#  }!|!D "cg c]  }"|"d   	 }#}"|!D ]
  \  }$}|#| |$<    % ty        | | g        t{        |j-                               }%|D cg c]	  }||%vs| }}| j}                  |%|       |D cg c]	  }d|vs| }}| j                  j~                  D &cg c]  }&|&j                  dk(  s|& c}&| _A        d| _B        t        |      D 'ci c]  \  }'}||'
 }(}'}| j                  d      D ]  \  }}||(vst        |(      |(|<    t        | |(       | j                  j                          y c c}w c c}"w c c}w c c}w c c}&w c c}}'w )Nz%Unflattening on JointExportModule NYIr    F)requires_grad)r@   T)r@   rA   c                 6    |    }|j                  ||f       y rE   )r   )obj_id	node_nametarget_name	name_list
consts_maps       r8   add_to_consts_mapz5UnflattenedModule.__init__.<locals>.add_to_consts_map  s     "6*Ii56r7   namez?Constants should be either aliased or appear in graph signaturer"   @r   remove_duplicate)Hre   rf   graph_signaturebackward_signature
ValueErrormodule_call_graphfqnr   rc   rP   rz   r   rg   r   adaptedr:   rh   _inplace_buffer_mutations_IValsivals_outline_submodulescreatevalues_copy_graph_attrs_graph_modulerange_constraintsequality_constraints
state_dictrM   
parametersidrQ   rW   cloner   r`   r*   r2   addnon_persistent_buffersbuffers	constantsr3   rU   rV   rY   r4   r   ru   input_specskindr   rA   rN   argr?   r   r   CONSTANT_TENSOR
CUSTOM_OBJ_sink_params_deduplicate_modules_dispatch_modulesr   r   input_placeholderscheck_input_constraints	enumeratenamed_modulesrw   _reorder_submodulesr   )+ri   r   r   entryfqn_listexport_graphseen_modules
seen_attrsr   assigned_paramsid_to_paramr   paramr   assigned_buffersid_to_bufferrA   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ttargetsnredirected_call_indicesr   i	fqn_orderr   rj   s+                                            @r8   rf   zUnflattenedModule.__init__  s1   
 	((;;GDEE+8+J+JK%EIIKK{b    3 34'(E(EFXX^^%
#'

 !)-*I*I!J!2%9"!,0D0DEX
 $7|T#J j 	

,--/6 	-55tZH!.!@!@*,! #--
$'E57((33 	&Dt$E%y+).););KKM1D1D *< *BuI& BuI&#--	 %	& "%T%9%9%P%P!Q%(UCE((00 	'D--"
&006!
#D)&z-,2LLNJ+GRZ(RZ(+#**%   &%	', ',,. !	LD&&$2B*BI&z\)**2 !	vJl2 0LF,  F,Q/'..# f:[0.4K6
+6
+'11	9!	H NP*44::< 	MC(|;.h5'~~/H,4BxL)#BxL1I#,,		 8C47H
#&5	7 *-%%11 	-Avv,,,)***q||quuf---!!((C000!}//9:AEEJJ ""188,$((29+++ALL66Y66666Y111quuf---!!((C000!}..qxx89155::qxx ""188,+	-0 "/!8!8!>!>!@ 	3J/uI+UTU+'5	215
!"U)WjA$((2	3 )3399; 	<KC..f:Z/ '6
3A6
!"V*gs;	< 13%,,. 	-K%01qt1G1# -1%,"-	-
 	T?B/"6|7J7J7L"M#+RCs:Q/QCRR6G#+>Cs#~C>> "ZZ--#
M1ID#
 (,$*3H*=>3S!V>	>))5)A 	1GD!9$"%i.	$	1 	D),

o Lz 2 S ?
#

 ?s6    c*9c/	c4c4(	c92c9c>%c>dc                     | j                         D ]d  \  }}t        |dz          t        |d      s!t        |j                  t
        j                  j                        sPt        |j                         f y )N:rc   )r   printrN   rV   rc   rP   rz   r   )ri   r   r   s      r8   _print_graphzUnflattenedModule._print_graph  sU    **, 	!HC#)sG$CIIuxx~~)Ncii 	!r7   c                 x   | j                   d   j                  }||j                  k(  r|S | j                  t	        d      | j                  j                  |j                  ||      }t        |      |j                  j                  k7  r.t	        dt        |       d|j                  j                         |S )Nr   zeThere is no flat args adapter sepcified. Are you sure you are calling this with the right arguments? )r   r   r   z<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )r   	signaturein_specr   	TypeErrorr   rw   
num_leaves)ri   	flat_argsr  r  s       r8   _adapt_flat_argsz"UnflattenedModule._adapt_flat_args  s    **1-77	i'''!!)O 
 ..44%--"$ 5 I 9~!2!2!=!==  #I/ 0((1(9(9(D(D'EG 
 r7   c                 p   | j                   d   j                  }t        ||j                        }t	        j
                  ||f      \  }}|D cg c]  }|d   	 }}t               r|S ||j                  k7  rM| j                  s(t        d| dd|j                          t        d       | j                  ||      }d| _        | j                  r\ddlm}	 | j                  du r(|D 
cg c]  }
t        d	      t        d
      f|
f }}
n|} |	| j                  || j                          |S c c}w c c}
w )Nr   r"   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_graph)idxz<unknown location>)r   )r   r  r   r  r   tree_flatten_with_pathr   r   r  r  r   torch._export.utilsr  r!   r    r   r   )ri   r}   rn   r  reordered_kwargsflat_args_with_pathr  xr  r  r   new_flat_args_with_paths               r8   process_forward_inputsz(UnflattenedModule.process_forward_inputs  sY   **1-77	)&)2C2CD'-'D'D#$(
$W $77aQqT7	7?i'''<<''.ir301B1B0CD
 MN--iAIDL'' O||t#  )+ "a(*:N*OPRUV+' +
 +>'.'')@$BXBX I 82+s   D.(!D3c                    t        j                  j                  | j                        |i |}| j                  d   j
                  }t               r_t        j                  j                  | | j                        j                  |ddi}t        |t              rt        |      dk(  r|d   S |S t         j                  j                         r9| j                   s-t        j                  j#                  | | j                        | }n:t        j                  j                  | | j                        j                  |ddi}t%        j&                  ||j(                        S )Nr   ro   rp   Fr"   )rP   _dynamodisabler   r   r  r   rz   r{   rc   r|   rV   ry   rw   rq   rr   rh   r   r   tree_unflattenout_spec)ri   r}   rn   r  r  
return_valtree_outs          r8   rt   zUnflattenedModule.forward@  s!   MM))$*E*EFWPVW	**1-77	?--d$**-EII16J *e,ZA1E!!}$>>--/8R8Rxx++D$**=yIHxx++D

+CGG16H $$Xy/A/ABBr7   c                 ~   t        t              | j                  D ]  }|j                  s|j                  s|j                  }t        | |j                  ||            }d|v r|j                  d      n|dg\  }}|   j                  t        |      |f        t        t              }|D ]D  }	d|	v r*|	j                  dd      \  }
}||
   j                  |       1|d   j                  |	       F j                         D ]  \  }
}t        |      D cg c]  \  }}|	 }}}t        |      dkD  s2t!        |      D ]R  \  }}t#        |
|dz         }||vs|j                  d      ^ }}t%        | |      j&                  j)                  |       T | j+                  |
t-        ||
   |              fd}| j/                  d      D ]d  \  }}t1        |d	      r |||j2                         &t1        |d
      s3|j4                  D ]#  }t1        |d	      sJ  |||j2                         % f yc c}}w )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   0rC   r"   r   c                     |j                   D ]G  }|j                  dk(  s|j                  j                  d      d   }| r|  d| n|}|v sA||_        I y )Nr   r   r   rC   )r   r   r?   rL   )r\   rc   r   r   pathcalled_moduless        r8   elide_call_indicesz?UnflattenedModule._dispatch_modules.<locals>.elide_call_indices  s^     *77m+++++C03C06fXQse,CD~-&)*r7   Fr   rc   r   N)r   ru   r   r   r  r   getrL   r   intrM   rsplitr   rU   sortedrw   r   
_call_namer   rT   popset_submoduler&   r   rN   rc   r   )ri   r	  r   r   r   r   baser  	attrs_mapr?   orig_fqnr   indexed_call_modulesr  r   r
  r   r\   r-  mod_r,  s                       @r8   r   z#UnflattenedModule._dispatch_modulesU  s7    %T*++ 	=EyyU__ ii&=&A&A#s&KL.1SjCIIcNsCj	ct$++SXsO<	=  $	$ 	*Ff}!'sA!6$(#''-"!!&)	* /=.B.B.D 	*H*.45I.JKFAsCKLK< 1$&/&= QNA{$Xq1u5C"99(+		#/f=FFJJ4P	Q
 ""/	(0C\R		* **E*B 	8HCsG$"3		2o.-- 8D"4111&sDJJ78		8- Ls   H9c                 "    t        | d||||      S )Nr'   r   r   s        r8   r   z UnflattenedModule.print_readable  r   r7   rE   r   )r/   r0   r1   r   r   r$   rf   r  r  r   rt   r   r   r   r   s   @r8   r'   r'     sW     8<`&` $O4`D!2,\C*88x 
r7   r'   r.   r   r   c                 0    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     r8   r(   r(     s    , #6*FV%677r7   rc   r   c                    t        t        t        | j                                    }|j                  dk(  rt        |j                        dk(  sJ |j                  d   }|j                  }|dt        |       }|j                  j                         D ci c]  \  }}||
 }}}| j                  D 	ci c]  }	|	j                  dk(  s|	j                  |	! }
}	|D ]  }||j                     }||   }|
|   }| j                  |      5  | j                  dt        j                  j                  j                   ||f      |j"                  j                         D ]  \  }}|j"                  |<    	 ddd       |j%                  fd        t'        |t        |      d       }|f|_        yc c}}w c c}	w # 1 sw Y   KxY w)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   Nr   call_functionc                     | uS rE   r6   )r  new_nodes    r8   <lambda>z+_inplace_buffer_mutations.<locals>.<lambda>  s    1H;L r7   )nextiterreversedr   r   rw   r}   buffers_to_mutateinputs_to_buffersrU   r   inserting_aftercreate_noderP   opsatencopy_metareplace_all_uses_withry   )rc   r   output_nodereturn_argsmutation_node_to_buffer	mutationsrH   vbuffers_to_inputsr   input_name_to_nodemutationbuffer_name
input_name
input_nodeuser_outputsr@  s                   @r8   r   r     s   2 tHU[[123K>>X%#k.>.>*?1*DDD""1%K-??:c"9:;I*9*K*K*Q*Q*ST$!QATT$)KK 477m3K		4   N-hmm<&{3
'
3
""8, 	%((!5!5
H7MH !++- %1#$a %		% 	&&x1LMN" C/023L &(K3 U	% 	%s   F46F:F:A)F??G	c                 V    t        |       t        |      k  xr |dt        |        | k(  S )z2Check whether `candidate` is a prefix of `target`.N)rw   )	candidater?   s     r8   
_is_prefixr\    s+    y>CK'QF3CS^,D	,QQr7   
parent_fqn	child_fqnc                     | dk(  r|S | j                  d      }|j                  d      }|d t        |       |k7  rt        d| d|  d      dj                  |t        |      d        S )Nr   rC   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.)rL   rw   RuntimeErrorr   )r]  r^  parent_splitchild_splits       r8   _compute_accessorrc    s    R##C(L//#&K &S&'<7YK'OPZ| \P P
 	

 88KL 1 3455r7   r  yc                 T   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 )Nrc   r   c                 T  
 g }i 
dt         f
fd}t        | j                        D ]  \  }}t        j                  ||j
                        D cg c]  }t        |       }}|t        j                  ||j                        j                         D cg c]  \  }}| d|  c}}z  }|j                  dv r|j                  nd}	|j                  | d|j                   d|	 dd	j                  |       d
       |
t        |      <    dj                  |      S c c}w c c}}w )Nr   c                     t        | t        j                  j                        rdt	        t        |                z   S t	        |       S )N%)rV   rP   rz   Noder   r   )r   	nodes_idxs    r8   arg_dumpz>_check_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump  s7    #uxx}}-S2c7!3444s8Or7   =)r>  get_attrr   z: [z](z, )r   )r   r   r   r   tree_mapr}   rn   rU   r   r?   r   r   r   )rc   retrk  r
  r   r   	args_dumpkeyvaluer?   rj  s             @r8   
graph_dumpz,_check_graph_equivalence.<locals>.graph_dump  s   $&		S 	
 !- 	$GAt-3__Xtyy-QRcSRIR"(//(DKK"H"N"N"PC %q  I %)GG/L$LT[[RTFJJ!Btwwiq499Y3G2HJK"#Ibh	$ yy~ Ss   DD$
)rP   rz   r   r   rV   rc   )r  rd  ru  s      r8   _check_graph_equivalencerv    st    %((.. S ( agguxx~~...agguxx~~...agg*QWW"555r7   gmc                 v    d}t        | d|       r|dz  }t        | d|       rd| }t        | ||       |S )Nr   _spec_r"   )rN   rO   )rw  specr
  r   s       r8   	_add_specr{  &  sN    	A
"qcl
#	Q "qcl
#A3<DBdKr7   c                     | j                   j                  t        j                  |f      }| j                   j                  t        j
                  |df      }|S r   )rc   r>  r   tree_flattenoperatorgetitem)rw  r   flatten	getitem_0s       r8   _generate_flattenr  /  sF    hh$$V%8%84'BG&&x'7'7'1FIr7   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rE   )r{  rc   rm  r>  	fx_pytreetree_flatten_spec)rw  r   rz  r   	spec_nodes        r8   _generate_flatten_specr  5  sF     RD!!$'I88!!)"="=i?PQQr7   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S rE   )r{  rc   rm  r>  r   r$  )rw  r   rz  r   r  s        r8   _generate_unflattenr  =  sF     RD!!$'I88!!&"7"7%9KLLr7   r   c                     |j                  d      ^ }}|D ];  }t        | |d       }| y t        |t        j                  j
                        s y |} = t        | |d       S NrC   )rL   r   rV   rP   rQ   rR   )r   r?   r\   r]   rJ   submods         r8   _get_submoduler  E  sd    \\#&NVU 	dD)>&%((//2	 3t$$r7   module_to_addcreate_modulec           	      z   |j                  d      ^ }}t        |      D ]  \  }}t        | |d       }|K| |dj                  |d |dz                }nt        j
                  j                         }t        | ||       t        |t        j
                  j                        s y|}  | j                  ||       y )NrC   r"   F)
rL   r   r   r   rP   rQ   rR   rO   rV   
add_module)	r   r?   r  r  r\   r]   r
  rJ   r  s	            r8   _add_submoduler  V  s     \\#&NVUV$ 4dD)>(&sxxwQ'@A*Cv&&%((//2 NN5-(r7   r5  r  c                 $    |dk(  r| S |  d|dz
   S )Nr"   r   r6   )r5  r  s     r8   r2  r2  p  s#     64.$q1.r7   	call_namec                 ^    t        j                  t        j                  |      dz   |       d uS )Nz(@\d+)?$)rematchescape)r  r5  s     r8   rF   rF   v  s%    88BIIdOk19=TIIr7   c                      e Zd Z	 ddej                  j
                  deej                  j                  df   d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y)_ModuleFrameN
flat_graphr   .module_stackr   r.   c                     | _         | _        | _        | _        | _        | _         _        | _        |	 _        |
 _	        d _
         j                  d   \   _        }t         j                  |dz          _          |'| _        t        |d      r|j                   ni  _        na j
                  j#                   j                  t%        t&        j(                  j+                                      _        j                    _         j                  j,                   _        i  _        i  _        d  _        Bt5        j                   j                        } fd}t7        j                  | j                  |       j,                  j9                  |       _         j                   j                     rA j                   j                     d   }|j                  j:                   j                  _         j                   j                     j=                  t?         j                  j                   j                  j                   j2                   j                  |dz    j                               |
j#                   j                        }| j                  |j@                  jB                  dk(  sJ |j@                  jD                  d   }|j@                  jD                  d   }|jF                  J |jF                  J  j,                  jI                  d       5  tK        |jB                        D cg c]   } j,                  jM                  d	|       " }}i }|jF                  D ]   } j,                  jM                  |      ||<   " tO         j                  tQ        |      |f|j@                        }tS        |jT                        D ]  \  }} j,                  jW                  d
tX        jZ                  ||ft]        |t^              s|j`                  nd|       }t]        |t^              re|j`                   j                  v s~tc        jb                   j                  |j`                     jd                        |_2        | j0                   j                  |j`                     <    	 d d d         j                  j,                  jg                   j2                        5  g }|jT                  D ]  }t]        |t^              r|j=                  |jh                         /|j`                   j                  vr|j=                  d        Yt]        |tj        tl        tn        tp        f      sJ |j=                   j                  js                   j                  |j`                                   tu         j                  j                  ||j@                        } j                  j,                  jw                  tX        jZ                  |df      } j                  j,                  jw                  tX        jZ                  |df      }tK        |jB                        D cg c]8  } j                  j,                  jw                  tX        jZ                  ||f      : }}|jF                  D  ci c]9  } |  j                  j,                  jw                  tX        jZ                  || f      ; }} d d d         j2                  J tQ               j2                  _<         j2                  _=        y y y c c}w # 1 sw Y   xY wc c}w c c} w # 1 sw Y   axY w)NFr"   r   c                     j                   rj                    d|  n| }|j                  v rj                  |   S t        t        j                  j                               }|j                  |<   |S r  )r   created_modulesr%   rP   rz   r   )r   r+  r  parentri   s      r8   r  z,_ModuleFrame.__init__.<locals>.create_module  sh    06

&**Qse,4/////55*588>>+;<-3$$T*r7   r   )r]  parent_moduleparent_call_moduler   call_idxr.      _positional_arg_r>  
_constant_)r   r?   r}   r   )>r  r   
seen_nodesr   r   r  r  r  	module_idr   verboser   r2  r^  r.   rN   r   r.  r%   rP   rz   r   rc   node_mapnode_to_placeholderr  rc  r  r   rT   r   _SubmoduleEntryr  num_childrenchildren_specscontextrG  ranger   r  ry   r   inputsrH  r~  r  rV   r   r   copyrL  inserting_beforert  r   r   r   r   remap_inputr  r>  r}   rn   )!ri   r  r   r  r   r   r  r  r  r  r   r.   	num_callsr   r  base_module_framer  	args_speckwargs_specr  	arg_nodeskwarg_nodesr   r  r   flat_arg_nodeinput_nodesinputinputs_node	args_nodekwargs_noder
  rH   s!   `      `                         r8   rf   z_ModuleFrame.__init__|  sv    %
$($.("!2"//3)#DHHi!m< DK)0)ArDJ..22!%((.."23DK  DJ[[&&
 =?#% ;?(T^^DH 6==(DKKO&,ll&>&>x&HD#  0$($5$5dnn$Ea$H!'8'?'?'H'H$dnn-44#{{"&++"4"4'+'>'>&];;	 &))$..9	 T[[%<$$11Q666!))88;I#++::1=K$$,,,&&222++D1 *  %Y%;%;< JJ**-=cU+CD	  !'// ED(,

(>(>t(DK%E2KK9%{3%%	
 !*)*:*: ; *HC$(JJ$:$:*'//'- $.c3C#D  HH#-cU!3 %; 	%M "#'78 xx4??2-1YYtsxx7P7U7U-V* * 00 OOCHH5!**B ""33D4K4KL *=?&-- E!%)9:#**5;;74??:#**40)! . . / 0	     $** KK33DOOEJJ4OP& 2KK&&%% !KK--;;$${A&6	 #kk//==$${A&6
 #9#9#9: KK%%33H4D4DyRSnU	  )00	  t{{((66 ((;*:  K*V **666+0+;D##(-8D##*k &= * *DK* *sQ   5]%]2C1]$A#]?E;]':=]7]'>]"]']]
]'']0c                    | j                   dk7  sJ d| d       |j                  | j                  u sJ | j                  j                  d       5  | j                  j	                  |j
                  |j                        }d d d        t        j                  |j                        _        || j                  |<   y # 1 sw Y   =xY w)Nr   zCannot add placeholder z to root module	type_expr)
r   rc   r  r  r   r   rs   r  rL  r  )ri   r  placeholder_nodes      r8   add_placeholderz_ModuleFrame.add_placeholder   s    xx2~K!8?KK~ww$//)))ZZ((. 	P#zz55aff5O	P !%		!&& 1&6  #	P 	Ps   2B>>Cc                    t        j                  t        j                  j                  | j
                  |j                        }t        j                  t        j                  j                  | j
                  |j                        }| j                  j                  |j                  ||      }t        j                  |j                        |_        || j                  |<   |S rE   )r   tree_map_onlyrP   rz   ri  r  r}   rn   rc   r>  r?   r  rL  r  )ri   r  r}   rn   r   s        r8   copy_sym_call_functionz#_ModuleFrame.copy_sym_call_function+  s     ##EHHMM43C3CQVVL%%ehhmmT5E5EqxxPzz''$?IIaff%	ar7   c                 `   |j                   | j                  u sJ || j                  v r| j                  |   S | j                  d| d       || j                  v r| j                  |   S |j
                  dk(  s%| j                  j                  | j                        | j                  |       | j                  m| j                  j                   j                  | j                        5  | j                  j                  d| j                  j                  |             d d d        | j                  |   S |j
                  dk(  rk|j                  t         j"                  j$                  j&                  j(                  t         j"                  j$                  j*                  j,                  t         j"                  j$                  j.                  j(                  t         j"                  j$                  j0                  j2                  t         j"                  j$                  j4                  j,                  t         j"                  j$                  j6                  j,                  fv s/t9        |j                  d      r9|j                  j:                  dk(  r | j=                  |       | j                  |   S | j                  j                  | j                        1| j>                  jA                  | j                  | j                   |      S tC        d|j
                   d	|       # 1 sw Y   xY w)
Nzremap_input(ro  r   r   r>  r0   	_operatorz(Could not run remap_input() on op type: z
 for node )"rc   r  r  r  r  r   r   r.  r   r  r  r  r  
insert_argr  r?   rP   rI  rJ  sym_sizer/  rJ   defaultunbindsumdim_IntListviewdiffrN   r0   r  r   readr`  )ri   r  s     r8   r  z_ModuleFrame.remap_input9  s^   ww$//)))==##

\!A&'(((++A..DDM!%%))$((3;   #&&2 [[&&778O8OP V++66q$++:Q:QRS:TUV++A..TT_$HH		''++		##++		%%))		""..		##++		##++ ,/AHH4G4G;4V ''*==####''1= ::??488TZZ;;:144&
1#N 1V Vs   *6L##L-c                      j                   j                   j                  d        g } j                  j	                   j
                        }| j                  |j                  D ]  }t        |t        t        t        t        f      rS|j                   j                  v r)|j                   j                  |j                            d|j                  d        vt!        d|         fdt#         j$                  t'        fd|D              |j(                        }t+         j                  j$                   j,                  |j(                        }|}ng } j.                  j1                         D ]\  }|j2                  D ]K  }|j                   j                  vs|j                  |       |j                   j.                  |           \ ^  j,                  }t5        |      dk(  r|d   }t        |t6        t8        j:                  j<                  f      sJ  j>                  jA                  |       |y t        |t8        j:                  j<                        r|jB                  j	                  d      n(|D 	cg c]  }	|	jB                  j	                  d       c}	|jB                  d<   t5        |      dk(  r|| j                  j.                  |d   <   y tE        |      D ]u  \  }
}|	t8        j:                  jG                  |      |
   jH                  }|jB                  j	                  d      |jB                  d<   | j                  j.                  |<   w y c c}	w )Nz'Unsupported data type for output node: c                     | y 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`  rc   )r=  	seen_noderi   s     r8   get_actual_output_nodez=_ModuleFrame.finalize_outputs.<locals>.get_actual_output_nodez  sx    > OOFKK8	-==33$":"::33I>>&5fXYtzzlS r7   c              3   .   K   | ]  } |        y wrE   r6   )rG   r=  r  s     r8   rK   z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>  s     P,V4Ps   r"   r   val)%r  r3  r   r   r.  r^  r  r   rV   r   r   r   r   r   r  r   r`  r  r.   ry   r%  r  r  r  keysusersrw   ru   rP   rz   ri  rc   r=  rL  r   Proxyr   )ri   orig_outputsr  r=  tree_out_node
parent_outgraph_outputs	orig_node	user_nodeor
  orig_output	proxy_outr  s   `            @r8   finalize_outputsz_ModuleFrame.finalize_outputse  s     40**..t~~>	 T[[%<#++ #^_FVW {{doo5$++DOOFKK,HI$++D1&A&J  0P<PP""M
 3I""D$;$;Y=O=O3J HUMM!]]//1 	!* I ~~T__<$++I6%,,T]]9-EF 00J=!Q& -a 0-$)>???

-(  -7 ""5)-:;!&&**U#; 	 |!i&74>DKK  a1"+L"9 >;&!HHNN:6q9>>	(3(8(8(<(<U(C	u%4=$$[1> <s   "M+c                     | j                  d|j                                | j                  j                  || j                        | j
                  |<   || j                  |j                  <   y )Ncopying)r  format_noderc   	node_copyr  r  r  r   r   s     r8   	copy_nodez_ModuleFrame.copy_node  sP    

9d..01"jj2249I9IJd%)		"r7   c                     d}| j                   j                  D ]A  }| j                  ||j                  j	                  d      |j                                |dz  }C d}| j                  |   }|j                  dk(  r5| j                  |       |dz  }| j                  |   }|j                  dk(  r5| j                  |       | j                   j                  D ]#  }|j                  dk(  s| j                  |       % y )Nr   nn_module_stackr"   r   r=  )	r  r   r  rL  r.  r  r   r  run_from)ri   r
  r   node_idxs       r8   	run_outerz_ModuleFrame.run_outer  s    OO)) 	DJJq$))--(9:D<L<L<NOFA	
 zz(#gg&NN4 MH::h'D gg&
 	h OO)) 	%Dww("t$	%r7   c                 4    | j                   rt        |i | y y rE   )r  r  )ri   r}   rn   s      r8   r  z_ModuleFrame.print  s    <<4"6" r7   c                    d}|t        | j                        k  r2| j                  |   }|j                  dk7  sJ | j                          | j                  d||j	                                | j                  | j
                         t        | j
                        }|j                  dk(  r|dk(  r|S | j                          |S t        |j                  j                  di             dk(  rt        d|       |j                  d   }ddl
m} t        |      dk(  r||v r| j
                  }nW|j                  d   j                         D 	
cg c]-  \  }\  }	}
|	d	|v rt        |j                  d	      d
         ndf/ }}	}}
|d | | j
                  k7  rI| j                          | j                  d| j                         | j                  | j                          |S |J t#        | j
                  |      r||   }| j                  d|       t%        |j                  d   j'                               |   }t)        | j*                  | j                  | j,                  | j.                  | j0                  | j2                  | | j
                  |gz   |j                  d	      d   | j4                  
      j7                  |      }|dz  }|| j
                  k(  sJ |j                  dk(  r2| j0                  | j8                     j;                  |j<                         | j?                  |       |dz  }|t        | j                        k  r1y y c c}
}	}w )Nr   r   STEPr=  r"   r  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYr   r  	outliningzCreating new stack frame forrm  ) rw   r   r   r  r  r  r  rL  r.  r`  (torch._export.passes._node_metadata_hookr  rU   r/  rL   r   rc   r\  ru   r  r  r  r  r   r   r  r   r  r^  r   r?   r  )ri   r  
module_idxr   depthr  r  node_module_stackrH   r+  tynext_modulenext_module_keys                r8   r  z_ModuleFrame.run_from  s   
TZZ(::h'D77m+++JJLJJvx)9)9);<JJt(()))*Eww("A: $O %%'499==!2B78A="%MdV#TUU"ii(9:O
 O$)./A %)$5$5! *.3D)E)K)K)M% %%:D" C1H3qwws|B/0!D%! %
 !%(D,=,== %%'

;1

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

9;G #'tyy1B'C'H'H'J"KE"R'OOJJOO%%OO((%%5#))#.q1** (8$  a
 %(9(9999ww*$/33DKK@NN4 MHi TZZ(D%s   2MrE   )r/   r0   r1   rP   rz   r   r   ri  r   r   r/  r   r   r   r   r   r'   rf   r  r  r  r  r  r  r  r  r6   r7   r8   r  r  {  s     LPb9HHNNb9 UXX]]C'(b9 5c?+b9  %8 89b9 uxx335FFGHb9H	7*XR>h*
%*#Wr7   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<   y)r  r]  r  r  r   r  r.   N)r/   r0   r1   r   r   rP   rQ   rR   rz   ri  r/  r6   r7   r8   r  r  1  s;    O88??"%	HMHHOOr7   r  
orig_graphroot_modulec                 :   i }t        t              }t        t              }i }t        | t	        | j
                        ||||d dgd|j                  D ci c]%  }|j                  r|j                  |j                  ' c}|      j                          ||fS c c}w )N)r   r   r   )r.   )
r   ru   rM   r  ry   r   r   r  r   r  )r  r   r  r   r   r  r   s          r8   r   r   ;  s    +-J5@5FL&1#&6J24Oj		
 %66	
 IIu&	

   ik##	
s   *Br  r  r\   c                    |dk(  rWt        |j                               dd  D ]8  }t        | |      t        | |t        j
                  j                                : g }t        | j                  j                               D ]A  \  }}|	||z   }t        |||dz          t        | |       |j                  ||   ||f       C |j                  t        j                  d             |D ]  \  }}}| j                  ||        y )Nr   r"   rC   )r\   r   )rs  )ru   r  r  r  rP   rQ   rR   rT   rU   r   delattrr   sortr~  
itemgetterregister_module)r  r  r\   r   childrenr   childr  s           r8   r   r   T  s     |	()!"- 	?Cfc*2vsEHHOO,=>	? HFOO1134 7e=tmE9S3Y?3u567 MMh))!,M-" ,4tU+,r7   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   av  
    Collect the intermediate values of buffer mutations in a graph,
    along with the module call fqns that create and use them. Later,
    in each fqn associated with an intermediate value we will install
    a corresponding attribute, so that it can be updated and read.

    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.

    Thus our analysis will produce {ival1: {n0, n1}, ival2: {n1, n2}}.
    c                 :    t        t              | _        i | _        y rE   )r   rM   fqnsstorager   s    r8   rf   z_IVals.__init__  s    $	r7   c                    |j                  d      5  |j                  d|j                  z   |j                        }t	        j                  |j
                        |_        ddd       |j                  | j                  vrQ|j
                  d   }t        j                  |j                  |j                        | j                  |j                  <   | j                  |j                     j                  |       S # 1 sw Y   xY w)zM
        Read attribute corresponding to a given intermediate value.
        N__ival__r  r  )dtype)r  rm  r   rs   r  rL  r  rP   emptyshaper  r  r   )ri   r   rc   r   	ival_nodefakes         r8   r  z_IVals.read  s    
 ##D) 	2zDII'=SI!YYtyy1IN	2 99DLL( 99U#D&+kk$**DJJ&ODLL#		$))  %	2 	2s   AC<<Dc                 >   | j                   |j                     j                  |       |j                  |      5  |j	                  d|j                  z   |j
                        }t        j                  |j                        |_        ddd       |j                        5  |j                  dt        j                  j                  j                  ||f      }t        j                  |j                        |_        ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zO
        Update attribute corresponding to a given intermediate value.
        r  r  Nr>  )r  r   r   rG  rm  rs   r  rL  rH  rP   rI  rJ  rK  )ri   r   rc   r   r  new_ival_nodes         r8   rS   z_IVals.update  s     			$))  % ""4( 	2zDII'=SI!YYtyy1IN	2 ""9- 	6!--!5!5	47HM "&499!5M		6 	6	2 	2	6 	6s   AD"ADDDc                 0   d|fg}|D ]  }|D ]  }|j                  |j                  |j                  f       |j                  j                  }|j                  D ]8  }|j
                  | j                  v s| j                  |j                  ||       :   t        t              }| j                  j                         D ]   \  }	}
|
D ]  }||   j                  |	        " |D ].  \  }}||   D ]!  }	d|	 }t        ||| j                  |	          # 0 y)z
        Update attributes corresponding to intermediate values that were read.
        Finally, initialize attributes in all modules that read or update
        corresponding intermediate values.
        r   r  N)r   r   r.   rc   r   r   r  rS   r   ru   r  rU   rO   )ri   
partitionsr   entriesshared_submodulesr   rc   r   r   r   r  r   r   	ival_names                 r8   r   z_IVals.create  s%    $%!+ 	<* <		5<<89**!KK <DyyDLL0EIIud;<<	< D!))//+ 	(JD$ (c
!!$'(	(   	<HCc
 <&tf-	 YT(:;	<	<r7   N)r/   r0   r1   r   rf   r  rS   r   r6   r7   r8   r   r   j  s    2$6 <r7   r   r   c                     |j                         D ]5  \  }}|rt        ||      n|}|D ]  }t        | |      }t        |||        7 y rE   )rU   r   r   rO   )rw  r   r   r^  namesr.   r   r  s           r8   r   r     sX    
 ',,. '	56?;	2[ 	'D"d#CFD#&	''r7   c                    i }| D ]I  }t        |      D ]7  \  }}t        |j                  |j                        }t	        |j
                  |      }d}|d | D ]  }t        |j                  |j                        s$|j                  }	|j                  |j                  k(  r|j                  d      ^ }
}t        |	|
      j                  j                  |       t        |j                  |j                        }t	        |j
                  |      }||j                  _        |||<    |r|	j                  ||j                         d} : L |S )NFrC   T)r   r2  r   r  rc  r]  rv  r.   r  rL   r   rT   r3  r  r?   r4  )r  r	  r  r
  r   r^  r?   deduplicatedseenr  r\   r   seen_child_fqnseen_targets                 r8   r   r     s?    ' $,!"34 #	,HAu"599enn=I&u'7'7CF L)"1- ,+DKKF"00F xx599,
 )/S(9/?HHLLTR)3DHHdmm)L&7!,,n' ;F007=K/	:) ,,VT[[A'+=,#	,$,L #"r7   r  scopemodule_id_to_inputs_removedc           	         t        t              t        |       v rt        |       t        |          iS | j                  j	                         D ]e  \  }t        t        t        j                  j                        |||gz         }|j	                         D ]  \  }}|   j                  |        g t        | dd      }|t        |j                        dk(  rS t        |t        j                  j                         sJ t#        t%        d |j                              }	|	d   }
t%        d |j                        }|D ]Q  }t'        | |j(                        t              v s*t+        t%        fd|j,                              |_        S i }|	D ]u  }|j.                  |vrd}||j.                     D ]H  }|j1                  d      }|dt        |       |D cg c]  }|j1                  d	      d    c}k(  sF|} n |q|||<   w t               }|j	                         D ]  \  }}t        |j2                        dkD  r|t        |      d }t5        | |      }t        |t        j6                  t        j8                  f      sJ |j;                  |
      5  |j=                  d
dj?                  |            }ddd       |jA                  d       |jC                  |       |jE                  |j.                          t        | tF              r| jI                          t        |       |iS c c}w # 1 sw Y   |xY w)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.
    Nrc   r   c                      | j                   dk(  S )Nr   r   r  s    r8   rA  z_sink_params.<locals>.<lambda>0  s    144=#8 r7   r  c                      | j                   dk(  S )Nr   r&  r'  s    r8   rA  z_sink_params.<locals>.<lambda>4  s    )> r7   c                 8    | j                   t                 vS rE   )r   r   )r  r#  	submodules    r8   rA  z_sink_params.<locals>.<lambda><  s    aff,G9,VV r7   rC   r   rm  T)propagate_meta)%r   rM   r   rT   rU   r   r
   rP   rQ   rR   rS   r   rw   r   rV   rz   r   ru   filterr   r?   ry   r}   r   rL   r  r   rY   r[   rG  rH  r   rM  
erase_noder   r%   r   )r.   r  r"  r#  r   submod_id_to_inputs_removedrH   rR  rc   r  the_last_inputcall_module_nodesr   inputs_to_state_of_scope
state_namesnsn_splitr  inputs_removed	attr_path
state_attrr@  r*  s      `                  @r8   r   r     s3   , #*&1#&6#	&z006
76
CDD "??002 5i&2),TFN'	'
# 0557 	5DAq'*11!4	55 FGT*E}EKK(A-**eUXX^^,,,&8%++FGFBZN >L! 
fdkk2	  R	]6Q%QVIIDI
 @B 499O+
!$)), 	Bxx}H#e*%5)Ia!''#,q/)II%
		 )3 &/44  #uN4::< &jtzz?Q"3u:<0I0CJj5<<9K9K*LMMM &&~6 N ,,Z)9LMN &&x&E499%& &+,vJ''O *J8N Ns   )M 
("MM	)TrE   )r   )dr   r  loggingr~  r  collectionsr   
contextlibr   r   dataclassesr   enumr   typingr   r	   r
   r   r   r   r   r   r   rP   torch.fx._pytreerz   _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*   r:   r<   rY   r[   rQ   rR   r   boolr`   r%   r&   ABCr$   r'   r(   r   r   r\  rc  rv  r{  r   ri  r  r  r  r  r  r/  r2  rF   r  r  r   r   r   r   r   r   r6   r7   r8   <module>rM     se   
    	 # %  !  O O O  $ $ $ $ ? 3
 
 
 3 U U 7 = g!    ( (" 00ELL%"4"4ehhooEF00xx00 00 	00
 00fS
 S
l,"%((// ,"^cgg  M
 M
b MQ88080I8848)88>>8))8) 
8)vR
6# 6# 6# 6$6 6EHHOO 64%((// C %((..  Rehh""$57HHIR
XX]]RMehh""$57HHIM
XX]]M% % %* AE	)	)) 88??) HcUEHHOO%;<=	)4/S /S /S /JS J J J
s sl   $EHHNN $AR $4 GI,HHOO,(,S#X,@C,,]< ]<@	'	'"	' S#c(]#	'(#^ BF	p(HHOOp(#tCy.)p( 9p( "*$sCH}*=!>	p(r7   