o
    4/i7+                     @   s  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dZeedd  ejsHee  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dZ	d-dede de de de!edf f
ddZ"	d-dede de de de!edf f
ddZ#eddG dd  d Z$G d!d" d"Z%d#d$ Z&dee!eef ddf fd%d&Z'd.d'd(Z(de!e!ee)f df fd)d*Z*d+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              
   c   sp    t j| d }|t j| < zd V  W |d u rt j| d  d S |t j| < d S |d u r2t j| d  w |t j| < w N)osenvirongetpop)r   r   	old_value r   a/lsinfo/ai/hellotax_ai/llm_service/venv_vllm/lib/python3.10/site-packages/flashinfer/artifacts.pytemp_env_var-   s   
r         
   sourceretriesdelaytimeoutreturn.c                 C   s   t d|d D ]T}ztj| |d}|  td|j}tdd |D W   S  tjj	y[ } z&t
d|  d| d|  ||k rQt
d	| d
 t| W Y d }~qd }~ww t
d t S )Nr   r    z\<a href=".*\.cubin">c                 s   s     | ]}|d d d V  qdS )	   iz.cubinNr   ).0hr   r   r   	<genexpr>B   s    z,get_available_cubin_files.<locals>.<genexpr>zFetching available files 
: attempt 	 failed: Retrying in  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er   r   r   get_available_cubin_files:   s"   


r>   c                    sH   g d	dt dt ddf fdd|  td  tS )
z
    Recursively navigates through child directories (e.g., include/) and finds
    all *.h header files, returning them as a tuple of relative paths.
     urlprefixr!   Nc                    s.  t dd D ]}zNtj| d}|  td|j}|D ]}|r(|| n| qtd|j}|D ]}|dks@|drAq5t	| |}|rL|| n|}	||	 q5W  d S  tj
jy }
 z&td|  d| d	|
  |k rtd
  d t  W Y d }
~
qd }
~
ww td|  d d S )Nr   r"   z<a href="([^"]+\.h)">z<a href="([^"]+/)">z../z..z Fetching available header files r'   r(   r)   r*   zMax retries reached for z. Fetch failed.)r+   r,   r   r-   r.   r/   r0   append
startswithr
   r2   r3   r4   r5   r6   r7   r8   r9   )r@   rA   r:   r;   header_hrefsr%   	dir_hrefsd
subdir_urlsubdir_prefixr=   r   fetch_directoryresultr   r    r   r   rJ   [   s2   

	z3get_available_header_files.<locals>.fetch_directoryzresult: )r?   )strr4   r6   r1   )r   r   r   r    r   rI   r   get_available_header_filesR   s
   $"rM   T)frozenc                   @   sN   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.
    z9f1ed60e5666a7620683a8c34a41c850a25029b35/fmha/trtllm-gen/TRTLLM_GEN_FMHAzFb55211623be7f5697c5262ffd8361fc06c147bc9/batched_gemm-b3c1646-c111d7c/TRTLLM_GEN_BMMz>1fddc48b7b48af33914d040051b3e2ee9ba4701e/gemm-145d1b1-9b113e3/TRTLLM_GEN_GEMMz4a72d85b019dc125b9f711300cb989430f762f5a6/fmha/cudnn/
CUDNN_SDPAz3a72d85b019dc125b9f711300cb989430f762f5a6/deep-gemm/DEEPGEMMN)__name__
__module____qualname____doc__rP   rL   __annotations__rQ   rR   rS   rT   r   r   r   r   rO      s   
 

rO   c                	   @   s   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.
    @10a54e8c3175099481aed2739ae30fa0f782368c40f9ad1b423ed8353315d65brP   @0af823880730c4f0b3832d2208fab035946694b83444410b9309db5613d60195rQ   @1a2a166839042dbd2a57f48051c82cd1ad032815927c753db269a4ed10d0ffbfrT   @15cb8c85dfb5eddd4f121d64cb5a718321fb55b85aa19df10ddc1329d4a726b9rR   checksums.txtmap_checksumsN)rU   rV   rW   rX   rP   rL   rY   rQ   rT   rR   r
   rO   r`   dictr   r   r   r   rZ      s   
 


rZ   c           	   	   C   s   i }| D ]E}t tt |d}tt |d }t|| t|d"}|D ]}|  \}}d|v r5t ||}|||< q"W d    n1 sDw   Y  q|S )Nr_   rz.h)r
   r	   r   r   openstripsplit)	subdirs	checksumssubdirurichecksum_pathflinesha256filenamer   r   r   get_checksums   s"   



ro   c                  c   s    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 ]9}t|d}|tj	| fV  t
t| |D ]}t|||| fV  qVtt| |D ]}t||}||| fV  qjq@d S )Nzinclude/flashInferMetaInfo.hzinclude/flashinferMetaInfo.hr_   )r	   rO   rP   rQ   rR   rT   ro   r
   rZ   r`   r>   rM   )base
cubin_dirsrg   	cubin_dirrj   name	full_pathr   r   r   get_subdir_file_list   sB   







ru   c               	      sV  ddl m}  t }ttttf  t }tt	j
dd}| t|dd[ d fdd	}t|@}g }|D ]*\}}tt|}	t| }
|
jjd
d
d |jt|	t|
|d}|| || q7dd t|D }W d    n1 suw   Y  W d    n1 sw   Y  t|}|std|D ]\}}t| }
tt|
|stdqd S )Nr   )tqdm_logging_redirect!FLASHINFER_CUBIN_DOWNLOAD_THREADS4zDownloading cubins)totaldescr!   c                    s     d d S )Nr   )update)_pbarr   r   update_pbar_cb   s   z*download_artifacts.<locals>.update_pbar_cbT)parentsexist_ok)sessionc                 S   s   g | ]}|  qS r   )rK   )r$   futr   r   r   
<listcomp>
  s    z&download_artifacts.<locals>.<listcomp>zFailed to download cubinsz,Failed to download cubins: checksum mismatchr!   N)tqdm.contrib.loggingrv   r,   Sessionlistr1   rL   ru   intr   r   r   lenr   r
   r	   r   parentmkdirsubmitr   add_done_callbackrB   r   allRuntimeErrorr   )rv   r   cubin_filesnum_threadsr   poolfuturesrs   r|   r   
local_pathr   resultsall_successchecksumr   r}   r   download_artifacts   sB   


r   c                     s6   t  } dtdtttf fdd t fdd| D S )zy
    Check which cubins are already downloaded and return (num_downloaded, total).
    Does not download any cubins.
    	file_namer!   c                 S   s"   t jt| }t j|}| |fS r   )r   pathjoinr   isfile)r   r   existsr   r   r   _check_file_status  s   z0get_artifacts_status.<locals>._check_file_statusc                 3   s    | ]	\}} |V  qd S r   r   )r$   r   r|   r   r   r   r&   %  s    z'get_artifacts_status.<locals>.<genexpr>)ru   rL   r1   bool)r   r   r   r   get_artifacts_status  s   r   c                   C   s>   t jtrtdt  t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   r   clear_cubin(  s   r   )r   r   r   r   ),rX   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   rL   r   r   r1   r>   rM   rO   rZ   ro   ru   r   r   r   r   r   r   r   r   <module>   sd    
	



0 
,)