
    i                         S r SSKrSSKrSSKrSSKrSSKJrJr  SSKJ	r	J
r
  / SQr\" \5      rS rS rS	 rS
 r\R$                  SS j5       rS rS rS rS rS rS rSS jrg)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                  :    [         R                  " 5       [        ;   $ )zBCheck if we are currently sharing a cache -- thread specific.
    )	threading	get_ident_SHARING_STACK     R/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/opt_einsum/sharing.pyr   r      s      N22r   c                  @    [         [        R                  " 5          S   $ )z=Return the most recent sharing cache -- thread specific.
    )r   r   r   r   r   r   r	   r	      s     )--/044r   c                 Z    [         [        R                  " 5          R                  U 5        g N)r   r   r   appendcaches    r   _add_sharing_cacher   #   s    9&&()007r   c                      [         R                  " 5       n [        U    R                  5         [        U    (       d  [        U 	 g g r   )r   r   r   pop)tids    r   _remove_sharing_cacher!   '   s6    



C3#3 r   c              #   h   #    U c  0 n [        U 5         U v   [        5         g! [        5         f = f7f)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   s    r   r
   r
   .   s0     , }u s   2# 2/2c                 B    [        S U R                  5        5       5      $ )zjReturns a counter of the types of each op in the cache.
This is useful for profiling to increase sharing.
c              3   *   #    U  H	  oS    v   M     g7f)r   Nr   ).0keys     r   	<genexpr>#count_cached_ops.<locals>.<genexpr>Q   s     2\cq6\   )r   keysr   s    r   r   r   M   s     2UZZ\222r   c                  H    [        5       nU  H  nX!S[        U5      4'   M     g)zsSave tensors in the cache to prevent their ids from being recycled.
This is needed to prevent false cache lookups.
tensorN)r	   id)tensorsr   r,   s      r   _save_tensorsr/   T   s'     E&,h6
"# r   c                 D    [        5       nX;   a  X@   $ U" U0 UD6nXTU '   U$ )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   results         r   _memoizer5   ]   s4    
 E
|z  F#JMr   c                 J   ^  [         R                  " T 5      SU 4S jj5       nU$ )ziDecorates a ``transpose()`` implementation to be memoized inside a
:func:`shared_intermediates` context.
c                    > [        5       (       d  T" XUS9$ [        U 5        [        U5      nSU[        U 5      U4n[	        UTXUS9$ )Nbackend	transpose)r   r/   tupler-   r5   )aaxesr9   r&   r:   s       r   cached_transpose.transpose_cache_wrap.<locals>.cached_transposen   sO     ""Qg66 	aT{7BqE4/YAAr   )numpy	functoolswraps)r:   r>   s   ` r   r   r   j   s*     __YB  B r   c                 J   ^  [         R                  " T 5      SU 4S jj5       nU$ )ziDecorates a ``tensordot()`` implementation to be memoized inside a
:func:`shared_intermediates` context.
c           	        > [        5       (       d  T" XX#S9$ [        X5        [        U[        R                  5      (       aj  [        [        [        U R                  5      5      5      [        U R                  5      U-
  S  [        [        [        UR                  5      5      5      S U 4n[        US   5      [        US   5      4nSU[        U 5      [        U5      U4n[        UTXX#S9$ )Nr8   r   r   	tensordot)r   r/   
isinstancenumbersNumberlistrangelenshaper;   r-   r5   )xyr=   r9   r&   rF   s        r   cached_tensordot.tensordot_cache_wrap.<locals>.cached_tensordot   s     ""Q499 	adGNN++c!''l+,S\D-@-ABDsSTSZSZ|I\D]^c_cDddDT!W~uT!W~-7BqE2a5$6YdDDr   )   r@   rA   )rF   rP   s   ` r   tensordot_cache_wraprS   |   s*     __Y
E  
E r   c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )zgDecorates an ``einsum()`` implementation to be memoized inside a
:func:`shared_intermediates` context.
c            	        > [        5       (       d  T" U 0 UD6$ UR                  SS5      nU S   n[        U 5      u  pEnUR                  S5      n[	        U6   [        [        U[        [        U5      5      S S9n[        S U 5       5      nSR                  S U 5       5      n	[        U	S	-   U-   5      n
S
X*U4n[        UTU/UQ7SU06$ )Nr9   r@   r   ,c                     U S   $ )Nr   r   )rN   s    r   <lambda>:einsum_cache_wrap.<locals>.cached_einsum.<locals>.<lambda>   s    1r   )r&   c              3   *   #    U  H	  u  pUv   M     g 7fr   r   )r%   _id_s      r   r'   ;einsum_cache_wrap.<locals>.cached_einsum.<locals>.<genexpr>   s     :	fac	r)   c              3   *   #    U  H	  u  pUv   M     g 7fr   r   )r%   input_r[   s      r   r'   r]      s     #FIyvFIr)   z->einsum)r   r   r   splitr/   sortedzipmapr-   r;   joinr   r5   )r2   r3   r9   equationinputsoutputoperands	canonicalcanonical_idscanonical_inputscanonical_equationr&   r`   s               r   cached_einsum(einsum_cache_wrap.<locals>.cached_einsum   s     ""4*6** **Y07#5d#; c"x  3vs2x'89~N	:	::88#FI#FF/0@40G&0PQ]BVXJJ'JJr   rA   )r`   rn   s   ` r   r   r      s*     __VK K* r   c                    ^  T c  [         R                  " [        US9$ U(       a#  [         R                  " T 5      SU 4S jj5       nU$ [         R                  " T 5      U 4S j5       nU$ )zDecorates an ``to_backend()`` implementation to be memoized inside a
:func:`shared_intermediates` context (e.g. ``to_cupy``, ``to_torch``).
)	constantsc                 t   > [        5       (       d  T" XS9$ TR                  [        U 5      U4n[        UTXS9$ )N)constantr   __name__r-   r5   )arrayrs   r&   
to_backends      r   cached_to_backend0to_backend_cache_wrap.<locals>.cached_to_backend   s>    $&&!%;; %%r%y(:CCUFFr   c                 z   > [        5       (       d  T" U 5      $ TR                  [        U 5      4n[        UTU 5      $ r   rt   )rv   r&   rw   s     r   rx   ry      s<    $&&!%(( %%r%y0CCU33r   )F)rB   partialr   rC   )rw   rq   rx   s   `  r   r   r      sl    
   !6)LL		$	G 
%	G&  
	$	4 
%	4 r   r   )NF)__doc__
contextlibrB   rH   r   collectionsr   r   parserr   r   __all__rJ   r   r   r	   r   r!   contextmanagerr
   r   r/   r5   r   rS   r   r   r   r   r   <module>r      s        , :
 T"358     <3-
$(:r   