a
    Sic                     @   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 rtdd l} z|  }W n: tyV   z| jj }W n tyP   d}Y n0 Y n0 | j }|sjd}| ||fa t S )Nr   Fcpu)	_CACHED_TF_DEVICE
tensorflowexecuting_eagerlyAttributeErrorcontribeagerin_eager_modetestZgpu_device_name)tfr   device r   Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/opt_einsum/backends/tensorflow.py_get_tensorflow_and_device   s    

r   T)	constantsFc                 C   s   t  \}}}|rTt| tjrP|| || W  d   S 1 sF0    Y  | S t| tjr~|rn|| 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   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   r"   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)r/   expr_r4   r   r1   r   build_expression_graph>   s
    r8   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   r"   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 )NTr9   )r   nextr:   Z
new_constsr   r   r!   \   r"   )r   r,   iterr.   )const_arraysr6   r   r7   new_opsnew_contraction_listr0   r   r?   r   evaluate_constants_graphO   s    rD   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   s   g | ]}t |qS r   r   r:   r   r   r   r!   h   r"   zMbuild_expression_eager.<locals>.tensorflow_eager_contract.<locals>.<listcomp>r	   r#   )r5   numpy)r/   r6   r   r   tensorflow_eager_contractg   s    z9build_expression_eager.<locals>.tensorflow_eager_contractr   )r7   r6   rG   r   rF   r   build_expression_eagerd   s    rH   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   s   g | ]}t |qS r   r   r:   r   r   r   r!   q   r"   z,evaluate_constants_eager.<locals>.<listcomp>r	   Tr<   r   )rA   r6   r   r   r   evaluate_constants_eagerm   s    rI   c                 C   s"   t  \}}}|rtnt}|| |S r=   )r   rH   r8   )r/   r6   r7   r   fnr   r   r   r   w   s    r   c                 C   s"   t  \}}}|rtnt}|| |S r=   )r   rI   rD   )rA   r6   r7   r   rJ   r   r   r   r   }   s    r   )F)__doc__rE   r   sharingr   __all__r   r   r   r8   rD   rH   rI   r   r   r   r   r   r   <module>   s   	
