
    PiS                        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	  e j
        d           dd            Ze j        dd	d
            Ze j        j         e j
        d           ddddede j        dee j        edf         dede	j        dee j        ee j        df         f         fd                        Zee_        dS )    N   )util)UnionTuplec                      d fd	S )Nc                 6     | fd|D             |          S )Nc                 &    g | ]} |          S  r
   .0xts     e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/rearrange.py
<listcomp>z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>
   s!    ,,,A11Q44,,,    r
   )exprs_in
tensors_in	exprs_outbackendcr   s       r   <lambda>z<lambda>.<locals>.<lambda>	   s1    QQ,,,,,,,iN N r   Nr
   r   r   s   ``r   r   r   	   s$           r   )tracec                     t                     t                    k    r/t          dt                      dt                               t          d t                     t          |          z   D                       rt          dt           d          fdt                     D                                 d          t          j         	          \   t          j        |          }t          d
  D                       sJ t          d |D                       sJ t                     t          |          k    r0t          dt                      dt          |           d          t          j
         |          } fd|D              fd|D             fdt           |          D             }t          j        |||	          }||fS )N	Expected z input tensor(s), got c              3      K   | ]?}|                                 D ](}t          |t          j        j        j                  V  )@d S r   )all
isinstanceeinxexprstage3Marker)r   rootr!   s      r   	<genexpr>z#rearrange_stage3.<locals>.<genexpr>   sg        HHJJ   	4)011      r   zMarker 'z' is not allowedc           	      h    g | ].\  }}t           j                            ||j        d d          /S )	embedding	rearrange)nameinit)r    tracercall_factoryshape)r   tensorr!   r   s      r   r   z$rearrange_stage3.<locals>.<listcomp>   sK       FD 	  W;U` aa  r   T)convert_scalarsr   c              3   ^   K   | ](}t           j        j                            |          V  )d S r   r    r!   r"   is_flatr   r!   s     r   r%   z#rearrange_stage3.<locals>.<genexpr>!   s5      CC$ty''--CCCCCCr   c              3   ^   K   | ](}t           j        j                            |          V  )d S r   r2   r4   s     r   r%   z#rearrange_stage3.<locals>.<genexpr>"   s5      II$ty''--IIIIIIr   zGot different number of input (z) and output expressions (z) (after flattening)c                      g | ]
}|         S r
   r
   )r   ir   s     r   r   z$rearrange_stage3.<locals>.<listcomp>+   s    ------r   c                      g | ]
}|         S r
   r
   )r   r7   r   s     r   r   z$rearrange_stage3.<locals>.<listcomp>,   s    111A*Q-111r   c                 T    g | ]$\  }}}t          j        |||           d         %S )r0   r   )r   transpose_broadcast)r   expr_inr.   expr_outr   s       r   r   z$rearrange_stage3.<locals>.<listcomp>/   sH       %GVX 	 &(GLLLQO  r   )len
