Ë
    f^(h_B  ã                   ó  — d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ  ej$                  e«      Zej+                  ej,                  «        G d	„ d
«      Z G d„ d«      Z G d„ d«      Zy)é    N)ÚIterableÚSequence)Úcopy)ÚOptional)ÚGraphModule)Ú_get_qualified_nameÚNode)ÚOperatorSupportBase)Úfuse_by_partitionsc                   ó\   — e Zd Z	 ddee   deee      fd„Zdefd„Z	defd„Z
defd	„Zd
„ Zy)Ú	PartitionNÚidÚnodesc                 óZ   — || _         |�t        j                  |«      | _        y i | _        y ©N)r   ÚdictÚfromkeysr   )Úselfr   r   s      ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/passes/infra/partitioner.pyÚ__init__zPartition.__init__   s&   € ð ˆŒØ-2Ð->”T—]‘] 5Ó)ˆ�
ÀBˆ�
ó    Úreturnc                 ó,   — t        | j                  «      S r   )Ústrr   ©r   s    r   Ú__repr__zPartition.__repr__   ó   € Ü�4—:‘:‹Ðr   Únodec                 ó>   — | j                   j                  |d i«       y r   )r   Úupdate©r   r   s     r   Úadd_nodezPartition.add_node   s   € Ø�
‰
×Ñ˜4 ˜,Õ'r   c                 ó   — | j                   |= y r   )r   r!   s     r   Úremove_nodezPartition.remove_node    s   € Ø�J‰J�tÑr   c                 ó,   — t        | j                  «      S r   )Úlenr   r   s    r   ÚsizezPartition.size#   r   r   )NN)Ú__name__Ú
__module__Ú__qualname__r   Úintr   r	   r   r   r   r"   r$   r'   © r   r   r   r      sS   „ àJNñGØ˜3‘-ðGØ/7¸À¹Ñ/GóGð˜#ó ð(˜Tó (ð ó ór   r   c                   óD   — e Zd Zdefd„Zdedee   fd„Zdedee   fd„Zy)Ú_DependencyViewerÚgraph_modulec                 ób  — t        j                  t        «      | _        t        j                  t        «      | _        |j
                  j                  D ]\  }|j                  D ]K  }| j                  |   j                  |«       | j                  |   j                  | j                  |   «       ŒM Œ^ t        |j
                  j                  «      D ]\  }|j                  D ]K  }| j                  |   j                  |«       | j                  |   j                  | j                  |   «       ŒM Œ^ y r   )ÚcollectionsÚdefaultdictÚsetÚ	upstreamsÚdownstreamsÚgraphr   Úall_input_nodesÚaddr    ÚreversedÚusers)r   r/   r   Ú
input_nodeÚoutput_nodes        r   r   z_DependencyViewer.__init__(   s  € Ü$×0Ñ0´Ó5ˆŒÜ&×2Ñ2´3Ó7ˆÔà ×&Ñ&×,Ñ,ò 	HˆDØ"×2Ñ2ò H�
à—‘˜tÑ$×(Ñ(¨Ô4Ø—‘˜tÑ$×+Ñ+¨D¯N©N¸:Ñ,FÕGñHð	Hô ˜\×/Ñ/×5Ñ5Ó6ò 	MˆDØ#Ÿz™zò M�à× Ñ  Ñ&×*Ñ*¨;Ô7Ø× Ñ  Ñ&×-Ñ-¨d×.>Ñ.>¸{Ñ.KÕLñMñ	Mr   r   r   c                 ó    — | j                   |   S r   )r5   r!   s     r   Údownstreams_ofz _DependencyViewer.downstreams_of8   s   € Ø×Ñ Ñ%Ð%r   c                 ó    — | j                   |   S r   )r4   r!   s     r   Úupstreams_ofz_DependencyViewer.upstreams_of;   s   € Ø�~‰~˜dÑ#Ð#r   N)	r(   r)   r*   r   r   r	   r3   r>   r@   r,   r   r   r.   r.   '   s>   „ ðM [ó Mð & 4ð &¨C°©Ió &ð$ ð $¨#¨d©)ô $r   r.   c                   ó°   — e Zd Z	 	 	 ddedededeee      deee      ddfd„Z	d	e
defd
„Zdee   fd„Z	 ddee   dedefd„Zdee   fd„Zddedefd„Zy)ÚCapabilityBasedPartitionerNr/   Úoperator_supportÚallows_single_node_partitionÚnon_compute_opsÚ!allowed_single_node_partition_opsr   c                 óz   — || _         || _        || _        |�|ng | _        |�|ng | _        t        |«      | _        y r   )r/   rC   rD   rE   rF   r.   Údependency_viewer)r   r/   rC   rD   rE   rF   s         r   r   z#CapabilityBasedPartitioner.__init__@   sS   € ð )ˆÔØ 0ˆÔØ,HˆÔ)Ø2AÐ2M™ÐSUˆÔð 1Ð<ñ .àð 	Ô.ô
 "3°<Ó!@ˆÕr   r   c                 ó|   — | j                   j                  t        | j                  j	                  «       «      |«      S r   )rC   Úis_node_supportedr   r/   Únamed_modulesr!   s     r   Ú__is_node_supportedz.CapabilityBasedPartitioner.__is_node_supportedS   s4   € Ø×$Ñ$×6Ñ6Ü�×"Ñ"×0Ñ0Ó2Ó3°Tó
