
    Opf                         d Z ddlZddlmZ g dZdad Z ed          dd
            Zd Z	d Z
d Zd Zd Z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                     t           ~dd l} 	 |                                 }nB# t          $ r5 	 | j        j                                        }n# t          $ r d}Y nw xY wY nw xY w| j                                        }|sd}| ||fa t           S )Nr   Fcpu)	_CACHED_TF_DEVICE
tensorflowexecuting_eagerlyAttributeErrorcontribeagerin_eager_modetestgpu_device_name)tfr   devices      ^/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/opt_einsum/backends/tensorflow.py_get_tensorflow_and_devicer      s      	((**EE 	 	 	
(6688!   	 ((** 	F-s2   " 
A!AA!AA!AA! A!T)	constantsFc                    t                      \  }}}|r^t          | t          j                  rB|                    |          5  |                    |           cddd           S # 1 swxY w Y   | S t          | t          j                  r7|r|                    |           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   s        r   r   r   %   s    344B eRZ(( 	36"" 3 3++E223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 %$$ 8 	/''...~~ek5;777Ls   A&&A*-A*c                     t                      \  }}d | D             |                    d          fd}|S )zBBuild a tensorflow function based on ``arrays`` and ``expr``.
    c                 ,    g | ]}t          |          S  r   ).0r    s     r   
<listcomp>z*build_expression_graph.<locals>.<listcomp>C   s     ===UM%((===    r   backendc                                                       }d t          |           D             }|                    |          S )Nc                 :    i | ]\  }}|j         j        d k    ||S )Placeholder)optype)r&   pas      r   
<dictcomp>zGbuild_expression_graph.<locals>.tensorflow_contract.<locals>.<dictcomp>I   s-    ^^^da149P]C]C]QC]C]C]r(   )	feed_dict)get_default_sessionziprun)arrayssessionr3   graphplaceholdersr   s      r   tensorflow_contractz3build_expression_graph.<locals>.tensorflow_contractF   sG    ((**^^c,&?&?^^^	{{5I{666r(   )r   	_contract)r7   expr_r;   r9   r:   r   s       @@@r   build_expression_graphr?   >   sk     *++HB1==f===LNN<N>>E7 7 7 7 7 7 7 r(   c                     t                      \  }}}d | D             }  || ddd\  }}|                                }t          |                    d |D                                 fd|D             }||fS )zConvert constant arguments to tensorflow constants, and perform any
    possible constant contractions. Requires evaluating a tensorflow graph.
    c                 0    g | ]}t          |d           S )Tr!   r%   r&   xs     r   r'   z,evaluate_constants_graph.<locals>.<listcomp>V   s%    JJJM!d333JJJr(   r   Tr*   r   c                     g | ]}||S Nr$   rC   s     r   r'   z,evaluate_constants_graph.<locals>.<listcomp>[   s    "G"G"G1r(   c                 T    g | ]$}|d nt          t                    d          %S )NTrB   )r   next)r&   rD   
new_constss     r   r'   z,evaluate_constants_graph.<locals>.<listcomp>\   s7    fffYZqyttmD4D4Dt&T&T&Tfffr(   )r   r4   iterr6   )const_arraysr=   r   r>   new_opsnew_contraction_listr8   rJ   s          @r   evaluate_constants_graphrO   O   s     *++HB1 KJ\JJJL$(D,ae$f$f$f!G! $$&&Ggkk"G"Gg"G"G"GHHIIJffff^efffG(((r(   c                     fd}|S )zHBuild a eager tensorflow function based on ``arrays`` and ``expr``.
    c                  j                         d | D             d                                          S )Nc                 ,    g | ]}t          |          S r$   r%   rC   s     r   r'   zMbuild_expression_eager.<locals>.tensorflow_eager_contract.<locals>.<listcomp>h   s     @@@A}Q//@@@r(   r   r)   )r<   numpy)r7   r=   s    r   tensorflow_eager_contractz9build_expression_eager.<locals>.tensorflow_eager_contractg   s4    ~~@@@@@,~WW]]___r(   r$   )r>   r=   rT   s    ` r   build_expression_eagerrU   d   s)    ` ` ` ` ` %$r(   c                 (     |d | D             dddS )zoConvert constant arguments to tensorflow_eager arrays, and perform any
    possible constant contractions.
    c                 ,    g | ]}t          |          S r$   r%   rC   s     r   r'   z,evaluate_constants_eager.<locals>.<listcomp>q   s     999q-""999r(   r   TrE   r$   )rL   r=   s     r   evaluate_constants_eagerrX   m   s+     499L999<dhiiiir(   c                 ^    t                      \  }}}|rt          nt          } || |          S rG   )r   rU   r?   )r7   r=   r>   r   fns        r   r   r   w   s6    ,..KAq%#(	D		.DB2fdr(   c                 ^    t                      \  }}}|rt          nt          } || |          S rG   )r   rX   rO   )rL   r=   r>   r   rZ   s        r   r   r   }   s6    ,..KAq%%*	H	!	!0HB2lD!!!r(   )F)__doc__rS   r   sharingr   __all__r
   r   r   r?   rO   rU   rX   r   r   r$   r(   r   <module>r_      s         + + + + + +
E
E
E   . &&&   '&0  ") ) )*% % %j j j  " " " " "r(   