
    IЦi0                     b   S SK Jr  S SKJrJrJrJr  S SKJrJ	r	   " S S5      r
 " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      rS\	S\\	   S\4S jrS\
S\\	\4   S\4S jrS\\
   S\\	\4   S\\
\4   4S jrS\
S\
S\4S jrS\\
   S\\
\4   S\4S jrg)     )Enum)DictList
NamedTupleSet)map_argNodec                   B    \ rS rSrSrS\SS4S jrS rS rS	 r	S
 r
Srg)	Partition   zPartition class contains all the information about an individual partition.
It also provides necessary methods for manipulation the partition.
partition_idreturnNc                     [        5       U l        Xl        [        5       U l        [        5       U l        SU l        SU l        / U l        g )Nr   )setnodesr   parentschildren	bfs_levelused_mem_byteslogical_device_ids)selfr   s     f/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/experimental/partitioner_utils.py__init__Partition.__init__   s:     #
('*u(+ #$-/    c                 ,    [        U R                  5      $ N)strr   )r   s    r   __str__Partition.__str__   s    4$$%%r   c                     SU l         U R                   H+  nU =R                   [        XR                  5      -  sl         M-     g )Nr   )r   r   get_extra_size_of)r   nodes     r   recalculate_mem_sizePartition.recalculate_mem_size   s3    JJD#4T::#FF r   c                 J   0 n[        UR                  UR                  5        [        UR                  UR                  5        U H0  nUR                  S;   d  M  U R
                  R                  U5        M2     U R
                  R                  U5        U R                  5         g )N>   get_attrplaceholder)r   args