ð 	
r   c           	      ó¤  ‡ ‡‡‡‡— t        j                  t        «      Ši Ši Ši }i Št        j                  «       }dt
        dt
        fˆˆˆˆˆ fd„}dt        dt        t
           fˆˆˆˆ fd„}t        j                  d«       t        ‰ j                  j                  j                  «      D ]¥  }i }‰ j                  |«      r'|‰vr#t        |«      }|||<   |‰|<    |||«       d ||<   t!        ‰j#                  «       d„ ¬	«      D ]
  \  }}d ||<   Œ t%        |j'                  «       «      }	t)        |	«      d
kD  sŒŽ|	d   }
|	d
d  D ]  } ||
|«       Œ Œ§ t        j                  d«       i }‰ j                  j                  j                  D ]�  }d}|j*                  D ]-  }|j,                  dk7  st/        |j0                  «      dk7  sŒ+d} n |sŒD‰j3                  |d «      }|j*                  D ]  }‰j3                  |d «      |k7  sŒ|||<   Œ Œƒ |j#                  «       D ]  \  }} |||«       Œ ‰ j4                  sõt        j                  d«       ddh}|j7                  t        ‰ j8                  «      «      }g }‰j#                  «       D ]™  \  }}d}|j                  D ]l  }|j,                  dk(  sŒt;        |j0                  «      sJ ‚t/        |j0                  «      |vr|d
z  }t/        |j0                  «      ‰ j<                  v sŒh|d
z  }Œn |d
k  sŒ‰|j?                  |«       Œ› |D ]  }‰|= Œ t        j                  d«       ‰j#                  «       D ]>  \  }}t        j                  d||j                  D �cg c]  }|j@                  ‘Œ c}«       Œ@ ‰jC                  «       D �cg c]  }|jE                  «       dkD  sŒ|‘Œ c}S c c}w c c}w )NÚself_idÚother_idc                 óà  •‡ ‡‡— t        ‰	‰    j                  «      Š‰j                  ‰	‰   j                  «       dt        t           fˆˆˆˆˆˆ fd„}t        «       }‰D ])  }|j
                  D ]  }|‰vsŒ|j                  |«       Œ Œ+  ||«      ry‰‰	‰    _        ‰	‰   j                  D ]  }‰ ‰|<   Œ	 ‰	‰= t        ‰
