
    :i7              	       V   d dl Z d dlmZ d dl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dS )    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.
    Ncudarootgraphconst_subgraphfx_const_folded_attrs_namedevice_for_folded_attrsc                     t                                          ||           |d nt          j                            ||          | _        d| _        || _        || _        d S )NF)	super__init__torchfxGraphModuleconst_subgraph_modulehas_folding_been_runr   r   )selfr   r   r   r   r   	__class__s         S/var/www/icac/venv/lib/python3.11/site-packages/torch/fx/experimental/const_fold.pyr   zFoldedGraphModule.__init__   sk     	u%%% % D%%dN;; 	"
 %*!*D''>$$$    c                 f    | j         s|                                   t                      j        | S N)r   run_foldingr   __call__)r   argskwargsr   s      r   r!   zFoldedGraphModule.__call__.   s4    ( 	uww&&r   c                 >     j          j        d S  j        rJ d _                                          } fdt          |t                    r+t
          j                            fd|D                       n
 |          }t            j        |           d S )NTc                 V   t           j                            t          | t                    s&|                                                                 n-t          j        | g                              j	                  t          | t           j                  r| j
        n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_paramD   s    8%%!!S))O

  """\1#&&))1M)NN1;Au|1L1LWaooRW	 &   r   c                 &    g | ]} |          S  r3   ).0r0   r1   s     r   
<listcomp>z1FoldedGraphModule.run_folding.<locals>.<listcomp>M   s#    #K#K#KMM!$4$4#K#K#Kr   )	r   r   r   r*   tupler   r(   ParameterListsetattr)r   folded_attrsparamsr1   s   `  @r   r    zFoldedGraphModule.run_folding3   s     &..6F,,,,$(!
 1133	 	 	 	 	 ,..-EH""#K#K#K#Kl#K#K#KLLL|,, 	
 	d5v>>>>>r   )NNr   )__name__
__module____qualname____doc__r   r(   Moduler   Graphr   strr   r!   r    __classcell__)r   s   @r   r   r      s          4848'-? ?ho? x~? !0	?
 %-SM? "%? ? ? ? ? ?$' ' ' ' '
? ? ? ? ? ? ?r   r   gminline_mod_namec                 ,   t          |                                           |         }t          |t          j        j                  sJ d}| j        j        D ]}|j        dk    r|j	        |k    r|} n|J |j
        }|j        }i d}fd}|j        j        D ]}	|	j        dk    r'|	j        |v r||	j                 n||         |	<   |dz  }4|	j        dk    r3|	j
        d         }
t          |
|          }|                    |           r| j                            |          5  | j                            |	|          }ddd           n# 1 swxY w Y   ||	<   | j                            |           | j                                         dS )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                 T    |          }| j                                         |_         |S r   )metacopy)nodenew_nodereplacement_mappings     r   replacement_fnz&_inline_module.<locals>.replacement_fnk   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
erase_nodeeliminate_dead_code)rC   rD   
inline_modcall_mod_node_to_replacerJ   call_mod_argscall_mod_kwargsph_countrM   inline_nodeoutputsoutput_replacementsrK   rL   s                @r   _inline_modulerd   T   s9    b&&(())/:Jj%("677777#  7m##(F(F'+$E#/// -1M.5O>@H    
 "'- 4 4>]** #66   011"8,  , MH>X%%!&q)G")'>"B"B$::;NOOOX&&'?@@ 	G 	Gx))+~FFH	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G+3K((
 H0111H  """""s   (EE	E	
mod_tracedrV   returnc                 J   t          j        dd|          }|d                                         rd| }t          | |          r]t          j        d|          }||dz   }n0|                    dd          \  }}| dt          |          dz    }t          | |          ]|S )	zP
    Make sure the name is unique (in a module) and can represents an attr.
    z[^0-9a-zA-Z_]+_r   z(.*)_(\d+)$N_1rO      )resubisdigithasattrmatchgroupr+   )re   rV   ro   basenums        r   r	   r	      s    
 6"C..DAw 4zz
*d
#
# ,..=$;DDAq))ID#++SXX\++D *d
#
# , Kr   cpumoduleskip_folding_node_fnr   c           	      
   ddl }t          | t          j        j                  s t          j                            |           }n| }dt          j        j        dt          ffdt                      d}|j        j	        D ]}|j
        dv r|j
        dk    r(t          |j                                                s@|r ||          rN|                                rct          |j                            d	d          |j                  r|j
        d
k    rG|                    |j                  x}r+t          |t          j        j                  r |          r                    |           |j
        dk    rd}|st)          ||j                  S dt          j        j        ffd}t-          || |          }	d\  }
}|	j        t1          |	|d          }}|r|j        j	        ng D ]6}|j
        d
