Ë
    L²jç9  ã            
      óÖ  — d dl mZ d dlZd dl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 d d
lmZ d dlmZmZmZmZ d dlmZmZmZ d dlmZ erd dlm Z m!Z!  ede"e#e$edf   «      Z% ede"e#e$edf   e&e'eef   e(e   «      Z) G d„ de*«      Z+ G d„ deee%e)f   «      Z, G d„ de,e%e)f   «      Z- G d„ de,e%e)f   «      Z. G d„ de.e%e)f   «      Z/ G d„ de/e%e)f   «      Z0y)é    )ÚannotationsN)ÚABCÚabstractmethod)Úliteral_eval)Úcached_property)Úsha256)Úgetenv)ÚPath)Ú
gettempdir)ÚLock)ÚAnyÚGenericÚTYPE_CHECKINGÚTypeVar)Úassert_neverÚoverrideÚSelf)ÚFileLock)ÚFutureÚThreadPoolExecutorÚKey.ÚValuec                  ó   — e Zd ZdZy)Ú
CacheErrorzJ
    Exception raised for errors encountered during cache operations.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úT/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/_inductor/cache.pyr   r      s   „ òr    r   c                  ó4   — e Zd ZdZedd„«       Zedd„«       Zy)ÚCachezi
    Abstract base class for cache implementations.
    Provides the interface for cache operations.
    c                 ó   — y©z¾
        Retrieve a value from the cache.
        Args:
            key (Key): The key to look up.
        Returns:
            Value | None: The cached value if present, else None.
        Nr   )ÚselfÚkeys     r!   Úgetz	Cache.get*   ó   � r    c                 ó   — y)á  
        Insert a value into the cache.
        Args:
            key (Key): The key to insert.
            value (Value): The value to associate with the key.
        Returns:
            bool: True if the value was inserted, False if the key already exists.
        Nr   ©r&   r'   Úvalues      r!   ÚinsertzCache.insert4   r)   r    N©r&   r   r'   r   ÚreturnzValue | None©r&   r   r'   r   r-   r   r0   Úbool)r   r   r   r   r   r(   r.   r   r    r!   r#   r#   $   s/   „ ñð
 òó ðð òó ñr    r#   c                  óL   — e Zd ZdZdd„Zd	d„Zd
d„Zedd„«       Zedd„«       Z	y)ÚInMemoryCachezL
    In-memory cache implementation using a dictionary and thread lock.
    c                ó0   — i | _         t        «       | _        y)z6
        Initialize an empty in-memory cache.
        N)Ú_cacher   Ú_lock©r&   s    r!   Ú__init__zInMemoryCache.__init__E   s   € ð )+ˆŒÜ›6ˆ�
r    c                ó˜   — | j                   5  | j                  j                  |«      x}�|cddd«       S 	 ddd«       y# 1 sw Y   yxY wr%   )r7   r6   r(   r,   s      r!   r(   zInMemoryCache.getL   sJ   € ð �Z‰Zñ 	ØŸ™Ÿ™¨Ó-Ð-�Ð:Ø÷	ñ 	ð ÷	÷ 	ñ 	ús   �A ¶A Á A	c                ó–   — | j                   5  || j                  v r
	 ddd«       y|| j                  |<   	 ddd«       y# 1 sw Y   yxY w)r+   NFT)r7   r6   r,   s      r!   r.   zInMemoryCache.insertY   sL   € ð �Z‰Zñ 	Ø�d—k‘kÑ!à÷	ð 	ð  %ˆD�K‰K˜ÑØ÷	÷ 	ñ 	ús   �?¦?¿Ac           
     ó¶  —  | «       }t        |«      x}€|S |j                  d«      D ]Ú  }|j                  «       }|sŒ	 |j                  dd«      \  }}|j                  «       |j                  «       }}	 t        |«      }	 t        |«      }		 t        j                  |«      }
	 t        j                  |	«      }|j                  |
