o
    \ôjæ  ã                
   @   sô   d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ edƒZeeedf ee f Zedd	�d
ededeeeef fdd„ƒZdee d
edeeee f fdd„Zdedefdd„Zdedee d
edee fdd„ZdS )é    )Ú	lru_cache)ÚListÚUnionÚTypeVarÚTupleÚSequence)ÚEinopsError)Úget_backend)ÚParsedExpressionÚTensor.é€   )ÚmaxsizeÚpatternÚopnameÚreturnc           
   	   C   sÀ   |   ¡ }t|ƒ}t|ƒt|ƒkrtd|› d| › d�ƒ‚d|vr*td|› d| › d�ƒ‚|D ]}|dkrIt |¡\}}|sItd|› d|› d| › d�ƒ‚q,| d¡}t|ƒ| d }|| }	|||	fS )	NzDuplicates in axes names in z(..., "z")Ú*zNo *-axis in zInvalid axis name z in é   )ÚsplitÚsetÚlenr   r
   Úcheck_axis_name_return_reasonÚindex)
r   r   ÚaxesZaxes_setÚaxisÚis_validÚreasonÚn_axes_beforeÚn_axes_afterÚmin_axes© r   ú_/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/einops/packing.pyÚanalyze_pattern   s    €

r!   Útensorsc                 C   sÎ   t |dƒ\}}}t| d ƒ}g }g }t| ƒD ]G\}}	| |	¡}
t|
ƒ|k r6td|› d|
› d|› d|› d�	ƒ‚t|
ƒ| }| |
||… ¡ | | |	g |
d|… ¢d	‘|
|d… ¢R ¡¡ q|j||d
�|fS )a  
    Packs several tensors into one.
    See einops tutorial for introduction into packing (and how it replaces stack and concatenation).

    Parameters:
        tensors: tensors to be packed, can be of different dimensionality
        pattern: pattern that is shared for all inputs and output, e.g. "i j * k" or "batch seq *"

    Returns:
        (packed_tensor, packed_shapes aka PS)

    Example:
    ```python
    >>> from numpy import zeros as Z
    >>> inputs = [Z([2, 3, 5]), Z([2, 3, 7, 5]), Z([2, 3, 7, 9, 5])]
    >>> packed, ps = pack(inputs, 'i j * k')
    >>> packed.shape, ps
    ((2, 3, 71, 5), [(), (7,), (7, 9)])
    ```

    In this example, axes were matched to: i=2, j=3, k=5 based on order (first, second, and last).
    All other axes were 'packed' and concatenated.
    PS (packed shapes) contains information about axes that were matched to '*' in every input.
    Resulting tensor has as many elements as all inputs in total.

    Packing can be reversed with unpack, which additionally needs PS (packed shapes) to reconstruct order.

    ```python
    >>> inputs_unpacked = unpack(packed, ps, 'i j * k')
    >>> [x.shape for x in inputs_unpacked]
    [(2, 3, 5), (2, 3, 7, 5), (2, 3, 7, 9, 5)]
    ```

    Read the tutorial for introduction and application scenarios.
    Úpackr   zpacked tensor #z' (enumeration starts with 0) has shape z, while pattern z assumes at least z axesNéÿÿÿÿ)r   )	r!   r	   Ú	enumerateÚshaper   r   ÚappendÚreshapeÚconcat)r"   r   r   r   r   ÚbackendZreshaped_tensorsÚpacked_shapesÚiÚtensorr&   Zaxis_after_packed_axesr   r   r    r#   "   s$   $
ÿÿÿ2r#   Úxc                 C   s   d}| D ]}||9 }q|S )Nr   r   )r.   Úresultr,   r   r   r    Úprod[   s   
r0   r-   r+   c              
      sÄ  t |dd�\}}}tˆƒ‰ ˆ  ˆ¡}t|ƒ|d | kr&td|› d|› �ƒ‚|}dd„ |D ƒ}tdd	„ |D ƒƒ}	|	dkrGtd|› d