k    r)t3          |	|j        t1          ||j                             7|j        j	        D ]6}|j
        d
k    r)t3          |	|j        t1          ||j                             7d}|	j        j	        D ]!}|j
        d
k    r|j        |
k    r	|j        } n"|J t          j                            |	|j                  }d}|j        j	        D ]}|j
        dk    r!t          |j        d         t6                    }/|j
        dk    r;|t9          |          k     sJ ||         }|dz  }|j
        dk    sJ |j                            |          5  |j                            |j                  }ddd           n# 1 swxY w Y   |j                                         |_        |!                    |           |j        "                    |           
dtG                      v sJ tI          |d          }t3          |	||rt          j%        &                                nt          j%        '                                           |	j        j	        D ]}|j
        d
k    r|j        |
k    r|j                            |          5  |j                            |          }ddd           n# 1 swxY w Y   |j                                         |_        |!                    |            ntQ          |	|          rtS          |	|           |	j        *                                 t)          |	|	j        |j        ||          S )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.
    r   Nrt   rf   c                 b   t          | t          j        j                  s
J d            | j        j        D ]w}|j        dk    r|                                r dS |j        dk    rH|                     |j	                  x}r,t          |t          j        j                  r |          c S xdS )z`
        Return True if a GraphModule type subgraph contains any impure op, else False.
        zDcaller should only pass GraphModule to subgraph_has_impure_ops checkcall_functionTrF   F)
r*   r   r   r   r   rS   rT   	is_impureget_submodulerU   )rt   rJ   	submodule_subgraph_has_impure_opss      r   r|   z7split_const_subgraphs.<locals>._subgraph_has_impure_ops   s     &%("677 	
 	
R	
 	
 	
 L& 
	; 
	;Dw/))dnn.>.>)tt =(("("6"6t{"C"CCY ) y%(*>?? )
 0/	:::::ur   F>   rP   rN   get_attr
fill_valuerF   TrJ   c                     | v rdndS )Nr   rO   r3   )rJ   const_nodess    r   mod_partitionz,split_const_subgraphs.<locals>.mod_partition   s    K''qqQ.r   )submod_0submod_1rP   rN   rO   multiple_outputs_FX_CONST_FOLDED_ATTRS)+sympyr*   r   r   r   symbolic_traceboolsetr   rS   rT   all_input_nodesissubsetry   r#   getExprrz   rU   addr   Noder   r   getattrr8   r"   r6   lenrX   r}   rH   rI   rW   rZ   localsr	   r(   r7   r)   rn   rd   r[   )rt   ru   r   r   re   found_const_foldingrJ   
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_noderK   r   r9   r|   r   s                         @@r   r
   r
      so    LLLfeh233 X,,V44


)= $      , '*eeK & '' '' 7/// 7j  T-A)B)B)K)K*
 *
     	$8$8$>$> 	 >> 	 dkoolD995:FF 	
 G}$$)77DDD % :ux';<< % )(44	 %  	7j  "&  ? Z-=>>>/EHM / / / / / / V];;E)?&N&"^WU<NPT-U-UlH
 -9@"((b L L7m##E4;dk(J(JKKK$ H H7m##E4;$+(F(FGGG !  7m##{n,,%)Y"))) H((??M
 F#) - -7h)$)A,>>7m##.//////$V,!zZ'''' 11$77 	D 	D$*33GNCCH	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D	((""8,,,&&t,,,,)))) "@," " "$4N   %(:L:L:N:N  
 !  7m##~(E(E,,T22 O O#z223MNNO O O O O O O O O O O O O O O $	 0 0L&&|444E
 u()) 2u0111	K##%%%"  s$    NN	N	R55R9	<R9	)Nrs   )rk   collections.abcr   typingr   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler   __all__r   r   r   rA   rd   r	   r(   r?   r   r   r
   r3   r   r   <module>r      s   				 $ $ $ $ $ $ " " " " " " " "  ! ! ! ! ! ! 5 5 5 5 5 5  ?? ?? ?? ?? ??, ?? ?? ??D5#ux+ 5#c 5# 5# 5# 5#pux/C 3 SV    , GK#(B B%(/58#778B"8UX]OT,A#BCB !B 	B B B B B Br   