
    'i+                     &   d dl Z d dlmZmZ d dlZd dlZd dlmZm	Z	m
Z
 d dlmZ  G d d      Z edddd	d
      ddf edddd	d
      ddf edddd
      ddf edddd
ddd      ddf edddddd      ddfgZg d Zd! Zg d"Zd# Zd$ Zd% Zy)&    N)AnyCallable)EinopsError_compactify_pattern_for_einsumeinsum)collect_test_backendsc                   (    e Zd ZdedefdZdefdZy)	Argumentsargskargsc                      || _         || _        y Nr   kwargs)selfr   r   s      R/var/www/stems/demucs_env/lib/python3.12/site-packages/einops/tests/test_einsum.py__init__zArguments.__init__   s    	    functionc                 :     || j                   i | j                  S r   r   )r   r   s     r   __call__zArguments.__call__   s    2dkk22r   N)__name__
__module____qualname__r   r   r   r    r   r   r
   r
      s!    c C 3 3r   r
   zb c_in h w -> w c_out h bz
c_in c_out      )
bias_shapec_outc_in)   r         )r#   r   r"   r!   r   zb c_in h w -> w c_in h b )r   r    )r#   r   r"   r!   zb c_in h w -> b c_outzc_in h w c_outr"   r#      )r   r    hwr   r!   r%   zb t head c_in -> b t head c_outzhead c_in c_out   )r   headr    r   r!   r"   r#   r%   )r!   r"   r#   r)   ))zb c h w, b w -> b hzabcd,ad->ac)r+   r(   )r!   r#   )zb c h w, b w, b c -> b hzabcd,ad,ab->ac))r!   r"   (   r%   r(   r!   r"   )r!   r,   )z2... one two three, three four five -> ... two fivez...abc,cde->...be)    r%   r!   r"   r#   r#   r%   r)   r/   r%   r"   r)   )z2one two three ..., three four five -> two five ...zabc...,cde->be...))r!   r"   r#   r/   r%   r0   )r"   r)   r/   r%   )z6... one two three, ... three four five -> ... two fivez...abc,...cde->...be)r.   )r/   r%   r#   r%   r)   r1   )z*first_tensor second_tensor -> first_tensorzab->a))r%   r#   r%   )zi i -> zaa->))r%   r%   r   )z% one  two  ,  three four->two  four  z	ab,cd->bd)r-   )r#   r%   )r"   r%   )zi middle i -> middlezaba->b))r%   
   r%   r3   )zi ... i -> ...z
a...a->...)r%   r"   r!      r#   r%   )r"   r!   r6   r#   )zi ... i -> i ...za...a->a...r5   )r%   r"   r!   r6   r#   )zone one one -> onezaaa->a))r%   r%   r%   r2   )zi j k -> j i kzabc->bac))r6   r!   r"   )r!   r6   r"   )z
... -> ...z...->...)r%   r#   r"   r!   r6   r7   )z..., ..., ... -> ...z...,...,...->...)r"   r!   r8   r8   r8   )zindex ->za->r4   r   c                     t        dd      D ]  } t        j                  j                         }| j                  dv s0| j                         j                  }t        D ]  \  }}} ||      }t        d|j                  d| j                         |j                  |      j                  d      }| j                  |      } ||      }	| j                  |	      }
|
j                  |k(  rJ   y )	NTF)layerssymbolic)
tensorflowtorchoneflowpaddleRunningforsizefloat32)r   nprandomdefault_rngframework_namer:   EinMixtest_layer_casesprinteinsum_patternuniformastype
from_numpyto_numpyshape)backendrng
layer_typer   in_shape	out_shapelayerinputinput_frameworkoutput_frameworkoutputs              r   
test_layerr\      s    (uE 1ii##%!!%QQ )00J-= 1)h	Z(i!5!5ug>T>TU299)D")"4"4U";#(#9  ))*:;||y0001	1r   )
r<   r=   jaxnumpyr>   cupyztensorflow.kerasr?   pytensormlxc            	         t        d t                     } | D ]S  }t        D ]F  \  }}}}t        d| d|j                          t        |      }||k(  sJ t        j                  j                  d      }|D cg c]#  }|j                  |      j                  d      % }	}|	D 
cg c]  }
|j                  |
       }}
dD ]  }|r |j                  |g| }nt        g || }t        |j                        |k7  rt        d| d	|j                         t        j                  |g|	 }|j!                  |      }t        j"                  j%                  ||d
        I V y c c}w c c}