|«      rŒ£|j                  |
«      |k7  sŒ¸t	        d|
›d|j                  |
«      ›d|›d�«      ‚ |S # t        $ r}t	        d|›d|›d�«      |‚d}~ww xY w# t        t        f$ r}t	        d|›d	|›d
�«      |‚d}~ww xY w# t        t        f$ r}t	        d|›d	|›d
�«      |‚d}~ww xY w# t        j                  $ r}t	        d|›d	|›d�«      |‚d}~ww xY w# t        j                  $ r}t	        d|›d	|›d�«      |‚d}~ww xY w)av  
        Create an in-memory cache from an environment variable.
        Args:
            env_var (str): Name of the environment variable containing cache data.
        Returns:
            InMemoryCache: An instance populated from the environment variable.
        Raises:
            CacheError: If the environment variable is malformed or contains invalid data.
        Nú;ú,é   zMalformed kv_pair z from env_var z!, likely missing comma separator.zMalformed key_bytes_repr z in kv_pair z, encoding is invalid.zMalformed value_bytes_repr z, not un-pickle-able.zMultiple values for key z found, got z and ú.)r	   ÚsplitÚstripÚ
ValueErrorr   r   ÚSyntaxErrorÚpickleÚloadsÚUnpicklingErrorr.   r(   )ÚclsÚenv_varÚcacheÚenv_valÚkv_pairÚkey_bytes_reprÚvalue_bytes_reprÚerrÚ	key_bytesÚvalue_bytesr'   r-   s               r!   Úfrom_env_varzInMemoryCache.from_env_vari   sI  € ñ “ˆä˜g“Ð&ˆGÐ/àˆLà—}‘} SÓ)ò 9	ˆGà—m‘m“oˆGáàðà3:·=±=ÀÀaÓ3HÑ0�Ð 0ð ×$Ñ$Ó&Ø ×&Ñ&Ó(ð -ˆNð
ä(¨Ó8�	ð
ä*Ð+;Ó<�ðÜ—l‘l 9Ó-�ð
ÜŸ™ [Ó1�ð —L‘L  eÕ,°5·9±9¸S³>ÀUÓ3JÜ Ø.¨s¨g°\À%Ç)Á)ÈCÃ.ÐASÐSXÐY^ÐXaÐabÐcóð ðo9	ðv ˆøôa ò Ü Ø(¨¨°>À'ÀÐLmÐnóàðûðûô ¤Ð,ò Ü Ø/°Ð/AÀÈgÈ[ÐXnÐoóàðûðûô ¤Ð,ò Ü Ø1Ð2BÐ1EÀ\ÐRYÐQ\Ð\rÐsóàðûðûô ×)Ñ)ò Ü Ø/°Ð/AÀÈgÈ[ÐXmÐnóàðûðûô ×)Ñ)ò Ü Ø1Ð2BÐ1EÀ\ÐRYÐQ\Ð\qÐróàðûðúsw   ¿DÁ5D+ÂEÂE?Â#F-Ä	D(ÄD#Ä#D(Ä+EÄ:EÅEÅE<Å$E7Å7E<Å?F*ÆF%Æ%F*Æ-GÇ GÇGc                óX  —  | «       }|j                  «       s|S 	 t        |d«      5 }t        j                  |«      |_        ddd«       t        |j                  t        «      st        d|› d�«      ‚|S # 1 sw Y   Œ4xY w# t        j
                  $ r}t        d|› d�«      |‚d}~ww xY w)a=  
        Create an in-memory cache from a file path.
        Args:
            fpath (Path): Path to the file containing pickled cache data.
        Returns:
            InMemoryCache: An instance populated from the file.
        Raises:
            CacheError: If the file is not a valid pickled dictionary.
        ÚrbNz&Failed to create cache from file path z#, file contents are un-pickle-able.z-, file contents not pickled dict[Key, Value].)	Úis_fileÚopenrE   Úloadr6   rG   r   Ú
