
    קgm0                        d dl mZ d dlmZmZmZ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DictListSet)Nodemap_argc                   :    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     c/var/www/html/ai-engine/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-   r6    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)r7   r8   r9   r   __annotations__r;   r<   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r7   r8   r9   floatrB   r<   r   r   rD   rD   B   s*         r   rD   c                   .    e Zd ZU eed<   eed<   eed<   dS )PartitionLatencyrE   rF   overall_latency_secNrG   r<   r   r   rJ   rJ   I   s6         r   rJ   c                   "    e Zd ZdZdZdZdZdZdS )PartitionModer               N)r7   r8   r9   
size_based	sparse_nn
cost_awarekl_based	aot_basedr<   r   r   rM   rM   R   s'        JIJHIIIr   rM   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)r7   r8   r9   r   r>   rB   rM   rR   rZ   r\   rH   r]   r   r   rD   r^   r;   r_   r`   boolr<   r   r   rX   rX   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   rX   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,   rc   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   	partitionr]   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 partitionri   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)   )r0   r,   ri   s     r   r1   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)ri   	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
        r[   rE   rF   rK   )	rK   maxrF   rE   r   r4   intersectionr   rJ   )r!   partition_latencynode_latencyrK   rE   rF   r4   max_latencyr,   new_partition_latency
dfs_helperr]   ri   s             r   ry   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   r[   rr   )r   r   r   rJ   rK   )ri   r]   rp   ro   critical_path_latencyr!   ru   ry   s   ``     @r   get_latency_of_one_partitionr{      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
    )r{   )r|   r]   partition_to_latency_mappingri   ru   s        r    get_partition_to_latency_mappingr      sJ     GI  D D	8.
 
 3D$Y//''r   parent_partitionchild_partitionr\   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.
    r[   r   rc   N)
r   r   r   r	   r&   r'   r(   rd   re   r*   )	r   r   r\   	comm_sizevisited_nodesr!   r+   r,   rc   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
    ri   latency_so_far_secr   c                     ||          j         z  }| j        }| j        r6d}| j        D ]*}t          | |	          } |||z             }||k    r|}+|S |S )zJThis function helps to recursively get the latency of a path of partitionsr[   )rK   r   r   )
ri   r   r   max_latency_secchildcomm_latency_secnew_latency_secry   r~   r\   s
          r   ry   z4get_latency_of_partitioned_graph.<locals>.dfs_helper  s     	:

	 % 	#!O"+ 	6 	6#;u&A$ $  #-*-0@@# # #_44&5O""!!r   r|   c                 n    g }| D ]/}t          |j                  dk    r|                    |           0|S )zvThis function is to return all the partitions without parents
        as the starting points of all the paths
        r   )lenr   rn   )r|   top_partitionsri   s      r   get_top_partitionsz<get_latency_of_partitioned_graph.<locals>.get_top_partitions-  sI     # 	1 	1I9$%%**%%i000r   r[   )r   rH   r   )	r|   r~   r\   r   r   critical_path_latency_secri   latency_secry   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   r   r   r   torch.fx.noder   r	   r   r>   rD   rJ   rM   rX   r;   r    r{   r   rH   r   r   r<   r   r   <module>r      st         . . . . . . . . . . . . ' ' ' ' ' ' ' '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0%Y0%"&y2B'B"C0% "'0% 0% 0% 0% 0% 0%r   