
    Vh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)
	PartitionzPartition 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 )Nr   )setnodesr	   parentschildren	bfs_levelused_mem_byteslogical_device_ids)selfr	   s     W/home/dcms/DCMS/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<    JJ 	GD#4T4::#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tt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      ,-ATZZ's   >   r!   r"   )
r   remover   r#   r$   r%   allusersr&   r   )r   r   r(   
input_nodes   `   r   remove_nodezPartition.remove_node)   s    4::JJd#,.KDII{556DKK!7!78 * 2
 1;1A1A  mm'BBJJ%%j1	2
 %%' r   )
__name__
__module____qualname____doc__intr   r   r   r*   r3    r   r   r   r      s/    0S 0T 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2D-2),,79T$"34913tD#I3@B'c49n)=BM4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      )zGiven 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DII{--.DKK//0 ! BE> L$7J)Z-C-CC)"#@AAB |T2J!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     "	OO 	'Dww55,.KDII{556DKK!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Cc--|/K/KG
 

 --0L0LL 	
 22\5V5VV 	 DJJ,,Y__=* ##SVK  8(2$')=?R)% *==!556 #8K8 13F
 	
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    
 tDz ((
 (
5E (
X i(I,#3  6& ##SV
 11#778 %6!6 ! r   
partitionsc                 :    i }| D ]  }t        ||      }|||<    |S )zGiven 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//?3U3UUIEM
  %% 	%(*		;112[334 	%A$***q/E$Qd;
)!7!77I!!!$	%		% 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 )zGiven 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&5O	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9J9J5Kq5PI
 
 
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 ",tI 4	?  (
3N ## 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k8I3JV!V!r(Y(:>t[?P:Q(	)%
%&( !3!3!3 "'!3H/%Y/%"&y2B'B"C/% "'/%r   