U
    dit                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZ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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mZmZmZmZmZ ee Z!e
" Z#d	d
 Z$ee%ej&f ddddZ'ee%ej&f ee% dddZ(ee%ej&f ee% dddZ)ee%ej&f ee% dddZ*ee%ej&f ee% dddZ+dde%ee%ej&f e,ej-dddZ.d+ee%ej&f e%eee%ej&f  e,ee, eee%e%f  eee,e%f  ee% e,ee% ee% e%dddZ/d,e%ee%ej&f eee%ej&f  e,ee, eee%e%f  eee,e%f  ee% e,ee% ee% ej-d d!d"Z0d-eee%ej&f ee ee% d#d$d%Z1d&d' Z2d(Z3d)d* Z4dS ).z3Utilities to dynamically load objects from the Hub.    N)Path)
ModuleType)AnyDictListOptionalUnion)try_to_load_from_cache   )HF_MODULES_CACHE TRANSFORMERS_DYNAMIC_MODULE_NAMEcached_fileextract_commit_hashis_offline_modeloggingc                  C   sP   t tjkrdS tjt  tjt dd tt d } |  sL|   t	
  dS )z_
    Creates the cache directory for modules with an init, and adds it to the Python path.
    NTexist_ok__init__.py)r   syspathappendosmakedirsr   existstouch	importlibinvalidate_caches)	init_path r   E/tmp/pip-unpacked-wheel-bm_b0l5e/transformers/dynamic_module_utils.pyinit_hf_modules1   s    
r    )namereturnc                 C   s\   t   tt|   }|j s*t|j tj|dd |d }| sX|	  t
  dS )z
    Creates a dynamic module in the cache directory for modules.

    Args:
        name (`str` or `os.PathLike`):
            The name of the dynamic module to create.
    Tr   r   N)r    r   r   resolveparentr   create_dynamic_moduler   r   r   r   r   )r!   Zdynamic_module_pathr   r   r   r   r%   A   s    

r%   )module_filer"   c              	   C   sV   t | ddd}| }W 5 Q R X tjd|tjd}|tjd|tjd7 }tt|S )z
    Get the list of modules that are relatively imported in a module file.

    Args:
        module_file (`str` or `os.PathLike`): The module file to inspect.

    Returns:
        `List[str]`: The list of relative imports in the module.
    rutf-8encodingz^\s*import\s+\.(\S+)\s*$flagsz^\s*from\s+\.(\S+)\s+import)openreadrefindall	MULTILINElistset)r&   fcontentZrelative_importsr   r   r   get_relative_importsW   s
    
r6   c                    s   d}| g}g  |sg }|D ]}| t| qt| jfdd|D } fdd|D }dd |D }t|dk}  | q S )a  
    Get the list of all files that are needed for a given module. Note that this function recurses through the relative
    imports (if a imports b and b imports c, it will return module files for b and c).

    Args:
        module_file (`str` or `os.PathLike`): The module file to inspect.

    Returns:
        `List[str]`: The list of all relative imports a given module needs (recursively), which will give us the list
        of module files a given module needs.
    Fc                    s   g | ]}t  | qS r   )str).0m)module_pathr   r   
<listcomp>   s     z-get_relative_import_files.<locals>.<listcomp>c                    s   g | ]}| kr|qS r   r   r8   r4   )all_relative_importsr   r   r;      s      c                 S   s   g | ]}| d qS ).pyr   r<   r   r   r   r;      s     r   )extendr6   r   r$   len)r&   Z	no_changeZfiles_to_checkZnew_importsr4   Znew_import_filesr   )r=   r:   r   get_relative_import_filesl   s    
rA   )filenamer"   c              	   C   s   t | ddd}| }W 5 Q R X tjdd|tjtjB d}tjdd|tjd}tjd|tjd}|tjd	|tjd7 }d
d |D }tt|S )a  
    Extracts all the libraries (not relative imports this time) that are imported in a file.

    Args:
        filename (`str` or `os.PathLike`): The module file to inspect.

    Returns:
        `List[str]`: The list of all packages required to use the input module.
    r'   r(   r)   z \s*try\s*:\s*.*?\s*except\s*.*?: r+   zHif is_flash_attn[a-zA-Z0-9_]+available\(\):\s*(from flash_attn\s*.*\s*)+z^\s*import\s+(\S+)\s*$z^\s*from\s+(\S+)\s+importc                 S   s$   g | ]}| d s|d d qS ).r   )
