o
    wiV,                     @   sD  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZ d dlmZmZ dZdd Zd	d
 Zdd ZedddZeg dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zg ed dded d dZd!d" eD Zej !d#ed$d% Z"ej !d#ed&d' Z#d(d) Z$d*d+ Z%dS ),    )testmodN)AbstractBackend)	rearrangeparse_shape_optimize_transformation)collect_test_backendsis_backend_testedzAlex Rogozhnikovc                   C   s0   t tjdttdd t tjdttdd d S )NT)np)raise_on_error
extraglobs)r   einopslayersdictnumpy r   r   T/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/einops/tests/test_other.pytest_doctests_examples   s   r   c                  C   s   ddl m}  |  }g }t D ]'}|j|vrqz|  W q ty6 } z||j|f W Y d}~qd}~ww t|dksAJ |dS )z
    This test will fail if some of backends are not installed or can't be imported
    Other tests will just work and only test installed backends.
       )parse_backends_to_testNr   ) r   r   __subclasses__framework_name	Exceptionappendlen)r   backends_to_testerrorsbackend_typeer   r   r   test_backends_installed   s   

r   c                  C   s  t d dd tdD } | dd tdD 7 } | g dg7 } | g dg7 } | D ]}td	D ]}t|}tjjd
d|ddg}|d d  }tjd
|d }ttj|d | }tj|| }tjjd
ddd}	t	||||	 \}
}}}}t
|	|sJ ||j|d|dg}||
j|d|dg}t
||sJ t	| }t||D ]\}}t
||sJ qq.q(d S )NzTesting optimizationsc                 S      g | ]}d g| qS )   r   .0n_dimensionsr   r   r   
<listcomp>,       z7test_optimize_transformations_numpy.<locals>.<listcomp>   c                 S   r    )   r   r"   r   r   r   r%   -   r&      )r!   r(         )r!   r(   r*   r+         r*   r   i   )sizer   i   iM  )axis)printranger   r   randomrandintreshapetuplepermutationr   array_equalsum	transposezip)shapesshapeattemptr$   x
init_shape	n_reducedreduced_axesaxes_reorderingfinal_shapeinit_shape2reduced_axes2axes_reordering2final_shape2combination2result1result2combination3abr   r   r   #test_optimize_transformations_numpy*   s6     rO   Fsymbolicr   
         (   c                  C   s   t D ]=} td| j ttd}t| td}||  kr(tdddddks+J  J |tddddd  kr<|ks?J  J qd S )	NzShape parsing for a b c drS   rT   rU   rV   )rM   rN   cdr   )_IMPERATIVE_BACKENDSr1   r   r   x_np
from_numpyr   backendparsed1parsed2r   r   r   test_parse_shape_imperativeO   s   
(*ra   c                  C   sF   t D ]} ttd}t| td}||  krt ks J  J qd S )Nz_ _ _ _rZ   r   r[   r\   r   r]   r   r   r   test_underscoreX   s
   
 rc   c                  C   sJ   t D ] } ttd}t| td}||  krtddks"J  J qd S )Nz_ _ _ hellorV   )hellorb   r]   r   r   r   test_underscore_one_   s
   
$re   c                  C   sL   t D ]!} ttd}t| td}||  kr tdddks#J  J qd S )Nz_ _ a1 a1a111arU   rV   )a1a1a111arb   r]   r   r   r   test_underscore_severalf   s
   
