
    Ph                       U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlZd dlmc 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'm(Z( d dl)m*Z*m+Z+ ddl,m-Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m-Z- ddl9m:Z: ddl;m<Z<  ejz                  e>      Z?ej                  j                  ZAej                  j                  ZBeZCeeCejl                  jJ                  f   ZD G d d      ZE eE       ZF G d d      ZG G d deH      ZIdidZJ G d d      ZK G d d      ZL G d  d!eL      ZM G d" d#eL      ZN G d$ d%eL      ZO G d& d'eL      ZP G d( d)eL      ZQ G d* d+eQ      ZR G d, d-eR      ZS G d. d/eR      ZT G d0 d1eR      ZU G d2 d3eQ      ZV G d4 d5eV      ZW G d6 d7eV      ZX G d8 d9eV      ZY G d: d;eL      ZZ G d< d=eL      Z[ G d> d?eL      Z\ G d@ dA      Z]ej                   G dB dC             Z_ej                   G dD dEe_             Z`ej                   G dF dGe_             Zaej                   G dH dIe_             ZbdJ ZcecdKdKdf	 	 	 djdLZd e.j                  dMN      	 dk	 dldO       ZfecfdMdP	 dmdQZgecfdMdP	 dmdRZhdndSZi ej                  dT      ZkdodUZldpdVZmdqdWZndrdXZo G dY dZ      Zpdsd[Zq	 	 	 	 dt	 dld\Zr ej                         dud]       Zt ej                         dud^       Zvdvd_Zwdwd`ZxdxdaZydb ZzdydcZ{ e|       Z}dde~de<   	 dz	 	 	 	 	 d{dfZd|dgZd}dhZy)~    )annotationsN)defaultdict)
AnyCallableDefaultDictDictIterableListNoReturnOptionalSetUnion)	TypeGuard)enable_python_dispatcher)counters)is_integer_dtype)Node)make_fxmaybe_disable_fake_tensor_mode)immutable_dictimmutable_list   )config)aot_functionmake_boxed_func)default_partition)FakeTensorMode)Transformer   )select_decomp_table)%fallback_node_due_to_unsupported_typec                      e Zd Zy)MultipleN)__name__
__module____qualname__     jC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_inductor/pattern_matcher.pyr#   r#   6   s    r(   r#   c                  t     e Zd ZdZdd fdZedd       ZddZddZd Z	ddZ
ddZdd	Zd
 ZddZ xZS )Matchz4
    Represents a successfully matched pattern.
    c                    t         |           || _        |xs g | _        |xs i | _        g | _        i | _        d | _        d | _        y N)	super__init__patternargskwargsnodestargetsctxreplacement_graph)selfr0   r1   r2   	__class__s       r)   r/   zMatch.__init__C   sH    JB	l*,
@B+/;?r(   c                J    | j                   sJ | j                   j                  S r-   )r5   graphr7   s    r)   r:   zMatch.graphP   s    xxxxx~~r(   c                2   | j                   rwt        | j                   j                               t        |j                   j                               z  D ]-  }| j                   |   |j                   |   k7  s#t        d|       | j                  j                  |j                         | j                  j                  |j                         | j                   j                  |j                          | j                  j                  |j                         y )Nzkwarg mismatch: {})	r2   setkeysFailedMatchr1   extendr3   updater4   )r7   otherkeys      r)   r@   zMatch.extendU   s    ;;4;;++-.U\\5F5F5H1II;;s#u||C'88%&:C@@ J 			$

%++&5<<(EMM*r(   c                f    | j                   rt        | j                         g| _         | S g | _         | S r-   )r1   tupler;   s    r)   bundlezMatch.bundle_   s.    *.))U499%&	 :<	r(   c                <    d| j                    d| j                   dS )NzMatch(..., , )r1   r2   r;   s    r)   __repr__zMatch.__repr__d   s    TYYKr$++a88r(   c                t    t        | j                        D ]   }|j                  r|j                  |       " y r-   )reversedr3   _erased
erase_node)r7   r:   ns      r)   erase_nodeszMatch.erase_nodesg   s*    $**%A99  # &r(   c                    | j                   sJ | j                   j                  D cg c]  }|| j                   j                  |   nd ! c}S c c}w r-   )r5   outputspattern_to_node)r7   ps     r)   output_nodeszMatch.output_nodesl   sV    xxx XX%%
% -.MTXX%%a(tC%
 	
 
s   $Ac                B    t        d | j                         D              S )Nc              3  &   K   | ]	  }|s|  y wr-   r'   ).0rU   s     r)   	<genexpr>z$Match.output_node.<locals>.<genexpr>t   s     82!aA2s   )nextrV   r;   s    r)   output_nodezMatch.output_nodes   s    8t002888r(   c                x    | j                   sJ t        j                  | | j                   j                  ||       y r-   )r5   ReplacementPatternEntryreplace_with_graphr:   )r7   r6   r1   s      r)   r_   zMatch.replace_with_graphv   s-    xxx22$((.."3T	
r(   c                    | j                   sJ |t        } ||t        j                  j	                  |d             }t
        j                  | | j                   j                  ||       y )Nc                     | j                   d   S Nvalmeta)args    r)   <lambda>z*Match.replace_by_example.<locals>.<lambda>   s    sxxr(   )r5   fwd_onlytorchfxmap_argr^   r_   r:   )r7   replacement_fnr1   trace_fnreplacements        r)   replace_by_examplezMatch.replace_by_example|   s]    xxxHEHH,,T3NO
 	 22HHNN		
r(   )NNr0   PatternExpr)returntorch.fx.Graph)rB   r+   )rr   r+   r:   rs   )rr   zList[Optional[torch.fx.Node]])rr   torch.fx.Noder-   )r$   r%   r&   __doc__r/   propertyr:   r@   rF   rK   rQ   rV   r\   r_   ro   __classcell__r8   s   @r)   r+   r+   >   sJ    @  +
9$

9

r(   r+   c                      e Zd Zd Zd Zd Zy)r?   c                f    || _         t        |      dkD  rt        d|       || _        || _        y )N   zUFormat string too long - use lazy construction of strings instead. Format string is
 )format_stringlenRuntimeErrorr1   r2   )r7   r}   r1   r2   s       r)   r/   zFailedMatch.__init__   sB    * }#hivhwx  	r(   c                b     | j                   j                  | j                  i | j                  S r-   )r}   formatr1   r2   r;   s    r)   __str__zFailedMatch.__str__   s(    (t!!(($))Ct{{CCr(   c                     yNFr'   r;   s    r)   __bool__zFailedMatch.__bool__       r(   N)r$   r%   r&   r/   r   r   r'   r(   r)   r?   r?      s    	Dr(   r?   c                    t        |       S )z
    TypeGuards cannot act on `self`. Thus this function exists to let mypy
    recognize FailedMatch.__bool__ as a TypeGuard.
    )bool)ms    r)   is_matchr      s    
 7Nr(   c                  2    e Zd ZdZ	 d	 	 	 	 	 ddZd Zd Zy)MatchContextz:
    State needed while running PatternExpr._match().
    Nc               D    || _         |i n|| _        || _        g | _        y r-   )rS   rT   r:   exclusive_node_set)r7   rS   rT   r:   s       r)   r/   zMatchContext.__init__   s)     %4%<r/
