Ë
    f^(hë%  ã                   óô  — 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 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f      dedeeeedf   eedf   f   fd„«       Z e	d
¬«      dededeedf   deedf   fd„«       Z e	d
¬«      dedefd„«       Z e	d
¬«      	 	 d!dedeeedf      dededef
d„«       Z y)"é    N)ÚSimpleQueue)ÚOptional)Úcompatibility)ÚGraph)ÚGraphModule)ÚNode)Úlegalize_graphÚNodeListÚNodeSet)Úlift_subgraph_as_moduleF)Úis_backward_compatibleÚnodesÚreturnc                 ó  — t         j                  | d«      }t        «       }| D ]?  }|j                  D ]  }||v sŒ||xx   dz  cc<   Œ ||   dk(  sŒ/|j	                  |«       ŒA g }|j                  «       so|j                  «       }|j                  |«       |j                  D ].  }||v sŒ||xx   dz  cc<   ||   dk(  sŒ|j	                  |«       Œ0 |j                  «       sŒot        | «      t        |«      k(  sJ d«       ‚|S )Nr   é   z@topological sorted nodes doesn't have same length as input nodes)
ÚdictÚfromkeysr   Úall_input_nodesÚputÚemptyÚgetÚappendÚusersÚlen)r   Úindegree_mapÚ
candidatesÚnodeÚnÚsorted_nodess         ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/passes/utils/fuser_utils.pyÚ	topo_sortr!      s"  € ô —=‘= ¨Ó*€LÜ)›m€Jàò !ˆØ×%Ñ%ò 	(ˆAØ�LÒ Ø˜TÓ" aÑ'Ô"ð	(ð ˜Ñ Ó"Ø�N‰N˜4Õ ð!ð  €LØ×ÑÔ Ø�~‰~ÓˆØ×Ñ˜DÔ!à—‘ò 	&ˆAØ�LÒ Ø˜Q“ 1Ñ$“Ø ‘? aÓ'Ø—N‘N 1Õ%ð		&ð	 ×ÑÕ ô ˆu‹:œØóò ð JàIóJð ð Ðó    Ú	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_cycle?   sn   ø€ ô ›5ˆð
 %ˆÙØ—i‘i“kˆGØ�K‰K˜Ô Ø˜-Ñ'ð Ø$Ÿ]™]ò (�	Ø Ñ'ØØ—‘˜YÕ'ð(ò ð r"   FT)r'   r   r   r
   Úbool)r#   Úoutputsr   r-   r/   r.   s        @r    Úvalidate_partitionr2   .   sl   ø€ ô
 ˜	“N€Mà€GØò *ˆØŸ™ò 	*ˆIØ Ò-à—‘˜yÕ)ñ	*ð*ð¤8ð ´õ ñ4 �gÔØàr"   ©Úalways_return_tupleÚgmÚmodule_nameÚpartition_lookup_tabler4   .c                óh  ‡‡‡‡— |D ]|  }|j                   j                  | u sJ |› d| j                  «       › �«       ‚|j                  r
