o
    eiF                     @   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r+tdtt||| _d S d S )Nz/nodes and node_orders must have the same length)r   r   dictfromkeyslistlenAssertionErrorzip)selfr   r   r   
nodes_listnode_orders_list r   k/var/www/addictedbytheproject.nl/epg/venv/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   size1   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   6   s   
z_DependencyViewer.__init__r!   r   c                 C   s
   | j | S r   )r2   r&   r   r   r   downstreams_of?   r    z _DependencyViewer.downstreams_ofN)r)   r*   r+   r   r   r   r1   r8   r   r   r   r   r-   5   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.   r:   r;   r<   r=   r-   dependency_viewer)r   r.   r:   r;   r<   r=   r   r   r   r   D   s   z#CapabilityBasedPartitioner.__init__r!   c                 C   s   | j t| j |S r   )r:   is_node_supportedr   r.   named_modulesr&   r   r   r   _is_node_supportedW   s   z-CapabilityBasedPartitioner._is_node_supportedc                    s4  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 ]2}d}|jD ]}|jdkst|jdkrd} nq|r |}|jD ]} ||kr|||< qq|sn| D ]
\}}||d | qqjsut	d ddh}| tj!}g } D ]G\}}d}|jD ]2}|jdkr^t"|jsEt#dt$|j t|j|vrQ|d7 }t|jj%v r^|d7 }q-|dkrj|&| q$|D ]}|= qnt	d  D ]\}}t	d|dd |jD  q~d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)r1   r>   r8   r6   )rD   	user_nodevisited_partition_ids	path_nodepartition_idp_map)
assignmentrC   other_nodespartition_mapr   rB   
self_nodesr   r   dfs_iter_find_cyclez   s    
ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)r   r1   r   difference_updater   r#   minunion)rB   rC   rN   rD   merge_id
removed_idr!   rJ   rL   partition_userspartitions_by_idpartitions_orderr   )rC   rK   rB   rM   r   maybe_merge_partitionu   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rM| | < |d u r4tdt|| g|gd|< t| j|< || | d S | | < | | | d S )Nr!   r   c                    sB   | j D ]} |}|d ur| | | |  qd S r   )r5   getr6   r#   )r!   r   rE   	target_id)rJ   rL   r   r   _update_partition_map   s   

zgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_mapz)node_order is required for new partitions)r   r   r   )	r   r,   r'   popr   r
   r1   r5   r$   )r!   r"   r   r[   )rJ   rL   rU   rV   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Expected callable target, got z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(   rd   	partitionr   r   r   re   ?  s    )'r/   r0   r1   	itertoolscountr,   r   r   loggerdebug	enumerater3   r.   r4   r   rA   nextsorteditemsoperator
itemgetterr   keysr   valuesr   r5   opr   targetrY   r;   rQ   r<   callabler   typer=   append)r   nodes_ordernew_partition_idrX   r]   r"   r!   merge_candidatesrH   _merge_candidates_listrB   rC   rh   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr<   partitions_to_removecompute_node_countr   rT   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   rb   r   r%   rg   r   r   r   re   J  rf   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>r   )rk   rl   r	   r.   )r   r   r   r   r   r   fuse_partitionsC  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 )Nra   )ru   r   rv   )r!   )r<   r   r   is_non_compute_nodeR  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noderh   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)ru   all_input_nodes)r!   rh   r   input_n)r   is_transparent_input_nodetransparent_input_nodesr   r   r   \  s    

z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    r   r   )ru   r5   )r!   rh   r   output_n)r   is_transparent_output_nodetransparent_output_nodesr   r   r   q  s$   

z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r1   r<   r   r   r6   r   r\   )r   r   rh   r'   r!   r   )r   r   r   r<   r   r   r   remove_bookend_non_compute_opsO  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   rA   r   r
   r   r   r   r   r   r   r   r   r9   C   s<    


 i
Kr9   )r/   ri   loggingrq   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   !torch.fx.passes.utils.fuser_utilsr	   	getLoggerr)   rk   setLevelWARNINGr
   r-   r9   r   r   r   r   <module>   s   
"