startswithsplit)r8   impr   r   r   r;      s     
 zget_imports.<locals>.<listcomp>)	r-   r.   r/   subr1   DOTALLr0   r2   r3   )rB   r4   r5   importsr   r   r   get_imports   s    
   rK   c                 C   s   t | }g }|D ]f}zt| W q tk
rt } z4td| d|  dt|krb|| n W 5 d}~X Y qX qt|dkrtdd	| dd		| d
t
| S )a.  
    Check if the current Python environment contains all the libraries that are imported in a file. Will raise if a
    library is missing.

    Args:
        filename (`str` or `os.PathLike`): The module file to check.

    Returns:
        `List[str]`: The list of relative imports in the file.
    z&Encountered exception while importing z: zNo module namedNr   z\This modeling file requires the following packages that were not found in your environment: z, z. Run `pip install  `)rK   r   import_moduleImportErrorloggerwarningr7   r   r@   joinr6   )rB   rJ   Zmissing_packagesrG   	exceptionr   r   r   check_imports   s    rT   Fforce_reload)
class_namer:   rV   r"   c          
   
   C   s  t j|}|dr"|dd }|t jjd}tt| }t |r^t	j
|d t  t	j
|}tjj||d}|gtttt| }tddd |D  }|dkrtj|}	|	t	j
|< n|}	t|	d	d
|kr|j|	 ||	_t|	| W  5 Q R  S Q R X dS )a  
    Import a module on the cache directory for modules and extract a class from it.

    Args:
        class_name (`str`): The name of the class to import.
        module_path (`str` or `os.PathLike`): The path to the module to import.
        force_reload (`bool`, *optional*, defaults to `False`):
            Whether to reload the dynamic module from file if it already exists in `sys.modules`.
            Otherwise, the module is only reloaded if the file has changed.

    Returns:
        `typing.Type`: The class looked for.
    r>   NrD   )location    c                 s   s   | ]}t ||  V  qd S )N)bytes