isinstanceÚdict)rH   ÚfpathrJ   ÚfprO   s        r!   Úfrom_file_pathzInMemoryCache.from_file_path·   s±   € ñ “ˆà�}‰}ŒàˆLð	Ü�e˜TÓ"ð / bÜ%Ÿ{™{¨2›�”÷/ô ˜%Ÿ,™,¬Ô-ÜØ8¸¸Ð?lÐmóð ð ˆ÷/ð /ûä×%Ñ%ò 	ÜØ8¸¸Ð?bÐcóàðûð	ús.   ›B §A5ÁB Á5A>Á:B ÂB)ÂB$Â$B)N©r&   r   r0   ÚNoner/   r1   )rI   Ústrr0   r   )rZ   r
   r0   r   )
r   r   r   r   r9   r(   r.   ÚclassmethodrR   r\   r   r    r!   r4   r4   @   sA   „ ñó"óóð  òKó ðKðZ òó ñr    r4   c                  óD   — e Zd ZdZ	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 dd„Zy)Ú
AsyncCachezE
    Asynchronous cache implementation using ThreadPoolExecutor.
    c                ó:   — |j                  | j                  |«      S )a  
        Retrieve a value from the cache asynchronously.
        Args:
            key (Key): The key to look up.
            executor (ThreadPoolExecutor): Executor for async execution.
        Returns:
            Future[Value | None]: Future for the cached value or None.
        )Úsubmitr(   )r&   r'   Úexecutors      r!   Ú	get_asynczAsyncCache.get_asyncÝ   s   € ð �‰˜tŸx™x¨Ó-Ð-r    c                ó<   — |j                  | j                  ||«      S )aO  
        Insert a value into the cache asynchronously.
        Args:
            key (Key): The key to insert.
            value (Value): The value to associate with the key.
            executor (ThreadPoolExecutor): Executor for async execution.
        Returns:
            Future[bool]: Future for the result of insertion.
        )rd   r.   )r&   r'   r-   re   s       r!   Úinsert_asynczAsyncCache.insert_asyncê   s   € ð �‰˜tŸ{™{¨C°Ó7Ð7r    N)r&   r   r'   r   re   r   r0   zFuture[Value | None])
