o
    Hh0                     @   s$  d dl mZ d dlmZ d dlmZmZ G dd dZG dd deZG dd	 d	eZ	G d
d deZ
G dd deZG dd deZdedee defddZdedeee	f de
fddZdee deee	f deee
f fddZdededefdd Zdee d!eee
f defd"d#Zd$S )%    )Enum)
NamedTuple)map_argNodec                   @   sB   e Zd ZdZdeddfddZdd Zd	d
 Zdd Zd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                 C   s4   t  | _|| _t  | _t  | _d| _d| _g | _d S )Nr   )setnodesr   parentschildren	bfs_levelused_mem_byteslogical_device_ids)selfr    r   [/var/www/vscode/kcb/lib/python3.10/site-packages/torch/fx/experimental/partitioner_utils.py__init__   s   
zPartition.__init__c                 C   s
   t | jS N)strr   r   r   r   r   __str__   s   
zPartition.__str__c                 C   s,   d| _ | jD ]}|  j t|| j7  _ qd S )Nr   )r   r   get_extra_size_of)r   noder   r   r   recalculate_mem_size   s   
zPartition.recalculate_mem_sizec                 C   sX   i }t |j|j t |j|j |D ]}|jdv r| j| q| j| |   d S )N>   get_attrplaceholder)r   args
setdefaultkwargsopr   addr   )r   r   input_nodesnr   r   r   add_node   s   
zPartition.add_nodec                    s~   | j v r= j | i }t|j|j t|j|j |D ]}t fdd|jD r6|jdv r6 j | q 	  d S d S )Nc                 3   s    | ]}| j vV  qd S r   )r   .0r$   r   r   r   	<genexpr>5   s    

z(Partition.remove_node.<locals>.<genexpr>>   r   r   )
r   remover   r   r   r    allusersr!   r   )r   r   r#   
input_noder   r   r   remove_node)   s   

zPartition.remove_node)
__name__
__module____qualname____doc__intr   r   r   r%   r-   r   r   r   r   r      s    	r   c                   @   s&   e Zd ZU eed< eed< eed< dS )Devicenameavailable_mem_bytes
logical_idN)r.   r/   r0   r   __annotations__r2   r   r   r   r   r3   <   s   
 r3   c                   @   s   e Zd ZU eed< eed< dS )NodeLatencymem_latency_seccomputer_latency_secNr.   r/   r0   floatr7   r   r   r   r   r8   B   s   
 r8   c                   @   s&   e Zd ZU eed< eed< eed< dS )PartitionLatencyr9   r:   overall_latency_secNr;   r   r   r   r   r=   I   s   
 r=   c                   @   s    e Zd ZdZdZdZdZdZdS )PartitionModer               N)r.   r/   r0   
size_based	sparse_nn
cost_awarekl_based	aot_basedr   r   r   r   r?   R   s    r?   c                   @   s   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)r.   r/   r0   listr3   r7   r?   rD   rK   rM   r<   rN   dictr   r8   rO   r2   rP   rQ   boolr   r   r   r   rI   Z   s   
 rI   r   r   r   c                 C   s   i }t | j|j t | j|j d}|D ]}||vr,t|dd}|r(||j7 }qtdqt| dd}|r<||j7 }|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$   rU   r   r   r   r   e   s    
r   	partitionrN   c                    sx   dt dtt fdd}dtdtf fdd |}tdddd	}|D ]} |tdddd	}|j|jkr9|}q%|S )
zVGiven a partition and its nodes' latency, return a PartitionLatency for this partitionr[   r   c                    s`   g } j D ](}|jdv rqi }t|j|j t|j|j t fdd|D s-|| q|S )z>Given a partition, return a list of nodes on the top bfs level>   r   r   c                 3   s$    | ]}| j v o|jd vV  qdS )>   r   r   N)r   r!   r&   r[   r   r   r(      s
    
zFget_latency_of_one_partition.<locals>.get_top_nodes.<locals>.<genexpr>)r   r!   r   r   r   r    anyappend)r[   	top_nodesr   r#   r   r\   r   get_top_nodes   s   


z3get_latency_of_one_partition.<locals>.get_top_nodesr   c           
         s   |  }|j t|j|j }|j|j }|j|j }t| jj}|rDtdddd}|D ]} |t|||}	|	j |j krA|	}q.|S t|||S )zyGiven a top node of a partition, this function returns
        the latency of the critical path in the partition
        rL   r9   r:   r>   )	r>   maxr:   r9   r
   r+   intersectionr   r=   )
r   partition_latencynode_latencyr>   r9   r:   r+   max_latencyr$   new_partition_latency
dfs_helperrN   r[   r   r   ri      s:   

z0get_latency_of_one_partition.<locals>.dfs_helperrL   ra   )r   rR   r   r=   r>   )r[   rN   r`   r_   critical_path_latencyr   rd   r   rh   r   get_latency_of_one_partition   s&   ,rk   
partitionsc                 C   s$   i }| D ]}t ||}|||< q|S )zGiven all the partitions and node_to_latency_mapping dictionary,
    return a mapping dictionary of each partition to its overall latency
    )rk   )rl   rN   partition_to_latency_mappingr[   rd   r   r   r    get_partition_to_latency_mapping   s   
rn   parent_partitionchild_partitionrM   c           	      C   s   | j g kr|j g kr| j |j krdS d}t }|jD ]4}i }t|j|j t|j|j |D ]}|| jv rM||vrMt|dd}|durH||j7 }|	| q.q|| S )zfGiven two partitions (parent and child),
    calculate the communication latency between the two.
    rL   r   rU   N)
r   r
   r   r   r   r   r    rV   rW   r"   )	ro   rp   rM   	comm_sizevisited_nodesr   r#   r$   rU   r   r   r   get_comm_latency_between   s&   





rs   rm   c                    sh   dt dtdtf fdd dtt  dtt  fdd}|| }d	}|D ]} |d	}||kr1|}q$|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
    r[   latency_so_far_secr   c                    sR   ||  j 7 }| jr'd}| jD ]}t| |} ||| }||kr$|}q|S |S )zJThis function helps to recursively get the latency of a path of partitionsrL   )r>   r   rs   )r[   rt   max_latency_secchildcomm_latency_secnew_latency_secri   rm   rM   r   r   ri     s&   
z4get_latency_of_partitioned_graph.<locals>.dfs_helperrl   c                 S   s   dd | D }|S )zvThis function is to return all the partitions without parents
        as the starting points of all the paths
        c                 S   s   g | ]}t |jd kr|qS )r   )lenr   )r'   r[   r   r   r   
<listcomp>2  s    zPget_latency_of_partitioned_graph.<locals>.get_top_partitions.<locals>.<listcomp>r   )rl   top_partitionsr   r   r   get_top_partitions-  s   z<get_latency_of_partitioned_graph.<locals>.get_top_partitionsrL   )r   r<   rR   )rl   rm   rM   r}   r|   critical_path_latency_secr[   latency_secr   ry   r    get_latency_of_partitioned_graph  s   	

r   N)enumr   typingr   torch.fx.noder   r   r   r3   r8   r=   r?   rI   r
   r2   r   rS   rk   rR   rn   r<   rs   r   r   r   r   r   <module>   sL   4	

Y



$
