o
    K+ 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 )ZaminZamaxr   r   listsortedr   NotImplementedError)r   r	   r
   i r   b/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/einops/_torch_specific.pyreduce   s   
zTorchJitBackend.reduceaxesc                 C   
   |  |S N)Z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   Z	unsqueezeexpand)r   r%   r&   r"   Zaxis_positionZ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   )Zdtyper   Zfloat16Zfloat32Zfloat64Zbfloat16r   r   r   r   is_float_type?   s   zTorchJitBackend.is_float_typec                 C   s   | j S r   )shaper*   r   r   r   r,   C   s   zTorchJitBackend.shaper,   c                 C   r   r   )reshape)r   r,   r   r   r   r-   G   r   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	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)r9   r   )r	   r
   )r%   r&   )r   r   r,   r-   r   lenr   r)   )r6   r7   r8   r9   backendZinit_shapesZaxes_reorderingr
   Z
added_axesZfinal_shapesZn_axes_w_addedr   r   r   apply_for_scriptable_torchM   s(   r=   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   r>   Ztorch._dynamor@   ImportErrorwarningswarnImportWarningZeinopsrB   r   r#   rC   ZpackingrD   rE   Z#_ops_were_registered_in_torchdynamo)r@   rB   r   r#   rC   rD   rE   r   r   r   allow_ops_in_compiled_graphf   s"   rK   )r1   rH   typingr   r   r   r   Zeinops.einopsr   r   r   r3   r4   r5   r=   rK   r   r   r   r   <module>   s&    9

