o
    ߗi0                     @   s0  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 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)DictList
NamedTupleSet)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   e/home/ubuntu/transcripts/venv/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(   r0   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)r1   r2   r3   r   __annotations__r5   r   r   r   r   r6   <   s   
 r6   c                   @   s   e Zd ZU eed< eed< dS )NodeLatencymem_latency_seccomputer_latency_secNr1   r2   r3   floatr:   r   r   r   r   r;   B   s   
 r;   c                   @   s&   e Zd ZU eed< eed< eed< dS )PartitionLatencyr<   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)r1   r2   r3   
size_based	sparse_nn
cost_awarekl_based	aot_basedr   r   r   r   rB   R   s    rB   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)r1   r2   r3   r   r6   r:   rB   rG   rN   rP   r?   rQ   r   r   r;   rR   r5   rS   rT   boolr   r   r   r   rL   Z   s   
 rL   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'   rV   r   r   r   r   e   s    
r   	partitionrQ   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
        rO   r<   r=   rA   )	rA   maxr=   r<   r   r.   intersectionr   r@   )
r   partition_latencynode_latencyrA   r<   r=   r.   max_latencyr'   new_partition_latency
dfs_helperrQ   r\   r   r   rj      s:   

z0get_latency_of_one_partition.<locals>.dfs_helperrO   rb   )r	   r   r   r@   rA   )r\   rQ   ra   r`   critical_path_latencyr   re   r   ri   r   get_latency_of_one_partition   s&   ,rl   
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
    )rl   )rm   rQ   partition_to_latency_mappingr\   re   r   r   r    get_partition_to_latency_mapping   s   
ro   parent_partitionchild_partitionrP   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.
    rO   r   rV   N)
r   r   r   r   r!   r"   r#   rW   rX   r%   )	rp   rq   rP   	comm_sizevisited_nodesr   r&   r'   rV   r   r   r   get_comm_latency_between   s&   





rt   rn   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 partitionsrO   )rA   r   rt   )r\   ru   max_latency_secchildcomm_latency_secnew_latency_secrj   rn   rP   r   r   rj     s&   
z4get_latency_of_partitioned_graph.<locals>.dfs_helperrm   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   )rm   top_partitionsr   r   r   get_top_partitions-  s   z<get_latency_of_partitioned_graph.<locals>.get_top_partitionsrO   )r	   r?   r   )rm   rn   rP   r~   r}   critical_path_latency_secr\   latency_secr   rz   r    get_latency_of_partitioned_graph  s   	

r   N)enumr   typingr   r   r   r   torch.fx.noder   r   r	   r6   r;   r@   rB   rL   r5   r   rl   ro   r?   rt   r   r   r   r   r   <module>   sL   4	

Y



$
