Ë
    M²j£>  ã                   ó  — d dl Z d dl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dej                  j                  d	ed
edeej                  j                   ej                  j                   f   fd„Zdej                  j                  dedefd„Z	 	 ddej&                  j(                  ej                  j                  z  deej                  j                   gef   dz  dedefd„Zy)é    N)ÚCallable)Ú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j                  j                  dz  de	dz  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Ú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         €ú_/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/fx/experimental/const_fold.pyr   zFoldedGraphModule.__init__   sW   ø€ ô 	‰Ñ˜˜uÔ%ð Ð%ñ ä—‘×%Ñ% d¨NÓ;ð 	Ô"ð
 %*ˆÔ!Ø*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                 ól  ‡ — ‰ j                   �‰ j                  €y ‰ j                  rt        d«      ‚d‰ _        ‰ j                  «       }ˆ fd„}t	        |t
        «      r3t        j                  j                  |D �cg c]
  } ||«      ‘Œ c}«      n ||«      }t        ‰ ‰ j                  |«       y c c}w )NzFolding has already been runTc                 ó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_paramD   s‰   ø€ Ü—8‘8×%Ñ%ä! !¤SÔ)ð —‘“
× Ñ Ô"ä—\‘\ 1 #Ó&×)Ñ)°×1MÑ1MÐ)ÓNÜ1;¸A¼u¿|¹|Ô1L˜aŸo™oð	 &ó ð ð SXð	 &ó ð r   )
r   r   r   ÚAssertionErrorr'   Útupler   r%   ÚParameterListÚsetattr)r   Úfolded_attrsr.   r-   Úparamss   `    r   r   zFoldedGraphModule.run_folding2   s©   ø€ ð ×&Ñ&Ð.Ø×.Ñ.Ð6àà×$Ò$Ü Ð!?Ó@Ð@Ø$(ˆÔ!ð
 ×1Ñ1Ó3ˆô	ô ˜,¬Ô.ô �H‰H×"Ñ"¸lÖ#K¸¡M°!Õ$4Ò#KÔLá˜|Ó,ð 	ô
 	��d×5Ñ5°vÕ>ùò	 $Ls   Á;B1)NNÚcuda)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   ÚModuler   ÚGraphÚstrr   r   r   Ú__classcell__)r   s   @r   r   r      ss   ø„ ñð 15Ø15Ø'-ñ?à�h‰h�o‰oð?ð �x‰x�~‰~ð?ð Ÿ™Ÿ™¨Ñ-ð	?ð
 %(¨$¡Jð?ð "%õ?ô$'ö
?r   r   ÚgmÚinline_mod_nameÚrun_dceÚreturnc                 ó¶  ‡— t        | j                  «       «      |   }t        |t        j                  j
                  «      st        dt        |«      › �«      ‚d}| j                  j                  D ]%  }|j                  dk(  sŒ|j                  |k(  sŒ#|} n |€t        d|› �«      ‚|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        ||	«      }g }t        |t         t"        f«      raddl}|j&                  D �cg c]H  }|j                  d
k(  r7|j                  |j(                  u rt        |j                  d   t*        «      r|‘ŒJ }}|j-                  |«       |D ]o  }|j                  d   }t        |t*        «      st        dt        |«      › �«      ‚|j-                  ||   «       | j                  j/                  |«       ||   ‰|<   Œq �Œi| j                  j1                  |«      5  | j                  j3                  |
|	«      }ddd«       ‰|
<   �Œ° | j                  j/                  |«       |r| j                  j5                  «        ‰S c c}w # 1 sw Y   ŒOxY w)a  
    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`.

    Returns a mapping from subgraph nodes to the newly created/mapped nodes in gm.
    zExpected GraphModule, got NÚcall_modulez$Could not find call_module node for r   c                 óP   •— ‰|    }| j                   j                  «       |_         |S r   )ÚmetaÚcopy)ÚnodeÚnew_nodeÚreplacement_mappings     €r   Úreplacement_fnz&_inline_module.<locals>.replacement_fnq   s$   ø€ Ø& tÑ,ˆØŸ	™	Ÿ™Ó(ˆŒØˆr   Úplaceholderé   ÚoutputÚcall_functionzExpected int index, got )ÚdictÚnamed_modulesr'   r   r   r   r/   Útyper   ÚnodesÚopÚtargetr   r    Únamer   Úlistr0   ÚoperatorÚusersÚgetitemr(   Úreplace_all_uses_withÚ
erase_nodeÚinserting_beforeÚ	node_copyÚeliminate_dead_code)r>   r?   r@   Ú
inline_modÚcall_mod_node_to_replacerG   Úcall_mod_argsÚcall_mod_kwargsÚph_countrJ   Úinline_nodeÚoutputsÚoutput_replacementsÚgetitem_usersrW   ÚuserÚidxrH   rI   s                     @r   Ú_inline_modulerj   T   sÛ  ø€ ô �b×&Ñ&Ó(Ó)¨/Ñ:€JÜ�j¤%§(¡(×"6Ñ"6Ô7ÜÐ9¼$¸zÓ:JÐ9KÐLÓMÐMØ#ÐØ—‘—‘ò ˆØ�7‰7�mÓ#¨¯©°Ó(FØ'+Ð$Ùðð  Ð'ÜÐCÀOÐCTÐUÓVÐVð -×1Ñ1€MØ.×5Ñ5€Oà>@ÐØ€Hôð
 "×'Ñ'×-Ñ-ó ,4ˆØ�>‰>˜]Ò*ð ×#Ñ# Ñ6ð   × 0Ñ 0Ò1à" 8Ñ,ð   Ñ,ð ˜‰MˆHØà�>‰>˜XÓ%Ø!×&Ñ& qÑ)ˆGÜ")¨'°>Ó"BÐð 24ˆMÜÐ-´´e¨}Ô=Ûð !9× >Ñ >ö!àØ—w‘w /Ò1ØŸ™ x×'7Ñ'7Ñ7Ü" 4§9¡9¨Q¡<´Ô5ò	 ð!�ð !ð %×:Ñ:Ð;NÔOð &ò E�Ø—i‘i ‘l�Ü! #¤sÔ+Ü(Ð+CÄDÈÃIÀ;Ð)OÓPÐPØ×*Ñ*Ð+>¸sÑ+CÔDØ—‘×#Ñ# DÔ)Ø,?ÀÑ,DÐ# DÒ)ðEñ à�X‰X×&Ñ&Ð'?Ó@ñ 	GØ—x‘x×)Ñ)¨+°~ÓFˆH÷	Gà+3Ð˜KÓ(ðY,4ðb ‡H�H×ÑÐ0Ô1ÙØ
�‰×$Ñ$Ô&àÐùòA!÷*	Gð 	Gús   Å3AK
É$KËK	Ú
mod_tracedrU   c                 ó"  — 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+)$Ú_1rL   é   )ÚreÚsubÚisdigitÚhasattrÚmatchÚgroupr(   )rk   rU   rt   ÚbaseÚnums        r   r   r   ®   s—   € ô
 �6‰6Ð" C¨Ó.€DØˆA�w‡�ÔØ�4�&ˆzˆä
�*˜dÔ
#Ü—‘˜¨Ó.ˆØˆ=Ø˜$‘;‰DàŸ™ A qÓ)‰IˆD�#Ø�V˜1œS ›X¨™\˜NÐ+ˆDô �*˜dÕ
#ð €Kr   ÚmoduleÚskip_folding_node_fnr   c           	      ó  ‡‡— ddl }t        | t        j                  j                  «      s t        j                  j                  | «      }n| }dt        j                  j                  dt        fˆfd„Št        «       Šd}|j                  j                  D �]  }|j                  dv rŒ|j                  dk7  r%t        |j                  «      j                  ‰«      sŒG|r	 ||«      rŒR|j                  «       rŒct        |j                  j                  d	d«      |j                   «      rŒ”|j                  d
k(  rJ|j#                  |j$                  «      x}r-t        |t        j                  j                  «      r	 ‰|«      rŒí‰j'                  |«       |j                  dk7  s�Œd}�Œ |st)        ||j                  «      S dt        j                  j*                  fˆfd„}t-        || |«      }	d\  }
}|	j.                  t1        |	|d«      }}|r|j                  j                  ng D ]=  }|j                  d
k(  sŒt3        |	|j$                  t1        ||j$                  «      «       Œ? |j                  j                  D ]=  }|j                  d
k(  sŒt3        |	|j$                  t1        ||j$                  «      «       Œ? d}|	j                  j                  D ]/  }|j                  d
k(  sŒ|j$                  |
k(  sŒ#|j4                  } n |€t7        d«      ‚t        j                  j	                  |	|j                  «      }d}|j                  j                  D �].  }|j                  dk(  rt        |j4                  d   t8        «      }Œ1|j                  dk7  rŒA|t;        |«      k\  rt7        d|› dt;        |«      › d�«      ‚||   }|dz  }|j                  dk7  rt7        d|j                  › �«      ‚|j                  j=                  |«      5  |j                  j?                  |j$                  «      }ddd«       |j@                  jC                  «       _         |jE                  |«       |j                  jG                  |«       �Œ1 dtI        «       vrt7        d«      ‚tK        |d«      }t3        |	|rt        jL                  jO                  «       nt        jL                  jQ                  «       «       |	j                  j                  D ]’  }|j                  d
k(  sŒ|j$                  |
k(  sŒ#|j                  j=                  |«      5  |j                  j?                  |«      }ddd«       |j@                  jC                  «       _         |jE                  |«        n tS        |	|«      rtU        |	|«       |	j                  jW                  «        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.
    r   Nrx   rA   c                 ó”  •— t        | t        j                  j                  «      st	        d«      ‚| j
                  j                  D ]  }|j                  dk(  r|j                  «       r y|j                  dk(  sŒ4| j                  |j                  «      x}sŒRt        |t        j                  j                  «      sŒw ‰|«      c S  y)z`
        Return True if a GraphModule type subgraph contains any impure op, else False.
        zDcaller should only pass GraphModule to subgraph_has_impure_ops checkrN   TrC   F)r'   r   r   r   r/   r   rR   rS   Ú	is_impureÚget_submodulerT   )rx   rG   Ú	submoduleÚ_subgraph_has_impure_opss      €r   r   z7split_const_subgraphs.<locals>._subgraph_has_impure_opsÖ   s¦   ø€ ô ˜&¤%§(¡(×"6Ñ"6Ô7Ü ØVóð ð —L‘L×&Ñ&ò 		;ˆDØ�w‰w˜/Ò)¨d¯n©nÔ.>Ùà—‘˜=Ó(à"(×"6Ñ"6°t·{±{Ó"CÐC�YÑCÜ˜y¬%¯(©(×*>Ñ*>Õ?á/°	Ó:Ò:ð		;ð r   F>   rM   rK   Úget_attrÚ
