o
    ,hK@                     @   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                   @   s`   e Zd Z	ddee deee  fddZdefddZ	d	efd
dZ
d	efddZdd ZdS )	PartitionNidnodesc                 C   s(   || _ |d urt|| _d S i | _d S N)r   dictfromkeysr   )selfr   r    r   \/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/fx/passes/infra/partitioner.py__init__   s   "zPartition.__init__returnc                 C   
   t | jS r   )strr   r   r   r   r   __repr__      
zPartition.__repr__nodec                 C   s   | j |d i d S r   )r   updater   r   r   r   r   add_node   s   zPartition.add_nodec                 C   s   | j |= d S r   r   r   r   r   r   remove_node    s   zPartition.remove_nodec                 C   r   r   )lenr   r   r   r   r   size#   r   zPartition.size)NN)__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   (   s   
z_DependencyViewer.__init__r   r   c                 C   s
   | j | S r   )r+   r   r   r   r   downstreams_of1   r   z _DependencyViewer.downstreams_ofN)r"   r#   r$   r   r   r   r*   r1   r   r   r   r   r&   '   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'   r3   r4   r5   r6   r&   dependency_viewer)r   r'   r3   r4   r5   r6   r   r   r   r   6   s   z#CapabilityBasedPartitioner.__init__r   c                 C   s   | j t| j |S r   )r3   is_node_supportedr   r'   named_modulesr   r   r   r   _is_node_supportedI   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 f fdd}t	d	 t
jjjD ]V}i }|ra| vrat|}|||< ||< ||| d ||< t td
dD ]\}}d ||< qlt| }	t|	d
kr|	d }
|	d
d  D ]	}||
|\}
}qq?t	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 ]	\}}||| qۈjsIt	d ddh}|tj}g } D ]?\}}d}|jD ]*}|jdkr2t|jsJ t|j|vr%|d
7 }t|jj v r2|d
7 }q	|d
kr>|!| q |D ]}|= qBt	d  D ]\}}t	d|dd |jD  qRd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)r*   r7   r1   r/   )r=   	user_nodevisited_partition_ids	path_nodepartition_idp_map)
assignmentr<   other_nodespartition_mapr   r;   
self_nodesr   r   dfs_iter_find_cyclel   s    
ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)r   r*   r   difference_updater    r   minunion)r;   r<   rG   r=   merge_id
removed_idr   rC   rE   partition_userspartitions_by_idpartitions_orderr   )r<   rD   r;   rF   r   maybe_merge_partitiong   s2   

"


zLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionr   r   c                    s   dt dtf fdd}|  v r |   |  |d u r$ |  d S |vrC| | < t|| gd|< t| j|< || | d S | | < | |  d S )Nr   r   c                    sD   | j D ]} |d }|d ur| | | |  qd S r   )r.   getr/   r   )r   r   r>   	target_id)rC   rE   r   r   _update_partition_map   s   
zgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map)r   r   )r   r%   r   popr
   r*   r.   r   )r   r   rT   )rC   rE   rN   rO   r   r   merge_single_node   s   	zHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_nodezProposing partitions...   )keyr   z=Reassigning getitem nodes to its producer node's partition...T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!   r\   	partitionr   r   r   r]     s    )#r(   r)   r*   	itertoolscountr%   r   r   loggerdebugr,   r'   r-   r   r:   nextsorteditemsoperator
itemgetterlistkeysr    r.   opr   targetrR   r4   rJ   r5   callabler6   appendvalues)r   nodes_ordernew_partition_idrQ   rV   r   merge_candidatesrA   _merge_candidates_listr;   r<   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr5   partitions_to_remover`   compute_node_countr   rM   r   propose_partitionsN   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   rZ   r   r   r_   r   r   r   r]   %  r^   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>r   )rc   rd   r	   r'   )r   r~   r   r   r   r   fuse_partitions  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 )NrY   )rl   r   rm   )r   )r5   r   r   is_non_compute_node-  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noder`   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)rl   all_input_nodes)r   r`   r   input_n)r   is_transparent_input_nodetransparent_input_nodesr   r   r   7  s    

z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    r   r   )rl   r.   )r   r`   r   output_n)r   is_transparent_output_nodetransparent_output_nodesr   r   r   L  s$   

z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r*   r5   r   r   r/   r    rU   )r   r~   r`   r   r   r   )r   r   r   r5   r   r   r   remove_bookend_non_compute_ops*  sF   


z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| j||d}|S )Nr   )r|   r   )r   r   r~   fused_gmr   r   r   partition_and_fuseu  s   z-CapabilityBasedPartitioner.partition_and_fuse)FNN)r}   )r"   r#   r$   r   r   boolr   r   r   r   r   r:   rj   r
   r|   r   r   r   r   r   r   r   r2   5   s<    


 R
Kr2   )r(   ra   loggingrh   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   !torch.fx.passes.utils.fuser_utilsr	   	getLoggerr"   rc   setLevelWARNINGr
   r&   r2   r   r   r   r   <module>   s   
