
    ^j]s                       d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZmZ d dlZd dlZd dlmZ d dlmZmZ d dlmZ d dlmZ d	d
lmZmZ d	dlmZ d	dlm Z m!Z!m"Z" d	dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d	dl*m+Z+m,Z, erd dl-m.Z.m/Z/  ed      Z0 G d dejb                  jd                        Z3 G d de      Z4 G d de      Z5 G d de      Z6 G d d      Z7 G d d      Z8 G d de       Z9 G d d e"      Z:y)!    )annotationsN)autoEnum)Any
NamedTupleTYPE_CHECKINGTypeVar)identity)Scope
TracerBase)Mod)SymT   )configdependencies)index_prevent_reordering)DefaultHandler
OpsHandlerWrapperHandler)cache_on_selfdecompose_indexflatten_indexreduction_num_outputssympy_index_symbol_with_prefix
sympy_subs)opsV)CallableSequenceTc                  b     e Zd Zeej
                  d               Z fdZd fdZ fdZ	 xZ
S )InterpreterShimc                 H    t         j                  j                  t              S N)torchfxsymbolic_tracer
        d/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_inductor/loop_body.py	_dummy_gmzInterpreterShim._dummy_gm)   s     xx&&x00r)   c                    t         |   | j                         d       | | _        || _        || _        d| _        |j                  | _        d | _	        y )NF)garbage_collect_values)
super__init__r+   modulegraph
submodulesextra_traceback__getitem__
fetch_attrcurrent_node)selfr1   r2   	__class__s      r*   r/   zInterpreterShim.__init__.   sP     	)%H
$$$00 r)   c                0    || _         t        | 	  |      S r$   )r6   r.   run_node)r7   nr8   s     r*   r:   zInterpreterShim.run_node9   s    w""r)   c                x    t        j                  |       5  t        |   |i |cd d d        S # 1 sw Y   y xY wr$   )r   set_interpreter_handlerr.   run)r7   argskwargsr8   s      r*   r>   zInterpreterShim.run>   s7    &&t, 	07;//	0 	0 	0s   09)r;   ztorch.fx.Nodereturnr   )__name__
__module____qualname__staticmethod	functoolscacher+   r/   r:   r>   __classcell__r8   s   @r*   r"   r"   (   s5    __1  1	!#
0 0r)   r"   c                       e Zd Z fdZ xZS )LightTracerc                    t         |           t        j                  j	                  | j
                        | _        t        dd       | _        i | _	        i | _
        y )N)
tracer_cls )r.   r/   r%   r&   Graphr8   r1   r   scopemodule_stacknode_name_to_scope)r7   r8   s    r*   r/   zLightTracer.__init__E   sE    XX^^t~~^>
2t_
"$r)   )rB   rC   rD   r/   rH   rI   s   @r*   rK   rK   D   s    % %r)   rK   c                  ,    e Zd ZU ded<   ded<   ded<   y)MemoryEntrystr
index_name
str | Nonebuffer_namemodeN)rB   rC   rD   __annotations__r(   r)   r*   rT   rT   M   s    O
r)   rT   c                  n    e Zd Z e       Z e       Z e       Z e       Z e       Z e       Z	 e       Z
y)MemoryUsageTypeN)rB   rC   rD   r   LOAD	LOAD_SEEDSTORESTORE_REDUCTION
INDEX_EXPRCHECK_BOUNDS	BUCKETIZEr(   r)   r*   r\   r\   S   s3    6DIFEfOJ6LIr)   r\   c                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   ed        Z	 d6 fd	Zd7dZd Z	d Z
d8dZd9dZd:dZ	 	 	 	 	 	 d;dZd<dZd:dZed         Zed!        Zed"        Zd# Zd$ Zd% Zd& Zd' Zd( Zd) Zd=d*ZeZ	 	 d>	 	 	 	 	 	 	 d?d+Zd, Z d- Z!d. Z"d/ Z#d6d0Z$dd1d2Z%d3 Z&d4 Z'd5 Z( xZ)S )@LoopBodyz
    Captures the body of a Loops subclass into an FX graph.  Persists any
    indexing simplifications and makes it easier to analyze loop bodies.
    zdict[str, sympy.Expr]indexing_exprsdict[str, Any]r2   zdict[str, LoopBodyBlock]	subblockszlist[sympy.Symbol]indirect_varszdict[sympy.Symbol, sympy.Expr]indirect_var_rangesLoopBodyBlock
