o
    ߗiB                     @   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 g dZedd	G d
d deZedd			ddededededeeeeef f f
ddZedd	dededefddZdS )    )DictTuple)compatibility)Graph)GraphModule)SubgraphMatcher)Module)HolderModulelift_subgraph_as_modulecompare_graphsF)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__ Z/home/ubuntu/transcripts/venv/lib/python3.10/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	    r   gmsubgraph	comp_name
class_namereturnc                 C   s   t i }i }|jD ]S}|jdvrq	|j}t|tsJ |d}|}	| }
|dd D ]}t|	|s9|	|t i  t	|	|}	t	|
|}
q*|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

    )call_moduleget_attr.N)r	   nodesoptarget
isinstancestrsplithasattrr   getattrsetattrr   )r   r    r!   r"   	submoduleorig_to_split_fqn_mappingnr*   target_name_partscurrorig_gmnameleaf_node_name	leaf_noder   r   r   r
      s(   





r
   leftrightc                 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)match_outputmatch_placeholderr   )r   matchlen)r:   r;   matchermatchesr   r   r   r   S   s   	
r   N)r   r   )typingr   r   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.graph_moduler   #torch.fx.passes.utils.matcher_utilsr   torch.nnr   __all__r	   r,   r
   boolr   r   r   r   r   <module>   s2   7