r&   r   r'   r   r-   r   re   r   r0   zFuture[bool])r   r   r   r   rf   rh   r   r    r!   rb   rb   Ø   sY   „ ñð.Øð.Øð.Ø(:ð.à	ó.ð8Øð8Øð8Ø%*ð8Ø6Hð8à	ô8r    rb   c                  ó‚   — e Zd ZU dZdZded<   ddd„Zedd„«       Zdd„Z	dd	„Z
edd
„«       Zedd„«       Zedd„«       Zy)ÚOnDiskCachea?  
    On-disk cache implementation using files and file locks.
    Stores cache data in files on disk, with atomic operations and versioning.
    Supports custom cache directory names.
    Attributes:
        version (int): The version used for cache versioning.
        name (str): The name of the cache directory.
    r   ÚintÚversionNc                ó   — |xs d| _         y)zÅ
        Initialize an on-disk cache instance.
        Args:
            name (str | None, optional): The name of the cache directory. If None,
                defaults to "on_disk_cache".
        Úon_disk_cacheN)Úname)r&   ro   s     r!   r9   zOnDiskCache.__init__  s   € ð Ò+˜Oˆ�	r    c                óH   — t        t        «       «      dz  | j                  z  S )z‹
        Get the base directory for the cache.
        Returns:
            Path: The base directory path for storing cache files.
        rJ   )r
   r   ro   r8   s    r!   Úbase_dirzOnDiskCache.base_dir  s   € ô ”J“LÓ! GÑ+¨d¯i©iÑ7Ð7r    c                óÞ   — 	 | j                   t        t        j                  |«      «      j	                  «       dd z  S # t
        t        j                  f$ r}t        d|›d�«      |‚d}~ww xY w)a  
        Get the file path for a given key.
        Args:
            key (Key): The key to convert to a file path.
        Returns:
            Path: The file path for the key.
        Raises:
            CacheError: If the key is not pickle-able.
        Né    zFailed to get fpath for key z, key is not pickle-able.)	rq   r   rE   ÚdumpsÚ	hexdigestÚAttributeErrorÚPicklingErrorr   r   )r&   r'   rO   s      r!   Ú_fpath_from_keyzOnDiskCache._fpath_from_key  sl   € ð	Ø—=‘=¤6¬&¯,©,°sÓ*;Ó#<×#FÑ#FÓ#HÈÈ"Ð#MÑMÐMøÜ¤× 4Ñ 4Ð5ò 	ÜØ.¨s¨gÐ5NÐOóàðûð	ús   ‚;> ¾A,ÁA'Á'A,c                ój   — t        t        |j                  dz  |j                  dd z  «      dz   «      S )z²
        Get a file lock for a given file path.
        Args:
            fpath (Path): The file path.
        Returns:
            FileLock: The file lock for the path.
        ÚlocksNé   z.lock)r   r_   Úparentro   )r&   rZ   s     r!   Ú_flock_from_fpathzOnDiskCache._flock_from_fpath*  s1   € ô œ˜EŸL™L¨7Ñ2°U·Z±ZÀÀ°^ÑCÓDÀwÑNÓOÐOr    c                ó„   — t        t        t        j                  «      j	                  «       «      j                  «       dd S )zŸ
        Get the version prefix for the cache.
        Returns:
            bytes: The version prefix as bytes, derived from the cache version string.
        Nr{   )r   r_   rj   rl   ÚencodeÚdigestr8   s    r!   Úversion_prefixzOnDiskCache.version_prefix9  s3   € ô ”cœ+×-Ñ-Ó.×5Ñ5Ó7Ó8×?Ñ?ÓAÀ"À1ÐEÐEr    c                ó4  — | j                  |«      }| j                  |«      }|5  |j                  «       s
	 ddd«       yd}t        | j                  «      }t        |d«      5 }|j                  |«      | j                  k(  r|j                  «       }ddd«       |€|j                  «        	 ddd«       y	 t        j                  |«      }|cddd«       S # 1 sw Y   ŒFxY w# t        j                  $ r}t        d|›d�«      |‚d}~ww xY w# 1 sw Y   yxY w)aŽ  
        Retrieve a value from the cache.
        Args:
            key (Key): The key to look up.
        Returns:
            Value | None: The cached value if present and version matches, else None.
        Raises:
            CacheError: If the value is corrupted or cannot be unpickled.
        Side Effects:
            Removes stale cache files if the version prefix does not match.
        NrT   zFailed to get key z?, value is potentially corrupted (value is not un-pickle-able).)rx   r}   rU   Úlenr�   rV   ÚreadÚunlinkrE   rF   rG   r   )	r&   r'   rZ   ÚflockrQ   Úprefix_lengthr[   r-   rO   s	            r!   r(   zOnDiskCache.getB  s  € ð ×$Ñ$ SÓ)ˆØ×&Ñ& uÓ-ˆàñ 	Ø—=‘=”?Ø÷	ð 	ð ˆKÜ × 3Ñ 3Ó4ˆMÜ�e˜TÓ"ð , bØ—7‘7˜=Ó)¨T×-@Ñ-@Ò@Ø"$§'¡'£)�K÷,ð Ð"ð —‘”Ø÷	ð 	ð"ÜŸ™ [Ó1�ð ÷1	ñ 	÷,ð ,ûô ×)Ñ)ò Ü Ø(¨¨Ð/nÐoóàðûðú÷'	ð 	úsM   ¥DÁ #DÁ#/CÂDÂ7C#ÃDÃC 	ÃDÃ#DÃ6DÄDÄDÄDc                óÆ  — | j                  |«      }| j                  |«      }|j                  j                  dd¬«       	 |5 }t	        |d«      5 }|j                  | j                  «       t        j                  ||«       ddd«       ddd«       y# 1 sw Y   ŒxY w# 1 sw Y   yxY w# t        j                  $ r}t        d|›d|›d�«      |‚d}~wt        $ r Y yw xY w)	a©  
        Insert a value into the cache.
        Args:
            key (Key): The key to insert.
            value (Value): The value to associate with the key.
        Returns:
            bool: True if the value was inserted, False if the key already exists.
        Raises:
            CacheError: If the value is not pickle-able.
        Side Effects:
            Creates the cache directory if it does not exist.
        T)ÚparentsÚexist_okÚxbNzFailed to insert key z with value z, value is not pickle-able.F)rx   r}   r|   ÚmkdirrV   Úwriter�   rE   Údumprw   r   ÚFileExistsError)r&   r'   r-   rZ   r†   Ú_r[   rO   s           r!   r.   zOnDiskCache.insertl  sè   € ð ×$Ñ$ SÓ)ˆØ×&Ñ& uÓ-ˆØ�‰×Ñ 4°$ÐÔ7ð	ð ð '˜!œT %¨Ó.ð '°"Ø—‘˜×,Ñ,Ô-Ü—‘˜E 2Ô&÷'÷ 'ð ÷'ð 'ú÷ 'ð ûô ×#Ñ#ò 	ÜØ'¨ w¨l¸5¸)ÐC^Ð_óàðûô ò 	Ùð	úsT   ÁB+ ÁBÁ2BÂBÂ
