ó
    Eñi$E  ã                   óÞ   • S SK r S SKrS SKrS SKJr  S SKJrJr  S SKJ	r	J
r
  S SKrS SKJrJr  S SKJr  SS/rS	 r\" 5       r\" S
S9\ " S S5      5       5       r\" S
S9 " S S5      5       rg)é    N)Údefaultdict)Ú	dataclassÚfield)ÚAnyÚUnion)ÚGraphÚNode)ÚcompatibilityÚSubgraphMatcherÚInternalMatchc                  ó‚  • [         R                  " [        5      n [        R                  R                  SS5      R                  5       nU R                  U5        [         R                  " 5       n[         R                  " S5      nUR                  U5        UR                  U5        U R                  U5        SU l        U $ )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       Ú`/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/fx/passes/utils/matcher_utils.pyÚ_init_loggerr"      s’   € Ü×ÒœxÓ(€Fä�J‰J�N‰NÐ5°yÓA×GÑGÓI€EØ
‡O�O�EÔÜ×#Ò#Ó%€GÜ×!Ò!Ð">Ó?€IØ×Ñ˜Ô#Ø×Ñ�UÔà
×Ñ�gÔØ€FÔØ€Mó    F)Úis_backward_compatiblec                   ó¤   • \ rS rSr% \\   \S'   \" \S9r	\\\4   \S'   \" \S9r
\\   \S'   \" \S9r\\   \S'   \" \S9r\\\4   \S'   S rS	rg
)r   é$   Úanchors)Údefault_factoryÚ	nodes_mapÚplaceholder_nodesÚreturning_nodesÚname_node_mapc                 ó¾   • [        U R                  U R                  R                  5       U R                  R                  5       U R
                  R                  5       S9$ )N)r'   r)   r*   r+   )r   r'   r)   Úcopyr*   r+   )Úselfs    r!   Ú__copy__ÚInternalMatch.__copy__6   sJ   € ÜØ—L‘LØ—n‘n×)Ñ)Ó+Ø"×4Ñ4×9Ñ9Ó;Ø ×0Ñ0×5Ñ5Ó7ñ	
ð 	
r#   © N)r   Ú
__module__Ú__qualname__Ú__firstlineno__Úlistr	   Ú__annotations__r   Údictr)   r*   r+   r,   Ústrr0   Ú__static_attributes__r2   r#   r!   r   r   $   so   ‡ ð �$‰ZÓá"'¸Ñ"=€Iˆt�D˜$�JÑÓ=ñ %*¸$Ñ$?Ð�t˜D‘zÓ?ñ #(¸Ñ"=€O�T˜$‘ZÓ=ñ &+¸4Ñ%@€M�4˜˜T˜	‘?Ó@õ
r#   c                   ó  • \ rS rSr    SS\S\S\S\S\SS4S	 jjrS
\S\S\4S jrSS
\S\S\	S\4S jjr
S\\\4   S\4S jrS\\   S\\   4S jrS
\S\S\S\4S jr SS
\S\S\S\	S\4
S jjrSS\S\	S\\   4S jjrSrg)r   é?   ÚpatternÚmatch_outputÚmatch_placeholderÚremove_overlapping_matchesÚignore_literalsÚreturnNc                 óÚ  • Xl         X l        X0l        X@l        XPl        [        UR                  5      S:X  a  [        S5      eUR                   HP  nUR                  S:w  d  M  UR                  5       (       a  M,  [        UR                  5      S:X  d  MG  [        S5      e   UR                   Vs/ s H  owR                  S:X  d  M  UPM     snU l        [        [        [        UR                  5      5      5      nUR                   U l        / U l        U(       a	  U/U l        gUR                    Vs/ s H   n[        UR                  5      S:X  d  M  UPM"     snU l        gs  snf s  snf )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@   rA   ÚlenÚnodesÚ
ValueErrorÚopÚ	is_impureÚusersÚAssertionErrorÚpattern_placeholder_nodesÚnextÚiterÚreversedÚall_input_nodesÚpattern_returning_nodesÚpattern_anchors)	r/   r=   r>   r?   r@   rA   ÚnodeÚnÚoutput_nodes	            r!   Ú__init__ÚSubgraphMatcher.__init__A   s0  € ð* ŒØ(ÔØ!2ÔØ*DÔ'Ø.Ôäˆw�}‰}Ó Ó"ÜØMóð ð —M”MˆDØ�w‰w˜(Õ"¨4¯>©>×+;Ó+;Ü�t—z‘z“? aÕ'Ü(Ø^óð ñ "ð —}’}ó*
Ú$�!¯©°Ñ(=�A‘}ñ*
ˆÔ&ô œ4¤¨¯©Ó 7Ó8Ó9ˆà3>×3NÑ3NˆÔ$à+-ˆÔÞØ$/ =ˆDÕ ð
 '×6Ò6ó$Ú6�a¼#¸a¿g¹g»,È!Ñ:K—Ñ6ñ$ˆDÕ ùò*
ùò$s   Â1E#ÃE#Ä3E(ÅE(ÚpnÚgnc                 óÂ  • [        UR                  [        5      (       d  [        SUR                   S35      e[        UR                  [        5      (       d  [        SUR                   S35      e[        R
                  R                  R                  UR                  R                  UR                  5      n[        R
                  R                  R                  UR                  R                  UR                  5      n[        U5      [        U5      La  g[        U[        R                  5      (       a  [        U[        R                  5      $ [        SU S35      e)Nz
pn.target z must be a string.z
gn.target FzUnsupported type z when matching attributes)Ú
isinstanceÚtargetr9   rM   ÚtorchÚfxÚgraph_moduleÚ	_get_attrÚgraphÚowning_moduleÚtypeÚTensorÚRuntimeError)r/   rZ   r[   Úpn_valueÚgn_values        r!   Ú_match_attributesÚ!SubgraphMatcher._match_attributes{   s÷   € ä˜"Ÿ)™)¤S×)Ñ)Ü  :¨b¯i©i¨[Ð8JÐ!KÓLÐLÜ˜"Ÿ)™)¤S×)Ñ)Ü  :¨b¯i©i¨[Ð8JÐ!KÓLÐLä—8‘8×(Ñ(×2Ñ2°2·8±8×3IÑ3IÈ2Ï9É9ÓUˆÜ—8‘8×(Ñ(×2Ñ2°2·8±8×3IÑ3IÈ2Ï9É9ÓUˆä�‹>¤ h£Ò/Øô �h¤§¡×-Ñ-Ü˜h¬¯©Ó5Ð5äÐ!2°8°*Ð<UÐVÓWÐWr#   Únode_name_matchc                 ó`  • U R                   (       d  UR                  S:X  a  gU(       a  X2R                  ;   a  gUR                  UR                  :X  a[  UR                  S:X  d  UR                  S:X  a  gUR                  S:X  a  U R                  X5      $ UR                  UR                  :H  $ g)NrE   TrD   Úget_attrF)r?   rJ   Únamerj   r^   )r/   rZ   r[   rl   s       r!   Ú_nodes_are_equalÚ SubgraphMatcher._nodes_are_equal�   s„   € à×%×%¨"¯%©%°=Ó*@Øæ˜·'±'Ó9Øà�5‰5�B—E‘E‹>Ø�u‰u˜Ó%¨¯©°(Ó):ØØ—‘˜*Ó$Ø×-Ñ-¨bÓ5Ð5Ø—9‘9 §	¡	Ñ)Ð)Ør#   r)   c                 ó  • UR                  5        VVs0 s H  u  p#UR                  S:w  d  M  X2_M     nnnUR                  5        H1  u  p2X R                  ;   a  M  UR                   H  nXT;  d  M
      g   M3     gs  snnf )NrE   FT)ÚitemsrJ   rS   rL   )r/   r)   rZ   r[   ÚlookupÚusers         r!   Ú_is_containedÚSubgraphMatcher._is_contained    s   € ð "+§¡Ô!2ô$
Ú!2‘v�r°b·e±e¸}Ñ6L‹FˆBŠFÑ!2ð 	ñ $
ð —l‘l–n‰FˆBà×1Ñ1Ó1ÙàŸœ�ð Õ%Ú ó	 !ñ %ð ùó$
s
   ”A=®A=Úmatchesc                 óp  • / n[        5       nU H£  nSnUR                  R                  5        H   u  pgUR                  S;  d  M  Xs;   d  M  Sn  O   U(       a  ML  UR	                  U5        UR                  R                  5        H(  u  pgUR                  S;  d  M  UR                  U5        M*     M¥     U$ )NF>   rD   rE   T)Úsetr)   rs   rJ   ÚappendÚadd)r/   rx   Únon_overlapping_matchesÚnodes_matchedÚmatchÚfound_overlaprZ   r[   s           r!   Ú_remove_overlapping_matchesÚ+SubgraphMatcher._remove_overlapping_matchesµ   s¤   € ð 8:ÐÜ#&£5ˆãˆEØ!ˆMØŸ/™/×/Ñ/Ö1‘�Ø—5‘5Ð 9Õ9¸bÕ>QØ$(�MÙñ 2÷
 !�=Ø'×.Ñ.¨uÔ5Ø#Ÿo™o×3Ñ3Ö5‘F�BØ—u‘uÐ$=Õ=Ø%×)Ñ)¨"Ö-ó 6ñ ð 'Ð&r#   r   c                 óÚ  • [        U[        5      (       a   [        U[        5      (       a  [        S5      e[        U[        5      (       aV  [        U[        5      (       dA  UR                  S:X  a0  XR                  ;   a  UR                  U   U:H  $ X#R                  U'   gg[        U[        5      (       d  [        U[        5      (       a  g[        U5      [        U5      L =(       a    X!:H  $ )Nzpn and gn cannot both be NoderE   TF)r]   r	   rM   rJ   r)   re   )r/   rZ   r[   r   s       r!   Ú_match_literalsÚSubgraphMatcher._match_literalsÉ   s´   € Ü�bœ$×Ñ¤J¨r´4×$8Ñ$8Ü Ð!@ÓAÐAä�bœ$×Ñ¬
°2´t×(<Ñ(<Ø�u‰u˜Ó%ð Ÿ™Ó(Ø Ÿ?™?¨2Ñ.°"Ñ4Ð4à&(—‘ Ñ#ØàÜ˜B¤×%Ñ%¬*°R¼×*>Ñ*>Øä˜“8œt B›xÐ'×4¨B©HÐ4r#   c                 ó
  ^ ^^
^• [         R                  SX5        [        U[        5      (       a  [        U[        5      (       d  [	        SU SU 35      eUTR
                  ;   a  TR
                  U   U:H  $ UTR
                  R                  5       ;   a  gT R                  XU5      (       d  g[        R                  " T5      nUTR
                  U'   UR                  S:X  a  gSnS[        [        [        4   S[        [        [        4   S	[        4U
UU 4S
 jjm
Su  px[        UR                  5      [        UR                  5      :w  dH  [        UR                   R#                  5       5      [        UR                   R#                  5       5      :w  a¥  UR                  S:X  a•  [        UR$                  [&        R(                  R*                  5      (       ab  UR$                  R,                  R.                  mU4S jn	U	" UR                  UR                   5      nU	" UR                  UR                   5      nGO[        UR                  5      [        UR                  5      :X  a×  [        UR                   R#                  5       5      [        UR                   R#                  5       5      :X  a�  [        UR                  5      n[        UR                  5      nUR1                  [        UR                   R                  5       5      5        UR1                  [        UR                   R                  5       5      5        OSnU=(       a    US L=(       a    US L=(       a    T
" Xx5      nU(       d  [        R                  " U5      mgg)Nz  matching %s to %szpn and gn must be Node, pn: z, gn: FrE   TÚargs1Úargs2rB   c                 ó´  >• [        U 5      [        U5      :w  a  g[        X5       H¯  u  p#[        U[        5      (       a(  [        U[        5      (       a  TR	                  X#T5      nOd[        U[
        [        45      (       a$  [        U[
        [        45      (       a	  T" X#5      nO%TR                  X#T5      =(       d    TR                  nU(       a  M¯    g   g)NFT)	rG   Úzipr]   r	   Ú_match_nodesr6   Útupler„   rA   )r‡   rˆ   Úa1Úa2ÚmatchedÚ_match_argsr   r/   s        €€€r!   r�   Ú1SubgraphMatcher._match_nodes.<locals>._match_argsþ   s®   ø€ Ü�5‹zœS ›ZÓ'Øä˜eÖ+‘�Ü˜b¤$×'Ñ'¬J°r¼4×,@Ñ,@Ø"×/Ñ/°¸Ó>‘GÜ ¤T¬5 M×2Ñ2´zÀ"ÄtÌUÀm×7TÑ7TÙ)¨"Ó1‘Gð ×,Ñ,¨R°UÓ;×S¸t×?SÑ?Sð ÷ �wÙ ñ ,ð r#   )NNÚcall_functionc                 ó2  >• / n[        T5       H„  u  p4UR                  U;   a  UR                  XR                     5        M4  UR                  (       d$  U[	        U 5      :  a  UR                  X   5        Mi  UR                  UR
                  5        M†     U$ )N)Ú	enumeratero   r{   Ú
kwarg_onlyrG   Údefault_value)Ú	orig_argsÚorig_kwargsÚall_argsÚiÚschemaÚargs_schemas        €r!   Úget_all_argumentsÚ7SubgraphMatcher._match_nodes.<locals>.get_all_arguments  ss   ø€ Ø�Ü!*¨;Ö!7‘I�AØ—{‘{ kÓ1Ø Ÿ™¨·K±KÑ(@ÖAØ#×.×.°1´s¸9³~Ó3EØ Ÿ™¨	©Ö5à Ÿ™¨×(<Ñ(<Ö=ñ "8ð  �r#   )r   Úinfor]   r	   rM   r)   Úvaluesrp   r.   rJ   r   r6   rŒ   ÚboolrG   ÚargsÚkwargsÚkeysr^   r_   Ú_opsÚ
OpOverloadÚ_schemaÚ	argumentsÚextend)r/   rZ   r[   r   rl   Úsaved_matchÚmatch_foundÚpn_argsÚgn_argsr�   r�   rœ   s   `  `      @@r!   r‹   ÚSubgraphMatcher._match_nodesÝ   s¥  û€ ô 	�‰Ð)¨2Ô2ä˜2œt×$Ñ$¬°B¼×)=Ñ)=Ü Ð#?À¸tÀ6È"ÈÐ!NÓOÐOð �—‘Ó Ø—?‘? 2Ñ&¨"Ñ,Ð,ð �—‘×'Ñ'Ó)Ó)Øà×$Ñ$ R¨_×=Ñ=Øô —i’i Ó&ˆØ ˆ�‰˜Ñð �5‰5�MÓ!Øð ˆð	œu¤T¬5 [Ñ1ð 	¼%ÄÄeÀÑ:Lð 	ÔQU÷ 	ñ 	ð( &Ñˆô �B—G‘G“¤ B§G¡G£Ó,Ü˜Ÿ	™	Ÿ™Ó(Ó)¬T°"·)±)·.±.Ó2BÓ-CÓCà—‘˜Ó(Ü˜2Ÿ9™9¤e§j¡j×&;Ñ&;×<Ñ<àŸ)™)×+Ñ+×5Ñ5ˆKõ	 ñ (¨¯©°·±Ó;ˆGÙ'¨¯©°·±Ó;ŠGä�—‘‹\œS §¡›\Ó)¬d°2·9±9·>±>Ó3CÓ.DÌØ�I‰I�N‰NÓóI
ó /
ô ˜2Ÿ7™7“mˆGÜ˜2Ÿ7™7“mˆGØ�N‰Nœ4 §	¡	× 0Ñ 0Ó 2Ó3Ô4Ø�N‰Nœ4 §	¡	× 0Ñ 0Ó 2Ó3Õ4àˆKð ÷ .Ø˜tÐ#÷.à˜tÐ#÷.ñ ˜GÓ-ð	 	ö ä—I’I˜kÓ*ˆEØàr#   rc   c                 ó²  ^ ^^^^• SSK Jn  [        [        5      nT R                   HB  nUR
                   H/  nT R                  XVT5      (       d  M  XE   R                  U5        M1     MD     [        UR                  5       5      m[        R                  ST5        / mUUUUU 4S jm[        T R                  S9nT(       a	  T" SU5        [        T5      nT Vs/ s H'  nT R                  UR                  5      (       d  M%  UPM)     snm[        T5      n	X‰:w  a  [        R                  SX‰-
  5        / n
T Hc  nUR                  R                  5        VVs/ s H  u  p¼UR                  S;  d  M  UPM     nnnU" U5      (       d  MR  U
R                  U5        Me     [        U
5      [        T5      :w  a+  [        R                  S[        T5      [        U
5      -
  5        T R                   (       aD  [        U
5      nT R#                  U
5      m[        T5      n	X‰:w  a  [        R                  S	X‰-
  5        [        R                  S
T5        T$ s  snf s  snnf )a  
Returns:
    The matched subgraphs.
    The 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                 ó^  >• U [        T	5      :X  a„  TR                   Vs/ s H  o!R                  U   PM     snUl        TR                   Vs/ s H  o!R                  U   PM     snUl        T
R                  U5        [        R                  SU5        g T	U    u  p4[        R                  " U5      nU Hl  n[        R                  SX65        TR                  X6UT5      nU(       a  T" U S-   U5        O[        R                  SX65        [        R                  " U5      nMn     g s  snf s  snf )NzFound a match: %s
zTrying to match anchor %s to %srF   z Failed to match anchor %s to %s
)rG   rN   r)   r*   rS   r+   r{   r   rŸ   r.   r‹   )Úanchor_indexr   rZ   Úpattern_anchorÚcandidate_nodesrª   rU   r«   ÚbacktrackingÚmatch_candidates_listrx   rl   r/   s           €€€€€r!   rµ   Ú+SubgraphMatcher.match.<locals>.backtrackingu  s  ø€ ØœsÐ#8Ó9Ó9à26×2PÒ2Pó+Ú2P¨B—O‘O BÔ'Ñ2Pñ+�Ô'ð 37×2NÒ2Nó)Ú2N¨B—O‘O BÔ'Ñ2Nñ)�Ô%ð —‘˜uÔ%ä—‘Ð1°5Ô9Øà.CÀLÑ.QÑ+ˆNÜŸ)š) EÓ*ˆKã'�Ü—‘Ð=¸~ÔTà"×/Ñ/Ø"¨%°ó�ö á  °Ñ!1°5Õ9ä—K‘KØ;¸^ôô
 Ÿ	š	 +Ó.’ò (ùò+ùò)s   ŸD%ÁD*)r'   z<Filtered out %s matches because they are not fully contained>   rD   rE   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   r6   rT   rH   rp   r{   rs   r   rŸ   r   rG   rv   r)   rJ   r@   r�   )r/   rc   rl   r°   Úmatch_candidatesr³   rU   r   ÚbeforeÚafterÚvalid_matchesrZ   r[   Úmatched_compute_nodesrµ   r¶   rx   s   ` `           @@@r!   r   ÚSubgraphMatcher.matchC  s  ü€ õH 	Iô 4?¼tÓ3DÐØ"×2Ô2ˆNØŸœ�Ø×(Ñ(¨¸×OÓOØ$Ñ4×;Ñ;¸DÖAó $ñ 3ô !%Ð%5×%;Ñ%;Ó%=Ó >Ðä�‰Ð9Ð;PÔQà')ˆ÷	/ñ 	/ôB  d×&:Ñ&:Ñ;ˆÞ Ù˜˜EÔ"ô �W“ˆÙ&-ÓU¢g˜U°×1CÑ1CÀEÇOÁO×1T—5¡gÑUˆÜ�G“ˆØ‹?Ü�K‰KØNØ‘ôð ˆÛˆEð $Ÿo™o×3Ñ3Ô5ô%â5‘F�BØ—5‘5Ð 9Ñ9÷ Ù5ð "ñ %ñ
 "Ð"7×8Ó8Ø×$Ñ$ UÖ+ñ ô ˆ}Ó¤ W£Ó-Ü�K‰KðHä�G“œs =Ó1Ñ1ôð ×*×*Ü˜Ó'ˆFØ×6Ñ6°}ÓEˆGÜ˜“LˆEØ‹Ü—‘ØWØ‘Nôô
 	�‰Ð*¨GÔ4àˆùòK Vùó%s   Ã$IÄIÅIÅ1I)rA   r>   r?   r=   rT   rN   rS   r@   )FFTF)Ú )r   r3   r4   r5   r   r¡   rX   r	   rj   r9   rp   r8   rv   r6   r   r�   r   r„   r‹   r   r:   r2   r#   r!   r   r   ?   sL  † ð
 #Ø"'Ø+/Ø %ñ8àð8ð ð8ð  ð	8ð
 %)ð8ð ð8ð 
õ8ðt Dð ¨dð °tô ñ* 4ð ¨Tð ÀCð ÐQUõ ð  t¨D°$¨JÑ'7ð ¸Dô ð*'Ø˜MÑ*ð'à	ˆmÑ	ô'ð(5 #ð 5¨3ð 5°}ð 5Èô 5ð* PRñdØðdØ ðdØ)6ðdØILðdà	õdñL~˜5ð ~°3ð ~ÀÀ]Ñ@S÷ ~ð ~r#   )r.   r   r   Úcollectionsr   Údataclassesr   r   Útypingr   r   r_   Útorch.fxr   r	   Útorch.fx._compatibilityr
   Ú__all__r"   r   r   r   r2   r#   r!   Ú<module>rÆ      s�   ðã Û Û 	Ý #ß (ß ã ß  Ý 1ð ˜oÐ
.€òñ 
‹€ñ  eÑ,Ø
÷
ð 
ó ó -ð
ñ2  eÑ,÷Að Aó -ñAr#   