o
    
۾i                  
   @   sT  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
mZ d dlmZ eeZdedefd	d
ZdedefddZdedefddZ		d!dededB deeB dB dee fddZdeeB deeef fddZdeeB deeef fddZededefddZdeeB deee	f fddZdedefdd ZdS )"    N)cache)PathLike)Path)Any)init_loggermodel_or_pathreturnc                 C      |   dS )Nzs3://lower
startswithr    r   Q/home/ubuntu/.local/lib/python3.10/site-packages/vllm/transformers_utils/utils.pyis_s3      r   c                 C   r	   )Nzgs://r
   r   r   r   r   is_gcs   r   r   c                 C   s   t | pt| S N)r   r   r   r   r   r   is_cloud_storage   s   r   repo_idrevisiontokenc                 C   s:   ddl m} | }|| dd |j| |ddD }|S )z List files in a modelscope repo.r   )HubApic                 S   s    g | ]}|d  dkr|d qS )Typeblobr   r   ).0filer   r   r   
<listcomp>)   s
    z.modelscope_list_repo_files.<locals>.<listcomp>T)model_idr   	recursive)modelscope.hub.apir   loginget_model_files)r   r   r   r   apifilesr   r   r   modelscope_list_repo_files   s   
r%   pathc              	   C   sl   t | (}zt| W W  d    S  ty+   tttf   Y W  d    S w 1 s/w   Y  d S r   )openjsonloadsread	Exceptiondictstr)r&   fr   r   r   _maybe_json_dict3   s   
r/   c              
   C   sx   t ttf  }t| '}| D ]}z|  \}}|||< W q ty)   Y qw W d    |S 1 s5w   Y  |S r   )r,   r-   r'   	readlinesstripsplitr+   )r&   parsed_dictr.   line
model_nameredirect_namer   r   r   _maybe_space_split_dict;   s   

r7   modelc                 C   sR   t j}|s| S t| s| S t|pt|}||  }r'td| | |S | S )z
    Use model_redirect to redirect the model name to a local folder.

    :param model: hf model name
    :return: maybe redirect to a local folder
    z model redirect: [ %s ] -> [ %s ])	envsVLLM_MODEL_REDIRECT_PATHr   existsr/   r7   getloggerinfo)r8   model_redirect_pathredirect_dictredirect_modelr   r   r   maybe_model_redirectG   s   	
rB   c                 C   s^   t | d }td|dd }t||d}|W  d    S 1 s(w   Y  d S )Nrbz<Q   r   zutf-8)r'   structunpackr*   r(   r)   decode)r&   r.   length_of_metadatametadatar   r   r   parse_safetensors_file_metadatab   s
   $rJ   
model_repoc                 C   s2   t jr	t|  r| S ddlm} tj| | S )zUWhen VLLM_USE_MODELSCOPE is True convert a model
    repository string to a Path str.r   )get_model_cache_root)	r9   VLLM_USE_MODELSCOPEr   r;   modelscope.utils.file_utilsrL   osr&   join)rK   rL   r   r   r   convert_model_repo_to_pathi   s   rQ   )NN)r(   rO   rE   	functoolsr   r   pathlibr   typingr   	vllm.envsr9   vllm.loggerr   __name__r=   r-   boolr   r   r   listr%   r,   r/   r7   rB   rJ   rQ   r   r   r   r   <module>   s<   

