import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import YolosForObjectDetection, YolosModel
	from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor





class UpperCAmelCase						:


				'''simple docstring'''






				def __init__(						self		,     lowercase		,     lowercase=1_3		,     lowercase=[3_0, 3_0]		,     lowercase=2		,     lowercase=3		,     lowercase=True		,     lowercase=True		,     lowercase=3_2		,     lowercase=5		,     lowercase=4		,     lowercase=3_7		,     lowercase="gelu"		,     lowercase=0.1		,     lowercase=0.1		,     lowercase=1_0		,     lowercase=0.02		,     lowercase=3		,     lowercase=None		,     lowercase=8		,     lowercase=1_0		,     ):



						"""simple docstring"""





						A_		:    Optional[int]         =				parent
						A_		:    Union[str, Any]         =				batch_size
						A_		:    Optional[Any]         =				image_size
						A_		:    List[Any]         =				patch_size
						A_		:    List[Any]         =				num_channels
						A_		:    List[Any]         =				is_training
						A_		:    Optional[int]         =				use_labels
						A_		:    List[Any]         =				hidden_size
						A_		:    List[str]         =				num_hidden_layers
						A_		:    Union[str, Any]         =				num_attention_heads
						A_		:    Any         =				intermediate_size
						A_		:    Dict         =				hidden_act
						A_		:    Tuple         =				hidden_dropout_prob
						A_		:    Tuple         =				attention_probs_dropout_prob
						A_		:    int         =				type_sequence_label_size
						A_		:    Union[str, Any]         =				initializer_range
						A_		:    int         =				num_labels
						A_		:    int         =				scope
						A_		:    List[Any]         =				n_targets
						A_		:    Optional[Any]         =				num_detection_tokens
						# we set the expected sequence length (which is used in several tests)
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
						A_		:    Union[str, Any]         =				(image_size[1] // patch_size) * (image_size[0] // patch_size)
						A_		:    Any         =				num_patches + 1 + self.num_detection_tokens
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )

						A_		:    List[Any]         =				None
						if self.use_labels:
								# labels is a list of Dict (each Dict being the labels for a given example in the batch)
								A_		:    Any         =				[]
								for i in range(self.batch_size ):
										A_		:    List[Any]         =				{}
										A_		:    Dict         =				torch.randint(
										    high=self.num_labels		,     size=(self.n_targets,)		,     device=lowercase )
										A_		:    Any         =				torch.rand(self.n_targets		,     4		,     device=lowercase )
										labels.append(lowercase )

						A_		:    Tuple         =				self.get_config()

						return config, pixel_values, labels
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						return YolosConfig(
						    image_size=self.image_size		,     patch_size=self.patch_size		,     num_channels=self.num_channels		,     hidden_size=self.hidden_size		,     num_hidden_layers=self.num_hidden_layers		,     num_attention_heads=self.num_attention_heads		,     intermediate_size=self.intermediate_size		,     hidden_act=self.hidden_act		,     hidden_dropout_prob=self.hidden_dropout_prob		,     attention_probs_dropout_prob=self.attention_probs_dropout_prob		,     is_decoder=lowercase		,     initializer_range=self.initializer_range		,     num_detection_tokens=self.num_detection_tokens		,     num_labels=self.num_labels		,     )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    Optional[int]         =				YolosModel(config=lowercase )
						model.to(lowercase )
						model.eval()
						A_		:    str         =				model(lowercase )
						self.parent.assertEqual(
						    result.last_hidden_state.shape		,     (self.batch_size, self.expected_seq_len, self.hidden_size) )
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase ):



						"""simple docstring"""





						A_		:    List[Any]         =				YolosForObjectDetection(lowercase )
						model.to(lowercase )
						model.eval()

						A_		:    Union[str, Any]         =				model(pixel_values=lowercase )
						A_		:    List[Any]         =				model(lowercase )

						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1) )
						self.parent.assertEqual(result.pred_boxes.shape		,     (self.batch_size, self.num_detection_tokens, 4) )

						A_		:    Optional[Any]         =				model(pixel_values=lowercase		,     labels=lowercase )

						self.parent.assertEqual(result.loss.shape		,     () )
						self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1) )
						self.parent.assertEqual(result.pred_boxes.shape		,     (self.batch_size, self.num_detection_tokens, 4) )







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    str         =				self.prepare_config_and_inputs()
						A_       ,      A_       ,      A_		:    Optional[Any]         =				config_and_inputs
						A_		:    Union[str, Any]         =				{'pixel_values': pixel_values}
						return config, inputs_dict





