
    'i                         d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
mZ ddlmZ dZ G d	 d
e
e      Z G d dee      Z G d dee      Zy)aj  
Comment about tensorflow layers:
unfortunately instructions on creation of TF layers change constantly,
and changed way too many times at this point to remember what-compatible-where.

Layers in einops==0.7.0 (and several prior versions)
 are compatible with TF 2.13

Layers in einops==0.8.0 were re-implemented
 according to official instructions for TF 2.16

    )DictOptionalcastN)Layer   )RearrangeMixinReduceMixin)_EinmixMixinzAlex Rogozhnikovc                       e Zd Zd Zd Zd Zy)	Rearrangec                      y N selfinput_shapes     R/var/www/stems/demucs_env/lib/python3.12/site-packages/einops/layers/tensorflow.pybuildzRearrange.build           c                 $    | j                  |      S r   _apply_reciper   inputss     r   callzRearrange.call       !!&))r   c                 6    d| j                   i| j                  S )Npattern)r   axes_lengthsr   s    r   
get_configzRearrange.get_config    s    4<<=4+<+<==r   N__name__
__module____qualname__r   r   r"   r   r   r   r   r      s    *>r   r   c                       e Zd Zd Zd Zd Zy)Reducec                      y r   r   r   s     r   r   zReduce.build%   r   r   c                 $    | j                  |      S r   r   r   s     r   r   zReduce.call(   r   r   c                 L    | j                   | j                  d| j                  S )N)r   	reduction)r   r,   r    r!   s    r   r"   zReduce.get_config+   s     <<dnnZHYHYZZr   Nr#   r   r   r   r(   r(   $   s    *[r   r(   c            	       T    e Zd Zd Zdee   dee   dee   dee   fdZd Zd Z	d	 Z
y
)EinMixc                     ||||g| _         y r   )_params)r   weight_shapeweight_bound
bias_shape
bias_bounds        r   _create_parameterszEinMix._create_parameters0   s     %lJ
Kr   pre_reshape_patternpre_reshape_lengthspost_reshape_patternpost_reshape_lengthsc                     d | _         |t        |fi t        t        |      | _         d | _        | t        |fi t        t        |      | _        y y r   )pre_rearranger   r   dictpost_rearrange)r   r6   r7   r8   r9   s        r   _create_rearrange_layerszEinMix._create_rearrange_layers5   s[     "*!*+>!b$tM`Ba!bD"+"+,@"eDOcDd"eD ,r   c                     | j                   \  }}}}| j                  |t        j                  | |      d      | _        |/| j                  |t        j                  | |      d      | _        y d | _        y )NT)shapeinitializer	trainable)r0   
add_weighttfrandom_uniform_initializerweightbias)r   r   r1   r2   r3   r4   s         r   r   zEinMix.buildD   s    ?C||<|Zoo55|m\R & 
 ! 99:+zR ( DI DIr   c                    | j                   | j                  |      }t        j                  | j                  || j                        }| j
                  || j
                  z   }| j                  | j                  |      }|S r   )r;   rD   einsumeinsum_patternrF   rG   r=   )r   r   results      r   r   zEinMix.callU   ss    )''/F4..D99 dii'F*((0Fr   c                 b    | j                   | j                  | j                  d| j                  S )N)r   r1   r3   )r   r1   r3   r    r!   s    r   r"   zEinMix.get_config_   s3    || --//
 	
 	
r   N)r$   r%   r&   r5   r   strr   r>   r   r   r"   r   r   r   r.   r.   /   sW    L
f%c]f &d^f 'sm	f
 'tnf"
r   r.   )__doc__typingr   r   r   
tensorflowrD   tensorflow.keras.layersr    r   r	   _einmixr
   
__author__r   r(   r.   r   r   r   <module>rU      sO    ( '  ) ) !
> >[[% [6
\5 6
r   