o
    ڷii                     @   s.  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	m
Z
 d dlmZ d dlmZmZ edddZedddZej Zg d	Zg d
Zg dZdd Zdd Zd]d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"d#d$ Z#d%d& Z$d'd( Z%d)d* Z&d+d, Z'd-d. Z(d/e) fd0e)d1d1d2d3d4fd5e)d6d7fd8e)d1d6d1d9fd:e)d;d7fd<e)d6d1d=fd>e) fd?e)d1d2d=fd@e)d1d6d=fg	Z*dAdB Z+dCdD Z,dEdF Z-dGdH Z.dIdJ Z/dKe) fdLe)d6d;dMdNfdOe)d2d7fdPe) fdQe)d1d2d=fdRe) fgZ0dSdT Z1dUdV Z2dWdX Z3dYdZ Z4d[d\ Z5dS )^    N)EinopsError)_enumerate_directions	rearrangereducerepeat)FLOAT_REDUCTIONS)collect_test_backendsis_backend_testedFsymboliclayersT)...->...za b c d e-> a b c d eza b c d e ...-> ... a b c d eza b c d e ...-> a ... b c d ez... a b c d e -> ... a b c d eza ... e-> a ... eza ... -> a ... za ... c d e -> a (...) c d e))za b c d e -> (a b) c d eza b ... -> (a b) ... )za b c d e -> a b (c d) ez... c d e -> ... (c d) e)za b c d e -> a b c d ez... -> ... )za b c d e -> (a b c d e)... ->  (...))za b c d e -> b (c d e) aza b ... -> b (...) a)za b c d e -> b (a c d) eza b ... e -> b (a ...) e))a b c d e -> z	 ... ->  )za b c d e -> (e a)za ... e -> (e a))za b c d e -> d (a e)z a b c d e ... -> d (a e) )za b c d e -> (a b)z ... c d e  -> (...) c                  C   s   t g d} t| d tt t| d W d    n1 s!w   Y  t| d tt t| d W d    d S 1 sAw   Y  d S )N   r   r   r   r   za b c d ... ->  a b c ... dza b c d (...) ->  a b c ... dr   z(...) -> (...))npzerosr   pytestraisesr   x r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/einops/tests/test_ops.py"test_collapsed_ellipsis_errors_out,   s   

"r   c               
   C   s   t dg d} tD ]}t | t| |sJ |qtD ]\}}t t| |t| |s0J qdD ]}tD ]\}}t t| ||dt| ||dsMJ q7q3g t}tD ]}|	| qUd S )N                 minmaxsum	reduction)
r   arangereshapeidentity_patternsarray_equalr   equivalent_rearrange_patternsequivalent_reduction_patternsr   extend)r   patternpattern1pattern2r'   all_rearrange_patternspattern_pairsr   r   r   test_ellipsis_ops_numpy7   s   &r4   r   c                    s    fdd}||}t j}d|r1fdd|jD }	| j|	d}
||
}| ||
|fg}n|| |}| |}||| dS )z
    Helper to test result of operation (rearrange or transpose) against numpy
    if reduction == 'rearrange', rearrange op is tested, otherwise reduce
    c                    s.   dkrt | fi  S t| fi  S )Nr   )r   r   r   )axes_lengthsr/   r'   r   r   	operationO   s   z)check_op_against_numpy.<locals>.operationg      ?c                    s    g | ]}t   kr|nd qS N)rngrandom).0d)p_none_dimensionr   r   
<listcomp>Y   s     z*check_op_against_numpy.<locals>.<listcomp>shapeN)r   r+   r?   create_symboleval_symbol
from_numpyto_numpy)backendnumpy_inputr/   r5   r'   is_symbolicr6   numpy_resultcheck_equalsymbol_shapesymbolresult_symbolbackend_resultr   )r5   r<   r/   r'   r   check_op_against_numpyI   s   
rM   c                  C   s   t dg d} dD ]8}t|ddD ]/}tttjt  D ]}t	|| |i d|d qdD ]}tjt
 D ]}t	|| |i ||d q5q.qqd	S )
z'Checking various patterns against numpyr   r   )TFFr
   r   )r5   r'   rF   r"   N)r   r(   r)   r   r*   list	itertoolschainr,   rM   r-   )r   rF   rD   r/   r'   r   r   r   test_ellipsis_ops_imperatived   s    rQ   c                  C   s   dd l } ddlm} | jdk rt  tdg d}t	t