8:r(   c                    || j                   v r(| j                   |   |k(  rt        |      S t        d      S |j                  ||       }|| j                   vsJ |r|nd| j                   |<   | |_        |S )z)wrapper to check reused nodes in patternszrepeated pattern differsN)rT   r+   r?   _matchr5   )r7   r0   noder   s       r)   matchzMatchContext.match   s}    d***##G,4W~%"#=>>NN4&d2222201tW%r(   c                    | j                   j                         D ci c]  \  }}|j                         r||| c}}S c c}}w r-   )rT   itemshas_multiple_users)r7   r0   r   s      r)   filter_multi_user_patternsz'MatchContext.filter_multi_user_patterns   sQ     "&!5!5!;!;!=
!=))+0@ TM!=
 	
 
s   Ar-   )rS   zList[Optional[PatternExpr]]rT   z!Optional[Dict[PatternExpr, Node]]r:   rs   )r$   r%   r&   rv   r/   r   r   r'   r(   r)   r   r      s8     >B
;,
; ;
;
 
;
r(   r   c                  B    e Zd ZdZ	 	 	 	 	 	 ddZd	dZd
dZd ZddZy)rq   z*
    Base class for types of patterns
    c                    t               r-   NotImplementedErrorr7   r   r5   s      r)   r   zPatternExpr._match   s     "##r(   c                    	 t        | g|j                        j                  | |      S # t        $ r}|cY d }~S d }~ww xY wNr:   )r   r:   r   r?   r7   r   es      r)   r   zPatternExpr.match   s<    	djj9??dKK 	H	s   '* 	?:??c                     yr   r'   r;   s    r)   r   zPatternExpr.has_multiple_users   r   r(   c                4    | j                   j                  dz   S )Nz())r8   r$   r;   s    r)   rK   zPatternExpr.__repr__   s    ~~&&--r(   c              #  L   K   | |j                   v r|j                   |     y y wr-   )rT   r7   r5   searcheds      r)   find_anchor_nodeszPatternExpr.find_anchor_nodes   s*     3&&&%%d++ 's   "$N)r   ru   r5   r   rr   Union[Match, FailedMatch]r   ru   rr   r   rr   r   r5   r   )	r$   r%   r&   rv   r   r   r   rK   r   r'   r(   r)   rq   rq      s8    $!$(4$	"$
.,r(   rq   c                      e Zd ZdZddZy)Argzn
    Capture an arg which will become an input to the handler.  Args are
    passed in depth first order.
    c                    t        | |g      S )N)r1   r+   r   s      r)   r   z
Arg._match   s    T''r(   Nr   NodeOrConstantr5   r   r$   r%   r&   rv   r   r'   r(   r)   r   r      s    
(r(   r   c                  &    e Zd ZdZddZd ZddZy)Ignoredz4
    Match an arg, but don't pass it to handler
    c                    t        |       S r-   r   r   s      r)   r   zIgnored._match   s    T{r(   c                     y)N*r'   r;   s    r)   rK   zIgnored.__repr__   s    r(   c                     y)Nz	Ignored()r'   )r7   pps     r)   pretty_printzIgnored.pretty_print   s    r(   Nr   r   PatternPrettyPrinter)r$   r%   r&   rv   r   rK   r   r'   r(   r)   r   r      s    r(   r   c                  2     e Zd ZdZd fdZd ZddZ xZS )
