o
    #i=                     @   s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ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 )    )fuse_by_partitionsN)copy)DictIterableListOptionalSequenceSet)GraphModule)Node_get_qualified_name)OperatorSupportBasec                   @   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 t | _d S N)r   setr   )selfr   r    r   _/home/ubuntu/SoloSpeech/.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      | j | d S r   )r   addr   r   r   r   r   add_node      zPartition.add_nodec                 C   r   r   )r   remover    r   r   r   remove_node   r"   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defaultdictr   	upstreamsdownstreamsgraphr   all_input_nodesr   updatereversedusers)r   r,   r   
input_nodeoutput_noder   r   r   r   $   s   

z_DependencyViewer.__init__r   r   c                 C   
   | j | S r   )r0   r    r   r   r   downstreams_of4   r   z _DependencyViewer.downstreams_ofc                 C   r8   r   )r/   r    r   r   r   upstreams_of7   r   z_DependencyViewer.upstreams_ofN)	r'   r(   r)   r
   r   r   r	   r9   r:   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,   r<   r=   r>   r?   r+   dependency_viewer)r   r,   r<   r=   r>   r?   r   r   r   r   <   s   z#CapabilityBasedPartitioner.__init__r   c                 C   s   | j t| j |S r   )r<   is_node_supporteddictr,   named_modulesr    r   r   r   __is_node_supportedN   s   z.CapabilityBasedPartitioner.__is_node_supportedc                    s  t t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 ]C}i }|rQ| vrQt|}||| d ||<  D ]}d | | < qSt| }t|d
krz|d }|d
d  D ]}	|||	 qrq7t	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s+t	d ddh}|tj}g } D ]<\}}d}|jD ](}|jdkrt|jsJ t|j|vr	|d
7 }t|jjv r|d
7 }q|d
kr!|| q|D ]}|= q$t	d  D ]\}}t	d|dd |jD  q4d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=   < = 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@   r9   r   )rG   	user_nodevisited_partition_ids	path_nodepartition_idp_map)
assignmentmerged_nodesrF   partition_mapr   rE   r   r   dfs_iter_find_cyclef   s    
ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)	r   r   r3   r	   r   r   r5   r   union)rE   rF   rP   rG   r   rH   rM   rO   partitions_by_idr   )rN   rF   rE   r   maybe_merge_partitiona   s&    



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                    st   j | }|D ]} |d }|d ur| | qj | }|D ]} |d }|d ur7| | q$d S r   )r@   r9   getr   r:   )r   r   downstream_nodes	curr_node	target_idupstream_nodes	source_id)rM   rO   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[   rR   r   r   merge_single_node   s   zHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_nodezProposing partitions...   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   s   g | ]}|j qS r   )name).0r   r   r   r   
<listcomp>  s    zACapabilityBasedPartitioner.propose_partitions.<locals>.<listcomp>c                 S   s   g | ]
}|  d kr|qS )r   )r&   ra   	partitionr   r   r   rb     s    ) r-   r.   r   	itertoolscountr*   r   r   loggerdebugr4   r,   r1   r   ._CapabilityBasedPartitioner__is_node_supportednextlistkeysr%   r5   opr   targetrU   itemsr=   rQ   r>   callabler?   appendvalues)r   new_partition_idrT   r]   r   merge_candidatesrK   merge_candidates_listrE   rF   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr>   partitions_to_removerd   compute_node_countr   rR   r   propose_partitionsS   s~   
 <
"










z-CapabilityBasedPartitioner.propose_partitionsfused_
partitionsprefixc                 C   s$   t d t| jdd |D |dS )NzFusing partitions...c                 S   s   g | ]}t |jqS r   )rk   r   rc   r   r   r   rb     s    z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>r   )rg   rh   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 ].}t  }|jD ]} |r[||j|sV||j|r[|| qBt|d
krh|j| |_q:d S )Nr   c                    s   | j dkot| j v S )Nr_   )rm   r   rn   )r   )r>   r   r   is_non_compute_node  s   
zVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_noderd   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)rm   r2   )r   rd   r   input_n)r   is_transparent_input_nodetransparent_input_nodesr   r   r        
z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    r   r   )rm   r5   )r   rd   r   output_n)r   is_transparent_output_nodetransparent_output_nodesr   r   r   .  r   z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r   r>   r   r	   r   r   r%   )r   r~   rd   r$   r   r   )r   r   r   r>   r   r   r   remove_bookend_non_compute_ops  s(   
&&

z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| j||d}|S )Nr   )r|   r   )r   r   r~   fused_gmr   r   r   partition_and_fuseK  s   z-CapabilityBasedPartitioner.partition_and_fuse)FNN)r}   )r'   r(   r)   r
   r   boolr   r   r   r   r   ri   r   r   r|   r   r   r   r   r   r   r   r;   :   s.    


 8
7r;   )!torch.fx.passes.utils.fuser_utilsr   r-   re   loggingr   typingr   r   r   r   r   r	   torch.fx.graph_moduler
   torch.fx.noder   r    torch.fx.passes.operator_supportr   	getLoggerr'   rg   setLevelWARNINGr   r+   r;   r   r   r   r   <module>   s    
