o
    i                     @   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ZddlmZ ddlmZ ddlm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 m!Z! e"e#Z$de%de%fddZ&e' Z(dd Z)de%ej*B ddfddZ+de%ej*B de,e% fddZ-de%ej*B de,e% fddZ.de%ej*B de,e% fddZ/de%ej*B de,e% fddZ0ddde%d e%ej*B d!e1de2fd"d#Z3								d@d$e%ej*B de%d%e%ej*B dB d&e1d'e4e%e%f dB d(e1e%B dB d)e%dB d*e1d+e%dB d,e%dB de%fd-d.Z5								d@d/e%d$e%ej*B d%e%ej*B dB d&e1d'e4e%e%f dB d(e1e%B dB d)e%dB d*e1d+e%dB d0e%dB de2fd1d2Z6dAd3ed4e%ej*B d5e4dB de,e% fd6d7Z7d8d9 Z8d:Z9	dBd;d<Z:dCd>d?Z;dS )Dz3Utilities to dynamically load objects from the Hub.    N)Path)
ModuleType)Any)is_offline_modetry_to_load_from_cache)version   )HF_MODULES_CACHE TRANSFORMERS_DYNAMIC_MODULE_NAMEcached_fileextract_commit_hashlogging)VersionComparisonsplit_package_versionnamereturnc                 C   sx   |  dd dd}|r|d  rd| }t|r*td| d|  d	 |S | s:td| d|  d
 |S )a  
    Tries to sanitize a module name so that it can be used as a Python module.

    The following transformations are applied:

    1. Replace `.` in module names with `_dot_`.
    2. Replace `-` in module names with `_hyphen_`.
    3. If the module name starts with a digit, prepend it with `_`.
    4. Warn if the sanitized name is a Python reserved keyword or not a valid identifier.

    If the input name is already a valid identifier, it is returned unchanged.
    ._dot_-_hyphen_r   _zThe module name z (originally z\) is a reserved keyword in Python. Please rename the original module to avoid import issues.z]) is not a valid Python identifier. Please rename the original module to avoid import issues.)replaceisdigitkeyword	iskeywordloggerwarningisidentifier)r   new_name r   c/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/dynamic_module_utils.py_sanitize_module_name1   s   

	r!   c                  C   sT   t tjv rdS tjt  tjt dd tt d } |  s(|   t	
  dS 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_pathr   r   r    init_hf_modulesU   s   
r/   c                 C   s`   t   tt|   }|j st|j tj|dd |d }| s.|	  t
  dS 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   dynamic_module_pathr.   r   r   r    r2   e   s   

r2   module_filec                 C   sh   t | dd}| }W d   n1 sw   Y  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.
    utf-8encodingNz^\s*import\s+\.(\S+)\s*$)flagsz^\s*from\s+\.(\S+)\s+import)openreadrefindall	MULTILINElistset)r4   fcontentrelative_importsr   r   r    get_relative_imports{   s   

rC   c                    sz   d}| g}g  |s;g }|D ]	}| t| qt| jfdd|D } fdd|D }t|dk}  | |r	 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  |  d qS ).py)str).0m)module_pathr   r    
<listcomp>   s    z-get_relative_import_files.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   rF   r@   )all_relative_importsr   r    rI      s    r   )extendrC   r   r1   len)r4   	no_changefiles_to_checknew_importsr@   new_import_filesr   )rK   rH   r    get_relative_import_files   s   

rR   filenamec                    sl   t | dd}| }W d   n1 sw   Y  t  ddl f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.
    r5   r6   Nr   c                    s  t | tjrd S t | tjr;| j}t|D ]#}t |tjr9t|jdd}|	dr/|
ds6tjj|r9 d S qn9t | tjrW| jD ]}|jdd }|rU | qDnt | tjrt| jdkrt| jrt| jdd }|rt | t| D ]}| qyd S )Nid 	availableis_flash_attnr   r   )
isinstanceastTryIftestwalkCallgetattrfuncendswith
startswithhasattrutilsimport_utilsImportnamesr   splitadd
ImportFromlevelmoduleiter_child_nodes)noder\   condition_nodecheck_functionalias
top_modulechildimported_modulesrecursive_look_for_importstransformersr   r    rv      s>   



z/get_imports.<locals>.recursive_look_for_imports)r9   r:   r?   transformers.utilsrY   parsesorted)rS   r@   rA   treer   rt   r    get_imports   s   


!r|   c                 C   s   t | }g }|D ]3}zt| W q ty; } ztd| d|  dt|v r0|| n W Y d}~qd}~ww t|dkrSt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  `)r|   r,   import_moduleImportErrorr   r   rE   r'   rM   joinrC   )rS   importsmissing_packagesimp	exceptionr   r   r    check_imports   s,   
r   Fforce_reload
class_namerH   r   c          
      C   s  t j|}|d}|t jjd}tt| }tf |r)t	j
