a
    Sic                     @   s   d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
 g dZeeZdd Zd	d
 Zdd Zdd Zejd ddZdd Zdd Zdd Zdd Zdd Zdd Zd!ddZdS )"z`
A module for sharing intermediates between contractions.

Copyright (c) 2018 Uber Technologies
    N)Counterdefaultdict   )alpha_canonicalizeparse_einsum_input)currently_sharingget_sharing_cacheshared_intermediatescount_cached_opstranspose_cache_wrapeinsum_cache_wrapto_backend_cache_wrapc                   C   s   t  tv S )zBCheck if we are currently sharing a cache -- thread specific.
    )	threading	get_ident_SHARING_STACK r   r   N/var/www/html/django/DPS/env/lib/python3.9/site-packages/opt_einsum/sharing.pyr      s    r   c                   C   s   t t  d S )z=Return the most recent sharing cache -- thread specific.
    )r   r   r   r   r   r   r   r      s    r   c                 C   s   t t  |  d S N)r   r   r   appendcacher   r   r   _add_sharing_cache#   s    r   c                  C   s&   t  } t|    t|  s"t| = d S r   )r   r   r   pop)tidr   r   r   _remove_sharing_cache'   s    r   c                 c   s2   | du ri } t |  z| V  W t  nt  0 dS )a  Context in which contract intermediate results are shared.

    Note that intermediate computations will not be garbage collected until
    1. this context exits, and
    2. the yielded cache is garbage collected (if it was captured).

    Parameters
    ----------
    cache : dict
        If specified, a user-stored dict in which intermediate results will
        be stored. This can be used to interleave sharing contexts.

    Returns
    -------
    cache : dict
        A dictionary in which sharing results are stored. If ignored,
        sharing results will be garbage collected when this context is
        exited. This dict can be passed to another context to resume
        sharing.
    N)r   r   r   r   r   r   r	   .   s    r	   c                 C   s   t dd |  D S )zrReturns a counter of the types of each op in the cache.
    This is useful for profiling to increase sharing.
    c                 s   s   | ]}|d  V  qdS )r   Nr   ).0keyr   r   r   	<genexpr>Q       z#count_cached_ops.<locals>.<genexpr>)r   keysr   r   r   r   r
   M   s    r
   c                  G   s$   t  }| D ]}||dt|f< q
dS )z{Save tensors in the cache to prevent their ids from being recycled.
    This is needed to prevent false cache lookups.
    tensorN)r   id)tensorsr   r!   r   r   r   _save_tensorsT   s    r$   c                 O   s0   t  }| |v r||  S ||i |}||| < |S )zMemoize ``fn(*args, **kwargs)`` using the given ``key``.
    Results will be stored in the innermost ``cache`` yielded by
    :func:`shared_intermediates`.
    )r   )r   fnargskwargsr   resultr   r   r   _memoize]   s    r)   c                    s   t  d fdd	}|S )zqDecorates a ``transpose()`` implementation to be memoized inside a
    :func:`shared_intermediates` context.
    numpyc                    sF   t  s | ||dS t|  t|}d|t| |f}t| | ||dS )Nbackend	transpose)r   r$   tupler"   r)   )aaxesr,   r   r-   r   r   cached_transposen   s    z.transpose_cache_wrap.<locals>.cached_transpose)r*   	functoolswraps)r-   r2   r   r1   r   r   j   s    
r   c                    s   t  d fdd	}|S )zqDecorates a ``tensordot()`` implementation to be memoized inside a
    :func:`shared_intermediates` context.
       r*   c                    s   t  s | |||dS t| | t|tjrjttt| jt| j| d  ttt|jd | f}t	|d t	|d f}d|t
| t
||f}t| | |||dS )Nr+   r   r   	tensordot)r   r$   
isinstancenumbersNumberlistrangelenshaper.   r"   r)   )xyr0   r,   r   r7   r   r   cached_tensordot   s    
>z.tensordot_cache_wrap.<locals>.cached_tensordot)r6   r*   r3   )r7   rB   r   rA   r   tensordot_cache_wrap|   s    rC   c                    s   t   fdd}|S )zoDecorates an ``einsum()`` implementation to be memoized inside a
    :func:`shared_intermediates` context.
    c                     s   t  s | i |S |dd}| d }t| \}}}|d}t|  tt|tt|dd d}t	dd	 |D }d
d
d	 |D }	t|	d | }
d||
|f}t| |g|R d|iS )Nr,   r*   r   ,c                 S   s   | d S )Nr   r   )r?   r   r   r   <lambda>   r   z:einsum_cache_wrap.<locals>.cached_einsum.<locals>.<lambda>)r   c                 s   s   | ]\}}|V  qd S r   r   )r   _id_r   r   r   r      r   z;einsum_cache_wrap.<locals>.cached_einsum.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S r   r   )r   input_rF   r   r   r   r      r   z->einsum)r   r   r   splitr$   sortedzipmapr"   r.   joinr   r)   )r&   r'   r,   equationinputsoutputoperands	canonicalZcanonical_idsZcanonical_inputsZcanonical_equationr   rI   r   r   cached_einsum   s    
z(einsum_cache_wrap.<locals>.cached_einsumr3   )rI   rU   r   rT   r   r      s    r   Fc                    sN    du rt jt|dS |r4t  d fdd	}nt   fdd}|S )zDecorates an ``to_backend()`` implementation to be memoized inside a
    :func:`shared_intermediates` context (e.g. ``to_cupy``, ``to_torch``).
    N)	constantsFc                    s2   t  s | |dS  jt| |f}t| | |dS )N)constantr   __name__r"   r)   )arrayrW   r   
to_backendr   r   cached_to_backend   s    z0to_backend_cache_wrap.<locals>.cached_to_backendc                    s(   t  s | S  jt| f}t| | S r   rX   )rZ   r   r[   r   r   r]      s    )F)r4   partialr   r5   )r\   rV   r]   r   r[   r   r      s    
r   )N)NF)__doc__
contextlibr4   r9   r   collectionsr   r   parserr   r   __all__r;   r   r   r   r   r   contextmanagerr	   r
   r$   r)   r   rC   r   r   r   r   r   r   <module>   s*   	