B
    `;                 @   s4  d dl Zd dlZd dlmZmZmZmZmZ d dl	m
Z
mZmZ yd dlZdZW n ek
rh   dZY nX y d dlZe Zdej_dZW n ek
r   dZY nX y"d dlZdejd< d dlZdZW n ek
r   dZY nX yd dlZdZW n ek
r
   dZY nX yd dlZdZW n ek
r6   dZY nX yd dlZdZ W n ek
rb   dZ Y nX dd	d
dddddgZ!ej"j#e ddej"$de!dd Z%ej"j#e ddej"$dd dhd dhddhgdd Z&ej"j#e ddej"$de!dd Z'ej"j#e ddej"$de!dd Z(ej"j#e ddej"$dd dhd dhddhgd d! Z)ej"j#e ddej"$de!d"d# Z*ej"j#e d$dej"$de!d%d& Z+ej"j#e d$dej"$dd dhd dhddhgd'd( Z,ej"j#e d)dej"$de!d*d+ Z-ej"j#e d)dej"$dd dhd dhddhgd,d- Z.ej"j#e d)dd.d/ Z/ej"j#e  d0dd1d2 Z0ej"$de!d3d4 Z1ej"$de!d5d6 Z2ej"j#e d7dej"$de!d8d9 Z3ej"j#e d7dej"$dd dhd dhddhgd:d; Z4d<d= Z5ej"$de!d>d? Z6dS )@    N)backendscontractcontract_expressionhelperssharing)Shapedinfer_backendparse_backendTFZGNUZMKL_THREADING_LAYERz	ab,bc->cazabc,bcd,deazabc,def->fedcbazabc,bcd,df->fazijk,ikjzi,j->ijz	ijk,k->ijz	AB,BC->CAzTensorflow not installed.)reasonstringc          	   C   s   t | }t| f|ddd}t|}dd |D }t| f|ddi}tjtd}|	  ||d|d	 W d Q R X |
  t||std
d |D }||  d S )NF)optimizeuse_blasc             S   s   g | ]
}|j qS  )shape).0vr   r   M/home/dcms/DCMS/lib/python3.7/site-packages/opt_einsum/tests/test_backends.py
<listcomp>D   s    z#test_tensorflow.<locals>.<listcomp>r   T)config
tensorflow)backendoutc             S   s   g | ]}t |qS r   )r   to_tensorflow)r   viewr   r   r   r   O   s    )r   build_viewsr   npZ
empty_liker   tfSession
_TF_CONFIG
as_defaultcloseallcloseAssertionError)r   viewseinoptshpsexprsessZtensorflow_viewsr   r   r   test_tensorflow=   s    


r)   	constants      c       	   	      s
  d}d}dddh  \} fddt |D tjj||  t|f fdd	td
D  }t|fd i}tjt	d
  |dd}W d Q R X tdd	 |jd D stt||st|dd}t||st|t}t|tjstd S )Nzij,jk,kl->li))r,      )r-      )r.      r   r+   r,   c                s(   g | ] \}}| kr t jj| n|qS r   )r   randomrand)r   ishp)r*   r   r   r   Y   s    z2test_tensorflow_with_constants.<locals>.<listcomp>c             3   s"   | ]}| kr| nV  qd S )Nr   )r   r2   )r*   opsvarr   r   	<genexpr>[   s    z1test_tensorflow_with_constants.<locals>.<genexpr>r-   r*   )r   r   )r   c             s   s"   | ]}|d kpt |dkV  qd S )Nr   )r   )r   arrayr   r   r   r6   b   s   numpy)	enumerater   r0   r1   r   ranger   r   r   r   r   all_evaluated_constantsr"   r!   r   r   
isinstanceTensor)	r*   eqshapes	non_constres_expr'   res_gotres_got2res_got3r   )r*   r4   r5   r   test_tensorflow_with_constantsS   s     $rF   c       
   
   C   s  t | }t| f|ddd}dd |D }t| f|ddi}tjtd}| j t	 V}||dd	i}t
 |kstt|}|d
kst||dd	i}	t||kstW d Q R X W d Q R X tdd | D stt||stt||	std S )NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r   u   s    z0test_tensorflow_with_sharing.<locals>.<listcomp>r   T)r   r   r   r   c             s   s   | ]}t |tjV  qd S )N)r=   r   r>   )r   tr   r   r   r6      s    z/test_tensorflow_with_sharing.<locals>.<genexpr>)r   r   r   r   r   r   r   r   r   shared_intermediatesget_sharing_cacher"   lenr;   valuesr   r!   )
r   r#   r$   r&   r'   r(   cacheZtfl1cache_szZtfl2r   r   r   test_tensorflow_with_sharingo   s    
$rN   zTheano not installed.c             C   s   t | }t| f|ddd}dd |D }t| f|ddi}||ddi}t||s`td	d |D }|| }t|tj	j
std S )
NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r      s    ztest_theano.<locals>.<listcomp>r   Tr   theanoc             S   s   g | ]}t |qS r   )r   	to_theano)r   r   r   r   r   r      s    )r   r   r   r   r   r!   r"   r=   rO   tensorTensorVariable)r   r#   r$   r&   r'   r%   Ztheano_viewsZ
theano_optr   r   r   test_theano   s    
rS   ztheano not installed.c       	         s   d}d}dddh  \} fddt |D tjj||  t|f fdd	td
D  }t|fd i}|dd}tdd	 |jd D st	t
||st	|dd}t
||st	|t}t|tjjst	d S )Nzij,jk,kl->li))r,   r-   )r-   r.   )r.   r/   r   r+   r,   c                s(   g | ] \}}| kr t jj| n|qS r   )r   r0   r1   )r   r2   r3   )r*   r   r   r      s    z.test_theano_with_constants.<locals>.<listcomp>c             3   s"   | ]}| kr| nV  qd S )Nr   )r   r2   )r*   r4   r5   r   r   r6      s    z-test_theano_with_constants.<locals>.<genexpr>r-   r*   rO   )r   c             s   s"   | ]}|d kpt |dkV  qd S )NrO   )r   )r   r7   r   r   r   r6      s    r8   )r9   r   r0   r1   r   r:   r   r;   r<   r"   r!   r   rP   r=   rO   rQ   rR   )	r*   r?   r@   rA   rB   r'   rC   rD   rE   r   )r*   r4   r5   r   test_theano_with_constants   s    $rT   c       	   	   C   s   t | }t| f|ddd}dd |D }t| f|ddi}t V}||ddi}t |ksjtt|}|d	ks~t||ddi}t||kstW d Q R X t	d
d |
 D stt||stt||std S )NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r      s    z,test_theano_with_sharing.<locals>.<listcomp>r   Tr   rO   r   c             s   s   | ]}t |tjjV  qd S )N)r=   rO   rQ   rR   )r   rG   r   r   r   r6      s    z+test_theano_with_sharing.<locals>.<genexpr>)r   r   r   r   r   rH   rI   r"   rJ   r;   rK   r   r!   )	r   r#   r$   r&   r'   rL   Zthn1rM   Zthn2r   r   r   test_theano_with_sharing   s    

