
    N j>                     $   S SK r S SKJr  S SKJrJr  S SKrS SKJ	r	  S SK
Jr  / SQr " S S\R                  R                  5      r SS	\R                  R                  S
\S\S\\R                  R&                  \R                  R&                  4   4S jjrS\R                  R                  S\S\4S jr  SS\\R,                  R.                  \R                  R                  4   S\\\R                  R&                  /\4      S\S\4S jjrg)    N)Callable)OptionalUnion)map_arg)split_module)FoldedGraphModuleget_unique_attr_name_in_modulesplit_const_subgraphsc                      ^  \ rS rSrSr   SS\R                  R                  S\R                  R                  S\
\R                  R                     S\
\   S\4
U 4S jjjrU 4S	 jrS
 rSrU =r$ )r      as  
FoldedGraphModule is a GraphModule which also contains another
`const_subgraph_module` representing a subgraph which has all const attr
inputs and which can be run once before running the main standard
`graph`. The `const_output_names` are the ordered list names of attrs which
represent what each respective output from the const_subgraph should be set
on which attrs.
rootgraphconst_subgraphfx_const_folded_attrs_namedevice_for_folded_attrsc                    > [         TU ]  X5        Uc  S O[        R                  R	                  X5      U l        SU l        X@l        XPl        g )NF)	super__init__torchfxGraphModuleconst_subgraph_modulehas_folding_been_runr   r   )selfr   r   r   r   r   	__class__s         q/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/fx/experimental/const_fold.pyr   FoldedGraphModule.__init__   sO     	% % %%d; 	"
 %*!*D''>$    c                 `   > U R                   (       d  U R                  5         [        TU ]  " U6 $ N)r   run_foldingr   __call__)r   argskwargsr   s      r   r"   FoldedGraphModule.__call__.   s(    ((w&&r   c                   ^  T R                   b  T R                  c  g T R                  (       a  [        S5      eST l        T R                  5       nU 4S jn[	        U[
        5      (       a5  [        R                  R                  U Vs/ s H
  o2" U5      PM     sn5      OU" U5      n[        T T R                  U5        g s  snf )NzFolding has already been runTc                 X  > [         R                  R                  [        U [        5      (       d  U R                  5       R                  5       O-[         R                  " U /5      R                  TR                  S9[        U [         R                  5      (       a  U R                  S9$ SS9$ )N)deviceF)requires_grad)r   nn	Parameter
isinstanceintdetachcloneTensortor   r)   )ir   s    r   _create_param4FoldedGraphModule.run_folding.<locals>._create_paramE   s    88%%!!S)) 
  "\\1#&))1M1M)N1;Au||1L1Laoo	 &   SX	 &  r   )
r   r   r   AssertionErrorr,   tupler   r*   ParameterListsetattr)r   folded_attrsr3   r2   paramss   `    r   r!   FoldedGraphModule.run_folding3   s     &&...6$$ !?@@$(!
 113	 ,.. HH""l#KlM!$4l#KL|, 	
 	d55v>	 $Ls   C )r   r   r   r   )NNcuda)__name__
__module____qualname____firstlineno____doc__r   r*   Moduler   Graphr   strr   r"   r!   __static_attributes____classcell__)r   s   @r   r   r      s}     4848'-?hhoo? xx~~? !0	?
 %-SM? "%? ?$'
