o
    ॵi?                     @   s  d dl 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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 d dlmZmZmZmZ d d	lT d d
lmZ ejdk rbd dlZnd dlmZ eej dZ!dZ"de#fddZ$dd Z%de&de#de'fddZ(dxddZ)dd Z*h dZ+e+,dhZ-ej./dd0 Z1ej./dd0 Z2d Z3e2e-v re1e+vrej45d!duZ6e6rze7d!Z3e!8d"e3 d# W n ej9y   dZ6Y n	w ne!8d$ dZ6ej45d%duZ:ze7d%Z;e!<d&e;  W n ej9y   dZ:Y nw d Z=e1e-v r`e2e+vr`ej45d'duZ>e>rAd(Z?dZ=e?D ]Z@ze7e@Z=W  n ej9y<   Y q&w e=duZ>e>r_d d)lAm7Z7 e7Be=e7Bd*k rVne!8d+e= d# ne!8d, dZ>d-d. ZCd/d0 ZDd1d2 ZEd3d4 ZFd5d6 ZGd7d8 ZHd9d: ZId;d< ZJd=d> ZKd?d@ ZLdAdB ZMdCdD ZNdEdF ZOdGdH ZPdIdJ ZQdKdL ZRdMdN ZSdOdP ZTdQdR ZUe
dSeFeVffdTeEeWffdUeDeXffdVeMeYffd'eMeYffd%eHeZffdWeGe[ffd!eIe\ffdXeKe]^dYe3ffdZeCe_ffd[eNe`ffd\eOeaffd]eQd^ebffd_eRec^d`daffdaeRec^d`daffdbeQdbedffdceQdcedffddeQddeeffdeeQdeefffdfeQdfegffdgeQdgehffdheQdheiffdieQdiejffdjeQdjekffdkeQdkelffdleQdlemffdmeQdmenffdneQdneoffgZpeqg doZrdpdq Zsdrds Ztdtdu ZuG dvdw dweZvdS )y    N)OrderedDict)import_module)chain)Path)
ModuleType)Any)	INDEX_KEY
MODULE_KEYREQUIREMENT_KEY
load_index)*)
get_logger)      )	log_levelpy_filec                 C   s`   t j| \}}|dkrt }t|d }tjd| t	|  t
|}tjd ||fS )zw Import module from a certrain file

    Args:
        py_file: path to a python file to be imported

    Return:

     r   )ospathsplitr   cwdospsplitextsysinsertvalidate_py_syntaxr   pop)r   dirnamebasefilemodule_namemod r!   Q/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/utils/import_utils.pyimport_modules_from_file    s   	r#   c                 C   s>   t |ts	J dt |ts|j}t|| }t|| }||kS )a  Check if a method of base class is overridden in derived class.

    Args:
        method (str): the method name to check.
        base_class (type): the class of the base class.
        derived_class (type | Any): the class or instance of the derived class.
    z>base_class doesn't accept instance, Please pass class instead.)
isinstancetype	__class__getattr)method
base_classderived_classbase_methodderived_methodr!   r!   r"   is_method_overridden4   s   


r-   objr(   returnc                 C   s   t | |ott| |S )zCheck whether the object has a method.

    Args:
        method (str): The method name to check.
        obj (object): The object to check.

    Returns:
        bool: True if the object has the method else False.
    )hasattrcallabler'   )r.   r(   r!   r!   r"   
has_methodG   s   
r2   Fc              	   C   s   | sdS d}t | trd}| g} t | tstdt|  g }| D ]7}t |ts5t| dt| dzt|}W n tyS   |rOt| d d}ntY nw |	| q"|r`|d }|S )	aA  Import modules from the given list of strings.

    Args:
        imports (list | str | None): The given module names to be imported.
        allow_failed_imports (bool): If True, the failed imports will return
            None. Otherwise, an ImportError is raise. Default: False.

    Returns:
        list[module] | module | None: The imported modules.

    Examples:
        >>> osp, sys = import_modules(
        ...     ['os.path', 'sys'])
        >>> import os.path as osp_
        >>> import sys as sys_
        >>> assert osp == osp_
        >>> assert sys == sys_
    NFTz+custom_imports must be a list but got type z is of type z and cannot be imported.z! failed to import and is ignored.r   )
r$   strlist	TypeErrorr%   r   ImportErrorloggerwarningappend)importsallow_failed_importssingle_importimportedimpimported_tmpr!   r!   r"   import_modulesT   s:   


r@   c              
   C   st   t | ddd}| }W d    n1 sw   Y  zt| W d S  ty9 } z
