
    Pin1                     p   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	m
Z
mZ d dlmZ ddgZ e j        d 	          dd
            Ze j        ddd            Ze j        j         e j        d 	          ddi ddede j        dede
e j        edf         dede	dej        fd                        Zee_        e j        j        	 	 ddede j        de
e j        edf         dedej        f
d            Ze j        j        	 	 ddede j        de
eee         f         de
e j        edf         dedej        fd            Ze j        j        	 	 ddede j        de
e j        edf         dedej        f
d            Ze j        j        	 	 ddede j        de
e j        edf         dedej        f
d            ZdS )     N   )util)partial)CallableMappingUnionTuplerollflipc                      i d f fd	S )Nc                 :     | fd|D             |||          S )Nc                 &    g | ]} |          S  r   .0xts     j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/vmap_with_axis.py
<listcomp>z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>   s!    ,,,A11Q44,,,    r   )exprs_in
tensors_in	exprs_outopkwargsbackendcr   s         r   <lambda>z<lambda>.<locals>.<lambda>   s7    \]\],,,,,,,iV] ] r   r   r   r   s   ``r   r   r      s)    "VZ       r   )tracec           	      	   |i }t          |           t          |          k    r/t          dt          |            dt          |                     t          t          |                    dk    rt          d          t          |           t          |          z   D ]L}|                                D ]5}t          |t          j        j        j	                  rt          d          6Mt          |          dk    rt          d          t	          d |D                       rt          d          i |}fd	t          ||           D             }                    |          }t          j        | |
          \  } }d | D             fdt          j        |          }fd|D             t          |           dk    }	d fdt          |           t                    z   D             }
t          |           t                    z   D ]5}|D ]0}|j        |
v  |          k    rt          d|j         d          16d | D             d D             }|                              rt          d          |	rx|k    rt          d          fdt          | |          D             }d |D             }d |D             } t          d | D                       dk    sJ d | D             n0t          |           dk    sJ | d         fdfdD             |	r/t!          fdt#          d                   D                       }nHd | D             t!          fd t%          t          d                             D                       }t          |          dk    r t          |          dk    r|n|d         |d!<   t          |t&                    rt)          |          }n't          |t          j        j                  s|fd"} ||i |}t          |t           t          f          s|f}t          |          t                    k    r/t          dt                     d#t          |                     fd$t          ||          D             }t          j        |||
          }||fS )%N	Expected z input tensor(s), got r   z'All output expressions must be the samezConcatenation not allowedzOnly one output tensor allowedc              3   T   K   | ]#}t           j                            |          V  $d S N)einxtracer	is_scalarr   tensors     r   	<genexpr>z(vmap_with_axis_stage3.<locals>.<genexpr>   s2      
