o
    iRD                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ eeZeej G dd	 d	ZG d
d dZG dd dZdS )    N)IterableSequence)Optional)GraphModule)_get_qualified_nameNode)OperatorSupportBase)fuse_by_partitionsc                	   @   sz   e Zd Z			ddee deee  deee  fddZdefdd	Z	dd
edee fddZ
d
efddZdd ZdS )	PartitionNidnodesnode_ordersc                 C   sn   || _ i | _|d ur5|d u rt|d | _d S t|}t|}t|t|ks+J dtt||| _d S d S )Nz/nodes and node_orders must have the same length)r   r   dictfromkeyslistlenzip)selfr   r   r   
nodes_listnode_orders_list r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/torch/fx/passes/infra/partitioner.py__init__   s   zPartition.__init__returnc                 C   
   t | jS N)strr   r   r   r   r   __repr__'      
zPartition.__repr__node
node_orderc                 C   s   | j ||i d S r   )r   update)r   r    r!   r   r   r   add_node*   s   zPartition.add_nodec                 C   s   | j |= d S r   r   r   r    r   r   r   remove_node-   s   zPartition.remove_nodec                 C   r   r   )r   r   r   r   r   r   size0   r   zPartition.size)NNNr   )__name__
__module____qualname__r   intr   r   r   r   r   r#   r&   r'   r   r   r   r   r
      s    


r
   c                   @   s0   e Zd ZdefddZdedee fddZdS )	_DependencyViewergraph_modulec                 C   sT   t t| _t|jjD ]}|jD ]}| j| | | j| 	| j|  qqd S r   )
collectionsdefaultdictsetdownstreamsreversedgraphr   usersaddr"   )r   r-   r    output_noder   r   r   r   5   s   
z_DependencyViewer.__init__r    r   c                 C   s
   | j | S r   )r1   r%   r   r   r   downstreams_of>   r   z _DependencyViewer.downstreams_ofN)r(   r)   r*   r   r   r   r0   r7   r   r   r   r   r,   4   s    	r,   c                   @   s   e Zd Z			ddedededeee  deee  ddfd	d
Z	de
defddZdee fddZ	ddee dedefddZdee fddZddedefddZdS )CapabilityBasedPartitionerFNr-   operator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 C   sD   || _ || _|| _|d ur|ng | _|d ur|ng | _t|| _d S r   )r-   r9   r:   r;   r<   r,   dependency_viewer)r   r-   r9   r:   r;   r<   r   r   r   r   C   s   z#CapabilityBasedPartitioner.__init__r    c                 C   s   | j t| j |S r   )r9   is_node_supportedr   r-   named_modulesr%   r   r   r   _is_node_supportedV   s   z-CapabilityBasedPartitioner._is_node_supportedc                    s  t ti  i i }i i t }dtdtf fdd}dtdtt dtt f fdd	}t	d
 t
tjjjD ]Y\}}i }|rj| vrjt|}|||< ||< |||| d ||< t tddD ]\}}	d ||< qut| }
t|
dkr|
d }|
dd  D ]	}|||\}}	qqE D ]}tt|j tddd|_qt	d i }jjjD ]4}d}|jD ]}|jdkst|jdkrd} nq|r |d }|jD ]} |d |kr|||< qq| D ]
\}}||d | qjsjt	d ddh}| tj!}g } D ]?\}}d}|jD ]*}|jdkrSt"|js:J t|j|vrF|d7 }t|jj#v rS|d7 }q*|dkr_|$| q!|D ]}|= qct	d  D ]\}}t	d|dd |jD  qsdd  D S )Nself_idother_idc                    s   j   j dtt f 	fdd}   B }| ||r1dfS  }}ttk rC||}}| j | j  | j D ]}||< qS|= t| | |< |= | | |< |= ||< |= |dfS )Nall_user_nodesc                    s   | D ]<}t  }j|D ]0}|v s|v r  dS | v r= | }||v r(q| }|v s4|v r8  dS || qqdS )NTF)r0   r=   r7   r5   )rC   	user_nodevisited_partition_ids	path_nodepartition_idp_map)
assignmentrB   other_nodespartition_mapr   rA   
self_nodesr   r   dfs_iter_find_cycley   s    
ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)r   r0   r   difference_updater   r"   minunion)rA   rB   rM   rC   merge_id
removed_idr    rI   rK   partition_userspartitions_by_idpartitions_orderr   )rB   rJ   rA   rL   r   maybe_merge_partitiont   s2   