w )Nc                 &    | j                   t        v S r   rH   valid_backends_functionalxs    r   <lambda>z!test_functional.<locals>.<lambda>   s     0 04M M r   	Running 'z' for r   rB   rD   TFExpected output shape 	 but got r%   decimal)filterr   test_functional_casesrK   rH   r   rE   rF   RandomStaterM   rN   rO   r   tuplerQ   
ValueErrorrP   testingassert_array_almost_equal)backendsrR   einops_patterntrue_pattern	in_shapesrV   predicted_patternrstaterQ   	in_arraysarrayin_arrays_frameworkdo_manual_call	out_arraytrue_out_arraypredicted_out_arrays                   r   test_functionalr      s   MOdOfgH eBW 	e>NL)YIn-VG4J4J3KLM !?~ N$444 YY**1-FS\]%U3::9E]I]JS"T7#5#5e#<"T"T #0 e! ./@ WCV WI & L(; L^ LI )Y6$'=i[	R[RaRaQb%cdd "$<!D)!D&-&6&6y&A#

445H.bc4de	ee ^"Ts   ;(E0)E5c                     t        d t        dd            } | D ]^  }t        D ]Q  \  }}}}t        d| d|j                          t        |      }||k(  sJ t        j                  j                  d      }|D cg c]  }|j                  |       }	}|D cg c]#  }|j                  |      j                  d	      % }
}t        j                  |g|
 }d
D ]  }|r |j                  |g|	 }nt        g |	| }|j                  |t        t        |	|
                  }|j                   |k7  rt#        d| d|j                          t        j$                  j'                  ||d        T a y c c}w c c}w )Nc                 &    | j                   t        v S r   rd   rf   s    r   rh   z*test_functional_symbolic.<locals>.<lambda>   s    !""&?? r   TF)r;   r:   ri   z' for symbolic r   rB   rD   rj   rk   rl   r%   rm   )ro   r   rp   rK   rH   r   rE   rF   rq   create_symbolrM   rN   r   eval_symbollistziprQ   rs   rt   ru   )rv   rR   rw   rx   ry   rV   rz   r{   rU   in_symsin_dataexpected_out_datar   predicted_out_symbolpredicted_out_datas                  r   test_functional_symbolicr      s   ?AV`dmrAsH  gBW 	g>NL)YIn-_W=S=S<TUV >~ N$444YY**1-FGPQ8w,,X6QGQW`a8v~~8~4;;IFaGa "		, A A"/ g!+97>>:K+Vg+V(+1+K7+KN+K(%,%8%8(Wg./&" &++y8$'=i[	RdRjRjQk%lmm

445GIZde4fg	gg Ras   >E>(Fc            	      P   t         j                  j                  d      fd} t        j                  t
        d      5  t         | dd      d       d d d        t        j                  t
        d      5  t         | dd      d	       d d d        t        j                  t
        d      5  t         | d
d      d       d d d        t        j                  t
        d      5  t         | dd      d       d d d        t        j                  t        d      5  t         | dd      d       d d d        t        j                  t        d      5  t         | ddd      d       d d d        t        j                  t        d      5  t         | d      dj                  t        j                        dz          d d d        t        j                  t        d      5  t         | dd      d       d d d        t        j                  t        d      5  t        d | ddd             d d d        t        j                  t        d      5  t        d       d d d        t        j                  t        d      5  t         | dd             d d d        y # 1 sw Y   IxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   vxY w# 1 sw Y   -xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   c                  F    j                  |       j                  d      S )NrB   rD   )rM   rN   )rQ   r{   s    r   create_tensorz-test_functional_errors.<locals>.create_tensor  s    ~~5~)00;;r   z
^Singleton)matchr%   r6   z	i () -> iz^Shape rearrangementza b -> (a b)r3   z(a b) -> a bz^Anonymous axeszi 2 -> iz^Invalid axis identifierz	i 2j -> iz^Einsum patternr"   r!   zi j kz^Too many axes z	 extra ->z^Unknown axiszi j -> kz^The last argumentz
i j k -> ir#   z^`einops.einsum` takes)rE   rF   rq   pytestraisesNotImplementedErrorr   r   rs   RuntimeErrorjoinstringascii_letters)r   r{   s    @r   test_functional_errorsr   	  s    YY""1%F< 
*,	? 
!Q	

 
*2H	I 
!Q	

 
*2H	I 
"a 	

 
*2C	D 
!Q	

 
{*D	E 
!Q	

 
z):	; 
!Q"	

 
|+;	< 
!HHV))*[8	

 
|?	; 
!Q	

 
z)=	> 
!Q"	

 
z)A	B 
	

 
z)A	B 
!Q	

 
Y
 

 

 

 

 

 

 

 

 

 

 
s   J8J)/J6&KKK3K*"K7LL LJ&)J36K KKK'*K47LLLL%)r   typingr   r   r^   rE   r   einops.einopsr   r   r   einops.testsr   r
   rJ   rp   r\   re   r   r   r   r   r   r   <module>r      s         M M .3 3 	-|TV]_` 	-|WY`bc 	,bTK 	)+;SUYZ^_ghi 	35FSW^_fgopq+ >s l1  eFg@Y
r   