ó
    Ä
9j+  ã                  óÐ   • S r SSKJr  SSKrSSKrSSKrSSKrSS jr\" \" S5      5      r	\R                  R                  S5      r " S S\R                  5      r " S	 S
\5      rg)a“  Kernel cache backend infrastructure for CuPy compiler.

This module provides cache backend abstractions for storing compiled kernel
binaries. The cache helps avoid recompiling kernels that have already been
compiled.

.. warning::
   User-defined custom cache backends are experimental and should not expect
   API stability. The interface may change in future releases without
   deprecation warnings.
é    )ÚannotationsNc                óH   • [         R                  " U SS9R                  5       $ )z%Compute SHA1 hash of the given bytes.F)Úusedforsecurity)ÚhashlibÚsha1Ú	hexdigest)Úvalues    ÚS/home/wildlama/miniconda3/lib/python3.13/site-packages/cupy/cuda/_compiler_cache.pyÚ_hash_hexdigestr      s   € ä<Š<˜¨uÑ5×?Ñ?ÓAÐAó    r   z~/.cupy/kernel_cachec                  óh   • \ rS rSrSr\R                  SS j5       r\R                  SS j5       rSr	g)	ÚKernelCacheBackendé   aˆ  Abstract base class for kernel cache storage backends.

This class defines the interface for pluggable cache storage backends.
Subclasses should implement methods to load and save compiled kernel
binaries.

.. warning::
   User-defined custom cache backends are experimental and should not
   expect API stability. The interface may change in future releases
   without deprecation warnings.
c                ó   • [         e)zÛLoad a cached kernel binary.

Args:
    name (str): The cache key (filename) for the compiled kernel.

Returns:
    bytes or None: The cubin binary data (without hash prefix) if
        found and valid, None otherwise.
©ÚNotImplementedError)ÚselfÚnames     r
   ÚloadÚKernelCacheBackend.load+   s
   € ô "Ð!r   c                ó   • [         e)a  Save a compiled kernel binary to cache.

This method may perform I/O asynchronously to avoid blocking
kernel execution.

Args:
    name (str): The cache key (filename) for the compiled kernel.
    cubin (bytes): The compiled kernel binary data.
    source (str): The CUDA source code.
r   )r   r   ÚcubinÚsources       r
   ÚsaveÚKernelCacheBackend.save8   s
   € ô "Ð!r   © N©r   ÚstrÚreturnúbytes | None©r   r   r   Úbytesr   r   r   ÚNone)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚabcÚabstractmethodr   r   Ú__static_attributes__r   r   r
   r   r      s;   † ñ
ð 	×Ñó
"ó ð
"ð 	×Ñó"ó ó"r   r   c                  óX   • \ rS rSrSrSSS jjrSS jrSS jrSS jrSS jr	SS	 jr
S
rg)ÚDiskKernelCacheBackendéG   zoDisk-based kernel cache storage backend.

