
    sgsB                         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m	Z	 d dl
mZ d dlmZmZmZmZmZmZ d dlZd dlZddgZd	 Z e       Z ed
      e G d d                    Z ed
       G d d             Zy)    )	dataclassfield)defaultdictN)NodeGraph)compatibility)DictListSetAnyUnionTupleSubgraphMatcherInternalMatchc                  |   t        j                  t              } t        j                  j                  dd      j                         }| j                  |       t        j                         }t        j                  d      }|j                  |       |j                  |       | j                  |       d| _        | S )NPYTORCH_MATCHER_LOGLEVELWARNINGz%(filename)s > %(message)sF)logging	getLogger__name__osenvirongetuppersetLevelStreamHandler	FormattersetFormatter
addHandler	propagate)loggerlevelconsole	formatters       V/var/www/html/venv/lib/python3.12/site-packages/torch/fx/passes/utils/matcher_utils.py_init_loggerr&      s    x(FJJNN5yAGGIE
OOE##%G!!">?I#U
gFM    F)is_backward_compatiblec                       e Zd ZU ee   ed<    ee      Ze	eef   ed<    ee
      Zee   ed<    ee
      Zee   ed<    ee      Ze	eef   ed<   d Zy)	r   anchors)default_factory	nodes_mapplaceholder_nodesreturning_nodesname_node_mapc                     t        | j                  | j                  j                         | j                  j                         | j
                  j                               S )N)r*   r,   r-   r.   )r   r*   r,   copyr-   r.   )selfs    r%   __copy__zInternalMatch.__copy__4   sJ    T\\T^^=P=P=R/3/E/E/J/J/L-1-A-A-F-F-HJ 	Jr'   N)r   
__module____qualname__r
   r   __annotations__r   dictr,   r	   listr-   r.   r/   strr3    r'   r%   r   r   "   sp     $Z"'"=ItD$J= %*$$?tDz? #("=OT$Z= &+4%@M4T	?@Jr'   c                       e Zd Z	 	 	 	 ddedededededdfdZd	ed
edefdZd	ed
edefdZde	eef   defdZ
dee   dee   fdZd	ed
ededefdZd	ed
ededefdZdedee   fdZy)r   patternmatch_outputmatch_placeholderremove_overlapping_matchesignore_literalsreturnNc                    || _         || _        || _        || _        || _        t        |j                        dk(  rt        d      |j                  D ]1  }|j                  dk7  st        |j                        dkD  r,J d        |j                  D cg c]  }|j                  dk(  s| c}| _
        t        t        t        |j                                    }|j                  | _        g | _        |r	|g| _        y|j                  D cg c]  }t        |j                        dk(  s| c}| _        yc c}w c c}w )a  
        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.
            ignore_literals: If True, will not check if literals are equal and
                will instead treat them as wildcards.
        r   z;SubgraphMatcher cannot be initialized with an empty patternoutputzDSubgraphMatcher cannot be initialized with an pattern with dead codeplaceholder   N)r<   r=   r>   r?   r@   lennodes
ValueErroropuserspattern_placeholder_nodesnextiterreversedall_input_nodespattern_returning_nodespattern_anchors)	r2   r<   r=   r>   r?   r@   nodenoutput_nodes	            r%   __init__zSubgraphMatcher.__init__;   s$   $ (!2*D'.w}}"Z[[MM 	^Dww("4::* ^]^*	^ 6=]])\addmF[!)\&4 7893>3N3N$+-$/=D  0;/J/J#`!cRSRYRYl^_N_A#`D  *] $as   D9*D9D>,D>pngnc                 X   t        |j                  t              sJ d|j                   d       t        |j                  t              sJ d|j                   d       dt        j                  j
                  dt        fd} ||j                  j                  |j                        } ||j                  j                  |j                        }t        |      t        |      k7  ryt        |t        j                        rt        |t        j                        S t        d| d	      )