tjt  D ]}t||}|| ||}t||d |sCJ q&d S )Nr   	array_api2.0.0r   r   )numpyeinopsrS   __version__r   skipr   r(   r)   r*   rN   rO   rP   r,   r   from_dlpackr+   asnumpy)xpAAr   r/   expectedresultr   r   r   test_rearrange_array_apiu   s   

r_   c               
   C   s   dd l } ddlm} | jdk rt  tdg d}t	j
t D ])}dD ]$}t|||d}|j| |||d}t|t|d |sJJ q&q"d S )Nr   rR   rT   r   r   r"   r&   )rU   rV   rS   rW   r   rX   r   r(   r)   rO   rP   r-   r   rY   r+   rZ   asarray)r[   r\   r   r/   r'   r]   r^   r   r   r   test_reduce_array_api   s   
"ra   c            	      C   sl  g d} t t | | }dD ]}t||}tt ||dks$J |j|jks,J qt|d}t |	 |	 s>J t|d}t ||sKJ t|d}t|d}t ||s]J tt|dd	d
dd}t ||spJ t
td| }t|dfi |}t|d	fi |}t ||sJ t dg d}t|d}|d |d ksJ |d |d ksJ d S )N)r   r   r   r          )za b c d e f -> a b c d e fzb a c d e f -> a b d e f ca b c d e f -> f e d c b aza b c d e f -> (f e) d (c b a)za b c d e f -> (f e d c b a)r   za b c d e f -> a (b) (c d e) fz0a aa aa1 a1a1 aaaa a11 -> a aa aa1 a1a1 aaaa a11rd   zf e d c b a -> a b c d e fza b c d e f -> (f d) c (e b) az(f d) c (e b) a -> a b c d e fr   r    )br;   abcdef   r   r   r   za b c -> b c a)r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r(   prodr)   r   len	setdiff1ddtyper+   flattendictzip)	r?   r   r/   r^   result1result2sizestempx2r   r   r    test_rearrange_consistency_numpy   s.   





ru   c                  C   s  t ddD ]Q} td|  dg|  }t| }ddd t | D }ddd |D }|d | }t||}td	dd| gD ]}|t	| |t	||  ksUJ qCqt ddD ]g} td|  dg|  }t| }dd
d t | d d d D }ddd |d d d D }|d | }t||}|j
|j
ksJ t|}t|D ]\}	}
|||	? d@ |
> O }qt||sJ q\d S )Nr   
   r    c                 s       | ]	}d t | V  qdS iNstrr:   axisr   r   r   	<genexpr>       z4test_rearrange_permutations_numpy.<locals>.<genexpr>c                 s   rx   ry   r{   r}   r   r   r   r      r    -> r   c                 s   rx   ry   r{   r}   r   r   r   r      r   c                 s   rx   ry   r{   r}   r   r   r   r      r   )ranger   r(   r)   r8   permutationjoinr   integerstupler?   
zeros_like	enumerater+   )n_axesinputr   left_expressionright_expression
expressionr^   pickexpected_resultoriginal_axisresult_axisr   r   r   !test_rearrange_permutations_numpy   s.   

"
"

