
    ^j                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ erd dlmZ d dlmZ  ed      Z e	d	      Z e	d
      Z e       Z ed      ZddZ	 	 	 	 ddZy)    )annotationsN)ThreadPoolExecutor)ConcatenateTYPE_CHECKINGTypeVar)	ParamSpec)Callable)Future_P_T_C   )max_workersc                     t         t        j                         dd fd}dfd}dfd}d fd}||_        ||_        ||_        |S )	z
    Like functools.cache but with a prefetch() method that starts computing
    the value in a background thread, and a set() method for prepopulating.
    Nc                     5   t         ur cd d d        S j                          d  cd d d        S           cd d d        S # 1 sw Y   y xY wN)_cache_sentinelresult_cache_future_lockfuncs   a/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/utils/_functools.pywrapperz#prefetchable_cache.<locals>.wrapper"   sb     	_,	 	 " )	 	 VF	 	 	s   
A	A	A		Ac                ^    5  t         urt        d      | d d d        y # 1 sw Y   y xY w)Nz$prefetchable_cache value already set)r   RuntimeError)valr   r   s    r   set_valz#prefetchable_cache.<locals>.set_val.   s4     	_,"#IJJF	 	 	s   #,c                 D    5  t          d d d d        y # 1 sw Y   y xY wr   )r   )r   r   r   s   r   clearz!prefetchable_cache.<locals>.clear5   s%     	$FG	 	 	s   	c                     5   t         us
	 d d d        y t        j                        d d d        y # 1 sw Y   y xY wr   )r   _prefetch_executorsubmitr   s   r   prefetchz$prefetchable_cache.<locals>.prefetch;   sG     	6_,0C	6 	6 )//5G	6 	6 	6s   77A )returnr   )r   r   r&   None)r&   r'   )r   	threadingLocksetr!   r%   )r   r   r   r!   r%   r   r   r   s   `    @@@r   prefetchable_cacher+      sV    
 *FNNE!%G
 
6 6 GKGMGN    c                f     d j                   z   t        j                         d fd       }|S )a  
    Like `@functools.cache` but for methods.

    `@functools.cache` (and similarly `@functools.lru_cache`) shouldn't be used
    on methods because it caches `self`, keeping it alive
    forever. `@cache_method` ignores `self` so won't keep `self` alive (assuming
    no cycles with `self` in the parameters).

    Footgun warning: This decorator completely ignores self's properties so only
    use it when you know that self is frozen or won't change in a meaningful
    way (such as the wrapped function being pure).
    _cache_method_c                    |rt        d      t        | d       x}si }t        | |       |j                  |t              }|t        ur|S  | g|i |}|||<   |S )Nz.cache_method does not accept keyword arguments)AssertionErrorgetattrsetattrgetr   )selfargskwargscachecached_valuevalue
cache_namefs         r   wrapzcache_method.<locals>.wrapY   sw     !QRR z4888ED*e,yy7.$(((dr,   )r4   r   r5   z_P.argsr6   z	_P.kwargsr&   r   )__name__	functoolswraps)r;   r<   r:   s   ` @r   cache_methodr@   H   s5     "AJJ.J__Q  Kr,   )r   Callable[[], _T]r&   rA   )r;   !Callable[Concatenate[_C, _P], _T]r&   rB   )
__future__r   r>   r(   concurrent.futuresr   typingr   r   r   typing_extensionsr   collections.abcr	   r
   r   r   r   objectr   r#   r+   r@    r,   r   <module>rJ      sq    "   1 6 6 ' () t_T]T] ('A6 ,^(&r,   