
    3j4                     B   S SK r S SKrS SKJr  S SKJr  S SKrS SKrS SK	r	S SK
r	S SKJr  S SKJrJrJr  S SKJrJr  Sr\R*                  R-                  5       rS rS	 rS
 r\" SSS9r\R8                  " / SQ5      rS rS rS r S r!S r"S r#S r$S r%/ \" SSS9Q\" SSS9Qr&\& V s/ s H  o RN                  S:w  d  M  U PM     sn r&\RP                  RS                  S\&5      S 5       r*\RP                  RS                  S\&5      S 5       r+S r,S r-S r.Sr/S r0gs  sn f )     N)testmod)Path)AbstractBackend)_optimize_transformationparse_shape	rearrange)collect_test_backendsis_backend_testedzAlex Rogozhnikovc                      [        [        R                  S[        [        S9S9  [        [        R                  S[        [        S9S9  g )NT)np)raise_on_error
extraglobs)r   einopslayersdictr        Q/home/wildlama/miniconda3/lib/python3.13/site-packages/einops/tests/test_other.pytest_doctests_examplesr      s*    FMM$42;GFMM$42;Gr   c                  0   SSK Jn   [        U " 5       5      n/ n/ n[        R                  " 5       nU(       a<  UR                  5       nXER	                  5       -  nUR                  U5        U(       a  M<  U H8  nUR                  U;  a  M   U" 5         UR                  UR                  5        M:     [        U5      S:X  d   U5       e[        U5      S:X  d   SU< S35       eg! [         a(  nUR                  UR                  U45         SnAM  SnAff = f)z
This test will fail if some of backends are not installed or can't be imported
Other tests will just work and only test installed backends.
   )parse_backends_to_testNr   z%did not instantiate backends_to_test=z, they won't be tested) r   setr   __subclasses__popappendframework_nameremove	Exceptionlen)r   backends_to_testerrorsbackend_subclassesbackendsbackendbackend_typees           r   test_backends_installedr)      s
   
 )134F--/H
,,.**,,!!'* (
 +&&.>>	<N##L$?$?@ + v;!#V# A%g)O>N=PPf'gg%  	<MM<66:;;	<s   "C##
D-DDc                  J   [        S5        [        S5       V s/ s H	  n S/U -  PM     nn U[        S5       V s/ s H	  n S/U -  PM     sn -  nU/ SQ/-  nU/ SQ/-  nU GH  n[        S5       GH  n[        U5      n [        R	                  S	S
US9R                  S/5      nUS S  n[        R	                  S	U S-   5      n[        [        R                  U 5      S U 5      n[        R                  X-
  5      n[        R	                  S	SSS9n	[        XWX5      =u  ppn[        R                  " X5      (       d   eUR                  U5      R                  US9R                  U5      R                  S/5      nUR                  U
5      R                  US9R                  U5      R                  S/5      n[        R                  " UU5      (       d   e[        U6 n[        UU5       H$  u  nn[        R                  " UU5      (       a  M$   e   GM     GM     g s  sn f s  sn f )NzTesting optimizations            )r,   r.         )r,   r.   r/   r0         r/   r   i   )sizer   i   iM  )axis)printranger!   rngintegersreshapetuplepermutationr   r   array_equalsum	transposezip)n_dimensionsshapesshape_attemptx
init_shape	n_reducedreduced_axesaxes_reorderingfinal_shapeinit_shape2reduced_axes2axes_reordering2final_shape2combination2result1result2combination3abs                       r   #test_optimize_transformations_numpyrU   8   s    	
!"5:2Y?Y\qcL YF?
eAh?hls\!h??F
|nF
#$$FaHu:LQE2::B4@AqJQq(89I !>z	!JKL!ool.FGO,,q$S,9KXp/Y FK(8 >>+<<<<ii
+//\/BLL_]eegifjkGii,00m0DNNO_`hhjlimnG>>'73333 4\BLL,71~~a++++ 8) !  @?s
   HH Fsymbolicr   
         (   c            	         [          H  n [        SU R                  5        [        [        S5      n[        U R                  [        5      S5      nXs=:X  a  [        SSSSS9:X  d   e   eU[        SSSSS9s=:w  a	  U:w  a  M|   e   e   g )	NzShape parsing for a b c drY   rZ   r[   r\   )rS   rT   cdr   )_IMPERATIVE_BACKENDSr6   r   r   x_np
from_numpyr   r&   parsed1parsed2s      r   test_parse_shape_imperativerg   ]   s    '"G$:$:;dI.g006	BATB"b%AAAAAA$bB"5@@@@@@ (r   c                      [          HK  n [        [        S5      n[        U R                  [        5      S5      nXs=:X  a  [	        5       :X  a  MH   e   e   g )Nz_ _ _ _ra   r   rb   rc   r   rd   s      r   test_underscorerj   f   sH    'dI.g006	B+TV+++++ (r   c                      [          HJ  n [        [        S5      n[        U R                  [        5      S5      nXs=:X  a  [	        SS9:X  a  MG   e   e   g )Nz_ _ _ hellor\   )hellori   rd   s      r   test_underscore_onerm   m   sJ    'dM2g006F3T^33333 (r   c                      [          HK  n [        [        S5      n[        U R                  [        5      S5      nXs=:X  a  [	        SSS9:X  a  MH   e   e   g )Nz_ _ a1 a1a111ar[   r\   )a1a1a111ari   rd   s      r   test_underscore_severalrq   t   sO    'd$45g0068HI<TR%<<<<<< (r   c                  v   [         R                  " [        R                  5         [	        [
        S5        S S S 5        [         HO  n [         R                  " [        R                  5         [	        U R                  [
        5      S5        S S S 5        MQ     g ! , (       d  f       Nh= f! , (       d  f       Mt  = f)Nza a b b)pytestraisesr   EinopsErrorr   rb   ra   rc   r&   s    r   test_repeatingrw   {   sp    	v))	*D)$ 
+ (]]6--.**40)< /. ( 
+	* /.s   B+ B)
B&)
B8	c                     [          H  n SS/S[        5       4S/S[        SS94SS/S[        SS94/ SQS[        SS94/ SQS[        S	S94S/S
[        SS94SS/S
[        SS94/ SQS
[        SS94/ SQS
[        SS94/ SQS[        SS	S94SS	/S[        SS	S944 HS  u  pn[        R                  " U5      n[	        XB5      n[	        U R                  U5      U5      nXVs=:X  a	  U:X  a  MP   e   e   M     g )NrY   rZ   ...... arS   rY   rZ   r[   r[   rX   r\   a ... a ... brS   rT   ra   r   r   onesr   rc   r&   rC   patternexpectedrE   re   rf   s          r   test_ellipsisr      s   '"Xudf%T7D2J'"Xwr
+7D2J/wr
3T7D2J'"Xwr
+7D2J/wr
3z4"+;<"Xz4"#34)
$EH A!!-G!'"4"4Q"7AG111111!)
 (r   c            	         [          H  n / SQS[        SS94/ SQS[        SS94/ SQS[        SS944 HS  u  pn[        R                  " U5      n[	        XB5      n[	        U R                  U5      U5      nXVs=:X  a	  U:X  a  MP   e   e   M     g )	Nr   r,   r.      z1 2 3 ar   r{   )rY   r   r,   za 1 2rY   za () 2r   r   s          r   test_parse_with_anonymous_axesr      s    '9dQi0$*-4":.)
$EH
 A!!-G!'"4"4Q"7AG111111)
 (r   c                  <   [          H  n / SQS4/ SQS4/ SQS4/ SQS4/ SQS4/ SQS44 HY  u  p[        R                  " [        5         [        R
                  " U5      n[        U R                  U5      U5        S S S 5        M[     M     g ! , (       d  f       Mq  = f)Nr   za b cz2 a b cza b c ()z	a b c d eza b c d e ...)ra   rs   rt   RuntimeErrorr   r   r   rc   )r&   rC   r   rE   s       r   test_failuresr      s    ' 7#9%:&;'?+:&
NE |,GGENG..q17; -,
 ( -,s   	2B
BTztensorflow.kerasr&   c           
         / SQ/ SQ/ SQ4 GH1  n[        SU R                  < SU< 35        U R                  U5      n[        US5      n0 nUR	                  5        HH  u  pV[        U[        5      (       a  UO)U R                  Xb[        R                  " / SQ5      4/5      XE'   MJ     [        U5        [        US40 [        US5      DS	S
0D6nU R                  Xr[        R                  " / SQ5      4/5      n[        UR                  5        UR                  S:X  d   e[        R                  " US5      (       a  GM2   e   g )NrX   )rY   rZ   NNNNNNz-special shape parsing backend.framework_name=z input_shape=r^   z*a b (c1 c2) (d1 d2) -> (a b d1) c1 (c2 d2)za b c1 _d2r,   )i  r[   r,   r   )r6   r   create_symbolr   items
isinstanceinteval_symbolr   zerosr   rC   allclose)	r&   input_shapeinput_symbolshape_placeholder	out_shapenamesymbolresult_placeholderresults	            r   test_parse_shape_symbolicr      s9    	 
 	>w'='=&?~OP,,[9'i@	-335LD fc** ((JZA[2\1]^ O 6 	i&F
JUVbdnJo
tu
 $$%7RbIc:d9effll||8888{{61%%%%/r   c                    SS/S S /S[        5       4S/S /S[        SS94SS/S S /S[        SS94/ SQ/ SQS[        SS94/ S	Q/ S
QS[        SS94S/S /S[        SS94SS/S S /S[        SS94/ SQ/ SQS[        SS94/ S	Q/ S
QS[        SS94/ S	Q/ S
QS[        SSS94SS/S S /S[        SSS944 H  u  pp4U R                  U5      n[        XS5      n0 nUR                  5        HJ  u  p[	        U	[
        5      (       a  XU'   M   U R                  X[        R                  " U5      4/5      Xx'   ML     Xt:X  a  M   e   g )NrY   rZ   ry   rz   r{   r|   )NNNr[   rX   r   r\   r}   r~   r   z	 a ... b )	r   r   r   r   r   r   r   r   r   )
r&   static_shaperC   r   r   r   r   r   r   r   s
             r   "test_parse_shape_symbolic_ellipsisr      ss    bD$</
vwr
+
bD$<$*5	)7D2J?	3WdRjI
vwr
+
bD$<$*5	)7D2J?	3WdRjI	3ZbAQR
bD$<dR2.>?3.W ,,U3'>	-335LD&#&&"($")"5"5fbhhWcNd?e>f"g		 6
 $$$+3r   c                      [        SSS9n U [        SSS9-  n U  Ha  nS HX  nSU;   n[        R                  " / SQUS9nUR                  U5      nUR	                  U5      U:X  a  MG   X!UR
                  45       e   Mc     g )NFrV   T)int32int64float32float64float)r.   r   r/   )dtype)r	   r   r   rc   is_float_typer   )r%   r&   r   is_floatinputs        r   test_is_float_typer      s    $eEBH%uTBBH=E%'HHHYe4E&&u-E((/8;Zeekk=ZZ;	 > r   c            	        ^^^^^ [        S5      (       d  [        R                  " 5         SSKn SSKJn  SSKJmJmJmJ	mJ
m  SSKJn  U" 5          " UUUUU4S jSUR                  5      nU" 5       nU R                  US	S
9nS Hm  nU R                  XfS-   US-   /5      nS HK  nU" Xx5      n	U" UR!                  5       U5      R#                  5       n
U R$                  R'                  XSSS9  MM     Mo     g)a  
Test ensures that allow_ops_in_compiled_graph allows compiling in a single graph
Additionally we ensure that after compilation cache works properly
 (by changing shapes and patterns)
We additionally check that pack/unpack still can be handled
 despite variable number of inputs/outputs
torchr   Nnn)einsumpackreducerepeatunpack)allow_ops_in_compiled_graphc                   F   >^  \ rS rSrSU 4S jjrSUUUUU4S jjrSrU =r$ )Ctest_torch_compile_for_functions.<locals>.TorchModuleWithOperationsi  c                 "   > [         TU ]  5         g )N)super__init__)self	__class__s    r   r   Ltest_torch_compile_for_functions.<locals>.TorchModuleWithOperations.__init__  s    Gr   c                 L  >^ UR                   u  p4nU4S jnT" X" S5      5      nT" Xv" S5      S5      nT" U/S[        T5      -   -  U" S5      5      u  pT" [        X" S5      5      U	S5      n
U
S	   [        U
5      -   n[        X" S
5      US9nT" XU" S5      5      S	   nX-   $ )Nc                    > U R                  5       nSR                  U Vs/ s H  o"S   S;  a  UOUT-   PM     sn5      $ s  snf )N r4   acd)splitjoin)r   partspsuffixs      r   sufXtest_torch_compile_for_functions.<locals>.TorchModuleWithOperations.forward.<locals>.suf  sD    xxRW XRWQe5&8a&j!HRW XYY Xs   Aza b c -> a b c 4za b c d -> a b cminr,   za b * cza b d c -> (a b ) 1 c dzab one1 c *r   z(a b ) 1 c -> a b c)rT   za b c , a b c d -> d)rC   r!   r   )r   x_abcr   rS   rT   r_   r   x_abcdx_abdcpsx_arrayx1additionr   r   r   r   r   s     `          r   forwardKtest_torch_compile_for_functions.<locals>.TorchModuleWithOperations.forward  s    kkGA!Z E3'9#:;F63'9#:EBEugS[93y>JJFYvs3L/MNPRTabGc'l*B2s#89Q?BeS1G-HI!LH= r   r   )returnN)r   )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__)r   r   r   r   r   r   s   @r   TorchModuleWithOperationsr     s    		! 	! 	!r   r   T	fullgraph)rY   rZ   r\   r   r,   )r   suf1other_suffixgh㈵>)atolrtol)r
   rs   skipr   r   r   r   r   r   r   r   einops._torch_specificr   Modulecompileranddoubler   testingassert_close)r   r   r   r   originalcompiledr3   rE   r   rP   rQ   r   r   r   r   r   s              @@@@@r    test_torch_compile_for_functionsr     s     W%%;;B!! !BII !, )*H}}X}6HJJQhq122Fq)Gqxxz6288:GMM&&wd&N	 3 r   c                  j   [        S5      (       d  [        R                  " 5         SSKn SSKJn  SSKJnJnJn  UR                  U" SSS9U" S	S
SSSSS9U" SSSS95      nU R                  USS9nS H=  nU R                  Xw/5      nU" U5      n	U" U5      n
U R                  X5      (       a  M=   e   g)z{
Einops layers are in general very friendly towards tracing/compiling,
but we still want to make sure we can compile them.
r   r   Nr   )EinMix	RearrangeReducezb (t c) -> b t c   )r_   zb t c -> qkv b t coutz
qkv c coutzqkv coutr.      )weight_shape
bias_shapeqkvr_   coutzqkv b t cout -> b t qkvr   )r   Tr   )r       @   )r
   rs   r   r   r   einops.layers.torchr   r   r   
Sequentialr   r   r   )r   r   r   r   r   r   r   r3   rE   rP   rQ   s              r   test_torch_compile_for_layersr   4  s    
 W%%==}}$+&\j^_celmn(%a8H }}X}6HJJ|$1+1+~~g////	 r   aS  
import einops
import numpy as np
from concurrent.futures import ThreadPoolExecutor
import torch

def f():
    return einops.rearrange(np.ndarray((20, 150, 150)), "... i j -> ... j i")
with ThreadPoolExecutor(max_workers=2) as ex:
    fs = []
    for i in range(20):
        fs.append(ex.submit(f))
    for fut in fs:
        fut.result()
c                     [        S5      (       d  [        R                  " 5         [        S5      (       d  [        R                  " 5         [        R                  " 5        n [        U 5      R                  S5      nUR                  [        5        [        R                  " SUR                  5       R                  5       /SS9  S S S 5        g ! , (       d  f       g = f)Nr   numpyztest.pypythonT)check)r
   rs   r   tempfileTemporaryDirectoryr   joinpath
write_textsrc
subprocessrunabsoluteas_posix)r`   testfiles     r   test_einops_threadingr  a  s    W%%W%%		$	$	&!7##I.C ("3"3"5">">"@AN 
'	&	&s    A#C
C)1r  r  doctestr   pathlibr   r   r   rs   r   einops.layerseinops._backendsr   einops.einopsr   r   r   einops.testsr	   r
   
__author__randomdefault_rngr8   r   r)   rU   ra   r   rb   rg   rj   rm   rq   rw   r   r   r   _SYMBOLIC_BACKENDSr   markparametrizer   r   r   r   r   r  r  rv   s   0r   <module>r     sW           , J J A
iiHh<,@ -eEJ 	xx !A,4==2*
2< D7D6  .@p-?'CYCY]oCog-?p  $67& 8&6 $67% 8%2[0Of08"
OI qs   )D D