
    )`i(#                     ~   d 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m	Z	 ddl
mZ ddlZddlZ ej        d          Ze                     ej        dd                                                     ej        s"e                     ej                               d	d
lmZmZmZmZmZ ddlmZ ededefd            Z	 d$dede de de de!edf         f
dZ" ed           G d d                      Z# G d d          Z$d Z%dee!eef         ddf         fd Z&d%d!Z'de!e!ee(f         df         fd"Z)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.
    )	dataclassN)ThreadPoolExecutoras_completed)	Generatorzflashinfer.artifactsFLASHINFER_LOGGING_LEVELINFO   )FLASHINFER_CUBINS_REPOSITORYsafe_urljoinFLASHINFER_CUBIN_DIRdownload_fileverify_cubin)contextmanagerkeyvaluec              #   F  K   t           j                            | d           }|t           j        | <   	 d V  |"t           j                            | d            d S |t           j        | <   d S # |!t           j                            | d            n|t           j        | <   w xY wN)osenvirongetpop)r   r   	old_values      h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/artifacts.pytemp_env_varr   -   s      
sD))IBJsO(JNN3%%%%%'BJsOOO JNN3%%%%'BJsO''''s   A, ,4B       
   sourceretriesdelaytimeoutreturn.c           
      4   t          d|dz             D ]}	 t          j        | |          }|                                 t	          j        d|j                  }t          d |D                       c S # t          j        j	        $ re}t                              d|  d| d|            ||k     r2t                              d| d	           t          j        |           Y d }~d }~ww xY wt                              d
           t                      S )Nr	   )r!   z\<a href=".*\.cubin">c              3   0   K   | ]}|d d         dz   V  dS )	   iz.cubinN ).0hs     r   	<genexpr>z,get_available_cubin_files.<locals>.<genexpr>B   s/      ==!!AbD'H,======    zFetching available files z
: attempt z	 failed: zRetrying in z seconds...z"Max retries reached. Fetch failed.)rangerequestsr   raise_for_statusrefindalltexttuple
exceptionsRequestExceptionloggerwarninginfotimesleeperror)r   r   r    r!   attemptresponsehrefses           r   get_available_cubin_filesr>   :   s8    GaK(( " "	"|FG<<<H%%'''J7GGE==u========"3 	" 	" 	"NNSFSSgSSPQSS     =5===>>>
5!!!	" LL566677Ns   AA66C/
AC**C/T)frozenc                   Z    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed	<   d
Z
eed<   dS )ArtifactPatha   
    This class is used to store the paths of the cubin files in artifactory.
    The paths are generated in cubin publishing script logs (accessible by codeowners).
    When compiling new cubins for backend directories, update the corresponding path.
    z975d477a640f268ea9ad117cc596eb39245713b9e/fmha/trtllm-gen/TRTLLM_GEN_FMHAzEccae3ed120a12a2c6922b458086b460413dbf731/batched_gemm-0d275a2-9936841TRTLLM_GEN_BMMz=1fddc48b7b48af33914d040051b3e2ee9ba4701e/gemm-145d1b1-9b113e3TRTLLM_GEN_GEMMz4a72d85b019dc125b9f711300cb989430f762f5a6/fmha/cudnn/
CUDNN_SDPAz3a72d85b019dc125b9f711300cb989430f762f5a6/deep-gemm/DEEPGEMMN)__name__
__module____qualname____doc__rB   str__annotations__rC   rD   rE   rF   r&   r*   r   rA   rA   R   s           WOSVVVO C    	H S    MJLLLIHcIIIIIr*   rA   c            
           e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	eed	<    e
ej        d
          e e
ej        d
          e e
ej        d
          e e
ej	        d
          e	iZeeef         ed<   dS )CheckSumHasha  
    This class is used to store the checksums of the cubin files in artifactory.
    The sha256 hashes are generated in cubin publishing script logs (accessible by codeowners).
    When updating the ArtifactPath for backend directories, update the corresponding hash.
    @e014d7a54c396733ef012b223603c1be2861019f88faa5dcc882ed1ecfe5c2d9rB   @b7689d3046493806251351c2744c6d7faed6af25518647a955b35c4919b014fcrC   @1a2a166839042dbd2a57f48051c82cd1ad032815927c753db269a4ed10d0ffbfrF   @15cb8c85dfb5eddd4f121d64cb5a718321fb55b85aa19df10ddc1329d4a726b9rD   checksums.txtmap_checksumsN)rG   rH   rI   rJ   rB   rK   rL   rC   rF   rD   r   rA   rT   dictr&   r*   r   rN   rN   f   s           	K S    	K C    WHcVVVJ S    	\1?CC_\0/BBN\*O<<h\1?CC_	%M4S>     r*   rN   c                    i }| D ]}t          t          t          |d                    }t          t          |d          z  }t          ||           t	          |d          5 }|D ]D}|                                                                \  }}d|v rt          ||          }|||<   E	 d d d            n# 1 swxY w Y   |S )NrS   rz.h)r   r
   r   r   openstripsplit)	subdirs	checksumssubdirurichecksum_pathflinesha256filenames	            r   get_checksumsrd      s    I - -(,v*O*O
 
 -|FO/T/TTc=)))-%% 	- - -#'::<<#5#5#7#7  8##+FH==H&,	(##-	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- s   "AB77B;	>B;	c               #     K   t           } t          j        t          j        t          j        t          j        g}t          |          }t          t          j        d          |t          t          j        d                   fV  t          t          j        d          |t          t          j        d                   fV  t          t          j        d          |t          t          j        d                   fV  |D ]b}t          |d          }|t          j	        |         fV  t          t          | |                    D ]}t          ||          ||         fV  cd S )Nzinclude/flashInferMetaInfo.hzinclude/flashinferMetaInfo.hrS   )r
   rA   rB   rC   rD   rF   rd   r   rN   rT   r>   )base
cubin_dirsr\   	cubin_dirr_   names         r   get_subdir_file_listrj      s     'D 	$#$	J j))I 	\13QRR57UVV	
    	\13QRR57UVV	
    	\02PQQ46TUU	
      C C	$Y@@l8GHHHH-l4.K.KLL 	C 	CD	400)D/BBBBB	CC Cr*   c            
         ddl m}  t          j                    }t	          t                                }t          t          j        	                    dd                    } | t          |          d          5 dfd}t          |          5 }g }|D ]\  }}t          t          |          }	t          |z  }
|
j                            d	d	
           |                    t$          |	t'          |
          |          }|                    |           |                    |           d t-          |          D             }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t/          |          }|st1          d          |D ];\  }}t          |z  }
t3          t'          |
          |          st1          d          <d S )Nr   )tqdm_logging_redirect!FLASHINFER_CUBIN_DOWNLOAD_THREADS4zDownloading cubins)totaldescr"   c                 2                         d           d S )Nr	   )update)_pbars    r   update_pbar_cbz*download_artifacts.<locals>.update_pbar_cb   s    KKNNNNNr*   T)parentsexist_ok)sessionc                 6    g | ]}|                                 S r&   )result)r'   futs     r   
<listcomp>z&download_artifacts.<locals>.<listcomp>   s     EEEszz||EEEr*   zFailed to download cubinsz,Failed to download cubins: checksum mismatchr"   N)tqdm.contrib.loggingrl   r,   Sessionlistrj   intr   r   r   lenr   r   r
   r   parentmkdirsubmitr   rK   add_done_callbackappendr   allRuntimeErrorr   )rl   rx   cubin_filesnum_threadsru   poolfuturesri   rs   r   
local_pathr{   resultsall_successchecksumrt   s                  @r   download_artifactsr      s   ::::::   G+--..Kbjnn%H#NNOOK		+%9
 
 
 F		 	 	 	 	 	  ,, 	FG& 	$ 	$a%&BDII1D8
!''t'DDDkk!63z??G "   %%n555s####EE|G/D/DEEEG	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	FF F F F F F F F F F F F F F F, g,,K 86777 & O Oh)D0
C
OOX66 	OMNNN	OO Os7   =E*B4EE*E	E*E	E**E.1E.c                      t                      } dt          dt          t          t          f         fdt          fd| D                       S )zy
    Check which cubins are already downloaded and return (num_downloaded, total).
    Does not download any cubins.
    	file_namer"   c                     t           j                            t          |           }t           j                            |          }| |fS r   )r   pathjoinr   isfile)r   r   existss      r   _check_file_statusz0get_artifacts_status.<locals>._check_file_status   s8     W\\"6	BB

++6""r*   c              3   4   K   | ]\  }} |          V  d S r   r&   )r'   r   rs   r   s      r   r)   z'get_artifacts_status.<locals>.<genexpr>   s3      OO<9a##I..OOOOOOr*   )rj   rK   r1   bool)r   r   s    @r   get_artifacts_statusr      s`    
 '((K#c #eCI.> # # # # OOOO;OOOOOOr*   c                     t           j                            t                    r=t                              dt                      t          j        t                     d S t                              dt                      d S )NzClearing cubin directory: z Cubin directory does not exist: )r   r   r   r   r4   r6   shutilrmtreer&   r*   r   clear_cubinr      so    	w~~*++ OG1EGGHHH*+++++M7KMMNNNNNr*   )r   r   r   r}   )+rJ   dataclassesr   loggingr   r.   r7   concurrent.futuresr   r   typingr   r,   r   	getLoggerr4   setLevelgetenvupperhandlers
addHandlerStreamHandlerjit.cubin_loaderr
   r   r   r   r   
contextlibr   rK   r   r   r1   r>   rA   rN   rd   rj   r   r   r   r   r&   r*   r   <module>r      s     " ! ! ! ! !  				 				  ? ? ? ? ? ? ? ?         
	1	2	2 		4f==CCEE F F F /
+g+--...              & % % % % % 	(c 	(# 	( 	( 	( 	( CE *-<?
38_   0 $J J J J J J J J&       2  &&Cic3ht(CD &C &C &C &CR&O &O &O &ORPeE#t)$4c$9: P P P P"O O O O Or*   