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   g/home/www/facesmatcher.com/frenv_anti/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   Z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*   r0   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'   r2   r3   r4   r5   r&   dependency_viewer)r   r'   r2   r3   r4   r5   r   r   r   r   6   s   z#CapabilityBasedPartitioner.__init__r   c                 C   s   | j t| j |S r   )r2   Zis_node_supportedr   r'   Z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} qq|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*   r6   r0   r/   )r:   	user_nodeZvisited_partition_idsZ	path_nodepartition_idZp_map)
assignmentr9   other_nodespartition_mapr   r8   
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)r8   r9   rA   r:   Zmerge_idZ
removed_idr   r=   r?   partition_userspartitions_by_idZpartitions_orderr   )r9   r>   r8   r@   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;   Z	target_id)r=   r?   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   rJ   )r=   r?   rF   rG   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!   rR   	partitionr   r   r   rS     s
    )#r(   r)   r*   	itertoolscountr%   r   r   loggerdebugr,   r'   r-   r   r7   nextsorteditemsoperator
itemgetterlistkeysr    r.   opr   targetrI   r3   rD   r4   callabler5   appendvalues)r   Znodes_orderZnew_partition_idrH   rL   r   Zmerge_candidatesr<   _Zmerge_candidates_listr8   r9   Znodes_reassignmentZis_tuple_outputuserr   Zdefault_non_compute_opsr4   Zpartitions_to_removerV   Zcompute_node_countr   rE   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   rP   r   r   rU   r   r   r   rS   %  rT   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>rl   )rY   rZ   r	   r'   )r   rk   rl   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 )NrO   )rb   r   rc   )r   )r4   r   r   is_non_compute_node-  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noderV   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)rb   Zall_input_nodes)r   rV   rp   Zinput_n)ro   is_transparent_input_nodetransparent_input_nodesr   r   rt   7  s&   
z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    rq   rr   )rb   r.   )r   rV   rp   Zoutput_n)ro   is_transparent_output_nodetransparent_output_nodesr   r   rv   L  s*   
z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r*   r4   r   r   r/   r    rK   )r   rk   rV   r   r   r   )ro   rt   rv   r4   ru   rw   r   remove_bookend_non_compute_ops*  sF   


z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| j||d}|S )Nrm   )ri   rn   )r   rl   rk   Zfused_gmr   r   r   partition_and_fuseu  s   z-CapabilityBasedPartitioner.partition_and_fuse)FNN)rj   )r"   r#   r$   r   r   boolr   r   r   r   r   r7   r`   r
   ri   rn   rx   ry   r   r   r   r   r1   5   s<    


 R
Kr1   )r(   rW   loggingr^   collections.abcr   r   typingr   Ztorch.fx.graph_moduler   Ztorch.fx.noder   r   Z torch.fx.passes.operator_supportr   Z!torch.fx.passes.utils.fuser_utilsr	   	getLoggerr"   rY   setLevelWARNINGr
   r&   r1   r   r   r   r   <module>   s   
