o
    †ôjC+  ã                   @   s„   d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ dedefdd	„ZG d
d„ dƒZG dd„ deƒZdS )é    N)ÚAnyÚDictÚListÚOptional)ÚEinopsError)Ú_product)ÚParsedExpressionÚ	_ellipsisÚaxesÚreport_messagec                 C   s   t | ƒdkrt| | ¡ƒ‚d S )Nr   )Úlenr   Úformat)r
   r   © r   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/einops/layers/_einmix.pyÚ_report_axes
   s   ÿr   c                	       sŒ   e Zd Zddededee def‡ fdd„Zdededee def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‡  ZS )Ú_EinmixMixinNÚpatternÚweight_shapeÚ
bias_shapeÚaxes_lengthsc                    s8   t ƒ  ¡  || _|| _|| _|| _| j||||d� dS )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__r   r   r      s   
*
ÿz_EinmixMixin.__init__c                    sê  |  d¡\}}t|ƒ}t|ƒ‰t|ƒ}tt ˆjh |j£|j£¡dƒ |jr*tdƒ‚|js0ˆjrG|jr6ˆjs=td|› �ƒ‚|jrGtd|› �ƒ‚t	dd„ |ˆ|fD ƒƒrWtdƒ‚d	|v s_d
|v rftd|› �ƒ‚d }	d }
d }t	dd„ |j
D ƒƒr g ‰|j
D ]}ˆ|7 ‰q{dd„ ˆD ƒ‰d ˆ¡}|› d|› �}	‡fdd„ˆ  ¡ D ƒ}
t	dd„ ˆj
D ƒƒs­ˆjrÌg ‰ˆj
D ]}ˆ|7 ‰q²dd„ ˆD ƒ‰d ˆ¡}|› d|› �}|  |	|
|i ¡ |jD ]}|ˆ vråtd|› d�ƒ‚q×tt tˆ ƒh |j£|j£¡dƒ tt |jh |j£ˆj£¡dƒ t|jƒdk�rtjddd� ‡ fdd„|j
D ƒ}t‡ ‡fdd„|j
D ƒƒ}|d u�r›t|tƒ�s<td ƒ‚t|ƒ}tt |jˆj¡d!ƒ tt |jtˆ ƒ¡d"ƒ g }d#}ˆj
D ];}|tk�rn|�rltd$ƒ‚�q^|D ]'}|tk�r€|�r~td$ƒ‚�qp||jv �r‘| ˆ | ¡ d%}�qp| d&¡ �qp�q^nd }d'| d( }d&| d( }|  ||||¡ h |j£ˆj£|j£}t|v �rÆ| t¡ t|ƒ}d)d„ ttj|ƒD ƒ‰d*ˆt< d+tf‡fd,d-„}||ƒ› d.||ƒ› d|ˆƒ› �| _d S )/Nz->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                 s   s   � | ]}|j V  qd S ©N)Úhas_non_unitary_anonymous_axes)Ú.0Úxr   r   r   Ú	<genexpr>S   s   € z1_EinmixMixin.initialize_einmix.<locals>.<genexpr>z2Anonymous axes (numbers) are not allowed in EinMixú(ú)z,Parenthesis is not allowed in weight shape: c                 s   ó   � | ]	}t |ƒd kV  qdS ©é   N©r   ©r   Úgroupr   r   r   r    [   ó   € c                 S   ó   g | ]
}|t kr
|nd ‘qS ©ú...©r	   ©r   Únamer   r   r   Ú
<listcomp>_   ó    z2_EinmixMixin.initialize_einmix.<locals>.<listcomp>ú z-> c                    s   i | ]\}}|ˆ v r||“qS r   r   )r   r/   Úlength)Únamesr   r   Ú
<dictcomp>b   s    z2_EinmixMixin.initialize_einmix.<locals>.<dictcomp>c                 s   r#   r$   r&   r'   r   r   r   r    d   r)   c                 S   r*   r+   r-   r.   r   r   r   r0   h   r1   z ->z
Dimension z of weight should be specifiedzAxes {} are not used in patternzWeight axes {} are redundantr   zCEinMix: weight has no dimensions (means multiplication by a number)é   )Ú
stacklevelc                    s   g | ]\}ˆ | ‘qS r   r   ©r   Úaxis)r   r   r   r0   {   ó    c                    s    g | ]\}|ˆj vrˆ | ‘qS r   )Úidentifiersr8   )r   Úrightr   r   r0   }   s     zAbias shape should be string specifying which axes bias depends onz"Bias axes {} not present in outputz#Sizes not provided for bias axes {}Fz:all bias dimensions should go after ellipsis in the outputTr%   é   g      à?c                 S   s   i | ]\}}||“qS r   r   )r   ÚletterÚkr   r   r   r5   ª   r:   r,   r
   c                    sT   g }| j D ]}t|tƒr| ‡ fdd„|D ƒ¡ q|tksJ ‚| d¡ qd |¡S )Nc                    s   g | ]}ˆ | ‘qS r   r   r8   ©Úmapping2lettersr   r   r0   ±   s    zO_EinmixMixin.initialize_einmix.<locals>.write_flat_remapped.<locals>.<listcomp>r,   Ú )ÚcompositionÚ