fill_valuerC   TrG   c                 ó   •— | ‰v rdS dS )Nr   rL   © )rG   Úconst_nodess    €r   Úmod_partitionz,split_const_subgraphs.<locals>.mod_partition  s   ø€ Ø˜KÑ'ˆqÐ.¨QÐ.r   )Úsubmod_0Úsubmod_1z,Could not find call_module node for const_gmrM   rK   zPlaceholder index z out of range for args (len=ú)rL   zExpected get_attr, got Úmultiple_outputsz multiple_outputs not set in loopÚ_FX_CONST_FOLDED_ATTRS),Úsympyr'   r   r   r   Úsymbolic_traceÚboolÚsetr   rR   rS   Úall_input_nodesÚissubsetr|   r    ÚgetÚExprr}   rT   Úaddr   ÚNoder   r†   Úgetattrr2   r   r/   r0   Úlenr\   r€   rE   rF   rZ   r[   Úlocalsr   r%   r1   r&   rs   rj   r^   )rx   ry   r   r‹   rk   Úfound_const_foldingrG   Ú
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_noderH   r   r3   r   r„   s                         @@r   r   r   Â   sM  ù€ ó ä�fœeŸh™h×2Ñ2Ô3Ü—X‘X×,Ñ,¨VÓ4‰
àˆ
ð¬¯©×)=Ñ)=ð Ä$õ ô, '*£e€KØÐØ× Ñ ×&Ñ&ó &'ˆð �7‰7Ð/Ñ/Øð �7‰7�jÒ ¬¨T×-AÑ-AÓ)B×)KÑ)KØô*
ð ñ  Ñ$8¸Ô$>Øð �>‰>ÔØô �d—k‘k—o‘o l°DÓ9¸5¿:¹:ÔFØð �G‰G�}Ò$à)×7Ñ7¸¿¹ÓDÐD�ÐDÜ˜:¤u§x¡x×';Ñ';Ô<Ù(¨Ô4àð 	�‰˜ÔØ�7‰7�jÔ Ø"&ÒðM&'ñR Ü  ¨Z×-=Ñ-=Ó>Ð>ð/œEŸH™HŸM™Mõ /ô ˜ V¨]Ó;€Eà)?Ñ&€NÐ&à"Ÿ^™^¬W°UÐ<NÐPTÓ-Uˆl€Hñ
 -9�×"Ñ"×(Ò(¸bò LˆØ�7‰7�mÓ#Ü�E˜4Ÿ;™;¬°¸d¿k¹kÓ(JÕKðLð —‘×$Ñ$ò HˆØ�7‰7�mÓ#Ü�E˜4Ÿ;™;¬°¸$¿+¹+Ó(FÕGðHð ÐØ—‘×!Ñ!ò ˆØ�7‰7�mÓ#Ø�{‰{˜nÓ,Ø%)§Y¡YÐ"Ùð	ð
 Ð!ÜÐKÓLÐLô —H‘H×(Ñ(¨°·±Ó?€Mð
 €FØ×#Ñ#×)Ñ)ó -ˆØ�7‰7�hÒÜ)¨$¯)©)°A©,¼Ó>ÐØØ�7‰7�mÒ#ØØ”SÐ+Ó,Ò,Ü Ø$ V Hð -ÜÐ.Ó/Ð0°ð3óð ð % VÑ,ˆØ�!‰ˆØ�:‰:˜Ò#Ü Ð#:¸7¿:¹:¸,Ð!GÓHÐHØ× Ñ ×1Ñ1°$Ó7ñ 	DØ$×*Ñ*×3Ñ3°G·N±NÓCˆH÷	DàŸ	™	Ÿ™Ó(ˆŒØ×"Ñ" 8Ô,Ø×Ñ×&Ñ& tÖ,ð'-ð( ¤£Ñ)ÜÐ?Ó@Ð@ô "@ØÐ,ó"Ðô ØØ"Ù$4Œ�‰×ÑÔ ¼%¿(¹(×:LÑ:LÓ:Nôð
 —‘×!Ñ!ò ˆØ�7‰7�mÓ#¨¯©°~Ó(EØ—‘×,Ñ,¨TÓ2ñ OØ#Ÿz™z×2Ñ2Ð3MÓN�÷Oà $§	¡	§¡Ó 0ˆLÔØ×&Ñ& |Ô4Ùðô ˆuÐ(Ô)Ü�uÐ0Ô1à	‡K�K×#Ñ#Ô%äØØ�‰Ø×ÑØ"Øóð ÷I	Dñ 	Dú÷,Oð Oús   Ð&W3ÕX ×3W=	Ø X		)T)NÚcpu)rp   Úcollections.abcr   Útorch.fxr   Útorch.fx.noder   Útorch.fx.passes.split_moduler   Ú__all__r   r   r   r<   r�   rO   r”   rj   r   r%   r:   r   rƒ   r   r   ú<module>r©      s  ðã 	Ý $ã Ý !Ý 5ò€ô@?˜Ÿ™×,Ñ,ô @?ðH EIñWØ�‰×ÑðWØ/2ðWØ=AðWà	ˆ%�(‰(�-‰-˜Ÿ™Ÿ™Ð
&Ñ'óWðt¨u¯x©x×/CÑ/Cð È3ð ÐSVó ð, DHØ#(ñHØ�H‰H�O‰O˜eŸh™h×2Ñ2Ñ2ðHà" E§H¡H§M¡M ?°DÐ#8Ñ9¸DÑ@ðHð !ðHð ô	Hr   