o
    7?e                     @   st   d Z ddlZddlmZ g dZdadd Zedd	dddZdd Z	dd Z
dd Zdd Zdd Zdd ZdS )zQ
Required functions for optimized contractions of numpy arrays using tensorflow.
    N   )to_backend_cache_wrap)to_tensorflowbuild_expressionevaluate_constantsc                  C   sx   t d u r:dd l} z|  }W n ty+   z| jj }W n ty(   d}Y nw Y nw | j }|s5d}| ||fa t S )Nr   Fcpu)	_CACHED_TF_DEVICE
tensorflowZexecuting_eagerlyAttributeErrorcontribeagerZin_eager_modetestZgpu_device_name)tfr   device r   _/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/opt_einsum/backends/tensorflow.py_get_tensorflow_and_device   s"   

r   T)	constantsFc                 C   s   t  \}}}|r*t| tjr(|| || W  d   S 1 s#w   Y  | S t| tjr?|r7|| S || j| jS | S )zDConvert a numpy array to a ``tensorflow.placeholder`` instance.
    N)	r   
isinstancenpZndarrayr   Zconvert_to_tensorplaceholderZdtypeshape)arrayconstantr   r   r   r   r   r   r   %   s    
r   c                    s<   t  \}}dd | D |jdd  fdd}|S )zBBuild a tensorflow function based on ``arrays`` and ``expr``.
    c                 S      g | ]}t |qS r   r   ).0r   r   r   r   
<listcomp>C       z*build_expression_graph.<locals>.<listcomp>r	   backendc                     s*     }dd t| D }|j |dS )Nc                 S   s"   i | ]\}}|j jd kr||qS )ZPlaceholder)optype)r   par   r   r   
<dictcomp>I   s   " zGbuild_expression_graph.<locals>.tensorflow_contract.<locals>.<dictcomp>)	feed_dict)get_default_sessionziprun)arrayssessionr&   graphZplaceholdersr   r   r   tensorflow_contractF   s   z3build_expression_graph.<locals>.tensorflow_contract)r   	_contract)r*   expr_r.   r   r,   r   build_expression_graph>   s
   r2   c                    sh   t  \}}}dd | D } || ddd\}}| }t|dd |D   fdd|D }||fS )zConvert constant arguments to tensorflow constants, and perform any
    possible constant contractions. Requires evaluating a tensorflow graph.
    c                 S   s   g | ]}t |d dqS )Tr   r   r   xr   r   r   r   V       z,evaluate_constants_graph.<locals>.<listcomp>r	   Tr    r   c                 S   s   g | ]}|d ur|qS Nr   r4   r   r   r   r   [   r6   c                    s(   g | ]}|d u r
d nt t ddqS )NTr3   )r   nextr4   Z
new_constsr   r   r   \   s   ( )r   r'   iterr)   )const_arraysr0   r   r1   Znew_opsZnew_contraction_listr+   r   r:   r   evaluate_constants_graphO   s   r=   c                    s    fdd}|S )zHBuild a eager tensorflow function based on ``arrays`` and ``expr``.
    c                     s    j dd | D dd S )Nc                 S   r   r   r   r4   r   r   r   r   h   r   zMbuild_expression_eager.<locals>.tensorflow_eager_contract.<locals>.<listcomp>r	   r   )r/   numpy)r*   r0   r   r   tensorflow_eager_contractg   s   z9build_expression_eager.<locals>.tensorflow_eager_contractr   )r1   r0   r@   r   r?   r   build_expression_eagerd   s   rA   c                 C   s   |dd | D dddS )zoConvert constant arguments to tensorflow_eager arrays, and perform any
    possible constant contractions.
    c                 S   r   r   r   r4   r   r   r   r   q   r   z,evaluate_constants_eager.<locals>.<listcomp>r	   Tr7   r   )r<   r0   r   r   r   evaluate_constants_eagerm   s   rB   c                 C   "   t  \}}}|r
tnt}|| |S r8   )r   rA   r2   )r*   r0   r1   r   fnr   r   r   r   w      
r   c                 C   rC   r8   )r   rB   r=   )r<   r0   r1   r   rD   r   r   r   r   }   rE   r   )F)__doc__r>   r   Zsharingr   __all__r   r   r   r2   r=   rA   rB   r   r   r   r   r   r   <module>   s    	