&rh   c               	   C   s   t tj ttd W d    n1 sw   Y  tD ] } t tj t| td W d    n1 s8w   Y  qd S )Nza a b b)pytestraisesr   EinopsErrorr   r[   rZ   r\   )r^   r   r   r   test_repeatingm   s   rl   c                  C   s   t D ]} ddgdt fdgdtddfddgdtddfg ddtddfg ddtd	dfdgd
tddfddgd
tddfg dd
tddfg dd
tddfg ddtdd	dfdd	gdtdd	dffD ]%\}}}t|}t||}t| ||}||  kr|ksJ  J qgqd S )NrS   rT   ...... arM   rS   rT   rU   rU   rR   rV   a ... a ... brM   rN   rZ   r   r   onesr   r\   r^   r=   patternexpectedr?   r_   r`   r   r   r   test_ellipsisv   s&   

ry   c                  C   s   t D ]E} g ddtddfg ddtddfg ddtddffD ]%\}}}t|}t||}t| ||}||  krC|ksFJ  J q!qd S )	Nr   r!   r(      z1 2 3 ar{   ro   )rS   r   r!   za 1 2rS   za () 2rt   rv   r   r   r   test_parse_with_anonymous_axes   s   

r|   c               
   C   s   t D ]I} g ddfg ddfg ddfg ddfg ddfg ddffD ]&\}}tt t|}t| || W d    n1 sEw   Y  q$qd S )Nrz   za b cz2 a b cza b c ()z	a b c d eza b c d e ...)rZ   ri   rj   RuntimeErrorr   ru   r   r\   )r^   r=   rw   r?   r   r   r   test_failures   s   






r~   Tc                 C   s   g | ]	}|j d kr|qS )ztensorflow.keras)r   )r#   r^   r   r   r   r%      s    r%   r^   c              
   C   s   g dg dg dfD ]o}t d| jd| | |}t|d}i }| D ]\}}t|tr3|n| ||t	g dfg||< q(t | t
|dfi t|dd	d
i}| ||t	g dfg}t |j |jdksrJ t|dszJ qd S )NrR   )rS   rT   NNNNNNz-special shape parsing backend.framework_name=z shape=rW   z*a b (c1 c2) (d1 d2) -> (a b d1) c1 (c2 d2)za b c1 _d2r!   )i  rU   r!   r   )r1   r   create_symbolr   items
isinstanceinteval_symbolr   zerosr   r=   allclose)r^   r=   input_symbolshape_placeholdernamesymbolresult_placeholderresultr   r   r   test_parse_shape_symbolic   s8   


r   c           
      C   s|  ddgd d gdt  fdgd gdt ddfddgd d gdt ddfg dg ddt ddfg d	g d
dt ddfdgd gdt ddfddgd d gdt ddfg dg ddt ddfg d	g d
dt ddfg d	g d
dt dddfddgd d gdt dddffD ]9\}}}}| |}t||}i }| D ]\}}	t|	tr|	||< q| |	|t|fg||< q||ksJ qd S )NrS   rT   rm   rn   ro   rp   )NNNrU   rR   r   rV   rq   rr   rs   z	 a ... b )	r   r   r   r   r   r   r   r   r   )
r^   static_shaper=   rw   rx   r   r   	out_shaper   r   r   r   r   "test_parse_shape_symbolic_ellipsis   s*   



r   c                  C   sv   t ddd} | t ddd7 } | D ](}dD ]#}d|v }tjg d|d}||}|||ks7J |||jfqqd S )NFrP   T)int32int64float32float64float)r(   r{   r*   )dtype)r   r   r   r\   is_float_typer   )backendsr^   r   is_floatinputr   r   r   test_is_float_type   s   
 r   c                     s   t dst  ddl} ddlm} ddlmmmm	m
  ddlm} |  G  fddd|j}| }| j|d	d
d}dD ]%}| ||d |d g}dD ]}|||}	|||}
| |	|
siJ qUqEdS )a7  
    Test ensures that allow_ops_in_compiled_graph allows compiling in a single graph
    Additionally we ensure that after compilation cache works properly
     (by changing shapes and patterns)
    We additionally check that pack/unpack still can be handled
     despite variable number of inputs/outputs
    torchr   N)nn)repeatreducepackunpackeinsum)allow_ops_in_compiled_graphc                       s4   e Zd Zd fddZd	fdd	Z  ZS )
z5test_torch_compile.<locals>.TorchModuleWithOperationsreturnNc                    s   t    d S )N)super__init__)self	__class__r   r   r     s   z>test_torch_compile.<locals>.TorchModuleWithOperations.__init__r   c                    s   |j \}}} fdd}||d}||dd}|gdt   |d\}}	t||d|	d	}
|
d
 t|
 }t||d|d}|||dd
 }|| S )Nc                    s    |   }d fdd|D S )N c                    s$   g | ]}|d  dvr|n|  qS )r/   acdr   )r#   psuffixr   r   r%     s   $ z^test_torch_compile.<locals>.TorchModuleWithOperations.forward.<locals>.suf.<locals>.<listcomp>)splitjoin)rw   partsr   r   r   suf  s   zJtest_torch_compile.<locals>.TorchModuleWithOperations.forward.<locals>.sufza b c -> a b c 4za b c d -> a b cminr!   za b * cza b d c -> (a b ) 1 c dzab one1 c *r   z(a b ) 1 c -> a b c)rN   za b c , a b c d -> d)r=   r   r   )r   x_abcr   rM   rN   rX   r   x_abcdx_abdcpsx_arrayx1additionr   r   r   r   r   r   r   forward
  s    z=test_torch_compile.<locals>.TorchModuleWithOperations.forward)r   N)r   )__name__
__module____qualname__r   r   __classcell__r   r   r   r   TorchModuleWithOperations  s    r   T	aot_eager)	fullgraphr^   )rS   rT   rV   r   r!   )r   suf1other_suffix)r   ri   skipr   r   r   r   r   r   r   r   einops._torch_specificr   Modulecompilerandr   )r   r   r   r   originalcompiledr.   r?   r   rJ   rK   r   r   r   test_torch_compile   s$   

r   )&doctestr   r   ri   r   einops.layerseinops.parsingeinops._backendsr   einops.einopsr   r   r   einops.testsr   r   
__author__r   r   rO   rZ   r   r[   ra   rc   re   rh   rl   ry   r|   r~   _SYMBOLIC_BACKENDSmarkparametrizer   r   r   r   r   r   r   r   <module>   sF     		



