U
    &ºc¯N  ã                   @   sn   d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
mZmZmZ e e¡ZG dd„ deƒZdS )z( Configuration base class and utilities.é    N)ÚDictÚOptionalÚTupleé   )ÚCONFIG_NAMEÚcached_pathÚhf_bucket_urlÚis_remote_urlc                   @   s  e Zd ZU dZi Zeeef ed< dZeed< dd„ Z	e
dd„ ƒZejd	d„ ƒZd
d„ Zed dœdd„ƒZed,eee eeef dœdd„ƒZeed dœdd„ƒZeed dœdd„ƒZeedœdd„ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd-d%d&„Zd.d'd(„Zed)œd*d+„ZdS )/ÚPretrainedConfigaÅ   Base class for all configuration classes.
        Handles a few parameters common to all models' configurations as well as methods for loading/downloading/saving configurations.

        Note:
            A configuration file can be loaded and saved to disk. Loading the configuration file and using this file to initialize a model does **not** load the model weights.
            It only affects the model's configuration.

        Class attributes (overridden by derived classes):
            - ``pretrained_config_archive_map``: a python ``dict`` with `shortcut names` (string) as keys and `url` (string) of associated pretrained model configurations as values.
            - ``model_type``: a string that identifies the model type, that we serialize into the JSON file, and that we use to recreate the correct object in :class:`~transformers.AutoConfig`.

        Args:
            finetuning_task (:obj:`string` or :obj:`None`, `optional`, defaults to :obj:`None`):
                Name of the task used to fine-tune the model. This can be used when converting from an original (TensorFlow or PyTorch) checkpoint.
            num_labels (:obj:`int`, `optional`, defaults to `2`):
                Number of classes to use when the model is a classification model (sequences/tokens)
            output_attentions (:obj:`bool`, `optional`, defaults to :obj:`False`):
                Should the model returns attentions weights.
            output_hidden_states (:obj:`string`, `optional`, defaults to :obj:`False`):
                Should the model returns all hidden-states.
            torchscript (:obj:`bool`, `optional`, defaults to :obj:`False`):
                Is the model used with Torchscript (for PyTorch models).
    Úpretrained_config_archive_mapÚ Ú
model_typec                 K   s\  |  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  di ¡| _|  d	d¡| _|  d
d¡| _|  dd¡| _	|  dd¡| _
|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dd ¡| _|  dd¡| _|  dd ¡| _|  dd ¡| _|  dd ¡| _|  d d ¡| _| jd k	r†td!d"„ | j ¡ D ƒƒ| _n|  d#d$¡| _|  d%d ¡| _|  d&d ¡| _|  d'd ¡| _|  d(d ¡| _ |  d)d ¡| _!|  d*d ¡| _"|  d+d ¡| _#| ¡ D ]X\}}zt$| ||ƒ W n< t%k
rR } zt& 'd, (||| ¡¡ |‚W 5 d }~X Y nX qþd S )-NÚoutput_attentionsFÚoutput_hidden_statesÚ	use_cacheTÚtorchscriptÚuse_bfloat16Úpruned_headsÚis_encoder_decoderÚ
is_decoderÚ
max_lengthé   Ú
min_lengthr   Ú	do_sampleÚearly_stoppingÚ	num_beamsr   Útemperatureg      ð?Útop_ké2   Útop_pÚrepetition_penaltyÚlength_penaltyÚno_repeat_ngram_sizeÚbad_words_idsÚnum_return_sequencesÚarchitecturesÚfinetuning_taskÚid2labelÚlabel2idc                 s   s   | ]\}}t |ƒ|fV  qd S ©N©Úint©Ú.0ÚkeyÚvalue© r0   úD/tmp/pip-unpacked-wheel-ymerj3tt/transformers/configuration_utils.pyÚ	<genexpr>\   s     z,PretrainedConfig.__init__.<locals>.<genexpr>Ú
num_labelsé   ÚprefixÚbos_token_idÚpad_token_idÚeos_token_idÚdecoder_start_token_idÚtask_specific_paramsÚ
xla_devicez!Can't set {} with value {} for {}))Úpopr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   ÚdictÚitemsr3   r5   r6   r7   r8   r9   r:   r;   ÚsetattrÚAttributeErrorÚloggerÚerrorÚformat)ÚselfÚkwargsr.   r/   Úerrr0   r0   r1   Ú__init__:   sR    zPretrainedConfig.__init__c                 C   s
   t | jƒS r)   )Úlenr'   ©rD   r0   r0   r1   r3   v   s    zPretrainedConfig.num_labelsc                 C   s4   dd„ t |ƒD ƒ| _tt| j ¡ | j ¡ ƒƒ| _d S )Nc                 S   s   i | ]}|d   |¡“qS )zLABEL_{})rC   )r-   Úir0   r0   r1   Ú
