
    sgm0                     X   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y)     )Enum)
NamedTupleDictListSet)Nodemap_argc                   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     Z/var/www/html/venv/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-   r6    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)r7   r8   r9   r   __annotations__r;   r<   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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<   y)PartitionLatencyrE   rF   overall_latency_secNrG   r<   r   r   rJ   rJ   I   s    r   rJ   c                        e Zd ZdZdZdZdZdZy)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Ir   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	<   y
)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   sq    &\'22D-2),,79T$"34913tD#I3@B'c49n)=BM4r   rX   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,   rc   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   	partitionr]   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 partitionri   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)   )r0   r,   ri   s     r   r1   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)ri   	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
        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/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   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& ##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
    )r{   )r|   r]   partition_to_latency_mappingri   ru   s        r    get_partition_to_latency_mappingr      sD     GI  D	8.
 3D$Y/	D
 ('r   parent_partitionchild_partitionr\   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.
    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.."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
    ri   latency_so_far_secr   c                     ||    j                   z  }| j                  }| j                  r6d}| j                  D ]#  }t        | |	      } |||z         }||kD  s"|}% |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#;u&A$  #--0@@# #_4&5O	6 #"!!r   r|   c                 l    g }| D ],  }t        |j                        dk(  s|j                  |       . |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-  sA     # 	1I9$$%*%%i0	1 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	? 	 (
3N ## 4	 C022(3%4 %$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      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0%Y0%"&y2B'B"C0% "'0%r   