read_bytesr<   r   r   r   	<genexpr>   s     z&get_class_in_module.<locals>.<genexpr>__transformers_module_hash__rC   )r   r   normpathendswithreplacesepr   r   _HF_REMOTE_CODE_LOCKr   modulespopr   r   getutilspec_from_file_locationsortedmaprA   hashlibsha256rR   	hexdigestmodule_from_specgetattrloaderexec_moduler^   )
rW   r:   rV   r!   r&   cached_moduleZmodule_specZmodule_filesZmodule_hashmoduler   r   r   get_class_in_module   s*    
rt   )pretrained_model_name_or_pathr&   	cache_dirforce_downloadresume_downloadproxiestokenrevisionlocal_files_only	repo_type_commit_hashr"   c                 K   s  | dd}|dk	r4tdt |dk	r0td|}t rL|sLtd d}t| } t	j
| }|rrt	j
| }n"| dt	j
j}t| |||
|	d}g }z8t| |||||||||	|
d	}|s||kr|| W n. tk
r   td
| d|  d  Y nX t|}tt	j
j | }t| tt| }|t	j
| kr||  rbt|t|| szt|||  t  |D ]Z}| d}t	j
 | |}||  rt|t|| s~t|||  t  q~nt!||
}|| }|t	j
j | }t| ||  s,t|||  t  |D ]L}|| d  s0t"| | d||||||||d
 || d q0t#|dkr|dkrd dd |D }|	dkrdn|	 d}d| |  }t$d| d| d t	j
 ||S )a	  
    Prepares Downloads a module from a local folder or a distant repo and returns its path inside the cached
    Transformers module.

    Args:
        pretrained_model_name_or_path (`str` or `os.PathLike`):
            This can be either:

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              huggingface.co.
            - a path to a *directory* containing a configuration file saved using the
              [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`.

        module_file (`str`):
            The name of the module file containing the class to look for.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.
        repo_type (`str`, *optional*):
            Specify the repo type (useful when downloading from a space for instance).

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `str`: The path to the module inside the cache.
    use_auth_tokenNrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.V`token` and `use_auth_token` are both specified. Please set only the argument `token`.z+Offline mode: forcing local_files_only=TrueT/)rv   r{   r}   )	rv   rw   ry   rx   r|   rz   r{   r}   r~   zCould not locate the z inside rD   r>   )rv   rw   rx   ry   rz   r{   r|   r~   r   
c                 S   s   g | ]}d | qS )z- r   r<   r   r   r   r;     s     z*get_cached_module_file.<locals>.<listcomp>rC   zs/zhttps://huggingface.co/z9A new version of the following files was downloaded from z:
z
. Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.)%re   warningswarnFutureWarning
ValueErrorr   rP   infor7   r   r   isdirbasenamera   rb   r	   r   r   EnvironmentErrorerrorrT   r   r%   r   r   r   filecmpcmpshutilcopyr   r   rR   r   get_cached_module_filer@   rQ   )ru   r&   rv   rw   rx   ry   rz   r{   r|   r}   r~   Zdeprecated_kwargsr   is_local	submodulerr   Z	new_filesZresolved_module_fileZmodules_neededZfull_submoduleZsubmodule_pathZmodule_neededZmodule_needed_fileZcommit_hashZrepo_type_strurlr   r   r   r      s    >

     

 

r   )class_referenceru   rv   rw   rx   ry   rz   r{   r|   r}   code_revisionr"   c                 K   s   | dd}|dk	r4tdt |dk	r0td|}d| krL| d\}} n|}| d\}}|
dkrr||krr|}
t||d ||||||
||	d
}t|||d	S )
a>  
    Extracts a class from a module file, present in the local folder or repository of a model.

    <Tip warning={true}>

    Calling this function will execute the code in the module file found locally or downloaded from the Hub. It should
    therefore only be called on trusted repos.

    </Tip>



    Args:
        class_reference (`str`):
            The full name of the class to load, including its module and optionally its repo.
        pretrained_model_name_or_path (`str` or `os.PathLike`):
            This can be either:

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              huggingface.co.
            - a path to a *directory* containing a configuration file saved using the
              [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`.

            This is used when `class_reference` does not specify another repo.
        module_file (`str`):
            The name of the module file containing the class to look for.
        class_name (`str`):
            The name of the class to import in the module.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or `bool`, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.
        repo_type (`str`, *optional*):
            Specify the repo type (useful when downloading from a space for instance).
        code_revision (`str`, *optional*, defaults to `"main"`):
            The specific revision to use for the code on the Hub, if the code leaves in a different repository than the
            rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-based system for
            storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git.

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `typing.Type`: The class, dynamically imported from the module.

    Examples:

    ```python
    # Download module `modeling.py` from huggingface.co and cache then extract the class `MyBertModel` from this
    # module.
    cls = get_class_from_dynamic_module("modeling.MyBertModel", "sgugger/my-bert-model")

    # Download module `modeling.py` from a given repo and cache then extract the class `MyBertModel` from this
    # module.
    cls = get_class_from_dynamic_module("sgugger/my-bert-model--modeling.MyBertModel", "sgugger/another-bert-model")
    ```r   Nr   r   z--rD   r>   )rv   rw   rx   ry   rz   r{   r|   r}   rU   )re   r   r   r   r   rF   r   rt   )r   ru   rv   rw   rx   ry   rz   r{   r|   r}   r   kwargsr   Zrepo_idr&   rW   Zfinal_moduler   r   r   get_class_from_dynamic_module  s8    Zr   )objfolderconfigr"   c           	         s    j dkr&td  d| d dS  fdd}t|ttfrT|D ]}|| qDn|dk	rd|| g }tj j  j}t	|t	|j
 }t|| || t|D ],}t	|t	|j
 }t|| || q|S )a  
    Save the modeling files corresponding to a custom model/configuration/tokenizer etc. in a given folder. Optionally
    adds the proper fields in a config.

    Args:
        obj (`Any`): The object for which to save the module files.
        folder (`str` or `os.PathLike`): The folder where to save.
        config (`PretrainedConfig` or dictionary, `optional`):
            A config in which to register the auto_map corresponding to this custom object.

    Returns:
        `List[str]`: The list of files saved.
    __main__z We can't save the code defining z in z as it's been defined in __main__. You should put this code in a separate module so we can include it in the saved folder and make it easier to share via the Hub.Nc           
         s
   j j}|dd }| d j j }d|krd }d } j jdr| d j j }t dd d k	rt d}|j}|dd }| d|j }n| d j j }||f}t| tr| di }	||	 j	< |	| d< n*t| dd d k	r|| j
 j	< n j	|i| _
