
    sgq/              	          d dl Z d dlmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ g dZ G d dej                  j                        Zdej                  j                  d	efd
Zdej                  j                  dedefdZ	 	 ddeej(                  j*                  ej                  j                  f   deeej                  j,                  gef      dedefdZy)    N)CallableDictOptionalSetUnion)map_arg)split_module)FoldedGraphModuleget_unique_attr_name_in_modulesplit_const_subgraphsc                        e Zd ZdZ	 	 	 d
dej
                  j                  dej                  j                  de	ej                  j                     de	e
   de
f
 fdZ fdZd	 Z xZS )r
   a  
    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         |   ||       |d nt        j                  j	                  ||      | _        d| _        || _        || _        y )NF)	super__init__torchfxGraphModuleconst_subgraph_modulehas_folding_been_runr   r   )selfr   r   r   r   r   	__class__s         S/var/www/html/venv/lib/python3.12/site-packages/torch/fx/experimental/const_fold.pyr   zFoldedGraphModule.__init__   sW     	u% % %%dN; 	"
 %*!*D''>$    c                 T    | j                   s| j                          t        |   | S N)r   run_foldingr   __call__)r   argskwargsr   s      r   r"   zFoldedGraphModule.__call__(   s(    ((w&&r   c                 Z     j                    j                  y  j                  rJ d _         j                         } fd}t        |t              r3t
        j                  j                  |D cg c]
  } ||       c}      n ||      }t          j                  |       y c c}w )NTc                 T   t         j                  j                  t        | t              s| j                         j                         n/t        j                  | g      j                  j                        t        | t         j                        r| j                        S d      S )N)deviceF)requires_grad)r   nn	Parameter
isinstanceintdetachcloneTensortor   r(   )ir   s    r   _create_paramz4FoldedGraphModule.run_folding.<locals>._create_param>   s    88%%!!S) 
  "\\1#&))1M1M)N1;Au||1Laoo	 &   SX	 &  r   )	r   r   r   r+   tupler   r)   ParameterListsetattr)r   folded_attrsr2   r1   paramss   `    r   r!   zFoldedGraphModule.run_folding-   s     &&...6,,,,$(!
 113	 ,. HH""l#KM!$4#KL|, 	
 	d55v>	 $Ls   2B()NNcuda)__name__
__module____qualname____doc__r   r)   Moduler   Graphr   strr   r"   r!   __classcell__)r   s   @r   r
   r
      ss     4848'-?hhoo? xx~~? !0	?
 %-SM? "%?$'
