
    `i4                        U d Z ddlZddlmZ ddlmZ ddl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mZmZ ddlmZmZmZ dd	lmZmZ d
dlmZ d
dlmZ d
dlmZ d
dl m!Z! eej"        e#e$e%f         Z&de&fdZ'de&fdZ(e G d d                      Z) G d d          Z*e G d d                      Z+ee+e,d         f         Z-ee.d<    G d de          Z/dS )a  This file implements the IndexPropagation ops handler, which wraps an
underlying handler to add a limited form of constant propagation, as well as
propagation of sympy expressions downstream of ops.index_expr calls.

For example, say we have the IR:

    tmp0 = ops.index_expr(x, torch.int32)
    tmp1 = ops.constant(2, torch.int32)
    tmp2 = ops.mul(tmp0, tmp1)
    tmp3 = ops.indirect_indexing(tmp2, x_size)
    tmp4 = ops.load("buf0", tmp3)

The underlying handler would just see:

    ops.load("buf0", x * 2)

This is limited by the set of operators handled in the sympy expression
printers. So simple operations like minimum and maximum cannot be translated to
SymPy expressions yet, despite sympy.Min and sympy.Max existing.

    N)Sequence)	dataclass)AnyLiteralOptionaloverloadUnion)	TypeAlias)dtype_to_typeis_integer_dtype)FloorDivModularIndexingWhere)bound_sympyValueRanges   )DefaultHandler)statically_known_true)generate_assert)Vvalc                     t          | t          j                  r| j        S t          | t          t
          t          f          S N)
isinstancesympyBasic	is_numberintfloatboolr   s    u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_inductor/index_propagation.py_is_constantr#   .   s5    #u{## }cC-...    c                 b    t          | t          j                  rt          |           j        n| S r   )r   r   Exprr   upperr!   s    r"   upper_boundr(   4   s)    %/UZ%@%@I;s!!cIr$   c                   >    e Zd ZU dZeed<   ej        ed<   d Zd Z	dS )	TypedExprz'A SymPy expression with associated typeexprdtypec                 *    t          | j                  S r   )r#   r+   selfs    r"   is_constantzTypedExpr.is_constant?   s    DI&&&r$   c                    t          | j                  r| j        }t          |t          j                  r|                    d          } t          | j                  |          }t          | j                  rTt          j
        | j                  j        }| j        j        r|d|dz
  z  z   }|d|z  z  }| j        j        r|d|dz
  z  z
  }|| _        d S d S )NT)identity   r   )r#   r+   r   r   r&   expandr   r,   r   torchiinfobits	is_signed)r/   r+   r7   s      r"   __post_init__zTypedExpr.__post_init__B   s    	"" 	9D$
++ 2{{D{11,=,,T22D
++ 2{4:..3:' 2!q/1Dag~:' 2!q/1DDIII	 	r$   N)
__name__
__module____qualname____doc__	_ExprType__annotations__r5   r,   r0   r9    r$   r"   r*   r*   8   sQ         11
OOO;' ' '    r$   r*   c                      e Zd ZdZededefd            Zedeee	e
f         dej        defd            Zedeej        ef         dej        defd            Ze	 	 ddedej        d
eej                 de
def
d            Zededefd            Zededefd            Zedededefd            Zedededefd            Zedededefd            Zededefd            Zedededefd            Zedededee         fd            Zedededee         fd            Zedededefd            Zedededefd            ZdS )SymPyOpszAn ops handler where all IR values are SymPy expressions

    When a value cannot be represented as a SymPy expression, the method is
    either not defined, or returns NotImplemented

    valuereturnc                     | S r   r@   )rC   s    r"   r2   zSymPyOps.identityZ   s    r$   r,   c                 "    t          | |          S r   r*   rC   r,   s     r"   constantzSymPyOps.constant^       &&&r$   c                 "    t          | |          S r   rG   rH   s     r"   
index_exprzSymPyOps.index_exprb   rJ   r$   NF	src_dtypeuse_compute_typesc                 ,    t          | j        |          S r   )r*   r+   )rC   r,   rM   rN   s       r"   to_dtypezSymPyOps.to_dtypef   s     U+++r$   xc                 P    t          t          | j                  | j                  S r   )r*   absr+   r,   rQ   s    r"   rS   zSymPyOps.abso   s    QVag...r$   c                 F    t          | j        | j        z  | j                  S r   r*   r+   r,   rT   s    r"   squarezSymPyOps.squares   s    !&!'222r$   yc                 z    t          j        | j        |j                  }t          | j        |j        z   |          S r   r5   promote_typesr,   r*   r+   rQ   rX   result_types      r"   addzSymPyOps.addw   0    )!'17;;!&+666r$   c                 z    t          j        | j        |j                  }t          | j        |j        z
  |          S r   rZ   r\   s      r"   subzSymPyOps.sub|   r_   r$   c                 z    t          j        | j        |j                  }t          | j        |j        z  |          S r   rZ   r\   s      r"   mulzSymPyOps.mul   r_   r$   c                 8    t          | j         | j                  S r   rV   rT   s    r"   negzSymPyOps.neg   s    !&!'***r$   c                     t          j        | j        |j                  }t          |          st          S t          t          | j        |j                  |          S r   )r5   r[   r,   r   NotImplementedr*   r   r+   r\   s      r"   floordivzSymPyOps.floordiv   sL    )!'17;;,, 	"!!!&!&11;???r$   c                     t          j        | j        |j                  }t          |          st          S t          | j        t          j        j	        |j                  }t          ||          S r   )r5   r[   r,   r   rg   r   r+   r   SOner*   )rQ   rX   r]   result_exprs       r"   modzSymPyOps.mod   sW    )!'17;;,, 	"!!%afegk16BBk222r$   c                    t          j        | j        |j                  }t          |          st          S t          j        | j                  }t          j        |j                  }|j        J|j        |j	        k    r:t          | j        t
          j        j        |j                  }t          ||          S t          S r   )r5   r[   r,   r   rg   r   sympifyr+   is_nonnegativeis_positiver   rj   rk   r*   )rQ   rX   r]   x_expry_exprrl   s         r"   	remainderzSymPyOps.remainder   s    )!'17;;,, 	"!!qv&&qv&& !-%);;;)!&%'+qvFFK[+666r$   c                     t          j        | j        |j                  }t          t	          j        | j        |j                  |          S r   )r5   r[   r,   r*   r   Minr+   r\   s      r"   minimumzSymPyOps.minimum   8    )!'17;;161622K@@@r$   c                     t          j        | j        |j                  }t          t	          j        | j        |j                  |          S r   )r5   r[   r,   r*   r   Maxr+   r\   s      r"   maximumzSymPyOps.maximum   rx   r$   )NF)r:   r;   r<   r=   staticmethodr   r2   r	   r   r   r    r5   r,   r*   rI   r   r&   rL   r   rP   rS   rW   r^   ra   rc   re   rh   rm   rt   rw   r{   r@   r$   r"   rB   rB   R   s              \ 'c5$./ ' '	 ' ' ' \' '%
C0 ' ' ' ' ' \'  ,0"'	, ,,{, EK(,  	,
 
, , , \, /y /Y / / / \/ 3) 3	 3 3 3 \3 7y 7Y 79 7 7 7 \7 7y 7Y 79 7 7 7 \7 7y 7Y 79 7 7 7 \7 +y +Y + + + \+ @I @) @	 @ @ @ \@ 3y 3Y 38I+> 3 3 3 \3 Y 9 )1D    \" A9 A Ay A A A \A A9 A Ay A A A \A A Ar$   rB   c                   N    e Zd ZU eed<   dZeed<   ededd fd            Z	d Z
dS )	IndexPropVarrC   Fis_symbolicr+   rD   c                 $    t          | d          S )NTr   )r~   )r+   s    r"   new_symboliczIndexPropVar.new_symbolic   s    Dd3333r$   c                 `    | j         r$t          | j        t                    sJ d            d S d S )Nz.Symbolic IndexPropVar must contain a TypedExpr)r   r   rC   r*   r.   s    r"   r9   zIndexPropVar.__post_init__   sI    # 	
z$*i'H'H 	
 	
