
    Pi/                     b   d dl Z ddlmZ d dlZd dlmZ d dlmZm	Z	 d dl
mZ eZ e j        d           d+d            Ze j        d,d
            Ze j        j         e j        d           	 	 	 d-dede j        de	eef         de	edf         de	e j        edf         dedej        de j        fd                        Zee_        e j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Zd Ze j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Zd Ze j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Zd Ze j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Z d Z!e j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Z"d Z#e j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd            Z$d  Z%e j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd!            Zd" Z&e j        j        	 	 	 d-dede j        de	edf         de	e j        edf         dedej        de j        fd#            Z'd$ Z(e j        j        	 	 d.dede j        de	edf         de	e j        edf         dej        de j        fd%            Z)d& Z*e j        j        	 	 d.dede j        de	edf         de	e j        edf         dej        de j        fd'            Z+d( Z,e j        j        	 	 d.dede j        de	edf         de	e j        edf         dej        de j        fd)            Z-d* Z.dS )/    N   )util)partial)CallableUnionc                      d fd	S )Nc                 4     |  |          ||          S )N)op )expr_in	tensor_inexpr_outr
   backendcts        b/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/reduce.py<lambda>z<lambda>.<locals>.<lambda>   s,    aa9xBO O O     Nr   r   r   s   ``r   r   r      $           r   )tracec                    | |fD ]L}|                                 D ]5}t          |t          j        j        j                  rt          d          6Mt          j        | g|g|g||          \  }}|d         |d         fS )NzConcatenation not allowed)r   r   )all
isinstanceeinxexprstage3Concatenation
ValueErrorvmap_with_axis_stage3)	r   r   r   r
   r   rootr   tensors_out	exprs_outs	            r   reduce_stage3r%      s     (# > >HHJJ 	> 	>D$	 0 >?? > !<===>	> "7		I;
B  K q>9Q<''r   Tc                   	 t           j        j                            | |          \  } }t           j        j                            |           }t          |          dk    rt           j                            t           j        	                    |d         d         |          gd |
                                D             z   |d          d         }t          d |                                D                       st          d          fd}t           j        j                            ||          }nt          d	          t          |d                   dk    r%t          d
t          |d                              t          |d                   dk    r%t          dt          |d                              t           j                            t           j        	                    |d         d         |          gt           j        	                    |d         d                   gz   d |
                                D             z   |d          d d         \  }}t          d |                                D                       sFd |                                D             	t           j        j                            |	fd          }||fS )Nr   r   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS .N)depth1depth2r   r   Equationnpasarraynewaxis.0kvs      r   
<listcomp>zparse.<locals>.<listcomp>&   Y       Aq 	""1bjmmCO&DTZ^"__  r   T)csecse_in_markersc              3   ^   K   | ](}t          |t          j        j        j                  V  )d S r   )r   r   r   r   Markerr1   r   s     r   	<genexpr>zparse.<locals>.<genexpr>.   s4      XX$JtTY%5%<==XXXXXXr   z No axes are marked for reductionc                     t          | t          j        j        j                  r*r&t          j        j                            d d          gS g S d S )Nr   )r   r   r   r   r9   Axis)r   keepdimss    r   replacezparse.<locals>.replace2   sR    $	 0 788   I,11$::;;I	 r   z(keepdims cannot be given when using '->'z%Expected 1 input expression, but got z&Expected 1 output expression, but got c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS r(   r+   r0   s      r   r4   zparse.<locals>.<listcomp>G   r5   r      c              3   ^   K   | ](}t           j        j                            |          V  )d S r   )r   r   r   	is_markedr:   s     r   r;   zparse.<locals>.<genexpr>Q   s5      OODI$..t44OOOOOOr   c                 d    h | ]-}t          |t          j        j        j                  &|j        .S r   r   r   r   r   r=   name)r1   axiss     r   	<setcomp>zparse.<locals>.<setcomp>R   sA       "
4IYI^8_8_	  r   c                 ^    t          | t          j        j        j                  o| j        vS r   rE   )r   axes_names_outs    r   r   zparse.<locals>.<lambda>W   s+    Zdi.>.CDD 4I^3 r   )r   r
   r   !_clean_description_and_parametersr   stage1parse_oplensolver,   items_anyr   r    r   r?   mark)
