
    Pi<                     l   d dl Z d dlZddlmZ d dlZd dlmZmZm	Z	 d dl
mZ  e j        d           ddddddd	            Ze j        d
dd            Ze j        j         e j        d           ddd
i ddede j        dededee j        edf         dede	dej        fd                        ZdS )    N   )util)CallableUnionMappingc                       fdS )Nc                 2     | fd|D             |fi |S )Nc                 &    g | ]} |          S  r   .0xts     `/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/vmap.py
<listcomp>z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>   s!    ,,,A11Q44,,,    r   )exprs_in
tensors_in	exprs_outkwargscr   s       r   <lambda>z<lambda>.<locals>.<lambda>
   s@    ,,,,,,,iJ J;AJ J r   r   r   r   s   ``r   r   r   
             r   )traceF)flatbackendopr   verbosec                    !" i t                     t          |          k    r/t          dt                      dt          |                     t                     t          |          z   D ]L}|                                D ]5}	t	          |	t
          j        j        j                  rt          d          6Mfdt          |           D             }
                    |          }rCt          d           t          dd  D                        t          dd	 |D                        t          j         |
          \  }
}t          j        |          }t          d |
D                       sJ t          d |D                       sJ rCt          d           t          dd |
D                        t          dd |D                        d  D             d |D             d |
D             d |D              ryt          d           s4t          dd D                        t          dd D                        t          dd D                        t          dd  D                        t          j        d D                       |d  f	d
            }d |
D             }d  |
D             fd!!d" }g }t          |
          D ]5}|D ]0" |"          r#"j        |vr|                    "j                   16rt          d#|            t          |
          t          |          z   D ]@}|D ];""j        |v p
 !"           |"          k    rt          d$"j         d%          <A!fd&|D             }d' |D             }r:t          d(d) |D                        t          d*|           t          d+|           g }t#          d, |
D                       }t#          d- |D                       }t%          |          D ]@"t#          "fd.|D                       }t#          "fd/|D                       }rt          d0" d1| d2|            t          ||          D ]\  }}|t          d3" d4| d5          |                    ||||f           d6 t#          fd7t          ||          D                       }t#          fd8t          ||          D                       }||z   D ]4}"|v r|                    "           "|v r|                    "           5rt          d9| d:|            Bt%          |          D ]!\  }}}}                    |||||;          }"rt          d<d= |D                        t
          j                            ||t#          d> |D                       ?          }r,t          ||          D ]\  }}	t          d@|j        |	           fdAt          |||          D             }rSt          dB           t          ||          D ]3\  }}	t          dCt
          j                            |          |	           4t          j        |||
          }rt          dDdE |D                        ||fS )FN	Expected z input tensor(s), got zConcatenation not allowedc                 d    g | ],\  }}t           j                            ||j                   -S )r   )einxtracercall_factoryshape)r   tensorexprr   s      r   r   zvmap_stage3.<locals>.<listcomp>#   sF       FD 	  W EE  r   zExpressions:z    IN:c                 ,    g | ]}t          |          S r   strr   es     r   r   zvmap_stage3.<locals>.<listcomp>+   s    333Q#a&&333r   z    OUT:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>,   s    555a3q66555r   r#   c              3   ^   K   | ](}t           j        j                            |          V  )d S Nr$   r)   stage3is_flatr   r)   s     r   	<genexpr>zvmap_stage3.<locals>.<genexpr>1   s5      HH$ty''--HHHHHHr   c              3   ^   K   | ](}t           j        j                            |          V  )d S r1   r2   r5   s     r   r6   zvmap_stage3.<locals>.<genexpr>2   s5      II$ty''--IIIIIIr   zFlat expressions:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>6   s    888Q#a&&888r   c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>7   s    :::a3q66:::r   c                 V    g | ]&}t           j        j                            |          'S r   r$   r)   r3   
get_markedr5   s     r   r   zvmap_stage3.<locals>.<listcomp>:   s+    PPPt)44T::PPPr   c                 V    g | ]&}t           j        j                            |          'S r   r;   r5   s     r   r   zvmap_stage3.<locals>.<listcomp>;   s+    RRR$)*55d;;RRRr   c                 V    g | ]&}t           j        j                            |          'S r   r;   r5   s     r   r   zvmap_stage3.<locals>.<listcomp><   s+    ZZZDdi.99$??ZZZr   c                 V    g | ]&}t           j        j                            |          'S r   r;   r5   s     r   r   zvmap_stage3.<locals>.<listcomp>=   s+    \\\Tty/::4@@\\\r   zExpressions used in op:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>B   s    @@@c!ff@@@r   c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>C   s    BBB!s1vvBBBr   z    IN_FLAT:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>D   s    FFF!s1vvFFFr   z    OUT_FLAT:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>E   s    HHH1AHHHr   c                 V    g | ]&}t           j                            |j                  'S r   r$   r%   Tensorr'   r5   s     r   r   zvmap_stage3.<locals>.<listcomp>G   s*    XXXdk((44XXXr   )args)r   c                   		 r4t          dd |D                        t          dd |D                        t          |          t                    k    sJ sWt          j        |
          }rt          dd |D                        t          |          t                    k    sJ n|}sn	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          |                     t          d |D                       rt          dd |D                        r;t          d           t          	|          D ]\  }}t          d||j                   t!          t          	|                    D ]j\  }\  }}t          j                            |          |j        k    r:t          d|j         d| dt          j                            |                     ks`t          j        |
          \  }}rt          dd |D                        |k    sJ d |D              d            d D               rt          dd |D                        t          |          S )Nz&Flat input tensors that arrived in op:c                 f    g | ].}t          t          j                            |                    /S r   r,   r$   r%   	get_shaper   as     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>L   s0    HHH1T[**1--..HHHr   z Input types to vmapped function:c                 ,    g | ]}t          |          S r   typer   r   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>N   s    6X6X6X1tAww6X6X6Xr   r#   z Unflattened input tensors in op:c                 f    g | ].}t          t          j                            |                    /S r   rJ   rL   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>X   s0    GGGqS..q1122GGGr   c            	          t           j                            | |t                    dk    rd D             n)t           j                            d         j                            S )Nr   c                 V    g | ]&}t           j                            |j                  'S r   rE   r5   s     r   r   z=vmap_stage3.<locals>.op.<locals>.<lambda>.<locals>.<listcomp>g   s*    VVV4**4:66VVVr   r   )rG   r   output)r$   r%   applylenrF   r'   )rG   r   concrete_opexprs_out_expecteds     r   r   z)vmap_stage3.<locals>.op.<locals>.<lambda>c   so    ):):)**Q.. WVCUVVVV[''(:1(=(CDD *; * * r   r!   z1 output tensor(s) from vmapped function, but got c              3   V   K   | ]$}t          |t          j        j                   V  %d S r1   )