<	
 	
H 	
 	
 	
 	
r$   N)r:   r;   r<   r   r?   r   r    r|   r*   r   r9   r@   r$   r"   r~   r~      sj         JJJK49 4 4 4 4 \4
 
 
 
 
r$   r~   )IndexPropResult.r   c            	       .   e Zd ZdZdedeej        ej        f         deej        ej        f         ddfdZ	dej        d	e
j        defd
Zdeeef         defdZdefdZeded         dee         deeef         defd            Zededee         deeef         defd            Zdedee         deeef         defdZdedee         deeef         defdZdedeedf         deeef         defdZd Z	 	 ddeeef         dededefdZdS )IndexPropagationzOps wrapper that tries to propagate constant and index_expr values through the computation.

    This aims to maximize the compile time simplification possible, and convert
    indirect indexing from arange into normal static indexing.

    inneriter_rangesindirect_var_rangesrD   Nc                 6   || _         t          j        j        j        | _        d |                                D             }t          t          j        | j        j	                                        |                                                    | _	        || _
        g }|                                D ]7\  }}|                    d|k               |                    ||k                8t          |          | j                                        z   | _        d S )Nc           	      V    i | ]&\  }}|t          d t          |          dz
            'S )r   r   r   r(   .0kvs      r"   
<dictcomp>z-IndexPropagation.__init__.<locals>.<dictcomp>   s@     
 
 
6:aA{1k!nnq011
 
 
r$   r   )_innerr   graphsizevars	shape_envitemstuple	itertoolschainvar_to_ranger   append
get_axiomsaxioms)r/   r   r   r   r   r   rQ   ss           r"   __init__zIndexPropagation.__init__   s    )3
 