root_blockz(dict[MemoryUsageType, list[MemoryEntry]]memory_usagecollections.Counter[str]	op_countszdict[sympy.Expr, str]indexing_exprs_namec                R    t        |       t        u rt        j                  |       S | S r$   )typeintsympyInteger)exprs    r*   _wrap_int_to_sympy_integerz#LoopBody._wrap_int_to_sympy_integerp   s$     :==&&r)   Fc                H   t         |           t        |j                               }|d t	        |       |t	        |      d  f| _        || _        || _        || _        t        |t              r| j                  |||       d | _        y | j                  ||       d | _        y r$   )r.   r/   tuplevalueslensizes	iter_varsreduce_vars
var_ranges
isinstancere   _init_with_copy_init_with_tracingindexing)	r7   fnr?   r   r}   r~   allow_same_symbol_in_index_flat_sizesr8   s	           r*   r/   zLoopBody.__init__w   s     	J--/0(#i.)I()


 #&$b(#  T+EF  ##B-r)   c                    | j                   sJ | j                  j                  j                  dd      d   }|j                  d   }|d   S )Ncall_methodpartial_accumulateoptargetr   num_reduction_dims)has_partial_accumulaterl   r1   
find_nodesr?   )r7   nodemetas      r*   get_original_num_rdimszLoopBody.get_original_num_rdims   sY    ****$$//%9 0 

 yy}())r)   c                    | j                   j                         | _         d| _        | j                  | j                  z   | _        g | _        | j
                  d   | j
                  d   z   t               f| _        | S )NTr   r   )rl   extract_pw_from_reductionr   r}   r~   r|   ry   r7   s    r*   r   z"LoopBody.extract_pw_from_reduction   sd    //CCE&*#$*:*::jjmdjjm3UW=
r)   c                r   i | _         i | _        d| j                  i| _        i | _        g | _        i | _        t        D ci c]  }|g  c}| _        t        j                         | _        t        | ||      | _        t        | j                  j                  j!                  dd            | _        | `yc c}w )z9Do an FX trace of an arbitrary callable to construct self	get_indexr   r   r   N)rf   rp   r   r2   rh   ri   rj   r\   rm   collectionsCounterro   rk   rl   boolr1   r   r   )r7   r   r?   ts       r*   r   zLoopBody._init_with_tracing   s     #% &7CE ,;<qQU<$,,.'b$7&*OO!!,, )= - '
#
 $ =s   
B4c                X   |j                  ||      }|j                         D ci c]I  \  }}|| j                  t        j                  j
                  j                  || j                              K c}}| _        |j                  j                         D ci c]  \  }}||j                  |        c}}| _	        |j                  | _        |j                  | _        |j                  | _        |j                  | _        |j                  j                  |       | _        |j                   | _        i |j"                  }	|	j%                  d       d| j&                  i|	j                         D ci c]  \  }}||j                  |        c}}| _        yc c}}w c c}}w c c}}w )z
        _init_with_tracing() is slow, so this is a fast path in the case
        where we are just reordering/merging/splitting the args of an
        existing LoopBody.
        r   N)indexing_from_argsitemsrw   r   r1   sizevarssimplify_with_rangesr   rf   rh   cloneri   rj   rm   ro   rl   r   r2   popr   )
r7   otherr?   r   rf   namerv   kvr2   s
             r*   r   zLoopBody._init_with_copy   sl    11$8RS
 -224	
 d $11  55dDOOL 
 8=7L7L7NOtq!!QWWT]*O"00#(#<#< !..**006&+&B&B#)(()
{#
,6,<,<,>?DAqq!''$-?
!
 P @s   AFF 3F&c                @    | j                   j                  |d      dkD  S )Nr   )ro   getr7   r   s     r*   has_opzLoopBody.has_op   s    ~~!!$*Q..r)   c           	        | }| j                   }|j                  \  }}|\  }}g |j                  j                         }t        j
                  j                  j                  ||t        |||            \  }}	}
t        j
                  j                  j                  ||t        |||            \  }}}
||k(  r||k(  r|S t        j                  ||d      \  \  }}}t        | |	|       ||      g|||d      }|S )zU
        Merge both iteration and reduction loops and return a new LoopBody.
        p)prefixTr   )r|   varsrf   rz   r   r1   r   _simplify_loopsr   r   index_vars_no_squeezere   )r7   old_body	old_sizesold_iter_varsold_reduce_varsold_iter_sizesold_reduce_sizesindex_exprs
iter_sizesiter_reindex_reduce_sizesreduce_reindexr}   r~   r   new_bodys                    r*   merge_loopszLoopBody.merge_loops   s    JJ	)1&+4((9//6689&'gg&6&6&F&F$[-P'
#
L! +,''*:*:*J*J$[/CST+
'na 'L<L,LO ..z<PST	
)$n[&AB'+
 r)   c                ,   | | j                   }|\     t              }||<   |f}t        j                  |ddi\  \  }}}dfd}	t	        |	||f|||      }
t        j                  |ddi\  \  }}}t	        |
||f|||      }	|	S )z~
        Expand node on `dimension` to `new_range` and rely on index modular to avoid
        out-of-boundary access.
        r   r   c                    g t         j                  j                  |       }t        |      t              t        	      z   k(  sJ |d t               }|t              d  }t	        |      }t        |         |<    ||      S r$   )	itertoolschainfrom_iterabler{   listr   )
indicesindexiter_idx
reduce_idxnew_iter_idx	dimension	iter_sizer   original_rangereduce_sizes
        r*   r   z>LoopBody.expand_dimension_for_pointwise_node.<locals>.new_body  s    =ioo33G<=Eu:Y#k2B!BBBB-s9~.Hs9~/0J>L&)(9*=~&NL#L*55r)   r   r   Sequence[sympy.Expr]rA   r   r|   r   r   r   re   )r7   r   	new_ranger   new_iter_size	new_sizesr}   r~   r   r   	loop_body
iter_vars2reduce_vars2var_ranges2r   r   r   r   s    `            @@@@r*   #expand_dimension_for_pointwise_nodez,LoopBody.expand_dimension_for_pointwise_node   s     JJ	!*	;"9-Y#,i "K0	/;/Q/Q0
0
, K*
		6 		6 y+.
