U
    Jc3                     @   s   d dl mZmZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlm  mZ d dlmZmZmZmZ d dlZdd	gZeeZed
deG dd	 d	Zed
dG dd dZdS )    )	dataclassfield)defaultdictN)Graph)Node)compatibility)DictListSetAnySubgraphMatcherInternalMatchF)Zis_backward_compatiblec                   @   sh   e Zd ZU ee ed< eedZe	eef ed< ee
dZee ed< ee
dZee ed< dd ZdS )	r   anchors)default_factory	nodes_mapplaceholder_nodesreturning_nodesc                 C   s$   t | j| j | j | j dS )N)r   r   r   r   )r   r   r   copyr   r   self r   G/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/passes/utils/matcher_utils.py__copy__   s    zInternalMatch.__copy__N)__name__
__module____qualname__r	   r   __annotations__r   dictr   r   listr   r   r   r   r   r   r   r      s
   
c                   @   s   e Zd ZdeeeeddddZeeeddd	Zeeef ed
ddZ	e
e e
e dddZeeeedddZeeeedddZee
e dddZdS )r   FTN)patternmatch_outputmatch_placeholderremove_overlapping_matchesreturnc                 C   s   || _ || _|| _|| _t|jdkr.td|jD ]$}|jdkr4t|jdks4t	dq4dd |jD | _
ttt|j}|j| _g | _|r|g| _ndd |jD | _dS )	al  
        Args:
            pattern: the targeted matching pattern, represented in fx.Graph.
            match_output: If True, output node in the pattern graph will be treated as a part of the targeted pattern.
                If False, output node is ignored during match.
            match_placeholder: If True, placeholder node in the pattern graph will be treated as a part of
                the targeted pattern. If False, placeholder nodes will be used a wildcard.
            remove_overlapping_matches: If True, in the case of overlapping matches, only the first match
                will be returned.
        r   z;SubgraphMatcher cannot be initialized with an empty patternoutputzDSubgraphMatcher cannot be initialized with an pattern with dead codec                 S   s   g | ]}|j d kr|qS )placeholderop.0nr   r   r   
<listcomp>B   s     
 z,SubgraphMatcher.__init__.<locals>.<listcomp>c                 S   s   g | ]}t |jd kr|qS )   )lenusersr(   r   r   r   r+   M   s      N)r   r    r!   r"   r-   nodes
ValueErrorr'   r.   AssertionErrorpattern_placeholder_nodesnextiterreversedZall_input_nodespattern_returning_nodespattern_anchors)r   r   r    r!   r"   nodeZoutput_noder   r   r   __init__$   s$    


zSubgraphMatcher.__init__)pngnr#   c                 C   sH   | j s|jdkrdS |j|jkrD|jdks4|jdkr8dS |j|jkS dS )Nr%   Tr$   F)r!   r'   target)r   r:   r;   r   r   r   _nodes_are_equalO   s    z SubgraphMatcher._nodes_are_equal)r   r#   c                 C   s\   dd |  D }|  D ]<\}}|jdkr.q|| jkr:q|jD ]}||kr@  dS q@qdS )Nc                 S   s   i | ]\}}||qS r   r   r)   r:   r;   r   r   r   
<dictcomp>]   s      z1SubgraphMatcher._is_contained.<locals>.<dictcomp>r%   FT)itemsr'   r6   r.   )r   r   lookupr;   r:   userr   r   r   _is_containedZ   s    


zSubgraphMatcher._is_contained)matchesr#   c                 C   s   t  }t }|D ]l}d}|j D ]"\}}|jdkr"||kr"d} qFq"|s|| |j D ]\}}|jdkr^|| q^q|S )NF>   r$   r%   T)r   setr   r@   r'   appendadd)r   rD   Znon_overlapping_matchesZnodes_matchedmatchZfound_overlapr:   r;   r   r   r   _remove_overlapping_matchesn   s    

z+SubgraphMatcher._remove_overlapping_matches)r:   r;   rH   r#   c                 C   s   t |trt |trtdt |trft |tsf|jdkr`||jkrR|j| |kS ||j|< dS dS n0t |ts~t |tr~dS t|t|ko||kS d S )Nzpn and gn cannot both be Noder%   TF)
isinstancer   r1   r'   r   type)r   r:   r;   rH   r   r   r   _match_args   s    