r   c                  C   s  t D ]} td| j tD ]}tjdddg d}|dv r'||d  }di t	|| gd	i t	|| gd
t
dddt	|| gdi t	||dddddddggdi t	||dddddddggdi t	||dddddddggdi t	||ddddddggdt
dd|gg}|D ]'\}}}t| | |fd|i|}| |}t||sJ d| qqqd S )NReduction tests for r   int64rl   r   meanri   float64r   	a ... -> z(a1 a2) ... (e1 e2) -> r   r   )a1e2a b c d e -> (e c) ar   r   r~   r   r   a ... c d e -> (e c) aa b c d e ... -> (e c) aa b c d e -> (e c a)(a a2) ... -> (a2 a) ...a2r'   z
Failed at )imp_op_backendsprintframework_name
REDUCTIONSr   r(   r)   astyper   getattrrn   	transposer   rB   copyrC   allclose)rD   r'   r   
test_casesr/   r5   r   r^   r   r   r   test_reduction_imperatives   s>   """& 
r   c                  C   s:  t D ]} td| j tD ]}tjdddg d}||d  }di t	|| gdi t	|| gd	t
d
d
dt	|| gdi t	||dddd
dddggdi t	||dddd
dddggdi t	||dddd
dddggdi t	||dddd
ddggdt
d
d|gg}|D ]}\}}}|jdd |jD g}|D ]#}| |}	t|	|fd|i|}
| |
|	|fg}t||sJ q	 g }i |}td|jD ]\}}||v r|d  |||< q|| q| |}	t|	|fd|i|}
| |
|	|fg}t||sJ qqqd S )Nr   r   r   r   r   r   r   r   z(a a2) ... (e e2) -> r   )r   r   r   r   r   r   r   r   r   r   r   r   r   c                 S   s   g | ]}d qS r7   r   )r:   _r   r   r   r=     s    z+test_reduction_symbolic.<locals>.<listcomp>r'   Tabcde)sym_op_backendsr   r   r   r   r(   r)   r   r   r   rn   r   r?   r@   r   rA   r   ro   append)rD   r'   r   r   r/   r5   expected_numpy_resultshapesr?   sym
result_symr^   _axes_lengthsr~   lengthr   r   r   test_reduction_symbolic   sZ   

"""&



r   c                  C   sx  t D ]} td| j g tdD ]}d}tj}|dv r d}tj}d}d| jv r)d}d	| jv r0d
}t|D ]}tj	dd|d}t
|}|dkrIdnt	|d }ddd t|D }	ddd ||d  D }
|	d |
 }tjddt| |d|}|dkr||  }t|||d}||}|dkrt||tt|d}|||sJ t| |||i dd q4qqd S )NzStress-testing reduction for r   r   r   r   rc   oneflowrb   paddle	   r   r   sizer   r   rw   c                 s   rx   r   Nr{   r:   rz   r   r   r   r   A  r   z4test_reduction_stress_imperatives.<locals>.<genexpr>c                 s   rx   r   r{   r   r   r   r   r   B  r   ->r   ri   r&   r   F)r'   r5   rF   )r   r   r   r   r   r+   r   r   r8   r   r   r   r(   ri   r)   r   r   r   r   r   rM   )rD   r'   rl   coincidemax_dimr   r?   r   skippedleftrightr/   r   rp   rq   r   r   r   !test_reduction_stress_imperatives/  s@   


 
r   c                  C   s   t dg dd} | |   } dd }dd }dd	 }d
d }ddlm}m}m}m	} |j
||j
||j
||j
|i}	tD ]:}
|
j
|	vrGq?|	|
j
 }|
| }tD ]%\}}td|
j
|| t| ||d}t|||d}t ||
|sxJ qSq?d S )Nr   r   float32c                 S   s
   |  |S r7   )	logsumexp)r   tuple_of_axesr   r   r   logsumexp_torchS  s   
zAtest_reduction_with_callable_imperatives.<locals>.logsumexp_torchc                 S   s   dd l }|| |S Nr   )
tensorflowreduce_logsumexp)r   r   tfr   r   r   logsumexp_tfV  s   z>test_reduction_with_callable_imperatives.<locals>.logsumexp_tfc                 S   s   dd l m  m} || |S r   )tensorflow.keras.backendkerasrD   r   )r   r   kr   r   r   logsumexp_keras[  s   zAtest_reduction_with_callable_imperatives.<locals>.logsumexp_kerasc                 S   sB   |  |}| | j |dd }t|}tj||d}t|| S )NT)keepdimsr   )r$   r   expr%   log)r   r   minusedyr   r   r   logsumexp_numpy`  s
   

zAtest_reduction_with_callable_imperatives.<locals>.logsumexp_numpyr   )NumpyBackendTensorflowBackendTFKerasBackendTorchBackendz!Test reduction with callable for r&   )r   r(   r)   r   r$   einops._backendsr   r   r   r   r   r   rB   r-   r   r   r   rC   )x_numpyr   r   r   r   r   r   r   r   backend2callbackrD   backend_callback	x_backendr0   r1   output_numpyoutput_backendr   r   r   (test_reduction_with_callable_imperativesO  s6   



r   c                     s   t D ]c td j g dgg dg dfD ]O} tt| | }t|}t |}t	|t	|  kr>t	| ksAJ  J  fdd|D }t
||D ]\}}|j|jks[J t||scJ qOqqd S )Nztesting directions forr   )r   r   r   r   r   r    rb   c                       g | ]}  |qS r   )rC   )r:   axrD   r   r   r=         z/test_enumerating_directions.<locals>.<listcomp>)r   r   r   r   r(   ri   r)   r   rB   rj   ro   r?   r   )r?   r   axes1axes2ax1ax2r   r   r   test_enumerating_directions  s   (r   c               	      s   t D ]l td j dD ]a} g dgddgg ddgd g}|D ]Mfddt| D } fdd|D }t|}t|d	}t|d	}t||sLJ t| |sWJ t|d
}t|d
}t| |slJ qqqd S )Nztesting shapes for )r   r   r    r   r   r!   c              	      s(   g | ]}t ||t    qS r   )r   r(   ri   r)   r   r>   r   r   r=     s   ( z4test_concatenations_and_stacking.<locals>.<listcomp>c                    r   r   )rB   )r:   arrayr   r   r   r=     r   r   zb ... -> ... b)	r   r   r   r   r   r`   r   r+   rC   )n_arraysr   arrays1arrays2result0rp   rq   r   )rD   r?   r    test_concatenations_and_stacking  s$   




