Ë
    ÄÓ^j¿+  ã                   ó  — d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZ d dlmZ  ed¬	«      d
edefd„«       Z ed¬	«      dedefd„«       Z ed¬	«      	 dddœde	d
ededeeedz  f   dz  dedee	eedf   eedf   f   fd„«       Z ed¬	«      	 dde	de	deedf   deedf   dedz  de	fd„«       Z ed¬	«      de	d
eddfd„«       Z ed¬	«      	 	 d de	deeeedz  f      dedede	f
d„«       Zy)!é    N)Úcompatibility)ÚGraph)ÚGraphModule)ÚNode)Úlegalize_graphÚNodeListÚNodeSet)Úlift_subgraph_as_moduleF)Úis_backward_compatibleÚnodesÚreturnc                 óL  — t         j                  | d«      }t        | «      D ��ci c]  \  }}||“Œ
 }}}g }| D ]I  }|j                  D ]  }||v sŒ||xx   dz  cc<   Œ ||   dk(  sŒ/t	        j
                  |||   |f«       ŒK g }|rst	        j                  |«      \  }}|j                  |«       |j                  D ]8  }||v sŒ||xx   dz  cc<   ||   dk(  sŒt	        j
                  |||   |f«       Œ: |rŒst        | «      t        |«      k7  rt        d«      ‚|S c c}}w )Nr   é   z@topological sorted nodes doesn't have same length as input nodes)ÚdictÚfromkeysÚ	enumerateÚall_input_nodesÚheapqÚheappushÚheappopÚappendÚusersÚlenÚAssertionError)	r   Úindegree_mapÚiÚnodeÚpositionÚ
candidatesÚnÚsorted_nodesÚ_s	            úl/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/utils/fuser_utils.pyÚ	topo_sortr$      sH  € ô
 —=‘= ¨Ó*€LÜ'0°Ó'7×8™G˜A˜t��a‘Ð8€HÑ8Ø)+€Jàò ?ˆØ×%Ñ%ò 	(ˆAØ�LÒ Ø˜TÓ" aÑ'Ô"ð	(ð ˜Ñ Ó"Ü�N‰N˜:¨°©¸Ð'=Õ>ð?ð  €LÙ
Ü—-‘- 
Ó+‰ˆˆ4Ø×Ñ˜DÔ!à—‘ò 	AˆAØ�LÒ Ø˜Q“ 1Ñ$“Ø ‘? aÓ'Ü—N‘N :°¸±¸QÐ/?Õ@ð		Aò	 ô ˆ5ƒz”S˜Ó&Ò&ÜØNó
ð 	
ð Ðùó5 9s   ¥D Ú	partitionc                 ó²   ‡— t        | «      Šg }‰D ])  }|j                  D ]  }|‰vsŒ|j                  |«       Œ Œ+ dt        dt        fˆfd„} ||«      ryy)NÚ
root_nodesr   c                 óÂ   •— t        «       }| }|rP|j                  «       }|j                  |«       |‰v ry|j                  D ]  }||v rŒ|j	                  |«       Œ |rŒPy)NTF)ÚsetÚpopÚaddr   r   )r'   ÚvisitedÚqueueÚcurrentÚ	user_nodeÚpartition_sets        €r#   Úbfs_find_cyclez*validate_partition.<locals>.bfs_find_cycleA   sn   ø€ ô ›5ˆð
 %ˆÙØ—i‘i“kˆGØ�K‰K˜Ô Ø˜-Ñ'ð Ø$Ÿ]™]ò (�	Ø Ñ'ØØ—‘˜YÕ'ð(ò ð ó    FT)r)   r   r   r   Úbool)r%   Úoutputsr   r/   r1   r0   s        @r#   Úvalidate_partitionr5   0   sl   ø€ ô
 ˜	“N€Mà€GØò *ˆØŸ™ò 	*ˆIØ Ò-à—‘˜yÕ)ñ	*ð*ð¤8ð ´õ ñ4 �gÔØàr2   ©Úalways_return_tupleÚgmÚmodule_nameÚpartition_lookup_tabler7   .c                ó¢  ‡‡‡‡— |D ]ˆ  }|j                   j                  | urt        |› d| j                  «       › �«      ‚|j                  rt        |› d�«      ‚|| j                   j
                  vsŒlt        |› d| j                  «       › �«      ‚ t        |«      st        d«      ‚‰€t        j                  |«      Št        «       Ši Ši Šdt        dt        fˆˆˆˆfd„}|D ]  }‰j                  ||«      }|‰|<   Œ i }|D ]   }|j                  D ]  }	|	‰vsŒ‰|   ||<   Œ Œ" t        |j                  «       «      }
