
    Vh<0              	          d dl Z d dl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Optional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         P/home/dcms/DCMS/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_paramC   s    88%%!!S) 
  "\\1#&))1M1M)N1;Au||1Laoo	 &   SX	 &  r   )	r   r   r   r)   tupler   r'   ParameterListsetattr)r   folded_attrsr0   r/   paramss   `    r   r   zFoldedGraphModule.run_folding2   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                 L   t        | j                               |   }t        |t        j                  j
                        sJ d}| j                  j                  D ]%  }|j                  dk(  s|j                  |k(  s#|} n |J |j                  }|j                  }i d}fd}|j                  j                  D ]  }	|	j                  dk(  r+|	j                  |v r||	j                     n||   |	<   |dz  }=|	j                  dk(  r-|	j                  d   }
t        |
|      }|j                  |       y| 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_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)r?   r@   
inline_modcall_mod_node_to_replacerF   call_mod_argscall_mod_kwargsph_countrI   inline_nodeoutputsoutput_replacementsrG   rH   s                @r   _inline_moduler_   S   s    b&&()/:Jj%(("6"6777# 77m#(F'+$ $/// -11M.55O>@H
 "''-- 4>>]* ##6   0 01"8,  , MH>>X%!&&q)G")'>"B$::;NOXX&&'?@ 	Gxx))+~FH	G+3K('4* HH  "		G 	Gs   FF#	
mod_tracedrR   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+)$_1rK      )resubisdigithasattrmatchgroupr*   )r`   rR   rj   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>   rL   rJ   get_attrTrF   c                     | v rdS dS )Nr   rK    )rF   const_nodess    r   mod_partitionz,split_const_subgraphs.<locals>.mod_partition   s    K'q.Q.r   )submod_0submod_1NrB   rL   r   rJ   c              3   V   K   | ]   }|j                   j                  k(  s| " y wr   )rR   rQ   ).0nrF   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   rO   rP   all_input_nodesissubset	is_impureaddr   Noder   rv   getattrr3   rQ   r!   r1   nextrT   rq   rD   rE   rS   
erase_nodelocalsr	   r'   r2   r(   ri   r_   rV   )rn   ro   r   r`   found_const_foldingru   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmr|   in_noderG   r   r4   rt   rF   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)rf   typingr   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler   __all__r   r   r   r=   r_   r	   r'   r;   r   boolr
   rs   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   