rU   zCupy not installed.c             C   s   t | }t| f|ddd}dd |D }t| f|ddi}||ddi}t||s`td	d |D }|| }t|tj	stt|t
|std S )
NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r      s    ztest_cupy.<locals>.<listcomp>r   Tr   cupyc             S   s   g | ]}t |qS r   )r   Zto_cupy)r   r   r   r   r   r      s    )r   r   r   r   r   r!   r"   r=   rV   ndarrayZasnumpy)r   r#   r$   r&   r'   r%   Z
cupy_viewsZcupy_optr   r   r   	test_cupy   s    
rX   c       	         s   d}d}dddh  \} fddt |D tjj||  t|f fdd	td
D  }t|fd i}|dd}tdd	 |jd D st	t
||st	|dd}t
||st	|t}t|tjst	t
|| st	d S )Nzij,jk,kl->li))r,   r-   )r-   r.   )r.   r/   r   r+   r,   c                s(   g | ] \}}| kr t jj| n|qS r   )r   r0   r1   )r   r2   r3   )r*   r   r   r      s    z,test_cupy_with_constants.<locals>.<listcomp>c             3   s"   | ]}| kr| nV  qd S )Nr   )r   r2   )r*   r4   r5   r   r   r6      s    z+test_cupy_with_constants.<locals>.<genexpr>r-   r*   rV   )r   c             s   s"   | ]}|d kpt |dkV  qd S )NrV   )r   )r   r7   r   r   r   r6      s    r8   )r9   r   r0   r1   r   r:   r   r;   r<   r"   r!   rV   Zasarrayr=   rW   get)	r*   r?   r@   rA   rB   r'   rC   rD   rE   r   )r*   r4   r5   r   test_cupy_with_constants   s    $rZ   zjax not installed.c             C   st   t | }t| f|ddd}dd |D }t| f|ddi}||ddi}t||s`tt|tjsptd S )	NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r      s    ztest_jax.<locals>.<listcomp>r   Tr   jax)	r   r   r   r   r   r!   r"   r=   rW   )r   r#   r$   r&   r'   r%   r   r   r   test_jax   s    
r\   c                s   d}d}dddh  \} fddt |D tjj||  t|f fdd	td
D  }t|fd i}|dd}tdd	 |jd D st	t
||st	d S )Nzij,jk,kl->li))r,   r-   )r-   r.   )r.   r/   r   r+   r,   c                s(   g | ] \}}| kr t jj| n|qS r   )r   r0   r1   )r   r2   r3   )r*   r   r   r     s    z+test_jax_with_constants.<locals>.<listcomp>c             3   s"   | ]}| kr| nV  qd S )Nr   )r   r2   )r*   r4   r5   r   r   r6     s    z*test_jax_with_constants.<locals>.<genexpr>r-   r*   r[   )r   c             s   s"   | ]}|d kpt |dkV  qd S )Nr[   )r   )r   r7   r   r   r   r6     s    )r9   r   r0   r1   r   r:   r   r;   r<   r"   r!   )r*   r?   r@   rA   rB   r'   rC   r   )r*   r4   r5   r   test_jax_with_constants  s    $r]   c        
         s   d} d}dd |D }t | f|   | }t }||  }|tj|ddksXttt fdd}||}td	d
 t	||D stdd t	||D }||  }	|	|k std S )Nz
