
    [Th0                     V   S SK Jr  S SK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)
NamedTuple)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     _/var/www/auris/envauris/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   ).0r-   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"   r.   r9   __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_idrB   N)r;   r<   r=   r>   r   __annotations__r@   rA   rB   r   r   rD   rD   <   s    
IOr   rD   c                   *    \ rS rSr% \\S'   \\S'   Srg)NodeLatencyB   mem_latency_seccomputer_latency_secrB   Nr;   r<   r=   r>   floatrI   rA   rB   r   r   rK   rK   B   s    r   rK   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)PartitionLatencyI   rM   rN   overall_latency_secrB   NrO   rB   r   r   rR   rR   I   s    r   rR   c                   (    \ rS rSrSrSrSrSrSrSr	g)	PartitionModeR   r               rB   N)
r;   r<   r=   r>   
size_based	sparse_nn
cost_awarekl_based	aot_basedrA   rB   r   r   rV   rV   R   s    JIJHIr   rV   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_hostrB   N)r;   r<   r=   r>   listrD   rI   rV   r\   re   rg   rP   rh   dictr   rK   ri   r@   rj   rk   boolrA   rB   r   r   rb   rb   Z   sq    &\'22D-2),,79T$"34913tD#I3@B'c49n)=BM4r   rb   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_nodesr-   rp   s         r   r    r    e   s     %'KDII{--.DKK//0 !> L$7J)-C-CC)"#@AA  |T2J!%:%::! %$ 899r   	partitionrh   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 partitionrv   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*   )r2   r-   rv   s     r   r3   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)rv   	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
rf   rM   rN   rT   )	rT   maxrN   rM   r   r7   intersectionr   rR   )r!   partition_latencynode_latencyrT   rM   rN   r7   max_latencyr-   new_partition_latency
dfs_helperrh   rv   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   rf   r   )r   rl   r   rR   rT   )rv   rh   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   rh   partition_to_latency_mappingrv   r   s        r    get_partition_to_latency_mappingr      s7     GI 	8
 3DY/	  
 ('r   parent_partitionchild_partitionrg   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.
rf   r   rp   N)
r   r   r   r   r'   r(   r)   rq   rr   r+   )	r   r   rg   	comm_sizevisited_nodesr!   r,   r-   rp   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
rv   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 partitionsrf   )rT   r   r   )	rv   r   max_latency_secchildcomm_latency_secnew_latency_secr   r   rg   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   rv   top_partitionss      r   get_top_partitions<get_latency_of_partitioned_graph.<locals>.get_top_partitions-  s?     (2
'1)S9J9J5Kq5PIz 	 
 
s   00rf   )r   rP   rl   )	r   r   rg   r   r   critical_path_latency_secrv   latency_secr   s	    ``     @r    get_latency_of_partitioned_graphr     sy    "i "U "u " ",tI 4	?  (
3N ##	 C02(3% $ %$r   N)enumr   typingr   torch.fx.noder   r   r   rD   rK   rR   rV   rb   r   r@   r    rm   r   rl   r   rP   r   r   rB   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   