U
    ©Ê¹j  ã                   @   s    d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 dddgZ
e	d	d
�G dd„ deƒƒZe	d	d
�deeeedœdd„ƒZe	d	d
�eeedœdd„ƒZdS )é    )ÚModule)ÚGraphModule)ÚGraph)ÚSubgraphMatcher)ÚcompatibilityÚHolderModuleÚlift_subgraph_as_moduleÚcompare_graphsF)Zis_backward_compatiblec                       s    e Zd ZdZ‡ fdd„Z‡  ZS )r   zy
    HolderModule is used to copy all the attributes from original module to submodules
    that uses the attributes
    c                    s,   t ƒ  ¡  | ¡ D ]\}}|  ||¡ qd S )N)ÚsuperÚ__init__ÚitemsÚ
add_module)ÚselfÚdÚkÚv©Ú	__class__© ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/fx/passes/utils/common.pyr      s    
zHolderModule.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r      s   r   )ÚgmÚsubgraphÚ
class_nameÚreturnc                 C   s°   t i ƒ}|jD ]”}|jdkrq|j}t|tƒs2t‚| d¡}|}| }|dd… D ]2}	t||	ƒsn| 	|	t i ƒ¡ t
||	ƒ}t
||	ƒ}qP|d }
t
||
ƒ}t||
|ƒ qt|||ƒS )aB  
    Create a GraphModule for subgraph, which copies the necessary attributes from the original parent graph_module.

    Args:
        gm (GraphModule): parent graph module

        subgraph (Graph): a valid subgraph that contains copied nodes from the parent graph

        class_name (str): name for the submodule

    )Zcall_moduleZget_attrÚ.Néÿÿÿÿ)r   ZnodesÚopÚtargetÚ
isinstanceÚstrÚAssertionErrorÚsplitÚhasattrr   ÚgetattrÚsetattrr   )r   r   r   Ú	submoduleÚnr"   Ztarget_name_partsÚcurrZorig_gmÚnameZleaf_node_nameZ	leaf_noder   r   r   r      s$    





)ÚleftÚrightr   c                 C   s$   t | ddd�}| |¡}t|ƒdkS )zó
    Return True if two graphs are identical, i.e they
        - have the same number of outputs in the same order
        - have the same number of inputs in the same order
        - have the same set of nodes, and identical connectivity
    T)Zmatch_outputZmatch_placeholderr   )r   ÚmatchÚlen)r.   r/   ZmatcherÚmatchesr   r   r   r	   G   s    	
N)r   )Ztorch.nnr   Ztorch.fx.graph_moduler   Ztorch.fx.graphr   Z#torch.fx.passes.utils.matcher_utilsr   Ztorch.fx._compatibilityr   Ú__all__r   r$   r   Úboolr	   r   r   r   r   Ú<module>   s   
.