?r   r
   gminline_mod_namec                    t        | j                               |   }t        |t        j                  j
                        sJ d}| j                  j                  D ]%  }|j                  dk(  s|j                  |k(  s#|} n |J |j                  }i d}fd}|j                  j                  D ]  }|j                  dk(  r||   |<   |dz  } |j                  dk(  r-|j                  d   }	t        |	|      }
|j                  |
       \| j                  j                  |      5  | j                  j                  ||      }ddd       |<    | j                  j                          y# 1 sw Y   +xY w)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    |    }| j                   j                         |_         |S r    )metacopy)nodenew_nodereplacement_mappings     r   replacement_fnz&_inline_module.<locals>.replacement_fnc   s$    &t,		(r   placeholder   output)dictnamed_modulesr+   r   r   r   r   nodesoptargetr#   r   replace_all_uses_withinserting_before	node_copyeliminate_dead_code)rA   rB   
inline_modcall_mod_node_to_replacerH   call_mod_argsph_countrK   inline_nodeoutputsoutput_replacementsrI   rJ   s               @r   _inline_moduler_   N   s~    b&&()/:Jj%(("6"6777# 77m#(F'+$ $/// -11M>@H
 "''-- 4>>]*/<X/F,MH>>X%!&&q)G")'>"B$::;NOXX&&'?@ 	Gxx))+~FH	G+3K(4  HH  "		G 	Gs   *E11E:	
mod_tracednamereturnc                 "   t        j                  dd|      }|d   j                         rd| }t        | |      rSt        j                  d|      }||dz   }n(|j                  dd      \  }}| dt        |      dz    }t        | |      rS|S )zP
    Make sure the name is unique (in a module) and can represents an attr.
    z[^0-9a-zA-Z_]+_r   z(.*)_(\d+)$_1rM      )resubisdigithasattrmatchgroupr,   )r`   ra   rk   basenums        r   r   r   {   s    
 66"C.DAw4&z
*d
#.=$;DAq)ID#V1SX\N+D *d
# Kr   moduleskip_folding_node_fnr   c           	      
   t        | t        j                  j                        s t        j                  j	                  |       }n| }t               d}|j                  j                  D ]  j                  dv rj                  dk7  r%t        j                        j                        sF|r	 |      rQj                         rbj                         j                  dk7  sd} |st        ||j                        S dt        j                  j                  ffd}t        || |      }d\  }}|j                   t#        ||d      }
}	|
r|
j                  j                  ng D ]=  j                  d	k(  st%        |j&                  t#        |
j&                               ? |	j                  j                  D ]=  j                  d	k(  st%        |j&                  t#        |	j&                               ? d}|j                  j                  D ]/  j                  d	k(  sj&                  |k(  s#j(                  } n |J t        j                  j                  ||	j                        }|j                  j                  D ]  j                  d
k(  rt        j(                  d   t*              }0j                  dk7  r@t-        fd|D              }|j                  dk(  sJ |j                  j/                        5  |j                  j1                  |j&                        }ddd       j2                  j5                         _        j7                  |       |j                  j9                          dt;               v sJ t=        |d      }t%        ||rt        j>                  jA                         nt        j>                  jC                                |j                  j                  D ]  j                  d	k(  sj&                  |k(  s#j                  j/                        5  j                  j1                  |      }ddd       j2                  j5                         _        j7                  |        n tE        ||      rtG        ||       |j                  jI                          t        ||j                  |j                  ||      S # 1 sw Y   xY w# 1 sw Y   xY w)aJ  
    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>   rN   rL   get_attrTrH   c                     | v rdS dS )Nr   rM    )rH   const_nodess    r   mod_partitionz,split_const_subgraphs.<locals>.mod_partition   s    K'q.Q.r   )submod_0submod_1NrD   rN   r   rL   c              3   V   K   | ]   }|j                   j                  k(  s| " y wr    )ra   rS   ).0nrH   s     r   	<genexpr>z(split_const_subgraphs.<locals>.<genexpr>   s      NQ$++8MqNs   ))multiple_outputs_FX_CONST_FOLDED_ATTRS)%r+   r   r   r   symbolic_tracesetr   rQ   rR   all_input_nodesissubset	is_impureaddr
   Noder	   rw   getattrr5   rS   r#   r3   nextrU   rr   rF   rG   rT   
erase_nodelocalsr   r)   r4   r*   rj   r_   rW   )ro   rp   r   r`   found_const_foldingrv   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmr}   in_noderI   r   r6   ru   rH   s                     @@r   r   r      s`    fehh223XX,,V4

 '*eK  && ' 77// 77j T-A-A)B)K)K*
   $8$> >> 	77j "&1'6  Z-=-=>>/EHHMM / V];E)?&N&"^^WU<NPT-UlH
 -9""((b L77m#E4;;dkk(JKL $$ H77m#E4;;$++(FGH !! 77m#{{n,%)YY"	
 ))) HH((?M##)) -77h)$))A,>77m#N"4NNzzZ'''  11$7 	D$**33GNNCH	D		(""8,&&t,- ))) "@," "$4 %((:L:L:N
 !! 77m#~(E,,T2 O#zz223MNO $		 0L&&|4 u()u01	KK##%" G	D 	D*O Os   &S1S>1S;	>T	)Ncpu)rg   typingr   r   r   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler	   __all__r   r   r
   r?   r_   r   r)   r=   r   boolr   rt   r   r   <module>r      s    	 7 7  ! 5 [??,, ??D*#uxx++ *#c *#Zuxx/C/C 3 SV , GK#(U%((//588#7#778U"8UXX]]OT,A#BCU !U 	Ur   