|› d�ƒ‚dgt|ƒ || g ‰|	dkrot|dd… ƒD ]\}
}ˆ|
 | ˆ|
d < q_n5| d¡}t|ƒD ]}
ˆ|
 ||
  ˆ|
d < qxt|d t|ƒƒddd… D ]}ˆ|d  ||  ˆ|< q•|d|… ‰||d d… ‰t	ddƒf| ‰z‡ ‡‡‡‡‡fdd„t|ƒD ƒW S  t
yá   td|› dˆd › d|› �ƒ‚w )a8  
    Unpacks a single tensor into several by splitting over a selected axes.
    See einops tutorial for introduction into packing (and how it replaces stack and concatenation).

    Parameters:
        tensor: tensor to be unpacked
        packed_shapes: packed_shapes (aka PS) is a list of shapes that take place of '*' in each output.
            output will contain a single tensor for every provided shape
        pattern: pattern that is shared for input and all outputs, e.g. "i j * k" or "batch seq *",
            where * designates an axis to be unpacked

    Returns:
        list of tensors

    If framework supports views, results are views to the original tensor.

    Example:
    ```python
    >>> from numpy import zeros as Z
    >>> inputs = [Z([2, 3, 5]), Z([2, 3, 7, 5]), Z([2, 3, 7, 9, 5])]
    >>> packed, ps = pack(inputs, 'i j * k')
    >>> packed.shape, ps
    ((2, 3, 71, 5), [(), (7,), (7, 9)])
    ```

    In this example, axes were matched to: i=2, j=3, k=5 based on order (first, second, and last).
    All other axes were 'packed' and concatenated.
    PS (packed shapes) contains information about axes that were matched to '*' in every input.
    Resulting tensor has as many elements as all inputs in total.

    Packing can be reversed with unpack, which additionally needs PS (packed shapes) to reconstruct order.

    ```python
    >>> inputs_unpacked = unpack(packed, ps, 'i j * k')
    >>> [x.shape for x in inputs_unpacked]
    [(2, 3, 5), (2, 3, 7, 5), (2, 3, 7, 9, 5)]
    ```

    Read the tutorial for introduction and application scenarios.
    Úunpack)r   r   zunpack(..., z)) received input of wrong dim with shape c                 S   s    g | ]}d |v r
d nt |ƒ‘qS )r$   )r0   )Ú.0Zp_shaper   r   r    Ú
<listcomp>”   s     zunpack.<locals>.<listcomp>c                 s   s   � | ]	}t |d kƒV  qdS )r$   N)Úint)r2   r.   r   r   r    Ú	<genexpr>–   s   € zunpack.<locals>.<genexpr>z) received more than one -1 in z and can't infer dimensionsr   Nr$   c                    sJ   g | ]!\}}ˆ   ˆg ˆ¢tˆ| ˆ|d   ƒ‘R  g ˆ¢|¢ˆ¢R ¡‘qS )r   )r(   Úslice)r2   r,   Zelement_shape©r*   Z	shape_endZshape_startZslice_fillerZsplit_positionsr-   r   r    r3   ±   s    û"ýÿzError during unpack(..., "z!"): could not split axis of size z into requested )r!   r	   r&   r   r   Úsumr%   r   Úranger6   ÚBaseExceptionÚRuntimeError)r-   r+   r   r   r   r   Úinput_shapeZunpacked_axisZlengths_of_composed_axesZn_unknown_composed_axesr,   r.   Zunknown_composed_axisÚjr   r7   r    r1   b   sH   )
ÿ
ÿ
 úÿÿþr1   N)Ú	functoolsr   Útypingr   r   r   r   r   Úeinopsr   Zeinops._backendsr	   Zeinops.parsingr
   r   r4   ZShapeÚstrr!   r#   r0   r1   r   r   r   r    Ú<module>   s    "&9&