o
    i 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
tensorflowexecuting_eagerlyAttributeErrorcontribeagerin_eager_modetestgpu_device_name)tfr   device r   M/var/www/myenv/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npndarrayr   convert_to_tensorplaceholder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 )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-   graphplaceholdersr   r   r   tensorflow_contractF   s   z3build_expression_graph.<locals>.tensorflow_contract)r   	_contract)r1   expr_r6   r   r3   r   build_expression_graph>   s
   r:   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   r<   r   r   r   r#   [   r>   c                    s(   g | ]}|d u r
d nt t ddqS )NTr;   )r   nextr<   
new_constsr   r   r#   \   s   ( )r   r.   iterr0   )const_arraysr8   r   r9   new_opsnew_contraction_listr2   r   rB   r   evaluate_constants_graphO   s   rH   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!   r<   r   r   r   r#   h   r$   zMbuild_expression_eager.<locals>.tensorflow_eager_contract.<locals>.<listcomp>r	   r%   )r7   numpy)r1   r8   r   r   tensorflow_eager_contractg   s   z9build_expression_eager.<locals>.tensorflow_eager_contractr   )r9   r8   rK   r   rJ   r   build_expression_eagerd   s   rL   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!   r<   r   r   r   r#   q   r$   z,evaluate_constants_eager.<locals>.<listcomp>r	   Tr?   r   )rE   r8   r   r   r   evaluate_constants_eagerm   s   rM   c                 C   "   t  \}}}|r
tnt}|| |S r@   )r   rL   r:   )r1   r8   r9   r   fnr   r   r   r   w      
r   c                 C   rN   r@   )r   rM   rH   )rE   r8   r9   r   rO   r   r   r   r   }   rP   r   )F)__doc__rI   r   sharingr   __all__r   r   r   r:   rH   rL   rM   r   r   r   r   r   r   <module>   s    	
