+
    Pj4                     V   ^ RI t ^ RIt^ RIHt ^ RIHt ^ RIt^ RIt^ RI	t	^ RI
t	^ RIHt ^ RIHtHtHt ^ RIHtHt Rt]P*                  P-                  4       tR tR	 tR
 t]! RRR7      t]P8                  ! . RO4      tR tR tR t R t!R t"R t#R t$R t%. ]! RRR7      O]! RRR7      Ot&]& U u. uF  q PN                  R8w  g   K  V NK  	  up t&]PP                  PS                  R]&4      R 4       t*]PP                  PS                  R]&4      R 4       t+R t,R t-R t.Rt/R t0R# u up i )     N)testmod)Path)AbstractBackend)_optimize_transformationparse_shape	rearrange)collect_test_backendsis_backend_testedzAlex Rogozhnikovc                      \        \        P                  R \        \        R7      R7       \        \        P                  R \        \        R7      R7       R# )T)np)raise_on_error
extraglobsN)r   einopslayersdictr        p/Users/ahmed/devFolder/Ultron/claude-voice/gateway/.venv/lib/python3.14/site-packages/einops/tests/test_other.pytest_doctests_examplesr      s*    FMM$42;GFMM$42;Gr   c                 6   ^RI Hp  \        V ! 4       4      p. p. p\        P                  ! 4       pV'       d:   VP                  4       pWEP	                  4       ,          pVP                  V4       KA  V F9  pVP                  V9  d   K   V! 4        VP                  VP                  4       K;  	  \        V4      ^ 8X  g   Q V4       h\        V4      ^ 8X  g   Q RV: R24       hR#   \         d)   pTP                  TP                  T34        Rp?K  Rp?ii ; i)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_testNz%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0DDc                     \        R 4       \        ^4       U u. uF  p ^.V ,          NK  	  pp T\        ^4       U u. uF  p ^.V ,          NK  	  up ,          pV. RO.,          pV. RO.,          pV EF  p\        ^4       EF  p\        V4      p \        P	                  ^ R	VR7      P                  R
.4      pVR,          p\        P	                  ^ V ^,           4      p\        \        P                  V 4      RV 4      p\        P                  W,
          4      p\        P	                  ^ RRR7      p	\        WWW4      ;w  rrp\        P                  ! W4      '       g   Q hVP                  V4      P                  VR7      P                  V4      P                  R
.4      pVP                  V
4      P                  VR7      P                  V4      P                  R
.4      p\        P                  ! VV4      '       g   Q h\        V!  p\        VV4       F%  w  pp\        P                  ! VV4      '       d   K%  Q h	  EK  	  EK  	  R# u up i u up i )zTesting optimizations)size:NNNNi   iM  )axis)            )r,   r-   r.   r/         i   )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_numpyrR   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
   I