I{
	
 3?2T2T3
3
/"\K 
L1;
L
 r)   c                4   | | j                   d   | j                   d   }t              t        |      f}t        j                  |ddi\  \  }}}dfd}t	        |||f|||      }t        j                  |ddi\  \  }	}
}t	        ||	|
f||	|
      S )a  
        Reindex iteration loops into a different factorization of the same
        total numel. For example, [1024, 8192] -> [65536, 128].

        The old iteration vars are expressed as functions of the new vars via
        FloorDiv and ModularIndexing on the flat index.
        r   r   r   r   c                     g t         j                  j                  |       }|d t               }|t              d  }t	        |      }t        |      } |t        |            S r$   )r   r   r   r{   r   r   r   )	r   r   r   r   flatold_iter_idxnew_iter_sizesr   r   s	         r*   r   z-LoopBody.reindex_iter_loops.<locals>.new_bodyB  sj    =ioo33G<=E !63~#67Ls>245J ~>D*4@LL$z*:;;r)   r   r   r   )r7   r   r   r   r}   r~   r   r   r   r   r   r   r   r   s    `          @@r*   reindex_iter_loopszLoopBody.reindex_iter_loops/  s     Azz!}.)4+=>	/;/Q/Q0
0
, K*
	< y+.
I{
	 3?2T2T3
3
/"\K &
 	
r)   c                   ddl m} | | j                  }t        |d         t        |      k(  sJ  ||      }|\   |      }|f}t	        j
                  |ddi\  \  }}}	t        |      D 
ci c]  \  }
}||

 c}}
t        t        |            D cg c]  }|   	 c}dfd}t        |||f|	||      S c c}}
w c c}w )zD
        Reorder iteration loops and return a new LoopBody.
        r   )same_reorderr   r   r   c                    g t         j                  j                  |       }t        |      t              t              z   k(  sJ |d t               }|t              d  }D cg c]  }||   	 }} ||d      S c c}w )NTr   )r   r   r   r{   )	r   r   r   r   iinverse_orderr   r   r   s	        r*   r   z-LoopBody.reorder_iter_loops.<locals>.new_bodyr  s    =ioo33G<=Eu:Y#k2B!BBBB-s9~.Hs9~/0J-:;;H;HjTRR <s   (Br   )	irr   r|   r{   r   r   	enumeraterangere   )r7   	new_orderr   r   
