o
    eq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

    )OptionalDict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   _/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/einops/layers/tensorflow.pybuild      zRearrange.buildc                 C   
   |  |S r   Z_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_parameters2   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.   r/   r0   r   r   r   _create_rearrange_layers7   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)shapeZinitializerZ	trainable)r'   Z
add_weighttfZrandom_uniform_initializerweightbias)r   r   r(   r)   r*   r+   r   r   r   r   F   s   
zEinMix.buildc                 C   sR   | j d ur
|  |}t| j|| j}| jd ur|| j }| jd ur'| |}|S r   )r1   r6   ZeinsumZeinsum_patternr7   r8   r3   )r   r   resultr   r   r   r   W   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   a   s   zEinMix.get_configN)r    r!   r"   r,   r   strr   r4   r   r   r   r   r   r   r   r&   1   s    

r&   )__doc__typingr   r   r   Z
tensorflowr6   Ztensorflow.keras.layersr    r   r   Z_einmixr	   
__author__r   r$   r&   r   r   r   r   <module>   s    