|r‰j                  |
«       n$‰j                  t!        |
«      dk(  r|
d	   n|
«       ‰j#                  «        t%        | ‰d
|¬«      \  }}t        ‰j'                  «       «      }t        |j'                  «       «      }|||fS )a  
    Fuse nodes in graph_module into a GraphModule.

    Args:
        gm (GraphModule): target graph_module

        nodes (List[Node]): list of nodes in `gm` to fuse, where the node must be topologically sorted

        module_name: class name for the fused GraphModule

        partition_lookup_table (Optional[Dict[Node, None]]): optional dict of nodes to speed up lookup

        always_return_tuple (bool): whether to always return a tuple, even if there is only one output

    Returns:
        fused_gm (GraphModule): fused graph module, where its node is a copy of `nodes` in `gm`

        original_inputs (Tuple[Node, ...]): input nodes to `nodes` in original `gm`

        original_outputs (Tuple[Node, ...]): consumer nodes of `nodes` in original `gm`

    z* doesn't belong to passed in graph module z# has been removed from owning graphz is not found in graph module z*Invalid partition, found dependency cyclesÚxr   c                 óè   •— | j                   dk(  r	 | ‰v r‰|    S | ‰vrP‰j                  | j                  | j                  ¬«      }t	        j                  | j
                  «      |_        |‰| <   ‰|    S )NÚget_attr)Ú	type_expr)ÚopÚplaceholderÚnameÚtypeÚcopyÚmeta)r<   Úplaceholder_nodeÚnode_mapÚnode_to_placeholderr:   Úsubgraphs     €€€€r#   Úremap_inputsz)fuse_as_graphmodule.<locals>.remap_inputsŸ   s|   ø€ Ø�4‰4�:Òð àÐ&Ñ&ð ˜A‘;ÐàÐ'Ñ'à'×3Ñ3°A·F±FÀaÇfÁfÐ3ÓMÐä$(§I¡I¨a¯f©fÓ$5ÐÔ!Ø%5Ð Ñ"à" 1Ñ%Ð%r2   r   r   Ú )Ú	comp_nameÚ
class_name)ÚgraphÚowning_moduler   Ú	_get_nameÚ_erasedÚ_find_nodes_lookup_tabler5   r   r   r   r   Ú	node_copyr   ÚtupleÚvaluesÚoutputr   Úlintr
   Úkeys)r8   r   r9   r:   r7   r   rJ   Únew_nodeÚoutput_mappingr/   ÚoutsÚfused_gmr"   Úoriginal_inputsÚoriginal_outputsrG   rH   rI   s      `           @@@r#   Úfuse_as_graphmoduler_   a   s  û€ ðD ò 
ˆØ�:‰:×#Ñ#¨2Ñ-Ü Ø�&ÐBÀ2Ç<Á<Ã>ÐBRÐSóð ð �<Š<Ü  D 6Ð)LÐ!MÓNÐNØ�r—x‘x×8Ñ8Ò8Ü Ø�&Ð6°r·|±|³~Ð6FÐGóð ð
ô ˜eÔ$ÜÐIÓJÐJð Ð%Ü!%§¡¨uÓ!5Ðä‹w€Hð 	ð ð "$€Hð&œð &¤÷ &ð &ð* ò "ˆØ×%Ñ% d¨LÓ9ˆØ!ˆ�Šð"ð
 (*€Nàò 6ˆØŸ™ò 	6ˆIØÐ 6Ò6à'/°¡~�˜tÒ$ñ	6ð6ô �×&Ñ&Ó(Ó)€Dáà�‰˜Õð 	�‰¤3 t£9°¢>˜˜Qš°tÔ<ð ‡M�M„Oä)Ø
ˆH ¨{ô�K€Hˆaô
 ).Ð.A×.FÑ.FÓ.HÓ(I€Oô */¨~×/BÑ/BÓ/DÓ)EÐà�_Ð&6Ð6Ð6r2   Úsub_gmÚorig_inputsÚorig_outputsÚinsertion_pointc                 ó„  — |j                   j                  }| j                  ||«       |€9t        | j                  j
                  «      D ]
  }||v sŒ|} n |€t        d«      ‚| j                  j                  |«      5  | j                  j                  ||d ¬«      }|j                  j                  «       }d d d «       |råj                  }	| j                  j                  |	«      5  t        |«      dk(  r4t        j                  d   t        «      s|d   j!                  |d¬«       nqt#        |«      D ]D  \  }
}t$        j&                  j)                  |«      |
   j*                  }|j!                  |d¬«       ŒF t        d„ |D «       «      |j,                  d<   d d d «       | S | S # 1 sw Y   ŒòxY w# 1 sw Y   | S xY w)	Nz‰Cannot determine insertion point: no insertion_point provided and orig_outputs is empty. Pass the last partition node as insertion_point.)ÚargsÚkwargsr   r   T)Úpropagate_metac              3   óT   K  — | ]   }|j                   j                  d d«      –— Œ" y­w)ÚvalN)rE   Úget)Ú.0Úorig_outputs     r#   ú	<genexpr>zinsert_subgm.<locals>.<genexpr>  s(   è ø€ ò 0Ø:E�K×$Ñ$×(Ñ(¨°×5ñ0ùs   ‚&(ri   )Ú	__class__Ú__name__Úadd_submoduleÚreversedrN   r   r   Úinserting_afterÚcall_moduleÚoutput_nodeÚnextÚinserting_beforer   Ú