reorder_fnr   r   r}   r~   r   abr   r   r   r   r   r   s                 @@@@r*   reorder_iter_loopszLoopBody.reorder_iter_loopsZ  s     	%JJ	9Q< C	N222!),
!*	;"9-"K0	/;/Q/Q0
0
, K*
 +4I*>?$!QA?38Y3HIaq)I	S 	S $
 	
 @Is   7CCc                j    | j                   J | j                  J | j                   | j                  fS r$   )r}   r~   r   s    r*   r   zLoopBody.vars  s9    ~~)))+++~~t////r)   c                    t        j                  | j                  j                  fd | j                  j                         D              }|D cg c]  }|j                  D ]  }|  c}}S c c}}w )Nc              3  4   K   | ]  }|j                     y wr$   )r1   ).0blocks     r*   	<genexpr>z%LoopBody.get_nodes.<locals>.<genexpr>  s     >UU[[>s   )r   r   rl   r1   rh   rz   nodes)r7   
all_graphsr1   r   s       r*   	get_nodeszLoopBody.get_nodes  s[    ____""$>dnn&;&;&=>

 #-EEEEEEs   A.c                    ddl m}  ||       S )Nr   )	BoundVars)boundsr   )r7   r   s     r*   r   zLoopBody.bounds  s     	&r)   c                    t        | j                  t        j                           D ],  }|j                  |k(  s| j
                  |j                     c S  t        |      r$   )reversedrm   r\   r]   rX   rf   rV   KeyErrorr7   rX   entrys      r*   get_read_exprzLoopBody.get_read_expr  sY    d//0D0DEF 	=E  K/**5+;+;<<	= {##r)   c                   t        j                  | j                  t        j                     | j                  t        j
                           D ],  }|j                  |k(  s| j                  |j                     c S  t        |      r$   )
r   r   rm   r\   r_   r`   rX   rf   rV   r   r   s      r*   get_write_exprzLoopBody.get_write_expr  sv    __o334o==>
 	=E   K/**5+;+;<<	= {##r)   c                    | j                   t        j                     D cg c]  }| j                  |j                      c}S c c}w r$   )rm   r\   r]   rf   rV   r7   r   s     r*   get_read_exprszLoopBody.get_read_exprs  sF     **?+?+?@
  0 01
 	
 
s    Ac                    g }t        | j                  t        j                           D ]:  }|j                  |k(  s|j                  | j                  |j                            < |S r$   )r   rm   r\   r]   rX   appendrf   rV   r7   rX   outr   s       r*   get_all_read_exprzLoopBody.get_all_read_expr  s`    d//0D0DEF 	BE  K/

4..u/?/?@A	B 
r)   c                    t        j                  | j                  t        j                     | j                  t        j
                           D cg c]  }| j                  |j                      c}S c c}w r$   )r   r   rm   r\   r_   r`   rf   rV   r  s     r*   get_write_exprszLoopBody.get_write_exprs  sf     #!!/"7"78!!/"A"AB
  0 01
 	
 
s    A2c                   g }t        j                  | j                  t        j                     | j                  t        j
                           D ]:  }|j                  |k(  s|j                  | j                  |j                            < |S r$   )
r   r   rm   r\   r_   r`   rX   r  rf   rV   r  s       r*   get_all_write_exprzLoopBody.get_all_write_expr  s}    __o334o==>
 	BE   K/

4..u/?/?@A	B 
r)   c           	        dt        | j                         g}|j                  | j                  j	                         D cg c]  \  }}| d|  c}}       |j                  t        j                  d| j                  fg| j                  j	                               D cg c]  \  }}|j                  |       c}}       dj                  |      S c c}}w c c}}w )Nzvar_ranges = z = body
)dictr   extendrf   r   r   r   rl   rh   	debug_strjoin)r7   linesr   valr   s        r*   r  zLoopBody.debug_str  s     doo!6 7899L9L9R9R9TUID#c#'UV $-??doo./1E1E1G$D% %	
 yy Vs   C
