U
    JºcÜ'  ã                   @   s®   d dl 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 d dlmZmZ d dlmZ d dlmZ d dlZd dlZe e¡Ze ej¡ G d	d
„ d
ƒZG dd„ dƒZdS )é    )ÚDictÚListÚSetÚIterableÚOptional)Úfuse_by_partitions)ÚNodeList)ÚGraphModule)ÚNodeÚ_get_qualified_name)ÚOperatorSupportBase)ÚdefaultdictNc                   @   sT   e Zd Zdeee dœdd„Zedœdd„Zedœd	d
„Z	edœdd„Z
dd„ ZdS )Ú	PartitionN©ÚidÚnodesc                 C   s"   || _ |d k	rt|ƒntƒ | _d S ©N)r   Úsetr   )Úselfr   r   © r   úE/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/passes/infra/partitioner.pyÚ__init__   s    zPartition.__init__©Úreturnc                 C   s
   t | jƒS r   )Ústrr   ©r   r   r   r   Ú__repr__   s    zPartition.__repr__)Únodec                 C   s   | j  |¡ d S r   )r   Úadd©r   r   r   r   r   Úadd_node   s    zPartition.add_nodec                 C   s   | j  |¡ d S r   )r   Úremover   r   r   r   Úremove_node   s    zPartition.remove_nodec                 C   s
   t | jƒS r   )Úlenr   r   r   r   r   Úsize   s    zPartition.size)NN)Ú__name__Ú
__module__Ú__qualname__Úintr   r
   r   r   r   r    r"   r$   r   r   r   r   r      s
   r   c                   @   s¢   e Zd Zdeeeddœdd„Zeee	e f dœdd„Z
eeed	œd
d„Zeeedœdd„Zedœdd„Zee dœdd„Zee edœdd„Zedœdd„ZdS )ÚCapabilityBasedPartitionerFN)Úgraph_moduleÚoperator_supportÚallows_single_node_partitionr   c                 C   s    || _ || _|| _|  ¡ | _d S r   )r*   r+   r,   Ú1_CapabilityBasedPartitioner__build_dependency_mapÚdependency_map)r   r*   r+   r,   r   r   r   r   $   s    z#CapabilityBasedPartitioner.__init__r   c                 C   sH   t tƒ}| jjjD ]0}|jD ]$}||  |¡ ||  || ¡ qq|S r   )r   r   r*   Úgraphr   Zall_input_nodesr   Úupdate)r   r.   r   Z
input_noder   r   r   Z__build_dependency_map1   s    
z1CapabilityBasedPartitioner.__build_dependency_map)ÚaÚbr   c                 C   s,   || j | krdS || j | kr$dS dS d S )Né   éÿÿÿÿr   )r.   )r   r1   r2   r   r   r   Z__node_depends_on=   s
    z,CapabilityBasedPartitioner.__node_depends_on)Úpartition_aÚpartition_br   c                 C   s<   |j D ]0}|j D ]$}|  ||¡}|dkr|    S qqdS )Nr   )r   Ú,_CapabilityBasedPartitioner__node_depends_on)r   r5   r6   Znode_aZnode_bÚ
dependencyr   r   r   Z__partition_depends_onJ   s    

z1CapabilityBasedPartitioner.__partition_depends_onc                 C   sD   t  d¡ g }| jjjD ]&}| j t| j ¡ ƒ|¡r| 	|¡ q|S )NzCollecting supported nodes...)
ÚloggingÚdebugr*   r/   r   r+   Zis_node_supportedÚdictZnamed_modulesÚappend)r   Zsupported_nodesr   r   r   r   Z__get_supported_nodesY   s    
z0CapabilityBasedPartitioner.__get_supported_nodesc                    s:  |   ¡ }i ‰ i ‰t ¡ }dttt dœ‡ ‡fdd„}t d¡ t|ƒD ]j}i }|j	D ]2}|ˆ krxˆ | }d |ˆ| < qVd |t
|gd< qVt| ¡ ƒ}tt|ƒƒD ]f}	t|	d t|ƒƒD ]N}
||	 }||
 }|  ||¡}|dkrð||krð||= q¸|dkr¸||kr¸||= q¸q¢dd	„ |D ƒ}t|ƒd