isinstancer$   r%   rF   rQ   s     r   r6   z*vmap_stage3.<locals>.op.<locals>.<genexpr>u   s4      JJ:a!3444JJJJJJr   z0Expected tensors from vmapped function, but got c                 ,    g | ]}t          |          S r   rO   rQ   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>x   s    CaCaCaPQDGGCaCaCar   z!Unflattened output tensors in op:    zExpected output shape z from z5-th (zero-based) output of vmapped function, but got zFlattened output tensors in op:c                 f    g | ].}t          t          j                            |                    /S r   rJ   rL   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>   s0    HHHqS..q1122HHHr   c                 ,    g | ]}t          |          S r   r+   r   ss     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>   s    <<<a3q66<<<r   z != c                 ,    g | ]}t          |          S r   r+   r`   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>   s    8881A888r   z&Returning types from vmapped function:c                 ,    g | ]}t          |          S r   rO   rQ   s     r   r   z+vmap_stage3.<locals>.op.<locals>.<listcomp>   s    <Z<Z<ZT!WW<Z<Z<Zr   )printrW   r   	unflattenr[   r,   getattrr$   r%   Tracertuplelist
ValueErroranyzipr'   	enumeraterK   flatten)r   tensors_in_flatr   tensors_outexpr_out
tensor_outiexprs_out_funcargs_flat2rX   rY   r   r   exprs_in_funcargsexprs_in_funcargs_flatexprs_out_funcargsexprs_out_funcargs_flatr   r   r   s           @@r   r   zvmap_stage3.<locals>.opG   s    	Z8HHHHH   46X6X6X6X6XYYY?##s+A'B'BBBBB 	)&9JT[  J  6GGJGGG   z??c(mm33333(J7;X//AXb# 	"%%BBB 233 		K    B b*///+t}55 	)&.K{s#566668C 233 8 8%(%5%58 8   JJkJJJJJ 	cCaCaU`CaCaCacc    	:5666(+,>(L(L : :$*fh
(89999)237I;3W3W)X)X 	 	%A%*{$$Z00HNBB _X^ _ _1 _ _;?;;P;PQ[;\;\_ _   C  	;48L"K5 5 51$k  5HHKHHH  
 ),CCCC<<#;<<<BBB DCC88 7888:: 	\:<Z<Zk<Z<Z<Z[[[[!!!r   c                 &    g | ]}d  |D             S )c                     g | ]	}|j         
S r   namerL   s     r   r   z*vmap_stage3.<locals>.<listcomp>.<listcomp>   s    +++af+++r   r   r   roots     r   r   zvmap_stage3.<locals>.<listcomp>   s'    FFF++d+++FFFr   c                 &    h | ]}|D ]	}|j         
S r   r{   )r   r~   rM   s      r   	<setcomp>zvmap_stage3.<locals>.<setcomp>   s'    HHHD4HHaHHHHr   c                     t          | t          j        j        j                  o-| j        vo$t          j        j                            |            S r1   )r[   r$   r)   r3   Axisr|   	is_marked)r)   axes_names_in_sets    r   is_broadcast_axisz&vmap_stage3.<locals>.is_broadcast_axis   sK    tTY-233 5	!225I$..t444	
r   c                 L    t           j        j                            |            S r1   )r$   r)   r3   r   )r)   s    r   
is_vmappedzvmap_stage3.<locals>.is_vmapped   s    9#--d3333r   zVmapping the following axes: zAxis z( appears both as vmapped and non-vmappedc                 Z    g | ]'}t           j        j                            |          (S r   )r$   r)   r3   remove)r   r)   r   s     r   r   zvmap_stage3.<locals>.<listcomp>   s<     ( ( (=A	&788( ( (r   c                 &    g | ]}d  |D             S )c                     g | ]	}|j         
S r   r{   rL   s     r   r   z*vmap_stage3.<locals>.<listcomp>.<listcomp>   s    Ar   r   r}   s     r   r   zvmap_stage3.<locals>.<listcomp>   s4     ( ( (#'( ( (r   z*Flat output expressions without broadcast:c                 ,    g | ]}t          |          S r   r+   r-   s     r   r   zvmap_stage3.<locals>.<listcomp>   s    >>>SVV>>>r   zGot input axis names:z:Got output axis names (excluding broadcasted output axes):c              3   $   K   | ]}|j         V  d S r1   r'   r5   s     r   r6   zvmap_stage3.<locals>.<genexpr>   s$      >>>>>>>>r   c              3   $   K   | ]}|j         V  d S r1   r   r5   s     r   r6   zvmap_stage3.<locals>.<genexpr>   s$      RR$*RRRRRRr   c              3   N   K   | ]}|v r|                               nd V   d S r1   indexr   
axes_namesvs     r   r6   zvmap_stage3.<locals>.<genexpr>   sO       
 
AK1
??JQ
 
 
 
 
 
r   c              3   N   K   | ]}|v r|                               nd V   d S r1   r   r   s     r   r6   zvmap_stage3.<locals>.<genexpr>   sR       
 
 $%
??JQ
 
 
 
 
 
r   zApplying backend.vmap to axis z, with input axis indices z and output axis indices z<All vmapped axes must appear in the output expression, but 'z' does not appear in ''c                 :    || S | d |         | |dz   d          z   S )Nr   r   )r'   axiss     r   	drop_axiszvmap_stage3.<locals>.drop_axis   s,    |UdU|eD1HJJ&777r   c              3   6   K   | ]\  }} ||          V  d S r1   r   r   r'   r   r   s      r   r6   zvmap_stage3.<locals>.<genexpr>   s3      bbtYYud33bbbbbbr   c              3   6   K   | ]\  }} ||          V  d S r1   r   r   s      r   r6   zvmap_stage3.<locals>.<genexpr>   sD       
 
