U
    yhC                     @   s   d dl mZmZ 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eeeeef f dddZed
deeeddd	ZdS )    )DictTuple)compatibility)Graph)GraphModule)SubgraphMatcher)Module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__ N/var/www/html/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	comp_name
class_namereturnc                 C   s   t i }i }|jD ]}|jdkr"q|j}t|ts6t|d}|}	| }
|dd D ]2}t|	|sr|		|t i  t
|	|}	t
|
|}
qT|d }t
|
|}| d| ||< t|	|| qt||||fS )aw  
    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

        comp_name (str): name for the new component

        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    r!   	submoduleZorig_to_split_fqn_mapping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)r2   r3   Zmatchermatchesr   r   r   r   T   s    	
N)r   r   )typingr   r   Ztorch.fx._compatibilityr   Ztorch.fx.graphr   Ztorch.fx.graph_moduler   Z#torch.fx.passes.utils.matcher_utilsr   Ztorch.nnr   __all__r	   r(   r
   boolr   r   r   r   r   <module>   s(   
  7