kr6||t|ƒƒ qFt|ƒdkrX|d
 }|||ƒ qF|d
 }|||ƒ i }|dd … D ] }ˆ| jD ]}|||< qˆqz|D ]}|||ƒ q qFt d¡ i }| jjjD ]x}d}|j	D ],}|jdksút|jƒdkrÚd} qqÚ|rÌˆ  |d ¡}|j	D ] }ˆ  |d ¡|kr |||< q qÌ| ¡ D ]\}}|||ƒ qN| jsòt d¡ ddh}g }ˆ ¡ D ]T\}}d
}|jD ]*}|jdkrœt|jƒ|krœ|d7 }qœ|dkrŠ| |¡ qŠ|D ]}ˆ|= qät d¡ ˆ ¡ D ](\}}t d|› dd	„ |jD ƒ¡ qtˆ ¡ ƒS )N)r   r   c                    sx   | ˆ kr:ˆ |  }ˆ | = ˆ|   | ¡ ˆ|  ¡ dkr:ˆ|= |d k	rt|ˆ | < |ˆkrft|| gdˆ|< nˆ|  | ¡ d S )Nr   r   )r"   r$   r   r    )r   r   Zoriginal_id©Z
assignmentZpartitions_by_idr   r   Úassigni   s    z=CapabilityBasedPartitioner.propose_partitions.<locals>.assignzProposing partitions...)r   r3   r4   c                 S   s   g | ]}|j d k	r|j ‘qS r   )r   ©Ú.0Ú	partitionr   r   r   Ú
<listcomp>œ   s     
 zACapabilityBasedPartitioner.propose_partitions.<locals>.<listcomp>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 #c                 S   s   g | ]
}|j ‘qS r   )Úname)r@   r   r   r   r   rB   ×   s     )N)Ú0_CapabilityBasedPartitioner__get_supported_nodesÚ	itertoolsÚcountr
   r   r(   Úloggerr:   ÚreversedZusersr   ÚlistÚkeysÚranger#   Ú1_CapabilityBasedPartitioner__partition_depends_onÚnextr   r*   r/   Úopr   ÚtargetÚgetÚitemsr,   r<   Úvalues)r   Ú
candidatesZnew_partition_idr>   r   Zuser_partitionsZ	user_noder   Zuser_partitions_listÚiÚjÚpiZpjr8   Z assigned_candidate_partition_idsZreassignmentZother_idZ
other_nodeZnodes_reassignmentZis_tuple_outputÚuserZnon_compute_opsZpartitions_to_removerA   Zcompute_node_countr   r=   r   Úpropose_partitionsa   s’    


	


ÿ



ÿ


"z-CapabilityBasedPartitioner.propose_partitions)Ú
partitionsr   c                 C   s    t  d¡ t| jdd„ |D ƒƒS )NzFusing partitions...c                 S   s   g | ]}t |jƒ‘qS r   )rJ   r   r?   r   r   r   rB   Þ   s     z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>)rH   r:   r   r*   )r   rZ   r   r   r   Úfuse_partitionsÛ   s    
z*CapabilityBasedPartitioner.fuse_partitionsc                 C   s   |   ¡ }|  |¡}|S r   )rY   r[   )r   rZ   Zfused_gmr   r   r   Úpartition_and_fuseà   s    
z-CapabilityBasedPartitioner.partition_and_fuse)F)r%   r&   r'   r	   r   Úboolr   r   r
   r   r-   r(   r7   r   rM   r   rE   r   rY   r[   r\   r   r   r   r   r)   "   s    ýüzr)   )Útypingr   r   r   r   r   Z!torch.fx.passes.utils.fuser_utilsr   Ztorch.fx.passes.tools_commonr   Ztorch.fx.graph_moduler	   Ztorch.fx.noder
   r   Z torch.fx.passes.operator_supportr   Úcollectionsr   r9   rF   Ú	getLoggerr%   rH   ÚsetLevelÚWARNINGr   r)   r   r   r   r   Ú<module>   s   