IFsymbolicr   c            	      ,   \          F  p \        R V P                  4       \        \        R4      p\        V P                  \        4      R4      pYu;8X  d   \        ^
^^^(R7      8X  g   Q h Q hV\        ^^^^(R7      u;8w  d   V8w  d   K  Q h Q h	  R# )zShape parsing for a b c d)rP   rQ   cdN)_IMPERATIVE_BACKENDSr3   r   r   x_np
from_numpyr   r%   parsed1parsed2s      r   test_parse_shape_imperativer_   ]   s    '"G$:$:;dI.g006	BATB"b%AAAAAA$bB"5@@@@@@ (r   c                      \          FM  p \        \        R 4      p\        V P                  \        4      R 4      pYu;8X  d   \	        4       8X  d   KJ  Q h Q h	  R# )z_ _ _ _NrY   r   rZ   r[   r   r\   s      r   test_underscorerb   f   sH    'dI.g006	B+TV+++++ (r   c                      \          FO  p \        \        R 4      p\        V P                  \        4      R 4      pYu;8X  d   \	        ^(R7      8X  d   KL  Q h Q h	  R# )z_ _ _ hello)helloNra   r\   s      r   test_underscore_onere   m   sJ    'dM2g006F3T^33333 (r   c                      \          FP  p \        \        R 4      p\        V P                  \        4      R 4      pYu;8X  d   \	        ^^(R7      8X  d   KM  Q h Q h	  R# )z_ _ a1 a1a111a)a1a1a111aNra   r\   s      r   test_underscore_severalri   t   sO    'd$45g0068HI<TR%<<<<<< (r   c                     \         P                  ! \        P                  4      ;_uu_ 4        \	        \
        R 4       RRR4       \         FW  p \         P                  ! \        P                  4      ;_uu_ 4        \	        V P                  \
        4      R 4       RRR4       KY  	  R#   + '       g   i     Ls; i  + '       g   i     K  ; i)za a b bN)pytestraisesr   EinopsErrorr   rZ   rY   r[   r%   s    r   test_repeatingro   {   sy    	v))	*	*D)$ 
+ (]]6--..**40)< /. ( 
+	* /..s   B); B<)B9	<Cc                  6   \          EF  p ^
^.R\        4       3^
.R\        ^
R7      3^
^.R\        ^R7      3. ROR\        ^R7      3. R	OR\        ^(R7      3^
.R\        ^
R7      3^
^.R\        ^
R7      3. ROR\        ^
R7      3. R	OR\        ^
R7      3. R	OR\        ^
^(R7      3^
^(.R\        ^
^(R7      33 FU  w  rp\        P                  ! V4      p\	        WB4      p\	        V P                  V4      V4      pYVu;8X  d   V8X  d   KR  Q h Q h	  EK  	  R# )

   ...... arP   a ... a ... brP   rQ   Nrq         rq   ry   rz   (   rY   r   r   onesr   r[   r%   r@   patternexpectedrB   r]   r^   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            	      6   \          F  p . ROR\        ^R7      3. ROR\        ^
R7      3. ROR\        ^
R7      33 FU  w  rp\        P                  ! V4      p\	        WB4      p\	        V P                  V4      V4      pYVu;8X  d   V8X  d   KR  Q h Q h	  K  	  R# )   z1 2 3 art   za 1 2za () 2Nr   r,   r-      )rq   r   r,   r}   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            	      R   \          F  p . ROR3. ROR3. ROR3. ROR3. ROR3. ROR33 Fa  w  r\        P                  ! \        4      ;_uu_ 4        \        P
                  ! V4      p\        V P                  V4      V4       RRR4       Kc  	  K  	  R#   + '       g   i     K|  ; i)r   za b cz2 a b cza b c ()z	a b c d eza b c d e ...Nr   )rY   rk   rl   RuntimeErrorr   r~   r   r[   )r%   r@   r   rB   s       r   test_failuresr      s    ' 7#9%:&;'?+:&
NE |,,GGENG..q17; -,
 ( -,,s   2B	B&Tztensorflow.kerasr%   c           
         . RO. R	O. R
O3 EF4  p\        RV P                  : RV: 24       V P                  V4      p\        VR4      p/ pVP	                  4        FI  w  rV\        V\        4      '       d   TM)V P                  Wb\        P                  ! . RO4      3.4      WE&   KK  	  \        V4       \        VR3/ \        VR4      BR^/B pV P                  Wr\        P                  ! . RO4      3.4      p\        VP                  4       VP                  R8X  g   Q h\        P                  ! V^ 4      '       d   EK5  Q h	  R# )rq   Nz-special shape parsing backend.framework_name=z input_shape=rV   z*a b (c1 c2) (d1 d2) -> (a b d1) c1 (c2 d2)za b c1 _d2r{   )rq   ry   NNNNNN)i  rz   r,   )r3   r   create_symbolr   items
isinstanceinteval_symbolr   zerosr   r@   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                    ^
^.RR.R\        4       3^
.R.R\        ^
R7      3^
^.RR.R\        ^R7      3. R	O. R
OR\        ^R7      3. RO. ROR\        ^(R7      3^
.R.R\        ^
R7      3^
^.RR.R\        ^
R7      3. R	O. R
OR\        ^
R7      3. RO. ROR\        ^
R7      3. RO. ROR\        ^
^(R7      3^
^(.RR.R\        ^
^(R7      33 F  w  rr4V P                  V4      p\        WS4      p/ pVP                  4        FK  w  r\	        V	\
        4      '       d   WV&   K!  V P                  W\        P                  ! V4      3.4      Wx&   KM  	  Wt8X  d   K  Q h	  R# )rq   Nrr   rs   rt   ru   rv   rw   z	 a ... b rx   )NNNr{   r   )	r   r   r   r   r   r   r   r   r   )
r%   static_shaper@   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                     \        R R R7      p V \        R RR7      ,          p V  Fe  pR F\  pRV9   p\        P                  ! . ROVR7      pVP                  V4      pVP	                  V4      V8X  d   KK  Q W!VP
                  34       h	  Kg  	  R# )FrS   Tfloat)dtypeN)int32int64float32float64)r-   r   r.   )r	   r   r   r[   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            	       aaaaa \        R4      '       g   \        P                  ! 4        ^ RIp ^ RIHp ^ RIHoHoHoH	oH
o ^ RIHp V! 4         ! VVVVV3R lRVP                  4      pV! 4       pV P                  VRR	7      pR Fx  pV P                  Wf^,           V^,           .4      pR FN  pV! Wx4      p	V! VP!                  4       V4      P#                  4       p
V P$                  P'                  WR
R
R7       KP  	  Kz  	  R# )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
torchNnn)einsumpackreducerepeatunpack)allow_ops_in_compiled_graphc                   X   <a a ] tR tRt oV3R lV 3R lltRVVVVV3R lltRtVtV ;t# )Ctest_torch_compile_for_functions.<locals>.TorchModuleWithOperationsi  c                   < V ^8  d   QhRR/# )r,   returnNr   )format__classdict__s   "r   __annotate__Ptest_torch_compile_for_functions.<locals>.TorchModuleWithOperations.__annotate__  s     	 	d 	r   c                $   < \         SV `  4        R # )N)super__init__)self	__class__s   &r   r   Ltest_torch_compile_for_functions.<locals>.TorchModuleWithOperations.__init__  s    Gr   c                  <a VP                   w  r4pV3R  lpS! W! R4      4      pS! Wv! R4      R4      pS! V.^\        S4      ,           ,          V! R4      4      w  rS! \        W! R4      4      V	R4      p
V
^ ,          \        V
4      ,           p\        W! R4      VR7      pS! WV! R	4      4      ^ ,          pW,           # )
c                    < V P                  4       pR P                  V Uu. uF  q"R,          R9  d   TMVS,           NK  	  up4      # u upi ) acdr2   )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minza b * cza b d c -> (a b ) 1 c dzab one1 c *z(a b ) 1 c -> a b c)rQ   za b c , a b c d -> d)r@   r    r   )r   x_abcr   rP   rQ   rW   r   x_abcdx_abdcpsx_arrayx1additionr   r   r   r   r   s   &&f          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   )r   )	__name__
__module____qualname____firstlineno__r   r   __static_attributes____classdictcell____classcell__)r   r   r   r   r   r   r   s   @@r   TorchModuleWithOperationsr     s     	 		! 	! 	!r   r   T	fullgraphgh㈵>)atolrtol)rq   ry   r|   )r   suf1other_suffix)r
   rk   skipr   r   r   r   r   r   r   r   einops._torch_specificr   Modulecompileranddoubler   testingassert_close)r   r   r   r   originalcompiledr*   rB   r   rM   rN   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                    \        R4      '       g   \        P                  ! 4        ^ RIp ^ RIHp ^ RIHpHpHp VP                  V! R^R7      V! RRR	^^^R
7      V! RR^R7      4      pV P                  VRR7      pR F>  pV P                  Ww.4      pV! V4      p	V! V4      p
V P                  W4      '       d   K>  Q h	  R# )z{
Einops layers are in general very friendly towards tracing/compiling,
but we still want to make sure we can compile them.
r   Nr   )EinMix	RearrangeReducezb (t c) -> b t c)rW   zb t c -> qkv b t coutz
qkv c coutzqkv cout)weight_shape
bias_shapeqkvrW   coutzqkv b t cout -> b t qkvr   )r   Tr   )       @   )r
   rk   r   r   r   einops.layers.torchr   r   r   
