
    )`i                     <   d Z ddlZddlZddlZddlmZ ddlZddlZddlZddl	Z	ddl
mZ ddlmZ ej                            dd          Zd	 Z	 	 	 	 	 d#dedededededefdZdedefdZdededefdZdededefdZd$dededefdZdefd Zi Zd!eddfd"ZdS )%a3  
Copyright (c) 2025 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)urljoin   )logger)FLASHINFER_CUBIN_DIRFLASHINFER_CUBINS_REPOSITORYzWhttps://edge.urm.nvidia.com/artifactory/sw-kernelinferencelibrary-public-generic-local/c                 V    |                      d          s| dz  } t          | |          S )z2Join URLs ensuring base is treated as a directory./)endswithr   )basepaths     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/jit/cubin_loader.pysafe_urljoinr   &   s/    == 4          
      sourcedestinationretriesdelaytimeoutlock_timeoutc                 p   ddl }||                                }| d}t          j        ||          }		 |	5  t	          j        d|            | dt          j                    j         d}
t          j
                            |           r)	 t          j        | |
           t          j        |
|           t	          j        d|            	 t          j
                            |
          rt          j        |
           ddd           d	S # t           $ r`}t	          j        d
|            Y d}~t          j
                            |
          rt          j        |
           ddd           dS d}~ww xY w# t          j
                            |
          rt          j        |
           w w xY wt%          d|dz             D ]}	 |                    | |          }|                                 t+          |
d          5 }|                    |j                   ddd           n# 1 swxY w Y   t          j        |
|           t	          j        d|  d|            	 t          j
                            |
          rt          j        |
            ddd           d	S # |j        j        $ r}t	          j        d|  d| d|            ||k     r8|d|dz
  z  z  }t	          j        d| d           t7          j        |           nYt	          j        d           Y d}~t          j
                            |
          rt          j        |
            ddd           dS Y d}~nd}~ww xY w	 t          j
                            |
          rt          j        |
           # t          j
                            |
          rt          j        |
           w w xY w	 ddd           dS # 1 swxY w Y   dS # t          j        $ r t	          j        d| d| d           Y dS w xY w)a/  
    Downloads a file from a URL or copies from a local path to a destination.
    If the filesystem supports atomic file rename operations, the destination file is
    either written completely or not at all with respect to concurrent access.

    Parameters:
    - source (str): The URL or local file path of the file to download.
    - destination (str): The local file path to save the downloaded/copied file.
    - retries (int): Number of retry attempts for URL downloads (default: 3).
    - delay (int): Initial delay in seconds for exponential backoff (default: 5).
    - timeout (int): Timeout for the HTTP request in seconds (default: 10).
    - lock_timeout (int): Timeout in seconds for the file lock (default: 30).

    Returns:
    - bool: True if download or copy is successful, False otherwise.
    r   Nz.lock)r   zAcquired lock for .z.tmpzFile copied successfully: TzFailed to copy local file: Fr   wbzFile downloaded successfully: z -> zDownloading z
: attempt z	 failed:    zRetrying in z seconds...z%Max retries reached. Download failed.zFailed to acquire lock for z within z	 seconds.)requestsSessionfilelockFileLockr   infouuiduuid4hexosr   existsshutilcopyreplaceremove	Exceptionerrorrangegetraise_for_statusopenwritecontent
exceptionsRequestExceptionwarningtimesleepTimeout)r   r   r   r   r   r   sessionr   	lock_pathlock	temp_patheattemptresponsefilebackoff_delays                   r   download_filerC   -   s   4 OOO""$$%%%IY===D9 2	- 2	-K:[::;;;&??)9???I w~~f%% -
-K	222Jy+666K J[ J JKKK
 w~~i00 -	),,,#2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- ! ! ! !L!Bq!B!BCCC 555w~~i00 -	),,,#2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	-! w~~i00 -	),,,,- !GaK00 - --&{{67{CCH--///i.. 5$

8#34445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Jy+666KRRR[RR     w~~i00 -	),,,e2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	-H  *; % % %NNvNNNN1NN   (((-w{1C(D$M=$M$M$MNNN
=1111%LMMM$uuuw~~i00 -	),,,e2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	-X 21111% 2
 w~~i00 -	),,, w~~i00 -	),,,,-;-)2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	- 2	-h    V+VV|VVV	
 	
 	
 uu	s   P AO:AD3O:P 
