Ë
    f^(h0  ã                   óL  — d dl mZ d dlmZ d dlmZmZ  G d„ d«      Z G d„ de«      Z G d„ d	e«      Z	 G d
„ de«      Z
 G d„ de«      Z G d„ de«      Zdedee   defd„Zdedeee	f   de
fd„Zdee   deee	f   deee
f   fd„Zdededefd„Zdee   deee
f   defd„Zy) é    )ÚEnum)Ú
NamedTuple)Úmap_argÚNodec                   ó8   — e Zd ZdZdeddfd„Zd„ Zd„ Zd„ Zd	„ Z	y)
Ú	Partitionz—Partition class contains all the information about an individual partition.
    It also provides necessary methods for manipulation the partition.
    Úpartition_idÚreturnNc                 ó–   — t        «       | _        || _        t        «       | _        t        «       | _        d| _        d| _        g | _        y )Néÿÿÿÿr   )ÚsetÚnodesr	   ÚparentsÚchildrenÚ	bfs_levelÚused_mem_bytesÚlogical_device_ids)Úselfr	   s     úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/experimental/partitioner_utils.pyÚ__init__zPartition.__init__   s<   € Ü #£ˆŒ
Ø(ˆÔÜ'*£uˆŒÜ(+«ˆŒØ ˆŒØ#$ˆÔØ-/ˆÕó    c                 ó,   — t        | j                  «      S ©N)Ústrr	   )r   s    r   Ú__str__zPartition.__str__   s   € Ü�4×$Ñ$Ó%Ð%r   c                 ó†   — d| _         | j                  D ]+  }| xj                   t        || j                  «      z  c_         Œ- y )Nr   )r   r   Úget_extra_size_of)r   Únodes     r   Úrecalculate_mem_sizezPartition.recalculate_mem_size   s<   € ØˆÔØ—J‘Jò 	GˆDØ×ÒÔ#4°T¸4¿:¹:Ó#FÑFÖñ	Gr   c                 ó@  — i }t        |j                  |j                  «       t        |j                  |j                  «       |D ],  }|j                  dv sŒ| j
                  j                  |«       Œ. | j
                  j                  |«       | j                  «        y )N>   Úget_attrÚplaceholder)r   ÚargsÚ
setdefaultÚkwargsÚopr   Úaddr   )r   r   Úinput_nodesÚns       r   Úadd_nodezPartition.add_node   sy   € Ø(*ˆÜ�—	‘	˜;×1Ñ1Ô2Ü�—‘˜[×3Ñ3Ô4àò 	"ˆAØ�t‰tÐ2Ò2Ø—
‘
—‘˜qÕ!ð	"ð 	�
‰
�‰�tÔØ×!Ñ!Õ#r   c                 óž  ‡ — |‰ j                   v r¾‰ j                   j                  |«       i }t        |j                  |j                  «       t        |j
                  |j                  «       |D ]K  }t        ˆ fd„|j                  D «       «      sŒ"|j                  dv sŒ1‰ j                   j                  |«       ŒM ‰ j                  «        y y )Nc              3   ó:   •K  — | ]  }|‰j                   v–— Œ y ­wr   )r   )Ú.0r)   r   s     €r   ú	<genexpr>z(Partition.remove_node.<locals>.<genexpr>5   s   øè ø€ ò Ø,-�A˜TŸZ™ZÔ'ñùs   ƒ>   r!   r"   )