KeywordArgD
    Capture a kwarg which will become an input to the handler.
    c                0    t         |           || _        y r-   r.   r/   namer7   r   r8   s     r)   r/   zKeywordArg.__init__      	r(   c                "    d| j                   dS )NzKeywordArg(rI   r   r;   s    r)   rK   zKeywordArg.__repr__	  s    TYYM++r(   c                4    t        | | j                  |i      S )Nr2   )r+   r   r   s      r)   r   zKeywordArg._match  s    T499d"344r(   )r   strr   r$   r%   r&   rv   r/   rK   r   rx   ry   s   @r)   r   r      s    ,5r(   r   c                  0     e Zd ZdZ fdZd ZddZ xZS )ExclusiveKeywordArgr   c                0    t         |           || _        y r-   r   r   s     r)   r/   zExclusiveKeywordArg.__init__  r   r(   c                "    d| j                   dS )NzExclusiveKeywordArg(rI   r   r;   s    r)   rK   zExclusiveKeywordArg.__repr__  s    %dii]!44r(   c                    ||j                   v rt        d      S |j                   j                  |       t        | | j                  |i      S )Nzexclusive arg appears twicer   )r   r?   appendr+   r   r   s      r)   r   zExclusiveKeywordArg._match  sF    3)))<==%%d+T499d"344r(   r   r   ry   s   @r)   r   r     s    55r(   r   c                  b     e Zd ZU dZdZded<   d fd	ZddZd ZddZ	dd	Z
dd
ZddZ xZS )_TargetExprz7
    Base class for filtering match by node.target
    NOptional[str]opc           
        | j                   st        d      t        |           t	        |      st        |t              r|gn
t        |      }t        |      D ]^  }t        |t        j                  j                        s(|j                  |j                         D cg c]  }t        ||       c}       ` || _        t        |      | _        || _        y c c}w )Nz%Shouldn't directly use _BaseNodeMatch)r   r   r.   r/   callable
isinstancer   listri   _opsOpOverloadPacketr@   	overloadsgetattrfnsr=   fns_setusers)r7   r   r   fnoverloadr8   s        r)   r/   z_TargetExpr.__init__+  s    ww%&MNN}
3(<se$s)s)B"ejj99:

",,.Q.hGB1.QR  :=<?H+0
	 Rs    C
c                t   | j                   d   }t        |t              s|j                  }t	        | j                         dkD  rd| dS | j                   d   t        t        |d       u rd| S t        | j                   d   t        j                  j                        rt        | j                   d         S |S )Nr   r   [z, ...]torch.)	r   r   r   r$   r~   r   ri   r   
OpOverload)r7   
first_reprs     r)   fns_reprz_TargetExpr.fns_repr8  s    XXa[
*c*#,,Jtxx=1zl&))XXa[GE:t<<J<((UZZ%:%:;txx{##r(   c                V    | j                   j                   d| j                          dS N(rI   )r8   r$   r   r;   s    r)   rK   z_TargetExpr.__repr__F  s'    ..))*!DMMO+<A>>r(   c                X    t        | j                  t              xs | j                  dkD  S )Nr   )r   r   r#   r;   s    r)   r   z_TargetExpr.has_multiple_usersI  s     $**h/A4::>Ar(   c                    t               r-   r   r   s      r)   r   z_TargetExpr.find_anchor_nodesL      !##r(   c                    t        |t        j                  j                        xr2 |j                  | j                  k(  xr t        |      | j                  v S r-   )r   ri   rj   r   r   extract_targetr   )r7   r   s     r)   
_match_fnsz_TargetExpr._match_fnsO  sD    tUXX]]+ 5477"5t$4	
r(   c                    | |j                   v xs6 | j                  t        u xs" t        |j                        | j                  k(  S r-   )rS   r   MULTIPLEr~   r   s      r)   _match_usersz_TargetExpr._match_usersV  s=    CKK -zzX%-4::$**,	
r(   )r   )rr   r   r   r   )r   ru   r   ru   r5   r   )r$   r%   r&   rv   r   __annotations__r/   r   rK   r   r   r   r   rx   ry   s   @r)   r   r   $  s8     B1?B$

r(   r   c                  j     e Zd ZdZdd fd
Zedd       Zedd       Zd ZddZ	dd	Z
dd
Z xZS )_TargetArgsExprzE
    Base class for filtering match by node.{target,args,kwargs}
    r   )_usersc               j   t         |   ||       t        |      | _        t	        |      | _        t        d t        j                  ||j                               D              r| j                  | _        n| j                  | _        | j                  | j                  | j
                        | _        y )Nc              3  R   K   | ]  }t        |t        t        t        f       ! y wr-   )r   dictr   rE   rY   xs     r)   rZ   z+_TargetArgsExpr.__init__.<locals>.<genexpr>g  s&      
; q4u-.;   %')r.   r/   rE   r1   r   r2   any	itertoolschainvaluespytree_flattenflattensimple_flattenflat_args_kwargs)r7   r   r   r1   r2   r8   s        r)   r/   z_TargetArgsExpr.__init__c  s    f%$K	6l 
__T6==?;
 
  ..DL..DL $TYY Dr(   c                f    g | |j                         t        |       g|j                         fS r-   )r  r~   r>   rJ   s     r)   r  z_TargetArgsExpr.simple_flattenp  s/    (((3t9*Ev{{}*EEEr(   c                \    dfdt        j                  | |g      \  }} |      }||fS )Nc           
     $   | j                   | S t        t        t        t        t        t
        i}t        j                  |j                  | j                   | j                         | j                  t        t        | j                                    S r-   )typer   r   rE   r   r   pytreeTreeSpecgetcontextmapchildren_specs)smapping	norm_specs     r)   r  z1_TargetArgsExpr.pytree_flatten.<locals>.norm_specv  sf    vv~%tUD.$OG??AFFAFF+		SA$4$456 r(   )r  zpytree.TreeSpec)r
  tree_flatten)r1   r2   flatspecr  s       @r)   r  z_TargetArgsExpr.pytree_flattent  s5    	 (($8
dTzr(   c                   | j                         gt        t        | j                        | j                  j                         D cg c]  \  }}| d|  c}}}| j                  j                   ddj                  |       dS c c}}w )N=r   rH   rI   )	r   r  reprr1   r2   r   r8   r$   join)r7   kvr1   s       r)   rK   z_TargetArgsExpr.__repr__  s    MMO
tyy!
 &*[[%6%6%89%8TQ1QCj%89

 ..))*!DIIdO+<A>> :s   B	c           
        | j                         gfd| j                  D        | j                  j                         D cg c]  \  }}| dj	                  |        c}}}t        | j                  t              r|j                  d       n-| j                  dkD  r|j                  d| j                          d}| j                  j                   d|j                  |       dS c c}}w )	Nc              3  @   K   | ]  }j                  |        y wr-   )r   )rY   r   r   s     r)   rZ   z/_TargetArgsExpr.pretty_print.<locals>.<genexpr>  s     4)Qbooa )s   r  z_users=MULTIPLEr   z_users=rH   r   rI   )r   r1   r2   r   r   r   r   r#   r   r8   r$   r  )r7   r   r  r  r1   
joiner_strs    `    r)   r   z_TargetArgsExpr.pretty_print  s    MMO
4$))4
 7;kk6G6G6IJ6Ida1R__Q'()6IJ

 djj(+KK)*ZZ!^KK'$**./
..))*!JOOD,A+B!DD Ks    C/c                V   | j                  |      r+t        |j                        t        | j                        k7  rt        d||       S | j	                  ||      st        d|       S |j                  }|j
                  }t        |      t        | j
                        k  rddlm}  ||j                  |j                  |j
                        }|t        d||       S |\  }}t        |      t        | j                        k(  rDt        |      t        | j
                        k\  r#|D ci c]  }|| j
                  v s|||    }}n/t        d||       S |D ci c]  }|| j
                  v s|||    }}| j                  ||      \  }}	| j                  \  }
}|	|k7  rt        d|	|      S t        |      t        |
      k(  sJ t        |       }t        t        j                         |
|      D ]x  \  }}}t        |t               r*|j#                  ||      }|s|c S |j%                  |       At        |t&        j(                  j*                        s||k7  skt        d||      c S  |j,                  j/                  |       |j                  |j0                  | <   |S c c}w c c}w )Nz&function_mismatch: node={}, pattern={}zmultiple_users {}r   )normalize_functionzargs_structure {} {}z#constant_args: {} {!r}!={pattern!r})r   r~   r1   r?   r   r2   torch.fx.operator_schemasr   targetr  r  r+   zipr   countr   rq   r   r@   ri   rj   r   r3   r   r4   )r7   r   r5   _args_kwargsr   normalized_args_and_kwargsi
node_items	node_spec
self_items	self_specr   r0   
child_nodechild_matchs                   r)   r   z_TargetArgsExpr._match  sW   t$DII#dii.(HGtTT  s+2D99		++w<#dkk**D);TYY*& *1"#KTSWXX!;wu:TYY/CLCDT4T6=RgdkkAQq'!*}gGR&@$  /6Jgdkk9Iq'!*}gGJ $UG <
I $ 5 5
I	!5y)LL:#j/111$K&))//*;Z&T"Aw
';/!ii<"&&%J6*:O"94  'U 	
t++		$7 S Ks   )J!=J!J&-J&c              #    K   | |j                   v r|j                   |     y| j                  d   D ]  }t        |t              s|j	                  ||      D ]d  }t        |t
        j                  j                        s(|j                  D ].  }||vs| j                  |      s| |j                  |       0 f  yw)a  
        This is used when we are matching a pattern with multiple outputs.
        There is a partial match (stored in ctx) and we want to walk
        this pattern to find a connection to an already-matched node.

        Yields candidate nodes that `self._match` might like.
        Nr   )rT   r  r   rq   r   ri   rj   r   r   r   add)r7   r5   r   r0   