>I>O>O>Q>Q
 
 
 "ODN7==??ASASAUAUVV
 

 $7 %%'' 	! 	!DAqMM!q&!!!MM!a%    Fmmdn&?&?&A&AAr$   r+   r,   c                     t          |          r3 t          |          |          }| j                            ||          S | j                            ||          S r   )r#   r   r   rI   rL   )r/   r+   r,   r   s       r"   materialize_exprz!IndexPropagation.materialize_expr   sY     	4&-&&t,,C;''U333{%%dE222r$   ac                     t          |t          t          f          rt           fd|D                       S t          |t                    s|S |j        r*                     |j        j        |j        j                  S |j        S )Nc              3   B   K   | ]}                     |          V  d S r   unwrapr   r   r/   s     r"   	<genexpr>z*IndexPropagation.unwrap.<locals>.<genexpr>   s-      33AQ333333r$   )	r   listr   r~   r   r   rC   r+   r,   r/   r   s   ` r"   r   zIndexPropagation.unwrap   s    a$'' 	43333333333!\** 	H = 	F((qw}EEEwr$   c                      t          |t          t          f          rt           fd|D                       S t          |          S )Nc              3   B   K   | ]}                     |          V  d S r   )wrapr   s     r"   r   z(IndexPropagation.wrap.<locals>.<genexpr>  s-      11!1111111r$   )r   r   r   r~   r   s   ` r"   r   zIndexPropagation.wrap   sH    a$'' 	21111q111111Ar$   nameindirect_indexingargskwargsc                     d S r   r@   r/   r   r   r   s       r"   fallbackzIndexPropagation.fallback  s	     sr$   c                     d S r   r@   r   s       r"   r   zIndexPropagation.fallback  s	     #r$   c                       fd|D             } fd|                                 D             }                      t           j        |          |i |          S )Nc                 :    g | ]}                     |          S r@   r   )r   r   r/   s     r"   
