
    Ph%                        U d 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 ddlZddlmZ ddlZddlmZmZ ddlmZmZm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      Zy)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)	dataclass)AnyCallableDictLiteralOptionaloverloadTupleUnion)	TypeAlias)is_boolean_dtypeis_integer_dtype)FloorDivModularIndexingWherec                   N    e Zd ZU dZej
                  ed<   ej                  ed<   y)	TypedExprz'A SymPy expression with associated typeexprdtypeN)	__name__
__module____qualname____doc__sympyExpr__annotations__torchr        lC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_inductor/index_propagation.pyr   r   #   s    1
**;;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j                   dej                  deeef   fd       Ze	 ddedej                  d	eej                     deeef   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fd       Zedededefd       Zy)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 Nr   )r#   s    r    identityzSymPyOps.identity3   s    r   r   c                     t        |      rt        j                  t        |             }nHt	        |      rt        j                  t        |             }nt        j                  t        |             }t        ||      S r&   )	r   r   Integerboolr   intFloatfloatr   )r#   r   r   s      r    constantzSymPyOps.constant7   sU    E"==e-De$==U,D;;uU|,Du%%r   c                 d    t        | t              rt        j                  |       } t	        | |      S r&   )
isinstancer+   r   r)   r   )r#   r   s     r    
index_exprzSymPyOps.index_exprA   s'    eS!MM%(E&&r   N	src_dtypec                 6   t        | j                  t        j                  t        j                  f      r t
        j                  | j                  |      S t        |      r5t        | j                        r t
        j                  | j                  |      S t        S r&   )r0   r   r   r)   r,   r"   r.   r   r   r1   NotImplemented)r#   r   r2   s      r    to_dtypezSymPyOps.to_dtypeG   si     ejj5==%++">?$$UZZ77e$)9%++)F&&uzz599 "!r   xc                 \    t        | j                  | j                  z  | j                        S r&   r   r   r   r6   s    r    squarezSymPyOps.squareS   s    !&&!''22r   yc                     t        j                  | j                  |j                        }t        | j                  |j                  z   |      S r&   r   promote_typesr   r   r   r6   r;   result_types      r    addzSymPyOps.addW   5    ))!''177;!&&+66r   c                     t        j                  | j                  |j                        }t        | j                  |j                  z
  |      S r&   r=   r?   s      r    subzSymPyOps.sub\   rB   r   c                     t        j                  | j                  |j                        }t        | j                  |j                  z  |      S r&   r=   r?   s      r    mulzSymPyOps.mula   rB   r   c                 D    t        | j                   | j                        S r&   r8   r9   s    r    negzSymPyOps.negf   s    !&&!''**r   c                     t        j                  | j                  |j                        }t        |      st        S t        t        | j                  |j                        |      S r&   )r   r>   r   r   r4   r   r   r   r?   s      r    floordivzSymPyOps.floordivj   sF    ))!''177;,!!!&&!&&1;??r   c                     t        j                  | j                  |j                        }t        |      st        S t        | j                  t        j                  d      |j                        }t        ||      S )N   )
r   r>   r   r   r4   r   r   r   r)   r   )r6   r;   r@   result_exprs       r    	remainderzSymPyOps.remainderr   sV    ))!''177;,!!%affemmA.>Gk22r   c                     t        j                  | j                  |j                        }t        t	        j
                  | j                  |j                        |      S r&   )r   r>   r   r   r   Minr   r?   s      r    minimumzSymPyOps.minimum{   <    ))!''177;1661662K@@r   c                     t        j                  | j                  |j                        }t        t	        j
                  | j                  |j                        |      S r&   )r   r>   r   r   r   Maxr   r?   s      r    maximumzSymPyOps.maximum   rR   r   r&   )r   r   r   r   staticmethodr   r'   r   r+   r-   r*   r   r   r   r.   r   r   r1   r   r5   r:   rA   rD   rF   rH   rJ   rN   rQ   rU   r   r   r    r"   r"   +   s`        &c5$./ & &	 & & '%** 'U[[ 'U3	>=R ' '
 KO	"	" ;;	"3;EKK3H	"	sI~		" 	" 3) 3	 3 3 7y 7Y 79 7 7 7y 7Y 79 7 7 7y 7Y 79 7 7 +y +Y + + @I @) @	 @ @ 3Y 39 3)1D 3 3 A9 A Ay A A A9 A Ay A Ar   r"   c                   F    e Zd ZU eed<   dZeed<   ededd fd       Z	d Z