@require_torch
class UpperCAmelCase						( __A ,	__A ,	unittest.TestCase  ):


				'''simple docstring'''






				lowerCamelCase_    = (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				lowerCamelCase_    = (
				    {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
				)

				lowerCamelCase_    = False
				lowerCamelCase_    = False
				lowerCamelCase_    = False
				lowerCamelCase_    = False
				def   lowerCAmelCase_       (						self		,     lowercase		,     lowercase		,     lowercase=False ):



						"""simple docstring"""





						A_		:    str         =				super()._prepare_for_class(lowercase		,     lowercase		,     return_labels=lowercase )

						if return_labels:
								if model_class.__name__ == "YolosForObjectDetection":
										A_		:    Optional[int]         =				[]
										for i in range(self.model_tester.batch_size ):
												A_		:    List[Any]         =				{}
												A_		:    List[Any]         =				torch.ones(
												    size=(self.model_tester.n_targets,)		,     device=lowercase		,     dtype=torch.long )
												A_		:    Tuple         =				torch.ones(
												    self.model_tester.n_targets		,     4		,     device=lowercase		,     dtype=torch.float )
												labels.append(lowercase )
										A_		:    Optional[Any]         =				labels

						return inputs_dict
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Tuple         =				YolosModelTester(self )
						A_		:    Any         =				ConfigTester(self		,     config_class=lowercase		,     has_text_modality=lowercase		,     hidden_size=3_7 )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						self.config_tester.run_common_tests()
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						pass
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_       ,      A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    Tuple         =				model_class(lowercase )
								self.assertIsInstance(model.get_input_embeddings()		,     (nn.Module) )
								A_		:    Optional[Any]         =				model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(lowercase		,     nn.Linear ) )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_       ,      A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    int         =				model_class(lowercase )
								A_		:    Optional[Any]         =				inspect.signature(model.forward )
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								A_		:    int         =				[*signature.parameters.keys()]

								A_		:    Any         =				['pixel_values']
								self.assertListEqual(arg_names[:1]		,     lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_       ,      A_		:    str         =				self.model_tester.prepare_config_and_inputs_for_common()
						A_		:    Dict         =				True

						# in YOLOS, the seq_len is different
						A_		:    str         =				self.model_tester.expected_seq_len
						for model_class in self.all_model_classes:
								A_		:    Tuple         =				True
								A_		:    List[Any]         =				False
								A_		:    Any         =				True
								A_		:    Union[str, Any]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    List[str]         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )
								A_		:    Optional[Any]         =				outputs.attentions
								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )

								# check that output_attentions also work using config
								del inputs_dict["output_attentions"]
								A_		:    Tuple         =				True
								A_		:    Optional[int]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    Any         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )
								A_		:    List[Any]         =				outputs.attentions
								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )

								self.assertListEqual(
								    list(attentions[0].shape[-3:] )		,     [self.model_tester.num_attention_heads, seq_len, seq_len]		,     )
								A_		:    List[Any]         =				len(lowercase )

								# Check attention is always last and order is fine
								A_		:    List[Any]         =				True
								A_		:    List[str]         =				True
								A_		:    int         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()
								with torch.no_grad():
										A_		:    Tuple         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )

								A_		:    Optional[int]         =				1
								self.assertEqual(out_len + added_hidden_states		,     len(lowercase ) )

								A_		:    Tuple         =				outputs.attentions

								self.assertEqual(len(lowercase )		,     self.model_tester.num_hidden_layers )
								self.assertListEqual(
								    list(self_attentions[0].shape[-3:] )		,     [self.model_tester.num_attention_heads, seq_len, seq_len]		,     )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						def check_hidden_states_output(lowercase		,     lowercase		,     lowercase ):
								A_		:    List[str]         =				model_class(lowercase )
								model.to(lowercase )
								model.eval()

								with torch.no_grad():
										A_		:    List[Any]         =				model(**self._prepare_for_class(lowercase		,     lowercase ) )

								A_		:    Dict         =				outputs.hidden_states

								A_		:    List[str]         =				getattr(
								    self.model_tester		,     'expected_num_hidden_layers'		,     self.model_tester.num_hidden_layers + 1 )
								self.assertEqual(len(lowercase )		,     lowercase )

								# YOLOS has a different seq_length
								A_		:    Optional[Any]         =				self.model_tester.expected_seq_len

								self.assertListEqual(
								    list(hidden_states[0].shape[-2:] )		,     [seq_length, self.model_tester.hidden_size]		,     )

						A_       ,      A_		:    Union[str, Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
								A_		:    Union[str, Any]         =				True
								check_hidden_states_output(lowercase		,     lowercase		,     lowercase )

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								A_		:    Any         =				True

								check_hidden_states_output(lowercase		,     lowercase		,     lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_object_detection(*lowercase )







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								A_		:    Union[str, Any]         =				YolosModel.from_pretrained(lowercase )
								self.assertIsNotNone(lowercase )




def     UpperCamelCase						(	):
		'''simple docstring'''




		A_		:    int         =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
		return image





@require_torch
@require_vision
class UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				@cached_property
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						return AutoImageProcessor.from_pretrained('hustvl/yolos-small' ) if is_vision_available() else None







				@slow
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				YolosForObjectDetection.from_pretrained('hustvl/yolos-small' ).to(lowercase )

						A_		:    Dict         =				self.default_image_processor
						A_		:    int         =				prepare_img()
						A_		:    Union[str, Any]         =				image_processor(images=lowercase		,     return_tensors='pt' ).to(lowercase )

						# forward pass
						with torch.no_grad():
								A_		:    Any         =				model(inputs.pixel_values )

						# verify outputs
						A_		:    Any         =				torch.Size((1, 1_0_0, 9_2) )
						self.assertEqual(outputs.logits.shape		,     lowercase )

						A_		:    int         =				torch.tensor(
						    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]		,     device=lowercase		,     )
						A_		:    List[str]         =				torch.tensor(
						    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]		,     device=lowercase )
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,     lowercase		,     atol=1E-4 ) )
						self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]		,     lowercase		,     atol=1E-4 ) )

						# verify postprocessing
						A_		:    List[str]         =				image_processor.post_process_object_detection(
						    lowercase		,     threshold=0.3		,     target_sizes=[image.size[::-1]] )[0]
						A_		:    Dict         =				torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861] ).to(lowercase )
						A_		:    Optional[int]         =				[7_5, 7_5, 1_7, 6_3, 1_7]
						A_		:    List[str]         =				torch.tensor([335.0609, 79.3848, 375.4216, 187.2495] ).to(lowercase )

						self.assertEqual(len(results['scores'] )		,     5 )
						self.assertTrue(torch.allclose(results['scores']		,     lowercase		,     atol=1E-4 ) )
						self.assertSequenceEqual(results['labels'].tolist()		,     lowercase )
						self.assertTrue(torch.allclose(results['boxes'][0, :]		,     lowercase ) )







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