"


zLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionr    r!   r   c                    s   dt dtf fdd}|  v r |   |  |d u r$ |  d S |vrK| | < |d us2J t|| g|gd|< t| j|< || | d S | | < | | | d S )Nr    r   c                    sD   | j D ]} |d }|d ur| | | |  qd S r   )r4   getr5   r"   )r    r   rD   	target_id)rI   rK   r   r   _update_partition_map   s   
zgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map)r   r   r   )r   r+   r&   popr
   r0   r4   r#   )r    r!   r   rZ   )rI   rK   rT   rU   r   r   merge_single_node   s   	

zHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_nodezProposing partitions...   )keyr   T)r^   reversez=Reassigning getitem nodes to its producer node's partition...call_functionz_operator.getitemFz'Filtering out single node partitions...ztorch.ops.aten.viewzPartitions proposed:zpartition #%s: %sc                 S      g | ]}|j qS r   )name).0r    r   r   r   
<listcomp>/      zACapabilityBasedPartitioner.propose_partitions.<locals>.<listcomp>c                 S   s   g | ]
}|  d kr|qS )r   )r'   rc   	partitionr   r   r   rd   2  s    )%r.   r/   r0   	itertoolscountr+   r   r   loggerdebug	enumerater2   r-   r3   r   r@   nextsorteditemsoperator
itemgetterr   keysr   valuesr   r4   opr   targetrX   r:   rP   r;   callabler<   append)r   nodes_ordernew_partition_idrW   r\   r!   r    merge_candidatesrG   _merge_candidates_listrA   rB   rg   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr;   partitions_to_removecompute_node_countr   rS   r   propose_partitions[   s   
 (H











z-CapabilityBasedPartitioner.propose_partitionsfused_
partitionsprefixc                 C   s$   t d t| jdd |D |dS )NzFusing partitions...c                 S   ra   r   r$   rf   r   r   r   rd   =  re   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>r   )rj   rk   r	   r-   )r   r   r   r   r   r   fuse_partitions6  s   
z*CapabilityBasedPartitioner.fuse_partitionsc                    s   t | jdtffdd i i dtdt t dt t f fdddtdt t dt t f fdd	|D ]8}t  }|jD ]} |r_|t |j|sZ|t |j|r_|| qBt|d
krr|D ]	}|j|d  qhq:d S )Nr    c                    s   | j dkot| j v S )Nr`   )rt   r   ru   )r    )r;   r   r   is_non_compute_nodeE  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noderg   removed_nodesc                    t   | j dks| |vs| |v rdS | v r|  S  | r4| jD ]}|||s-d| <  dS qd| < dS d| < dS NplaceholderTF)rt   all_input_nodes)r    rg   r   input_n)r   is_transparent_input_nodetransparent_input_nodesr   r   r   O  s    

z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    r   r   )rt   r4   )r    rg   r   output_n)r   is_transparent_output_nodetransparent_output_nodesr   r   r   d  s$   

z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r0   r;   r   r   r5   r   r[   )r   r   rg   r&   r    r   )r   r   r   r;   r   r   r   remove_bookend_non_compute_opsB  sF   


z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| j||d}|S )Nr   )r   r   )r   r   r   fused_gmr   r   r   partition_and_fuse  s   z-CapabilityBasedPartitioner.partition_and_fuse)FNN)r   )r(   r)   r*   r   r   boolr   r   r   r   r   r@   r   r
   r   r   r   r   r   r   r   r   r8   B   s<    


 ]
Kr8   )r.   rh   loggingrp   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   !torch.fx.passes.utils.fuser_utilsr	   	getLoggerr(   rj   setLevelWARNINGr
   r,   r8   r   r   r   r   <module>   s   
!