
    !Sh*                     L   d dl mZmZ d dlmZ d dlmZmZmZ d dl	Z
d dlZd dl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dS )&    )AnyCallable)collect_test_backends)_compactify_pattern_for_einsumeinsumEinopsErrorNc                   *    e Zd ZdedefdZdefdZdS )	Argumentsargskargsc                 "    || _         || _        d S Nr   kwargs)selfr   r   s      ^/var/www/html/movieo_spanner_bot/venv/lib/python3.11/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    x2dk222r   N)__name__
__module____qualname__r   r   r   r    r   r   r
   r
   	   sQ        c C    3 3 3 3 3 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 ]} | j        dv r|                                 j        }t          D ]\  }}} ||          }t          d|j        d| j                   t          j        	                    |          
                    d          }|                     |          } ||          }|                     |          }	|	j        |k    sJ d S )	NTF)layerssymbolic)
tensorflowtorchoneflowpaddleRunningforsizefloat32)r   framework_namer:   EinMixtest_layer_casesprinteinsum_patternnprandomuniformastype
from_numpyto_numpyshape)
backend
layer_typer   in_shape	out_shapelayerinputinput_frameworkoutput_frameworkoutputs
             r   
test_layerrZ      s    (uEEE 
1 
1!%QQQ ))0J-= 1 1)h	Z((i!5ug>TUUU	))x)88??	JJ")"4"4U";";#(5#9#9  ))*:;;|y00000
1 
1r   )	r<   r=   jaxnumpyr>   cupyztensorflow.kerasr?   pytensorc            	         t          d t                                } | D ]t          D ]\  }}}}t          d| dj                    t          |          }||k    sJ t          j                            d          fd|D             }fd|D             }dD ]}|r j	        |g|R  }	nt          g ||R  }	t          |	j                  |k    rt          d| d	|	j                   t          j	        |g|R  }
                    |	          }t          j                            ||
d
           !d S )Nc                     | j         t          v S r   rE   valid_backends_functionalxs    r   <lambda>z!test_functional.<locals>.<lambda>   s     04M M r   	Running 'z' for r   c                 b    g | ]+}                     |                               d          ,S rB   rD   rL   rM   ).0rP   rstates     r   
<listcomp>z#test_functional.<locals>.<listcomp>   s5    ]]]%U33::9EE]]]r   c                 :    g | ]}                     |          S r   )rN   )rj   arrayrQ   s     r   rl   z#test_functional.<locals>.<listcomp>   s'    "T"T"T7#5#5e#<#<"T"T"Tr   TFExpected output shape 	 but got r%   decimal)filterr   test_functional_casesrH   rE   r   rJ   rK   RandomStater   tuplerP   
ValueErrorrO   testingassert_array_almost_equal)backendseinops_patterntrue_pattern	in_shapesrT   predicted_pattern	in_arraysin_arrays_frameworkdo_manual_call	out_arraytrue_out_arraypredicted_out_arrayrQ   rk   s               @@r   test_functionalr      s   MMOdOfOfggH e eBW 	e 	e>NL)YLnLLG4JLLMMM !?~ N N$4444 Y**1--F]]]]S\]]]I"T"T"T"T)"T"T"T #0 e e! M ./@ WCV W W WII & L(; L^ L L LI ))Y66$%ci%c%cR[Ra%c%cddd "$<!D)!D!D!D&-&6&6y&A&A#
445H.bc4dddde	ee er   c                     t          d t          dd                    } | D ].t          D ]"\  }}}}t          d| dj                    t          |          }||k    sJ t          j                            d          fd|D             }fd	|D             }t          j	        |g|R  }d
D ]}	|	r j	        |g|R  }
nt          g ||R  }

                    |
t          t          ||                              }|j        |k    rt          d| d|j                   t          j                            ||d           $0d S )Nc                     | j         t          v S r   ra   rc   s    r   re   z*test_functional_symbolic.<locals>.<lambda>   s    !"&?? r   TF)r;   r:   rf   z' for symbolic r   c                 :    g | ]}                     |          S r   )create_symbol)rj   rS   rQ   s     r   rl   z,test_functional_symbolic.<locals>.<listcomp>   s'    QQQ8w,,X66QQQr   c                 b    g | ]+}                     |                               d          ,S rh   ri   )rj   rS   rk   s     r   rl   z,test_functional_symbolic.<locals>.<listcomp>   s5    aaa8v~~8~44;;IFFaaar   ro   rp   rq   r%   rr   )rt   r   ru   rH   rE   r   rJ   rK   rv   r   eval_symbollistziprP   rx   ry   rz   )r{   r|   r}   r~   rT   r   in_symsin_dataexpected_out_datar   predicted_out_symbolpredicted_out_datarQ   rk   s               @@r   test_functional_symbolicr      s   ??AV`dmrAsAsAs H  g gBW 	g 	g>NL)YUnUUW=SUUVVV >~ N N$4444Y**1--FQQQQyQQQGaaaaW`aaaG "	, A A A A"/ g g! L+97>:K+Vg+V+V+V((+1+K7+KN+K+K+K(%,%8%8(Wg..//& &" &+y88$%li%l%lRdRj%l%lmmm
445GIZde4ffffg	gg gr   c            	         t           j                            d          fd} t          j        t
          d          5  t           | dd          d           d d d            n# 1 swxY w Y   t          j        t
          d          5  t           | dd          d	           d d d            n# 1 swxY w Y   t          j        t
          d          5  t           | d
d          d           d d d            n# 1 swxY w Y   t          j        t
          d          5  t           | dd          d           d d d            n# 1 swxY w Y   t          j        t          d          5  t           | dd          d           d d d            n# 1 swxY w Y   t          j        t          d          5  t           | ddd          d           d d d            n# 1 swxY w Y   t          j        t          d          5  t           | d          d
                    t          j                  dz              d d d            n# 1 swxY w Y   t          j        t          d          5  t           | dd          d           d d d            n# 1 swxY w Y   t          j        t          d          5  t          d | ddd                     d d d            n# 1 swxY w Y   t          j        t          d          5  t          d           d d d            n# 1 swxY w Y   t          j        t          d          5  t           | dd                     d d d            d S # 1 swxY w Y   d S )Nr   c                  V                         |                               d          S )NrB   rD   ri   )rP   rk   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)rJ   rK   rv   pytestraisesNotImplementedErrorr   r   rx   RuntimeErrorjoinstringascii_letters)r   rk   s    @r   test_functional_errorsr     s    Y""1%%F< < < < < 
*,	?	?	? 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
*2H	I	I	I 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
*2H	I	I	I 
 
M"a  	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
*2C	D	D	D 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{*D	E	E	E 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z):	;	;	; 
 
M!Q""	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
|+;	<	<	< 
 
M!HHV)**[8	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
|?	;	;	; 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)=	>	>	> 
 
M!Q""	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)A	B	B	B 
 
	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)A	B	B	B 
 
M!Q	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   A((A,/A,B55B9<B9DD	D(EEE5FF #F G**G.1G.:III<J##J'*J'	K11K58K5L33L7:L7N  NN)typingr   r   einops.testsr   einops.einopsr   r   r   r\   rJ   r   r   r
   rG   ru   rZ   rb   r   r   r   r   r   r   <module>r      s                   . . . . . . M M M M M M M M M M      3 3 3 3 3 3 3 3 		-|TV]_``` 		-|WY`bccc 		,bTKKK 		)+;SUYZ^_ghiii 		35FSW^_fgopqqq+ >s s s l1 1 1
 
 
  e  e  eFg g g@Y
 Y
 Y
 Y
 Y
r   