U
    Å9%e‘1  ã                   @   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ee 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   ú`/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/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__r   Ú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eee  eee  ddœdd„Z	e
edœdd„Zee d	œd
d„Zee edœdd„Zee dœdd„Zed	œ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 k	r|ng | _|d k	r0|ng | _d S r   )r-   r.   r/   r0   r1   )r   r-   r.   r/   r0   r1   r   r   r   r   $   s    ÿýz#CapabilityBasedPartitioner.__init__)r    r   c                 C   s   | j  t| j ¡ ƒ|¡S r   )r.   Zis_node_supportedÚdictr-   Znamed_modulesr"   r   r   r   Z__is_node_supported5   s    ÿz.CapabilityBasedPartitioner.__is_node_supportedr   c                    s˜  i ‰ i ‰t  ¡ }ttdœ‡ ‡fdd„}ttt dœ‡ ‡fdd„}t d¡ t| jj	j
ƒD ]†}i }|  |¡rŒ|ˆ krŒt|ƒ}|||ƒ d ||< ˆ D ]}d |ˆ | < qt| ¡ ƒ}t|ƒdkrX|d	 }|dd … D ]}	|||	ƒ qÎqXt d
¡ i }
| jj	j
D ]t}d}|jD ],}|jdks&t|jƒdkrd} q4q|røˆ  |d ¡}|jD ] }ˆ  |d ¡|krJ||
|< qJqø|
 ¡ D ]\}}|||ƒ qv| jsTt d¡ ddh}| t| jƒ¡}g }ˆ ¡ D ]~\}}d	}|j
D ]T}|jdkrÔt|jƒsôt‚t|jƒ|kr|d7 }t|jƒ| jkrÔ|d7 }qÔ|dkrÂ| |¡ qÂ|D ]}ˆ|= qFt d¡ ˆ ¡ D ]$\}}t d|dd„ |j
D ƒ¡ qftˆ ¡ ƒS )N)Úself_idÚother_idc                    s   t ˆ|  jƒ‰ ˆ  ˆ| j¡ tƒ ‰‡‡ ‡‡fdd„}ˆ D ](}|jD ]}|ˆ krD||ƒrD  dS qDq:ˆ ˆ|  _ˆ| jD ]}| ˆ|< qxˆ|= dS )Nc                    sž   t ƒ }| | ¡ |rš| ¡ }|ˆkr&q|ˆkr2dS |ˆ krxˆˆ |  jD ],}|jD ] }|ˆˆ |  jkrR| |¡ qRqHn|jD ]}| |¡ q~ˆ |¡ qdS )NTF)r   ÚappendÚpopr   Úusersr!   )Z	root_nodeÚstackr    Z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   r7   )r3   r4   r>   r    r9   ©r:   r<   )r;   r=   r   Úmaybe_merge_partitionC   s    


zLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition©r    r   c                    sl   | ˆ krˆˆ |     | ¡ |d kr.ˆ  | ¡ n:|ˆkrR|ˆ | < t|| gdˆ|< n|ˆ | < ˆ|  | ¡ d S )Nr   )r%   r6   r   r#   rB   r@   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&   r7   Úopr   ÚtargetÚgetÚitemsr/   Úunionr   r0   ÚcallableÚAssertionErrorr1   r5   Úvalues)r   Znew_partition_idrA   rC   r    Zmerge_candidatesZpartition_idZmerge_candidates_listr3   r4   Znodes_reassignmentZis_tuple_outputÚuserr   Zdefault_non_compute_opsr0   Zpartitions_to_removeÚ	partitionZcompute_node_countr   r@   r   Úpropose_partitions:   st    ;



ÿ






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   )rQ   r   )rG   r\   r   r   r   rH   Ù   s     z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>)rK   rL   r	   r-   )r   r^   r   r   r   Úfuse_partitionsÖ   s    
z*CapabilityBasedPartitioner.fuse_partitions)r^   c                    sÎ   t | jƒ‰tdœ‡fdd„‰ i ‰i ‰ttt tt dœ‡ ‡‡fdd„‰ttt tt dœ‡ ‡‡fdd„‰|D ]\}t ƒ }|jD ]2}ˆ |ƒr|ˆ||j|ƒs¤ˆ||j|ƒr|| |¡ q|t|ƒd	krl|j| |_qld S )
Nr   c                    s   | j dkot| jƒˆ kS )NrE   )rS   r   rT   r   )r0   r   r   Úis_non_compute_nodeß   s    
ÿzVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node)r    r\   Úremoved_nodesc                    st   | j dks| |ks| |krdS | ˆkr.ˆ|  S ˆ | ƒrh| jD ]}ˆ|||ƒs<dˆ| <  dS q<dˆ| < dS dˆ| < dS ©NÚplaceholderTF)rS   Zall_input_nodes)r    r\   ra   Zinput_n)r`   Úis_transparent_input_nodeÚtransparent_input_nodesr   r   rd   ç   s    
z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodec                    st   | j dks| |ks| |krdS | ˆkr.ˆ|  S ˆ | ƒrh| jD ]}ˆ|||ƒs<dˆ| <  dS q<dˆ| < dS dˆ| < dS rb   )rS   r7   )r    r\   ra   Zoutput_n)r`   Úis_transparent_output_nodeÚtransparent_output_nodesr   r   rf   ö   s    
z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noder   )r   r0   r   r   r   r!   r&   )r   r^   r\   r%   r    r   )r`   rd   rf   r0   re   rg   r   Úremove_bookend_non_compute_opsÜ   s"    
""
ÿþz9CapabilityBasedPartitioner.remove_bookend_non_compute_opsc                 C   s   |   ¡ }|  |¡}|S r   )r]   r_   )r   r^   Zfused_gmr   r   r   Úpartition_and_fuse  s    
z-CapabilityBasedPartitioner.partition_and_fuse)FNN)r(   r)   r*   r
   r   Úboolr   r   r   r   r   rO   r   r   r]   r_   rh   ri   r   r   r   r   r,   "   s"      û

ú 7r,   )Útypingr   r   r   r   r   r   r   Z!torch.fx.passes.utils.fuser_utilsr	   Ztorch.fx.graph_moduler
   Ztorch.fx.noder   r   Z torch.fx.passes.operator_supportr   ÚloggingrI   r   Úcollectionsr   Ú	getLoggerr(   rK   ÚsetLevelÚWARNINGr   r,   r   r   r   r   Ú<module>   s   $
