
    Pi>                     
   d dl Z ddlmZ d dlmZ d dlZd dlmZm	Z	 d dl
mZ  e j        d           d8d            Ze j        d	d
d            Ze j        j         e j        d           dd	ddede j        de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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        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	ddede j        de	e j        edf         dedej        de j        fd.            Z0d/ Z1e j        j        dd	ddede j        de	e j        edf         dedej        de j        fd0            Z2d1 Z3e j        j        dd	ddede j        de	e j        edf         dedej        de j        fd2            Z4d3 Z5e j        j        dd	ddede j        de	e j        edf         dedej        de j        fd4            Z6d5 Z7e j        j        dd	ddede j        de	e j        edf         dedej        de j        fd6            Z8d7 Z9dS )9    N   )util)partial)CallableUnionc                      d fd	S )Nc                 8     | fd|D             ||          S )Nc                 &    g | ]} |          S  r   .0xts     g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/elementwise.py
<listcomp>z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>   s!    ,,,A11Q44,,,    r   )exprs_in
tensors_inexpr_outopbackendcr   s        r   <lambda>z<lambda>.<locals>.<lambda>
   s5    PQPQ,,,,,,,hQ Q r   Nr   r   r   s   ``r   r   r   
   s$           r   )tracec                 n  	 t          |           |gz   D ]L}|                                D ]5}t          |t          j        j        j                  rt          d          6Mt          d | D                       rJ t          d |                                D                       rJ d 		fdt          ||           D             }
                    |          }t          j        | ||g          \  }}t          |          dk    rt          |          dk    sJ |d         |d         fS )	NzConcatenation not allowedc              3      K   | ]?}|                                 D ](}t          j        j                            |          V  )@d S r   )alleinxexprstage3	is_marked)r   rootr!   s      r   	<genexpr>z%elementwise_stage3.<locals>.<genexpr>   sO      ``UYU]U]U_U_``T49#--d33```````r   c              3   ^   K   | ](}t           j        j                            |          V  )d S r   )r    r!   r"   r#   r   r!   s     r   r%   z%elementwise_stage3.<locals>.<genexpr>   s5      OO49#--d33OOOOOOr   c                 &    | dk    rdS | dk    rdS | S )Naddbiasmultiplyscaler   )ss    r   get_namez$elementwise_stage3.<locals>.get_name   s#    ::6*__7Hr   c                     g | ][\  }}t           j                            ||j         t	          j                            t	          j                             \S ))nameinit)r    tracercall_factoryshaper   
_op_to_str)r   tensorr!   r   r.   r   s      r   r   z&elementwise_stage3.<locals>.<listcomp>    su     	 	 	 FD 	  J$/"--..$$ 	! 	
 	
	 	 	r   r   r   r   )listr   
isinstancer    r!   r"   Concatenation
ValueErroranyzipall_to_tensorvmap_with_axis_stage3len)
r   r   r   r   r   r$   r!   tensors_out	exprs_outr.   s
      ``    @r   elementwise_stage3rC   	   s    X(+ > >HHJJ 	> 	>D$	 0 >?? > !<===>	> ``````````OOOOOOOOOO  	 	 	 	 	 	  
H55	 	 	J &&z22J!7*xj"g  K {q  S^^q%8%8%88q>9Q<''r   T)csec          	         t           j        j                            | |          \  } }t           j        j                            |           }t          |d                   dk    rt          |          dk    rt           j        j                            t           j        j        	                    t           j        j        
                    |d         d                   t           j        j                            |d         d                   g          gt          |dd                    z             }t          |          dk    r'|d         }d |D             }t                      }t          |          D ]R\  }}	t          |          D ]"\  }
}||
k    r|                    |	          s n#|                    ||                    St          |          dk    rt#          d| d          t%          t'          |                                                    }t           j        j                            |d         t           j        j        	                    |g          g          }t          |d                   t          |          k    r5t#          dt          |d                    dt          |                     t          |d                   dk    r%t#          d	t          |d                              t           j                            d
 t-          |d         |          D             t           j                            |d         d                   gz   d |                                D             z   |d          d t          |d                   dz            }|d d         |d         }}||fS )Nr   r      c                 J    g | ] }d  |                                 D             !S )c                 d    h | ]-}t          |t          j        j        j                  &|j        .S r   )r9   r    r!   stage1	NamedAxisr0   r'   s     r   	<setcomp>z#parse.<locals>.<listcomp>.<setcomp>M   s1    ^^^4D$)BRB\1]1]^TY^^^r   )r   )r   r$   s     r   r   zparse.<locals>.<listcomp>L   s?     
 
 
 _^488::^^^
 
 