ij,jk,kl->))r,   r-   )r-   r.   )r.   r,   c             S   s   g | ]}t jj| qS r   )r   r0   randn)r   sr   r   r   r      s    z)test_jax_jit_gradient.<locals>.<listcomp>gh㈵>)relc                s    |  S )Nr   )r#   )r'   r   r   <lambda>)      z'test_jax_jit_gradient.<locals>.<lambda>c             s   s   | ]\}}|j |j kV  qd S )N)r   )r   v1v2r   r   r   r6   +  s    z(test_jax_jit_gradient.<locals>.<genexpr>c             S   s   g | ]\}}|d |  qS )gMbP?r   )r   r   dvr   r   r   r   .  s    )
r   r[   ZjititempytestZapproxr"   gradr;   zip)
r?   r@   r#   x0Zjit_exprx1	grad_expr
view_grads	new_viewsZx2r   )r'   r   test_jax_jit_gradient  s    
ro   zautograd not installed.c                 s   d} d}dd |D }t | f|   | }t fdd}||}tdd t||D sbtd	d t||D } | }||k std S )
Nz
ij,jk,kl->))r,   r-   )r-   r.   )r.   r,   c             S   s   g | ]}t jj| qS r   )r   r0   r^   )r   r_   r   r   r   r   7  s    z*test_autograd_gradient.<locals>.<listcomp>c                s    |  S )Nr   )r#   )r'   r   r   ra   <  rb   z(test_autograd_gradient.<locals>.<lambda>c             s   s   | ]\}}|j |j kV  qd S )N)r   )r   rc   rd   r   r   r   r6   >  s    z)test_autograd_gradient.<locals>.<genexpr>c             S   s   g | ]\}}|d |  qS )gMbP?r   )r   r   re   r   r   r   r   A  s    )r   autogradrh   r;   ri   r"   )r?   r@   r#   rj   rl   rm   rn   rk   r   )r'   r   test_autograd_gradient3  s    rq   c                s   t d t| }t| f|ddd}dd |D }t| f|ddi} fdd|D }|| }t| jsvtt	
|t	|stt| f| }t| jstt	
|t	|std S )	Nz
dask.arrayF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r   L  s    ztest_dask.<locals>.<listcomp>r   Tc                s   g | ]} j |d dqS )r,   )chunks)Z
from_array)r   x)dar   r   r   P  s    )rg   importorskipr   r   r   r   r=   Arrayr"   r   r!   r7   )r   r#   r$   r&   r'   Zda_viewsZda_optr   )rt   r   	test_daskF  s    

