U ˜€d_Jã@sjdZddlZddlZddlZddlZddlmZmZddlm Z m Z m Z m Z e  e¡ZGdd„deƒZdS)z( Configuration base class and utilities.éN)ÚDictÚTupleé)Ú CONFIG_NAMEÚ cached_pathÚ hf_bucket_urlÚ is_remote_urlc@sòeZdZUdZdZeed<dd„Zedd„ƒZ e j dd„ƒZ d d „Z e dd œd d „ƒZ e eeeefdœdd„ƒZe eddœdd„ƒZe eddœdd„ƒZe edœdd„ƒZdd„Zdd„Zdd„Zd d!„Zd+d#d$„Zd,d%d&„Zed'œd(d)„Zd*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): - ``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_hidden_states (:obj:`bool`, `optional`, defaults to :obj:`False`): Should the model returns all hidden-states. output_attentions (:obj:`bool`, `optional`, defaults to :obj:`False`): Should the model returns all attentions. torchscript (:obj:`bool`, `optional`, defaults to :obj:`False`): Is the model used with Torchscript (for PyTorch models). ÚÚ model_typec Ksh| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| di¡|_| d d¡|_| d d¡|_| d d ¡|_ | d d¡|_ | dd¡|_ | dd¡|_ | dd¡|_ | dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| dd¡|_| d d¡|_|jdk r’| d!d¡td"d#„|j ¡Dƒƒ|_n| d!d$¡|_| d%d¡|_| d&d¡|_| d'd¡|_| d(d¡|_ | d)d¡|_!| d*d¡|_"| d+d¡|_#| ¡D]X\}}zt$|||ƒWn<t%k r^}zt& 'd, (|||¡¡|‚W5d}~XYnXq dS)-NÚoutput_hidden_statesFÚoutput_attentionsÚ 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Úlabel2idÚ num_labelscss|]\}}t|ƒ|fVqdS©N©Úint©Ú.0ÚkeyÚvalue©r/ú7/home/yxchng/Downloads/elia/bert/configuration_utils.pyÚ [sz,PretrainedConfig.__init__..éÚ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 rrrrrrrrrrrrrrrrr r!r"r#r$r%r&ÚdictÚitemsr'r3r4r5r6r7r8r9ÚsetattrÚAttributeErrorÚloggerÚerrorÚformat)ÚselfÚkwargsr-r.Úerrr/r/r0Ú__init__8sT  zPretrainedConfig.__init__cCs t|jƒSr()Úlenr%©rBr/r/r0r'uszPretrainedConfig.num_labelscCs4dd„t|ƒDƒ|_tt|j ¡|j ¡ƒƒ|_dS)NcSsi|]}|d |¡“qS)zLABEL_{})rA)r,Úir/r/r0Ú {sz/PretrainedConfig.num_labels..)Úranger%r;ÚzipÚvaluesÚkeysr&)rBr'r/r/r0r'yscCsXtj |¡rtd |¡ƒ‚tj|ddtj |t¡}|j|ddt   d |¡¡dS)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. z4Provided path ({}) should be a directory, not a fileT)Úexist_ok©Úuse_diffzConfiguration saved in {}N) ÚosÚpathÚisfileÚAssertionErrorrAÚmakedirsÚjoinrÚ to_json_filer?Úinfo)rBZsave_directoryZoutput_config_filer/r/r0Úsave_pretrained~s z PretrainedConfig.save_pretrained)ÚreturncKs |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_pathrCÚ config_dictr/r/r0Úfrom_pretraineds8z PretrainedConfig.from_pretrained)r^rZc KsJ| dd¡}| dd¡}| dd¡}| dd¡}| dd¡}tj |¡rXtj |t¡}n(tj |¡slt|ƒrr|}nt|tdd}z.t ||||||d } | dkr¢t ‚|  | ¡} Wndt k rèd |›d |›d |›d t›d } t | ƒ‚Yn,t j k rd || ¡} t | ƒ‚YnX| |kr0t d |¡¡nt d || ¡¡| |fS)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. 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)ÚfilenameZuse_cdn)rarbrdrcrezCan't load config for 'z'. Make sure that: - 'zQ' is a correct model identifier listed on 'https://huggingface.co/models' - or 'z2' is the correct path to a directory containing a z 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: {}.zloading configuration file {}z.loading configuration file {} from cache at {})r:rQrRÚisdirrVrrSrrrÚEnvironmentErrorÚ_dict_from_json_fileÚjsonÚJSONDecodeErrorrAr?rX) r]r^rCrarbrcrdreZ config_fileZresolved_config_filer_Úmsgr/r/r0r[ËsJ      ú ÿ þÿ z PretrainedConfig.get_config_dict)r_rZcKsª| dd¡}|f|Ž}t|dƒr:tdd„|j ¡Dƒƒ|_g}| ¡D](\}}t||ƒrFt|||ƒ| |¡qF|D]}| |d¡qtt dt |ƒ¡|r¢||fS|SdS)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_kwargsFrcss|]\}}t|ƒ|fVqdSr(r)r+r/r/r0r1!sz-PretrainedConfig.from_dict..NzModel config %s) r:Úhasattrr;rr<r=Úappendr?rXÚstr)r]r_rCrmÚconfigÚ to_remover-r.r/r/r0r\ s      zPretrainedConfig.from_dict)Ú json_filerZcCs| |¡}|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]rsr_r/r/r0Úfrom_json_file2s zPretrainedConfig.from_json_file)rsc Cs,t|ddd}| ¡}W5QRXt |¡S)NÚrúutf-8©Úencoding)ÚopenÚreadrjÚloads)r]rsÚreaderÚtextr/r/r0riBsz%PretrainedConfig._dict_from_json_filecCs |j|jkSr()Ú__dict__)rBÚotherr/r/r0Ú__eq__HszPretrainedConfig.__eq__cCsd |jj| ¡¡S)Nz{} {})rAÚ __class__Ú__name__Úto_json_stringrGr/r/r0Ú__repr__KszPretrainedConfig.__repr__cCsH| ¡}tƒ ¡}i}| ¡D]$\}}||ks:|||kr|||<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<)rBr_Zdefault_config_dictZserializable_config_dictr-r.r/r/r0Ú to_diff_dictNs   zPretrainedConfig.to_diff_dictcCs(t |j¡}t|jdƒr$|jj|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~rnrr )rBÚoutputr/r/r0r…es   zPretrainedConfig.to_dictTcCs.|dkr| ¡}n| ¡}tj|ddddS)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. Tr2)ÚindentÚ sort_keysÚ )r†r…rjÚdumps)rBrPr_r/r/r0rƒqs  zPretrainedConfig.to_json_stringc Cs0t|ddd}| |j|d¡W5QRXdS)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. ÚwrvrwrON)ryÚwriterƒ)rBZjson_file_pathrPÚwriterr/r/r0rW‚s zPretrainedConfig.to_json_file)r_cCs"| ¡D]\}}t|||ƒqdS)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=)rBr_r-r.r/r/r0ÚupdateszPretrainedConfig.updateN)T)T)r‚Ú __module__Ú __qualname__Ú__doc__r rpÚ__annotations__rEÚpropertyr'ÚsetterrYÚ classmethodr`rrr[r\rtrir€r„r†r…rƒrWr‘r/r/r/r0r s2  =  :@%  r )r”r‡rjÚloggingrQÚtypingrrÚ file_utilsrrrrÚ getLoggerr‚r?Úobjectr r/r/r/r0Ús