|d t  t	j
|}tjj||d}|gtttt| }tddd |D  }|du rbtj|}	|	t	j
|< n|}	t|	dd	|kru|j|	 ||	_t|	| W  d   S 1 sw   Y  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.
    rD   r   N)location    c                 s   s     | ]}t ||  V  qd S N)bytes
read_bytesrJ   r   r   r    	<genexpr>*  s    z&get_class_in_module.<locals>.<genexpr>__transformers_module_hash__rU   )r(   r&   normpathremovesuffixr   sepr   r	   _HF_REMOTE_CODE_LOCKr%   modulespopr,   r-   getutilspec_from_file_locationrz   maprR   hashlibsha256r   	hexdigestmodule_from_specr_   loaderexec_moduler   )
r   rH   r   r   r4   cached_modulemodule_specmodule_filesmodule_hashrl   r   r   r    get_class_in_module
  s*   
$r   pretrained_model_name_or_path	cache_dirforce_downloadproxiestokenrevisionlocal_files_only	repo_type_commit_hashc
                 K   s   t  r|std d}t| } tj| }|r!ttj| }ntjj	
tt| d}t| |||	|d}g }zt| |||||||||	d
}|sS||krS|| W n tyh   td| d|  d  w t|}ttjj	 | }t| tt| }|ttj| kr||  rt|t|| s|| jjddd	 t|||  t  |D ]/}t|j| d
 }tj
| |}||  rt|t|| st|||  t  qnbt||	}|| }|tjj	 | }tj
||}tt|j ||  st|||  t  |D ]-}|| j| d
  sBt | t|j|  d
|||||||d	 || d
 qt!|dkry|du ryd
dd |D }|du rad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.
        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 `hf auth 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.
    z+Offline mode: forcing local_files_only=TrueT/)r   r   r   )r   r   r   r   r   r   r   r   zCould not locate the z inside r   )parentsr#   rD   )r   r   r   r   r   r   r   r   N
c                 S   s   g | ]}d | qS )z- r   rJ   r   r   r    rI     s    z*get_cached_module_file.<locals>.<listcomp>rU   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.)#r   r   inforE   r(   r&   isdirr!   basenamer   r   r   rh   r   r   r'   OSErrorr   r
   r2   r   r	   r*   filecmpcmpr1   mkdirshutilcopyr,   r-   r   get_cached_module_filerM   r   )r   r4   r   r   r   r   r   r   r   r   deprecated_kwargsis_local	submoduler   	new_filesresolved_module_filemodules_neededfull_submodulesubmodule_pathmodule_neededmodule_needed_filecommit_hashfull_submodule_module_file_pathrepo_type_strurlr   r   r    r   :  s   
:




r   class_referencecode_revisionc
                 K   sj   d| v r|  d\}} n|}|  d\}}|	du r||kr|}	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.
        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 `hf auth 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")
    ```z--r   NrD   )r   r   r   r   r   r   r   r   )rh   r   r   )r   r   r   r   r   r   r   r   r   r   kwargsrepo_idr4   r   final_moduler   r   r    get_class_from_dynamic_module  s$   Wr   objfolderconfigc           	         s    j dkrtd  d| d dS  fdd}t|ttfr*|D ]}|| q"n|dur2|| g }tj j  j}t	|t	|j
 }t|| || t|D ]}t	|t	|j
 }t|| || qS|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|v rYd }d } j jdrL| d j j }t dd d urKt d}|j}|dd }| d|j }n	| d j j }||f}t| tro| di }	||	 j	< |	| d< d S t| dd d ur|| j
 j	< d S  j	|i| _
d S )Nr   	TokenizerFastslow_tokenizer_classauto_map)	__class__
__module__rh   __name__ra   r_   rX   dictr   _auto_classr   )
_configmodule_namelast_module	full_namer   fast_tokenizer_classslow_tokenizerslow_tok_module_namelast_slow_tok_moduler   r   r   r    _set_auto_map_in_config`  s.   