descriptiontensor_shaper>   r6   
parametersr
   r   r?   r   rJ   s
     `      @r   parserV      s   "glLLZ K 
		"	";	/	/B
2ww!||)//Y1a,778 &,,..  
  " 
 
  XX'++--XXXXX 	A?@@@	 	 	 	 	 9#++GW== GHHHr!u::??QSAZZQQRRRr!u::??Rc"Q%jjRRSSS IOOY1a,778y!!"Q%(++,- &,,..    , 	
 	
 1"	 OOOOOOO 	 &.llnn  N i&++4 4 4 4 G Hr   c                      d fd	S )Nc                 *     |  |          fi |S r   r   )rS   tensorr   kwargsr   r   s       r   r   z<lambda>.<locals>.<lambda>`   s1    11QQvYYL L"(L L r   r   r   r   s   ``r   r   r   `   r   r   rS   rY   r
   r>   r   r6   rU   returnc                     t          | t          j                            |          f||d|\  }}t	          |||||          \  }}|S )a	  Applies a reduction operation on the given tensors.

    The operation reduces all marked axes in the input to a single scalar. It supports
    the following shorthand notation:

    * When no brackets are found, brackets are placed implicitly around all axes that do not
      appear in the output.

      Example: ``a b c -> a c`` resolves to ``a [b] c -> a c``.

    * When no output is given, it is determined implicitly by removing marked subexpressions
      from the input.

      Example: ``a [b] c`` resolves to ``a [b] c -> a c``.

    Args:
        description: Description string for the operation in einx notation.
        tensor: Input tensor or tensor factory matching the description string.
        op: Backend reduction operation. Is called with ``op(tensor, axis=...)``. If ``op`` is
            a string, retrieves the attribute of ``backend`` with the same name.
        keepdims: Whether to replace marked expressions with 1s instead of dropping them. Must
            be None when ``description`` already contains an output expression. Defaults to None.
        backend: Backend to use for all operations. If None, determines the backend from the
            input tensors. Defaults to None.
        cse: Whether to apply common subexpression elimination to the expressions. Defaults
            to True.
        graph: Whether to return the graph representation of the operation instead of
            computing the result. Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        The result of the reduction operation if ``graph=False``, otherwise the graph
        representation of the operation.

    Examples:
        Compute mean along rows of a matrix:

        >>> x = np.random.uniform(size=(16, 20))
        >>> einx.mean("a b -> b", x).shape
        (20,)
        >>> einx.mean("[a] b -> b", x).shape
        (20,)
        >>> einx.mean("[a] b", x).shape
        (20,)

        Compute sum along rows of a matrix and broadcast to the original shape:

        >>> x = np.random.uniform(size=(16, 20))
        >>> einx.sum("[a] b -> a b", x).shape
        (16, 20,)

        Sum pooling with kernel size 2:

        >>> x = np.random.uniform(size=(4, 16, 16, 3))
        >>> einx.sum("b (s [s2])... c", x, s2=2).shape
        (4, 8, 8, 3)

        Compute variance per channel over an image:

        >>> x = np.random.uniform(size=(256, 256, 3))
        >>> einx.var("[...] c", x).shape
        (3,)
    )r>   r6   )r
   r   )rV   r   tracer	get_shaper%   )	rS   rY   r
   r>   r   r6   rU   r   r   s	            r   reducer_   ^   si    \ T[**622=E3 R\ GX %Wfh2wWWWFHMr   c                 (    t          | |fd|||d|S )z7Specialization of :func:`einx.reduce` with ``op="sum"``sumr
   r>   r   r6   r_   rS   rY   r>   r   r6   rU   s         r   ra   ra      6     V %'s V`  r   c                      t          | ddi|S )Nr
   ra   r%   argsrZ   s     r   
sum_stage3rj          $353F333r   c                 (    t          | |fd|||d|S )z8Specialization of :func:`einx.reduce` with ``op="mean"``meanrb   rc   rd   s         r   rm   rm      7     V &7PS Wa  r   c                      t          | ddi|S )Nr
   rm   rg   rh   s     r   mean_stage3rp          $464V444r   c                 (    t          | |fd|||d|S )z7Specialization of :func:`einx.reduce` with ``op="var"``varrb   rc   rd   s         r   rs   rs      re   r   c                      t          | ddi|S )Nr
   rs   rg   rh   s     r   
