+
    PjC+                         ^ RI t ^ RIt^ RIHtHtHtHt ^ RIHt ^ RI	H
t
 ^ RIHtHt R R lt ! R R	4      t ! R
 R]4      tR# )    N)AnyDictListOptional)EinopsError)_product)ParsedExpression	_ellipsisc                0    V ^8  d   QhR\         R\        /# )   axesreport_message)setstr)formats   "n/Users/ahmed/devFolder/Ultron/claude-voice/gateway/.venv/lib/python3.14/site-packages/einops/layers/_einmix.py__annotate__r   
   s     7 7s 7C 7    c                 Z    \        V 4      ^ 8  d   \        VP                  V 4      4      hR# )r   N)lenr   r   )r   r   s   &&r   _report_axesr   
   s(    
4y1}.//566 r   c                   r   a a ] tR t^t oR
V3R lV 3R llltV3R lR ltV3R lR ltR tR tR	t	Vt
V ;t# )_EinmixMixinc                B   < V ^8  d   QhRS[ RS[ RS[S[ ,          RS[/# r   patternweight_shape
bias_shapeaxes_lengths)r   r   r   )r   __classdict__s   "r   r   _EinmixMixin.__annotate__   s.     1
 1
 1
3 1
HSM 1
jm 1
r   c                z   < \         SV `  4        Wn        W n        W0n        W@n        V P                  WW4R7       R# )a  
EinMix - Einstein summation with automated tensor management and axis packing/unpacking.

EinMix is a combination of einops and MLP, see tutorial:
https://github.com/arogozhnikov/einops/blob/main/docs/3-einmix-layer.ipynb

Imagine taking einsum with two arguments, one of each input, and one - tensor with weights
>>> einsum('time batch channel_in, channel_in channel_out -> time batch channel_out', input, weight)

This layer manages weights for you, syntax highlights a special role of weight matrix
>>> EinMix('time batch channel_in -> time batch channel_out', weight_shape='channel_in channel_out')
But otherwise it is the same einsum under the hood. Plus einops-rearrange.

Simple linear layer with a bias term (you have one like that in your framework)
>>> EinMix('t b cin -> t b cout', weight_shape='cin cout', bias_shape='cout', cin=10, cout=20)
There is no restriction to mix the last axis. Let's mix along height
>>> EinMix('h w c-> hout w c', weight_shape='h hout', bias_shape='hout', h=32, hout=32)
Example of channel-wise multiplication (like one used in normalizations)
>>> EinMix('t b c -> t b c', weight_shape='c', c=128)
Multi-head linear layer (each head is own linear layer):
>>> EinMix('t b (head cin) -> t b (head cout)', weight_shape='head cin cout', ...)

... and yes, you need to specify all dimensions of weight shape/bias shape in parameters.

Use cases:
- when channel dimension is not last, use EinMix, not transposition
- patch/segment embeddings
- when need only within-group connections to reduce number of weights and computations
- next-gen MLPs (follow tutorial link above to learn more!)
- in general, any time you want to combine linear layer and einops.rearrange

Uniform He initialization is applied to weight tensor.
This accounts for the number of elements mixed and produced.

Parameters
:param pattern: transformation pattern, left side - dimensions of input, right side - dimensions of output
:param weight_shape: axes of weight. A tensor of this shape is created, stored, and optimized in a layer
       If bias_shape is not specified, bias is not created.
:param bias_shape: axes of bias added to output. Weights of this shape are created and stored. If `None` (the default), no bias is added.
:param axes_lengths: dimensions of weight tensor
)r   r   r   r   N)super__init__r   r   r   r   initialize_einmix)selfr   r   r   r   	__class__s   &&&&,r   r$   _EinmixMixin.__init__   s?    T 	($(: 	 	
r   c                B   < V ^8  d   QhRS[ RS[ RS[S[ ,          RS[/# r   )r   r   dict)r   r    s   "r   r   r!   C   s5     v
 v
 v
C v
XVY] v
jn v
r   c                  a VP                  R 4      w  rV\        V4      p\        V4      p\        V4      p	\        \        P	                  VP
                  0 VP
                  mV	P
                  m4      R4       V	P                  '       d   \        R4      hVP                  '       g   VP                  '       dS   VP                  '       d   VP                  '       g   \        RV 24      hVP                  '       d   \        RV 24      h\        ;QJ d    R WxV	3 4       F  '       g   K   RM	  RM! R WxV	3 4       4      '       d   \        R4      hR	V9   g   R
V9   d   \        RV 24      hRp
RpRp\        ;QJ d&    R VP                   4       F  '       g   K   RM	  RM! R VP                   4       4      '       d   . pVP                   F  pW,          pK  	  V Uu. uF  q\        8w  d   TMRNK  	  ppRP                  V4      pV RV 2p
VP                  4        UUu/ uF  w  ppW9   g   K  VVbK  	  ppp\        ;QJ d&    R VP                   4       F  '       g   K   RM	  RM! R VP                   4       4      '       g   VP                  '       dS   . pVP                   F  pW,          pK  	  V Uu. uF  q\        8w  d   TMRNK  	  ppRP                  V4      pV RV 2pV P                  WV/ 4       V	P
                   F  pVV9  g   K  \        RV R24      h	  \        \        P	                  \        V4      0 VP
                  mV	P
                  m4      R4       \        \        P	                  V	P
                  0 VP
                  mVP
                  m4      R4       \        V	P
                  4      ^ 8X  d   \         P"                  ! R^R7       V	P                   Uu. uF  w  pVV,          NK  	  pp\%        V	P                   Uu. uF!  w  pVVP
                  9  g   K  VV,          NK#  	  up4      pVEe3   \'        V\(        4      '       g   \        R4      h\        V4      p\        \        P	                  VP
                  VP
                  4      R4       \        \        P	                  VP
                  \        V4      4      R4       . pRpVP                   F  pV\        8X  d   V'       d   \        R4      hK#  V Fa  pV\        8X  d   V'       d   \        R4      hK#  VVP
                  9   d   VP+                  VV,          4       RpKP  VP+                  ^4       Kc  	  K  	  MRp^V,          R,          p^V,          R,          pV P-                  VVVV4       0 VP
                  mVP
                  mV	P
                  mp\        V9   d   VP/                  \        4       \1        V4      p\3        \4        P6                  V4       UUu/ uF	  w  ppVVbK  	  uppoRS\        &   R V3R llpV! V4       R V! V	4       R V! V4       2V n        R# u upi u uppi u upi u upi u upi u uppi )!z->z7Unrecognized identifiers on the right side of EinMix {}zKEllipsis is not supported in weight, as its shape should be fully specifiedz,Ellipsis in EinMix should be on both sides, z3Ellipsis on left side can't be in parenthesis, got c              3   8   "   T F  qP                   x  K  	  R # 5iN)has_non_unitary_anonymous_axes).0xs   & r   	<genexpr>1_EinmixMixin.initialize_einmix.<locals>.<genexpr>S   s     O9NA//9Ns   TFz2Anonymous axes (numbers) are not allowed in EinMix()z,Parenthesis is not allowed in weight shape: Nc              3   >   "   T F  p\        V4      ^8g  x  K  	  R# 5i   Nr   r/   groups   & r   r1   r2   [   s     =,<5s5zQ,<   ... z-> c              3   >   "   T F  p\        V4      ^8g  x  K  	  R# 5ir6   r8   r9   s   & r   r1   r2   d   s     >,=5s5zQ,=r;   z ->z
Dimension z of weight should be specifiedzAxes {} are not used in patternzWeight axes {} are redundantzCEinMix: weight has no dimensions (means multiplication by a number))
stacklevelzAbias shape should be string specifying which axes bias depends onz"Bias axes {} not present in outputz#Sizes not provided for bias axes {}z:all bias dimensions should go after ellipsis in the outputg      ?c                $    V ^8  d   QhR\         /# )r   r   )r	   )r   s   "r   r   4_EinmixMixin.initialize_einmix.<locals>.__annotate__   s     	# 	#&6 	#r   c                   < . pV P                    Fa  p\        V\        4      '       d+   TP                  V Uu. uF  pSV,          NK  	  up4       KC  V\        8X  g   Q hVP                  R 4       Kc  	  RP                  V4      # u upi )r<    )composition
isinstancelistextendr
   appendjoin)r   resultcomposed_axisaxismapping2letterss   &   r   write_flat_remapped;_EinmixMixin.initialize_einmix.<locals>.write_flat_remapped   sx    F!%!1!1mT22MM]"S]T?4#8#8]"ST(I555MM%( "2 776?"	 #Ts   B
,)splitr	   r   r   
differenceidentifiershas_ellipsisr   has_ellipsis_parenthesizedanyrD   r
   rI   items_create_rearrange_layersr   warningswarnr   rE   r   rH   _create_parametersremovesortedzipstringascii_lowercaseeinsum_pattern) r&   r   r   r   r   left_patternright_patternleftrightweightpre_reshape_patternpre_reshape_lengthspost_reshape_patternnamesr:   namerD   lengthrL   _weight_shape_fan_inbias_bias_shapeused_non_trivial_sizer   weight_bound
bias_boundmapped_identifiersletterkrN   rM   s    &&&&&                          @r   r%   _EinmixMixin.initialize_einmixC   s   &-mmD&9#- /!,/NN5,,.V0@0@.V6CUCU.VWE	
 kll 2 2 2%%%%*<*<*<!$PQXPY"Z[[...!$WX_W`"abb3O$v9NO333O$v9NOOORSS,#"5 L\N[\\""#3=D,<,<=333=D,<,<===!E)) *FKLedY.TE9eEL((5/K%1N#k]"CDPDVDVDX"jDXLD&\`\i<4<DX"j3>E,=,=>333>E,=,=>>>%BbBbBbE** +FKLedY.TE9eEL((5/K&1]#m_#E %%&9Pdfhi&&D<'!Jtf4R"STT ' 	NN3|,.V0@0@.V6CUCU.VW-	
 	NN6--/V1A1A/VEDUDU/VWYw	
 v!!"a'MM_lmn;A;M;MN;Md++;MNf>P>Pr>P7DTX`e`q`qTq.L..>Prs!j#..!"eff#J/Dt//1B1BC4 t//\1BC5
 K$)!))9$,)*fgg - !%9,4&12n&o o  5!T%5%55'..|D/AB481'..q1 !% *  KG+'kc)