isinstancere   rT   Úreplace_all_uses_withr   ÚtorchÚfxÚProxyr   rE   )r8   r`   ra   rb   rc   Úsubmodule_namer   Úmodule_nodert   Ú	next_noder   rl   Ú	proxy_outs                r#   Úinsert_subgmr€   Û   s½  € ð ×%Ñ%×.Ñ.€NØ×Ñ�^ VÔ,ð ÐÜ˜RŸX™XŸ^™^Ó,ò 	ˆDØ�|Ò#Ø"&�Ùð	ð Ð"Ü ðZóð ð 
�‰×	!Ñ	! /Ó	2ñ 1Ø—h‘h×*Ñ*Ø °Tð +ó 
ˆð —l‘l×.Ñ.Ó0ˆ÷	1ñ Ø×$Ñ$ˆ	Ø�X‰X×&Ñ& yÓ1ñ 	Ü�<Ó  AÒ%¬j¸×9IÑ9IÈ!Ñ9LÌeÔ.Tà˜Q‘×5Ñ5°kÐRVÐ5ÕWä&/°Ó&=ò V‘N�A�{ä %§¡§¡¨{Ó ;¸AÑ >× CÑ C�IØ×5Ñ5°iÐPTÐ5ÕUðVô
 +0ñ 0ØIUô0ó +�× Ñ  Ñ'÷	ð €Iˆ2€I÷/1ð 1ú÷	ð €Iús   Á?9F)Ã)B4F5Æ)F2Æ5F?c                 óZ   — t        |«      D ]  }| j                  j                  |«       Œ y ©N)rq   rN   Ú
erase_node)r8   r   r   s      r#   Úerase_nodesr„     s)   € ô ˜“ò "ˆØ
�‰×Ñ˜DÕ!ñ"r2   Ú
partitionsÚprefixc           	      ón  — t        |«      D ]Y  \  }}t        t        |«      «      }|t        |«      z   }t	        | ||||¬«      \  }}	}
t        | ||	|
|d   «       t        | |«       Œ[ t        j                  j                  j                  j                  | «       | j                  j                  «        | S )Nr6   éÿÿÿÿ)r   r$   ÚlistÚstrr_   r€   r„   ry   rz   ÚpassesÚtools_commonÚstable_topological_sortrN   rW   )r8   r…   r†   r7   Úpartition_idr%   r!   r|   r`   ra   rb   s              r#   Úfuse_by_partitionsr�     s«   € ô $-¨ZÓ#8ò &Ñˆ�iÜ ¤ i£Ó1ˆà¤# lÓ"3Ñ3ˆÜ,?ØØØØØ 3ô-
Ñ)ˆ�˜\ô 	�R˜ ¨l¸LÈÑ<LÔMä�B˜Õ%ð&ô  
‡H�H‡O�O× Ñ ×8Ñ8¸Ô<Ø‡H�H‡M�M„Oà€Ir2   r‚   )Úfused_F)rD   r   Útorch.fxry   Útorch.fx._compatibilityr   Útorch.fx.graphr   Útorch.fx.graph_moduler   Útorch.fx.noder   Útorch.fx.passes.tools_commonr   r   r	   Útorch.fx.passes.utilsr
   r$   r3   r5   rŠ   r   ÚintrT   r_   r€   r„   r‰   r�   © r2   r#   ú<module>rš      s  ðÛ Û ã Ý 1Ý  Ý -Ý ß JÑ JÝ 9ñ  eÔ,ð�Xð  (ò ó -ðñD  eÔ,ð- (ð -¨tò -ó -ð-ñ`  eÔ,ð
 =Að	v7ð !&òv7Øðv7àðv7ð ðv7ð !  s¨T¡zÐ!1Ñ2°TÑ9ð	v7ð ðv7ð ˆ;˜˜d C˜iÑ(¨%°°c°	Ñ*:Ð:Ñ;òv7ó -ðv7ñr  eÔ,ð $(ñ/Øð/àð/ð �t˜S�yÑ!ð/ð ˜˜c˜	Ñ"ð	/ð
 ˜D‘[ð/ð ò/ó -ð/ñd  eÔ,ð"�Kð "¨ð "°Tò "ó -ð"ñ  eÔ,ð Ø %ñ	Øðà�T˜$  d¡
Ð*Ñ+Ñ,ðð ðð ð	ð
 òó -ñr2   