
    3j                         S r SSKrSSKJrJrJr  SSKrSSKJrJ	r	   " S S5      r
S\S\R                  S	\S
\\\\4      S\R                  4
S jrS r\" 5         g)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                   $   \ rS rSrSr\S\R                  S\S\	\
   4S j5       r\S\	\
   4S j5       r\S	\	\R                     4S
 j5       r\S\	\
   4S j5       r\S\
S\\
\
4   4S j5       r\S 5       r\S 5       r\S\	\
   4S j5       rSrg)TorchJitBackend   zu
Completely static backend that mimics part of normal backend functionality
but restricted to be within torchscript.
x	operationreduced_axesc                     US:X  a  U R                  US9$ US:X  a  U R                  US9$ US:X  a  U R                  US9$ US:X  a  U R                  US9$ US:X  a)  [	        U5      S S S2    H  nU R                  US9n M     U $ [        SU5      e)	Nmin)dimmaxsummeanprodzUnknown reduction )aminamaxr   r   sortedr   NotImplementedError)r   r   r   is       P/home/wildlama/miniconda3/lib/python3.13/site-packages/einops/_torch_specific.pyreduceTorchJitBackend.reduce   s    66l6++%66l6++%55\5**& 66l6++& L)$B$/FFqFM 0H%&:IFF    axesc                 $    U R                  U5      $ N)permute)r   r   s     r   	transposeTorchJitBackend.transpose,   s    yyr   tensorsc                 .    [         R                  " U 5      $ r!   )torchstack)r%   s    r   stack_on_zeroth_dimension)TorchJitBackend.stack_on_zeroth_dimension0   s    {{7##r   repeatsc                 $    U R                  U5      $ r!   )repeat)r   r+   s     r   tileTorchJitBackend.tile4   s    xx  r   n_axespos2lenc                     S/U-  nUR                  5        H  u  pE[        R                  " X5      n XSU'   M!     U R                  U5      $ )Nr   )itemsr'   	unsqueezeexpand)r   r0   r1   r+   axis_positionaxis_lengths         r   add_axesTorchJitBackend.add_axes8   sG    $-*1--/&M1A%0M" +: xx  r   c                     U R                   [        R                  [        R                  [        R                  [        R
                  4;   $ r!   )dtyper'   float16float32float64bfloat16r   s    r   is_float_typeTorchJitBackend.is_float_type@   s*    ww5==%--WWWr   c                     U R                   $ r!   )shaper@   s    r   rD   TorchJitBackend.shapeD   s    wwr   rD   c                 $    U R                  U5      $ r!   )reshape)r   rD   s     r   rG   TorchJitBackend.reshapeH   s    yyr    N)__name__
__module____qualname____firstlineno____doc__staticmethodr'   Tensorstrr   intr   r#   r)   r.   r   r8   rA   rD   rG   __static_attributes__rI   r   r   r	   r	      s   
 G%,, G3 Gd3i G G  49   $4+= $ $ !c ! ! !C !$sCx. ! ! X X    $s)    r   r	   recipetensorreduction_type	axes_dimsreturnc                 F   [         n[        XR                  U5      US9u  nnnnn	n
Ub  UR                  X5      nUb  UR	                  X5      n[        U5      S:  a  UR                  XUS9n[        U5      S:  a  UR                  XUS9nU	b  UR                  X5      nU$ )N)rW   r   )r   r   )r0   r1   )r	   r   rD   rG   r#   lenr   r8   )rT   rU   rV   rW   backendinit_shapesaxes_reorderingr   
added_axesfinal_shapesn_axes_w_addeds              r   apply_for_scriptable_torchra   N   s     G 	)v1FR[\5"""6;
<1|\
:!!&!T6Mr   c                     [        [        S5      (       a  [        R                  S   S:  a  g [        [        S5      (       a  [        R                  S:  a  g  SSKJn   SS	K	J
nJnJnJn  SS
KJnJn  U " U5        U " U5        U " U5        U " U5        U " U5        U " U5        Sqg ! [
         a    [        R                  " S[        SS9   g f = f)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'   rc   torch._dynamore   ImportErrorwarningswarnImportWarningeinopsrh   ri   r   r-   packingrj   rk   #_ops_were_registered_in_torchdynamo)re   rh   ri   r   r-   rj   rk   s          r   allow_ops_in_compiled_graphru   g   s    um$$):):1)=)Cum$$):):e)C0 :9%966646 +/'%  VXers	
 		s   B& &$CC)rN   ro   typingr   r   r   r'   einops.einopsr   r   r	   rP   rQ   rR   ra   ru   rI   r   r   <module>rx      s{   
  $ $  K5  5 r%*\\CFSWX]^acf^fXgSh
\\2/B  r   