other_noder   s         r)   r   z!_TargetArgsExpr.find_anchor_nodes  s      3&&&%%d++,,Q/G';/")";";C"JJ%j%((--@  * 0 0x/#t4&*
 (T 2	 !1 #K 0s   ACACC&C)r2   zDict[Any, Any]r   r   r   )r$   r%   r&   rv   r/   staticmethodr  r  rK   r   r   r   rx   ry   s   @r)   r   r   ^  sS     +, E F F  ?E0d3r(   r   c                      e Zd ZdZdZy)CallFunctionzR
    Matches a call_function node in the FX graphs: `fns[i](*args, **kwargs)`
    call_functionNr$   r%   r&   rv   r   r'   r(   r)   r4  r4    s     
Br(   r4  c                      e Zd ZdZdZy)
CallMethodzW
    Matches a call_method node in the FX graphs: `fns[i].method(*args, **kwargs)`
    call_methodNr6  r'   r(   r)   r8  r8         
Br(   r8  c                      e Zd ZdZdZy)
CallModulezP
    Matches a call_module node in the FX graphs: `module(*args, **kwargs)`
    call_moduleNr6  r'   r(   r)   r<  r<    r:  r(   r<  c                      e Zd ZdZddZy)_TargetExprVarArgsz[
    Matches a call_function node with any arguments which are passed into the pattern
    c                   | j                  |      st        d      S | j                  ||      st        d      S t        |       }|j                  j                  |       |j                  |j                  | <   |j                  j                  |j                         |j                  j                  |j                         |S )Nfunction_mismatchmultiple_users)r   r?   r   r+   r3   r   r"  r4   r1   r@   r2   rA   )r7   r   r5   r   s       r)   r   z_TargetExprVarArgs._match  s    t$233  s+/00$K	t++		$	dii 	$r(   Nr   r   r'   r(   r)   r?  r?    s    r(   r?  c                      e Zd ZdZy)CallFunctionVarArgsr5  Nr$   r%   r&   r   r'   r(   r)   rD  rD    s    	Br(   rD  c                      e Zd ZdZy)CallMethodVarArgsr9  NrE  r'   r(   r)   rG  rG        	Br(   rG  c                      e Zd ZdZy)CallModuleVarArgsr=  NrE  r'   r(   r)   rJ  rJ    rH  r(   rJ  c                  4     e Zd ZdZdd fdZd ZddZ xZS )ListOfz$
    Matches a repeated pattern
    c                b    t         |           t        |t              sJ || _        || _        y r-   )r.   r/   r   rq   r0   partial)r7   r0   rN  r8   s      r)   r/   zListOf.__init__!  s,    ';///r(   c                N    | j                   j                   d| j                   dS r   )r8   r$   r0   r;   s    r)   rK   zListOf.__repr__'  $    ..))*!DLL>;;r(   c                   t        |t        t        f      rt        |      dk(  rt	        d      S t        |       }|j                         }d}t        |      D ]  \  }}t        |j                  ||j                        }|j                  | j                  |      }	|j                         }|	s| j                  st	        d||	      c S rd}|j                  |	j                                 |st	        d      S |j                         S )Nr   non_listFr   zlist[{}]: {}Tzlist: no_match)r   r   rE   r~   r?   r+   r   	enumerater   rS   r:   r   r0   rN  r@   rF   )
r7   r   r5   r   rT   matchedr(  r-  	child_ctxr.  s
             r)   r   zListOf._match*  s    $u.#d)q.z**$K 88:&t_MAz$_J4D4DI $//$,,
CK'BBDO||&~q+FFGHH['')* - /00xxzr(   Frp   )r   List[torch.fx.Node]r5   r   r   ry   s   @r)   rL  rL    s    <r(   rL  c                  R     e Zd Z fdZed        Zd ZddZd	dZd Z	d
dZ
 xZS )MultiOutputPatternc                b    t         |           t        d |D              sJ |       || _        y )Nc              3  R   K   | ]  }t        |t        t        d       f       ! y wr-   )r   rq   r	  r   s     r)   rZ   z.MultiOutputPattern.__init__.<locals>.<genexpr>F  s!     MW:a+tDz!:;Wr   )r.   r/   allrS   )r7   rS   r8   s     r)   r/   zMultiOutputPattern.__init__D  s.    MWMMVwVM4;r(   c                    | j                   d   rt        | j                   d   d      sJ | j                   d   j                  S )Nr   r   )rS   hasattrr   r;   s    r)   r   zMultiOutputPattern.fnsI  s8    ||A74<<?E#BBB||A"""r(   c                N    | j                   j                   d| j                   dS r   )r8   r$   rS   r;   s    r)   rK   zMultiOutputPattern.__repr__N  rP  r(   c                    | j                   D cg c]  }|j                  |       }}dd }| j                  j                   d|j	                  |       }| d}|S c c}w )Nz,
z  z([z
]))rS   r   r8   r$   r  )r7   r   r   r1   r  str_outs         r)   r   zMultiOutputPattern.pretty_printQ  si    ,0LL9Lq"L94&\
^^,,-R
0E/FGIT"	 :s   Ac                    |j                  | j                  d   |      }|s|S | j                  dd  D ].  }|| j                  ||      }|s|c S |j                  |       0 |S )Nr   r   )r   rS   _match_from_anchorsr@   )r7   r   r5   r   r0   r.  s         r)   r   zMultiOutputPattern._matchX  sm    IIdll1ot,H||AB'G227C@K""HH[! ( r(   c                    t        |j                        }t        d      }|j                  |t	                     D ]*  }|j                  ||      }|r|c S t        |      |_        , |S )Nzno anchor found)r   rT   r?   r   r=   r   )r7   r0   r5   priorr   r   s         r)   rc  z&MultiOutputPattern._match_from_anchorsg  sa    S(())*--c359D		'4(A"&u+C : r(   c                    	 t        | j                  |j                        j                  | |      S # t        $ r}|cY d }~S d }~ww xY wr   )r   rS   r:   r   r?   r   s      r)   r   zMultiOutputPattern.matchr  s>    	DJJ?EEdDQQ 	H	s   03 	AAAAr   r   r   )r$   r%   r&   r/   rw   r   rK   r   r   rc  r   rx   ry   s   @r)   rY  rY  C  s5    <
 # #<	r(   rY  c                  <     e Zd ZdZd fdZed        ZddZ xZS )RepeatedExprzp
    Checks for a repeated pattern. Useful for repeated operations after a node such as `split` or `unbind`
    c                L    t         |           t        |d      sJ || _        y )Nr   )r.   r/   r^  inner_pattern)r7   rj  r8   s     r)   r/   zRepeatedExpr.__init__~  s&    }e,,,*r(   c                .    | j                   j                  S r-   )rj  r   r;   s    r)   r   zRepeatedExpr.fns  s    !!%%%r(   c                t   |j                  | j                  |      }|s|S |j                  j                  | j                         | j                  j	                  |t                     D ]K  }t        | g|j                        j                  | j                  |      }|s|c S |j                  |       M |S r   )	r   rj  rT   popr   r=   r   r:   r@   )r7   r   r5   r   anchor_nodeanchor_ms         r)   r   zRepeatedExpr._match  s    IId(($/H	
  --??SUKK#TF$**=CC""KH HHX L r(   )rj  rq   r   )	r$   r%   r&   rv   r/   rw   r   r   rx   ry   s   @r)   rh  rh  y  s&    +
 & &r(   rh  c                  6    e Zd ZdZd Zeddd       Zd Zd Zy)	r   z
    Serializes Patterns to executable python.
    XXX: currently only used and tested for fuse attention patterns. May not cover
    all patterns.
    c                z    t         j                  j                  j                         | _        i | _        i | _        y r-   )ri   rj   r:   
_Namespace	namespacememoized_objs_namesmemoized_objs_ppr;   s    r)   r/   zPatternPrettyPrinter.__init__  s*    224;= 8:r(   c                *   t               }t        | d      sJ | j                  |      }g }|j                  D ]2  }|j	                  |j                  |    d|j
                  |           4 |j	                  | d|        dj                  |      S )zU
        Serializes obj to python code with obj written out to `output_name`
        r   )r   z = 
)r   r^  r   rt  r   ru  r  )objoutput_namer   out_stroutputrC   s         r)   runzPatternPrettyPrinter.run  s     "#sN+++""b")))CMMR33C89R=P=PQT=U<VWX * 	S	23yy  r(   c                    t        |t              r0| j                  j                  |      x}r|S | j	                  |      S t        |d      r|j                  |       S t        |      S )Nr   )r   r   rt  r  memoizer^  r   r  )r7   rx  memoized_names      r)   r   z!PatternPrettyPrinter.pretty_print  sb    c?+ $ 8 8 < <S AA}A$$||C((3'##D))Cyr(   c                    |j                  |       }|j                         }dD ]  }|j                  |d      } | j                  j	                  |d       }|| j
                  |<   || j                  |<   |S )N)zaten.r   zprims. )r   r   replacers  create_namert  ru  )r7   rx  obj_strobj_nameprefixtmp_names         r)   r~  zPatternPrettyPrinter.memoize  sv    ""4(<<>3F''3H 4 >>--h=(0  %%,c"r(   N)r{  )rx  rq   )	r$   r%   r&   rv   r/   r2  r|  r   r~  r'   r(   r)   r   r     s*    ;
 ! !"		r(   r   c                  2    e Zd ZU ded<   ded<   ddZd	dZy)
PatternEntryrq   r0   zCallable[[Match], bool]extra_checkc                    t               r-   r   r7   r   r:   r   s       r)   applyzPatternEntry.apply  r   r(   Nc                T   |Ht        | j                  d      sJ | j                  j                  D ]  }| j                  |||        y t	        |t
        t        f      r-|r||   j                  d|        y ||   j                  |        y |D ]  }| j                  |||        y )Nr   prependr   )	r^  r0   r   registerr   r   PatternMatcherPassinsertr   )r7   