r   Úremover   r#   r$   r%   ÚallÚusersr&   r   )r   r   r(   Ú
input_nodes   `   r   Úremove_nodezPartition.remove_node)   s¬   ø€ à�4—:‘:ÑØ�J‰J×Ñ˜dÔ#à,.ˆKÜ�D—I‘I˜{×5Ñ5Ô6Ü�D—K‘K ×!7Ñ!7Ô8ð *ò 2�
Üó Ø1;×1AÑ1Aôõ à —m‘mÐ'BÒBØ—J‘J×%Ñ% jÕ1ð	2ð
 ×%Ñ%Õ'ð r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r   r   r*   r3   © r   r   r   r      s/   „ ñð0 Sð 0¨Tó 0ò&òGò
	$ó(r   r   c                   ó,   — e Zd ZU eed<   eed<   eed<   y)ÚDeviceÚnameÚavailable_mem_bytesÚ
logical_idN)r4   r5   r6   r   Ú__annotations__r8   r9   r   r   r;   r;   <   s   … Ø
ƒIØÓØ„Or   r;   c                   ó"   — e Zd ZU eed<   eed<   y)ÚNodeLatencyÚmem_latency_secÚcomputer_latency_secN©r4   r5   r6   Úfloatr?   r9   r   r   rA   rA   B   s   … àÓàÔr   rA   c                   ó,   — e Zd ZU eed<   eed<   eed<   y)ÚPartitionLatencyrB   rC   Úoverall_latency_secNrD   r9   r   r   rG   rG   I   s   … àÓàÓàÔr   rG   c                   ó    — e Zd ZdZdZdZdZdZy)ÚPartitionModer   é   é   é   é   N)r4   r5   r6   Ú
size_basedÚ	sparse_nnÚ
cost_awareÚkl_basedÚ	aot_basedr9   r   r   rJ   rJ   R   s   „ Ø€JØ€IØ€JØ€HØ�Ir   rJ   c                   óª   — e Zd ZU ee   ed<   ej                  Zeed<   dZ	e
ed<   i Zeeef   ed<   i Zeeef   ed<   i Zeeee   f   ed<   dZeed	<   y
)ÚPartitionerConfigÚdevicesÚmodeç        Útransfer_rate_bytes_per_secÚnode_to_latency_mappingÚnode_to_partition_mappingÚ#partition_to_logical_device_mappingFÚsaturate_hostN)r4   r5   r6   Úlistr;   r?   rJ   rO   rW   rY   rE   rZ   Údictr   rA   r[   r8   r\   r]   Úboolr9   r   r   rU   rU   Z   sq   … Ø�&‰\ÓØ'×2Ñ2€Dˆ-Ó2Ø),Ð Ó,Ø79Ð˜T $¨Ð"3Ñ4Ó9Ø13Ð˜t D¨# I™Ó3Ø@BÐ'¨¨c°4¸±9¨nÑ)=ÓBà€M�4Ôr   rU   r   r   r
   c                 óJ  — i }t        | j                  |j                  «       t        | j                  |j                  «       d}|D ]0  }||vsŒt	        |dd«      }|r||j
                  z  }Œ't        d«      ‚ t	        | dd«      }|r||j                  z  }|S t        d«      ‚)zƒGiven a node and a set of nodes,
    this function return the extra size that needed
    if this node is included in this set.
    r   Ú
size_bytesNznode has no size_bytes attr)r   r#   r$   r%   ÚgetattrÚoutput_sizeÚRuntimeErrorÚ
total_size)r   r   r(   Útotal_size_of_input_nodesr)   rb   s         r   r   r   e   s·   € ð %'€KÜˆD�I‰I�{×-Ñ-Ô.ÜˆD�K‰K˜×/Ñ/Ô0à !ÐØò Bˆà�EŠ>Ü   L°$Ó7ˆJÙØ)¨Z×-CÑ-CÑCÑ)ä"Ð#@ÓAÐAðBô ˜˜|¨TÓ2€JÙØ! Z×%:Ñ%:Ñ:Ð!ð %Ð$ô Ð8Ó9Ð9r   Ú	partitionrZ   c           	      óþ   ‡ ‡‡— dt         dt        t           fd„}dt        dt        fˆˆˆ fd„Š |‰ «      }t        ddd¬«      }|D ]3  } ‰|t        ddd¬«      «      }|j                  |j                  kD  sŒ2|}Œ5 |S )zVGiven a partition and its nodes' latency, return a PartitionLatency for this partitionrh   r
   c                 ó  ‡ — g }‰ j                   D ]y  }|j                  dv rŒi }t        |j                  |j                  «       t        |j
                  |j                  «       t        ˆ fd„|D «       «      rŒi|j                  |«       Œ{ |S )z>Given a partition, return a list of nodes on the top bfs level>   r!   r"   c              3   óZ   •K  — | ]"  }|‰j                   v xr |j                  d v–— Œ$ y­w)>   r!   r"   N)r   r&   )r-   r)   rh   s     €r   r.   zFget_latency_of_one_partition.<locals>.get_top_nodes.<locals>.<genexpr>“   s4   øè ø€ ò àð �Y—_‘_Ð$ÒP¨¯©Ð5PÐ)PÓPñùs   ƒ(+)r   r&   r   r#   r$   r%   ÚanyÚappend)rh   Ú	top_nodesr   r(   s   `   r   Úget_top_nodesz3get_latency_of_one_partition.<locals>.get_top_nodes†   s‡   ø€ à "ˆ	Ø—O‘Oò 	'ˆDà�w‰wÐ5Ñ5ØØ,.ˆKÜ�D—I‘I˜{×5Ñ5Ô6Ü�D—K‘K ×!7Ñ!7Ô8ô ó à$ôõ ð × Ñ  Õ&ð	'ð Ðr   r   c           	      óÔ  •— ‰|    }|j                   t        |j                  |j                  «      z   }|j                  |j                  z   }|j                  |j                  z   }t	        | j
                  «      j                  ‰j                  «      }|rGt        ddd¬«      }|D ]2  } ‰
