Ë
    ÄÓ^j7A  ã                   ó  — d dl Z d dlm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)ÚAny)Ú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	ddfˆ fd	„Z
d
ededefˆ fd„Zd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_attrsÚreturnc                 ó¢   •— 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         €úk/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/experimental/const_fold.pyr   zFoldedGraphModule.__init__   sW   ø€ ô 	‰Ñ˜˜uÔ%ð Ð%ñ ä—‘×%Ñ% d¨NÓ;ð 	Ô"ð
 %*ˆÔ!Ø*DˆÔ'Ø'>ˆÕ$ó    ÚargsÚkwargsc                 óT   •— | j                   s| j                  «        t        ‰| �  |Ž S ©N)r   Úrun_foldingr   Ú__call__)r   r   r   r   s      €r   r"   zFoldedGraphModule.__call__-   s(   ø€ Ø×(Ò(Ø×ÑÔÜ‰wÑ Ð&Ð&r   c                 óÐ  ‡ — ‰ j                   �‰ j                  €y ‰ j                  rt        d«      ‚d‰ _        ‰ j                  «       }dt        j
                  t        z  dt        j                  j                  fˆ fd„}t        |t        «      r3t        j                  j                  |D �cg c]
  } ||«      ‘Œ c}«      n ||«      }t        ‰ ‰ j                  |«       y c c}w )NzFolding has already been runTÚir   c                 ó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'   )r$   r   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   r.   r+   r(   r)   r*   ÚtupleÚParameterListÚsetattr)r   Úfolded_attrsr0   r$   Úparamss   `    r   r!   zFoldedGraphModule.run_folding2   sÊ   ø€ ð ×&Ñ&Ð.Ø×.Ñ.Ð6àà×$Ò$Ü Ð!?Ó@Ð@Ø$(ˆÔ!ð
 ×1Ñ1Ó3ˆð	œUŸ\™\¬CÑ/ð 	´E·H±H×4FÑ4Fõ 	ô ˜,¬Ô.ô �H‰H×"Ñ"¸lÖ#K¸¡M°!Õ$4Ò#KÔLá˜|Ó,ð 	ô
 	��d×5Ñ5°vÕ>ùò	 $Ls   Â-C#)NNÚcuda)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   ÚModuler   ÚGraphÚstrr   Úobjectr   r"   r!   Ú__classcell__)r   s   @r   r   r      s’   ø„ ñð 15Ø15Ø'-ñ?à�h‰h�o‰oð?ð �x‰x�~‰~ð?ð Ÿ™Ÿ™¨Ñ-ð	?ð
 %(¨$¡Jð?ð "%ð?ð 
õ?ð$'˜fð '°ð '¸3õ '÷
?r   r   ÚgmÚinline_mod_nameÚrun_dcer   c                 ó   ‡— 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}dt        j                  j                  dt        j                  j                  fˆ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                  j1                  |«       ||   ‰|<   Œq �Œi| j                  j3                  |«      5  | j                  j5                  |
|	«      }ddd«       ‰|
<   �Œ° | j                  j1                  |«       |r| j                  j7                  «        ‰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   Únoder   c                 óP   •— ‰|    }| j                   j                  «       |_         |S r    )ÚmetaÚcopy)rF   Ú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   r1   Útyper   ÚnodesÚopÚtargetr   r   ÚNodeÚnamer   Úlistr2   ÚoperatorÚusersÚgetitemr+   Úreplace_all_uses_withÚ
erase_nodeÚinserting_beforeÚ	node_copyÚeliminate_dead_code)rA   rB   rC   Ú
inline_modÚcall_mod_node_to_replacerF   Úcall_mod_argsÚcall_mod_kwargsÚph_countrL   Úinline_nodeÚoutputsÚoutput_replacementsÚgetitem_usersrZ   ÚuserÚidxrJ   rK   s                     @r   Ú_inline_modulerm   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ðœUŸX™XŸ]™]ð ¬u¯x©x¯}©}õ ð
 "×'Ñ'×-Ñ-ó ,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   Æ(AK?ÊLÌL	Ú