|[*U Zt//Y%2C2CYfFXFXY**%%i0#$6769&:P:PRd6ef6e1f96ef%*	"	# 	# #4()+>v+F*GrJ]^cJdIef 	q M #k M& OrZ gs0   %Y.)Y39Y3Y94Y>Z7Z&Zc          	      r   < V ^8  d   QhRS[ S[,          RS[ S[,          RS[ S[,          RS[ S[,          /# r   rg   rh   ri   post_reshape_lengthsr   r   r   )r   r    s   "r   r   r!      sI     T T%c]T &d^T 'sm	T
 'tnTr   c                    \        R 4      h).Should be defined in framework implementationsNotImplementedErrorr&   rg   rh   ri   rz   s   &&&&&r   rX   %_EinmixMixin._create_rearrange_layers   s     ""RSSr   c                    \        R4      h)zShape and implementationsr}   r~   r&   r   rr   r   rs   s   &&&&&r   r[   _EinmixMixin._create_parameters   s    !"RSSr   c                @   \        V P                  4      pVR V P                   R2,          pV P                  e   VR V P                   R2,          pV P                  P                  4        F  w  r#VRV RV 2,          pK  	  V P                  P                   RV R2# )z, ''z, =r3   r4   )reprr   r   r   r   rW   r'   __name__)r&   paramsrL   rl   s   &   r   __repr___EinmixMixin.__repr__   s    dll#C))*!,,??&DOO,A..F --335LD4&&**F 6..))*!F8155r   )r   r   ra   r   r   r-   )r   