_UpperCAmelCase            =		{
    """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""": 128,
    """add_cross_attention""": True,
    """tie_encoder_decoder""": True,
    """max_length""": 50,
    """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""": 10,
    """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""": 10,
    """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 UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				@classmethod
				def   lowerCAmelCase_       (						cls ):



						"""simple docstring"""





						A_		:    Optional[int]         =				TOKEN
						HfFolder.save_token(lowercase )
				@classmethod
				def   lowerCAmelCase_       (						cls ):



						"""simple docstring"""





						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   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Union[str, Any]         =				BertConfig(
						    vocab_size=9_9		,     hidden_size=3_2		,     num_hidden_layers=5		,     num_attention_heads=4		,     intermediate_size=3_7 )
						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(lowercase		,     getattr(lowercase		,     lowercase ) )

        # 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(lowercase		,     repo_id='test-config'		,     push_to_hub=lowercase		,     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(lowercase		,     getattr(lowercase		,     lowercase ) )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[str]         =				BertConfig(
						    vocab_size=9_9		,     hidden_size=3_2		,     num_hidden_layers=5		,     num_attention_heads=4		,     intermediate_size=3_7 )
						config.push_to_hub('valid_org/test-config-org'		,     use_auth_token=self._token )

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

        # 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(
								    lowercase		,     repo_id='valid_org/test-config-org'		,     push_to_hub=lowercase		,     use_auth_token=self._token )

						A_		:    Optional[Any]         =				BertConfig.from_pretrained('valid_org/test-config-org' )
						for k, v in config.to_dict().items():
								if k != "transformers_version":
										self.assertEqual(lowercase		,     getattr(lowercase		,     lowercase ) )







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						CustomConfig.register_for_auto_class()
						A_		:    Optional[int]         =				CustomConfig(attribute=4_2 )

						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[int]         =				AutoConfig.from_pretrained(F'''{USER}/test-dynamic-config'''		,     trust_remote_code=lowercase )
						# 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		,     4_2 )





class UpperCAmelCase						( unittest.TestCase  ):


				'''simple docstring'''






				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				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_		:    List[str]         =				c.resid_pdrop + 1.0  # float
						A_		:    str         =				not c.scale_attn_weights  # bool
						A_		:    Optional[int]         =				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(lowercase		,     c.n_embd		,     'mismatch for key: n_embd' )
						self.assertEqual(lowercase		,     c.resid_pdrop		,     'mismatch for key: resid_pdrop' )
						self.assertEqual(lowercase		,     c.scale_attn_weights		,     'mismatch for key: scale_attn_weights' )
						self.assertEqual(lowercase		,     c.summary_type		,     'mismatch for key: summary_type' )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Optional[int]         =				PretrainedConfig()
						A_		:    int         =				[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(
						    lowercase		,     ['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version'] )
						A_		:    List[str]         =				[key for key, value in config_common_kwargs.items() if value == getattr(lowercase		,     lowercase )]
						if len(lowercase ) > 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(lowercase )}.''' )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						with self.assertRaises(lowercase ):
								# config is in subfolder, the following should not work without specifying the subfolder
								A_		:    Union[str, Any]         =				BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder' )

						A_		:    Optional[int]         =				BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder'		,     subfolder='bert' )

						self.assertIsNotNone(lowercase )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				mock.Mock()
						A_		:    int         =				5_0_0
						A_		:    Union[str, Any]         =				{}
						A_		:    List[str]         =				HTTPError
						A_		:    List[Any]         =				{}

						# Download this model to make sure it's in the cache.
						A_		:    Tuple         =				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=lowercase ) as mock_head:
								A_		:    Optional[int]         =				BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert' )
								# This check we did call the fake head request
								mock_head.assert_called()
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    Any         =				BertConfig.from_pretrained(
						    'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json' )
				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    int         =				AutoConfig.from_pretrained('bert-base-cased' )
						A_		:    Tuple         =				['config.4.0.0.json']

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

								# This should pick the new configuration file as the version of Transformers is > 4.0.0
								A_		:    Tuple         =				AutoConfig.from_pretrained(lowercase )
								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_		:    int         =				['config.42.0.0.json']
								A_		:    str         =				7_6_8
								configuration.save_pretrained(lowercase )
								shutil.move(os.path.join(lowercase		,     'config.4.0.0.json' )		,     os.path.join(lowercase		,     'config.42.0.0.json' ) )
								A_		:    str         =				AutoConfig.from_pretrained(lowercase )
								self.assertEqual(new_configuration.hidden_size		,     7_6_8 )







				def   lowerCAmelCase_       (						self ):



						"""simple docstring"""





						A_		:    List[Any]         =				'hf-internal-testing/test-two-configs'

						import transformers as new_transformers

						A_		:    List[Any]         =				'v4.0.0'
						A_       ,      A_		:    List[str]         =				new_transformers.models.auto.AutoConfig.from_pretrained(
						    lowercase		,     return_unused_kwargs=lowercase )
						self.assertEqual(new_configuration.hidden_size		,     2 )
						# This checks `_configuration_file` ia not kept in the kwargs by mistake.
						self.assertDictEqual(lowercase		,     {} )

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

						A_		:    Optional[int]         =				'v3.0.0'
						A_		:    List[Any]         =				old_transformers.models.auto.AutoConfig.from_pretrained(lowercase )
						self.assertEqual(old_configuration.hidden_size		,     7_6_8 )