y)	IndexPropVarr#   Fis_symbolicr   r$   c                     t        | d      S )NTrY   )rX   )r   s    r    new_symboliczIndexPropVar.new_symbolic   s    Dd33r   c                 `    | j                   r"t        | j                  t              sJ d       y y )Nz.Symbolic IndexPropVar must contain a TypedExpr)rY   r0   r#   r   )selfs    r    __post_init__zIndexPropVar.__post_init__   s5    ##zJJ	(
 	<;	< 
 (
#r   N)r   r   r   r   r   rY   r*   rV   r   r\   r_   r   r   r    rX   rX      s6    JK49 4 4 4<r   rX   )IndexPropResult.r`   c            	          e Zd ZdZde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f   deeef   defd       Zededeedf   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edeedf   deeef   defdZdededef   fdZ	 ddeeef   dededefdZy)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.

    innerc                     || _         y r&   )_inner)r^   rc   s     r    __init__zIndexPropagation.__init__   s	    r   r   r   r$   c                    t        |t        j                        r%| j                  j	                  t        |      |      S |j                  r%| j                  j	                  t        |      |      S | j                  j                  ||      S r&   )	r0   r   r)   re   r.   r+   	is_numberr-   r1   )r^   r   r   s      r    materialize_exprz!IndexPropagation.materialize_expr   sd    dEMM*;;''D	599^^;;''dU;;{{%%dE22r   ac                      t        |t        t        f      rt         fd|D              S t        |t              s|S |j                  r: j                  |j                  j                  |j                  j                        S |j                  S )Nc              3   @   K   | ]  }j                  |        y wr&   )unwrap.0vr^   s     r    	<genexpr>z*IndexPropagation.unwrap.<locals>.<genexpr>   s     3AQ   )	r0   listtuplerX   rY   ri   r#   r   r   r^   rj   s   ` r    rm   zIndexPropagation.unwrap   sf    a$'3333!\*H ==((qww}}EEwwr   c                 n     t        |t        t        f      rt         fd|D              S t        |      S )Nc              3   @   K   | ]  }j                  |        y wr&   )wraprn   s     r    rq   z(IndexPropagation.wrap.<locals>.<genexpr>   s     1q!1qrr   )r0   rs   rt   rX   ru   s   ` r    rx   zIndexPropagation.wrap   s,    a$'1q111Ar   nameindirect_indexingargs.kwargsc                      y r&   r   r^   ry   r{   r|   s       r    fallbackzIndexPropagation.fallback   s     	r   c                      y r&   r   r~   s       r    r   zIndexPropagation.fallback   s     	r   c                    |D cg c]  }| j                  |       }}|j                         D ci c]  \  }}|| j                  |       }}}| j                   t        | j                  |      |i |      S c c}w c c}}w r&   )rm   itemsrx   getattrre   )	r^   ry   r{   r|   rj   new_argskrp   
new_kwargss	            r    r   zIndexPropagation.fallback   sz     -11DqDKKND14:LLNCNDAqaQ'N
Cyy3d3XLLMM 2Cs
   A<Bc                    dt         t        t        f   dt        fd}|D cg c]
  } ||       }}|j                         D ci c]  \  }}| ||       }	}} t	        t
        |      |i |	}
|
t        uxr< t        |
j                  t        j                        xs |
j                  j                  }|s| j                  |||      S t        j                  |
      S c c}w c c}}w )Nrj   r$   c                 >    t        | t              s| S | j                  S r&   )r0   rX   r#   )rj   s    r    rm   z0IndexPropagation.propagate_sympy.<locals>.unwrap   s    a.77Nr   )r   r   rX   r   r   r"   r4   r0   r   r   Number
is_integerr   r\   )r^   ry   r{   r|   rm   rj   r   r   rp   r   new_expris_valid_exprs               r    propagate_sympyz IndexPropagation.propagate_sympy   s    	eC-. 	3 	
 (,,t!F1It,/5||~>~tq!al~
>*78T*HC
C 6 
 x}}ell3 (}}''	 	 ==tV44((22 ->s   C"C'c                 <     dt         dt         dt        f fd}|S )Nr{   r|   r$   c                  F   t        t              sj                  | |      S t        j                  | |j                               D cg c]  }t        |t              r| }}t        d |D              sj                  | |      S j                  | |      S c c}w )Nc              3   4   K   | ]  }|j                     y wr&   r[   )ro   rp   s     r    rq   z>IndexPropagation.__getattr__.<locals>.inner.<locals>.<genexpr>   s     <mq}}ms   )
hasattrr"   r   	itertoolschainvaluesr0   rX   allr   )r{   r|   rj   var_argumentsry   r^   s       r    rc   z+IndexPropagation.__getattr__.<locals>.inner   s    8T*}}T488 #v}}??Aa. ?  
 <m<<}}T488''dF;;s   B)r   r`   )r^   ry   rc   s   `` r    __getattr__zIndexPropagation.__getattr__   s$    	< 	< 	< 	< r   indexsizecheckc                     t        |t              r5|j                  r)|j                  j                  }|t        |dk\  d|      z   S | j                  d|||fi       j                  S )Nr   rz   )r0   rX   rY   r#   r   r   r   )r^   r   r   r   s       r    rz   z"IndexPropagation.indirect_indexing   s^     e\*u/@/@KK$$E5!Q555}}05$2FKQQQr   N)T)r   r   r   r   r   rf   r   r   r   r   ri   r   rX   rm   r`   rx   r	   r   r
   r   strr   r   r   r   r*   rz   r   r   r    rb   rb      s   c 3UZZ 3 3 3c</0 S  
 )* CHo S#X	
 
  $S#X8<S#X	 
NN$S#XN8<S#XN	N33$S#X38<S#X3	3, o1E(F $ IMR3,-R58RAER	Rr   rb   )r   r   dataclassesr   typingr   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   torch._prims_commonr   r   torch.utils._sympy.functionsr   r   r   r   r"   rX   r`   r   rb   r   r   r    <module>r      s   *  ! Q Q Q  '  B I I   XA XAv < < < #<7M1N#NO OnR nRr   