<dictcomp>|   s      z/PretrainedConfig.num_labels.<locals>.<dictcomp>)Úranger'   r=   ÚzipÚvaluesÚkeysr(   )rD   r3   r0   r0   r1   r3   z   s    c                 C   sD   t j |¡stdƒ‚t j |t¡}| j|dd t d 	|¡¡ dS )aI  
        Save a configuration object to the directory `save_directory`, so that it
        can be re-loaded using the :func:`~transformers.PretrainedConfig.from_pretrained` class method.

        Args:
            save_directory (:obj:`string`):
                Directory where the configuration JSON file will be saved.
        zPSaving path should be a directory where the model and configuration can be savedT©Úuse_diffzConfiguration saved in {}N)
ÚosÚpathÚisdirÚAssertionErrorÚjoinr   Úto_json_filerA   ÚinforC   )rD   Zsave_directoryZoutput_config_filer0   r0   r1   Úsave_pretrained   s    	ÿþz PretrainedConfig.save_pretrained)Úreturnc                 K   s    | j |f|Ž\}}| j|f|ŽS )a¬  

        Instantiate a :class:`~transformers.PretrainedConfig` (or a derived class) from a pre-trained model configuration.

        Args:
            pretrained_model_name_or_path (:obj:`string`):
                either:
                  - a string with the `shortcut name` of a pre-trained model configuration to load from cache or
                    download, e.g.: ``bert-base-uncased``.
                  - a string with the `identifier name` of a pre-trained model configuration that was user-uploaded to
                    our S3, e.g.: ``dbmdz/bert-base-german-cased``.
                  - a path to a `directory` containing a configuration file saved using the
                    :func:`~transformers.PretrainedConfig.save_pretrained` method, e.g.: ``./my_model_directory/``.
                  - a path or url to a saved configuration JSON `file`, e.g.:
                    ``./my_model_directory/configuration.json``.
            cache_dir (:obj:`string`, `optional`):
                Path to a directory in which a downloaded pre-trained model
                configuration should be cached if the standard cache should not be used.
            kwargs (:obj:`Dict[str, any]`, `optional`):
                The values in kwargs of any keys which are configuration attributes will be used to override the loaded
                values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is
                controlled by the `return_unused_kwargs` keyword parameter.
            force_download (:obj:`bool`, `optional`, defaults to :obj:`False`):
                Force to (re-)download the model weights and configuration files and override the cached versions if they exist.
            resume_download (:obj:`bool`, `optional`, defaults to :obj:`False`):
                Do not delete incompletely recieved file. Attempt to resume the download if such a file exists.
            proxies (:obj:`Dict`, `optional`):
                A dictionary of proxy servers to use by protocol or endpoint, e.g.:
                :obj:`{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.`
                The proxies are used on each request.
            return_unused_kwargs: (`optional`) bool:
                If False, then this function returns just the final configuration object.
                If True, then this functions returns a :obj:`Tuple(config, unused_kwargs)` where `unused_kwargs` is a
                dictionary consisting of the key/value pairs whose keys are not configuration attributes: ie the part
                of kwargs which has not been used to update `config` and is otherwise ignored.

        Returns:
            :class:`PretrainedConfig`: An instance of a configuration object

        Examples::

            # We can't instantiate directly the base class `PretrainedConfig` so let's show the examples on a
            # derived class: BertConfig
            config = BertConfig.from_pretrained('bert-base-uncased')    # Download configuration from S3 and cache.
            config = BertConfig.from_pretrained('./test/saved_model/')  # E.g. config (or model) was saved using `save_pretrained('./test/saved_model/')`
            config = BertConfig.from_pretrained('./test/saved_model/my_configuration.json')
            config = BertConfig.from_pretrained('bert-base-uncased', output_attention=True, foo=False)
            assert config.output_attention == True
            config, unused_kwargs = BertConfig.from_pretrained('bert-base-uncased', output_attention=True,
                                                               foo=False, return_unused_kwargs=True)
            assert config.output_attention == True
            assert unused_kwargs == {'foo': False}

        )Úget_config_dictÚ	from_dict)ÚclsÚpretrained_model_name_or_pathrE   Úconfig_dictr0   r0   r1   Úfrom_pretrained’   s    8z PretrainedConfig.from_pretrainedN)r^   r   rZ   c              	   K   sr  |  dd¡}|  dd¡}|  dd¡}|  dd¡}|  dd¡}|dkrJ| j}||kr\|| }	nDtj |¡rxtj |t¡}	n(tj |¡sŒt|ƒr’|}	nt	|tdd}	z.t