z3custom_object_save.<locals>._set_auto_map_in_config)r   r   r   rX   r>   tupler%   r   __file__r   r   r   r   r'   rR   )	r   r   r   r   cfgresultobject_file	dest_fileneeded_filer   r   r    custom_object_saveJ  s,   
 

r   c                 C   s   t d)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.)
ValueError)signumframer   r   r    _raise_timeout_error  s   r      c              
   C   s  |du r2|durd| d| d| d}nt j|r)d| dt j| d}n	d| d| d}| du r|r;d} n||rtd	krd}zZz6ttjt}tt | du rtt	| d
| d}|
 dv rhd} n|
 dv rpd} | du sUtd	 W n ty   t| d
| dw W |durttj| td	 n|durttj| td	 w w |rtdd |r|s| st| d
| d| S )a  
    Resolves the `trust_remote_code` argument. If there is remote code to be loaded, the user must opt-in to loading
    it.

    Args:
        trust_remote_code (`bool` or `None`):
            User-defined `trust_remote_code` value.
        model_name (`str`):
            The name of the model repository in huggingface.co.
        has_local_code (`bool`):
            Whether the model has local code.
        has_remote_code (`bool`):
            Whether the model has remote code.
        error_message (`str`, *optional*):
            Custom error message to display if there is remote code to load and the user didn't opt-in. If unset, the error
            message will be regarding loading a model with custom code.

    Returns:
        The resolved `trust_remote_code` value.
    NzThe repository z% references custom code contained in zm which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/z .
zt contains custom code which must be executed to correctly load the model. You can inspect the repository content at z contains custom code which must be executed to correctly load the model. You can inspect the repository content at https://hf.co/Fr   z9 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0rU   zS.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.)r(   r&   r   abspathTIME_OUT_REMOTE_CODEsignalSIGALRMr   alarminputlower	Exceptionr   )trust_remote_code
model_namehas_local_codehas_remote_codeerror_messageupstream_repoprev_sig_handleranswerr   r   r    resolve_trust_remote_code  sl   




r  requirements.txtc              	   K   s\  g }zt d| |d|}t|d}| }W d   n1 s!w   Y  |D ]i}| }|r5|dr6q(z	t|\}}}	W n tyN   |}d\}}	Y nw ztj	|}
W n tjj
yj   || d Y q(w |dur|	durt|t	|
t	|	}nd}|s|| d|
 d	 q(W n	 ty   Y nw |rtd
|  dd| dS )a  
    Tries to locate `requirements_file` in a local folder or repo, and confirms that the environment has all the
    python dependencies installed.

    Args:
        path_or_repo_id (`str` or `os.PathLike`):
            This can be either:
            - a string, the *model id* of a model repo on huggingface.co.
            - a path to a *directory* potentially containing the file.
        kwargs (`dict[str, Any]`, *optional*):
            Additional arguments to pass to `cached_file`.
    )path_or_repo_idrS   rN#NNz (installed: None)Tz (installed: )z4Missing requirements in your local environment for `z`:
r   r   )r   r9   	readlinesstriprb   r   r   r,   metadatar   PackageNotFoundErrorr'   r   from_stringry   r   r   r   )r  requirements_filer   failedrequirementsr@   requirementpackage_name	delimiterversion_numberlocal_package_versionis_satisfiedr   r   r    check_python_requirements  sN   
r   )NFNNNFNNr   r  )r  )<__doc__rY   r   r   r,   importlib.metadataimportlib.utilr   r(   r;   r   r   r%   	threadingpathlibr   typesr   typingr   huggingface_hubr   r   	packagingr   rd   r	   r
   r   r   r   utils.import_utilsr   r   
get_loggerr   r   rE   r!   Lockr   r/   PathLiker2   r>   rC   rR   r|   r   booltyper   r   r   r   r   r   r   r  r   r   r   r   r    <module>   s   
! 7'
3
	

 &
	

*nL
U