B+ ÂB	ÂBÂB(Â$B+ Â(B+ Â+C Â>CÃC ÃC )N)r&   r   ro   z
str | Noner0   r^   ©r&   r   r0   r
   )r&   r   r'   r   r0   r
   )r&   r   rZ   r
   r0   r   )r&   r   r0   Úbytesr/   r1   )r   r   r   r   rl   Ú__annotations__r9   r   rq   rx   r}   Úpropertyr�   r   r(   r.   r   r    r!   rj   rj   ù   sv   … ñð €GˆSÓô,ð ò8ó ð8óó&Pð òFó ðFð ò'ó ð'ðR òó ñr    rj   c                  ó6   ‡ — e Zd ZdZdˆ fd„Zedd„«       Zˆ xZS )ÚInductorOnDiskCachezt
    Inductor-specific on-disk cache implementation.
    Uses a custom base directory for Inductor cache files.
    c                ó$   •— t         ‰| �  d«       y)zƒ
        Initialize an inductor on-disk cache instance.
        Sets the cache directory name to "inductor_on_disk_cache".
        Úinductor_on_disk_cacheN)Úsuperr9   )r&   Ú	__class__s    €r!   r9   zInductorOnDiskCache.__init__“  s   ø€ ô
 	‰ÑÐ1Õ2r    c                óF   — ddl m} t         |«       d| j                  «      S )z•
        Get the base directory for the Inductor cache.
        Returns:
            Path: The base directory path for Inductor cache files.
        r   )Údefault_cache_dirrJ   )Ú%torch._inductor.runtime.runtime_utilsrœ   r
   ro   )r&   rœ   s     r!   rq   zInductorOnDiskCache.base_dirš  s   € õ 	LäÑ%Ó'¨°$·)±)Ó<Ð<r    r]   r‘   )r   r   r   r   r9   r   rq   Ú__classcell__)rš   s   @r!   r–   r–   �  s!   ø„ ñõ
3ð ò=ó ô=r    r–   )1Ú
__future__r   rE   Úabcr   r   Úastr   Ú	functoolsr   Úhashlibr   Úosr	   Úpathlibr
   Útempfiler   Ú	threadingr   Útypingr   r   r   r   Útyping_extensionsr   r   r   Útorch.utils._filelockr   Úconcurrent.futuresr   r   r_   rk   Útupler   r’   rY   Úlistr   rC   r   r#   r4   rb   rj   r–   r   r    r!   ú<module>r®      sý   ðÝ "ã ß #Ý Ý %Ý Ý Ý Ý Ý ß 7Ó 7ß :Ñ :å *ñ ß=ñ ˆe�S˜#˜u S¨# X™Ó/€Ù�˜˜c 5¨¨c¨¡?°E¸4ÀÀSÀ¹>È4ÐPSÉ9ÓU€ô�ô ôˆC�˜˜e˜Ñ$ô ô8U�E˜#˜u˜*Ñ%ô Uôp8��s˜E�zÑ"ô 8ôBQ�*˜S %˜ZÑ(ô Qôh=˜+ c¨5 jÑ1õ =r    