J |› d�«       ‚|| j                   j                  v rŒdJ |› d| j                  «       › �«       ‚ t        |«      sJ d«       ‚‰€t        j                  |«      Št        «       Ši Ši Šˆˆˆˆ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 cyclesc                 óè   •— | 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)ÚxÚplaceholder_nodeÚnode_mapÚnode_to_placeholderr7   Ú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Ñ%Ð%r"   r   r   Ú )Ú	comp_nameÚ
class_name)ÚgraphÚowning_moduleÚ	_get_nameÚ_erasedÚ_find_nodes_lookup_tabler2   r   r   r   Ú	node_copyr   ÚtupleÚvaluesÚoutputr   Úlintr   Úkeys)r5   r   r6   r7   r4   r   rG   Únew_nodeÚoutput_mappingr-   ÚoutsÚfused_gmÚ_Úoriginal_inputsÚoriginal_outputsrD   rE   rF   s      `           @@@r    Úfuse_as_graphmoduler]   _   só  û€ ðD ò Cˆà�J‰J×$Ñ$¨Ñ*ð	OàˆVÐ=¸b¿l¹l»nÐ=MÐNó	OØ*à—<’<ÐM D 6Ð)LÐ!MÓMÐà�B—H‘H×5Ñ5Ò5ð	CàˆVÐ1°"·,±,³.Ð1AÐBó	CØ5ðCô ˜eÔ$ÐRÐ&RÓRÐ$ð Ð%Ü!%§¡¨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Ð6r"   Úsub_gmÚorig_inputsÚorig_outputsc                 ó.  — |j                   j                  }| j                  ||«       | j                  j	                  ||d ¬«      }|j                  j                  «       }t        |«      dk(  r5t        |j                  d   t        «      s|d   j                  |d¬«       | S t        |«      D ]D  \  }}t        j                  j                  |«      |   j                  }	|j                  |	d¬«       ŒF t        d„ |D «       «      |j                   d<   | S )N)ÚargsÚkwargsr   r   T)Úpropagate_metac              3   óT   K  — | ]   }|j                   j                  d d«      –— Œ" y­w)ÚvalN)rA   r   )Ú.0Úorig_outputs     r    ú	<genexpr>zinsert_subgm.<locals>.<genexpr>í   s(   è ø€ ò (
Ø2=ˆK×Ñ× Ñ  ¨×-ñ(
ùs   ‚&(rf   )Ú	__class__Ú__name__Úadd_submodulerK   Úcall_moduleÚoutput_noder   Ú
isinstancerb   rQ   Úreplace_all_uses_withÚ	enumerateÚtorchÚfxÚProxyr   rA   )
r5   r^   r_   r`   Úsubmodule_nameÚmodule_nodern   Úirh   Ú	proxy_outs
             r    Úinsert_subgmry   Õ   s  € ð ×%Ñ%×.Ñ.€NØ×Ñ�^ VÔ,ð —(‘(×&Ñ& ~¸KÐPTÐ&ÓU€Kà—,‘,×*Ñ*Ó,€KÜ
ˆ<Ó˜AÒ¤j°×1AÑ1AÀ!Ñ1DÄeÔ&Là�Q‰×-Ñ-¨kÈ$Ð-ÔOð €Iô (¨Ó5ò 	N‰NˆAˆ{äŸ™Ÿ™ {Ó3°AÑ6×;Ñ;ˆIØ×-Ñ-¨iÈÐ-ÕMð	Nô
 #(ñ (
ØAMô(
ó #
ˆ×Ñ˜Ñð €Ir"   c                 óZ   — t        |«      D ]  }| j                  j                  |«       Œ y ©N)ÚreversedrK   Ú
erase_node)r5   r   r   s      r    Úerase_nodesr~   ó   s)   € ô ˜“ò "ˆØ
�‰×Ñ˜DÕ!ñ"r"   Ú
partitionsÚprefixc                 óâ   — t        |«      D ]U  \  }}t        t        |«      «      }|t        |«      z   }t	        | ||||¬«      \  }}	}
t        | ||	|
«       t        | |«       ŒW t        | «       | S )Nr3   )rq   r!   ÚlistÚstrr]   ry   r~   r	   )r5   r   r€   r4   Úpartition_idr#   r   ru   r^   r_   r`   s              r    Úfuse_by_partitionsr…   ú   sƒ   € ô $-¨ZÓ#8ò &Ñˆ�iÜ ¤ i£Ó1ˆà¤# lÓ"3Ñ3ˆÜ,?ØØØØØ 3ô-
Ñ)ˆ�˜\ô 	�R˜ ¨lÔ;ä�B˜Õ%ð&ô" �2Ôà€Ir"   r{   )Úfused_F)!r@   r+   r   Útypingr   Ú	_OptionalÚtorch.fxrr   Ú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!   r0   r2   rƒ   r   rQ   r]   ry   r~   r‚   r…   © r"   r    ú<module>r‘      sÞ  ðã Ý Ý (ã Ý 1Ý  Ý -Ý ß JÑ JÝ 9ñ  eÔ,ð�Xð  (ò ó -ðñ<  eÔ,ð- (ð -¨tò -ó -ð-ñ`  eÔ,ð
 ;?ð	r7ð !&òr7Øðr7àðr7ð ðr7ð & d¨4°¨:Ñ&6Ñ7ð	r7ð ðr7ð ˆ;˜˜d C˜iÑ(¨%°°c°	Ñ*:Ð:Ñ;òr7ó -ðr7ñj  eÔ,ðØðàðð �t˜S�yÑ!ðð ˜˜c˜	Ñ"ò	ó -ðñ:  eÔ,ð"�Kð "¨ò "ó -ð"ñ  eÔ,ð Ø %ñ	Øðà�T˜$ ˜*Ñ%Ñ&ðð ðð ð	ð
 òó -ñr"   