setdefaultkwargsopr   addr%   )r   r$   input_nodesns       r   add_nodePartition.add_node   st    (*		;112[334Att22

q!  	

t!!#r   c                   ^  UT R                   ;   a  T R                   R                  U5        0 n[        UR                  UR                  5        [        UR
                  UR                  5        U HV  n[        U 4S jUR                   5       5      (       d  M)  UR                  S;   d  M;  T R                   R                  U5        MX     T R                  5         g g )Nc              3   @   >#    U  H  oTR                   ;  v   M     g 7fr   )r   ).0r0   r   s     r   	<genexpr>(Partition.remove_node.<locals>.<genexpr>5   s      1AATZZ'1As   >   r(   r)   )
r   remover   r*   r+   r,   allusersr-   r%   )r   r$   r/   
input_nodes   `   r   remove_nodePartition.remove_node)   s    4::JJd#,.KDII{556DKK!7!78 *
 1;1A1A   mm'BBJJ%%j1	 *
 %%' r   )r   r   r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__intr   r    r%   r1   r<   __static_attributes__ r   r   r   r      s/    0S 0T 0&G
	$(r   r   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)Device<   nameavailable_mem_bytes
logical_idrE   N)r>   r?   r@   rA   r   __annotations__rC   rD   rE   r   r   rG   rG   <   s    
IOr   rG   c                   *    \ rS rSr% \\S'   \\S'   Srg)NodeLatencyB   mem_latency_seccomputer_latency_secrE   Nr>   r?   r@   rA   floatrL   rD   rE   r   r   rN   rN   B   s    r   rN   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)PartitionLatencyI   rP   rQ   overall_latency_secrE   NrR   rE   r   r   rU   rU   I   s    r   rU   c                   (    \ rS rSrSrSrSrSrSrSr	g)	PartitionModeR   r               rE   N)
r>   r?   r@   rA   
size_based	sparse_nn
cost_awarekl_based	aot_basedrD   rE   r   r   rY   rY   R   s    JIJHIr   rY   c                       \ rS rSr% \\   \S'   \R                  r	\\S'   Sr
\\S'   0 r\\\4   \S'   0 r\\\4   \S'   0 r\\\\   4   \S'   S	r\\S
'   Srg)PartitionerConfigZ   devicesmode        transfer_rate_bytes_per_secnode_to_latency_mappingnode_to_partition_mapping#partition_to_logical_device_mappingFsaturate_hostrE   N)r>   r?   r@   rA   r   rG   rL   rY   r_   rh   rj   rS   rk   r   r	   rN   rl   rC   rm   rn   boolrD   rE   r   r   re   re   Z   sq    &\'22D-2),,79T$"34913tD#I3@B'c49n)=BM4r   re   r$   r   r   c                 b   0 n[        U R                  UR                  5        [        U R                  UR                  5        SnU H7  nXA;  d  M
  [	        USS5      nU(       a  X5R
                  -  nM.  [        S5      e   [	        U SS5      nU(       a  X5R                  -  nU$ [        S5      e)zwGiven 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_nodesr0   rq   s         r   r#   r#   e   s     %'KDII{--.DKK//0 !> L$7J)-C-CC)"#@AA  |T2J!%:%::! %$ 899r   	partitionrk   c           
         ^ ^^ S[         S[        [           4S jnS[        S[        4UUU 4S jjmU" T 5      n[        SSSS9nU H3  nT" U[        SSSS95      nUR                  UR                  :  d  M1  UnM5     U$ )zVGiven a partition and its nodes' latency, return a PartitionLatency for this partitionrw   r   c                 4  ^  / nT R                    H  nUR                  S;   a  M  0 n[        UR                  UR                  5        [        UR
                  UR                  5        [        U 4S jU 5       5      (       a  Ms  UR                  U5        M     U$ )z>Given a partition, return a list of nodes on the top bfs level>   r(   r)   c              3   n   >#    U  H*  nUTR                   ;   =(       a    UR                  S ;  v   M,     g7f)>   r(   r)   N)r   r-   )r5   r0   rw   s     r   r6   Fget_latency_of_one_partition.<locals>.get_top_nodes.<locals>.<genexpr>   s3      $A Y__$P5P)PP$s   25)r   r-   r   r*   r+   r,   anyappend)rw   	top_nodesr$   r/   s   `   r   get_top_nodes3get_latency_of_one_partition.<locals>.get_top_nodes   s     "	OODww55,.KDII{556DKK!7!78  $     & $ r   r$   c           	        > TU    nUR                   [        UR                  UR                  5      -   nUR                  UR                  -   nUR                  UR                  -   n[	        U R
                  5      R                  TR                  5      nU(       aG  [        SSSS9nU H4  nT
" U[        XEU5      5      n	U	R                   UR                   :  d  M2  U	nM6     U$ [        XEU5      $ )ziGiven a top node of a partition, this function returns
the latency of the critical path in the partition
ri   rP   rQ   rW   )	rW   maxrQ   rP   r   r:   intersectionr   rU   )r$   partition_latencynode_latencyrW   rP   rQ   r:   max_latencyr0   new_partition_latency
dfs_helperrk   rw   s             r   r   0get_latency_of_one_partition.<locals>.dfs_helper   s    /t4/CCc--|/K/KG
 

 --0L0LL 	
 22\5V5VV 	 DJJ,,Y__=* ##SVK (2$'?R)% *==!556 #8K  3F
 	
r   ri   r   )r   r   r	   rU   rW   )rw   rk   r   r~   critical_path_latencyr$   r   r   s   ``     @r   get_latency_of_one_partitionr      s    
 tDz ((
 (
5E (
 (
X i(I,#3 & ##SV
 11#778 %6!  ! r   
partitionsc                 :    0 nU  H  n[        X15      nXBU'   M     U$ )zGiven all the partitions and node_to_latency_mapping dictionary,
return a mapping dictionary of each partition to its overall latency
)r   )r   rk   partition_to_latency_mappingrw   r   s        r    get_partition_to_latency_mappingr      s7     GI 	8
 3DY/	  
 ('r   parent_partitionchild_partitionrj   c                    U R                   / :w  a+  UR                   / :w  a  U R                   UR                   :X  a  gSn[        5       nUR                   H  n0 n[        UR                  UR
                  5        [        UR                  UR
                  5        U HJ  nXpR                  ;   d  M  Xt;  d  M  [        USS5      nUb  X8R                  -  nUR                  U5        ML     M     X2-  $ )z^Given two partitions (parent and child),
calculate the communication latency between the two.
ri   r   rq   N)
r   r   r   r   r*   r+   r,   rr   rs   r.   )	r   r   rj   	comm_sizevisited_nodesr$   r/   r0   rq   s	            r   get_comm_latency_betweenr      s     	++r1.."4//?3U3UUIEM
  %%(*		;112[334A***q/E$Qd;
)!7!77I!!!$ 	 & 22r   r   c                    ^^^ S[         S[        S[        4UUU4S jjmS[        [            S[        [            4S jnU" U 5      nSnU H  nT" US5      nXu:  d  M  UnM     U$ )zGiven all partitions in a graph, find the critical path among all partitions
and return its latency as the latency of the whole graph
rw   latency_so_far_secr   c                    > UTU    R                   -  nU R                  (       a7  SnU R                   H#  n[        XT5      nT" X1U-   5      nXR:  d  M!  UnM%     U$ U$ )zJThis function helps to recursively get the latency of a path of partitionsri   )rW   r   r   )	rw   r   max_latency_secchildcomm_latency_secnew_latency_secr   r   rj   s	         r   r   4get_latency_of_partitioned_graph.<locals>.dfs_helper  s     	:


	 !O"++#;&A$  #-0@@# #4&5O , #"!!r   r   c                 j    U  Vs/ s H   n[        UR                  5      S:X  d  M  UPM"     nnU$ s  snf )zfThis function is to return all the partitions without parents
as the starting points of all the paths
r   )lenr   )r   rw   top_partitionss      r   get_top_partitions<get_latency_of_partitioned_graph.<locals>.get_top_partitions-  s?     (2
'1)S9J9J5Kq5PIz 	 
 
s   00ri   )r   rS   r   )	r   r   rj   r   r   critical_path_latency_secrw   latency_secr   s	    ``     @r    get_latency_of_partitioned_graphr     sy    "i "U "u " ",tI 4	?  (
3N ##	 C02(3% $ %$r   N)enumr   typingr   r   r   r   torch.fx.noder   r	   r   rG   rN   rU   rY   re   rC   r#   r   r   rS   r   r   rE   r   r   <module>r      s&    . . '1( 1(hZ  *  z D  
  %D %T %s %8V!V!37k8I3JV!V!r(Y(:>t[?P:Q(	)%
%&( !3!3!3 "'!3H/%Y/%"&y2B'B"C/% "'/%r   