+
    PjH                        ^ RI t ^ RIHt ^ RIt^ RIt^ RIHtHtH	t	 ^ RI
Ht ^ RI
HtHt Rt]! R. RO4      t]! R]! ^R	7      RRR R!R". R#O.4      ]! R
]! ^^R7      R. R$O4      ]! R]! ^
R7      R%. R&O4      .tR tR t. ]O]! R]! ^
R7      RR R%.4      N]! R]! ^^^R7      RR'.4      N]! R]! ^
R7      RR R(.4      NtR tR tR)R ltR tR tR tR tR tR tR# )*    N)
namedtuple)EinopsError	rearrangereduce)FLOAT_REDUCTIONS)collect_test_backendsis_backend_testedzAlex Rogozhnikovtestcaseb c h w -> b (c h w)cz(b c (h1 h2) (w1 w2) -> b (c h2 w2) h1 w1h2w2zb ... c -> c b ...)bc                  l   \        R RR7       EF  p \        RV P                  4       \         EF  w  rr4\        P
                  ! \        P                  ! V4      RR7      P                  V4      p\        WQ3/ VB pV P                  4       P                  ! V3/ VB pV F:  p V! V P                  \        P                  ! VRR7      4      4       \        R4      h	  \        P                   ! \        P"                  ! V4      4      p	V P%                  V! V P                  V4      4      4      p
