



import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    XLMForMultipleChoice,
		    XLMForQuestionAnswering,
		    XLMForQuestionAnsweringSimple,
		    XLMForSequenceClassification,
		    XLMForTokenClassification,
		    XLMModel,
		    XLMWithLMHeadModel,
		)
		from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST




class 							lowerCAmelCase__  :







					def __init__(       self      :     str			,		_A      :     Tuple			,		_A      :     List[Any]=13			,		_A      :     Dict=7			,		_A      :     str=True			,		_A      :     Tuple=True			,		_A      :     str=True			,		_A      :     Optional[Any]=True			,		_A      :     str=True			,		_A      :     List[Any]=False			,		_A      :     Optional[Any]=False			,		_A      :     List[str]=False			,		_A      :     List[Any]=2			,		_A      :     List[str]=99			,		_A      :     Dict=0			,		_A      :     Optional[Any]=32			,		_A      :     Dict=5			,		_A      :     Any=4			,		_A      :     Optional[int]=0.1			,		_A      :     Dict=0.1			,		_A      :     Union[str, Any]=512			,		_A      :     Tuple=2			,		_A      :     int=0.02			,		_A      :     int=2			,		_A      :     List[Any]=4			,		_A      :     Union[str, Any]="last"			,		_A      :     int=True			,		_A      :     List[Any]=None			,		_A      :     Dict=0			,		):
											A__	:							str						    = parent
											A__	:							Dict						    = batch_size
											A__	:							List[str]						    = seq_length
											A__	:							Union[str, Any]						    = is_training
											A__	:							List[Any]						    = use_input_lengths
											A__	:							Tuple						    = use_token_type_ids
											A__	:							str						    = use_labels
											A__	:							List[str]						    = gelu_activation
											A__	:							str						    = sinusoidal_embeddings
											A__	:							Union[str, Any]						    = causal
											A__	:							Optional[int]						    = asm
											A__	:							Optional[int]						    = n_langs
											A__	:							str						    = vocab_size
											A__	:							List[Any]						    = n_special
											A__	:							Tuple						    = hidden_size
											A__	:							Union[str, Any]						    = num_hidden_layers
											A__	:							Any						    = num_attention_heads
											A__	:							str						    = hidden_dropout_prob
											A__	:							Optional[Any]						    = attention_probs_dropout_prob
											A__	:							str						    = max_position_embeddings
											A__	:							Optional[Any]						    = type_sequence_label_size
											A__	:							List[Any]						    = initializer_range
											A__	:							List[str]						    = num_labels
											A__	:							List[Any]						    = num_choices
											A__	:							Optional[Any]						    = summary_type
											A__	:							int						    = use_proj
											A__	:							Optional[int]						    = scope
											A__	:							List[str]						    = bos_token_id







					def  _lowercase			(       self      :     Dict):
											A__	:							Union[str, Any]						    = ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size)
											A__	:							List[str]						    = random_attention_mask([self.batch_size, self.seq_length])

											A__	:							Any						    = None
											if self.use_input_lengths:
																	A__	:							Any						    = (
																	    ids_tensor([self.batch_size]			,		vocab_size=2) + self.seq_length - 2
																	)  # small variation of seq_length

											A__	:							List[Any]						    = None
											if self.use_token_type_ids:
																	A__	:							List[Any]						    = ids_tensor([self.batch_size, self.seq_length]			,		self.n_langs)

											A__	:							Any						    = None
											A__	:							Any						    = None
											A__	:							List[str]						    = None
											if self.use_labels:
																	A__	:							Optional[Any]						    = ids_tensor([self.batch_size]			,		self.type_sequence_label_size)
																	A__	:							Optional[int]						    = ids_tensor([self.batch_size, self.seq_length]			,		self.num_labels)
																	A__	:							Union[str, Any]						    = ids_tensor([self.batch_size]			,		2).float()
																	A__	:							str						    = ids_tensor([self.batch_size]			,		self.num_choices)

											A__	:							Optional[Any]						    = self.get_config()

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    input_lengths,
											    sequence_labels,
											    token_labels,
											    is_impossible_labels,
											    choice_labels,
											    input_mask,
											)







					def  _lowercase			(       self      :     Union[str, Any]):
											return XLMConfig(
											    vocab_size=self.vocab_size			,		n_special=self.n_special			,		emb_dim=self.hidden_size			,		n_layers=self.num_hidden_layers			,		n_heads=self.num_attention_heads			,		dropout=self.hidden_dropout_prob			,		attention_dropout=self.attention_probs_dropout_prob			,		gelu_activation=self.gelu_activation			,		sinusoidal_embeddings=self.sinusoidal_embeddings			,		asm=self.asm			,		causal=self.causal			,		n_langs=self.n_langs			,		max_position_embeddings=self.max_position_embeddings			,		initializer_range=self.initializer_range			,		summary_type=self.summary_type			,		use_proj=self.use_proj			,		num_labels=self.num_labels			,		bos_token_id=self.bos_token_id			,		)







					def  _lowercase			(       self      :     Optional[int]			,		_A      :     Any			,		_A      :     Any			,		_A      :     Any			,		_A      :     int			,		_A      :     List[str]			,		_A      :     Optional[int]			,		_A      :     Tuple			,		_A      :     Tuple			,		_A      :     Union[str, Any]			,		):
											A__	:							str						    = XLMModel(config=_A)
											model.to(_A)
											model.eval()
											A__	:							List[str]						    = model(_A			,		lengths=_A			,		langs=_A)
											A__	:							Dict						    = model(_A			,		langs=_A)
											A__	:							int						    = model(_A)
											self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size))







					def  _lowercase			(       self      :     Any			,		_A      :     Dict			,		_A      :     int			,		_A      :     List[Any]			,		_A      :     Dict			,		_A      :     Optional[Any]			,		_A      :     Optional[Any]			,		_A      :     List[Any]			,		_A      :     Union[str, Any]			,		_A      :     Optional[Any]			,		):
											A__	:							Union[str, Any]						    = XLMWithLMHeadModel(_A)
											model.to(_A)
											model.eval()

											A__	:							Tuple						    = model(_A			,		token_type_ids=_A			,		labels=_A)
											self.parent.assertEqual(result.loss.shape			,		())
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.vocab_size))







					def  _lowercase			(       self      :     int			,		_A      :     Tuple			,		_A      :     List[str]			,		_A      :     List[Any]			,		_A      :     int			,		_A      :     Any			,		_A      :     List[str]			,		_A      :     Optional[Any]			,		_A      :     Tuple			,		_A      :     Tuple			,		):
											A__	:							Any						    = XLMForQuestionAnsweringSimple(_A)
											model.to(_A)
											model.eval()

											A__	:							Optional[int]						    = model(_A)

											A__	:							Optional[int]						    = model(_A			,		start_positions=_A			,		end_positions=_A)
											A__	:							Optional[Any]						    = outputs
											self.parent.assertEqual(result.start_logits.shape			,		(self.batch_size, self.seq_length))
											self.parent.assertEqual(result.end_logits.shape			,		(self.batch_size, self.seq_length))







					def  _lowercase			(       self      :     List[str]			,		_A      :     Dict			,		_A      :     List[Any]			,		_A      :     Optional[Any]			,		_A      :     Tuple			,		_A      :     Any			,		_A      :     Any			,		_A      :     Any			,		_A      :     int			,		_A      :     List[Any]			,		):
											A__	:							int						    = XLMForQuestionAnswering(_A)
											model.to(_A)
											model.eval()

											A__	:							Dict						    = model(_A)

											A__	:							Tuple						    = model(
											    _A			,		start_positions=_A			,		end_positions=_A			,		cls_index=_A			,		is_impossible=_A			,		p_mask=_A			,		)

											A__	:							Any						    = model(
											    _A			,		start_positions=_A			,		end_positions=_A			,		cls_index=_A			,		is_impossible=_A			,		)

											((A__					)    ,       					)	:							Dict						    = result_with_labels.to_tuple()

											A__	:							Optional[int]						    = model(_A			,		start_positions=_A			,		end_positions=_A)

											((A__					)    ,       					)	:							int						    = result_with_labels.to_tuple()

											self.parent.assertEqual(result_with_labels.loss.shape			,		())
											self.parent.assertEqual(result.start_top_log_probs.shape			,		(self.batch_size, model.config.start_n_top))
											self.parent.assertEqual(result.start_top_index.shape			,		(self.batch_size, model.config.start_n_top))
											self.parent.assertEqual(
											    result.end_top_log_probs.shape			,		(self.batch_size, model.config.start_n_top * model.config.end_n_top))
											self.parent.assertEqual(
											    result.end_top_index.shape			,		(self.batch_size, model.config.start_n_top * model.config.end_n_top))
											self.parent.assertEqual(result.cls_logits.shape			,		(self.batch_size,))







					def  _lowercase			(       self      :     Optional[Any]			,		_A      :     Dict			,		_A      :     List[Any]			,		_A      :     Optional[Any]			,		_A      :     List[Any]			,		_A      :     Optional[int]			,		_A      :     Tuple			,		_A      :     Tuple			,		_A      :     Union[str, Any]			,		_A      :     List[Any]			,		):
											A__	:							Union[str, Any]						    = XLMForSequenceClassification(_A)
											model.to(_A)
											model.eval()

											A__	:							Tuple						    = model(_A)
											A__	:							Dict						    = model(_A			,		labels=_A)
											self.parent.assertEqual(result.loss.shape			,		())
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.type_sequence_label_size))







					def  _lowercase			(       self      :     str			,		_A      :     Dict			,		_A      :     List[str]			,		_A      :     Optional[int]			,		_A      :     Optional[int]			,		_A      :     int			,		_A      :     List[str]			,		_A      :     List[Any]			,		_A      :     Dict			,		_A      :     Any			,		):
											A__	:							List[Any]						    = self.num_labels
											A__	:							Tuple						    = XLMForTokenClassification(_A)
											model.to(_A)
											model.eval()

											A__	:							int						    = model(_A			,		attention_mask=_A			,		labels=_A)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.seq_length, self.num_labels))







					def  _lowercase			(       self      :     int			,		_A      :     Dict			,		_A      :     Tuple			,		_A      :     Union[str, Any]			,		_A      :     List[Any]			,		_A      :     Any			,		_A      :     Optional[int]			,		_A      :     Optional[Any]			,		_A      :     List[Any]			,		_A      :     List[str]			,		):
											A__	:							int						    = self.num_choices
											A__	:							List[str]						    = XLMForMultipleChoice(config=_A)
											model.to(_A)
											model.eval()
											A__	:							List[str]						    = input_ids.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											A__	:							Dict						    = token_type_ids.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											A__	:							Tuple						    = input_mask.unsqueeze(1).expand(-1			,		self.num_choices			,		-1).contiguous()
											A__	:							Dict						    = model(
											    _A			,		attention_mask=_A			,		token_type_ids=_A			,		labels=_A			,		)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.num_choices))







					def  _lowercase			(       self      :     List[str]):
											A__	:							List[str]						    = self.prepare_config_and_inputs()
											(
											    (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       (
											    A__					
											)    ,       					
											)	:							Any						    = config_and_inputs
											A__	:							str						    = {"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
											return config, inputs_dict




@require_torch
class 							lowerCAmelCase__  (	lowercase_					,							lowercase_					,							lowercase_					,							unittest.TestCase						):
					_A     :    Tuple           =						(
					    (
					        XLMModel,
					        XLMWithLMHeadModel,
					        XLMForQuestionAnswering,
					        XLMForSequenceClassification,
					        XLMForQuestionAnsweringSimple,
					        XLMForTokenClassification,
					        XLMForMultipleChoice,
					    )
					    if is_torch_available()
					    else ()
					)
					_A     :    Optional[int]           =						(
					    (XLMWithLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Check other models whether language generation is also applicable
					_A     :    int           =						(
					    {
					        '''feature-extraction''': XLMModel,
					        '''fill-mask''': XLMWithLMHeadModel,
					        '''question-answering''': XLMForQuestionAnsweringSimple,
					        '''text-classification''': XLMForSequenceClassification,
					        '''text-generation''': XLMWithLMHeadModel,
					        '''token-classification''': XLMForTokenClassification,
					        '''zero-shot''': XLMForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)







					def  _lowercase			(       self      :     Union[str, Any]			,		_A      :     Optional[int]			,		_A      :     Optional[int]			,		_A      :     Optional[int]			,		_A      :     Optional[int]			,		_A      :     Union[str, Any]):
											if (
											    pipeline_test_casse_name == "QAPipelineTests"
											    and tokenizer_name is not None
											    and not tokenizer_name.endswith("Fast")
											):
																	# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																	# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																	# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																	return True

											return False







					def  _lowercase			(       self      :     Dict			,		_A      :     Dict			,		_A      :     Dict			,		_A      :     Optional[Any]=False):
											A__	:							List[Any]						    = super()._prepare_for_class(_A			,		_A			,		return_labels=_A)

											if return_labels:
																	if model_class.__name__ == "XLMForQuestionAnswering":
																							A__	:							List[Any]						    = torch.zeros(
																							    self.model_tester.batch_size			,		dtype=torch.long			,		device=_A)
																							A__	:							Optional[Any]						    = torch.zeros(
																							    self.model_tester.batch_size			,		dtype=torch.long			,		device=_A)

											return inputs_dict







					def  _lowercase			(       self      :     str):
											A__	:							Optional[int]						    = XLMModelTester(self)
											A__	:							Dict						    = ConfigTester(self			,		config_class=_A			,		emb_dim=37)







					def  _lowercase			(       self      :     Union[str, Any]):
											self.config_tester.run_common_tests()







					def  _lowercase			(       self      :     int):
											A__	:							List[str]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_model(*_A)







					def  _lowercase			(       self      :     Optional[Any]):
											A__	:							Tuple						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_lm_head(*_A)







					def  _lowercase			(       self      :     List[str]):
											A__	:							Tuple						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_simple_qa(*_A)







					def  _lowercase			(       self      :     List[str]):
											A__	:							Optional[Any]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_qa(*_A)







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							str						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_sequence_classif(*_A)







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							List[Any]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_token_classif(*_A)







					def  _lowercase			(       self      :     Any):
											A__	:							str						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_xlm_for_multiple_choice(*_A)







					def  _lowercase			(       self      :     str			,		_A      :     List[Any]			,		_A      :     Optional[int]			,		_A      :     str			,		_A      :     Optional[Any]			,		_A      :     Optional[int]			,		_A      :     Tuple=False			,		_A      :     int=1):
											self.assertIsInstance(_A			,		_A)
											self.assertListEqual(
											    [isinstance(_A			,		_A) for iter_attentions in attentions]			,		[True] * len(_A))
											self.assertEqual(len(_A)			,		(max_length - min_length) * num_beam_groups)

											for idx, iter_attentions in enumerate(_A):
																	# adds PAD dummy token
																	A__	:							int						    = min_length + idx + 1
																	A__	:							Tuple						    = min_length + idx + 1

																	A__	:							Dict						    = (
																	    batch_size * num_beam_groups,
																	    config.num_attention_heads,
																	    tgt_len,
																	    src_len,
																	)
																	# check attn size
																	self.assertListEqual(
																	    [layer_attention.shape for layer_attention in iter_attentions]			,		[expected_shape] * len(_A))







					def  _lowercase			(       self      :     Union[str, Any]			,		_A      :     str			,		_A      :     Dict			,		_A      :     Any			,		_A      :     Union[str, Any]			,		_A      :     Optional[Any]			,		_A      :     Union[str, Any]=False			,		_A      :     str=1):
											self.assertIsInstance(_A			,		_A)
											self.assertListEqual(
											    [isinstance(_A			,		_A) for iter_hidden_states in hidden_states]			,		[True] * len(_A)			,		)
											self.assertEqual(len(_A)			,		(max_length - min_length) * num_beam_groups)

											for idx, iter_hidden_states in enumerate(_A):
																	# adds PAD dummy token
																	A__	:							Union[str, Any]						    = min_length + idx + 1
																	A__	:							Tuple						    = (batch_size * num_beam_groups, seq_len, config.hidden_size)
																	# check hidden size
																	self.assertListEqual(
																	    [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]			,		[expected_shape] * len(_A)			,		)
											pass







					@slow
					def  _lowercase			(       self      :     Tuple):
											for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__	:							Any						    = XLMModel.from_pretrained(_A)
																	self.assertIsNotNone(_A)




@require_torch
class 							lowerCAmelCase__  (	unittest.TestCase						):







					@slow
					def  _lowercase			(       self      :     Optional[Any]):
											A__	:							int						    = XLMWithLMHeadModel.from_pretrained("xlm-mlm-en-2048")
											model.to(_A)
											A__	:							Optional[Any]						    = torch.tensor([[14, 447]]			,		dtype=torch.long			,		device=_A)  # the president
											A__	:							Optional[int]						    = [
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											    14,
											    447,
											]  # the president the president the president the president the president the president the president the president the president the president
											# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
											A__	:							Optional[int]						    = model.generate(_A			,		do_sample=_A)
											self.assertListEqual(output_ids[0].cpu().numpy().tolist()			,		_A)




import json
import os
import shutil
import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoConfig, BertConfig, GPTaConfig
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402


snake_case    :						List[str]  =						{
    'return_dict': False,
    'output_hidden_states': True,
    'output_attentions': True,
    'torchscript': True,
    'torch_dtype': 'float16',
    'use_bfloat16': True,
    'tf_legacy_loss': True,
    'pruned_heads': {'a': 1},
    'tie_word_embeddings': False,
    'is_decoder': True,
    'cross_attention_hidden_size': 1_2_8,
    'add_cross_attention': True,
    'tie_encoder_decoder': True,
    'max_length': 5_0,
    'min_length': 3,
    'do_sample': True,
    'early_stopping': True,
    'num_beams': 3,
    'num_beam_groups': 3,
    'diversity_penalty': 0.5,
    'temperature': 2.0,
    'top_k': 1_0,
    'top_p': 0.7,
    'typical_p': 0.2,
    'repetition_penalty': 0.8,
    'length_penalty': 0.8,
    'no_repeat_ngram_size': 5,
    'encoder_no_repeat_ngram_size': 5,
    'bad_words_ids': [1, 2, 3],
    'num_return_sequences': 3,
    'chunk_size_feed_forward': 5,
    'output_scores': True,
    'return_dict_in_generate': True,
    'forced_bos_token_id': 2,
    'forced_eos_token_id': 3,
    'remove_invalid_values': True,
    'architectures': ['BertModel'],
    'finetuning_task': 'translation',
    'id2label': {0: 'label'},
    'label2id': {'label': '0'},
    'tokenizer_class': 'BertTokenizerFast',
    'prefix': 'prefix',
    'bos_token_id': 6,
    'pad_token_id': 7,
    'eos_token_id': 8,
    'sep_token_id': 9,
    'decoder_start_token_id': 1_0,
    'exponential_decay_length_penalty': (5, 1.01),
    'suppress_tokens': [0, 1],
    'begin_suppress_tokens': 2,
    'task_specific_params': {'translation': 'some_params'},
    'problem_type': 'regression',
}




@is_staging_test
class 							lowerCAmelCase__  (	unittest.TestCase						):







					@classmethod
					def  _lowercase			(       cls      :     str):
											A__	:							int						    = TOKEN
											HfFolder.save_token(_A)







					@classmethod
					def  _lowercase			(       cls      :     Optional[Any]):
											try:
																	delete_repo(token=cls._token			,		repo_id="test-config")
											except HTTPError:
																	pass

											try:
																	delete_repo(token=cls._token			,		repo_id="valid_org/test-config-org")
											except HTTPError:
																	pass

											try:
																	delete_repo(token=cls._token			,		repo_id="test-dynamic-config")
											except HTTPError:
																	pass







					def  _lowercase			(       self      :     Dict):
											A__	:							List[str]						    = BertConfig(
											    vocab_size=99			,		hidden_size=32			,		num_hidden_layers=5			,		num_attention_heads=4			,		intermediate_size=37)
											config.push_to_hub("test-config"			,		use_auth_token=self._token)

											A__	:							Dict						    = BertConfig.from_pretrained(F'{USER}/test-config')
											for k, v in config.to_dict().items():
																	if k != "transformers_version":
																							self.assertEqual(_A			,		getattr(_A			,		_A))

        # Reset repo
											delete_repo(token=self._token			,		repo_id="test-config")

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																	config.save_pretrained(_A			,		repo_id="test-config"			,		push_to_hub=_A			,		use_auth_token=self._token)

											A__	:							Tuple						    = BertConfig.from_pretrained(F'{USER}/test-config')
											for k, v in config.to_dict().items():
																	if k != "transformers_version":
																							self.assertEqual(_A			,		getattr(_A			,		_A))







					def  _lowercase			(       self      :     List[Any]):
											A__	:							List[Any]						    = BertConfig(
											    vocab_size=99			,		hidden_size=32			,		num_hidden_layers=5			,		num_attention_heads=4			,		intermediate_size=37)
											config.push_to_hub("valid_org/test-config-org"			,		use_auth_token=self._token)

											A__	:							Union[str, Any]						    = BertConfig.from_pretrained("valid_org/test-config-org")
											for k, v in config.to_dict().items():
																	if k != "transformers_version":
																							self.assertEqual(_A			,		getattr(_A			,		_A))

        # Reset repo
											delete_repo(token=self._token			,		repo_id="valid_org/test-config-org")

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																	config.save_pretrained(
																	    _A			,		repo_id="valid_org/test-config-org"			,		push_to_hub=_A			,		use_auth_token=self._token)

											A__	:							List[str]						    = BertConfig.from_pretrained("valid_org/test-config-org")
											for k, v in config.to_dict().items():
																	if k != "transformers_version":
																							self.assertEqual(_A			,		getattr(_A			,		_A))







					def  _lowercase			(       self      :     Optional[int]):
											CustomConfig.register_for_auto_class()
											A__	:							Optional[Any]						    = CustomConfig(attribute=42)

											config.push_to_hub("test-dynamic-config"			,		use_auth_token=self._token)

											# This has added the proper auto_map field to the config
											self.assertDictEqual(config.auto_map			,		{"AutoConfig": "custom_configuration.CustomConfig"})

											A__	:							Optional[Any]						    = AutoConfig.from_pretrained(F'{USER}/test-dynamic-config'			,		trust_remote_code=_A)
											# Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module
											self.assertEqual(new_config.__class__.__name__			,		"CustomConfig")
											self.assertEqual(new_config.attribute			,		42)




class 							lowerCAmelCase__  (	unittest.TestCase						):







					def  _lowercase			(       self      :     Optional[int]):
											A__	:							List[str]						    = GPTaConfig()

											# attempt to modify each of int/float/bool/str config records and verify they were updated
											A__	:							Optional[int]						    = c.n_embd + 1  # int
											A__	:							Tuple						    = c.resid_pdrop + 1.0  # float
											A__	:							Any						    = not c.scale_attn_weights  # bool
											A__	:							List[Any]						    = c.summary_type + "foo"  # str
											c.update_from_string(
											    F'n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}')
											self.assertEqual(_A			,		c.n_embd			,		"mismatch for key: n_embd")
											self.assertEqual(_A			,		c.resid_pdrop			,		"mismatch for key: resid_pdrop")
											self.assertEqual(_A			,		c.scale_attn_weights			,		"mismatch for key: scale_attn_weights")
											self.assertEqual(_A			,		c.summary_type			,		"mismatch for key: summary_type")







					def  _lowercase			(       self      :     Optional[int]):
											A__	:							Any						    = PretrainedConfig()
											A__	:							Optional[Any]						    = [key for key in base_config.__dict__ if key not in config_common_kwargs]
											# If this part of the test fails, you have arguments to addin config_common_kwargs above.
											self.assertListEqual(
											    _A			,		["is_encoder_decoder", "_name_or_path", "_commit_hash", "transformers_version"])
											A__	:							Optional[Any]						    = [key for key, value in config_common_kwargs.items() if value == getattr(_A			,		_A)]
											if len(_A) > 0:
																	raise ValueError(
																	    "The following keys are set with the default values in"
																	    " `test_configuration_common.config_common_kwargs` pick another value for them:"
																	    F' {", ".join(_A)}.')







					def  _lowercase			(       self      :     List[str]):
											with self.assertRaises(_A):
																	# config is in subfolder, the following should not work without specifying the subfolder
																	A__	:							Any						    = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder")

											A__	:							str						    = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder"			,		subfolder="bert")

											self.assertIsNotNone(_A)







					def  _lowercase			(       self      :     List[str]):
											# A mock response for an HTTP head request to emulate server down
											A__	:							Tuple						    = mock.Mock()
											A__	:							Dict						    = 500
											A__	:							Union[str, Any]						    = {}
											A__	:							List[Any]						    = HTTPError
											A__	:							Optional[int]						    = {}

											# Download this model to make sure it's in the cache.
											A__	:							Any						    = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")

											# Under the mock environment we get a 500 error when trying to reach the model.
											with mock.patch("requests.Session.request"			,		return_value=_A) as mock_head:
																	A__	:							List[str]						    = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert")
																	# This check we did call the fake head request
																	mock_head.assert_called()







					def  _lowercase			(       self      :     Optional[int]):
											# This test is for deprecated behavior and can be removed in v5
											A__	:							List[str]						    = BertConfig.from_pretrained(
											    "https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json")







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							Union[str, Any]						    = AutoConfig.from_pretrained("bert-base-cased")
											A__	:							str						    = ["config.4.0.0.json"]

											with tempfile.TemporaryDirectory() as tmp_dir:
																	configuration.save_pretrained(_A)
																	A__	:							Dict						    = 2
																	json.dump(configuration.to_dict()			,		open(os.path.join(_A			,		"config.4.0.0.json")			,		"w"))

																	# This should pick the new configuration file as the version of Transformers is > 4.0.0
																	A__	:							Any						    = AutoConfig.from_pretrained(_A)
																	self.assertEqual(new_configuration.hidden_size			,		2)

																	# Will need to be adjusted if we reach v42 and this test is still here.
																	# Should pick the old configuration file as the version of Transformers is < 4.42.0
																	A__	:							str						    = ["config.42.0.0.json"]
																	A__	:							Optional[int]						    = 768
																	configuration.save_pretrained(_A)
																	shutil.move(os.path.join(_A			,		"config.4.0.0.json")			,		os.path.join(_A			,		"config.42.0.0.json"))
																	A__	:							Optional[int]						    = AutoConfig.from_pretrained(_A)
																	self.assertEqual(new_configuration.hidden_size			,		768)







					def  _lowercase			(       self      :     Optional[int]):
											# This repo has two configuration files, one for v4.0.0 and above with a different hidden size.
											A__	:							List[str]						    = "hf-internal-testing/test-two-configs"

											import transformers as new_transformers

											A__	:							int						    = "v4.0.0"
											A__    ,       A__	:							str						    = new_transformers.models.auto.AutoConfig.from_pretrained(
											    _A			,		return_unused_kwargs=_A)
											self.assertEqual(new_configuration.hidden_size			,		2)
											# This checks `_configuration_file` ia not kept in the kwargs by mistake.
											self.assertDictEqual(_A			,		{})

											# Testing an older version by monkey-patching the version in the module it's used.
											import transformers as old_transformers

											A__	:							List[Any]						    = "v3.0.0"
											A__	:							Optional[Any]						    = old_transformers.models.auto.AutoConfig.from_pretrained(_A)
											self.assertEqual(old_configuration.hidden_size			,		768)