zSubgraphMatcher._match_argsc                 C   s  t d| d|  t|tr*t|tsBttd| d| ||jkrZ|j| |kS ||j krldS | ||s|dS t		|}||j|< |j
dkrdS d}t|j\}}t|j\}}|j |j kr
|j D ]$}	||j|	  ||j|	  qnd}|rt|t|krt||D ]P\}
}t|tr`t|
tr`| |
||}n| |
||}|s0d} qq0nd}|st		|}dS dS )Nz  matching  to zpn and gn must be Node, pn: z, gn: Fr%   T)loggerinforJ   r   r1   strr   valuesr=   r   r'   pytreeZtree_flattenargskwargskeysrF   r-   zip_match_nodesrL   )r   r:   r;   rH   saved_matchmatch_foundZpn_flatten_args_Zgn_flatten_argskeyZpn_Zgn_matchedr   r   r   rW      sB    ,




zSubgraphMatcher._match_nodes)graphr#   c                    sh  ddl m} tt}jD ]*}|jD ]}||r$|| | q$qt| g  fdd t	jd} d| t
}fddD t
}||krtd||  d	 g }	D ]*}d
d |j D }
||
r|	| qt
|	t
kr"tdt
t
|	  d jrdt
|	}|	t
}||krdtd||  d S )a  
        Returns:
            The matched subgraphs.
            Thre returned subgraph would be fully self-contained, meaning the nodes (except placeholder
            and nodes returned by output) can only be consumed by nodes within the matched subgraph.

        Subgraph pattern matcher is implemented with the backtracking style in the following steps:

        1. We first identify all the anchor nodes in the pattern graph. The anchor nodes
        are the "sinks" (nodes with no user other than the output node) of the pattern graph.
        One pattern graph could have multiple anchors if it has multiple return values.

        2. In the target graph, we identify the potential candidate nodes that can be matched
        with each anchor. These anchor-candidate pairs are the starting points for
        pairwise per-node matching.

        3. For each anchor-candidate pair, we simultaneously traverse backwards (DFS) in both
        pattern and target graphs. For every pattern nodes along traversal path, we compare it
        against the target nodes. In case any comparison failed, the match for this anchor-candidate
        pair fails. A match is found when DFS completes traversing the graph. See `self._match_nodes`
        for more details.

        4. In the case of multiple anchors, every anchor will need to find a match using step 3.
        In addition, the matches found between anchors need to have a common intersection node
        in order for the match to be valid. This is implemented with backtracking. See `backtracking`
        for more details.

        Notice: graph traversal must be done in the reverser order because a tensor can have multiple
        consumers, but can only have a single producer. Only with reverser order, we can we jointly
        traverse the pattern and target graph in a deterministic path.

        Warning: In theory, this backtracking algorithm have an **exponential** time complexity. However,
        in practice, it's unlikely to blow up.

        r   )validate_partitionc                    s   | t krX fddjD  _ fddjD  _  td  d d S |  \}}t }|D ]^}td| d|  	|| }|r| d   ntd	| d| d t| qrd S )
Nc                    s   g | ]} j | qS r   r   r)   r:   rH   r   r   r+      s     z?SubgraphMatcher.match.<locals>.backtracking.<locals>.<listcomp>c                    s   g | ]} j | qS r   r_   r`   ra   r   r   r+      s     zFound a match: 
zTrying to match anchor rM   r,   zFailed to match anchor )
r-   r2   r   r6   r   rF   rN   rO   r   rW   )Zanchor_indexrH   pattern_anchorZcandidate_nodesrX   r8   rY   backtrackingZmatch_candidates_listrD   r   ra   r   re      s    

z+SubgraphMatcher.match.<locals>.backtracking)r   c                    s   g | ]}  |jr|qS r   )rC   r   )r)   rH   r   r   r   r+     s      z)SubgraphMatcher.match.<locals>.<listcomp>zFiltered out z- matches because they are not fully containedc                 S   s   g | ]\}}|j d kr|qS )>   r$   r%   r&   r>   r   r   r   r+   #  s     
 zW matches because                           matched subgraph would form a cycle if fusedz2 matches because matched subgraphs are overlapping)Z!torch.fx.passes.utils.fuser_utilsr^   r   r   r7   r/   r=   rF   r@   r   r-   rN   rO   r   r"   rI   )r   r]   r^   Zmatch_candidatesrc   r8   rH   beforeafterZvalid_matchesZmatched_compute_nodesr   rd   r   rH      s>    $




zSubgraphMatcher.match)FFT)r   r   r   r   boolr9   r   r=   r   rC   r	   r   rI   r   rL   rW   rH   r   r   r   r   r   "   s       +:)Zdataclassesr   r   collectionsr   r   Ztorch.fx.graphr   Ztorch.fx.noder   Ztorch.fx._compatibilityr   Ztorch.utils._pytreeutilsZ_pytreerR   typingr   r	   r
   r   logging__all__	getLoggerr   rN   r   r   r   r   r   r   <module>   s   
