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r@t|d d d D ]}| j|d} q5| S td|)	Nmin)dimmaxsummeanprodzUnknown reduction )aminamaxr   r   sortedr   NotImplementedError)r   r	   r
   i r   L/home/ubuntu/vllm_env/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_dimension0   r   z)TorchJitBackend.stack_on_zeroth_dimensionrepeatsc                 C   r   r   )repeat)r   r$   r   r   r   tile4   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_axes8   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   )shaper4   r   r   r   r6   D   s   zTorchJitBackend.shaper6   c                 C   r   r   )reshape)r   r6   r   r   r   r7   H   r   zTorchJitBackend.reshapeN)__name__
__module____qualname____doc__staticmethodr!   Tensorstrr   intr   r   r#   r&   r   r.   r5   r6   r7   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)rC   r   )r	   r
   )r'   r(   )r   r   r6   r7   r   lenr   r.   )r@   rA   rB   rC   backendinit_shapesaxes_reorderingr
   
added_axesfinal_shapesn_axes_w_addedr   r   r   apply_for_scriptable_torchN   s(   rL   c                  C   s   t tdrtjd dk rd S t tdrtjdkrd S zddlm}  W n ty4   tjdtdd Y d S w dd	l	m
}m}m}m} dd
lm}m} | | | | | | | | | | | | dad S )N__version__r   2z2.8)allow_in_graphzHallow_ops_in_compiled_graph failed to import torch: ensure pytorch >=2.0   )
stacklevel)einsum	rearranger   r%   )packunpackT)hasattrr!   rM   torch._dynamorO   ImportErrorwarningswarnImportWarningeinopsrR   rS   r   r%   packingrT   rU   #_ops_were_registered_in_torchdynamo)rO   rR   rS   r   r%   rT   rU   r   r   r   allow_ops_in_compiled_graphg   s*   r_   )r;   rY   typingr   r   r   r!   einops.einopsr   r   r   r=   r>   r?   rL   r_   r   r   r   r   <module>   s&    9

!