rw   c       	         s   t d t| }x<|D ]4}tjd tjddg|jdddg}d||< qW t	| f|ddd}d	d
 |D }t
| f|ddi} fdd
|D }|| }t| jstt|| stt	| f| }t| jstt|| std S )Nsparse*   FTg?gffffff?r   )r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r   k  s    ztest_sparse.<locals>.<listcomp>r   c                s   g | ]} j |qS r   )COOZ
from_numpy)r   rs   )rx   r   r   r   o  s    )rg   ru   r   r   r   r0   seedchoicer   r   r   r=   rz   r"   r!   Ztodense)	r   r#   r   maskr$   r&   r'   Zsparse_viewsZ
sparse_optr   )rx   r   test_sparse^  s     


r~   zTorch not installed.c             C   s   t | }t| f|ddd}dd |D }t| f|ddi}||ddi}t||s`td	d |D }|| }t|tj	stt||
  std S )
NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r     s    ztest_torch.<locals>.<listcomp>r   Tr   torchc             S   s   g | ]}t |qS r   )r   to_torch)r   r   r   r   r   r     s    )r   r   r   r   r   r!   r"   r=   r   r>   cpur8   )r   r#   r$   r&   r'   r%   Ztorch_viewsZ	torch_optr   r   r   
test_torch}  s    
r   c       	         s  d}d}dddh  \} fddt |D tjj||  t|f fdd	td
D  }t|fd i}|dd}tdd	 |jd D st	t
||st	|dd}t
||st	|t}t|tjst	|jjdkr| n
|  }t
||st	d S )Nzij,jk,kl->li))r,   r-   )r-   r.   )r.   r/   r   r+   r,   c                s(   g | ] \}}| kr t jj| n|qS r   )r   r0   r1   )r   r2   r3   )r*   r   r   r     s    z-test_torch_with_constants.<locals>.<listcomp>c             3   s"   | ]}| kr| nV  qd S )Nr   )r   r2   )r*   r4   r5   r   r   r6     s    z,test_torch_with_constants.<locals>.<genexpr>r-   r*   r   )r   c             s   s"   | ]}|d kpt |dkV  qd S )Nr   )r   )r   r7   r   r   r   r6     s    r8   r   )r9   r   r0   r1   r   r:   r   r;   r<   r"   r!   r   r   r=   r   r>   Zdevicetyper8   r   )	r*   r?   r@   rA   rB   r'   rC   rD   rE   r   )r*   r4   r5   r   test_torch_with_constants  s     $ r   c              C   s0   t d} t| dkstt| gddks,td S )N)r+   r,   r-   
opt_einsumautor8   )r   r   r"   r	   )rs   r   r   r   +test_auto_backend_custom_array_no_tensordot  s    r   c             C   s   t | }t| f|ddd}|jtks.tdd |D }t| f|ddi}dd |D }t| f|dd	i}|jtkstt||	t
st||dd	i}|jtkstt||	t
std S )
NF)r   r   c             S   s   g | ]
}|j qS r   )r   )r   r   r   r   r   r     s    z.test_object_arrays_backend.<locals>.<listcomp>r   Tc             S   s   g | ]}| tqS r   )astypeobject)r   r   r   r   r   r     s    r   r   )r   r   r   Zdtyper   r"   r   r   r!   r   float)r   r#   r$   r&   r'   Z	obj_viewsZobj_optr   r   r   test_object_arrays_backend  s    
r   )7r8   r   rg   r   r   r   r   r   r   Zopt_einsum.contractr   r   r	   rV   Z
found_cupyImportErrorr   r   ZConfigProtor   Zgpu_optionsZallow_growthZfound_tensorflowosenvironrO   Zfound_theanor   Zfound_torchr[   Z	found_jaxrp   Zfound_autogradtestsmarkZskipifZparametrizer)   rF   rN   rS   rT   rU   rX   rZ   r\   r]   ro   rq   rw   r~   r   r   r   r   r   r   r   r   <module>   s   






*****