pass_dictsr"  r  r   r   s         r)   r  zPatternEntry.register  s    >4<<///ll&&j"g> '
T+=$>?6"))!T26"))$/a9  r(   r   r+   r:   rs   r   ru   r   )r$   r%   r&   r   r  r  r'   r(   r)   r  r    s    (($:r(   r  c                       e Zd ZU ded<   ddZy)LoweringPatternEntryCallable[..., Any]handlerc                    t        j                  | j                        t        j                  | j                  |            }|j	                  |      5  |j                  |t        |j                        |j                        }|j                  j                  |j                         |j                  |       d d d        |j                  d   |u sJ |j                  |       y # 1 sw Y   .xY w)N)	functoolswrapsr  rN  inserting_beforer5  rE   r1   r2   re   rA   replace_all_uses_withr3   rQ   )r7   r   r:   r   r  rn   s         r)   r  zLoweringPatternEntry.apply  s    /)//$,,/	0A0A$,,PU0VW##D)--guUZZ7H%,,WK##DII.&&{3 * {{2$&&&%  *)s   A'C))C2Nr  )r$   r%   r&   r   r  r'   r(   r)   r  r    s    !r(   r  c                  $    e Zd ZU dZded<   ddZy)GraphPatternEntryz8
    A pattern that runs a function on the FX graph
    r  r  c                    |j                  |      5   | j                  |g|j                  i |j                   d d d        y # 1 sw Y   y xY wr-   )r  r  r1   r2   r  s       r)   r  zGraphPatternEntry.apply  s<    ##D)DLL<<u||< *))s   +AANr  )r$   r%   r&   rv   r   r  r'   r(   r)   r  r    s      =r(   r  c                  B    e Zd ZU ded<   e	 	 	 	 	 	 	 	 dd       ZddZy)r^   zCallable[..., List[Any]]normalize_argsc                   | j                         }|d   } G fddt        j                  j                        }| j                         }t	        |      dk(  r|d   }n~|d   sJ t        |d   j                  j                        }|D 	cg c]9  }	t        |	t        j                  j                        r|j                  |	      |	f; }
}	t        |
d       d   }fdj                  |      5    ||      j                  | }t        |t        j                  j                        r|g}t	        |      t	        |      k(  sJ t        ||      D ]  \  }}||J ||j                  d        !d|j                   vr%|j                   j#                  |j                          d	|j                   v r ||j                   d	          |j                  |        	 d d d        | j%                         y c c}	w # 1 sw Y    xY w)
Nr   c                  0     e Zd ZdZdZdZd fdZ xZS )<ReplacementPatternEntry.replace_with_graph.<locals>.ReplacerNc                   |j                   dv rt        | 	  |      S |j                   dk(  r|j                  }| j	                  |      \  }}j                  |||      }d|j                  v r}d|j                  vro|j                  d   |j                  d<   t        |j                  d   t        j                        r,d|j                  v sJ |j                  d   |j                  d<   |S t        d|       )N)placeholderr{  r5  rc   tensor_metaz
unhandled )r   r.   run_noder"  fetch_args_kwargs_from_envr5  re   r   ri   Tensorr   )r7   r   r"  r1   r2   resultr8   r:   s         r)   r  zEReplacementPatternEntry.replace_with_graph.<locals>.Replacer.run_node  s    7777 7+D1177o-![[F#'#B#B4#HLD&"00vFF		)e6;;.F-1YYu-=E*%dii&6E#0DII#==#=9==9QFKK6!M)Jtf*=>>r(   )rr   r   )r$   r%   r&   r9  r=  get_attrr  rx   )r8   r:   s   @r)   Replacerr    s    KKH? ?r(   r  r   c                    | d   S )Nr   r'   )tups    r)   rg   z<ReplacementPatternEntry.replace_with_graph.<locals>.<lambda>.  s    SVr(   )rC   c                r    | j                   D ]'  }t        |d      s||j                  d<    ||       ) y )Nre   	recompute)all_input_nodesr^  re   )r   recompute_tagrf   percolate_tagss      r)   r  zBReplacementPatternEntry.replace_with_graph.<locals>.percolate_tags0  s5    ++3',9CHH[)"36 ,r(   rc   r  )rV   ri   rj   Interpreterr~   r   r:   r3   r   r   indexminr  r|  r#  r  re   rA   rQ   )r   r:   r6   r1   rV   
first_noder  	last_noder3   rP   indicesrn   oldnewr  s    `            @r)   r_   z*ReplacementPatternEntry.replace_with_graph  s    ))+!!_
	?uxx++ 	?( ))+|!$QI?"?a..445E &%Aa/ Q#%  
 G);<Q?I	7 ##I.9(#45994@K+uxx}}5*m{#s<'8888k:S;;&;[--d3CHH,1 #chh.&sCHH[,AB--c2) ; /6 	% Q /.s   >G=5A,H"B HHc           
         | j                  |||j                   | j                  |j                  i |j                         y r-   )r_   r6   r  r1   r2   r  s       r)   r  zReplacementPatternEntry.applyS  s<    ##D<u||<		
r(   N)r   r+   r:   rs   r6   rs   r1   z	List[Any]r  )r$   r%   r&   r   r2  r_   r  r'   r(   r)   r^   r^      sJ    ,,L!L!L! *L! 	L! L!\
r(   r^   c                     y)NTr'   )r   s    r)   _return_truer  \  s    r(   r'   c	           	         g t        j                         j                  j                         d fd}	fd}
t        u rt        j                         ryt        j                  d      5  |D cg c]*  }t        |t
        j                        xr |j                  , c}|t         |      }n|}t        j                  |      }|t        vsJ t        j!                  |       t#        ||	|
      }|j%                  |       |j&                  cddd       S c c}w # 1 sw Y   yxY w)a  
    Create a replacement rule based on example functions that get traced
    to create patterns.  This supports both training and inference when
    run on a joint forward+backward graph.

    Args:
        search_fn: traced to give original pattern
        replace_fn: traced to give replacement graph
        example_inputs: example inputs for initial trace
        trace_fn: fwd_only or joint_fwd_bwd
        pass_dict: dict of passes to register to
        extra_check: additional check to run on match(using real shapes)
    c           	        	D ]+  }|| j                   vst        d	 d| j                           t        t        j                  j                  	D cg c]  }| j                   |    c}d             }t        j                  j                  j                  |      5  t              D ]  \  }}t        ||   t        j                        s$|r"t        ||   j                        r
 ddd       yt        j                  ||   j                         ||   j!                         ||   j                  ||   j"                  |      ||<    	  |      }t+        |	
      }|j-                  | j/                         d	         }|r  |      r |      | _        	 ddd       y
	 ddd       yc c}w # t        $ r3}t$        j'                  dj(                  |       Y d}~ddd       yd}~ww xY w# 1 sw Y   yxY w)z
        Often shapes get burned into the pattern, so our initial match ran with
        `ignore_types=(int, ...)`.

        Recheck the match with the correct shapes.
        z_Not all inputs to pattern found in match.kwargs. Perhaps one of the inputs is unused? argnames=z, match.kwargs=c                     | j                   d   S rb   rd   )rP   s    r)   rg   z8register_replacement.<locals>.check_fn.<locals>.<lambda>  s    QVVE]r(   NF)dtypedevicerequires_gradz@Replacement pattern %s failed to apply due to shape mismatch: %s)argnamesexclusive_arg_namesscalar_workaroundr   T)r2   r   r   ri   rj   rk   _dynamoutilsdetect_fake_moderS  r   r  r   r  empty_stridedsizestrider  loginfor$   fx_to_patternr   rV   r6   )r   r   r1   r(  gradspecific_graphr   specific_patternspecific_pattern_matchr  r  r  
replace_fnr  r  	search_fnrm   s            r)   check_fnz&register_replacement.<locals>.check_fnz  s    D5<<'"99A
/RWR^R^Q_a   HH089d#9;R

 ]]  11$7$]34d1gu||4 0a ?$	 87 $11QQ("1gmm#Aw~~&*DG 4!))T!:  -!$7"3	  &6%;%;E<N<N<PQR<S%T"%+6L*M*2:t*D'A 87B C 87 :$   V&&
 + 87 87sP   F6 /G:G:4AG:	F;A
G:,G:;	G7!G2%G:2G77G::Hc                    g }D ]"  }|j                  | j                  |             $ t        dt        |       dz         D ].  }d| | vr n%|j                  | j                  d|              0 | r
J d|        |S )Nr   	tangents_zleftover kwargs: )r   rm  ranger~   )r2   r1   r   r(  r  s       r)   r  z,register_replacement.<locals>.normalize_args  s    DKK

4() q#f+/*A1#f,KK

Yqc?34 + 9.vj99zr(   Ffunctionalize_rng_opsN)r0   r  r  )r   r+   )inspect	signature
parametersr>   joint_fwd_bwdri   is_inference_mode_enabledfunctorch_configpatchr   r  r  gen_patternr   r|  _seen_patternsr0  r^   r  r0   )r  r  example_inputsrm   r  r  r  r  search_fn_patternr  r  r   r0   pattern_reprr  r  s   `` ` ```      @@r)   register_replacementr  `  s)   0 A""9-88==?@H4 4l	 =  **, 
		e	<ES%
ESJq%,,';AOO;^%
 $!!#G (G+//8>111<() )

 	$1 
=	<%
 
=	<s   =D5/D01A5D50D55D>Fr  c                t   g t        j                  |       j                  j                         }|dk(  ri }g }d}|D ]4  }||v r|j	                  ||          |j	                  ||          |dz  }6  || |      }	t        |	t        t        t        t        j                  t        j                  f|||      S )Nr'   r   r   )ignore_typesr  r  r  )r  r  r  r>   r   r  intfloatr   ri   r  r  )
r  r  rm   r  r  r  flat_inputs	input_idxargname	search_gms
             r)   r  r    s     A""9-88==?@HBKI''09:~i89NI  K0I5$ekkB+/ r(   r  c                     fd}|S )z
    Register an aten to inductor IR replacement pattern.  The decorated
    function is saved and then called a lowering time allowing direct
    pattern to inductor IR conversion.
    c                n    t        |       sJ t        |       j                         d| _        | S )Nr0   r  r  r  T)r   r  r  _inductor_lowering_functionr  r  	pass_dictr0   r  s    r)   	decoratorz,register_lowering_pattern.<locals>.decorator  s;       g	

