
    [Th<0              	          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         X/var/www/auris/envauris/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           	      
  ^^ [        U [        R                  R                  5      (       d   [        R                  R	                  U 5      nOU n[        5       mSnUR                  R                   H  mTR                  S;   a  M  TR                  S:w  a+  [        TR                  5      R                  T5      (       d  MP  U(       a  U" T5      (       a  Mf  TR                  5       (       a  M}  TR                  T5        TR                  S:w  d  M  SnM     U(       d  [        X3R                  5      $ S[        R                  R                  4U4S jjn[        X0U5      nSu  pxUR                   [#        XhS5      pU
(       a  U
R                  R                  O/  H@  mTR                  S	:X  d  M  [%        UTR&                  [#        U
TR&                  5      5        MB     U	R                  R                   H@  mTR                  S	:X  d  M  [%        UTR&                  [#        U	TR&                  5      5        MB     SnUR                  R                   H3  mTR                  S	:X  d  M  TR&                  U:X  d  M'  TR(                  n  O   Uc   e[        R                  R                  XiR                  5      nUR                  R                   GH   mTR                  S
:X  a  [        TR(                  S   [*        5      nM3  TR                  S:w  a  ME  [-        U4S jU 5       5      nUR                  S:X  d   eUR                  R/                  T5         UR                  R1                  UR&                  5      nSSS5        TR2                  R5                  5       Wl        TR7                  U5        UR                  R9                  T5        GM     S[;        5       ;   d   e[=        US5      n[%        UUW(       a  [        R>                  RA                  5       O[        R>                  RC                  5       5        UR                  R                   H  mTR                  S	:X  d  M  TR&                  U:X  d  M'  TR                  R/                  T5         TR                  R1                  U5      nSSS5        TR2                  R5                  5       Wl        TR7                  U5          O   [E        Xh5      (       a  [G        Xh5        UR                  RI                  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.
F>   rT   rR   get_attrTrM   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_1NrI   rT   r   rR   c              3   ^   >#    U  H"  oR                   TR                  :X  d  M  Uv   M$     g 7fr    )rZ   rY   ).0nrM   s     r   	<genexpr>(split_const_subgraphs.<locals>.<genexpr>  s"     N"4Q$++8Mqq"4s   -	-multiple_outputs_FX_CONST_FOLDED_ATTRS)%r,   r   r   r   symbolic_tracesetr   rW   rX   all_input_nodesissubset	is_impureaddr   Noder   r   getattrr7   rY   r#   r5   nextr\   ry   rK   rL   r[   
erase_nodelocalsr	   r*   r6   r+   rq   rg   r^   )rv   rw   r   rh   found_const_foldingr}   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmr   in_noderN   r   r8   r|   rM   s                     @@r   r
   r
      s)    fehh2233XX,,V4

 '*eK  && 77// 77j T-A-A)B)K)K*
 *
   $8$>$> >> 	77j "&1 '6  -=-=>>/EHHMM / ];E)?&N"^^WUPT-Ul
 -9""((b@77m#E4;;dkk(JK A $$77m#E4;;$++(FG % !!77m#{{n,%)YY"	 "
 ))) HH((?M##))77h)$))A,>77m#N"4NN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U
U	
U"	)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#(U%((//588#7#778U"8UXX]]OT,A#BCU !U 	Ur   