r   c                  C   s   t D ]y} | dv r	qtddg dd}i }tD ]9}||}t|ds(qt|d| d}t|d	| d}t|d
| dd}t|d| d}|	  |
|j}	|	||j< qtd|  | D ]\}
}| D ]\}}t||syJ |
|dgqhq`qd S )N)anyallr      rh   r   gradza b c -> c ar&   z
c a -> a cza (c1 c2) -> ar   )r'   c1z... -> zcomparing gradients forzprovided different gradients)r   r   r(   r)   r   r   rB   hasattrr   backwardrC   r   r   r   keysitemsr   )r'   r   resultsrD   y0y1y2y3y4r   name1grad1name2grad2r   r   r   test_gradients_imperatives  s.   

r  c                  C   s   t D ];} td| j tjdddg d}g d}|D ] }t||}| |}| ||}| |}t	||s<J qqd S NzTiling tests for    r   r   )r   r   r   r   r    )r   )r   r   r   r   r   )r   r   r   r   r   )
r   r   r   r   r(   r)   tilerB   rC   r+   )rD   r   r   repeatsr]   	convertedrepeatedr^   r   r   r   test_tiling_imperatives  s   

r  c                  C   s   t D ]^} td| j tjdddg d}g d}|D ]C}t||}| |j}| 	| ||||gg}t
||s?J | d gt|j }| 	| ||||gg}t
||s_J qqd S r  )r   r   r   r   r(   r)   r  r@   r?   rA   r+   rj   )rD   r   r   r  r]   r   r^   r   r   r   test_tiling_symbolic  s   r  za b c -> c a bza b c -> (c copy a b)r   r   r    )r   are   cza b c -> (a copy) b c r   r   za b c -> (c a) (copy1 b copy2))r  copy1copy2za ...  -> a ... copyr   z... c -> ... (copy1 c copy2))r  r  z...  -> ... z ...  -> copy1 ... copy2 za b c  -> copy1 a copy2 b c () c           	      K   s|   | d\}}|d | }t| |fi |}t||fddi|}t||fddi|}t| |s4J t| |s<J dS )z*Checks repeat pattern by running reductionr   r'   r#   r$   N)splitr   r   r   r+   )	r   repeat_patternrr   r   r   reduce_patternr  reduced_minreduced_maxr   r   r   check_reversion  s   r  c                  C   sZ   t dg d} t| ddd}t | d  |sJ tD ]\}}t| |fi | qd S )Nr  r   r   r    za b c -> copy a b c r   r  )r   r(   r)   r   r+   repeat_test_casesr  )r   x1r/   axis_dimensionsr   r   r   test_repeat_numpy  s   r$  c                  C   s   t dg d} tD ]3}td|j tD ](\}}t| |fi |}|| }t||fi |}|	|}t 
||s>J qqd S Nr  r   zRepeat tests for )r   r(   r)   r   r   r   r!  r   rB   rC   r+   )r   rD   r/   r#  r]   r  r  r^   r   r   r   test_repeat_imperatives  s   

r&  c                  C   s   t dg d} tD ]6}td|j tD ]+\}}t| |fi |}|| j	}|
t||fi ||| gg}t ||sAJ qqd S r%  )r   r(   r)   r   r   r   r!  r   r@   r?   rA   r+   )r   rD   r/   r#  r]   r   r^   r   r   r   test_repeat_symbolic  s    r'  c                  C   s   dd l } ddlm} | jdk rt  tdg d}t	D ]'\}}t
||fi |}|j
| ||fi |}t||d |sFJ qd S )Nr   rR   rT   r  r   )rU   rV   rS   rW   r   rX   r   r(   r)   r!  r   rY   r+   rZ   )r[   r\   r   r/   r#  r]   r^   r   r   r   test_repeat_array_api   s   
r(  za b c d -> c a d bza b c d -> (c 2 d a b)r!   )r  r  r;   z1 b c d -> (d copy 1) 3 b c z1 ...  -> 3 ... z"() ... d -> 1 (copy1 d copy2) ... z$1 b c d -> (1 1) (1 b) 2 c 3 d (1 1)c                  C   s8   t dg d} tD ]\}}t| |fi | qd S )N0   )r   r   r   r!   )r   r(   r)   test_cases_repeat_anonymousr  )r   r/   r#  r   r   r   test_anonymous_axes<  s   r+  c                  C   s   t dg d} t tt| dt| dsJ t tt| ddt| dds,J t tt| dddt| ddds@J d S )	Nr   r   z... -> (...)za ... e -> (...)r#   z...  -> b (...)r   )re   )r   r(   r)   r+   r   rN   r   r   r   r   r   r   test_list_inputsB  s   
r,  c                  C   s   t dst  dd l} d| jj_dd }| j|ddd}| j|ddd}| jg dd	}| j	||||d
ds;J | j	||||d
dsIJ | jg dd	}| j	||||d
ds_J | j	||||d
dsmJ d S )Ntorchr   Tc                 S   s4   | j ^}}}}t| ddd} t| dd|d d} | S )Nz"(a a2) b c ... -> b (c a2) (a ...)r   r   zb ca2 A -> b Ar%   )ca2)r?   r   r   )r   r  re   r  otherr   r   r   func1[  s   z4test_torch_compile_with_dynamic_shape.<locals>.func1F)dynamic	fullgraph)r   r    r!   r   r   gh㈵>)atol)r!   r   r   r   r   )
r	   r   rX   r-  _dynamoconfigverbosecompilerandnr   )r-  r0  func1_compiled_staticfunc1_compiled_dynamicr   r   r   r   %test_torch_compile_with_dynamic_shapeS  s   
	 r;  c                    s   t  fddtdD S )Nc                 3   s    | ]	} |? d @ V  qdS )r   Nr   r   r   r   r   r   q  r   zbit_count.<locals>.<genexpr>   )r%   r   r   r   r   r   	bit_countp  s   r=  c                  C   s  t dd tdD dgd } tD ]}td|j tdD ]`}t j| |dd}t j| |dd}t 	||r:J t