'2udIIeT""
 
 
 
 
 
r   zNow has remaining input axes z and output axes )in_axesout_axesinput_shapesoutput_shapesz 
Sending shapes to backend.vmap:c                 6    g | ]}t          |j                  S r   )r,   r'   rL   s     r   r   zvmap_stage3.<locals>.<listcomp>  s     3U3U3UQCLL3U3U3Ur   c              3   ^   K   | ](}t           j                            |j                  V  )d S r1   rE   r5   s     r   r6   zvmap_stage3.<locals>.<genexpr>  s4      aaT[''
33aaaaaar   )rG   rU   zGot overall flat tensor_out:c                 T    g | ]$\  }}}t          j        |||           d         %S )r#   r   )r   transpose_broadcast)r   expr_out_wbr(   rq   r   s       r   r   zvmap_stage3.<locals>.<listcomp>  sI       )K 	 fhPPPQRS  r   z/Got overall transposed+broadcasted tensors_out:r]   z$Got overall unflattened tensors_out:c                 f    g | ].}t          t          j                            |                    /S r   rJ   rL   s     r   r   zvmap_stage3.<locals>.<listcomp>  s1    4d4d4dWXS9N9Nq9Q9Q5R5R4d4d4dr   )rW   rj   ri   allr[   r$   r)   r3   Concatenationrl   all_to_tensorrd   r   rn   r   r|   appendrh   reversedr   vmapr%   rV   r'   rK   re   )#r   r   r   r   r   r   r   r   r~   r)   exprs_in_flatexprs_out_flataxes_names_inr   vmapped_axes exprs_out_flat_without_broadcast axes_names_out_without_broadcastvmapsr   r   r   r   out_axisrq   r   tensorsr(   r   r   ru   rv   rw   rx   r   r   s#   `  `` ``                   @@@@@@@@r   vmap_stage3r   	   s.
     ~