$C
c                   t        | j                  t        j                           dk(  xr` t        | j                  t        j                           dk(  xr5 t        | j
                        dk(  xr | j                  j                  d      S )zx
        True of this contains only a single loads and store.
        Note, this could involve a layout change.
        r   )loadstore)r{   rm   r\   r]   r_   r2   rl   contains_only_opsr   s    r*   is_memory_copyzLoopBody.is_memory_copy  s}     !!/"6"678A= ED%%o&;&;<=BEDOO$)E 112CD		
r)   c                    | j                  |      }| j                  j                  |      }|s6dt        | j                         }|| j                  |<   || j                  |<   | j
                  |   j                  t        |||             |S )Nr   )rw   rp   r   r{   rf   rm   r  rT   )r7   rv   mtyperX   rY   r   s         r*   add_index_exprzLoopBody.add_index_expr  s     ..t4''++D13t22345D-1D$$T*(,D%% ''D+t(LMr)   c                    |d   j                         r|| j                  vr|}n| t        | j                         }|| j                  |<   |S )zaNot actually for nn.Modules, but subblocks in generated code are mapped to FX call_module opcodesr   )	isnumericr2   r{   )r7   r   r   r   s       r*   add_submodulezLoopBody.add_submodule  sM    ":!fDOO&CDXc$//234D %r)   c                    t        t        j                  t        | j                              }|| j
                  vsJ | j                  j                  |       || j
                  |<   |S r$   )r   r   INDIRECTr{   ri   rj   r  )r7   sizevars      r*   add_indirectzLoopBody.add_indirect  sZ    ,T]]C@R@R<ST$22222!!#&(,  %
r)   c           
         t        |      t        |      k(  ry| j                  J | j                  j                         D ci c]  \  }}|t        |||i       c}}| _        yc c}}w )z,Swap in a variable used in indirect indexingN)rU   r   r   r   )r7   oldnewr   r   s        r*   replace_indirectzLoopBody.replace_indirect  s[    s8s3x}}(((BF--BUBUBWX$!QJq3*55XXs   A'c                <    | j                   J | j                   |   S r$   )r   r   s     r*   r   zLoopBody.get_index  s!    }}(((}}T""r)   c           	         g t         j                  j                  |      }t        |      t         j                        k(  sJ | j                  f       |s+t         fd|D              sJ d j                  d|       t        t         j                  j                         |            } j                  j                         D ci c]  \  }}|t        ||       c}}S c c}}w )Nc              3  :   K   | ]  }|j                   v  y wr$   )r   )r   r   r7   s     r*   r   z.LoopBody.indexing_from_args.<locals>.<genexpr>  s      1
)*AT__$1
   zself.var_ranges=z
, indices=)r   r   r   r{   r   allr  zipkeysrf   r   r   )r7   r   r   r   replacementsr   rv   s   `      r*   r   zLoopBody.indexing_from_args  s    9)////895zS11KE4??3KK1)S 1
.31
 .
 	-doo{',	- 
 C 4 4 6>? #11779
d *T<00
 	
 
s   C)r   c               b    | j                  ||      | _        | j                         }d | _        |S r$   )r   r   rl   )r7   r   r   results       r*   __call__zLoopBody.__call__  s/    //9ST"r)   c                |      fd}t        j                  t        j                        |_        |S )Nc           	     j    j                  t        j                  j                  |              y r$   )r*  r   r   indirect_indexing)new_varcheckr7   r$  r%  wrap_negs    r*   set_indirectz5LoopBody.bind_set_indirect_shim.<locals>.set_indirect#  s+    !!QUU,,WdE8Lr)   )r%  r$  r:  r;  )rF   partialre   bind_set_indirect_shimr   )r7   r%  r$  r:  r;  r<  s   ````` r*   r>  zLoopBody.bind_set_indirect_shim"  s?    	 	
 '..++
 r)   c                f    fd}t        j                  t        j                        |_        |S )Nc                F    t         j                  j                  | |      S r$   )r   r   scan)dtypesrz   
combine_fns     r*   shimz%LoopBody.bind_scan_shim.<locals>.shim2  s    55::fj&99r)   )rC  )rF   r=  re   bind_scan_shimr   )r7   rC  rD  s    ` r*   rE  zLoopBody.bind_scan_shim1  s)    	: &&x'>'>:V
r)   c                j      fd}t        j                  t        j                        |_        |S )Nc                `    t         j                  j                  | j                     |      S r$   )r   r   maskedrh   )maskr   r   r7   s     r*   rD  z'LoopBody.bind_masked_shim.<locals>.shim9  s#    55<<dnnT&:EBBr)   )r   )rF   r=  re   bind_masked_shimr   )r7   r   rD  s   `` r*   rJ  zLoopBody.bind_masked_shim8  s*    	C &&x'@'@tL
r)   )F)rA   rs   )r   re   )r   rU   )rA   re   )r   rs   r   rs   rA   re   )r   r   rA   re   rA   r   NN)rv   
sympy.Exprr  r\   rX   rW   rY   rW   )*rB   rC   rD   __doc__rZ   rE   rw   r/   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r  r  r	  r  r  r  r  __repr__r  r!  r&  r*  r   r   r5  r>  rE  rJ  rH   rI   s   @r*   re   re   ^   s{   
 *)''%%77::'' /.  $)8*%$