V P%                  V	! V P                  V4      4      4      p\        P&                  ! Wj4      '       g   Q h\        P&                  ! W4      '       g   Q hV P                  4       P)                  RRR	7      pV P                  V4      pV! V! V4      4      pVP+                  4        \        P&                  ! V P%                  VP,                  4      ^4      '       d   EK  Q h	  EK  	  R
#   \         d     EK  i ; i)FTsymboliclayersTest layer for float32dtypeFailure expected...->sum	reductionN)r   printframework_namerearrangement_patternsnparangeprodreshaper   r   	Rearrange
from_numpyzerosAssertionErrorBaseExceptionpickleloadsdumpsto_numpyallcloseReducebackwardgrad)backendpatternaxes_lengthsinput_shapewrong_shapesxresult_numpylayershapelayer2result1result2just_sumvariableresults                  q/Users/ahmed/devFolder/Ultron/claude-voice/gateway/.venv/lib/python3.14/site-packages/einops/tests/test_layers.pytest_rearrange_imperativerC   %   s   (%EE!7!78@V@V<G;		"''+.i@HHUA$Q@<@LNN$..wG,GE%=',,RXXe9-MNO ));<< & \\&,,u"56F&&uW-?-?-B'CDG&&vg.@.@.C'DEG;;|5555;;w0000~~'..w%.HH))!,HeHo.FOO;;w//>BBBB3 AW F % s   #-H##H3	2H3	c                     \        R R R7       EF  p \        RV P                  4       \         EF  w  rr4\        P
                  ! \        P                  ! V4      RR7      P                  V4      p\        WQ3/ VB pV P                  4       P                  ! V3/ VB pR.\        V4      ,          pW8.p	V	 EF!  p
V P                  V
4      pW3.pV! V4      pV P                  W4      p\        P                  ! Wn4      '       g   Q h\        P                   ! \        P"                  ! V4      4      pV! V4      pV P                  VV4      p\        P                  ! VV4      '       g   Q hV P                  4       P%                  RRR7      pV P                  V! V4      V4      p\        P&                  ! V4      p\        P                  ! VV4      '       d   EK"  Q h	  EK  	  EK  	  R# )	Tr   r   r   r   Nr   r   r   )r   r   r    r!   r"   r#   r$   r%   r   r   r&   lencreate_symboleval_symbolr/   r+   r,   r-   r0   r   )r3   r4   r5   r6   _wrong_shapesr8   r9   r:   input_shape_of_nonesshapesr;   symboleval_inputsresult_symbol1r=   r<   result_symbol2r>   r?   result_sum1result_sum2s                        rB   test_rearrange_symbolicrQ   E   s   ($tDD!7!78AWAW=G;		"''+.i@HHUA$Q@<@LNN$..wG,GE$(6C,<#< !8F ..u5 &{m!&v!--nJ{{<9999fll5&9:!'!--nkJ{{7G4444 #>>+227e2L%11(>2JKX ffQi{{;<<<<'   BX E    zb c h w -> b ()z b c (h1 h2) (w1 w2) -> b c h1 w1)h1r   r   zb ... c -> bc                     \        R RR7       EF  p \        RV P                  4       \         EF  p\         EF  w  r#rE\        WW#WE4       \
        P                  ! ^^\
        P                  ! V4      ,           RR7      P                  V4      pWfP                  4       ,          p\        WbV3/ VB pV P                  4       P                  ! W!3/ VB pV F:  p	 V! V P                  \
        P                  ! V	RR7      4      4       \        R4      h	  \"        P$                  ! \"        P&                  ! V4      4      p
V P)                  V! V P                  V4      4      4      pV P)                  V
! V P                  V4      4      4      p\
        P*                  ! W{4      '       g   Q h\
        P*                  ! W4      '       g   Q hV P                  4       P                  RRR	7      pV P                  V4      pV! V! V4      4      pVP-                  4        V P)                  VP.                  4      pVR8X  d    \
        P*                  ! V^4      '       g   Q hVR
8X  d.   \
        P*                  ! VVP1                  4       4      '       g   Q hVR9   g   EK\  \
        P2                  ! \
        P4                  ! V^ ^.4      4      '       g   Q h\
        P6                  ! V4      R8  d   EK  Q h	  EK  	  EK  	  R#   \          d     EK1  i ; i)FTr   r   r   r   r   r   r   r   meang      ?N)maxmin)r   r   r    
REDUCTIONSreduction_patternsr"   r#   r$   r%   rU   r   r   r0   r'   r(   r)   r*   r+   r,   r-   r.   r/   r1   r2   rW   allisinr   )r3   r   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   r2   s                    rB   test_reduce_imperativer\   n   sl   (%EE!7!78#IDVDV@{g'[IIaRWW[%9!9KSST_`VVX%a)L|L(//SlS)EAg00%y1QRS --?@@ *  fll5&9:!**51C1CA1F+GH!**6'2D2DQ2G+HI{{<9999{{74444">>+227e2L"--a0!%/2!''6%;;tQ////&;;tTXXZ8888.66"''$A"7888866$<#---E EW $ F ) s   -K77LLc            
      J   \        R R R7       EF  p \        RV P                  4       \         EFk  p\         EF\  w  r#rE\
        P                  ! ^^\
        P                  ! V4      ,           RR7      P                  V4      pWfP                  4       ,          p\        WbV3/ VB pV P                  4       P                  ! W!3/ VB pR.\        V4      ,          p	WI.p
V
 F  pV P                  V4      pW3.pV! V4      pV P                  W4      p\
        P                   ! W4      '       g   Q h\"        P$                  ! \"        P&                  ! V4      4      pV! V4      pV P                  VV4      p\
        P                   ! VV4      '       d   K  Q h	  EK_  	  EKn  	  EK  	  R# )Tr   r   r   r   N)r   r   r    rX   rY   r"   r#   r$   r%   rU   r   r   r0   rE   rF   rG   r/   r+   r,   r-   )r3   r   r4   r5   r6   rH   r8   r9   r:   rI   rJ   r;   rK   rL   rM   r=   r<   rN   r>   s                      rB   test_reduce_symbolicr^      sb   ($tDD!7!78#IEWEWA{IIaRWW[%9!9KSST_`VVX%a)L|L(//SlS(,vK0@'@$%<#E$2259F$*;-K%*6]N%11.NG;;|====#\\&,,u*=>F%+F^N%11.+NG;;w8888 $ FX $ ErR   c                    \        R 4      '       g   \        P                  ! 4        R# ^ RIp^ RIHpHpHpHpH	p ^ RI
HpHp	Hp
 T! V! ^^RR7      V '       d   V
! RR^^R7      MV! ^R7      V! ^^RR7      V
! RR^^R7      V'       d"   VP                  P                  V	! R4      4      MV	! R4      V! R^x4      V! 4       V! ^x^T4      V! 4       V! R	R
R^T^TR7      V! RRR^T^TR7      V! ^T^
4      4      # )torchN)Conv2dLinear	MaxPool2dReLU
SequentialEinMixr&   r0   kernel_sizeb c (h h2) (w w2) -> b c h wrV   r   r   b c1 -> (b c2)c1 c2c2weight_shape
bias_shapec1rm   (b c2) -> b c3c2 c3c3ro   rp   rm   rt   )   rv   i  )r	   pytestskip	torch.jittorch.nnra   rb   rc   rd   re   einops.layers.torchrg   r&   r0   jitscript)
use_reduceadd_scripted_layerr`   ra   rb   rc   rd   re   rg   r&   r0   s   &&         rB   create_torch_modelr      s    W%%HHAA1aV,ISF15Q1EYbopYq1bf-15Q1E! IIY'=>?12:s#F3OF#'drVXY#'drVXY2rN
 	
rR   c                     \        R 4      '       g   \        P                  ! 4        R# ^ RIp ^ RIp \        RR7      p\        RR7      pV P                  . RO4      pV P                  V! V4      V! V4      4      '       d   Q hVP                  \        P                  ! \        P                  ! VP                  4       4      4      4       V P                  V! V4      V! V4      4      '       g   Q hV P                  P                  W#R7      pV P                  P!                  V! V4      V! V4      RRR7       V P                  P!                  V! V^,           4      V! V^,           4      RRR7       V P                  P                  W#R7      pV P                  P!                  V! V4      V! V4      RRR7       V P                  P!                  V! V^,           4      V! V^,           4      RRR7       R# )	r`   NT)r~   F)example_inputsMbP?atolrtol
          r   )r	   rw   rx   r`   ry   r   randnr/   load_state_dictr+   r,   r-   
state_dictr|   tracetestingassert_close)r`   model1model2inputmodel3model4s         rB   test_torch_layerr      su   W%% 	#t4#u5O,>>&-????v||FLL9J9J9L,MNO~~fUmVE];;;; >""6%=&-dQU"V""6%!)#4fUQY6GdY]"^>""6%=&-dQU"V""6%!)#4fUQY6GdY]"^rR   c                  <   \        R 4      '       g   \        P                  ! 4        R# ^ RIp R Fi  p\	        RVR7      pV P
                  P                  V4      pV P                  . RO4      pV P                  P                  V! V4      V! V4      RRR7       Kk  	  R# )r`   NT)r~   r   r   r   )FTr   )
