o
    °2úh  ã                	   @   sØ  U d Z ddlZddlmZmZmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ ddlmZ dd	lmZ g d
¢ZddddddddœZd/dedededefdd„ZdejiZeeeef ef ed< zddlZejed< ejed< ejed< ejed< ejed< W n	 eyŠ   Y nw d0dedededefd d!„Zi Z eee!f ed"< dede!fd#d$„Z"i Z#eee!f ed%< dede!fd&d'„Z$ej%ej%ej%ej%e
j%d(œZ&ej'ej'ej'ej'e
j'd(œZ(d)d*„ Z%d+d,„ Z'dede!fd-d.„Z)dS )1z¥Handles dispatching array operations to the correct backend library, as well
as converting arrays to backend formats and then potentially storing them as
constants.
é    N)ÚAnyÚDictÚTuple)Úcupy)Újax)Úobject_arrays)Ú
tensorflow)Útheano)Útorch)Úget_funcÚ
has_einsumÚhas_tensordotÚbuild_expressionÚevaluate_constantsÚhas_backendz
dask.arrayztheano.tensorzopt_einsum.backends.torchz	jax.numpyzautograd.numpyzmars.tensor)Údaskr	   r
   r   ÚjaxlibÚautogradÚmarsÚfuncÚbackendÚdefaultÚreturnc                 C   sX   zt  t ||¡¡}|du rt|| ƒW S t|| |ƒW S  ty+   d}t| || ¡ƒ‚w )z½Try and import ``{backend}.{func}``.
    If library is installed and func is found, return the func;
    otherwise if default is provided, return default;
    otherwise raise an error.
    Nz´{} doesn't seem to provide the function {} - see https://optimized-einsum.readthedocs.io/en/latest/backends.html for details on which functions are required for which contractions.)Ú	importlibÚimport_moduleÚ_aliasesÚgetÚgetattrÚAttributeErrorÚformat)r   r   r   ÚlibÚ	error_msg© r"   úW/var/www/html/chatgem/venv/lib/python3.10/site-packages/opt_einsum/backends/dispatch.pyÚ_import_func%   s   "ÿúr$   )ÚeinsumÚobjectÚ_cached_funcs)Ú	tensordotÚnumpy)Ú	transposer)   )r%   r)   )r(   r&   )r*   r&   r)   c                 C   s>   zt | |f W S  ty   t| ||ƒ}|t | |f< | Y S w )ziReturn ``{backend}.{func}``, e.g. ``numpy.einsum``,
    or a default func if provided. Cache result.
    )r'   ÚKeyErrorr$   )r   r   r   Úfnr"   r"   r#   r   J   s   ýr   Ú_has_einsumc                 C   óX   zt |  W S  ty+   ztd| ƒ dt | < W n ty$   dt | < Y nw t |   Y S w )zCCheck if ``{backend}.einsum`` exists, cache result for performance.r%   TF)r-   r+   r   r   ©r   r"   r"   r#   r   Z   ó   

ÿùr   Ú_has_tensordotc                 C   r.   )zFCheck if ``{backend}.tensordot`` exists, cache result for performance.r(   TF)r1   r+   r   r   r/   r"   r"   r#   r   k   r0   r   )r   r	   r   r
   r   c                 C   ó   t |  ||ƒS )zxBuild an expression, based on ``expr`` and initial arrays ``arrays``,
    that evaluates using backend ``backend``.
    )ÚCONVERT_BACKENDS©r   ÚarraysÚexprr"   r"   r#   r   Œ   ó   r   c                 C   r2   )zƒConvert constant arrays to the correct backend, and perform as much of
    the contraction of ``expr`` with these as possible.
    )ÚEVAL_CONSTS_BACKENDSr4   r"   r"   r#   r   “   r7   r   c                 C   s   |   ¡ tv S )zChecks if the backend is known.)Úlowerr3   r/   r"   r"   r#   r   š   s   r   )N)r)   N)*Ú__doc__r   Útypingr   r   r   Úopt_einsum.backendsr   Ú_cupyr   Ú_jaxr   r   Ú_tensorflowr	   Ú_theanor
   Ú_torchÚ__all__r   Ústrr$   Úobject_einsumr'   Ú__annotations__r)   Únpr(   r*   r%   ÚModuleNotFoundErrorr   r-   Úboolr   r1   r   r   r3   r   r8   r   r"   r"   r"   r#   Ú<module>   sf    ùÿ



ÿû	û	