|t        |||«      «      }	|	j                   |j                   kD  sŒ1|	}Œ4 |S t        |||«      S )zyGiven a top node of a partition, this function returns
        the latency of the critical path in the partition
        rX   ©rB   rC   rH   )	rH   ÚmaxrC   rB   r   r1   Úintersectionr   rG   )r   Úpartition_latencyÚnode_latencyrH   rB   rC   r1   Úmax_latencyr)   Únew_partition_latencyÚ
dfs_helperrZ   rh   s             €€€r   rx   z0get_latency_of_one_partition.<locals>.dfs_helperš   s  ø€ ð /¨tÑ4ˆà/×CÑCÄcØ×-Ñ-¨|×/KÑ/KóG
ñ 
Ðð
 ×-Ñ-°×0LÑ0LÑLð 	ð
 ×2Ñ2°\×5VÑ5VÑVð 	ô �D—J‘J“×,Ñ,¨Y¯_©_Ó=ˆÙÜ*Ø #¸#ÐSVôˆKð ò 8�á(2ØÜ$Ø'Ð)=Ð?Róó)Ð%ð *×=Ñ=Ø!×5Ñ5ó6ð #8‘Kð8ð ÐäØÐ1Ð3Fó
ð 	
r   rX   rq   )r   r^   r   rG   rH   )rh   rZ   ro   rn   Úcritical_path_latencyr   rt   rx   s   ``     @r   Úget_latency_of_one_partitionrz   �   s¥   ú€ ð
¤ð ¬t´D©zó ð((
œð (
Ô5E÷ (
ñX ˜iÓ(€IÜ,Ø°#È3ôÐð ò 6ˆÙ&ØÜØ #¸#ÐSVôó
Ðð ×1Ñ1Ø#×7Ñ7ó8ð %6Ñ!ð6ð !Ð r   Ú
partitionsc                 ó:   — i }| D ]  }t        ||«      }|||<   Œ |S )zŽGiven all the partitions and node_to_latency_mapping dictionary,
    return a mapping dictionary of each partition to its overall latency
    )rz   )r{   rZ   Úpartition_to_latency_mappingrh   rt   s        r   Ú get_partition_to_latency_mappingr~   Ú   sD   € ð GIÐ àò Dˆ	Ü8ØÐ.ó
Ðð 3DÐ$ YÒ/ð	Dð
 (Ð'r   Úparent_partitionÚchild_partitionrY   c                 óÎ  — | j                   g k7  r)|j                   g k7  r| j                   |j                   k(  ryd}t        «       }|j                  D ]Ž  }i }t        |j                  |j
                  «       t        |j                  |j
                  «       |D ]E  }|| j                  v sŒ||vsŒt        |dd«      }|�||j                  z  }|j                  |«       ŒG Œ� ||z  S )zfGiven two partitions (parent and child),
    calculate the communication latency between the two.
    rX   r   rb   N)
