a
    d
                     @   sV   d Z ddlmZmZ ddlZddlmZmZ G dd dZeej	e
ej	ddd	ZdS )
a  
Specialization of einops for torch.

Unfortunately, torch's jit scripting mechanism isn't strong enough,
and to have scripting supported at least for layers,
a number of changes is required, and this layer helps.

Importantly, whole lib is designed so that you can't use it
    )DictListN)TransformRecipe _reconstruct_from_shape_uncachedc                   @   s   e Zd ZdZeejeee	 dddZ
eee	 dddZeeej dd	d
Zeee	 dddZee	ee	e	f dddZedd Zedd Zeee	 dddZdS )TorchJitBackendz{
    Completely static backend that mimics part of normal backend functionality
    but restricted to torch stuff only
    )x	operationreduced_axesc                 C   s   |dkr| j |dS |dkr(| j|dS |dkr<| j|dS |dkrP| j|dS |dkrtt|d d d D ]}| j|d} qn| S td|d S )	Nmin)ZdimmaxsummeanprodzUnknown reduction )ZaminZamaxr   r   listsortedr   NotImplementedError)r   r   r	   i r   _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/einops/_torch_specific.pyreduce   s    zTorchJitBackend.reduce)axesc                 C   s
   |  |S N)Zpermute)r   r   r   r   r   	transpose(   s    zTorchJitBackend.transposeZtensorsc                 C   s
   t | S r   )torchstackr   r   r   r   stack_on_zeroth_dimension,   s    z)TorchJitBackend.stack_on_zeroth_dimension)repeatsc                 C   s
   |  |S r   )repeat)r   r   r   r   r   tile0   s    zTorchJitBackend.tilen_axespos2lenc                 C   s:   dg| }|  D ]\}}t| |} |||< q| |S )Nr   )itemsr   Z	unsqueezeexpand)r   r"   r#   r   Zaxis_positionZaxis_lengthr   r   r   add_axes4   s
    

zTorchJitBackend.add_axesc                 C   s   | j tjtjtjfv S r   )Zdtyper   float16float32float64r   r   r   r   is_float_type<   s    zTorchJitBackend.is_float_typec                 C   s   | j S r   shaper*   r   r   r   r-   @   s    zTorchJitBackend.shaper,   c                 C   s
   |  |S r   )reshape)r   r-   r   r   r   r.   D   s    zTorchJitBackend.reshapeN)__name__
__module____qualname____doc__staticmethodr   Tensorstrr   intr   r   r   r    r   r&   r+   r-   r.   r   r   r   r   r      s"   

r   )recipetensorreduction_typereturnc           	      C   s   t }t| ||\}}}}}|||}t|dkrF|j|||d}|||}t|dkrz|j|t|t| |d}|||S )Nr   )r   r	   r!   )r   r   r-   r.   lenr   r   r&   )	r7   r8   r9   backendZinit_shapesr	   Zaxes_reorderingZ
added_axesZfinal_shapesr   r   r   apply_for_scriptable_torchJ   s    r=   )r2   typingr   r   r   Zeinops.einopsr   r   r   r4   r5   r=   r   r   r   r   <module>   s
   
9