‰    ‰
‰   «      ‰
‰ <   ‰
‰= ‰‰    j                  ‰‰   «      ‰‰ <   ‰‰= y)NÚall_user_nodesc                 óÖ   •— | D ]c  }t        «       }‰
j                  j                  |«      D ]9  }|‰v r  y|‰v sŒ‰|   }||v rŒ‰	|   }‰|v s‰|v r  y|j                  |«       Œ; Œe y)NTF)r3   rH   r>   r8   )rQ   Ú	user_nodeÚvisited_partition_idsÚ	path_nodeÚpartition_idÚp_mapÚ
assignmentÚmerged_nodesrO   Úpartition_mapr   rN   s         €€€€€€r   Údfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycles   sœ   ø€ Ø!/ò D�IÜ,/«EÐ)à%)×%;Ñ%;×%JÑ%JÈ9Ó%Uò D˜	ð %¨Ñ4Ú#'ð %¨
Ò2Ø+5°iÑ+@˜Lð  ,Ð/DÑDØ (Ø$1°,Ñ$?˜EØ&¨%Ñ/°8¸uÑ3DÚ'+à1×5Ñ5°lÕCñ+DðDð4 r   FT)	r   r   r    r3   r	   r:   r8   ÚminÚunion)rN   rO   r[   rQ   r   rS   rY   rX   rZ   Úpartitions_by_idÚpartitions_orderr   s   ``    @€€€€€r   Úmaybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionn   s/  û€ äÐ 0°Ñ 9× ?Ñ ?Ó@ˆLØ×ÑÐ 0°Ñ :× @Ñ @ÔAð´C¼±I÷ ò ô< !›UˆNØ$ò 6�Ø!%§¡ò 6�IØ ¨Ò4Ø&×*Ñ*¨9Õ5ñ6ð6ñ
 # >Ô2ð ð /;Ð˜WÑ%Ô+à(¨Ñ2×8Ñ8ò +�Ø#*�
˜4Ò ð+ð ! Ð*ä(+Ø  Ñ)Ð+;¸HÑ+Eó)Ð˜WÑ%ð ! Ð*à%2°7Ñ%;×%AÑ%AØ˜hÑ'ó&ˆM˜'Ñ"ð ˜hÐ'àr   r   r   c                 ó  •— dt         dt        fˆˆˆfd„}| ‰v r‰‰|       j                  | «       |€‰j                  | «       y |‰vr |‰| <   t	        || g¬«      ‰|<    || |«       y |‰| <   ‰|   j                  | «        || |«       y )Nr   r   c                 ó>  •— | j                   D ]B  }‰j                  |d «      }|€Œ‰|   j                  |«       ‰|   j                  ‰|   «       ŒD ‰	j                  j                  | «      }|D ]+  }‰j                  |d «      }|€Œ‰|   j                  |«       Œ- y r   )r:   Úgetr8   r    rH   r@   )
r   r   rS   Ú	target_idÚupstream_nodesÚ	curr_nodeÚ	source_idrX   rZ   r   s
          €€€r   Ú_update_partition_mapzgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map²   s«   ø€ ð "&§¡ò K�IØ *§¡¨y¸$Ó ?�IØ Ñ,Ø% bÑ)×-Ñ-¨iÔ8Ø% bÑ)×0Ñ0°¸yÑ1IÕJð	Kð "&×!7Ñ!7×!DÑ!DÀTÓ!J�Ø!/ò 9�IØ *§¡¨y¸$Ó ?�IØ Ñ,Ø% iÑ0×4Ñ4°RÕ8ñ9r   )r   r   )r	   r+   r$   Úpopr   r"   )r   r   rh   rX   rZ   r^   r   s      €€€€r   Úmerge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node±   s¡   ø€ ð9¬Dð 9´c÷ 9ð$ �zÑ!Ø  ¨DÑ!1Ñ2×>Ñ>¸tÔDàˆzØ—‘˜tÕ$ØÐ+Ñ+Ø#%�
˜4Ñ Ü'0°B¸t¸fÔ'EÐ  Ñ$Ù% d¨BÕ/à#%�
˜4Ñ Ø  Ñ$×-Ñ-¨dÔ3Ù% d¨BÕ/r   zProposing partitions...c                 ó   — | d   S )Né   r,   )Úitems    r   ú<lambda>z?CapabilityBasedPartitioner.propose_partitions.<locals>.<lambda>æ   s
   € ¸4À¹7€ r   )Úkeyrl   r   z=Reassigning getitem nodes to its producer node's partition...TÚcall_functionz_operator.getitemFz'Filtering out single node partitions...ztorch.ops.aten.viewzPartitions proposed:zpartition #%s: %s)#r1   r2   r3   Ú	itertoolsÚcountr+   r	   r   ÚloggerÚdebugr9   r/   r6   r   Ú._CapabilityBasedPartitioner__is_node_supportedÚnextÚsortedÚitemsÚlistÚkeysr&   r:   Úopr   Útargetrc   rD   r]   rE   ÚcallablerF   ÚappendÚnameÚvaluesr'   )r   Únodes_orderÚnew_partition_idr`   rj   r   Úmerge_candidatesrV   Ú_Úmerge_candidates_listrN   rO   Únodes_reassignmentÚis_tuple_outputÚuserr   Údefault_non_compute_opsrE   Úpartitions_to_removeÚ	partitionÚcompute_node_countrX   rZ   r^   r_   s   `                    @@@@r   Úpropose_partitionsz-CapabilityBasedPartitioner.propose_partitionsX   s  ü€ ô )4×(?Ñ(?ÄÓ(Dˆð ')ˆ