r   r   r   r   r#   r$   r%   rc   rd   r'   )	r   r€   rY   Ú	comm_sizeÚvisited_nodesr   r(   r)   rb   s	            r   Úget_comm_latency_betweenr„   ê   sì   € ð 	×+Ñ+¨rÒ1Ø×.Ñ.°"Ò4Ø×/Ñ/°?×3UÑ3UÒUàà€Iä“E€Mð
  ×%Ñ%ò 	%ˆØ(*ˆÜ�—	‘	˜;×1Ñ1Ô2Ü�—‘˜[×3Ñ3Ô4Øò 	%ˆAØÐ$×*Ñ*Ò*¨q¸Ò/EÜ$ Q¨°dÓ;�
ØÐ)Ø ×!7Ñ!7Ñ7�IØ×!Ñ! !Õ$ñ	%ð		%ð Ð2Ñ2Ð2r   r}   c                 óÀ   ‡‡‡— dt         dt        dt        fˆˆˆfd„Šdt        t            dt        t            fd„} || «      }d}|D ]  } ‰|d«      }||kD  sŒ|}Œ |S )zŽGiven all partitions in a graph, find the critical path among all partitions
    and return its latency as the latency of the whole graph
    rh   Úlatency_so_far_secr
   c                 ó°   •— |‰|    j                   z  }| j                  r6d}| j                  D ]#  }t        | |‰«      } ‰|||z   «      }||kD  sŒ"|}Œ% |S |S )zJThis function helps to recursively get the latency of a path of partitionsrX   )rH   r   r„   )	rh   r†   Úmax_latency_secÚchildÚcomm_latency_secÚnew_latency_secrx   r}   rY   s	         €€€r   rx   z4get_latency_of_partitioned_graph.<locals>.dfs_helper  sŽ   ø€ ð 	Ð:Øñ
ç
Ñ
ñ	Ðð ×ÒØ!ˆOØ"×+Ñ+ò 	6�ä#;Ø˜uÐ&Aó$Ð ñ #-ØÐ-Ð0@Ñ@ó#�ð # _Ó4Ø&5‘Oð	6ð #Ð"Ø!Ð!r   r{   c                 ó`   — | D �cg c]  }t        |j                  «      dk(  sŒ|‘Œ }}|S c c}w )zvThis function is to return all the partitions without parents
        as the starting points of all the paths
        r   )Úlenr   )r{   rh   Útop_partitionss      r   Úget_top_partitionsz<get_latency_of_partitioned_graph.<locals>.get_top_partitions-  s=   € ð (2ö
Ø#´S¸×9JÑ9JÓ5KÈqÓ5PŠIð
ˆð 
ð Ðùò
s   …+£+rX   )r   rE   r^   )	r{   r}   rY   r�   rŽ   Úcritical_path_latency_secrh   Úlatency_secrx   s	    ``     @r   Ú get_latency_of_partitioned_graphr’     s|   ú€ ð"œið "¼Uð "Äu÷ "ð,¤t¬I¡ð ¼4Ä	¹?ó ñ (¨
Ó3€NØ #ÐØ#ò 4ˆ	Ù  ¨CÓ0ˆØÐ2Ó2Ø(3Ñ%ð4ð %Ð$r   N)Úenumr   Útypingr   Útorch.fx.noder   r   r   r;   rA   rG   rJ   rU   r   r8   r   r_   rz   r^   r~   rE   r„   r’   r9   r   r   ú<module>r–      s#  ðå Ý ç '÷1(ñ 1(ôhˆZô ô �*ô  ô�zô ô�Dô ô ˜
ô  ð%˜Dð %¨¨T©ð %°só %ð8V!ØðV!Ø37¸¸kÐ8IÑ3JðV!àóV!ðr(Ø�Y‘ð(Ø:>¸tÀ[Ð?PÑ:Qð(à	ˆ)Ð%Ð
%Ñ&ó(ð !3Øð!3àð!3ð "'ó!3ðH/%Ø�Y‘ð/%à"& yÐ2BÐ'BÑ"Cð/%ð "'ô/%r   