8/*X--),-	-^)
V&
P 0 0
 F F  $$

 

 H #'   	
  Y#
 =B r)   re   c                  <    e Zd ZdZd	dZd Zd Zd
dZddZddZ	y)rk   a  
    Captures the body of a Loops subclass into an FX graph.
    In normal cases there will be a 1:1 mapping between LoopBody and
    LoopBodyBlock, however in the case of ops.masked() the masked out
    operations will manifest as an extra LoopBodyBlock.
    c                   || _         t               }|j                  dddi       }ddlm} t        t        |||      |j                        }t        j                  r2 ||| j                   j                  | j                   j                        }t        j                  |      5  t        j                   ||        d d d        |j                   | _        y # 1 sw Y   xY w)Nplaceholderr   r(   r   )IndexPropagation)r  rK   create_proxyindex_propagationrT  CountOpsCaptureIndexingro   r   constant_and_index_propagationr   rj   r   set_ops_handlerr   outputr1   )r7   r  r   r?   tracer	proxy_opsrT  handlers           r*   r/   zLoopBodyBlock.__init__H  s    	''ub"E	7	 NN
 00&--tyy/L/LG w' 	" JJr4y!	" \\
		" 	"s   $CC c           	     r   d }d }| j                   j                  D ]-  }|j                  dk(  r|rJ |}|j                  dk(  s(|rJ |}/ |sJ |sJ |j                  d   }|j                  d   }|j                  d   }|j                  d   }dt	        | j
                  j                        i}| j                   j                  |      5  | j                   j                  d|||||f       d d d        | j                   j                  |       | j                   j                  |       | S # 1 sw Y   AxY w)	N	reductionstore_reductionr   r   r   r   r   )
r1   r   r   r?   r{   r  r~   inserting_afterr   
erase_node)	r7   redr  r   reduction_typered_argbufr   
extra_metas	            r*   r   z'LoopBodyBlock.extract_pw_from_reductiond  s.   JJ$$ 	D{{k)w{{//  y	 
su"((2,jjmjjm !#dii&;&;"<

 ZZ''. 	JJ""$sC*&U	 	

e$

c"	 	s   "D--D6c                    | j                   }| j                  j                  }t        ||      j	                  t        j                               S r$   )r1   r  r2   r"   r>   r   get_ops_handler)r7   r1   r2   s      r*   r5  zLoopBodyBlock.__call__  s;    

YY))
uj155a6G6G6IJJr)   c           
        t         j                  j                  | j                  j                  | j
                        j                  }t        j                  dd|j                         j                  dd| d            S )Nz;[^\n]*rN   zdef forward(zdef ()r%   r&   GraphModuler  r2   r1   coderesubstripreplace)r7   r   ro  s      r*   r  zLoopBodyBlock.debug_str  sa    xx##DII$8$8$**EJJvvJJL  4vQ@	
 	
r)   c                `    t        fd| j                  j                  d      D              S )Nc              3  :   K   | ]  }|j                   v   y wr$   )r   )r   r   allowed_opss     r*   r   z2LoopBodyBlock.contains_only_ops.<locals>.<genexpr>  s"      
 KK;&
r.  r   )r   )r/  r1   r   )r7   rv  s    `r*   r  zLoopBodyBlock.contains_only_ops  s/     


---?
 
 	
r)   c                    t         j                  t               }|j                  j                  i | j                  d|i       |S )z'Shallow copy with a new parent LoopBodyr  )rk   __new____dict__update)r7   r  copys      r*   r   zLoopBodyBlock.clone  s:    $$]3<<vt<=r)   N)r  re   r   Callable[..., Any]r?   z	list[Any])r   rK  )r  re   )
rB   rC   rD   rN  r/   r   r5  r  r  r   r(   r)   r*   rk   rk   @  s&    "88K

r)   rk   c                      e Zd ZddZddZy)rW  c                     || _         || _        y r$   )_inner_counts)r7   innercountss      r*   r/   zCountOps.__init__  s    r)   c                h    | j                   |xx   dz  cc<    t        | j                  |      |i |S )Nr   )r  getattrr  )r7   r   r?   r@   s       r*   _defaultzCountOps._default  s3    Ta)wt{{D)4:6::r)   N)r  OpsHandler[Any]r  rn   )r   rU   r?   ztuple[Any, ...]r@   rg   rA   r   )rB   rC   rD   r/   r  r(   r)   r*   rW  rW    s    ;r)   rW  c                       e Zd Zd Z	 	 	 	 	 	 d fdZddZddZddZddZddZ	d Z
d Zd	 Zd
 Zd Z	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 ddZd Zd ZddZd Z xZS )rX  c                @    t         |   |       || _        || _        y r$   )r.   r/   r  r\  )r7   r  r  r\  r8   s       r*   r/   zCaptureIndexing.__init__  s      		r)   c           	     x    | j                   j                  dd | j                  j                  ||fi |fi       S )Ncall_moduler   )r\  rU  r  r  )r7   rv   r  r@   s       r*   
_add_indexzCaptureIndexing._add_index  s@    {{''%TYY%%dE<V<>	
 	
r)   c                ~    t         j                  j                  j                  || j                  j
                        S r$   )r   r1   r   r   r  r   )r7   rv   s     r*   	_simplifyzCaptureIndexing._simplify  s)    ww44T499;O;OPPr)   c                    | j                  |      }| j                  |t        j                  |      }| j                  j                  ||      S NrX   )r  r  r\   r]   r  r  r7   r   r   s      r*   r  zCaptureIndexing.load  sA    u%';';N{{e,,r)   c                    t        |t              sJ | j                  j                  t	        j
                  |      t        j                  |       | j                  j                  ||      S r  )
r   rs   r  r  rt   ru   r\   r^   r  	load_seedr  s      r*   r  zCaptureIndexing.load_seed  sX    %%%%		  MM% /";"; 	! 	
 {{$$T511r)   c                    | j                  |      }| j                  |t        j                  ||      }| j                  j                  ||||      S )N)rX   rY   )r  r  r\   r_   r  r  )r7   r   r   valuerY   s        r*   r  zCaptureIndexing.store  sO    u%?((d   
 {{  ueT::r)   c                    | j                  |      }| j                  |t        j                  |      }| j                  j                  |||      S r  )r  r  r\   r`   r  ra  )r7   r   r   r  s       r*   ra  zCaptureIndexing.store_reduction  sK    u%?22   
 {{**4>>r)   c                    | j                   j                  ||||      t        |      }|dkD  rt        fdt	        |      D              S S )Nr   c              3  (   K   | ]	  }|     y wr$   r(   r   r   r4  s     r*   r   z,CaptureIndexing.reduction.<locals>.<genexpr>  s     ?q?   )r  r`  r   ry   r   )r7   dtype	src_dtyperf  r  num_outputsr4  s         @r*   r`  zCaptureIndexing.reduction  sI    &&uiO+N;??E+,>???r)   c                &   | j                  |      }t        |t        t        j                  f      r%| j
                  j                  t        |      |      S | j                  |t        j                        }| j
                  j                  ||      S r$   )r  r   rs   rt   ru   r  constantr  r\   ra   
index_exprr7   r   r  s      r*   r  zCaptureIndexing.index_expr  k    u%ec5==12;;''E
E::'A'AB{{%%eU33r)   c                &   | j                  |      }t        |t        t        j                  f      r%| j
                  j                  t        |      |      S | j                  |t        j                        }| j
                  j                  ||      S r$   )r  r   rs   rt   ru   r  r  r  r\   ra   
value_exprr  s      r*   r  zCaptureIndexing.value_expr  r  r)   c                   | j                  |      }| j                  |t        j                        }| j                  j                  |      }| j                  |t        j                        }| j                  j                  ||||      S r$   )r  r  r\   rb   r  rw   r  check_bounds)r7   r   r$  loweruppers        r*   r  zCaptureIndexing.check_bounds  sk    u%'C'CDyy33D9t_%A%AB{{''tUEBBr)   c           	        |d   | j                  |d   t        j                  |d         | j                  |d   t        j                  |d         | j                  |d   t        j                  |d         f}|-|d   | j                  |d   t        j                  |d         f}| j                  j	                  |||||||      S )z3
        See [Note: Inductor bucketize op]
        r   r   r        )r  r\   rc   r  	bucketize)r7   rz   
boundariesboundary_indicesindexing_dtyperightsortersorter_indicess           r*   r  zCaptureIndexing.bucketize  s     qMOO1))&qM  
 OO1))&qM  
 OO1))&qM  

$ q	1I88fQi   F {{$$
 	
r)   c                :   | j                   j                  dd      }| j                   j                  |      | j                   j                  |<   t	        | j                   |g       | j                   j
                  |<   | j                  j                  d|||fi       S )zR
        Recursively capture the masked out body in another LoopBodyBlock
        Nmasked_subblockr  )r  r!  rJ  r2   rk   rh   r\  rU  )r7   
mask_proxymasked_bodyother_proxyr   s        r*   rH  zCaptureIndexing.masked%  s     yy&&t->?%)YY%?%?%E		T"$1$))["$M		D!{{''4*k!:B
 	
r)   c                    | j                   j                  |      }| j                   j                  |d      }| j                  j	                  d|||fi       t        fdt        t        |            D              S )NrA  r  c              3  (   K   | ]	  }|     y wr$   r(   r  s     r*   r   z'CaptureIndexing.scan.<locals>.<genexpr>?  s     @1VAY@r  )r  rE  r!  r\  rU  ry   r   r{   )r7   dtype_proxyrC  value_proxyrD  r   r4  s         @r*   rA  zCaptureIndexing.scan0  sq     yy''
3yy&&tV4))+&	
 @c+.>(?@@@r)   c                    | j                   j                  ||||      t        fdt        t	        |            D              S )Nc              3  (   K   | ]	  }|     y wr$   r(   r  s     r*   r   z'CaptureIndexing.sort.<locals>.<genexpr>D  s     ;1VAY;r  )r  sortry   r   r{   )r7   rB  rz   stable
descendingr4  s        @r*   r  zCaptureIndexing.sortA  s7    !!&&&*E;c&k(:;;;r)   c                L    | j                   j                  |      }|d   |d   fS )Nr   r   )r  frexp)r7   r  r4  s      r*   r  zCaptureIndexing.frexpF  s)    "";/q	6!9%%r)   c                    | j                   j                  |      }| j                   j                  ||||      }| j                  j	                  d| j                   j                  |d|       |fi        |S )zx
        Flow data from tensors into indexing formulas.
        Introduce a call_module to update the indexing.
        r  set_)r  r&  r>  r\  rU  r!  )r7   index_proxyr$  r:  r;  r%  r<  s          r*   r8  z!CaptureIndexing.indirect_indexingK  sp     ii$$T*yy77T5(S  II##LD,?N		
 
r)   c                @    | j                   j                  dd|i        y )Nr[  )r\  rU  )r7   r4  s     r*   r[  zCaptureIndexing.output[  s      8VR@r)   )r  r  r  re   r\  rK   )rv   rM  r  r\   r@   r   )rv   rM  rA   rM  )r   rU   r   rM  )r   rU   r   rs   r$   rL  )rz   r    r  z.tuple[str, sympy.Expr, sympy.Expr, sympy.Expr]r  r    r  ztorch.dtyper  r   r  ztuple[str, sympy.Expr] | Noner  zT | NonerA   r    )r  r|  )rC  z=Callable[[tuple[Any, ...], tuple[Any, ...]], tuple[Any, ...]])TT)rB   rC   rD   r   r/   r  r  r  r  r  ra  r`  r  r  r  r  rH  rA  r  r  r8  r[  rH   rI   s   @r*   rX  rX    s    D  	
Q-
2;?44C 15#'/
/
 C/
 	/

 $/
 /
 ./
 !/
 
/
b	
A RA"<
&
 Ar)   rX  );
__future__r   r   rF   r   rp  enumr   r   typingr   r   r   r	   rt   torch.fxr%   torch._dynamo.utilsr
   torch.fx.proxyr   r   torch.utils._sympy.functionsr   torch.utils._sympy.symbolr   rN   r   r   codegen.commonr   ops_handlerr   r   r   utilsr   r   r   r   r   r   virtualizedr   r   collections.abcr   r   r    r&   Interpreterr"   rK   rT   r\   re   rk   rW  rX  r(   r)   r*   <module>r     s    "    	  : :   ( , , * " 4 C C    2 CL0ehh** 08%* %* d _ _DY Yx;~ ;vAn vAr)   