B
BV4;  ((
B
B
B
B
B
Br   z.At least one input tensor must be a non-scalarc                 d    g | ],\  }}t           j                            ||j                   -S )r   )r%   r&   call_factoryshape)r   r)   exprr   s      r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>"   sF       FD 	  W EE  r   r,   c                 &    h | ]}|D ]	}|j         
S r   name)r   r/   axiss      r   	<setcomp>z(vmap_with_axis_stage3.<locals>.<setcomp>*   s'    EEE4EETYEEEEr   c                 ^    t          | t          j        j        j                  o| j        vS r$   
isinstancer%   r/   stage3Axisr2   )r/   in_axis_namess    r   is_broadcast_axisz0vmap_with_axis_stage3.<locals>.is_broadcast_axis,   s'    $	 0 566Y49M;YYr   c                 Z    g | ]'}t           j        j                            |          (S r   )r%   r/   r8   remove)r   r/   r;   s     r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>0   s<     ( ( (=A	&788( ( (r   c                 L    t           j        j                            |            S r$   )r%   r/   r8   	is_marked)r/   s    r   
is_vmappedz)vmap_with_axis_stage3.<locals>.is_vmapped7   s    9#--d3333r   c                 >    h | ]}|D ]} |          |j         S r   r1   )r   rootvr@   s      r   r4   z(vmap_with_axis_stage3.<locals>.<setcomp>:   sP         :a==		   r   zAxis z( appears both as vmapped and non-vmappedc                     h | ]h}|                                 D ]Q}t          |t          j        j        j                  &t          j        j                            |          J|j        RiS r   allr7   r%   r/   r8   r9   r?   r2   r   expr_inr3   s      r   r4   z(vmap_with_axis_stage3.<locals>.<setcomp>E   s       KKMM  dDI,122	 8<y7G7Q7QRV7W7W		   r   c                     h | ]h}|                                 D ]Q}t          |t          j        j        j                  &t          j        j                            |          J|j        RiS r   rE   )r   expr_outr3   s      r   r4   z(vmap_with_axis_stage3.<locals>.<setcomp>K   s       LLNN  dDI,122	 8<y7G7Q7QRV7W7W		   r   z8Marked output axes must be a subset of marked input axeszMWhen using multiple input tensors the same axes must be marked in all tensorsc           	          g | ]G\  }}t           j                            |          r||fnt          j        ||d          d          HS )r   F)	broadcastr   )r%   r&   r'   r   transpose_broadcast)r   rH   	tensor_inr    exprs_out_flat_without_broadcasts      r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>Z   sw     
 
 
 # {$$Y//Y  )03  
 
 
r   c                     g | ]
}|d          S r   r   r   r   s     r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>f   s    &&&qad&&&r   c                     g | ]
}|d          S )r   r   rR   s     r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>g   s    $$$QAaD$$$r   c                 R    h | ]$}t          |          d k    t          |          %S rQ   )lenr   r/   s     r   r4   z(vmap_with_axis_stage3.<locals>.<setcomp>h   s)    DDD$c$ii!mmCIImmmr   c                     h | ]h}|                                 D ]Q}t          |t          j        j        j                  &t          j        j                            |          J|j        RiS r   rE   rG   s      r   r4   z(vmap_with_axis_stage3.<locals>.<setcomp>i   s     
 
 

 
 $	 0 566	
 <@9;K;U;UVZ;[;[	
I
 
 
 
r   r   c                    d |                                  D             }g }                                 D ]t}t          |t          j        j        j                  rN|j        |v rEt          |j        t          j        j        j                  r|j        }|	                    |           ut          j        j        j
                            |          S )Nc                 d    h | ]-}t          |t          j        j        j                  &|j        .S r   r6   )r   r3   s     r   r4   z>vmap_with_axis_stage3.<locals>.to_op_output.<locals>.<setcomp>u   sB       dDI$4$9::	  r   )rF   r7   r%   r/   r8   r9   r2   parentMarkerappendListmaybe)expr_out_flat_wb
axis_namesnew_axesr3   rH   s       r   to_op_outputz+vmap_with_axis_stage3.<locals>.to_op_outputt   s     ,0022  J
 H * *dDI$4$9:: *tyJ?V?V!$+ty/?/FGG +#{OOD)))9#(..x888r   c                 &    g | ]} |          S r   r   )r   r_   rb   s     r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>   s1     
 
 
/?LL)**
 
 
r   c              3   4   K   | ]\  }}|j         v |V  d S r$   r1   )r   ir3   marked_input_axess      r   r*   z(vmap_with_axis_stage3.<locals>.<genexpr>   sA       
 
4y--- ----
 
r   c                 ,    g | ]}t          |          S r   )listrV   s     r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>   s    333$4::333r   c              3   V   K   | ]"t          fd D                       V  #dS )c              3   6   K   | ]}|         j         v V  d S r$   r1   )r   axes_inre   rf   s     r   r*   z2vmap_with_axis_stage3.<locals>.<genexpr>.<genexpr>   s/      OOG71:?&77OOOOOOr   N)any)r   re   rk   rf   s    @r   r*   z(vmap_with_axis_stage3.<locals>.<genexpr>   s^       
 