ð ð 	ð
 ð 	ð
 ð 	ô %Ÿ?™?Ó,Ðð
A	¬3ð A	¼#÷ A	ñ A	ðF	0¤Dð 	0¬h´s©m÷ 	0ð 	0ôB 	�‰Ð.Ô/ä˜T×.Ñ.×4Ñ4×:Ñ:Ó;ò 	=ˆDà02Ðð ×'Ñ'¨Ô-°$¸jÑ2HÜ#Ð$4Ó5�Ø$0�˜DÑ!Ø1=Ð  Ñ.Ù! $¨Ô5Ø15Ð  Ñ.ô $*Ø ×&Ñ&Ó(Ñ.Bô$ò 6‘�˜að 26Ð  Ò.ð6ô
 %)Ð)9×)>Ñ)>Ó)@Ó$AÐ!ÜÐ(Ó)¨AÓ-Ø/°Ñ2�Ø 5°a°bÐ 9ò =�Hñ *¨'°8Õ<ñ	=ð3	=ô@ 	�‰ÐTÔUØ.0ÐØ×%Ñ%×+Ñ+×1Ñ1ò 	6ˆDØ"ˆOØŸ
™
ò �à—G‘G˜Ò.Ü*¨4¯;©;Ó7Ð;NÓNà&+�OÙðò Ø—^‘^ D¨$Ó/�Ø ŸJ™Jò 6�DØ!—~‘~ d¨DÓ1°RÓ7Ø35Ð*¨4Ò0ñ6ð	6ð  +×0Ñ0Ó2ò 	(‰HˆD�"Ù˜d BÕ'ð	(ð ×0Ò0Ü�L‰LÐBÔCØ'<Ð>QÐ&RÐ#Ø5×;Ñ;¼CÀ×@TÑ@TÓ<UÓVˆOØ.0Ð Ø!1×!7Ñ!7Ó!9ò 4‘��IØ%&Ð"Ø%ŸO™Oò 	4�DØ—w‘w /Ó1Ü'¨¯©Ô4Ð4Ð4Ü.¨t¯{©{Ó;À?ÑRØ.°!Ñ3Ð.ä/°·±Ó<Ø#×EÑEòFð /°!Ñ3Ñ.ð	4ð &¨Ó*Ø(×/Ñ/°Õ3ð4ð +ò )�Ø$ RÑ(ð)ô 	�‰Ð+Ô,Ø-×3Ñ3Ó5ò 	‰MˆB�	Ü�L‰LØ# RÀ	ÇÁÖ)P¸¨$¯)«)Ò)Põð	ð (8×'>Ñ'>Ó'@ö
Ø#ÀIÇNÁNÓDTÐWXÓDXŠIò
ð 	
ùò *Qùò
s   Í:OÎ(OÏOÚ
partitionsÚprefixc                 ó–   — t         j                  d«       t        | j                  |D �cg c]  }|j                  ‘Œ c}|¬«      S c c}w )NzFusing partitions...©r�   )rs   rt   r   r/   r   )r   rŽ   r�   r‹   s       r   Úfuse_partitionsz*CapabilityBasedPartitioner.fuse_partitions*  s?   € ô 	�‰Ð+Ô,ä!Ø×ÑØ.8Ö9 ˆY�_‹_Ò9Øô
ð 	
ùâ9s   ªA
c                 óL  ‡‡‡‡‡	‡
— t        | j                  «      Šdt        fˆfd„Ši Š	i Š
dt        dt         t           dt         t           fˆˆˆ	fd„Šdt        dt         t           dt         t           fˆˆˆ
fd„Š|D ]¤  }t        «       }|j                  D ]W  } ‰|«      sŒ ‰|t        |j                  «      |«      s ‰|t        |j                  «      |«      sŒG|j	                  |«       ŒY t        |«      dk7  sŒ‚|D ]  }|j                  j                  |d «       Œ  Œ¦ y )Nr   c                 óT   •— | j                   dk(  xr t        | j                  «      ‰v S )Nrp   )r{   r   r|   )r   rE   s    €r   Úis_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node9  s+   ø€ à—‘˜?Ñ*ò HÜ'¨¯©Ó4¸ÐGðr   r‹   Úremoved_nodesc                 ó²   •— | j                   dk(  s| |vs| |v ry| ‰v r‰|    S  ‰| «      r(| j                  D ]  } ‰|||«      rŒd‰| <    y d‰| <   yd‰| <   y©NÚplaceholderTF)r{   r7   )r   r‹   r–   Úinput_nr•   Úis_transparent_input_nodeÚtransparent_input_nodess       €€€r   r›   z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodeC  s�   ø€ ð —‘˜=Ò(Ø 	Ñ)Ø˜MÑ)àØÐ.Ñ.Ø.¨tÑ4Ð4Ù" 4Ô(Ø#×3Ñ3ò %�GÙ4°W¸iÈÕWØ8=Ð/°Ñ5Ù$ð%ð 15Ð'¨Ñ-ØØ,1Ð# DÑ)Ør   c                 ó²   •— | j                   dk(  s| |vs| |v ry| ‰v r‰|    S  ‰| «      r(| j                  D ]  } ‰|||«      rŒd‰| <    y d‰| <   yd‰| <   yr˜   )r{   r:   )r   r‹   r–   Úoutput_nr•   Úis_transparent_output_nodeÚtransparent_output_nodess       €€€r   rŸ   z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_nodeX  s�   ø€ ð —‘˜=Ò(Ø 	Ñ)Ø˜MÑ)àØÐ/Ñ/Ø/°Ñ5Ð5Ù" 4Ô(Ø $§
¡
ò %�HÙ5Ø  )¨]õð :?Ð0°Ñ6Ù$ð%ð 26Ð(¨Ñ.ØØ-2Ð$ TÑ*Ør   r   )r3   rE   r	   r   r8   r&   ri   )r   rŽ   r‹   r$   r   r•   r›   rŸ   rE   rœ   r    s        @@@@@@r   Úremove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_ops6  s  ý€ Ü˜d×2Ñ2Ó3ˆð	¤dõ 	ð 57ÐØ57Ð ð	Üð	Ü#&¤t¡9ð	Ü=@Ä¹Y÷	ð*	Üð	Ü#&¤t¡9ð	Ü=@Ä¹Y÷	ð. $ò 	4ˆIô &)£UˆKØ!Ÿ™ò *�Ù& tÕ,Ù-¨d´C¸	¿¹Ó4HÈ+ÔVÙ1Øœc )§/¡/Ó2°Kõð  —O‘O DÕ)ð*ô �;Ó 1Ó$Ø'ò 4�DØ—O‘O×'Ñ'¨¨dÕ3ñ4ñ	4r   c                 óL   — | j                  «       }| j                  ||¬«      }|S )Nr‘   )r�   r’   )r   r�   rŽ   Úfused_gms       r   Úpartition_and_fusez-CapabilityBasedPartitioner.partition_and_fuse�  s*   € Ø×,Ñ,Ó.ˆ
Ø×'Ñ'¨
¸6Ð'ÓBˆØˆr   )FNN)Úfused_)r(   r)   r*   r   r
   Úboolr   r   r   r   r	   ru   ry   r   r�   r’   r¡   r¤   r,   r   r   rB   rB   ?   sØ   „ ð
 .3Ø37ØEIñAà!ðAð .ðAð '+ð	Að
 " (¨3¡-Ñ0ðAð ,4°H¸S±MÑ+BðAð 
óAð&
¨ð 
°ó 
ð
P
 D¨¡Oó P
ðf :Bñ	
Ø˜y™/ð	
Ø36ð	
à	ó	
ðI4¸¸i¹ó I4ñV¨ð ¸Kô r   rB   )r1   rq   ÚloggingÚcollections.abcr   r   r   Útypingr   Útorch.fx.graph_moduler   Útorch.fx.noder   r	   Ú torch.fx.passes.operator_supportr
   Ú!torch.fx.passes.utils.fuser_utilsr   Ú	getLoggerr(   rs   ÚsetLevelÚWARNINGr   r.   rB   r,   r   r   ú<module>r±      sg   ðã Û Û ß .Ý Ý å -ß 3Ý @Ý @ð 
ˆ×	Ñ	˜8Ó	$€Ø ‡��—‘Ô  ÷ñ ÷($ñ $÷0Eò Er   