E<E73E?73O:*P 7E<<E??6F55O:;J-
H1%J-1H55J-8H592J-,4O: P -M:<A/M5+N3/4O:#P 0N35M::N3>5O:36O))O:-P :O>>P O>P *P54P5checksums_bytesreturnc                     |                      d                                          }|D ]3}|                                                                \  }}d|v r|c S 4t	          d          )zb
    Parse the checksums.txt file and get the hash of corresponding flashinferMetaInfo.h file
    utf-8z.hz4Invalid checksums.txt, no flashinferMetaInfo.h found)decode
splitlinesstripsplit
ValueError)rD   checksums_lineslinesha256filenames        r   get_meta_hashrQ      sx     &,,W55@@BBO  ::<<--//8MMM 
K
L
LLr   
cubin_pathexpected_sha256c                    t          | d          5 }|                                }ddd           n# 1 swxY w Y   t          j        |                                          }||k    rt          j        d| d| d|             dS dS )z<
    Verify the cubin file against the sha256 checksum.
    rbNsha256 mismatch (expected  actual ) for FT)r1   readhashlibrO   	hexdigestr   r6   )rR   rS   fdataactual_sha256s        r   verify_cubinr_      s     
j$		 1vvxx              N4((2244M''ccc-ccWacc	
 	
 	
 u4s   266rO   c           	         t          j        d|             	 t          | d          5 }|                                }t	          j        d          r|cddd           S t          j                    }|                    |           |	                                }||k    r|cddd           S t          j
        d| d| d|             ddd           n# 1 swxY w Y   n# t          $ r Y nw xY wd	S )
z
    Load a cubin from the provide local path and
    ensure that the sha256 signature matches.

    Return None on failure.
    zLoading from rU   )mode"FLASHINFER_CUBIN_CHECKSUM_DISABLEDNrV   rW   rX   r   )r   debugr1   rY   r&   getenvrZ   rO   updater[   r6   r,   )rR   rO   r\   cubinm
actual_shas         r   
load_cubinri      s    L---...*4((( 	AFFHHEy=>> 	 	 	 	 	 	 	 	   AHHUOOOJ##	 	 	 	 	 	 	 	 N[V[[Z[[z[[  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    3sM   C& *CC& !AC$C& 1CC& CC& !C"C& &
C32C3	file_namec                     t          t          | z            }t          ||          }|r|S t          t          |           }t          j        d|  d|            t          |||           t          ||          S )z
    Load a cubin from the local cache directory with {file_name} and
    ensure that the sha256 signature matches.

    If the kernel does not exist in the cache, it will downloaded.

    Returns:
    None on failure.
    zFetching cubin z from )r:   )strr   ri   r   r   r   r"   rC   )rj   rO   r:   rR   rf   uris         r   	get_cubinrn      s     )I566Jz6**E  3Y
?
?C
K8)88388999#z73333j&)))r   r]   c                 *    t          j        |           S N)ctypesc_char_p)r]   s    r   convert_to_ctypes_char_prs      s    ?4   r   dll_pathc                    | t           v rd S t          j        |           t          j        d t          j        t          j                  }dt
          dt
          ffd} ||          }|t           | <                       |           d S )NnamerO   c                     t          |                     d          |                    d                    }                    t          |          t	          j        t          |                               d S )NrG   )rn   rH   FlashInferSetCurrentCubinrs   rq   c_intlen)rv   rO   rf   _LIBs      r   get_cubin_callbackz.setup_cubin_loader.<locals>.get_cubin_callback   si    $++g..g0F0FGG&&$U++V\#e**-E-E	
 	
 	
 	
 	
r   )dll_cubin_handlersrq   CDLL	CFUNCTYPErr   bytesFlashInferSetCubinCallback)rt   CALLBACK_TYPEr|   cbr{   s       @r   setup_cubin_loaderr      s    %%%;x  D $T6?FOLLM
 
 
 
 
 
 
 
 
)	*	*B#%x ##B'''''r   )r   r   r   r   Nrp   )__doc__rq   rZ   r&   urllib.parser   r(   r7   r#   r    corer   envr   environr/   r   r   rl   intrC   r   rQ   boolr_   ri   rn   rs   r}   r    r   r   <module>r      s       				                       % % % % % %  "z~~"]       [ [[[ [ 	[
 [ [ [ [ [|	M5 	MS 	M 	M 	M 	MS 3 4    3      4* * *c *E * * * *,!5 ! ! ! !
  ( ( ( ( ( ( ( (r   