d	}t
|}t
|}d
||< d|d d| }t|| |dd}	t|| |dd}
t 	|||	suJ t 	|||
sJ q t j| ddd}t j| ddd}d}t|| |dd}	t|| |dd}
t 	|||	sJ t 	|||
sJ qdS )z5Checks that any/all reduction works in all frameworksc                 S   s   g | ]
}t |d  dkqS )r   r   )r=  )r:   r   r   r   r   r=   v  s    z7test_reduction_imperatives_booleans.<locals>.<listcomp>@   r   r!   zReduction any/all tests for T)r~   r   rf   1rw   r   r   r&   r   )r   r   za b ... -> 1 1 ...N)r   r`   r   r)   r   r   r   r   r   r+   rN   r   r   rB   rC   )x_nprD   r~   expected_result_anyexpected_result_allaxesaxes_inaxes_outr/   res_anyres_allr   r   r   #test_reduction_imperatives_booleanst  s0   $rH  )r   F)6rO   rU   r   r   rV   r   einops.einopsr   r   r   r   einops.testsr   r   r   r	   r   r   r9   default_rngr8   r*   r,   r-   r   r4   rM   rQ   r_   ra   ru   r   r   r   r   r   r   r   r  r  r  rn   r!  r  r$  r&  r'  r(  r*  r+  r,  r;  r=  rH  r   r   r   r   <module>   sr    
	
&$5 3	