a
    d                     @   sT   d Z ddlZddlmZ ddlmZmZ ddlmZ G dd dZ	G d	d
 d
Z
dS )zAlex Rogozhnikov    N)_apply_recipe   )TransformRecipe_prepare_transformation_recipe)EinopsErrorc                       sJ   e Zd ZdZ fddZdd Zejddedd	d
Z	dd Z
  ZS )RearrangeMixinz
    Rearrange layer behaves identically to einops.rearrange operation.

    :param pattern: str, rearrangement pattern
    :param axes_lengths: any additional specification of dimensions

    See einops.rearrange for source_examples.
    c                    s$   t    || _|| _|  | _d S N)super__init__patternaxes_lengthsrecipe_recipe)selfr   r   	__class__ _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/einops/layers/__init__.pyr
      s    
zRearrangeMixin.__init__c                 C   s>   t | j}| j D ]\}}|d||7 }qd| jj|S )N, {}={}{}({}))reprr   r   itemsformatr   __name__r   paramsZaxislengthr   r   r   __repr__   s    
zRearrangeMixin.__repr__   maxsizereturnc              
   C   s^   z$t t| j }t| jd|dW S  tyX } ztd| |W Y d }~n
d }~0 0 d S )N	rearrangeZ	operationr    Error while preparing {!r}
 {})tuplesortedr   r   r   r   r   r   r   Zhashable_lengthser   r   r   r   !   s
    zRearrangeMixin.recipec                 C   s   t | j|ddS )Nr#   Zreduction_type)r   r   r   xr   r   r   r   )   s    zRearrangeMixin._apply_reciper   
__module____qualname____doc__r
   r   	functools	lru_cacher   r   r   __classcell__r   r   r   r   r      s   	
r   c                       sJ   e Zd ZdZ fddZdd Zejddedd	d
Z	dd Z
  ZS )ReduceMixinaN  
    Reduce layer behaves identically to einops.reduce operation.

    :param pattern: str, rearrangement pattern
    :param reduction: one of available reductions ('min', 'max', 'sum', 'mean', 'prod'), case-sensitive
    :param axes_lengths: any additional specification of dimensions

    See einops.reduce for source_examples.
    c                    s*   t    || _|| _|| _|  | _d S r   )r	   r
   r   	reductionr   r   r   )r   r   r5   r   r   r   r   r
   8   s
    
zReduceMixin.__init__c                 C   sD   d | j| j}| j D ]\}}|d ||7 }qd | jj|S )Nz
{!r}, {!r}r   r   )r   r   r5   r   r   r   r   r   r   r   r   r   ?   s    zReduceMixin.__repr__r   r   r!   c              
   C   s`   z&t t| j }t| j| j|dW S  tyZ } ztd| |W Y d }~n
d }~0 0 d S )Nr$   r%   )	r&   r'   r   r   r   r   r5   r   r   r(   r   r   r   r   E   s    
zReduceMixin.recipec                 C   s   t | j|| jdS )Nr*   )r   r   r5   r+   r   r   r   r   N   s    zReduceMixin._apply_reciper-   r   r   r   r   r4   -   s   

r4   )
__author__r1   Zeinops.einopsr   Zeinopsr   r    r   r   r4   r   r   r   r   <module>   s   "