__module____qualname____firstlineno__r$   r%   rX   r[   r   __static_attributes____classdictcell____classcell__)r'   r    s   @@r   r   r      s9     1
 1
fv
 v
pT TT6 6r   r   c                   <   a  ] tR t^t o RtV 3R lR ltR tRtV tR# )_EinmixDebuggerzUsed only to test mixinc          	      r   < V ^8  d   QhRS[ S[,          RS[ S[,          RS[ S[,          RS[ S[,          /# ry   r{   )r   r    s   "r   r   _EinmixDebugger.__annotate__   sC     
9 
9%c]
9 &d^
9 'sm	
9
 'tn
9r   c                6    Wn         W n        W0n        W@n        R # r-   )rg   rh   ri   rz   r   s   &&&&&r   rX   (_EinmixDebugger._create_rearrange_layers   s     $7 #6 $8!$8!r   c                    Wn         W0n        R # r-   )saved_weight_shapesaved_bias_shaper   s   &&&&&r   r[   "_EinmixDebugger._create_parameters   s    ". *r   )rz   ri   rh   rg   r   r   N)	r   r   r   r   __doc__rX   r[   r   r   )r    s   @r   r   r      s     !
9 
9+ +r   r   )r_   rY   typingr   r   r   r   einopsr   einops.einopsr   einops.parsingr	   r
   r   r   r    r   r   <module>r      s8      , ,  " 67
@6 @6F+l +r   