r   z9Could not implicitly determine output expression for op ''z	Expected z input tensors, but got z&Expected 1 output expression, but got c                 T    g | ]%\  }}t           j                            ||          &S r   )r    r!   Equation)r   expr_intensor_shapes      r   r   zparse.<locals>.<listcomp>e   s>     	
 	
 	
% Iw55	
 	
 	
r   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS ).N)depth1depth2)r    r!   rN   npasarraynewaxis)r   kvs      r   r   zparse.<locals>.<listcomp>n   sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   F)rD   
cse_concat)r    r   r   !_clean_description_and_parametersr!   rI   parse_opr@   OpArgsdemark
get_markedr8   set	enumerateissubsetr)   r;   nextiter__deepcopy__solver=   rN   items)descriptionrD   tensor_shapes
parametersr   
input_argsin_axis_namesvalid_parentsiparentjchildr   exprsr   s                  r   parsert   3   s   "glLLZ K 
		"	";	/	/B 2a5zzQ3}--22Y  	 %%I$++BqE!H55I$//1a99'   2abb6ll
 
 2ww!|| U

 
"
 
 

 "=11 	1 	1IAv%m44 1 1566%.."8"86E !!*Q-000}""^Y[^^^___]++,,99;;Y  "Q%)9)>)>z)J)J!KLL
