a
    þdí  ã                   @   sd   U d dl mZ d dlZd dlZd dlmZmZmZmZ dZ	e
ed< G dd„ deƒZG dd	„ d	ƒZdS )
é    )ÚEinopsErrorN)ÚListÚOptionalÚSetÚTupleu   â€¦Ú	_ellipsisc                   @   s&   e Zd ZdZedœdd„Zdd„ ZdS )ÚAnonymousAxiszIImportant thing: all instances of this class are not equal to each other )Úvaluec                 C   s<   t |ƒ| _| jdkr8| jdkr(tdƒ‚ntd | j¡ƒ‚d S )Né   zENo need to create anonymous axis of length 1. Report this as an issuez2Anonymous axis should have positive length, not {})Úintr	   r   Úformat)Úselfr	   © r   úW/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/einops/parsing.pyÚ__init__   s
    



zAnonymousAxis.__init__c                 C   s   d  t| jƒ¡S )Nz{}-axis)r   Ústrr	   )r   r   r   r   Ú__repr__   s    zAnonymousAxis.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   	   s   r   c                   @   st   e Zd ZdZddœedœdd„Zedœdd„Zedœd	d
„Ze	de
eeee
f dœdd„ƒZe	e
edœdd„ƒZdS )ÚParsedExpressionz¡
    non-mutable structure that contains information about one side of expression (e.g. 'b c (h w)')
    and keeps some information important for downstream
    F©Úallow_underscorec                   sV  dˆ_ d ˆ_tƒ ˆ_dˆ_g ˆ_d|v rrd|vr8tdƒ‚t |d¡dksXt |d¡dkr`tdƒ‚| 	dt
¡}dˆ_ d ‰‡ ‡‡fd	d
„}d }|D ]¢}|dv rî||ƒ d }|dkrÄˆd ur¾tdƒ‚g ‰n(|dkrìˆd u rÜtdƒ‚ˆj ˆ¡ d ‰qŽt |¡s|dt
fv r"|d u r|}n||7 }qŽtd |¡ƒ‚qŽˆd urJtd |¡ƒ‚||ƒ d S )NFÚ.z...z6Expression may contain dots only inside ellipsis (...)r
   é   zUExpression may contain dots only inside ellipsis (...); only one ellipsis for tensor Tc                    s  | d ur| ˆj v r.ˆ r | dks.td | ¡ƒ‚| tkrpˆj  t¡ ˆd u r^ˆj t¡ dˆ_nˆ t¡ dˆ_n¤t 	| ¡}|r¤t
| ƒdkr¤ˆd u r ˆj g ¡ n d S ˆj| ˆ d\}}|sÎ|sÎtd | |¡ƒ‚|rÚt| ƒ} ˆj  | ¡ |rðdˆ_ˆd u r
ˆj | g¡ n
ˆ | ¡ d S )NÚ_z5Indexing expression contains duplicate dimension "{}"FTr
   r   zInvalid axis identifier: {}
{})Úidentifiersr   r   r   ÚaddÚcompositionÚappendÚhas_ellipsis_parenthesizedr   Ú	isdecimalr   Úcheck_axis_name_return_reasonr   Úhas_non_unitary_anonymous_axes)ÚxZ	is_numberZis_axis_nameÚreason©r   Zbracket_groupr   r   r   Úadd_axis_name0   s6    




z0ParsedExpression.__init__.<locals>.add_axis_namez() ú(zDAxis composition is one-level (brackets inside brackets not allowed)ú)zBrackets are not balancedr   zUnknown character '{}'z*Imbalanced parentheses in expression: "{}")Zhas_ellipsisr!   Úsetr   r$   r   r   r   ÚcountÚreplacer   r    Úisalnumr   )r   Z
expressionr   r(   Zcurrent_identifierÚcharr   r'   r   r      sL     ÿ#


zParsedExpression.__init__)Úreturnc                 C   s:   g }| j D ]*}t|tƒs J dƒ‚|D ]}| |¡ q$q
|S )Nzdoes not work with ellipsis)r   Ú
isinstanceÚlistr    )r   ÚresultZcomposed_axisÚaxisr   r   r   Úflat_axes_orderm   s    
z ParsedExpression.flat_axes_orderc                 C   s,   | j D ] }t|tƒrt|ƒdkr dS qdS )Nr
   TF)r   r1   r2   Úlen)r   Zaxesr   r   r   Úhas_composed_axesu   s    
z"ParsedExpression.has_composed_axes)Únamer   r0   c                 C   sr   t  | ¡sdS | d dks&| d dkr:| dkr6|r6dS dS t | ¡rVt d | ¡t¡ | dv rjt d	t¡ dS d S )
N)Fznot a valid python identifierr   r   éÿÿÿÿ)TÚ )Fz8axis name should should not start or end with underscorez9It is discouraged to use axes names that are keywords: {})r4   zQIt is discouraged to use 'axis' as an axis name and will raise an error in future)	r   ÚisidentifierÚkeywordÚ	iskeywordÚwarningsÚwarnr   ÚRuntimeWarningÚFutureWarning)r8   r   r   r   r   r#   |   s    

ÿz.ParsedExpression.check_axis_name_return_reason)r8   r0   c                 C   s   t  | ¡\}}|S )z‹
        Valid axes names are python identifiers except keywords,
        and additionally should not start or end with underscore
        )r   r#   )r8   Zis_validZ_reasonr   r   r   Úcheck_axis_nameŒ   s    z ParsedExpression.check_axis_nameN)F)r   r   r   r   Úboolr   r   r5   r7   Ústaticmethodr   r   r#   rB   r   r   r   r   r      s   Pr   )Zeinopsr   r<   r>   Útypingr   r   r   r   r   r   Ú__annotations__Úobjectr   r   r   r   r   r   Ú<module>   s   