d S )NrD   	TokenizerZFastslow_tokenizer_classauto_map)	__class__
__module__rF   __name__r`   ro   
isinstancedictrf   Z_auto_classr   )
_configmodule_nameZlast_moduleZ	full_namer   Zfast_tokenizer_classZslow_tokenizerZslow_tok_module_nameZlast_slow_tok_moduler   r   r   r   _set_auto_map_in_configA  s,    



z3custom_object_save.<locals>._set_auto_map_in_config)r   rP   rQ   r   r2   tupler   rd   __file__r   r!   r   r   r   rA   )	r   r   r   r   cfgresultZobject_file	dest_fileZneeded_filer   r   r   custom_object_save+  s*    
 
r   c                 C   s   t dd S )NzLoading this model requires you to execute custom code contained in the model repository on your local machine. Please set the option `trust_remote_code=True` to permit loading of this model.)r   )signumframer   r   r   _raise_timeout_errorw  s    r      c              
   C   s  | d kr|rd} n|rt dkrd }zzhttjt}tt  | d krtd| d| d}| dkrnd} q>| dkr>d} q>td W n* tk
r   td| d| d	Y nX W 5 |d k	rttj| td X n|rtd d  |r|s| std
| d| S )NFr   zThe repository for z contains custom code which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/z.
You can avoid this prompt in future by passing the argument `trust_remote_code=True`.

Do you wish to run the custom code? [y/N] )yesy1T)non0rC   zS.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.zLoading z requires you to execute the configuration file in that repo on your local machine. Make sure you have read the code there to avoid malicious use, then set the option `trust_remote_code=True` to remove this error.)	TIME_OUT_REMOTE_CODEsignalSIGALRMalarmr   inputlower	Exceptionr   )Ztrust_remote_codeZ
model_nameZhas_local_codeZhas_remote_codeZprev_sig_handlerZanswerr   r   r   resolve_trust_remote_code  s>    


r   )	NFNNNNFNN)	NFNNNNFNN)N)5__doc__r   rk   r   importlib.utilr   r/   r   r   r   	threadingtypingr   pathlibr   typesr   r   r   r   r   r   Zhuggingface_hubr	   utilsr   r   r   r   r   r   Z
get_loggerr   rP   Lockrc   r    r7   PathLiker%   r6   rA   rK   rT   boolTypert   r   r   r   r   r   r   r   r   r   r   <module>   s    

!'4          4         }(L