
    קgq/              	       V   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dS )    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.
    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         \/var/www/html/ai-engine/env/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_param>   s    8%%!!S))O

  """\1#&&))1M)NN1;Au|1L1LWaooRW	 &   r   c                 &    g | ]} |          S  r5   ).0r2   r3   s     r   
<listcomp>z1FoldedGraphModule.run_folding.<locals>.<listcomp>G   s#    #K#K#KMM!$4$4#K#K#Kr   )	r   r   r   r,   tupler   r*   ParameterListsetattr)r   folded_attrsparamsr3   s   `  @r   r"   zFoldedGraphModule.run_folding-   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
        }i d}fd}|j        j        D ]}|j        dk    r||         |<   |dz  }|j        dk    r3|j
        d         }	t          |	|          }
|                    |
           \| 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_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)rE   rF   
inline_modcall_mod_node_to_replacerL   call_mod_argsph_countrO   inline_nodeoutputsoutput_replacementsrM   rN   s               @r   _inline_modulerc   N   s    b&&(())/:Jj%("677777#  7m##(F(F'+$E#/// -1M>@H    
 "'- 4 4>]**/<X/F,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   D33D7	:D7	
mod_tracedname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_1rQ      )resubisdigithasattrmatchgroupr-   )rd   re   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           	      <	   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b                               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                  }|j        j        D ]j        d
k    r!t          j        d         t*                    }/j        dk    r;t-          fd|D                       }|j        dk    sJ |j                                      5  |j                            |j                  }ddd           n# 1 swxY w Y   j                                        |_                            |           |j                                       dt;                      v sJ t=          |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tE          ||          rtG          ||           |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.
    F>   rR   rP   get_attrTrL   c                     | v rdndS )Nr   rQ   r5   )rL   const_nodess    r   mod_partitionz,split_const_subgraphs.<locals>.mod_partition   s    K''qqQ.r   )submod_0submod_1NrH   rR   r   rP   c              3   <   K   | ]}|j         j        k    |V  d S r!   )re   rW   )r6   nrL   s     r   	<genexpr>z(split_const_subgraphs.<locals>.<genexpr>   s1      NNQ$+8M8Mq8M8M8M8MNNr   multiple_outputs_FX_CONST_FOLDED_ATTRS)%r,   r   r   r   symbolic_tracesetr   rU   rV   all_input_nodesissubset	is_impureaddr
   Noder	   r{   getattrr:   rW   r$   r8   nextrY   rw   rJ   rK   rX   
erase_nodelocalsr   r*   r9   r+   rn   rc   r[   )rt   ru   r   rd   found_const_foldingrz   splitconst_mod_namenon_const_mod_nameconst_gmnon_const_gmcall_const_gm_argsroot_const_gmr   in_noderM   r   r;   ry   rL   s                     @@r   r   r      s    feh233 X,,V44


 '*eeK & ' ' 7/// 7j  T-A)B)B)K)K*
 *
     	$8$8$>$> 	 >> 	 	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#) - -7h)$)A,>>7m##NNNN"4NNNNN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$   - KK	 K	 PP	P	)Nrs   )rk   typingr   r   r   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler	   __all__r   r   r
   rC   rc   r   r*   rA   r   boolr   r5   r   r   <module>r      s~   				 7 7 7 7 7 7 7 7 7 7 7 7 7 7  ! ! ! ! ! ! 5 5 5 5 5 5 [
Z
Z?? ?? ?? ?? ??, ?? ?? ??D*#ux+ *#c *# *# *# *#Zux/C 3 SV    , GK#(U U%(/58#778U"8UX]OT,A#BCU !U 	U U U U U Ur   