<listcomp>z-IndexPropagation.fallback.<locals>.<listcomp>  s#    111qDKKNN111r$   c                 B    i | ]\  }}|                     |          S r@   r   )r   r   r   r/   s      r"   r   z-IndexPropagation.fallback.<locals>.<dictcomp>  s)    CCCDAqaQCCCr$   )r   r   getattrr   )r/   r   r   r   new_args
new_kwargss   `     r"   r   zIndexPropagation.fallback  sm     2111D111CCCCFLLNNCCC
yy3d33XLLLMMMr$   c                    dt           t          t          f         dt          fdfd|D             }fd|                                D             } t	          t
          |          |i |}|t          uo|                                p|j        j	        }|s| 
                    |||          S t                              |          S )Nr   rD   c                 >    t          | t                    s| S | j        S r   )r   r~   rC   )r   s    r"   r   z0IndexPropagation.propagate_sympy.<locals>.unwrap  s     a.. 7Nr$   c                 &    g | ]} |          S r@   r@   )r   r   r   s     r"   r   z4IndexPropagation.propagate_sympy.<locals>.<listcomp>"  s!    ,,,!FF1II,,,r$   c                 .    i | ]\  }}| |          S r@   r@   )r   r   r   r   s      r"   r   z4IndexPropagation.propagate_sympy.<locals>.<dictcomp>#  s'    >>>tq!a>>>r$   )r	   r   r~   r   r   rB   rg   r0   r+   
is_integerr   r   )	r/   r   r   r   r   r   new_expris_valid_exprr   s	           @r"   propagate_sympyz IndexPropagation.propagate_sympy  s    	eC-. 	3 	 	 	 	
 -,,,t,,,>>>>v||~~>>>
*78T**HC
CC 6 
   "">hm&> 	
  	5==tV444((222r$   .c                 J   t          t          |          s|                     |||          S d t          j        ||                                          D             }t          d |D                       s|                     |||          S |                     |||          S )Nc                 <    g | ]}t          |t                    |S r@   )r   r~   )r   r   s     r"   r   z-IndexPropagation._default.<locals>.<listcomp>2  s8     
 
 
!\**

 
 
r$   c              3   $   K   | ]}|j         V  d S r   r   )r   r   s     r"   r   z,IndexPropagation._default.<locals>.<genexpr>7  s$      88Q1=888888r$   )hasattrrB   r   r   r   valuesallr   )r/   r   r   r   var_argumentss        r"   _defaultzIndexPropagation._default.  s    x&& 	5==tV444
 
_T6==??;;
 
 

 88-88888 	5==tV444##D$777r$   c                     g | j         d | j                                        D             R }t          | j        || j        |          S )a  
        Given some iter_ranges, return a function that given an expression, returns whether
        it is true or false using value ranges, guard knowledge and runtime_asserts.

        FIXME I think this may not be entirely right, as we may not be able to use all runtime_asserts
              If this is an issue, just use guards in `self.axioms`.

              The proper way of handling this would be to have a global shape_env that adds
              runtime_asserts as they happen in the code. Then, it should be used in SimplifyIndexing
              to perform wrap_expr and in CSEProxy.check_bounds to elide upper / lower bounds also
              for indirect_indexing
        c              3   `   K   | ])\  }}|t          d t          |          dz
            fV  *dS )r   r   Nr   r   s      r"   r   z3IndexPropagation.statically_true.<locals>.<genexpr>K  sT        Aq K;q>>A#5667     r$   )r   r   r   r   r   r   )r/   er   s      r"   statically_truez IndexPropagation.statically_true<  sc    

  4::<<  
 
 %T^Q\RRRr$   Tindexsizecheckc                     t          |t                    r|j        rt          j        |j        j                  } fd}                     d|k              p                      |k              }                     |k               }|r ||          }t          |          r* 	                    d|ft          | |                      |S  	                    d|||fi           j        }	|	S )Nc                                          d| k              r| S                      | dk               r| z   S t          | dk     | z   |           S )Nr   )r   r   )r+   r/   r   s    r"   	wrap_exprz5IndexPropagation.indirect_indexing.<locals>.wrap_exprb  s`    ''T	22 >K))$(33 >$;& 4$;===r$   r   check_bounds)lowerr'   r   )r   r~   r   r   ro   rC   r+   r   r   r   dict)
r/   r   r   r   wrap_negr+   r   can_prove_lowercan_prove_upperindirect_vars
   ` `       r"   r   z"IndexPropagation.indirect_indexingR  s=    e\** 	u/@ 	 =!122D> > > > > > #2219== AUAUB BO #224$;??O ' yu%% "4L?2o:MNNN  
 K}}%uh!?
 

 	 r$   )TT)r:   r;   r<   r=   r   r   r   Symbolr&   r   r5   r,   r   r	   r~   r   r   r   r   r   r   strr   r   r   r   r   r    r   r@   r$   r"   r   r      s        BB %,
23B "%,
":;	B
 
B B B B23UZ 3 3 3 3 3 3c</0 S        
 )* sm S#X	
 
   X '}6:38n	   XNN'}N6:38nN	N N N N33'}36:38n3	3 3 3 3*8S 8c3h 8c3h 8TW 8 8 8 8S S S4 + +S,&'+ + 	+ 
+ + + + + +r$   r   )0r=   r   collections.abcr   dataclassesr   typingr   r   r   r   r	   typing_extensionsr
   r   r5   torch._prims_commonr   r   torch.utils._sympy.functionsr   r   r   torch.utils._sympy.value_rangesr   r   ops_handlerr   r   r   utilsr   virtualizedr   r&   r   r   r    r>   r#   r(   r*   rB   r~   r   r   r?   r   r@   r$   r"   <module>r      s}    ,     $ $ $ $ $ $ ! ! ! ! ! ! : : : : : : : : : : : : : : ' ' ' ' ' '   ? ? ? ? ? ? ? ? I I I I I I I I I I D D D D D D D D ' ' ' ' ' ' + + + + + + " " " " " "       %*eS$./	/i / / / /JY J J J J        2cA cA cA cA cA cA cA cAL 
 
 
 
 
 
 
 
 #<7M1N#NO O O Os s s s s~ s s s s sr$   