o
    h1                     @   s   d dl mZmZmZmZmZ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Zd dl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dS )    )DictListSetIterableSequenceOptionalDeque)fuse_by_partitions)GraphModule)Node_get_qualified_name)OperatorSupportBaseN)copy)dequec                   @   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   W/var/www/html/ai/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                   @   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ee defddZdee fddZdefddZdS )CapabilityBasedPartitionerFNgraph_moduleoperator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 C   s@   || _ || _|| _|d ur|ng | _|d ur|| _d S g | _d S r   )r.   r/   r0   r1   r2   )r   r.   r/   r0   r1   r2   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_supported5   s   z.CapabilityBasedPartitioner.__is_node_supportedc                    s  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 }| |rH| vrHt|}||| d ||<  D ]}d | | < qJt| }t|d
krq|d }|d
d  D ]}	|||	 qiq.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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  q*t S )Nself_idother_idc                    s   t |  j  | j t  fdd} D ]}|jD ]}| vr0||r0  dS q"q |  _| jD ]}| |< q<|= dS )Nc                    s   t  }||  |rN| }|v rq|v rdS | v r< |  jD ]}|jD ]}| |  jvr9|| q)q$n|jD ]}|| q?| |s
dS )NTF)r   appendpopr   usersr!   )	root_nodestackr   p_node	user_node)
assignmentmerged_nodespartitions_by_idvisitedr   r   dfs_iter_find_cycleM   s*   




ziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleFT)r   r   updater   r;   )r7   r8   rD   r   r?   r@   rB   )rA   rC   r   maybe_merge_partitionC   s   


zLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionr   r   c                    sp   |  v r |    |  |d u r |  d S |vr+| | < t|| gd|< d S | | < | |  d S )N)r   r   )r&   r:   r   r#   )r   r   rF   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>)	itertoolscountr,   r   r   loggerdebugreversedr.   graphr   ._CapabilityBasedPartitioner__is_node_supportednextlistkeysr'   r;   opr   targetgetitemsr0   unionr   r1   callabler2   r9   values)r   new_partition_idrG   rH   r   merge_candidatespartition_idmerge_candidates_listr7   r8   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr1   partitions_to_remove	partitioncompute_node_countr   rF   r   propose_partitions:   s|   ;











z-CapabilityBasedPartitioner.propose_partitions
partitionsc                 C   s    t d t| jdd |D S )NzFusing partitions...c                 S   s   g | ]}t |jqS r   )rV   r   )rL   rh   r   r   r   rM      s    z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>)rP   rQ   r	   r.   )r   rk   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 )NrJ   )rX   r   rY   )r   )r1   r   r   is_non_compute_node   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)rX   all_input_nodes)r   rh   rn   input_n)rm   is_transparent_input_nodetransparent_input_nodesr   r   rt         
z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    ro   rp   )rX   r;   )r   rh   rn   output_n)rm   is_transparent_output_nodetransparent_output_nodesr   r   rx      rv   z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r   r1   r   r   r   r!   r'   )r   rk   rh   r&   r   r   )rm   rt   rx   r1   ru   ry   r   remove_bookend_non_compute_ops   s(   
&&

z9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   }| |}|S r   )rj   rl   )r   rk   fused_gmr   r   r   partition_and_fuse  s   
z-CapabilityBasedPartitioner.partition_and_fuse)FNN)r)   r*   r+   r
   r   boolr   r   r   r   r   rT   r   r   rj   rl   rz   r|   r   r   r   r   r-   "   s.    


 7r-   )typingr   r   r   r   r   r   r   !torch.fx.passes.utils.fuser_utilsr	   torch.fx.graph_moduler
   torch.fx.noder   r    torch.fx.passes.operator_supportr   loggingrN   r   collectionsr   	getLoggerr)   rP   setLevelWARNINGr   r-   r   r   r   r   <module>   s   $ 