ValueErroranylistr!   zipall_to_tensorr   flattenr   
assignment	unflatten)r   r   r   r   exprs_out_flatindicestensorss   `` `   r   rearrange_stage3rI      s\    8}}J''[S]][[#j//[[\\\
  NNT)__4     <
 :D:::;;;   
H55  J &&z4&HHJ  <*gNNNHj\),,NCC(CCCCCCCCII.IIIIIIII
8}}N++++:c(mm : :N##: : :
 
 	
 oh77G----W---H1111111J   ),Xz>)R)R  G n^WiQQQGIr   Tcsec                   t           j        j                            | |          \  } }t           j        j                            |           }t          |d                   t          |          k    r5t          dt          |d                    dt          |                     t           j        	                    d t          |d         |          D             d |d         D             z   d |                                D             z   |          d t          |d                   t          |d                   z            }|d t          |d                            |t          |d                   d          }}||fS )	Nr   r   z input tensors, but got c                 T    g | ]%\  }}t           j                            ||          &S r
   r    r!   Equation)r   r;   tensor_shapes      r   r   zparse.<locals>.<listcomp>F   s>     	
 	
 	
% Iw55	
 	
 	
r   c                 L    g | ]!}t           j                            |          "S r
   rN   )r   r<   s     r   r   zparse.<locals>.<listcomp>J   s(    
>
>
>H49h''
>
>
>r   r   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS ).N)depth1depth2)r    r!   rO   npasarraynewaxis)r   kvs      r   r   zparse.<locals>.<listcomp>K   sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   rJ   )r    opr   !_clean_description_and_parametersr!   stage1parse_opr=   r>   solverA   items)descriptionrK   tensor_shapes
parametersrZ   exprsr   r   s           r   parserd   :   s   "glLLZ K 
		"	";	/	/B
2a5zzS'''']SAZZ]]]I[I[]]^^^IOO	
 	
),RUM)B)B	
 	
 	
 ?
>1
>
>
>		?

 
"((**
 
 
	
      BqE

SAZZ!E  #be**-uSAZZ\\/BiHYr   c                      d d fd
S )Nr0   c                4     | gfd|D             R i |S )Nc                 &    g | ]} |          S r
   r
   r   s     r   r   z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>Y   s!    ---qqtt---r   r
   )r`   r   rH   kwargsr   r   s       r   r   z<lambda>.<locals>.<lambda>X   sJ    QQN----W---N N N17N N r   r
   r   s   ``r   r   r   X   s+    T        r   )r   rK   r`   rH   r   rK   rb   return.c                    t          | gd |D             R d|i|\  }}t          ||||          \  }}t          |          dk    r|d         n|S )a  Rearranges the input tensors to match the output expressions.

    Args:
        description: Description string for the operation in einx notation. Must not contain
            brackets.
        tensors: Input tensors or tensor factories matching the description string.
        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 rearrange operation if ``graph=False``, otherwise the graph
        representation of the operation.

    Examples:
        Transpose the row and column axes of a batch of images:

        >>> x = np.random.uniform(size=(4, 64, 48, 3))
        >>> einx.rearrange("b h w c -> b w h c", x).shape
        (4, 48, 64, 3,)

        Insert new axis (repeats elements along the new axis):

        >>> x = np.random.uniform(size=(10, 10))
        >>> einx.rearrange("a b -> a c b", x, c=100).shape
        (10, 100, 10,)

        Concatenate two tensors along the first axis:

        >>> a, b = (
        ...     np.random.uniform(size=(10, 10)),
        ...     np.random.uniform(size=(20, 10)),
        ... )
        >>> einx.rearrange("a b, c b -> (a + c) b", a, b).shape
        (30, 10,)

        Split a tensor:

        >>> x = np.random.uniform(size=(10, 2))
        >>> a, b = einx.rearrange("a (1 + 1) -> a, a", x)
        >>> a.shape, b.shape
        ((10,), (10,))

        Swap the first and last third of a tensor along a given axis:

        >>> x = np.arange(6)
        >>> einx.rearrange("(b + c + d) -> (d + c + b)", x, b=2, c=2)
        array([4, 5, 2, 3, 0, 1])
    c                 L    g | ]!}t           j                            |          "S r
   )r    r+   	get_shape)r   r.   s     r   r   zrearrange.<locals>.<listcomp>   s(    KKKt{,,V44KKKr   rK   r0   r   r   )rd   rI   r=   )r`   r   rK   rH   rb   r   r   s          r   r(   r(   V   s    D  KK7KKK  QTXb Hi *(GYPWXXXGYY1,,71::'9r   r   )r     r   numpyrU   typingr   r   numpy.typingnptjitrI   	lru_cacherd   traceback_utilfilterstrTensorBackendbool	ArrayLiker(   r
   r   r   <module>r{      s                            
   
* * * 
*Z +/     6 	    /3	@: @: @:@:k@: 4<d*+@: 
	@:
 -@: 4;dk3.//0@: @: @:  @:F 	r   