OOOOOwOOOOO

 
 
 
 
 
r   r3   c            	          t           j                            | |t                    dk    rd D             n)t           j                            d         j                            S )Nr   c                 V    g | ]&}t           j                            |j                  'S r   )r%   r&   Tensorr.   rV   s     r   r   z;vmap_with_axis_stage3.<locals>.<lambda>.<locals>.<listcomp>   s*    OOOtDK&&tz22OOOr   r   )argsr   output)r%   r&   applyrU   ro   r.   )rp   r   concrete_opexprs_op_outputs     r   r   z'vmap_with_axis_stage3.<locals>.<lambda>   sl    T[%6%6?##a'' POOOOO##OA$6$<== &7 &
 &
 r   z output tensor(s), got c                 T    g | ]$\  }}}t          j        |||           d         %S )r,   r   )r   rM   )r   rH   
tensor_outrJ   r   s       r   r   z)vmap_with_axis_stage3.<locals>.<listcomp>   sI       )GZ 	 *hPPPQRS  r   )rU   
ValueErrorsetrh   rF   r7   r%   r/   r8   Concatenationzipall_to_tensorr   flattenr2   
differencetuple	enumeraterangestrgetattrr&   Tracer	unflatten)r   r   r   r   r   r   rB   r/   exprs_out_flattranspose_firstvmapped_axis_namesrC   marked_output_axesr   axis_indicestensors_outrk   rs   rH   rt   rO   r:   r;   r@   rf   rb   s        `          @@@@@@@@@@r   vmap_with_axis_stage3r      s    ~
8}}J''[S]][[#j//[[\\\
3y>>aBCCCXi0 > >HHJJ 	> 	>D$	 0 >?? > !<===>	> 9~~9:::

B
Bz
B
B
BBB KIJJJZF   
H55  J &&z22J  <*gNNNHjEE8EEEMZ Z Z Z Z \),,N( ( ( (ES( ( ($ (mma'O4 4 4   NNT*J%K%KK   X&F!G!GG [ [ 	[ 	[A,,A>> !Y!Y!Y!YZZZ ?	[    8   $$%677 USTTT 0
 222_  
 
 
 
 
 '*(J&?&?
 
 
 '&A&&&
$$!$$$DD(DDDEEJJJJ
 
#
 
 
 ;8}}!!!!1+	9 	9 	9 	9 	9
 
 
 
Cc
 
 

  
 
 
 
 
$%Ea%HII
 
 
 
 
 43(333 
 
 
 
 
3wqz??++
 
 
 
 

 <1),\):):Q)>)>LQROv "c 
Wb!!DK.// 	

 
 
 
 
 "j+F++KkE4=11 %"n
;3?@@@@&<== & &{##& &
 
 	
   -0+~-^-^  K .iQXYYYK	!!r   T)csec                   t           j        j                            | |          \  } }t           j        j                            |           }t          |          dk    rDt           j        j                            |d         |d         	                                g          }t          |d                   t          |          k    r5t          dt          |d                    dt          |                     t           j                            d t          |d         |          D             d |d         D             z   d |                                D             z   |d	          d t          |d                   t          |d                   z            }|d t          |d                            |t          |d                   d          }}||fS )
Nr   r   r"   z input tensors, but got c                 T    g | ]%\  }}t           j                            ||          &S r   r%   r/   Equation)r   rH   tensor_shapes      r   r   zparse.<locals>.<listcomp>   s>     	
 	
 	
% Iw55	
 	
 	
r   c                 L    g | ]!}t           j                            |          "S r   r   )r   rJ   s     r   r   zparse.<locals>.<listcomp>   s(    
>
>
>H49h''
>
>
>r   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS ).N)depth1depth2)r%   r/   r   npasarraynewaxis)r   krC   s      r   r   zparse.<locals>.<listcomp>   sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   F)r   
cse_concat)r%   r   r   !_clean_description_and_parametersr/   stage1parse_oprU   Op__deepcopy__rw   solverz   items)descriptionr   tensor_shapes
parametersr   exprsr   r   s           r   parser      s   "glLLZ K 
		"	";	/	/B 2ww!||Y  qEqE  "
  
 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 )Nr,   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   )r   r   tensorsr   r   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   r   r   r   r   r   r   r   r   r   c                    t          | gd |D             R d|i|\  }}t          ||||||          \  }}t          |          dk    r|d         n|S )a  Applies a function to the marked axes of the input tensors by passing the ``axis``
    argument and relying on implicit broadcasting rules.

    The function ``op`` must accept input tensors and an ``axis`` argument specifying the
    indices of the axes along which the operation is applied. When the function is applied on
    scalars, the ``axis`` argument is not passed. For multiple input tensors, the function
    must follow
    `Numpy broadcasting rules <https://numpy.org/doc/stable/user/basics.broadcasting.html>`_.

    Args:
        description: Description string for the operation in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        op: Backend operation. Is called with ``op(tensor, axis=...)``. If ``op`` is a string,
            retrieves the attribute of ``backend`` with the same name.
        kwargs: Additional keyword arguments that are passed to ``op``.
        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 operation if ``graph=False``, otherwise the graph
        representation of the operation.

    Examples:
        Reverse order of elements along an axis:

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

        Roll elements along two axes:

        >>> x = np.random.uniform(size=(16, 20))
        >>> einx.vmap_with_axis(
        ...     "a ([b c]) -> a ([b c])",
        ...     x,
        ...     op=partial(np.roll, shift=(2, 2)),
        ...     b=2,
        ... ).shape
        (16, 20)

        Compute sum along axis:

        >>> x = np.random.uniform(size=(16, 20))
        >>> einx.vmap_with_axis("a ([b] c) -> c a", x, op=np.sum, b=2).shape
        (16, 20)
    c                 L    g | ]!}t           j                            |          "S r   )r%   r&   	get_shaper(   s     r   r   z"vmap_with_axis.<locals>.<listcomp>!  s(    KKKt{,,V44KKKr   r   )r   r   r   r   r   )r   r   rU   )	r   r   r   r   r   r   r   r   r   s	            r   vmap_with_axisr      s    B  KK7KKK  QTXb Hi /'9FG  GY Y1,,71::'9r   r)   c                 &    t          | |fd||d|S )zASpecialization of :func:`einx.vmap_with_axis` with ``op="flip"``.r   r   r   r   r   r   r)   r   r   r   s        r   r   r   ,  s&     +va&'saaV`aaar   shiftc                 ,    t          | |fd|d|i|d|S )zjSpecialization of :func:`einx.vmap_with_axis` with ``op="roll"`` and
    ``kwargs={"shift": shift}``.
    r
   r   )r   r   r   r   r   )r   r)   r   r   r   r   s         r   r
   r
   8  sC          r   c                 &    t          | |fd||d|S )zCSpecialization of :func:`einx.vmap_with_axis` with ``op="softmax"``softmaxr   r   r   s        r   r   r   O  s'     +vd)WRUddYcdddr   c                 &    t          | |fd||d|S )zGSpecialization of :func:`einx.vmap_with_axis` with ``op="log_softmax"``log_softmaxr   r   r   s        r   r   r   [  s4     V -wC KU  r   )NN)NT) r%    r   numpyr   	functoolsr   typingr   r   r   r	   numpy.typingnpt	_op_namesjitr   	lru_cacher   traceback_utilfilterr   ro   Backendbool	ArrayLiker   r   intr
   r   r   r   r   r   <module>r      s                    2 2 2 2 2 2 2 2 2 2 2 2      V	 
   
h" h" h" 
h"V +/          F 	    /3A: A: A:A:kA: 	A: 4<d*+	A:
 
A: A: -A: A: A:  A:H    /3	b bbKb 4<d*+b 
	b
 -b b b b 
 /3 K eCj! 4<d*+	
 
 -   ,  /3	e eeKe 4<d*+e 
	e
 -e e e e  /3	
 

K
 4<d*+
 
	

 -
 
 
 
 
 
r   