|	|||||d	}
|
dkrÂt‚|  |
¡}W nl tk
r   ||krôd
 |	¡}nd |||t¡}t|ƒ‚Y n, tjk
r:   d |	|
¡}t|ƒ‚Y nX |
|	krXt d |	¡¡ nt d |	|
¡¡ ||fS )a  
        From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used
        for instantiating a Config using `from_dict`.

        Parameters:
            pretrained_model_name_or_path (:obj:`string`):
                The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.
            pretrained_config_archive_map: (:obj:`Dict[str, str]`, `optional`) Dict:
                A map of `shortcut names` to `url`. By default, will use the current class attribute.

        Returns:
            :obj:`Tuple[Dict, Dict]`: The dictionary that will be used to instantiate the configuration object.

        Ú	cache_dirNÚforce_downloadFÚresume_downloadÚproxiesÚlocal_files_only)ÚfilenameZuse_cdn)ra   rb   rd   rc   re   zNCouldn't reach server at '{}' to download pretrained model configuration file.z·Can't load '{}'. Make sure that:

- '{}' is a correct model identifier listed on 'https://huggingface.co/models'

- or '{}' is the correct path to a directory containing a '{}' file

z›Couldn't reach server at '{}' to download configuration file or configuration file is not a valid JSON file. Please check network or file content here: {}.zloading configuration file {}z.loading configuration file {} from cache at {})r<   r   rR   rS   rT   rV   r   Úisfiler	   r   r   ÚEnvironmentErrorÚ_dict_from_json_filerC   ÚjsonÚJSONDecodeErrorrA   rX   )r]   r^   r   rE   ra   rb   rc   rd   re   Úconfig_fileZresolved_config_filer_   Úmsgr0   r0   r1   r[   Í   sd    
ú	ÿúÿ
 þÿ
z PretrainedConfig.get_config_dict)r_   rZ   c                 K   sª   |  dd¡}| f |Ž}t|dƒr:tdd„ |j ¡ D ƒƒ|_g }| ¡ D ](\}}t||ƒrFt|||ƒ | |¡ qF|D ]}|  |d¡ qtt dt	|ƒ¡ |r¢||fS |S dS )a{  
        Constructs a `Config` from a Python dictionary of parameters.

        Args:
            config_dict (:obj:`Dict[str, any]`):
                Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved
                from a pre-trained checkpoint by leveraging the :func:`~transformers.PretrainedConfig.get_config_dict`
                method.
            kwargs (:obj:`Dict[str, any]`):
                Additional parameters from which to initialize the configuration object.

        Returns:
            :class:`PretrainedConfig`: An instance of a configuration object
        Úreturn_unused_kwargsFr   c                 s   s   | ]\}}t |ƒ|fV  qd S r)   r*   r,   r0   r0   r1   r2   6  s     z-PretrainedConfig.from_dict.<locals>.<genexpr>NzModel config %s)
r<   Úhasattrr=   r   r>   r?   ÚappendrA   rX   Ústr)r]   r_   rE   rn   ÚconfigZ	to_remover.   r/   r0   r0   r1   r\   !  s    