Nz
pn.target z must be a string.z
gn.target model	attr_namec                 v    |j                  d      ^ }}| }|D ]  }t        ||d       }|J  t        ||      S )N.)splitgetattr)rY   rZ   prefixr   titems         r%   _getattrz3SubgraphMatcher._match_attributes.<locals>._getattrp   sO    &__S1NVUA %AtT*}$}% 1e$$r'   FzUnsupported type z when matching attributes)
isinstancetargetr9   torchfxGraphModulegraphowning_moduletypeTensorRuntimeError)r2   rV   rW   rb   pn_valuegn_values         r%   _match_attributesz!SubgraphMatcher._match_attributesj   s    "))S)UZ		{BT+UU)"))S)UZ		{BT+UU)	%EHH00 	%S 	% BHH22BII>BHH22BII>>T(^+ h-h55!28*<UVWWr'   c                     | j                   s|j                  dk(  ry|j                  |j                  k(  rY|j                  dk(  s|j                  dk(  ry|j                  dk(  r| j                  ||      S |j                  |j                  k(  S y)NrD   TrC   get_attrF)r>   rI   ro   rd   )r2   rV   rW   s      r%   _nodes_are_equalz SubgraphMatcher._nodes_are_equal   sw    %%"%%=*@55BEE>uu%():*$--b"5599		))r'   r,   c                     |j                         D ci c]  \  }}|j                  dk7  s|| }}}|j                         D ],  \  }}|| j                  v r|j                  D ]	  }||vs  y . yc c}}w )NrD   FT)itemsrI   rP   rJ   )r2   r,   rV   rW   lookupusers         r%   _is_containedzSubgraphMatcher._is_contained   s    
 ;D//:K#fBruuXeOeBG#f#flln 		!FBT111 ! v% 	!		!  $gs
   A4A4matchesc                 N   g }t               }|D ]  }d}|j                  j                         D ]  \  }}|j                  dvs||v sd} n |rA|j	                  |       |j                  j                         D ]%  \  }}|j                  dvs|j                  |       '  |S )NF>   rC   rD   T)setr,   rt   rI   appendadd)r2   rx   non_overlapping_matchesnodes_matchedmatchfound_overlaprV   rW   s           r%   _remove_overlapping_matchesz+SubgraphMatcher._remove_overlapping_matches   s    79#&5 	.E!M////1 B55 99bM>Q$(M
 !'..u5#oo335 .FBuu$==%))"-.	. '&r'   r   c                    t        |t              rt        |t              rJ d       t        |t              rPt        |t              s@|j                  dk(  r0||j                  v r|j                  |   |k(  S ||j                  |<   yyt        |t              st        |t              ryt	        |      t	        |      k(  xr ||k(  S )Nzpn and gn cannot both be NoderD   TF)rc   r   rI   r,   rj   )r2   rV   rW   r   s       r%   _match_literalszSubgraphMatcher._match_literals   s    r4(ZD-AcDccBb$
2t(<uu% ( ??2."44&(#B%*R*>8tBx'4B"H4r'   c                    	
 t         j                  d||       t        |t              rt        |t              sJ t	        d| d|              |j
                  v rj
                  |   |k(  S |j
                  j                         v ry j                  ||      syt        j                        }|j
                  |<   |j                  dk(  ryd}dt        t        t        f   dt        t        t        f   d	t        f	 fd
	d\  }}t        |j                        t        |j                        k7  sGt!        |j"                  j%                               t!        |j"                  j%                               k7  r|j                  dk(  rt        |j&                  t(        j*                  j,                        ra|j&                  j.                  j0                  

fd} ||j                  |j"                        } ||j                  |j"                        }nt        |j                        t        |j                        k(  rt!        |j"                  j%                               t!        |j"                  j%                               k(  rt!        |j                        }t!        |j                        }|j3                  t!        |j"                  j                                      |j3                  t!        |j"                  j                                      nd}|xr |d uxr |d uxr	  	||      }|st        j                  |      yy)Nz  matching %s to %szpn and gn must be Node, pn: z, gn: FrD   Targs1args2rA   c                 |   t        |       t        |      k7  ryt        | |      D ]  \  }}t        |t              r$t        |t              rj	                  ||      }nWt        |t
        t        f      r t        |t
        t        f      r
 ||      }n!j                  ||      xs j                  }|r y y)NFT)	rF   ziprc   r   _match_nodesr8   tupler   r@   )r   r   a1a2matched_match_argsr   r2   s        r%   r   z1SubgraphMatcher._match_nodes.<locals>._match_args   s    5zSZ'eU+ 	!Bb$'Jr4,@"//B>GT5M2z"tUm7T)"b1G"222r5AYTEYEYG 	! r'   )NNcall_functionc                     g }t              D ]|  \  }}|j                  |v r|j                  ||j                            3|j                  s#|t	        |       k  r|j                  | |          b|j                  |j
                         ~ |S )N)	enumeratenamer{   
kwarg_onlyrF   default_value)	orig_argsorig_kwargsall_argsischemaargs_schemas        r%   get_all_argumentsz7SubgraphMatcher._match_nodes.<locals>.get_all_arguments  s~    !*;!7 >IAv{{k1 FKK(@A#..1s9~3E 	!5 (<(<=>  r'   )r!   inforc   r   r9   r,   valuesrr   r1   rI   r   r
   r   boolrF   argsr8   kwargskeysrd   re   _ops
OpOverload_schema	argumentsextend)r2   rV   rW   r   saved_matchmatch_foundpn_argsgn_argsr   r   r   s   `  `     @@r%   r   zSubgraphMatcher._match_nodes   s   )2r2"d#
2t(<pcD`ac`ddjkmjnBo>pp<  ??2&",, ''))$$R, ii&  55M! 	uT5[1 	%e:L 	QU 	$ &\S\)T"))..2B-CtBIINNL\G]-]EE_$ryy%**"7"78))++55K	  (;G';G\S\)d299>>3C.DRYY^^M]H^.^277mG277mGNN4		 0 0 234NN4		 0 0 234K  *4*4* )	 	 IIk*Er'   rh   c                 d    ddl m} t        t              } j                  D ]:  }|j
                  D ])  } j                  ||      s||   j                  |       + < t        |j                               t        j                  d       g  fdt         j                        }r	 d|       t              }D cg c]   } j                  |j                        s|" c}t              }||k7  rt        j                  d||z
         g }	D ]X  }|j                  j                         D 