r	   rw   rx   r`   r   r|   r}   r   r   r   )r`   script_layerr   r   r   s        rB   test_torch_layers_scriptingr      su    W%%)L'4LYFYY%%f-FKK0EMM&&ve}fUm$UY&Z *rR   c                  *  aaaaaaa \         P                  P                  4       p \        R 4      '       g   \        P
                  ! 4        R# ^ RIpVP                  R8  d   \        P
                  ! 4        ^ RIH	o ^ RIH
o ^ RIHo ^ RIHo ^ RIHoHoHoHp VVVVVVV3R lpV! 4       pV! 4       pV P'                  . ROR	7      P)                  R
4      p\         P*                  ! VP-                  V4      VP-                  V4      4      '       d   Q hRp\/        RV4       VP0                  P2                  P5                  WG4       VP0                  P2                  P7                  WrR7      p\         P8                  P;                  VP-                  V4      VP-                  V4      4       Rp	VP0                  P2                  P=                  VP?                  4       VR7      p
VPA                  V	4       V
PC                  V	4       VPC                  V	4       \         P8                  P;                  VP-                  V4      VP-                  V4      4       \         P8                  P;                  VP-                  V4      V
P-                  V4      4       R# )
tensorflowNz2.16.)Conv2D)Dense)rd   )re   )rg   r&   r0   keras_custom_objectsc                     < S! S ! ^^. ROR7      S! RR^^R7      S ! ^^R7      S! RR^^R7      S! R4      S! ^x4      S! 4       S! ^T4      S! 4       S! RRR	^T^TR
7      S! RRR^T^TR7      S! ^
4      .4      # )   )ri   r6   rj   rV   r   rh   r   rk   rl   rm   rn   rr   rs   rt   ru   )r   r   r    )ra   rg   rb   rd   r&   r0   re   s   rB   create_keras_model,test_keras_layer.<locals>.create_keras_model  s    1!E95Q1M21-95Q1M453KF2JF+'dWY^`a+'dWY^`a2J rR   )sizer   z/tmp/einops_tf_model.h5temp_path_keras1)custom_objectsz/tmp/einops_tf_model.weights.h5)r   r   r   r   )"r"   randomdefault_rngr	   rw   rx   r   __version__tensorflow.keras.layersr   r   rd   tensorflow.keras.modelsre   einops.layers.kerasrg   r&   r0   r   normalastyper/   predict_on_batchr   kerasmodels
save_model
load_modelr   assert_allclosemodel_from_jsonto_jsonsave_weightsload_weights)rngtfr   r   r   r   r   tmp_model_filenamer   weight_filenamer   ra   rg   rb   rd   r&   r0   re   s              @@@@@@@rB   test_keras_layerr      s   
))


