o
    Zh_B                     @   s   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
 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)copy)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   P/var/www/auris/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                   @   sF   e Zd ZdefddZdedee fddZdedee fdd	Zd
S )_DependencyViewergraph_modulec                 C   s   t t| _t t| _|jjD ]}|jD ]}| j| | | j| 	| j|  qqt
|jjD ]}|jD ]}| j| | | j| 	| j|  q7q2d S r   )collectionsdefaultdictset	upstreamsdownstreamsgraphr   all_input_nodesaddr   reversedusers)r   r(   r   Z
input_nodeZoutput_noder   r   r   r   (   s   

z_DependencyViewer.__init__r   r   c                 C   
   | j | S r   )r-   r   r   r   r   downstreams_of8   r   z _DependencyViewer.downstreams_ofc                 C   r3   r   )r,   r   r   r   r   upstreams_of;   r   z_DependencyViewer.upstreams_ofN)	r#   r$   r%   r   r   r   r+   r4   r5   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(   r7   r8   r9   r:   r'   dependency_viewer)r   r(   r7   r8   r9   r:   r   r   r   r   @   s   z#CapabilityBasedPartitioner.__init__r   c                 C   s   | j t| j |S r   )r7   Zis_node_supportedr   r(   Znamed_modulesr   r   r   r   Z__is_node_supportedS   s   z.CapabilityBasedPartitioner.__is_node_supportedc                    s  t t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 ]S}i }|r^| vr^t|}|||< ||< ||| d ||< t d
d dD ]\}}d ||< qht| }	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sBt	d ddh}|tj}g } D ]>\}}d}|jD ]*}|jdkr,t|jsJ t|j|vr|d7 }t|jjv r,|d7 }q|dkr8|| q|D ]}|= q;t	d  D ]\}}t	d|dd |jD  qKdd   D S )Nself_idother_idc                    s   t  j   j dtt f fdd}t } D ]}|jD ]}| vr4|| q)q$||r<dS   _ jD ]}|< qF= t  < =   < = dS )Nall_user_nodesc                    sz   | D ]8}t  }j|D ],}|v r  dS | v r9 | }||v r$q| }|v s0|v r4  dS || qqdS )NTF)r+   r;   r4   r0   )r>   	user_nodeZvisited_partition_idsZ	path_nodepartition_idZp_map)
assignmentmerged_nodesr=   partition_mapr   r<   r   r   dfs_iter_find_cycles   s    
ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)	r   r   r   r+   r   r2   r0   minunion)r<   r=   rD   r>   r   r?   rA   rC   partitions_by_idZpartitions_orderr   )rB   r=   r<   r   maybe_merge_partitionn   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r=| | < t|| gd|< || | d S | | < | |  || | d S )Nr   r   c                    s|   | j D ]} |d }|d ur| | | |  qj| }|D ]} |d }|d ur;| | q(d S r   )r2   getr0   r   r;   r5   )r   r   r?   Z	target_idZupstream_nodesZ	curr_nodeZ	source_id)rA   rC   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   rK   )rA   rC   rH   r   r   r   merge_single_node   s   zHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_nodezProposing partitions...c                 S   s   | d S )N   r   )itemr   r   r   <lambda>   s    z?CapabilityBasedPartitioner.propose_partitions.<locals>.<lambda>)keyrN   r   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"   rU   	partitionr   r   r   rV   &  s    )!r)   r*   r+   	itertoolscountr&   r   r   loggerdebugr1   r(   r.   r   ._CapabilityBasedPartitioner__is_node_supportednextsorteditemslistkeysr!   r2   opr   targetrJ   r8   rF   r9   callabler:   appendvalues)r   Znodes_orderZnew_partition_idrI   rM   r   Zmerge_candidatesr@   _Zmerge_candidates_listr<   r=   Znodes_reassignmentZis_tuple_outputuserr   Zdefault_non_compute_opsr9   Zpartitions_to_removerY   Zcompute_node_countr   rG   r   propose_partitionsX   s   
 C
!











z-CapabilityBasedPartitioner.propose_partitionsfused_
partitionsprefixc                 C   s$   t d t| jdd |D |dS )NzFusing partitions...c                 S   rS   r   r   rX   r   r   r   rV   1  rW   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>rn   )r\   r]   r
   r(   )r   rm   rn   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 )NrR   )rd   r   re   )r   )r9   r   r   is_non_compute_node9  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noderY   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)rd   r/   )r   rY   rr   Zinput_n)rq   is_transparent_input_nodetransparent_input_nodesr   r   rv   C  s    

z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    rs   rt   )rd   r2   )r   rY   rr   Zoutput_n)rq   is_transparent_output_nodetransparent_output_nodesr   r   rx   X  s$   

z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r+   r9   r   r   r0   r!   rL   )r   rm   rY   r    r   r   )rq   rv   rx   r9   rw   ry   r   remove_bookend_non_compute_ops6  sF   


z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| j||d}|S )Nro   )rk   rp   )r   rn   rm   Zfused_gmr   r   r   partition_and_fuse  s   z-CapabilityBasedPartitioner.partition_and_fuse)FNN)rl   )r#   r$   r%   r   r	   boolr   r   r   r   r   r^   rb   r   rk   rp   rz   r{   r   r   r   r   r6   ?   s<    


 T
Kr6   )r)   rZ   loggingcollections.abcr   r   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#   r\   setLevelWARNINGr   r'   r6   r   r   r   r   <module>   s   