8}}J''[S]][[#j//[[\\\Xi0 > >HHJJ 	> 	>D$	 0 >?? > !<===>	>
   
H55  J &&z22J 7ni33(333444j559555666 !%Xz7 S S SM:\),,NHH-HHHHHHHHII.IIIIIIII <!"""i88-888999j::>:::;;; QPxPPPRR	RRRZZMZZZ\\^\\\ J'((( 	D)@@.?@@@AAA*BB/ABBBCCCnFF/EFFFGGGoHH0GHHHIII	ZXXAWXXXYYY " P" P" P" P" P" P" P" P" P" P" P" P" P" P" ZYP"d GFFFFMHHMHHH
 
 
 
 
4 4 4 L]## , , 	, 	,Az!}} ,|!;!;##AF+++	,  ><l<<===]##d>&:&:: [ [ 	[ 	[A,&>*;*;A*>*>::a==PP !Y!Y!Y!YZZZ Q	[
( ( ( (ES( ( ($( (+K( ( ($  	
8>>=>>>	
 	
 	
 	%}555H,	
 	
 	

 E>>>>>>>LRR1QRRRRRMl## * * 
 
 
 
O\
 
 
 
 
  
 
 
 
>
 
 
 
 
  	@ @ @@ @5=@ @   #&h"?"? 	 	Hh AA A5=A A A     	gx}EFFF	8 	8 	8 bbbbs<Y`GaGabbbbb 
 
 
 
69-6R6R
 
 
 
 
 (*JJ 	; 	;JJ!!!$$$444077::: 	B B B?B B  
 ;C5// 
 
6<\\%'  
 
  W13U3U*3U3U3UVVVk
aa@`aaaaa    G
  F)IJJ 	F 	FLFD0&,EEEE   -0,g~.
 .
  G  ??@@@88 	? 	?LFD&$+//77>>>> n^WiQQQG 
24d4d\c4d4d4d	
 	
 	
 I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   expr_intensor_shapes      r   r   zparse.<locals>.<listcomp>4  s>     	
 	
 	
% Iw55	
 	
 	
r   c                 L    g | ]!}t           j                            |          "S r   r   )r   rq   s     r   r   zparse.<locals>.<listcomp>8  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   kr   s      r   r   zparse.<locals>.<listcomp>9  sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   F)r   
cse_concat)r$   r   r   !_clean_description_and_parametersr)   stage1parse_oprW   Op__deepcopy__rj   solverl   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                       fdS )Nc                 4     | gfd|D             R i |S )Nc                 &    g | ]} |          S r   r   r   s     r   r   z.<lambda>.<locals>.<lambda>.<locals>.<listcomp>H  s!    ---qqtt---r   r   )r   r   r   r   r   s      r   r   z<lambda>.<locals>.<lambda>G  sJ    qq@----W---@ @ @17@ @ r   r   r   s   ``r   r   r   G  r   r   )r   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 )aY	  Vectorizes and applies a function to the input tensors using automatic vectorization.

    The function ``op`` must accept input tensors and yield output tensors as specified in
    ``description`` with shapes matching the subexpressions that are marked with ``[]``-brackets.

    Args:
        description: Description string for the operation in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        op: Function that will be vectorized. If ``op`` is a string, retrieves the attribute
            of ``backend`` with the same name.
        flat: Whether to pass the tensors to ``op`` in flattened form or matching the nested
            layout in the input expressions. Defaults to False.
        kwargs: Additional keyword arguments that are passed to ``op``. Defaults to ``{}``.
        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 vectorized operation if `graph=False`, otherwise the graph
        representation of the operation.

    Examples:
        Compute the mean along rows of a matrix:

        >>> x = np.random.uniform(size=(10, 8))
        >>> einx.vmap("a [b] -> a", x, op=np.mean)
        (10,)

        Vectorize a custom function:

        >>> x, y = (
        ...     np.random.uniform(size=(10, 13, 4)),
        ...     np.random.uniform(
        ...         size=(
        ...             4,
        ...             9,
        ...         )
        ...     ),
        ... )
        >>> def op(x, y): # c, d -> 2
        >>>     return np.stack([np.mean(x), np.max(y)])
        >>> einx.vmap("b1 [c] b2, b2 [d] -> b2 [2] b1", x, y, op=op).shape
        (4, 2, 10)

        Compute a matrix-matrix multiplication

        >>> x, y = (
        ...     np.random.uniform(size=(5, 10)),
        ...     np.random.uniform(size=(10, 3)),
        ... )
        >>> einx.vmap("a [b], [b] c -> a c", x, y, op=np.dot).shape
        (5, 3)
    c                 L    g | ]!}t           j                            |          "S r   )r$   r%   rK   )r   r(   s     r   r   zvmap.<locals>.<listcomp>  s(    KKKt{,,V44KKKr   r   )r   r   r   r   r   r   )r   r   rW   )
r   r   r   r   r   r   r   r   r   r   s
             r   r   r   E  s    T  KK7KKK  QTXb Hi %'94RPV  GY Y1,,71::'9r   )r$   	functools r   numpyr   typingr   r   r   numpy.typingnptjitr   	lru_cacher   traceback_utilfilterr,   rF   boolBackend	ArrayLiker   r   r   r   <module>r      s                  + + + + + + + + + +       
    
P P P P 
Pf +/          F 	    .2J: J: J:J:kJ: 	J: 	J:
 4<d*+J: 
J: J: -J: J: J:  J: J: J:r   