!C\**>>G#KKM<;06WW	 	$ $%#%


077	B;;v66u=v?V?VW\?]^^^^ 7 "45
""6>++,>+d


""6#:#:5#A6CZCZ[`Cab;001ARf0gO,O,O,


""6#:#:5#A6CZCZ[`Cab 	

""6#:#:5#A6CZCZ[`CabrR   c                   aaaaaaa \        R4      '       g   \        P                  ! 4        R# ^ RIp ^ RIp^ RIHo ^ RIHo ^ RIH	oH
oHo  ! VVVV3R lRSP                  4      pV! 4       oSP                  . R
O4      oSP                  VP                  P!                  ^ 4      S4      pVVV3R lpVP#                  V4      pV! V4      pV! V4      w  rxSP%                  Wg4      '       g   Q hVP&                  R8  d   VP(                  p	MVP*                  P,                  p	V	! R	 W84      p
V! V
4      pWk8  g	   Q Wk34       hV P.                  P1                  V4      pV P.                  P3                  W<4      pR# )zp
One-off simple tests for Flax layers.
Unfortunately, Flax layers have a different interface from other layers.
jaxN)linenrf   c                   R   <a  ] tR tRt o  ZP                  VVV3R l4       tRtV tR# )test_flax_layers.<locals>.NNiC  c                   < S! R RR\        ^^^^R7      R7      ! V4      pS! R\        ^R7      R7      ! V4      pS! RR\        ^R	7      4      ! V4      pV# )
z b (h h2) (w w2) c -> b h w c_outzh2 w2 c c_outc_out)r   r   r   r   )sizeszb h w c -> b (w h c)r   z
b hwc -> brU   )hwc)dict)selfr8   rg   r&   r0   s   &&rB   __call__%test_flax_layers.<locals>.NN.__call__D  sc    6X\`afgkltuXv 4D1IFqI<)1DEaHrR   r   N)__name__
__module____qualname____firstlineno__compactr   __static_attributes____classdictcell__)__classdict__rg   r&   r0   nns   @rB   NNr   C  s     ZZ rR   r   c                 Z   < SP                   P                  SP                  V S4      4      # )N)linalgnormapply)paramsfixed_inputjnpmodels   &rB   eval_at_point'test_flax_layers.<locals>.eval_at_pointQ  s!    ::??5;;v{#CDDrR   z0.6.0c                      WR ,          ,
          # )r   r   )x1x2s   &&rB   <lambda>"test_flax_layers.<locals>.<lambda>]  s
    "Ez/rR   )r      	   r   )r	   rw   rx   flaxr   	jax.numpynumpyr   einops.layers.flaxrg   r&   r0   Moduleonesinitr   PRNGKeyvalue_and_gradr/   r   tree_maptreemapserializationto_bytes
from_bytes)r   r   r   r   r   vandgvalue0value1grad1r   params2value2fbytes_loadedrg   r&   r0   r   r   r   r   s                 @@@@@@@rB   test_flax_layersr   4  s4   
 U##$@@	 	 	 hh45CJJ..q1;?	E ""=1v&f||F++++??W$||Hxx||H96Iw'1&!11 ##,,V4$$//?rR   c                    ^ RI Hp  V ! RR^^^R7      pVP                  e   Q hVP                  e   Q hVP                  R8X  g   Q hVP
                  . R%O8X  g   Q hVP                  e   Q hV ! RRR^^^^^R7      pVP                  e   Q hVP                  e   Q hVP                  R8X  g   Q hVP
                  . R&O8X  g   Q hVP                  . R'O8X  g   Q hV ! R	R
R
R7      pVP                  e   Q hVP                  e   Q hVP                  R8X  g   Q hVP
                  . 8X  g   Q hVP                  . 8X  g   Q hV ! RR^^^R7      pVP                  e   Q hVP                  e   Q hVP                  R8X  g   Q hVP
                  . R(O8X  g   Q hVP                  e   Q hV ! RR^^^R7      pVP                  R8X  g   Q hVP                  \        ^^R7      8X  g   Q hVP                  R8X  g   Q hVP                  R8X  g   Q hVP
                  . R(O8X  g   Q hVP                  e   Q hV ! RRR^^^R7      pVP                  R8X  g   Q hVP                  \        ^^R7      8X  g   Q hVP                  R8X  g   Q hVP                  R8X  g   Q hVP
                  ^^.8X  g   Q hVP                  . R)O8X  g   Q hV ! RRR^^^R 7      pVP                  R!8X  g   Q hVP                  \        ^^R"7      8X  g   Q hVP                  R#8X  g   Q hVP                  R$8X  g   Q hVP
                  . R*O8X  g   Q hVP                  . R+O8X  g   Q hR# ),zH
Testing that einmix correctly decomposes into smaller transformations.
_EinmixDebuggera b c d e -> e d c b ad a bro   dar   Nzabcde,dab->edcbaz	a b c d e)ro   rp   r  r   r   r  ez
... -> ... )ro   rp   z	...,->...zb a ...  -> b c ...zb a c)ro   r  r   r   zba...,bac->bc...z(b a) ... -> b c (...)z(b a) ... -> b a ...)r  r   zb c ... -> b c (...)zb ... (a c) -> b ... (a d)zc dza d)ro   rp   r  r   r  zb ... (a c) -> b ... a c)r  r   zb ... a d -> b ... (a d)zb...ac,cd->b...adza ... (b c) -> a (... d b)zc d bzd b)ro   rp   r   r   r  za ... (b c) -> a ... b c)r   r   za ... d b -> a (... d b)za...bc,cdb->a...db)   r   rv   )r      r  )rv   r   r   r  r	  )r  r	  r   )r	  r	  r   )r   r   r  )r	  r   r  )	einops.layers._einmixr   pre_reshape_patternpost_reshape_patterneinsum_patternsaved_weight_shapesaved_bias_shapepre_reshape_lengthsr   )r   mixin1mixin2mixin3mixin4mixin5mixin6mixin7s           rB   test_einmix_decompositionr  g  s    6 
qAF
 %%---&&...  $6666$$	111""*** 
qAa	F %%---&&...  $6666$$	111""o555F
 %%---&&...  K///$$***""b(((
qAF
 %%---&&...  $6666$$	111""*** 
qAF
 %%)????%%Q777&&*@@@@  $6666$$	111""***$
qA	F %%)CCCC%%Q777&&*DDDD  $7777$$A...""i///$
qA	F %%)CCCC%%Q777&&*DDDD  $8888$$	111""i///rR   c            
     d   ^ RI Hp  \        P                  ! \        4      ;_uu_ 4        V ! RR^^R7       RRR4       \        P                  ! \        4      ;_uu_ 4        V ! RR^^^R7       RRR4       \        P                  ! \        4      ;_uu_ 4        V ! RR	^R
7       RRR4       \        P                  ! \        4      ;_uu_ 4        V ! RR	^R	R7       RRR4       R#   + '       g   i     L; i  + '       g   i     L; i  + '       g   i     Lo; i  + '       g   i     R# ; i)z
Testing different cases
r   r  r  )ro   r  r  Nzw a br  z(...) a -> ... ar  )ro   r  z(...) a -> a ...)ro   r  rp   )r
  r   rw   raisesr   r   s    rB   test_einmix_restrictionsr    s     6	{	#	#$ 1	
 
$ 
{	#	#$ 1	
 
$ 
{	#	#	
 
$ 
{	#	#	
 
$	#) 
$	# 
$	# 
$	# 
$	#	#s/   C% C8DD%C5	8D	D	D/	)r4   r5   r6   r7   )r         (   r   r   )r   r   r   )r      r  r  )r	  r  r	  r	  r	  )r   )r	  r	  r	  r	  )r	  r   r   r   )r   r  r  )r   r  )rv   r   )r   r     r  )   r   )FF)r+   collectionsr   r   r"   rw   einopsr   r   r   einops.testsr   rX   r   r	   
__author__r
   r   r!   rC   rQ   rY   r\   r^   r   r   r   r   r   r  r  r   rR   rB   <module>r'     sI    "   1 1 7 A
j"\] r
	UL"24DE	 2a*	 r
	 ,C@=Br
,<ul>ST /a1HJZ]m\no ^TBZ)9E8;LM	 &.R94
6_4[=c@0@fW0t
rR   