td|  d| d }~ww )Nrzutf-8)encodingz'There are syntax errors in config file z: )openreadastparseSyntaxError)filenamefcontenter!   r!   r"   r      s   
r   >   1ONYESTRUEAUTOUSE_TF	USE_TORCHzN/AtorchzPyTorch version z Found.z'Disabling PyTorch because USE_TF is settimmz#Successfully imported timm version 
tensorflow)
rU   ztensorflow-cpuztensorflow-gpuz
tf-nightlyztf-nightly-cpuztf-nightly-gpuzintel-tensorflowzintel-tensorflow-avx512ztensorflow-rocmztensorflow-macos)version2zTensorFlow version z-Disabling Tensorflow because USE_TORCH is setc                   C      t jdd uS )Nscipy	importlibutil	find_specr!   r!   r!   r"   is_scipy_available      r^   c                   C   s&   t jdd u r
dS t ot jdS )NsklearnFzsklearn.metrics)r[   r\   r]   r^   r!   r!   r!   r"   is_sklearn_available   s   ra   c                   C   rX   )NsentencepiecerZ   r!   r!   r!   r"   is_sentencepiece_available   r_   rc   c                   C   s$   t jdd u r
dS t jdd uS )NgoogleFzgoogle.protobufrZ   r!   r!   r!   r"   is_protobuf_available   s   re   c                   C   rX   )N
tokenizersrZ   r!   r!   r!   r"   is_tokenizers_available   r_   rg   c                   C      t S N)_timm_availabler!   r!   r!   r"   is_timm_available      rk   c                   C   rh   ri   )_torch_availabler!   r!   r!   r"   is_torch_available   rl   rn   c                  C   s   t  rdd l} | j S dS )Nr   F)rn   rS   cudais_available)rS   r!   r!   r"   is_torch_cuda_available   s   
rq   c                   C   rX   )NwenetruntimerZ   r!   r!   r!   r"   is_wenetruntime_available   r_   rs   c                   C   rX   )NswiftrZ   r!   r!   r!   r"   is_swift_available   r_   ru   c                   C   rh   ri   )_tf_availabler!   r!   r!   r"   is_tf_available   rl   rw   c                   C   rX   )Ncv2rZ   r!   r!   r!   r"   is_opencv_available  r_   ry   c                   C   rX   )Nz	PIL.ImagerZ   r!   r!   r!   r"   is_pillow_available  r_   rz   c                 C   s   t j| d uS ri   rZ   pkg_namer!   r!   r"   _is_package_available_fn	  r_   r}   c                 C   s   t t| S ri   )	functoolspartialr}   r{   r!   r!   r"   is_package_available  s   r   c                 C   s   t jdd uot jdS )Nespnet2espnetrZ   r{   r!   r!   r"   is_espnet_available  s   
r   c                   C   rX   )NvllmrZ   r!   r!   r!   r"   is_vllm_available  r_   r   c                   C   rX   )NtransformersrZ   r!   r!   r!   r"   is_transformers_available  r_   r   c                   C   rX   )Ntensorrt_llmrZ   r!   r!   r!   r"   is_tensorrt_llm_available  r_   r   protobufrb   r`   tfrf   rr   	TORCH_VERrY   rx   PILzpai-easynlpeasynlpr   REQr   funasrkwsbpdecord	deepspeedfairseqfasttextmegatron_utiltext2sql_lgesqlmpi4py	open_cliptamingxformersrt   )r   r   typingc                    s   t |ttfs
|g}t | tr|  nt| dr| jn| jj g }|D ]'}|dks,|tv r-q"|tv r6t| }nt	|}t
d|}||f}|| q" fdd|D }|r\td|d S )N__name__r   r   c                    s    g | ]\}}| s|  qS r!   )format).0	availablemsgnamer!   r"   
<listcomp>_  s     zrequires.<locals>.<listcomp>)r$   r4   tupler3   r0   r   r&   SYSTEM_PACKAGEREQUIREMENTS_MAAPINGr   GENERAL_IMPORT_ERRORreplacer9   r6   join)r.   requirementschecksreqcheckcheck_fnerr_msgfailedr!   r   r"   requiresK  s.   


