o
    -i*                     @   s   d Z ddlZddlmZmZmZ ddlZddlmZm	Z	 G dd dZ
dedejd	ed
eeeef  dejf
ddZdd Ze  dS )ao  
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 additional moves is needed.

Design of main operations (dynamic resolution by lookup) is unlikely
to be implemented by torch.jit.script,
but torch.compile seems to work with operations just fine.
    N)DictListTuple)TransformRecipe _reconstruct_from_shape_uncachedc                   @   s   e Zd ZdZedejdedee	 fddZ
edee	 fdd	Zed
eej fddZedee	 fddZede	dee	e	f fddZedd Zedd Zedee	 fddZdS )TorchJitBackendz
    Completely static backend that mimics part of normal backend functionality
    but restricted to be within torchscript.
    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r(| j|dS |dkrBtt|d d d D ]}| j|d} q7| S td|)	Nmin)dimmaxsummeanprodzUnknown reduction )aminamaxr   r   listsortedr   NotImplementedError)r   r	   r
   i r   O/home/ubuntu/LTX-2/.venv/lib/python3.10/site-packages/einops/_torch_specific.pyreduce   s   
zTorchJitBackend.reduceaxesc                 C   
   |  |S N)permute)r   r   r   r   r   	transpose+      
zTorchJitBackend.transposetensorsc                 C   s
   t | S r   )torchstack)r!   r   r   r   stack_on_zeroth_dimension/   r    z)TorchJitBackend.stack_on_zeroth_dimensionrepeatsc                 C   r   r   )repeat)r   r%   r   r   r   tile3   r    zTorchJitBackend.tilen_axespos2lenc                 C   s:   dg| }|  D ]\}}t| |} |||< q	| |S )Nr   )itemsr"   	unsqueezeexpand)r   r(   r)   r%   axis_positionaxis_lengthr   r   r   add_axes7   s
   


zTorchJitBackend.add_axesc                 C   s   | j tjtjtjtjfv S r   )dtyper"   float16float32float64bfloat16r   r   r   r   is_float_type?   s   zTorchJitBackend.is_float_typec                 C   s   | j S r   )shaper5   r   r   r   r7   C   s   zTorchJitBackend.shaper7   c                 C   r   r   )reshape)r   r7   r   r   r   r8   G   r    zTorchJitBackend.reshapeN)__name__
__module____qualname____doc__staticmethodr"   Tensorstrr   intr   r   r$   r'   r   r/   r6   r7   r8   r   r   r   r   r      s$    

r   recipetensorreduction_type	axes_dimsreturnc                 C   s   t }t| |||d\}}}}}	}
|d ur|||}|d ur&|||}t|dkr4|j|||d}t|dkrB|j||
|d}|	d urL|||	}|S )N)rD   r   )r	   r
   )r(   r)   )r   r   r7   r8   r   lenr   r/   )rA   rB   rC   rD   backendinit_shapesaxes_reorderingr
   
added_axesfinal_shapesn_axes_w_addedr   r   r   apply_for_scriptable_torchM   s(   rM   c                  C   s   t tdrtjd dk rd S zddlm}  W n ty&   tdt Y d S w ddl	m
}m}m}m} ddlm}m} | | | | | | | | | | | | d	ad S )
N__version__r   2)allow_in_graphzHallow_ops_in_compiled_graph failed to import torch: ensure pytorch >=2.0   )	rearranger   r&   einsum)packunpackT)hasattrr"   rN   torch._dynamorP   ImportErrorwarningswarnImportWarningeinopsrR   r   r&   rS   packingrT   rU   #_ops_were_registered_in_torchdynamo)rP   rR   r   r&   rS   rT   rU   r   r   r   allow_ops_in_compiled_graphf   s"   r_   )r<   rY   typingr   r   r   r"   einops.einopsr   r   r   r>   r?   r@   rM   r_   r   r   r   r   <module>   s&    9