(9g(
..2+r(   r'   r0   r  r  r  r  s   ```` r)   register_lowering_patternr    s     r(   c                     fd}|S )zk
    Register a pattern that runs a function on the FX graph, allowing
    custom transformation code.
    c                `    t        |       sJ t        |       j                         | S )Nr  r  )r   r  r  r  s    r)   r  z)register_graph_pattern.<locals>.decorator  s3       g	

(9g(
.r(   r'   r  s   ```` r)   register_graph_patternr    s     r(   c                B    |t        t        | j                              u S r-   )r[   iterr3   )r:   r   s     r)   is_start_of_fx_graphr    s    4U[[)***r(   z$_$|(\b|_)(set|enter|exit|seed)(\b|_)c                   | j                   dk(  r*t        j                  | j                  j                        r0y| j                   dk(  r t        j                  | j                        ry| j
                  j                  d      d uS )Nr5  Tr9  out)r   _mutation_op_researchr"  r$   r2   r  r   s    r)   is_mutation_opr
  (  sb    ww/!!!$++"6"67	M	!!!$++.;;??5!--r(   c                ,   |}d|j                   vr3t        | |      s'|j                  }d|j                   vrt        | |      s'|j                   j                  dd      }||ur0|j                  }t        |      r|dz  }||j                   d<   ||ur0|S )Nmutation_region_idr   r   )re   r  prevr  r[   r
  )r:   r   rP   r  s       r)   get_mutation_region_idr  2  s    A
aff
,5I%QR5SFF aff
,5I%QR5S$8!<
4-FF!!#'9#$	 4-
 r(   c                V    dt        t        | j                              j                  vS )Nr  )r[   r  r3   re   r   s    r)   "should_compute_mutation_region_idsr  ?  s!    tD,='>'C'CCCr(   c                h    d}| j                   D ]!  }t        |      r|dz  }||j                  d<   # y )Nr   r   r  )r3   r
  re   )r:   r  nds      r)   compute_mutation_region_idsr  C  s8    kk"!#(:$% r(   c                  6     e Zd Zd fd	ZddZddZd Z xZS )r  c                X    t         |           t        t              | _        || _        y r-   )r.   r/   r   r   patternsprevent_match_across_mutations)r7   r  r8   s     r)   r/   zPatternMatcherPass.__init__L  s*      	 /M+r(   c                     | j                   |   S r-   )r  )r7   items     r)   __getitem__zPatternMatcherPass.__getitem__S  s    }}T""r(   c           
     :   | j                   syt        |t        j                  j                        r|j
                  }| j                  r0t        |      rt        |       t        j                  t        |      }d}t        |j                        D ]  }t        |      }|j                  dv s|| j                   v s-t!        |d      r;| j                   |   D ]9  }|j"                  r \|j$                  j'                  |      }| j                  r7t)        |      r,t+        t-        t/        |j                                    dk7  rpt0        j2                  j5                  d      |j6                  k(  r-t8        j;                  d||j<                  ||j$                         t)        |      s|j?                  |      s|dz  }|jA                  |||       tB        d   d	xx   dz  cc<   tB        d   d
xx   t+        |j                        z  cc<   <  |S )Nr   )r5  r9  r=  F)allow_cpu_inputsr   !TORCHINDUCTOR_PATTERN_MATCH_DEBUGz
%s%s %s %sinductorpattern_matcher_countpattern_matcher_nodes)"r  r   ri   rj   GraphModuler:   r  r  r  r  rN  r  rM   r3   r   r   r!   rN   r0   r   r   r~   r=   r  osenvironr  r   r  warningr1   r  r  r   )r7   r:   get_mutation_region_id_partialr$  r   r"  entryr   s           r)   r  zPatternMatcherPass.applyV  s   }}eUXX112KKE..1%8+E2-6->->&.* U[[)D#D)FJJdmm+
 9PUV!]]62E||++D1A ;;$QKC(F$P QRVWW zz~~&IJdiiWL$		1emmT{u'8'8';
Aud3 ,-DEJE ,-DEQWWUE# 3 *< r(   c                8    | j                   j                          y r-   )r  clearr;   s    r)   r(  zPatternMatcherPass.clear  s    r(   rV  )r  ztorch.fx.node.Targetrr   zList[PatternEntry])r:   torch.fx.GraphModulerr   r  )r$   r%   r&   r/   r  r  r(  rx   ry   s   @r)   r  r  K  s    M#*Xr(   r  c                     t               r-   r   rJ   s     r)   _not_implementedr+    s    

r(   c                  	
 |xs i }|j                         D ci c]  \  }}||
 c}}
t        
      t        |      k(  sJ 
fdt        j                         	 G 	fddt        j
                  j                        } ||       j                         }t        |t              st        t        j                  |            S |S c c}}w )z
    Convert an FX graph into a PatternExpr.  This is useful for simple
    patterns that can only match single functions and fixed-length lists.
    c                    t        | t        t        f      r| v rt        |          S t	        |       v r
t               S t        | t              rt        d | D              r| r
t               S | S )Nc              3  <   K   | ]  }t        |t                y wr-   )r   r   )rY   ys     r)   rZ   z5fx_to_pattern.<locals>.process_arg.<locals>.<genexpr>  s     &Iq!z!W'=qs   )r   r  r  r   r	  r   r   r\  )r   r  inv_scalar_workarounds    r)   process_argz"fx_to_pattern.<locals>.process_arg  sd    a%&10E+E3A6777l"9a3&Iq&I#Ia9r(   c                  F     e Zd ZeZeZeZfdZfdZ fdZ	 xZ
S ) fx_to_pattern.<locals>.Converterc                    t              }|t              k  r|   }n1r|j                  d      sJ |}nt        j                  dd|      }|}|v rt        |      S t        |      S )Ntangentz_\d+$r  )r[   r~   
startswithresubr   r   )	r7   r"  r1   r2   rP   r   r  argnumr  s	         r)   r  z,fx_to_pattern.<locals>.Converter.placeholder  ss    VA3x= {((333"f5***400!$''r(   c                    t        j                  ||f      \  }}t        v r>|D cg c]
  } |       }}|j                         D ci c]  \  }}| |       }}}t	        |g|i |S c c}w c c}}w r-   )r
  tree_mapr   r   r4  )r7   r"  r1   r2   ar  r  r1  s         r)   r5  z.fx_to_pattern.<locals>.Converter.call_function  s    !??;vGLD&|#0451A58>G1![^+G8888 6Gs   A2A7c                ^   t         |   |      }|j                  dk(  rst        |t              rct        |      t        |j                  d         k(  sJ t        ||j                  d         D ]  \  }}t        |j                        |_        ! |S t        |j                        |_        |S )Nr{  r   )	r.   r  r   r   rE   r~   r1   r#  r   )r7   rP   rvrrf   r8   s        r)   r  z)fx_to_pattern.<locals>.Converter.run_node  s    !!$BttxJr5$92w#affQi.000!"affQi0FAs!#))nAG 1 I qww<Ir(   )r$   r%   r&   r+  r9  r=  r  r  r5  r  rx   )r8   r  r9  r  r  r1  s   @r)   	Converterr3    s&    &&#	(	9	 	r(   r@  )r   r~   r   r$  ri   rj   r  r|  r   rq   rY  r
  tree_leaves)gmr  r  r  r  r  r  r@  r0   r9  r0  r1  s    `` `    @@@r)   r  r    s     */R.?.E.E.GH.GdaQT.GH$%->)???? __F$ $EHH(( $L m!Gg{+!&"4"4W"=>>No Is   Cc                    t               5   t        | t                     | }ddd       j                  j	                          |j                          |S # 1 sw Y   5xY w)z>Build a normalized inference graph, for use with fx_to_patternN)r   r   r    r:   eliminate_dead_code	recompile)r   r1   rB  s      r)   rh   rh     sM     
"	#/WR,./6 
$HH  "LLNI	 
$	#s   AA c           
        dfd}t         j                  j                  d      5   t        | d |t	               dd      |  ddd       sJ ddlm} t               }t        t         j                  j                  j                  j                  t        d	      t        d
            }t        ||t              j!                  |j"                         |j%                  j&                         t         j(                  j&                  j+                         j&                  _        j&                  j/                          j1                          S # 1 sw Y   xY w)z=Build a normalized training graph, for use with fx_to_patternNc                <    rJ t        |       t        | |fi |S r-   )clone_graphr   )joint_graphinputsr2   rB  s      r)   record_joint_graphz)joint_fwd_bwd.<locals>.record_joint_graph  s&    v% f???r(   c                    t        |       S r-   )r   )gr(  s     r)   rg   zjoint_fwd_bwd.<locals>.<lambda>  s	    +r(   TF)partition_fndecompositionskeep_inference_input_mutations