r   c                       t   fdd}|S )Nc                     &   t  r
 | i |S td j d)NMethod `z` requires PyTorch.)rn   r6   r   argskwargsfuncr!   r"   wrapperf     ztorch_required.<locals>.wrapperr~   wrapsr   r   r!   r   r"   torch_requiredd     r   c                    r   )Nc                     r   )Nr   z` requires TF.)rw   r6   r   r   r   r!   r"   r   r  r   ztf_required.<locals>.wrapperr   r   r!   r   r"   tf_requiredp  r   r   c                       s~   e Zd ZdZedu re Z			d fdd	Zdd Z fddZd	ed
e	fddZ
defddZdd Zedd Z  ZS )LazyImportModuleNFc           
         s   t  | t| | _i | _| D ]\}}|D ]}	|| j|	< qqt| tt|	   | _
|| _|| _tj|g| _|d u rFi n|| _|| _|| _|rW|   d S d S ri   )super__init__setkeys_modules_class_to_moduleitemsr4   r   values__all____file____spec__r   r   r   __path___objects_name_import_structure_try_to_import)
selfr   module_fileimport_structuremodule_specextra_objectstry_to_pre_importkeyr   valuer&   r!   r"   r     s&   
zLazyImportModule.__init__c                 C   s\   | j  D ]&}zt| | W q ty+ } ztd| d|  W Y d }~qd }~ww d S )Nzpre load module z error, please check )r   r   r'   	Exceptionr7   r8   )r   
sub_modulerK   r!   r!   r"   r     s   zLazyImportModule._try_to_importc                    s,   t   }| jD ]}||vr|| q|S ri   )r   __dir__r   r9   )r   resultattrr   r!   r"   r     s   


zLazyImportModule.__dir__r   r/   c                 C   sz   || j v r
| j | S || jv r| |}n || j v r*| | j| }t||}ntd| j d| t| || |S )Nzmodule z has no attribute )	r   r   _get_moduler   r   r'   AttributeErrorr   setattr)r   r   r   moduler!   r!   r"   __getattr__  s   


zLazyImportModule.__getattr__r   c              
   C   s   z$| j d | }|tjt v rtjt | }t|| td| | j W S  ty? } ztd| j  d| d| |d }~ww )N.zFailed to import z@ because of the following error (look up to see its traceback):
)	r   r   	AST_INDEXr
   r   r[   r   r   RuntimeError)r   r   module_name_fullr   rK   r!   r!   r"   r     s$   
zLazyImportModule._get_modulec                 C   s   | j | j| j| jffS ri   )r&   r   r   r   )r   r!   r!   r"   
__reduce__  s   zLazyImportModule.__reduce__c                 C   sl   | t jt v r,t jt |  }|t }|t jt v r%t jt | }t|| t| dS t	|  d dS )z import a lazy import module using signature

        Args:
            signature (tuple): a tuple of str, (registry_name, registry_group_name, module_name)
        z not found in ast index fileN)
r   r   r   r	   r
   r   r[   r   r7   r8   )	signature	mod_indexr   r   r!   r!   r"   r     s   
zLazyImportModule.import_module)NNF)r   
__module____qualname__r   r   r   r   r   r3   r   r   r   r   staticmethodr   __classcell__r!   r!   r   r"   r   |  s    		r   )F)wrE   r~   r[   loggingr   os.pathr   r   r   collectionsr   r   	itertoolsr   pathlibr   typesr   r   r   modelscope.utils.ast_utilsr   r	   r
   r   modelscope.utils.errormodelscope.utils.loggerr   version_infoimportlib_metadataimportlib.metadatametadataWARNINGr7   r   r3   r#   r-   objectboolr2   r@   r   ENV_VARS_TRUE_VALUESunionENV_VARS_TRUE_AND_AUTO_VALUESenvirongetupperrQ   rR   _torch_versionr\   r]   rm   rV   infoPackageNotFoundErrorrj   _timm_versiondebug_tf_versionrv   
candidatespkg	packagingrF   r^   ra   rc   re   rg   rk   rn   rq   rs   ru   rw   ry   rz   r}   r   r   r   r   r   PROTOBUF_IMPORT_ERRORSENTENCEPIECE_IMPORT_ERRORSKLEARN_IMPORT_ERRORTENSORFLOW_IMPORT_ERRORTIMM_IMPORT_ERRORTOKENIZERS_IMPORT_ERRORPYTORCH_IMPORT_ERRORWENETRUNTIME_IMPORT_ERRORr   SCIPY_IMPORT_ERROROPENCV_IMPORT_ERRORPILLOW_IMPORT_ERROREASYNLP_IMPORT_ERRORr   AUDIO_IMPORT_ERRORDECORD_IMPORT_ERRORDEEPSPEED_IMPORT_ERRORFAIRSEQ_IMPORT_ERRORFASTTEXT_IMPORT_ERRORMEGATRON_UTIL_IMPORT_ERRORTEXT2SQL_LGESQL_IMPORT_ERRORMPI4PY_IMPORT_ERROROPENCLIP_IMPORT_ERRORTAMING_IMPORT_ERRORXFORMERS_IMPORT_ERRORSWIFT_IMPORT_ERRORr   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s  


/




	












&