Sequentialr   r   r   )r   r   r   r   r   r   r   r*   rB   rM   rN   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                     \        R 4      '       g   \        P                  ! 4        \        R4      '       g   \        P                  ! 4        \        P                  ! 4       ;_uu_ 4       p \        V 4      P                  R4      pVP                  \        4       \        P                  ! RVP                  4       P                  4       .RR7       RRR4       R#   + '       g   i     R# ; i)r   numpyztest.pypythonT)checkN)r
   rk   r   tempfileTemporaryDirectoryr   joinpath
write_textsrc
subprocessrunabsoluteas_posix)rX   testfiles     r   test_einops_threadingr  a  s    W%%W%%		$	$	&	&!7##I.C ("3"3"5">">"@AN 
'	&	&	&s   *A&CC+	r{   )1r	  r  doctestr   pathlibr   r  r   rk   r   einops.layerseinops._backendsr   einops.einopsr   r   r   einops.testsr	   r
   
__author__randomdefault_rngr5   r   r(   rR   rY   r   rZ   r_   rb   re   ri   ro   r   r   r   _SYMBOLIC_BACKENDSr   markparametrizer   r   r   r   r   r  r  rn   s   0r   <module>r     sY           , J J A
iiHh<,@ -eEJ 	xx !A,4==2*
2< D7D6  .@p-?'CYCY]oCogg-?p  $67& 8&6 $67% 8%2[0Of08"
OI qs   2D&	D&