enable_logr   )pointless_viewrf   r  )r0   r  r  )ri   _guardstracingr   r    fx_passes.joint_graphrR  r  r4  opsatenviewdefaultr   r  r  r  r  r  r:   rj   CodeGen_codegenrD  rE  )r   r1   rK  rR  matcher_passr0   rB  s         @r)   r  r    s    *.B@ 
		t	$	
++.0+/	
 	 
% I25%'L		##Z%6
68JG \h|$$%rxx  ..0BHHHH  "LLNI7 
%	$s   EE c                    t               }t        j                  j                  | j                  | j
                  f|j                         |S r-   )r   ri   rj   rk   r1   r2   r   )rP   r1   s     r)   r%  r%    s3    )-D	HHaffahh'5Kr(   c                    t        t              t               d fdt        | j                        D ]
  } |        s!t	              t	        | j                        k(  sJ y )Nc                   t        |       D cg c]	  }|vs| }}|r|d      j                  |        y | j                  |        j                  | d      D ]  }j                  |        |        y c c}w )Nr   r'   )r%  r   r0  rm  )r   r   waiting_forrB   checkcursorreadywaitings       r)   ra  z&stable_topological_sort.<locals>.check  s{    "'+@+Q%q+@KN#**40 FIIdO T2.e$e / As
   	A=A=)r   r   r=   r3   r~   )r:   rP   ra  rb  rc  rd  s     @@@@r)   stable_topological_sortre    sZ    $GEEF %++a 3u:U[[)99999r(   c                r     t        j                  d      t        j                          fd              }|S )z0Wrapper around lazy init functions in fx_passes/Nc                 :   t         d   j                         } t        j                  j	                  d       5  t               5  t               5          }d d d        d d d        d d d        | t         d<   S # 1 sw Y   $xY w# 1 sw Y   (xY w# 1 sw Y   ,xY w)Nr  )r   copyri   rS  rT  r   r   )counters_refr  r   s     r)   	lazy_initz%init_once_fakemode.<locals>.lazy_init&  sx      