2a5zzS'''']SAZZ]]]I[I[]]^^^
2a5zzQN#be**NNOOOIOO	
 	
),RUM)B)B	
 	
 	

 I1a 
		

 
"((**
 
 
	
      BqE

Q!E" ssU2YhHXr   c                      d d fd
S )Nr7   c                4     | gfd|D             R i |S )Nc                 &    g | ]} |          S r   r   r   s     r   r   z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>}   s!    ---qqtt---r   r   )ri   r   tensorskwargsr   r   s       r   r   z<lambda>.<locals>.<lambda>|   sJ    QQN----W---N N N17N N r   r   r   s   ``r   r   r   |   s+    T        r   )r   rD   ri   rx   r   r   rD   rk   returnc                r    t          | gd |D             R d|i|\  }}t          |||||          \  }}|S )a~  Applies an element-by-element operation over the given tensors.

    It supports the following shorthand notation:

    * The output is determined implicitly if one of the input expressions contains the named axes
      of all other inputs and if this choice is unique.

      | Example: ``a b, a`` expands to ``a b, a -> a b``.
      | Example: ``b a, b, a`` expands to ``b a, b, a -> b a``.
      | Example: ``a b, b a`` raises an exception.
      | Example: ``a b, a b`` expands to ``a b, a b -> a b``.

    * Bracket notation can be used when passing two input tensors to indicate that the second
      input is a subexpression of the first.

      Example: ``a [b]`` expands to ``a b, b``.

    Args:
        description: Description string for the operation in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        op: Backend elemebt-by-element operation. Must accept the same number of tensors
            as specified in the description string and comply with numpy broadcasting rules.
            If ``op`` is a string, retrieves the attribute of ``backend`` with the same name.
        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 elementwise operation if ``graph=False``, otherwise the graph
        representation of the operation.

    Examples:
        Compute a sum of two vectors:

        >>> a, b = np.random.uniform(size=(10,)), np.random.uniform(size=(10,))
        >>> einx.elementwise("a, a -> a", a, b, op=np.add).shape
        (10,)

        Add a vector on all columns of a matrix:

        >>> a, b = np.random.uniform(size=(10, 10)), np.random.uniform(size=(10,))
        >>> einx.add("a b, a -> a b", a, b).shape
        (10, 10,)

        Subtract a vector from all rows of a matrix:

        >>> a, b = np.random.uniform(size=(10, 10)), np.random.uniform(size=(10,))
        >>> einx.subtract("a b, b -> a b", a, b).shape
        (10, 10,)

        Select from one of two choices according to a boolean mask:

        >>> x, mask = (
        ...     np.random.uniform(size=(10, 10)),
        ...     np.random.uniform(size=(10,)),
        ... )
        >>> einx.where("a, a b, -> a b", mask, x, 0).shape
        (10, 10,)

        Add a bias onto all channels of a tensor:

        >>> x, w = (
        ...     np.random.uniform(size=(4, 16, 16, 64)),
        ...     np.random.uniform(size=(64,)),
        ... )
        >>> einx.add("b... [c]", x, w).shape
        (4, 16, 16, 64)
    c                 L    g | ]!}t           j                            |          "S r   )r    r2   	get_shape)r   r6   s     r   r   zelementwise.<locals>.<listcomp>   s(    KKKt{,,V44KKKr   rD   )r   r   )rt   rC   )	ri   r   r   rD   rx   rk   r   r   r6   s	            r   elementwiser~   z   sq    l KK7KKK  QTXb Hh *(GX"V]^^^FHMr   c                *    t          | g|R d||d|S )z<Specialization of :func:`einx.elementwise` with ``op="add"``r)   r   r   rD   r~   ri   r   rD   rx   rk   s        r   r)   r)      s,     {_W__S__T^___r   c                      t          | ddi|S )Nr   r)   rC   argsry   s     r   
add_stage3r      s    t88888r   c                *    t          | g|R d||d|S )zASpecialization of :func:`einx.elementwise` with ``op="subtract"``subtractr   r   r   s        r   r   r      -     {dWddWRUddYcdddr   c                      t          | ddi|S )Nr   r   r   r   s     r   subtract_stage3r          t=
=f===r   c                *    t          | g|R d||d|S )zASpecialization of :func:`einx.elementwise` with ``op="multiply"``r+   r   r   r   s        r   r+   r+      r   r   c                      t          | ddi|S )Nr   r+   r   r   s     r   multiply_stage3r     r   r   c                *    t          | g|R d||d|S )zDSpecialization of :func:`einx.elementwise` with ``op="true_divide"``true_divider   r   r   s        r   r   r   
  ?      "/c MW  r   c                      t          | ddi|S )Nr   r   r   r   s     r   true_divide_stage3r         t@@@@@r   c                *    t          | g|R d||d|S )zESpecialization of :func:`einx.elementwise` with ``op="floor_divide"``floor_divider   r   r   s        r   r   r     s?      "0's NX  r   c                      t          | ddi|S )Nr   r   r   r   s     r   floor_divide_stage3r   *  s    tAA&AAAr   c                *    t          | g|R d||d|S )z?Specialization of :func:`einx.elementwise` with ``op="divide"``divider   r   r   s        r   r   r   .  s-     {bWbb7PSbbWabbbr   c                      t          | ddi|S )Nr   r   r   r   s     r   divide_stage3r   :  s    t;;F;;;r   c                *    t          | g|R d||d|S )zDSpecialization of :func:`einx.elementwise` with ``op="logical_and"``logical_andr   r   r   s        r   r   r   >  r   r   c                      t          | ddi|S )Nr   r   r   r   s     r   logical_and_stage3r   L  r   r   c                *    t          | g|R d||d|S )zCSpecialization of :func:`einx.elementwise` with ``op="logical_or"``
logical_orr   r   r   s        r   r   r   P  ?      ".S LV  r   c                      t          | ddi|S )Nr   r   r   r   s     r   logical_or_stage3r   ^      t?????r   c                *    t          | g|R d||d|S )z>Specialization of :func:`einx.elementwise` with ``op="where"``wherer   r   r   s        r   r   r   b  ,     {aWaa'saaV`aaar   c                      t          | ddi|S )Nr   r   r   r   s     r   where_stage3r   n      t::6:::r   c                *    t          | g|R d||d|S )z=Specialization of :func:`einx.elementwise` with ``op="less"``lessr   r   r   s        r   r   r   r  s,     {`W``c``U_```r   c                      t          | ddi|S )Nr   r   r   r   s     r   less_stage3r   ~  s    t99&999r   c                *    t          | g|R d||d|S )zCSpecialization of :func:`einx.elementwise` with ``op="less_equal"``
less_equalr   r   r   s        r   r   r     r   r   c                      t          | ddi|S )Nr   r   r   r   s     r   less_equal_stage3r     r   r   c                *    t          | g|R d||d|S )z@Specialization of :func:`einx.elementwise` with ``op="greater"``greaterr   r   r   s        r   r   r     -     {cWccGQTccXbcccr   c                      t          | ddi|S )Nr   r   r   r   s     r   greater_stage3r         t<	<V<<<r   c                *    t          | g|R d||d|S )zFSpecialization of :func:`einx.elementwise` with ``op="greater_equal"``greater_equalr   r   r   s        r   r   r     s?      "17 OY  r   c                      t          | ddi|S )Nr   r   r   r   s     r   greater_equal_stage3r     s    tBB6BBBr   c                *    t          | g|R d||d|S )z>Specialization of :func:`einx.elementwise` with ``op="equal"``equalr   r   r   s        r   r   r     r   r   c                      t          | ddi|S )Nr   r   r   r   s     r   equal_stage3r     r   r   c                *    t          | g|R d||d|S )zBSpecialization of :func:`einx.elementwise` with ``op="not_equal"``	not_equalr   r   r   s        r   r   r     s?      "-wC KU  r   c                      t          | ddi|S )Nr   r   r   r   s     r   not_equal_stage3r     s    t>>v>>>r   c                *    t          | g|R d||d|S )z@Specialization of :func:`einx.elementwise` with ``op="maximum"``maximumr   r   r   s        r   r   r     r   r   c                      t          | ddi|S )Nr   r   r   r   s     r   maximum_stage3r     r   r   c                *    t          | g|R d||d|S )z@Specialization of :func:`einx.elementwise` with ``op="minimum"``minimumr   r   r   s        r   r   r     r   r   c                      t          | ddi|S )Nr   r   r   r   s     r   minimum_stage3r     r   r   r   ):r     r   	functoolsr   numpyrT   typingr   r   numpy.typingnptjitrC   	lru_cachert   traceback_utilfilterstrTensorBackendbool	ArrayLiker~   r)   r   r   r   r+   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s                    " " " " " " " "       
   
"( "( "( 
"(J +/ C C C C CL 	    /3T T TTkT 	T 4<d*+	T
 
T -T 
[T T T  Tn    /3	` ` ``k` 4<d*+` 
	`
 -` 
[` ` ` `9 9 9  /3	e e eeke 4<d*+e 
	e
 -e 
[e e e e> > >  /3	e e eeke 4<d*+e 
	e
 -e 
[e e e e> > >  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
A A A  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
B B B  /3	c c cckc 4<d*+c 
	c
 -c 
[c c c c< < <  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
A A A  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
@ @ @  /3	b b bbkb 4<d*+b 
	b
 -b 
[b b b b; ; ;  /3	a a aaka 4<d*+a 
	a
 -a 
[a a a a: : :  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
@ @ @  /3	d d ddkd 4<d*+d 
	d
 -d 
[d d d d= = =  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
C C C  /3	b b bbkb 4<d*+b 
	b
 -b 
[b b b b; ; ;  /3	
 
 

k
 4<d*+
 
	

 -
 
[
 
 
 
? ? ?  /3	d d ddkd 4<d*+d 
	d
 -d 
[d d d d= = =  /3	d d ddkd 4<d*+d 
	d
 -d 
[d d d d= = = = =r   