cg c]  \  }
}|
j                  dvs| c}}
} ||      sH|	j                  |       Z t        |	      t              k7  r+t        j                  dt              t        |	      z
          j                   rEt        |	      } j#                  |	      t              }||k7  rt        j                  d	||z
         t        j                  d
       S c c}w c c}}
w )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_partitionz"Initial match_candidates_list: %s
c                 H   | t        	      k(  rj                  D cg c]  }|j                  |    c}|_        j                  D cg c]  }|j                  |    c}|_        
j                  |       t        j                  d|       y 	|    \  }}t        j                  |      }|D ]g  }t        j                  d||       j                  |||      }|r | dz   |       nt        j                  d||       t        j                  |      }i y c c}w c c}w )NzFound a match: %s
zTrying to match anchor %s to %srE   z Failed to match anchor %s to %s
)rF   rK   r,   r-   rP   r.   r{   r!   r   r1   r   )anchor_indexr   rV   pattern_anchorcandidate_nodesr   rR   r   backtrackingmatch_candidates_listrx   r2   s           r%   r   z+SubgraphMatcher.match.<locals>.backtrackingY  s   s#899IMIgIg*h25??2+>*h'GKGcGc(d)<(d%u%159.CL.Q+NO))E*K' /=~tT"//eL !159KK C^UYZ 		+./ +i(ds   D
D)r*   z<Filtered out %s matches because they are not fully contained>   rC   rD   zfFiltered out %s matches because                           matched subgraph would form a cycle if fusedzAFiltered out %s matches because matched subgraphs are overlappingzMatches returned: %s)!torch.fx.passes.utils.fuser_utilsr   r   r8   rQ   rG   rr   r{   rt   r!   r   r   rF   rw   r,   rI   r?   r   )r2   rh   r   match_candidatesr   rR   r   beforeaftervalid_matchesrV   rW   matched_compute_nodesr   r   rx   s   `            @@@r%   r   zSubgraphMatcher.match'  s	   H 	I 4?t3D"22 	BN B((>$^4;;DAB	B !%%5%;%;%= >9;PQ')	/2 d&:&:; E" W&-UU1C1CEOO1T5UGU?KKVX^afXfg  	,E"'//"7"7"9dBRUUJc=cd "!"78$$U+		,
 }W-KK HILWX[\iXjIjl **'F66}EGLE_agjoaop*G43 V es    H'-H'H,H,)FFTF)r   r4   r5   r   r   rU   r   ro   rr   r	   rw   r
   r   r   r   r   r   r   r:   r'   r%   r   r   9   s#    ',+048).	-a -a#-a$(-a .2-a #'	-a 48	-a^D d t 84 T d tD$J'7 D &'43F '4P]K^ '$5# 53 5} 5 5&Zt Z Zm Z Zxj5 jT-%8 jr'   )dataclassesr   r   collectionsr   r1   re   torch.fxr   r   torch.fx._compatibilityr   typingr	   r
   r   r   r   r   r   r   __all__r&   r!   r   r   r:   r'   r%   <module>r      s    ( #   2 5 5  	o
. 
e,
J J  -J* e,W W -Wr'   