This backend stores compiled kernel binaries in a directory on disk.
Nc                óR  • Uc$  [         R                  R                  S[        5      nXl        [         R
                  R                  U R                  5      (       d  [         R                  " U R                  SS9  [        [         R                  R                  S5      5      U l	        g)zÂInitialize the disk cache backend.

Args:
    cache_dir (str, optional): Directory to store cache files.
        Defaults to CUPY_CACHE_DIR environment variable or
        ~/.cupy/kernel_cache.
NÚCUPY_CACHE_DIRT)Úexist_okÚCUPY_CACHE_SAVE_CUDA_SOURCE)
ÚosÚenvironÚgetÚ_default_cache_dirÚ
_cache_dirÚpathÚisdirÚmakedirsÚboolÚ_save_cuda_source)r   Ú	cache_dirs     r
   Ú__init__ÚDiskKernelCacheBackend.__init__M   sl   € ð ÑÜŸ
™
Ÿ™Ð'7Ô9KÓLˆIØ#ŒÜw‰w}‰}˜TŸ_™_×-Ñ-ÜKŠK˜Ÿ™°$Ò7Ü!%ÜJ‰JN‰NÐ8Ó9ó";ˆÕr   c                ó<   • [        U5      R                  S5      U-   $ )zÂEncode a cubin binary to the on-disk format (hash prefix + cubin).

Args:
    cubin (bytes): Raw compiled kernel binary data.

Returns:
    bytes: SHA-1 hash (ASCII hex) prepended to the cubin.
Úascii)r   Úencode)r   r   s     r
   Ú_encode_cubinÚ$DiskKernelCacheBackend._encode_cubin]   s   € ô ˜uÓ%×,Ñ,¨WÓ5¸Ñ=Ð=r   c                ó   • [        U5      [        :  a  gUS[         nU[        S nU[        U5      R                  S5      :w  a  gU$ )zÊDecode and validate data in the on-disk format.

Args:
    data (bytes): Raw bytes in the on-disk format (hash + cubin).

Returns:
    bytes or None: The raw cubin if the hash is valid, None otherwise.
NrA   )ÚlenÚ_hash_lengthr   rB   )r   ÚdataÚhash_storedr   s       r
   Ú_decode_cubinÚ$DiskKernelCacheBackend._decode_cubinh   sK   € ô ˆt‹9”|Ó#ØØ˜=œLÐ)ˆØ”\]Ð#ˆØœ/¨%Ó0×7Ñ7¸Ó@Ó@ØØˆr   c                óT  • [         R                  R                  U R                  U5      n[        R
                  " U R                  SS9 nUR                  U5        UR                  nSSS5         [         R                  " WU5        g! , (       d  f       N'= f! [         a     gf = f)au  Atomically write pre-encoded (hash + cubin) data to the cache dir.

Unlike :meth:`save`, this method accepts data that is already in the
on-disk format (i.e. the SHA-1 hash prefix is already prepended).
It does not save a `.cu` source file.

Args:
    name (str): The cache key (filename) for the compiled kernel.
    data (bytes): Pre-encoded bytes (hash prefix + cubin).
F)ÚdirÚdeleteN)
r3   r8   Újoinr7   ÚtempfileÚNamedTemporaryFileÚwriter   ÚreplaceÚPermissionError)r   r   rH   r8   ÚtfÚ	temp_paths         r
   Ú_write_encodedÚ%DiskKernelCacheBackend._write_encodedy   s€   € ô w‰w|‰|˜DŸO™O¨TÓ2ˆÜ×(Ò(Ø—O‘O¨Eò3Ø68ØH‰HTŒNØŸ™ˆI÷3ð	ÜJŠJy $Õ'÷3õ 3ûô ó 	Ùð	ús   Á
B	Á1B Â	
BÂ
B'Â&B'c                ó.  • [         R                  R                  U R                  U5      n[         R                  R	                  U5      (       d  g[        US5       nUR                  5       nSSS5        U R                  W5      $ ! , (       d  f       N= f)zåLoad a cached kernel binary from disk.

Args:
    name (str): The cache key (filename) for the compiled kernel.

Returns:
    bytes or None: The cubin binary data (without hash prefix) if
        found and valid, None otherwise.
NÚrb)r3   r8   rO   r7   ÚexistsÚopenÚreadrJ   )r   r   r8   ÚfilerH   s        r
   r   ÚDiskKernelCacheBackend.loadŽ   sj   € ô w‰w|‰|˜DŸO™O¨TÓ2ˆÜw‰w~‰~˜d×#Ñ#Øä$˜Ô Ø—9‘9“;ˆD÷ ð ×!Ñ! $Ó'Ð'÷ Õús   ÁBÂ
Bc                ó0  • U R                  XR                  U5      5        U R                  (       aT  [        R                  R                  U R                  U5      n[        US-   S5       nUR                  U5        SSS5        gg! , (       d  f       g= f)zÌSave a compiled kernel binary to disk.

Args:
    name (str): The cache key (filename) for the compiled kernel.
    cubin (bytes): The compiled kernel binary data.
    source (str): The CUDA source code.
z.cuÚwN)	rW   rC   r<   r3   r8   rO   r7   r\   rR   )r   r   r   r   r8   Úfs         r
   r   ÚDiskKernelCacheBackend.save¡   so   € ð 	×Ñ˜D×"4Ñ"4°UÓ";Ô<ð ×!×!Ü—7‘7—<‘< §¡°Ó6ˆDÜd˜U‘l CÔ(¨AØ—‘˜”÷ )Ð(ð "ç(Õ(ús   Á+BÂ
B)r7   r<   )N)r=   z
str | None)r   r"   r   r"   )rH   r"   r   r    )r   r   rH   r"   r   r#   r   r!   )r$   r%   r&   r'   r(   r>   rC   rJ   rW   r   r   r+   r   r   r
   r-   r-   G   s%   † ñö
;ô 	>ôô"ô*(÷& r   r-   )r	   r"   r   r   )r(   Ú
__future__r   r)   r   r3   rP   r   rF   rG   r8   Ú
expanduserr6   ÚABCr   r-   r   r   r
   Ú<module>rg      sh   ðñ
õ #ã 
Û Û 	Û ôBñ
 ‘? 3Ó'Ó(€Ø—W‘W×'Ñ'Ð(>Ó?Ð ô&"˜Ÿ™ô &"ôRh Ð/õ h r   