
    &Vji1              	       N   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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         Z/root/voice-cloning/.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_paramC   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>L   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_folding2   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                                         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_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)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_modulerc   S   s     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  """""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+   )rd   rV   rn   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| }t                      d}|j        j        D ]}|j	        dv r|j	        dk    r(t          |j
                                                s?|r ||          rM|                                rbt          |j                            dd          |j                  r                    |           |j	        dk    rd}|st#          ||j                  S dt          j        j        ffd	}t'          || |          }d
\  }	}
|j        t+          ||
d          }}|r|j        j        ng D ]6}|j	        dk    r)t-          ||j        t+          ||j                             7|j        j        D ]6}|j	        dk    r)t-          ||j        t+          ||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         t2                    }/|j	        dk    r;|t5          |          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tC                      v sJ tE          |d          }t-          |||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tM          ||
          rtO          ||
           |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   NF>   rP   rN   get_attr
fill_valueTrJ   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_1rF   rP   rN   rO   multiple_outputs_FX_CONST_FOLDED_ATTRS))sympyr*   r   r   r   symbolic_tracesetr   rS   rT   all_input_nodesissubset	is_impurer#   getExpraddr   Noder   r{   getattrr8   rU   r"   r6   lenrX   rv   rH   rI   rW   
erase_nodelocalsr	   r(   r7   r)   rm   rc   rZ   )rs   rt   r   r   rd   found_const_foldingrJ   rz   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   ry   s                        @r   r
   r
      s    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 	 	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$   ( LL	L	QQ		Q	)Nrr   )rj   typingr   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler   __all__r   r   r   rA   rc   r	   r(   r?   r   boolr
   r3   r   r   <module>r      su   				 , , , , , , , , , ,  ! ! ! ! ! ! 5 5 5 5 5 5  ?? ?? ?? ?? ??, ?? ?? ??D1#ux+ 1#c 1# 1# 1# 1#hux/C 3 SV    , GK#(c c%(/58#778c"8UX]OT,A#BCc !c 	c c c c c cr   