var_stage3ru      rk   r   c                 (    t          | |fd|||d|S )z7Specialization of :func:`einx.reduce` with ``op="std"``stdrb   rc   rd   s         r   rw   rw      re   r   c                      t          | ddi|S )Nr
   rw   rg   rh   s     r   
std_stage3ry      rk   r   c                 (    t          | |fd|||d|S )z8Specialization of :func:`einx.reduce` with ``op="prod"``prodrb   rc   rd   s         r   r{   r{     rn   r   c                      t          | ddi|S )Nr
   r{   rg   rh   s     r   prod_stage3r}     rq   r   c                 (    t          | |fd|||d|S )zASpecialization of :func:`einx.reduce` with ``op="count_nonzero"``count_nonzerorb   rc   rd   s         r   r   r     s>          r   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   count_nonzero_stage3r   *  s    $=?=f===r   c                 (    t          | |fd|||d|S )z7Specialization of :func:`einx.reduce` with ``op="any"``anyrb   rc   rd   s         r   r   r   .  re   r   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   
any_stage3r   =  rk   r   c                 (    t          | |fd|||d|S )z7Specialization of :func:`einx.reduce` with ``op="all"``r   rb   rc   rd   s         r   r   r   A  re   r   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   
all_stage3r   P  rk   r   c                 &    t          | |fd||d|S )z7Specialization of :func:`einx.reduce` with ``op="max"``maxr
   r>   r   rc   rS   rY   r>   r   rU   s        r   r   r   T  &     +vb%(GbbWabbbr   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   
max_stage3r   `  rk   r   c                 &    t          | |fd||d|S )z7Specialization of :func:`einx.reduce` with ``op="min"``minr   rc   r   s        r   r   r   d  r   r   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   
min_stage3r   p  rk   r   c                 &    t          | |fd||d|S )z=Specialization of :func:`einx.reduce` with ``op="logsumexp"``	logsumexpr   rc   r   s        r   r   r   t  s4     V +h S]  r   c                      t          | ddi|S )Nr
   r   rg   rh   s     r   logsumexp_stage3r     s    $9;9&999r   r   )NT)NNT)NN)/r    r   numpyr-   	functoolsr   typingr   r   numpy.typingnptr   rQ   jitr%   	lru_cacherV   traceback_utilfilterstrTensorboolBackend	ArrayLiker_   ra   rj   rm   rp   rs   ru   rw   ry   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s                    " " " " " " " "      
 
   
( ( ( 
( ? ? ? ?D 	    #'.2L LLKL 	hmL D$J	L
 4<d*+L 
L -L 
[L L L  L^   #'.2 K D$J 4<d*+	
 
 - 
[   4 4 4  #'.2 K D$J 4<d*+	
 
 - 
[   5 5 5  #'.2 K D$J 4<d*+	
 
 - 
[   4 4 4  #'.2 K D$J 4<d*+	
 
 - 
[   4 4 4  #'.2 K D$J 4<d*+	
 
 - 
[   5 5 5  #'.2 K D$J 4<d*+	
 
 - 
[   (> > >  #'.2 K D$J 4<d*+	
 
 - 
[   4 4 4  #'.2 K D$J 4<d*+	
 
 - 
[   4 4 4  #'.2	c ccKc D$Jc 4<d*+	c
 -c 
[c c c c4 4 4  #'.2	c ccKc D$Jc 4<d*+	c
 -c 
[c c c c4 4 4  #'.2	
 

K
 D$J
 4<d*+	

 -
 
[
 
 
 
: : : : :r   