isinstanceÚlistÚextendr	   ÚappendÚjoin)r
   ÚresultÚcomposed_axisr@   r   r   Úwrite_flat_remapped­   s   


z;_EinmixMixin.initialize_einmix.<locals>.write_flat_remappedú,)Úsplitr   r   ÚsetÚ
differencer;   Úhas_ellipsisr   Úhas_ellipsis_parenthesizedÚanyrC   rH   ÚitemsÚ_create_rearrange_layersr   ÚwarningsÚwarnr   rD   Ústrr	   rG   Ú_create_parametersÚremoveÚsortedÚzipÚstringÚascii_lowercaseÚeinsum_pattern)r   r   r   r   r   Úleft_patternZright_patternÚleftÚweightÚpre_reshape_patternÚpre_reshape_lengthsÚpost_reshape_patternr(   rC   r9   Z_weight_shapeZ_fan_inÚbiasZ_bias_shapeZused_non_trivial_sizer
   Úweight_boundÚ
bias_boundZmapped_identifiersrK   r   )r   rA   r4   r<   r   r   C   sÀ   þ






ÿþÿ
þþ

ÿ
ÿøú

ÿz_EinmixMixin.initialize_einmixrb   rc   rd   Úpost_reshape_lengthsc                 C   ó   t dƒ‚)Nú.Should be defined in framework implementations©ÚNotImplementedError©r   rb   rc   rd   rh   r   r   r   rT   »   s   z%_EinmixMixin._create_rearrange_layersc                 C   ri   )zShape and implementationsrj   rk   ©r   r   rf   r   rg   r   r   r   rX   Ä   s   z_EinmixMixin._create_parametersc                 C   st   t | jƒ}|d| j› d�7 }| jd ur|d| j› d�7 }| j ¡ D ]\}}|d|› d|› �7 }q!| jj› d|› d�S )Nz, 'ú'z, ú=r!   r"   )Úreprr   r   r   r   rS   r   Ú__name__)r   Úparamsr9   r3   r   r   r   Ú__repr__È   s   

z_EinmixMixin.__repr__r   )rr   Ú
__module__Ú__qualname__rW   r   r   r   Údictr   r   rT   rX   rt   Ú__classcell__r   r   r   r   r      s    $3xþýü
û	r   c                	   @   sB   e Zd ZdZdee dee dee dee fdd„Zdd	„ Zd
S )Ú_EinmixDebuggerzUsed only to test mixinrb   rc   rd   rh   c                 C   s   || _ || _|| _|| _d S r   )rb   rc   rd   rh   rm   r   r   r   rT   Õ   s   
z(_EinmixDebugger._create_rearrange_layersc                 C   s   || _ || _d S r   )Zsaved_weight_shapeZsaved_bias_shapern   r   r   r   rX   á   s   
z"_EinmixDebugger._create_parametersN)	rr   ru   rv   Ú__doc__r   rW   r   rT   rX   r   r   r   r   ry   Ò   s    þýü
ûry   )r\   rU   Útypingr   r   r   r   Úeinopsr   Zeinops.einopsr   Zeinops.parsingr   r	   rN   rW   r   r   ry   r   r   r   r   Ú<module>   s     D