? ?r   r   gminline_mod_namerun_dcereturnc                 "  ^ [        U R                  5       5      U   n[        U[        R                  R
                  5      (       d  [        S[        U5       35      eSnU R                  R                   H)  nUR                  S:X  d  M  UR                  U:X  d  M'  Un  O   Uc  [        SU 35      eUR                  nUR                  n0 mSnU4S jn	UR                  R                   GH  n
U
R                  S:X  a,  U
R                  U;   a  XzR                     OXh   TU
'   US-  nM@  U
R                  S	:X  Ga7  U
R                  S   n[        X5      n/ n[        U[         ["        45      (       aq  SSKnUR&                   Vs/ s HV  nUR                  S
:X  d  M  UR                  UR(                  L d  M0  [        UR                  S   [*        5      (       d  MT  UPMX     nnUR-                  U5        U Hu  nUR                  S   n[        U[*        5      (       d  [        S[        U5       35      eUR-                  UU   5        U R                  R/                  U5        UU   TU'   Mw     GM  U R                  R1                  U5         U R                  R3                  X5      nSSS5        WTU
'   GM     U R                  R/                  U5        U(       a  U R                  R5                  5         T$ s  snf ! , (       d  f       N[= f)z
Given `gm` and some graph module which is called with target name `inline_mod_name`,
this helper will inline all of the nodes from that called graph module into `gm`.

Returns a mapping from subgraph nodes to the newly created/mapped nodes in gm.
zExpected GraphModule, got Ncall_modulez$Could not find call_module node for r   c                 P   > TU    nU R                   R                  5       Ul         U$ r    )metacopy)nodenew_nodereplacement_mappings     r   replacement_fn&_inline_module.<locals>.replacement_fnr   s$    &t,		(r   placeholder   outputcall_functionzExpected int index, got )dictnamed_modulesr,   r   r   r   r5   typer   nodesoptargetr#   r$   namer   listr6   operatorusersgetitemr-   replace_all_uses_with
erase_nodeinserting_before	node_copyeliminate_dead_code)rG   rH   rI   
inline_modcall_mod_node_to_replacerP   call_mod_argscall_mod_kwargsph_countrS   inline_nodeoutputsoutput_replacementsgetitem_usersra   useridxrQ   rR   s                     @r   _inline_modulert   U   s    b&&()/:Jj%(("6"6779$z:J9KLMM#77m#(F'+$   'COCTUVV -11M.55O>@H
 "''-->>]* ##6   0 01",  , MH>>X%!&&q)G")'"B 24M-e}== !9 > >! >ww/1  x'7'77  #499Q<5	  >  ! %::;NO &iil!#s++(+CDI;)OPP**+>s+CD##D),?,D#D) & XX&&'?@xx))+FH A+3K(Y .b HH01
$$&A!* A@s$   K;K;9 K;K;L  
L	
mod_tracedr_   c                 B   [         R                  " SSU5      nUS   R                  5       (       a  SU 3n[        X5      (       aY  [         R                  " SU5      nUc  US-   nO'UR                  SS5      u  p4U S[        U5      S-    3n[        X5      (       a  MY  U$ )zH
Make sure the name is unique (in a module) and can represents an attr.
z[^0-9a-zA-Z_]+_r   z(.*)_(\d+)$_1rV      )resubisdigithasattrmatchgroupr-   )ru   r_   r~   basenums        r   r	   r	      s    
 66"C.DAw4&z
*
#
#.=$;DAq)IDV1SX\N+D *
#
# Kr   moduleskip_folding_node_fnr   c           	        ^^ SSK n[        U [        R                  R                  5      (       d   [        R                  R                  U 5      nOU nS[        R                  R                  S[        4U4S jjm[        5       mSnUR                  R                   GHF  nUR                  S;   a  M  UR                  S:w  a+  [        UR                  5      R                  T5      (       d  MQ  U(       a  U" U5      (       a  Mg  UR                  5       (       a  M~  [        UR                  R                  S	S5      UR                   5      (       a  M  UR                  S
:X  a[  UR#                  UR$                  5      =n(       a9  [        U[        R                  R                  5      (       a  T" U5      (       a  GM   TR'                  U5        UR                  S:w  d  GMD  SnGMI     U(       d  [)        XDR                  5      $ S[        R                  R*                  4U4S jjn[-        X@U5      n	Su  pU	R.                  [1        XS5      pU(       a  UR                  R                  O/  H>  nUR                  S
:X  d  M  [3        XR$                  [1        XR$                  5      5        M@     UR                  R                   H>  nUR                  S
:X  d  M  [3        XR$                  [1        XR$                  5      5        M@     SnU	R                  R                   H3  nUR                  S
:X  d  M  UR$                  U
:X  d  M'  UR4                  n  O   Uc  [7        S5      e[        R                  R	                  XR                  5      nSnUR                  R                   GH5  nUR                  S:X  a  [        UR4                  S   [8        5      nM3  UR                  S:w  a  ME  U[;        U5      :  a  [7        SU S[;        U5       S35      eUU   nUS-  nUR                  S:w  a  [7        SUR                   35      eUR                  R=                  U5         UR                  R?                  UR$                  5      nSSS5        UR@                  RC                  5       Wl         URE                  U5        UR                  RG                  U5        GM8     S[I        5       ;  a  [7        S5      e[K        US5      n[3        U	UW(       a  [        RL                  RO                  5       O[        RL                  RQ                  5       5        U	R                  R                   H  nUR                  S
:X  d  M  UR$                  U
:X  d  M'  UR                  R=                  U5         UR                  R?                  U5      nSSS5        UR@                  RC                  5       Wl         URE                  U5          O   [S        X5      (       a  [U        X5        U	R                  RW                  5         [)        U	U	R                  UR                  UU5      $ ! , (       d  f       GN= f! , (       d  f       N= f)a2  
Looks through `module` for any nodes that have all constant attribute inputs
and separates them out into their own constant subgraph, and returns a
FoldedGraphModule which runs that constant subgraph on the first run to set
attributes on the module prior to running the non-constant portion of the
graph.
r   Nr   rJ   c                   > [        U [        R                  R                  5      (       d  [	        S5      eU R
                  R                   H  nUR                  S:X  a  UR                  5       (       a    gUR                  S:X  d  M<  U R                  UR                  5      =n(       d  M`  [        U[        R                  R                  5      (       d  M  T" U5      s  $    g)zP
Return True if a GraphModule type subgraph contains any impure op, else False.
zDcaller should only pass GraphModule to subgraph_has_impure_ops checkrX   TrL   F)r,   r   r   r   r5   r   r\   r]   	is_impureget_submoduler^   )r   rP   	submodule_subgraph_has_impure_opss      r   r   7split_const_subgraphs.<locals>._subgraph_has_impure_ops   s     &%(("6"677 V  LL&&Dww/)dnn.>.>=("("6"6t{{"CCYCy%((*>*>??/	:: ' r   F>   rW   rU   get_attr
fill_valuerL   TrP   c                    > U T;   a  S$ S$ )Nr   rV    )rP   const_nodess    r   mod_partition,split_const_subgraphs.<locals>.mod_partition  s    K'q.Q.r   )submod_0submod_1z,Could not find call_module node for const_gmrW   rU   zPlaceholder index z out of range for args (len=)rV   zExpected get_attr, got multiple_outputsz multiple_outputs not set in loop_FX_CONST_FOLDED_ATTRS),sympyr,   r   r   r   symbolic_traceboolsetr   r\   r]   all_input_nodesissubsetr   r$   getExprr   r^   addr   Noder   r   getattrr8   r#   r5   r6   lenrf   r   rN   rO   rd   re   localsr	   r*   r7   r+   r}   rt   rh   )r   r   r   r   ru   found_const_foldingrP   
target_modr   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmph_idxr   in_noderQ   r   r9   r   r   s                         @@r   r
   r
      s    fehh2233XX,,V4

)=)= $ , '*eK  && 77// 77j T-A-A)B)K)K*
 *
   $8$>$> >> dkkoolD95::FF GG}$)77DDD:uxx';';<<(44 	77j "&M 'R  -=-=>>/EHHMM / ];E)?&N"^^WUPT-Ul
 -9""((b@77m#E;;kk(JK A $$77m#E;;++(FG % !!77m#{{n,%)YY"	 "
 !KLL HH((?M
 F##))77h)$))A,>77m#S+,, $VH -./03  %V,!::# #:7::,!GHH  11$7$**33GNNCH 8		(""8,&&t,' *( )?@@ "@," "$4 %((:L:L:N
 !!77m#~(E,,T2#zz223MN 3 $		 0L&&|4 " u))u1	KK##%" I 87, 32s   &Y(Y*
Y'	*
Y8	)T)Ncpu)rz   collections.abcr   typingr   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler   __all__r   r   r   rD   r   rY   r   rt   r	   r*   rB   r
   r   r   r   <module>r      s   	 $ "  ! 5@?,, @?H EIWW/2W=AW	%((--
&'Wtuxx/C/C 3 SV , GK#(H%((//588#7#778H"8UXX]]OT,A#BCH !H 	Hr   