o
    ٷi                     @   s   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
 d
e
eZG dd deeZG dd deeZdS )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d Zdd Zdd ZdS )	Rearrangec                 C      d S N selfinput_shaper   r   L/home/ubuntu/.local/lib/python3.10/site-packages/einops/layers/tensorflow.pybuild      zRearrange.buildc                 C   
   |  |S r   _apply_reciper   inputsr   r   r   call      
zRearrange.callc                 C   s   d| j i| jS )Npattern)r   axes_lengthsr   r   r   r   
get_config    s   zRearrange.get_configN__name__
__module____qualname__r   r   r   r   r   r   r   r          r   c                   @   r
   )Reducec                 C   r   r   r   r   r   r   r   r   %   r   zReduce.buildc                 C   r   r   r   r   r   r   r   r   (   r   zReduce.callc                 C   s   | j | jd| jS )N)r   	reduction)r   r&   r   r   r   r   r   r   +   s   zReduce.get_configNr    r   r   r   r   r%   $   r$   r%   c                	   @   sV   e Zd Zdd Zdee dee dee dee fddZd	d
 Zdd Z	dd Z
dS )EinMixc                 C   s   ||||g| _ d S r   )_params)r   weight_shapeweight_bound
bias_shape
bias_boundr   r   r   _create_parameters0   s   zEinMix._create_parameterspre_reshape_patternpre_reshape_lengthspost_reshape_patternpost_reshape_lengthsc                 C   sT   d | _ |d urt|fi tt|| _ d | _|d ur(t|fi tt|| _d S d S r   )pre_rearranger   r   dictpost_rearrange)r   r.   r/   r0   r1   r   r   r   _create_rearrange_layers5   s   zEinMix._create_rearrange_layersc                 C   s\   | j \}}}}| j|t| |dd| _|d ur)| j|t| |dd| _d S d | _d S )NT)shapeinitializer	trainable)r(   
add_weighttfrandom_uniform_initializerweightbias)r   r   r)   r*   r+   r,   r   r   r   r   D   s   
zEinMix.buildc                 C   sR   | j d ur
|  |}t| j|| j}| jd ur|| j }| jd ur'| |}|S r   )r2   r:   einsumeinsum_patternr<   r=   r4   )r   r   resultr   r   r   r   U   s   





zEinMix.callc                 C   s   | j | j| jd| jS )N)r   r)   r+   )r   r)   r+   r   r   r   r   r   r   _   s   zEinMix.get_configN)r!   r"   r#   r-   r   strr   r5   r   r   r   r   r   r   r   r'   /   s    

r'   )__doc__typingr   r   r   
tensorflowr:   tensorflow.keras.layersr    r   r   _einmixr	   
__author__r   r%   r'   r   r   r   r   <module>   s    