
    `i0                        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dS )     )Enum)
NamedTuple)map_argNodec                   :    e Zd ZdZdeddfdZd Zd Zd Zd	 Z	dS )
	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 | _        d S )Nr   )setnodesr	   parentschildren	bfs_levelused_mem_byteslogical_device_ids)selfr	   s     {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/fx/experimental/partitioner_utils.py__init__zPartition.__init__   sH     #
('*uu(+ #$-/    c                 *    t          | j                  S N)strr	   )r   s    r   __str__zPartition.__str__   s    4$%%%r   c                 n    d| _         | j        D ]%}| xj         t          || j                  z  c_         &d S )Nr   )r   r   get_extra_size_of)r   nodes     r   recalculate_mem_sizezPartition.recalculate_mem_size   sM    J 	G 	GD#4T4:#F#FF	G 	Gr   c                    i }t          |j        |j                   t          |j        |j                   |D ]%}|j        dv r| j                            |           &| j                            |           |                                  d S )N>   get_attrplaceholder)r   args
setdefaultkwargsopr   addr   )r   r   input_nodesns       r   add_nodezPartition.add_node   s    (*	;1222[3444 	" 	"At222
q!!!
t!!#####r   c                 v    | j         v r j                             |           i }t          |j        |j                   t          |j        |j                   |D ]E}t           fd|j        D                       r#|j        dv r j                             |           F 	                                 d S d S )Nc              3   *   K   | ]}|j         vV  d S r   )r   ).0r)   r   s     r   	<genexpr>z(Partition.remove_node.<locals>.<genexpr>5   s;        ,-ATZ'     r   >   r!   r"   )
r   remover   r#   r$   r%   allusersr&   r   )r   r   r(   
input_nodes   `   r   remove_nodezPartition.remove_node)   s    4:Jd###,.KDI{5666DK!7888 * 2 2
    1;1A     2 m'BBBJ%%j111%%''''' r   )
__name__
__module____qualname____doc__intr   r   r   r*   r3    r   r   r   r      s         0S 0T 0 0 0 0& & &G G G
	$ 	$ 	$( ( ( ( (r   r   c                   .    e Zd ZU eed<   eed<   eed<   dS )Devicenameavailable_mem_bytes
logical_idN)r4   r5   r6   r   __annotations__r8   r9   r   r   r;   r;   <   s.         
IIIOOOOOr   r;   c                   $    e Zd ZU eed<   eed<   dS )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<   dS )PartitionLatencyrB   rC   overall_latency_secNrD   r9   r   r   rG   rG   I   s6         r   rG   c                   "    e Zd ZdZdZdZdZdZdS )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IIIr   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	<   d
S )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   s         &\'2D-222),,,,79T$"3499913tD#I333@B'c49n)=BBBM4r   rU   r   r   r
   c                 <   i }t          | j        |j                   t          | j        |j                   d}|D ]3}||vr-t	          |dd          }|r||j        z  }$t          d          4t	          | dd          }|r||j        z  }nt          d          |S )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DI{-...DK/000 ! B BE>> L$77J B)Z-CC))"#@AAA  |T22J :!Z%::!!8999$$r   	partitionrZ   c           	          dt           dt          t                   fd}dt          dt          f fd |           }t          ddd          }|D ]0} |t          ddd                    }|j        |j        k    r|}1|S )zVGiven a partition and its nodes' latency, return a PartitionLatency for this partitionrh   r
   c                      g } j         D ]r}|j        dv ri }t          |j        |j                   t          |j        |j                   t           fd|D                       s|                    |           s|S )z>Given a partition, return a list of nodes on the top bfs level>   r!   r"   c              3   <   K   | ]}|j         v o|j        d vV  dS )>   r!   r"   N)r   r&   )r-   r)   rh   s     r   r.   zFget_latency_of_one_partition.<locals>.get_top_nodes.<locals>.<genexpr>   sK         Y_$P5P)P     r   )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 	' 	'Dw555,.KDI{5666DK!7888     $     '   &&&r   r   c           	         |          }|j         t          |j        |j                  z   }|j        |j        z   }|j        |j        z   }t	          | j                                      j                  }|rFt          ddd          }|D ]/} 
|t          |||                    }	|	j         |j         k    r|	}0|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/Cc-|/KG
 G
 

 -0LL 	
 2\5VV 	 DJ,,Y_== 	* ##SV  K  8 8(2
$')=?R ) )% *=!56 6 #8K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 6&J ##SV  
 
 1#78 8 %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~      sJ     GI  D D	8.
 
 3D$Y//''r   parent_partitionchild_partitionrY   c                    | j         g k    r|j         g k    r| j         |j         k    rdS d}t                      }|j        D ]|}i }t          |j        |j                   t          |j        |j                   |D ]A}|| j        v r6||vr2t          |dd          }|
||j        z  }|	                    |           B}||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1."44/?3UUUsIEEM
  % 	% 	%(*	;1222[3444 	% 	%A$***q/E/E$Qd;;
)!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          }||k    r|}|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             }||k    r|}+|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 	6#;u&A$ $  #-*-0@@# # #_44&5O""!!r   r{   c                     d | D             }|S )zvThis function is to return all the partitions without parents
        as the starting points of all the paths
        c                 B    g | ]}t          |j                  d k    |S )r   )lenr   )r-   rh   s     r   
<listcomp>zPget_latency_of_partitioned_graph.<locals>.get_top_partitions.<locals>.<listcomp>2  s4     
 
 
#S9J5K5Kq5P5PI5P5P5Pr   r9   )r{   top_partitionss     r   get_top_partitionsz<get_latency_of_partitioned_graph.<locals>.get_top_partitions-  s(    

 
'1
 
 
 r   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	?     ('
33N ## 4 4	 jC00222(3%$$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      sb               ' ' ' ' ' ' ' '1( 1( 1( 1( 1( 1( 1( 1(h    Z            *          z       D            
      %D %T %s % % % %8V!V!37k8I3JV!V! V! V! V!r(Y(:>t[?P:Q(	)%
%&( ( ( ( !3!3!3 "'!3 !3 !3 !3H/%Y/%"&y2B'B"C/% "'/% /% /% /% /% /%r   