+002]]""
)+^-=TF .>+
  , .>-=++
 
s;   BBA9BB9B>BB	
BB)r  	lru_cacher  )r   rj  s   ` r)   init_once_fakemoderl  #  s;     __R   r(   c                      fd}|S )z2Function for extra_check to put pass behind a flagc                $    t        t              S r-   )r   r   )r   r   s    r)   
flag_checkzconfig_flag.<locals>.flag_check;  s    vt$$r(   r'   )r   ro  s   ` r)   config_flagrp  8  s    % r(   c                L     G d dt               } ||       j                         S )Nc                       e Zd Z fdZ xZS )clone_graph.<locals>.CopyGraphc                J   t         |   |      }t        |t        j                  j
                        rn|j                  j                  j                  |j                         | j                  j                  j                  |j                  d       |j                  _        |S r-   )r.   r  r   ri   rj   Proxyr   re   rA   	new_graph_graph_namespacer  r   )r7   old_nodenew_noder8   s      r)   r  z'clone_graph.<locals>.CopyGraph.run_nodeC  sq    w'1H(EHHNN3""))(--8%)^^%D%D%P%PMM4&" Or(   )r$   r%   r&   r  rx   ry   s   @r)   	CopyGraphrs  B  s    	 	r(   rz  )r   	transform)input_graphrz  s     r)   rH  rH  A  s$    K  [!++--r(   zSet[str]r  c                    t        | j                        |kD  r| j                  |   S | j                  j                  |      S r-   )r~   r1   r2   r  )r   
arg_number
kwarg_names      r)   get_arg_valuer  R  s>    
 tyy>J& 			* [[__Z(r(   c           	        |g}t        |t        j                  j                        r7|j	                  |j                         D cg c]  }t        ||       c}       | D cg c]  }|j                  |v s| c}S c c}w c c}w r-   )r   ri   r   r   r@   r   r   r"  )r3   r   r   r   r   s        r)   filter_nodesr  \  sn    $C"ejj112

",,.I.hGB).IJ"9UTdkkS&8DU99 J9s   A>#B7Bc                    | j                   dk(  r4t        | j                  j                  | j                        j
                  S | j                  S )zFor call_function and call_method, we directly use the target function;
    For call_module, the target is string, and we treat the module class
     as a function.
    r=  )r   r   r:   owning_moduler"  r8   r	  s    r)   r   r   d  s;    
 ww-tzz//=GGG;;r(   )r   r   rr   zTypeGuard[Match])r  zIterable[Any]rm   zCCallable[[Callable[..., Any], Iterable[Any]], torch.fx.GraphModule])r'   r'   )rr   rq   rp   )r:   r)  r   ru   rr   r   )r   ru   rr   r   )r:   r)  r   ru   rr   r  )r:   r)  rr   r   )r:   r)  )rr   r   )r'   r'   r'   r'   )rr   r)  )rP   ru   rr   zList[torch.fx.node.Argument]rt   )r   r  )r|  r)  rr   r)  r-   )r   ru   r~  r  r  r   )r3   zIterable[torch.fx.Node]rr   rW  )r   r   )
__future__r   dataclassesr  r  r   loggingr"  r7  collectionsr   typingr   r   r   r   r	   r
   r   r   r   r   typing_extensionsr   ri   torch._guardstorch.fxtorch.utils._pytreer  _pytreer
  torch._dispatch.pythonr   torch._dynamo.utilsr   torch._prims_commonr   r   "torch.fx.experimental.proxy_tensorr   r   torch.fx.immutable_collectionsr   r   
_functorchr   r  _functorch.aot_autogradr   r   _functorch.partitionersr   _subclassesr   rj   r   r  decompositionr    loweringr!   	getLoggerr$   r  rV  rW  primsConstantr   r#   r   r+   r   r?   r   r   rq   r   r   r   r   r   r   r4  r8  r<  r?  rD  rG  rJ  rL  rY  rh  r   	dataclassr  r  r  r^   r  r  r  r  r  r  r  compiler  r
  r  r  r  r  r+  r  no_gradrh   enable_gradr  r%  re  rl  rp  rH  r=   r  r   r  r  r   r'   r(   r)   <module>r     s   "      	 	 #   (    $ $ ; ( 0  V I 3 C 7 (   . ;g!yy~~		x./	 	
 :J
 J
Z, &#
 #
L, ,6(+ (k 5 5 5+ 5(7
+ 7
tC3k C3L?    (, * * $[ $N3 3l; @2 2j : : :, 
!< 
! 
! 	= 	= 	= X
l X
 X
v z "z R	zz e4SU 58 '3JO* '3JO$+ "**DE.
D;8 8v  E EP   % %P:.*. 5   GK
%(6C:r(   