o
    
۾i-#                     @   s  d 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 ddl	m
Z
 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mZmZmZmZ dd
lmZ ddlmZ eeZedZ		d5deg ef dede de def
ddZ!edddddededB dedB dee"B dB de#e f
ddZ			d6dede#e dedB dedB dee"B dB de#e fddZ$			d6dede#e dedB dedB dee"B dB f
d d!Z%			d6dededB dedB dee"B dB de"f
d"d#Z&dddd$ded%ededB dedB dee"B dB de"fd&d'Z'd(ee
B d)ededB de"fd*d+Z(d7d(ee
B dedB fd,d-Z)	.d8d%ed(ee
B dedB de*dB fd/d0Z+	.d8d(ee
B d%ededB de
dB fd1d2Z,	.d8d%ed(ee
B dedB fd3d4Z-dS )9z%Utilities for model repo interaction.    N)Callable)cache)Path)TypeVar)hf_hub_downloadtry_to_load_from_cachelist_repo_files)EntryNotFoundErrorHfHubHTTPErrorLocalEntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundError)envs)init_logger_R   funclog_msgmax_retriesretry_delayreturnc                 C   s   t |D ]<}z|  W   S  ty@ } z(||d kr"td||  td|||d | t| |d9 }W Y d }~qd }~ww td)N   z%s: %sz%s: %s, retrying %d of %dr   zShould not be reached)range	ExceptionloggererrortimesleepAssertionError)r   r   r   r   attempte r"   V/home/ubuntu/.local/lib/python3.10/site-packages/vllm/transformers_utils/repo_utils.py
with_retry"   s   

r$   revision	repo_typetokenrepo_idr&   r'   r(   c                   s&   dt t f fdd}t|dS )Nr   c                     s   t    r fdd dD S ztjr*ddlm}  | tdd dW S t	dW S  t
jjy?   g  Y S w )	Nc                    s"   g | ]}|  rt| qS r"   )is_filestrrelative_to.0file
local_pathr"   r#   
<listcomp>D   s    z9list_repo_files.<locals>.lookup_files.<locals>.<listcomp>*r   modelscope_list_repo_filesMODELSCOPE_API_TOKEN)r&   r(   r%   )r   existsrglobr   VLLM_USE_MODELSCOPEvllm.transformers_utils.utilsr5   osgetenvhf_list_repo_fileshuggingface_huberrorsOfflineModeIsEnabledr4   r)   r'   r&   r(   r0   r#   lookup_filesA   s$   

z%list_repo_files.<locals>.lookup_fileszError retrieving file list)listr+   r$   )r)   r&   r'   r(   rB   r"   rA   r#   r	   9   s   
r	   model_name_or_pathallow_patternsc                    s`   z
t | |||d}W n ty   td g  Y S w g }|D ] | fdd|D  q|S )N)r)   r&   r(   r'   zError retrieving file list. Please ensure your `model_name_or_path``repo_type`, `token` and `revision` arguments are correctly set. Returning an empty list.c                    s$   g | ]}t  tj| r|qS r"   )fnmatchr;   pathbasenamer-   patternr"   r#   r2   y   s    z,list_filtered_repo_files.<locals>.<listcomp>)r	   r   r   r   extend)rD   rE   r&   r'   r(   	all_files	file_listr"   rI   r#   list_filtered_repo_files_   s*   

rN   c                 C   s   t t| ||||ddkS )NrD   rE   r&   r'   r(   r   )lenrN   rO   r"   r"   r#   any_pattern_in_repo_files   s   	rQ   c                 C   s   t | dg|||dS )Nzconsolidated*.safetensorsrO   )rQ   )rD   r&   r'   r(   r"   r"   r#   is_mistral_model_repo   s   rR   r'   r&   r(   	file_namec                C   s   t | |||d}||v S )NrS   r   )r)   rT   r'   r&   r(   rM   r"   r"   r#   file_exists   s   
rU   modelconfig_namec                 C   sJ   t |  } r||  S t| ||d}t|trdS tt| ||dS )Nr)   filenamer&   Tr&   )r   r7   r*   r   
isinstancer+   rU   )rV   rW   r&   r1   cached_filepathr"   r"   r#   file_or_path_exists   s   
r]   c                 C   sl   t j| r| S tjjsJ tjj|d}tjr'ddlm	} |dd| i|S ddlm	} |dd| i|S )N)local_files_onlyr&   r   )snapshot_downloadmodel_idr)   r"   )
r;   rG   r7   r>   	constantsHF_HUB_OFFLINEr   r9    modelscope.hub.snapshot_downloadr_   )rV   r&   common_kwargsr_   r"   r"   r#   get_model_path   s   re   mainc                 C   sr   t || |d}|du rt|| |d}t|}|dur7| r7t|d}| W  d   S 1 s2w   Y  dS )z7Get file contents from HuggingFace repository as bytes.rV   rT   r&   NrZ   rb)try_get_local_filer   r   r*   openread)rT   rV   r&   	file_pathhf_hub_filer/   r"   r"   r#   get_hf_file_bytes   s    rn   c                 C   sV   t | | }| r|S zt| ||d}t|trt |W S W d S  ty*   Y d S w )NrX   )r   r*   r   r[   r+   
ValueError)rV   rT   r&   rl   r\   r"   r"   r#   ri      s   

ri   c                 C   s   t || |d}|du r[z	t|| |d}W nB tjjy    Y dS  ttttfy< } zt	
d| W Y d}~dS d}~w tyV } zt	jd| |d W Y d}~dS d}~ww t|}|dur|| r|t|}t|W  d   S 1 sww   Y  dS )a  
    Downloads a file from the Hugging Face Hub and returns
    its contents as a dictionary.

    Parameters:
    - file_name (str): The name of the file to download.
    - model (str): The name of the model on the Hugging Face Hub.
    - revision (str): The specific version of the model.

    Returns:
    - config_dict (dict): A dictionary containing
    the contents of the downloaded file.
    rg   NrZ   z/File or repository not found in hf_hub_downloadzDCannot connect to Hugging Face Hub. Skipping file download for '%s':)exc_info)ri   r   r>   r?   r@   r   r   r
   r   r   debugr   warningr   r*   rj   jsonload)rT   rV   r&   rl   rm   r!   r/   r"   r"   r#   get_hf_file_to_dict  s<   
 ru   )r   r   )NNN)N)rf   ).__doc__rF   rs   r;   r   collections.abcr   	functoolsr   pathlibr   typingr   r>   r   r   r	   r=   huggingface_hub.utilsr
   r   r   r   r   vllmr   vllm.loggerr   __name__r   r   r+   intr$   boolrC   rN   rQ   rR   rU   r]   re   bytesrn   ri   ru   r"   r"   r"   r#   <module>   s  


(

&