mod_tracedrX   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+)$Ú_1rN   é   )ÚreÚsubÚisdigitÚhasattrÚmatchÚgroupr+   )rn   rX   rw   Ú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*                  dt,        fˆfd„}t/        || |«      }	d\  }
}t1        |	|
«      }t        |t        j                  j                  «      st3        d|
› dt5        |«      › �«      ‚t1        |	|d«      }d}|�@t        |t        j                  j                  «      st3        d|› dt5        |«      › �«      ‚|}|�V|j                  j                  D ]=  }|j                  d
k(  sŒt7        |	|j$                  t1        ||j$                  «      «       Œ? |j                  j                  D ]=  }|j                  d
k(  sŒt7        |	|j$                  t1        ||j$                  «      «       Œ? d}|	j                  j                  D ]/  }|j                  d
k(  sŒ|j$                  |
k(  sŒ#|j8                  } n |€t3        d«      ‚t        j                  j	                  |	|j                  «      }d}|j                  j                  D �].  }|j                  dk(  rt        |j8                  d   t:        «      }Œ1|j                  dk7  rŒA|t=        |«      k\  rt3        d|› dt=        |«      › d�«      ‚||   }|dz  }|j                  dk7  rt3        d|j                  › �«      ‚|j                  j?                  |«      5  |j                  jA                  |j$                  «      }ddd«       |jB                  jE                  «       _!        |jG                  |«       |j                  jI                  |«       �Œ1 dtK        «       vrt3        d«      ‚tM        |d«      }t7        |	|rt        jN                  jQ                  «       nt        jN                  jS                  «       «       |	j                  j                  D ]’  }|j                  d
k(  sŒ|j$                  |
k(  sŒ#|j                  j?                  |«      5  |j                  jA                  |«      }ddd«       |jB                  jE                  «       _!        |jG                  |«        n tU        |	|«      rtW        |	|«       |	j                  jY                  «        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   Nr{   r   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 checkrP   TrE   F)r*   r   r   r   r1   r   rT   rU   Ú	is_impureÚget_submodulerV   )r{   rF   Ú	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>   rO   rM   Úget_attrÚ
fill_valuerE   TrF   c                 ó   •— | ‰v rdS dS )Nr   rN   © )rF   Úconst_nodess    €r   Úmod_partitionz,split_const_subgraphs.<locals>.mod_partition  s   ø€ Ø˜KÑ'ˆqÐ.¨QÐ.r   )Úsubmod_0Úsubmod_1zExpected GraphModule for z, got z,Could not find call_module node for const_gmrO   rM   zPlaceholder index z out of range for args (len=ú)rN   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   rT   rU   Úall_input_nodesÚissubsetr   r   ÚgetÚExprr€   rV   Úaddr   rW   r+   r   Úgetattrr1   rS   r4   r   r2   Úlenr_   rƒ   rH   rI   r]   r^   Úlocalsr   r(   r3   r)   rv   rm   ra   )r{   r|   r   rŽ   rn   Úfound_const_foldingrF   Ú
target_modrˆ   ÚsplitÚconst_mod_nameÚnon_const_mod_nameÚconst_gmÚnon_const_modÚnon_const_gmÚcall_const_gm_argsÚroot_const_gmÚph_idxrŒ   Úin_noderJ   r   r5   r‚   r‡   s                          @@r   r	   r	   Â   sÝ  ù€ ó ä�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ð /¬cõ /ô ˜ V¨]Ó;€Eà)?Ñ&€NÐ&ä�u˜nÓ-€HÜ�h¤§¡× 4Ñ 4Ô5ÜØ'¨Ð'7°v¼dÀ8»nÐ=MÐNó
ð 	
ô ˜EÐ#5°tÓ<€MØ04€LØÐ Ü˜-¬¯©×)=Ñ)=Ô>Ü Ø+Ð,>Ð+?¸vÄdÈ=ÓFYÐEZÐ[óð ð %ˆð
 ÐØ ×&Ñ&×,Ñ,ò 	PˆDØ�w‰w˜-Ó'Ü˜˜tŸ{™{¬G°LÀ$Ç+Á+Ó,NÕOð	Pð —‘×$Ñ$ò 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   Ò&Y9×ZÙ9Z	ÚZ	)T)NÚcpu)rs   Úcollections.abcr   Útypingr   Útorch.fxr   Útorch.fx.noder   Útorch.fx.passes.split_moduler   Ú__all__r   r   r   r>   r�   rQ   rW   rm   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Ø#(ñUØ�H‰H�O‰O˜eŸh™h×2Ñ2Ñ2ðUà" E§H¡H§M¡M ?°DÐ#8Ñ9¸DÑ@ðUð !ðUð ô	Ur   