
    9i1              	          S SK r S SKJr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\R                  R                  S	\4S
 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         `/var/www/html/land-doc-ocr/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                 x  ^  T R                   b  T R                  c  g T R                  (       a   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 )NTc                 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_paramC   s    88%%!!S)) 
  "\\1#&))1M1M)N1;Au||1L1Laoo	 &   SX	 &  r   )	r   r   r   r,   tupler   r*   ParameterListsetattr)r   folded_attrsr3   r2   paramss   `    r   r!   FoldedGraphModule.run_folding2   s     &&...6,,,,$(!
 113	 ,.. HH""l#KlM!$4l#KL|, 	
 	d55v>	 $Ls   ?B7)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_namec                 v  ^ [        U R                  5       5      U   n[        U[        R                  R
                  5      (       d   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   eUR                  nUR                  n0 mSnU4S jnUR                  R                   H  n	U	R                  S:X  a,  U	R                  U;   a  XiR                     OXW   TU	'   US-  nM?  U	R                  S:X  a-  U	R                  S   n
[        X5      nUR                  U5        M|  U R                  R                  U5         U R                  R!                  X5      nSSS5        WTU	'   M     U R                  R#                  5         g! , (       d  f       N2= 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`.
Ncall_moduler   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_fnj   s$    &t,		(r   placeholder   output)dictnamed_modulesr,   r   r   r   r   nodesoptargetr#   r$   namer   replace_all_uses_withinserting_before	node_copyeliminate_dead_code)rF   rG   
inline_modcall_mod_node_to_replacerM   call_mod_argscall_mod_kwargsph_countrP   inline_nodeoutputsoutput_replacementsrN   rO   s                @r   _inline_modulerg   S   s    b&&()/:Jj%(("6"67777#77m#(F'+$  $/// -11M.55O>@H
 "''-->>]* ##6   0 01",  , MH>>X%!&&q)G")'"B$::;NOXX&&'?@xx))+FH A+3K(' .* HH  "	 A@s   "F**
F8	
mod_tracedrZ   returnc                 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+)$_1rS      )resubisdigithasattrmatchgroupr-   )rh   rZ   rr   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           	      D  ^ SSK n[        U [        R                  R                  5      (       d   [        R                  R                  U 5      nOU n[        5       mSnUR                  R                   H  nUR                  S;   a  M  UR                  S:w  a+  [        UR                  5      R                  T5      (       d  MP  U(       a  U" U5      (       a  Mf  UR                  5       (       a  M}  [        UR                  R                  SS5      UR                  5      (       a  M  TR!                  U5        UR                  S:w  d  M  SnM     U(       d  [#        XDR                  5      $ S[        R                  R$                  4U4S	 jjn['        X@U5      nS
u  pUR(                  [+        XS5      pU(       a  UR                  R                  O/  H>  nUR                  S:X  d  M  [-        XR.                  [+        XR.                  5      5        M@     UR                  R                   H>  nUR                  S:X  d  M  [-        XR.                  [+        XR.                  5      5        M@     SnUR                  R                   H3  nUR                  S:X  d  M  UR.                  U	:X  d  M'  UR0                  n  O   Uc   e[        R                  R	                  XR                  5      nSnUR                  R                   GH  nUR                  S:X  a  [        UR0                  S   [2        5      nM3  UR                  S:w  a  ME  U[5        U5      :  d   eX   nUS-  nUR                  S:X  d   eUR                  R7                  U5         UR                  R9                  UR.                  5      nSSS5        UR:                  R=                  5       Wl        UR?                  U5        UR                  RA                  U5        GM     S[C        5       ;   d   e[E        US5      n[-        UUW(       a  [        RF                  RI                  5       O[        RF                  RK                  5       5        UR                  R                   H  nUR                  S:X  d  M  UR.                  U	:X  d  M'  UR                  R7                  U5         UR                  R9                  U5      nSSS5        UR:                  R=                  5       Wl        UR?                  U5          O   [M        X5      (       a  [O        X5        UR                  RQ                  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   NF>   rT   rR   get_attr
fill_valueTrM   c                    > U T;   a  S$ S$ )Nr   rS    )rM   const_nodess    r   mod_partition,split_const_subgraphs.<locals>.mod_partition   s    K'q.Q.r   )submod_0submod_1rI   rT   rR   rS   multiple_outputs_FX_CONST_FOLDED_ATTRS))sympyr,   r   r   r   symbolic_tracesetr   rW   rX   all_input_nodesissubset	is_impurer$   getExpraddr   Noder   r   getattrr7   rY   r#   r5   lenr\   ry   rK   rL   r[   
erase_nodelocalsr	   r*   r6   r+   rq   rg   r^   )rv   rw   r   r   rh   found_const_foldingrM   r~   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmph_idxr   in_noderN   r   r8   r}   s                        @r   r
   r
      sd    fehh2233XX,,V4

 '*eK  && 77// 77j T-A-A)B)K)K*
 *
   $8$>$> >> dkkoolD95::FF 	77j "&9 '>  -=-=>>/EHHMM / ];E)?&N"^^WUPT-Ul
 -9""((b@77m#E;;kk(JK A $$77m#E;;++(FG % !!77m#{{n,%)YY"	 "
 ))) HH((?M
 F##))77h)$))A,>77m#.////$,!zzZ'''  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##%" G 87* 32s   &U?V?
V	
V	)Ncpu)rn   typingr   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler   __all__r   r   r   rC   rg   r	   r*   rA   r   boolr
   r|   r   r   <module>r      s    	 , ,  ! 5??,, ??D1#uxx++ 1#c 1#huxx/C/C 3 SV , GK#(c%((//588#7#778c"8UXX]]OT,A#BCc !c 	cr   