zPretrainedConfig.from_dict)Ú	json_filerZ   c                 C   s   |   |¡}| f |ŽS )a*  
        Constructs a `Config` from the path to a json file of parameters.

        Args:
            json_file (:obj:`string`):
                Path to the JSON file containing the parameters.

        Returns:
            :class:`PretrainedConfig`: An instance of a configuration object

        )ri   )r]   rs   r_   r0   r0   r1   Úfrom_json_fileG  s    
zPretrainedConfig.from_json_file)rs   c              	   C   s,   t |ddd}| ¡ }W 5 Q R X t |¡S )NÚrúutf-8©Úencoding)ÚopenÚreadrj   Úloads)r]   rs   ÚreaderÚtextr0   r0   r1   ri   W  s    z%PretrainedConfig._dict_from_json_filec                 C   s   | j |j kS r)   )Ú__dict__)rD   Úotherr0   r0   r1   Ú__eq__]  s    zPretrainedConfig.__eq__c                 C   s   d  | jj|  ¡ ¡S )Nz{} {})rC   Ú	__class__Ú__name__Úto_json_stringrI   r0   r0   r1   Ú__repr__`  s    zPretrainedConfig.__repr__c                 C   sH   |   ¡ }tƒ   ¡ }i }| ¡ D ]$\}}||ks:||| kr|||< q|S )a4  
        Removes all attributes from config which correspond to the default
        config attributes for better readability and serializes to a Python
        dictionary.

        Returns:
            :obj:`Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
        )Úto_dictr
   r>   )rD   r_   Zdefault_config_dictZserializable_config_dictr.   r/   r0   r0   r1   Úto_diff_dictc  s    	

zPretrainedConfig.to_diff_dictc                 C   s(   t  | j¡}t| jdƒr$| jj|d< |S )zÂ
        Serializes this instance to a Python dictionary.

        Returns:
            :obj:`Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
        r   )ÚcopyÚdeepcopyr~   ro   r   r   )rD   Úoutputr0   r0   r1   r…   z  s    zPretrainedConfig.to_dictTc                 C   s.   |dkr|   ¡ }n|  ¡ }tj|dddd S )a‹  
        Serializes this instance to a JSON string.

        Args:
            use_diff (:obj:`bool`):
                If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON string.

        Returns:
            :obj:`string`: String containing all the attributes that make up this configuration instance in JSON format.
        Tr4   )ÚindentÚ	sort_keysÚ
)r†   r…   rj   Údumps)rD   rQ   r_   r0   r0   r1   rƒ   †  s    
zPretrainedConfig.to_json_stringc              	   C   s0   t |ddd}| | j|d¡ W 5 Q R X dS )a‰  
        Save this instance to a json file.

        Args:
            json_file_path (:obj:`string`):
                Path to the JSON file in which this configuration instance's parameters will be saved.
            use_diff (:obj:`bool`):
                If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON file.
        Úwrv   rw   rP   N)ry   Úwriterƒ   )rD   Zjson_file_pathrQ   Úwriterr0   r0   r1   rW   —  s    
zPretrainedConfig.to_json_file)r_   c                 C   s"   |  ¡ D ]\}}t| ||ƒ qdS )zÏ
        Updates attributes of this class
        with attributes from `config_dict`.

        Args:
            :obj:`Dict[str, any]`: Dictionary of attributes that shall be updated for this class.
        N)r>   r?   )rD   r_   r.   r/   r0   r0   r1   Úupdate¤  s    zPretrainedConfig.update)N)T)T)r‚   Ú
__module__Ú__qualname__Ú__doc__r   r   rq   Ú__annotations__r   rG   Úpropertyr3   ÚsetterrY   Úclassmethodr`   r   r   r[   r\   rt   ri   r€   r„   r†   r…   rƒ   rW   r‘   r0   r0   r0   r1   r
      s>   
<

: ÿ 
þS%

r
   )r”   r‡   rj   ÚloggingrR   Útypingr   r   r   Z
file_utilsr   r   r   r	   Ú	getLoggerr‚   rA   Úobjectr
   r0   r0   r0   r1   Ú<module>   s   
