


from __future__ import annotations

import unittest

from transformers import LEDConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import TFLEDForConditionalGeneration, TFLEDModel
@require_tf
class 					SCREAMING_SNAKE_CASE__    :
      snake_case__							:	int	  =		LEDConfig
      snake_case__							:	Optional[int]	  =		{}
      snake_case__							:	Optional[int]	  =		'''gelu'''
      def __init__(			self							:			Any	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			Any=1_3	,				SCREAMING_SNAKE_CASE__							:			int=7	,				SCREAMING_SNAKE_CASE__							:			Dict=True	,				SCREAMING_SNAKE_CASE__							:			int=False	,				SCREAMING_SNAKE_CASE__							:			List[str]=9_9	,				SCREAMING_SNAKE_CASE__							:			List[str]=3_2	,				SCREAMING_SNAKE_CASE__							:			List[Any]=2	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=4	,				SCREAMING_SNAKE_CASE__							:			int=3_7	,				SCREAMING_SNAKE_CASE__							:			List[Any]=0.1	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=0.1	,				SCREAMING_SNAKE_CASE__							:			str=2_0	,				SCREAMING_SNAKE_CASE__							:			List[Any]=2	,				SCREAMING_SNAKE_CASE__							:			Dict=1	,				SCREAMING_SNAKE_CASE__							:			str=0	,				SCREAMING_SNAKE_CASE__							:			Any=4	,				)						->     List[Any]:
             a_   :						Optional[Any]				=    parent
             a_   :						List[Any]				=    batch_size
             a_   :						Dict				=    seq_length
             a_   :						Tuple				=    is_training
             a_   :						Tuple				=    use_labels
             a_   :						List[Any]				=    vocab_size
             a_   :						List[str]				=    hidden_size
             a_   :						int				=    num_hidden_layers
             a_   :						Tuple				=    num_attention_heads
             a_   :						int				=    intermediate_size

             a_   :						Tuple				=    hidden_dropout_prob
             a_   :						Tuple				=    attention_probs_dropout_prob
             a_   :						List[str]				=    max_position_embeddings
             a_   :						Dict				=    eos_token_id
             a_   :						List[str]				=    pad_token_id
             a_   :						Tuple				=    bos_token_id
             a_   :						Tuple				=    attention_window

             # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size
             # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention
             # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1]
             # because its local attention only attends to `self.attention_window` and one before and one after
             a_   :						str				=    self.attention_window + 2

             # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for
             # the `test_attention_outputs` and `test_hidden_states_output` tests
             a_   :						List[str]				=    (
                 self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
             )
      def 			SCREAMING_SNAKE_CASE				(			self							:			Union[str, Any]	)						->     Optional[int]:
             a_   :						Tuple				=    ids_tensor([self.batch_size, self.seq_length - 1]	,				self.vocab_size	)
             a_   :						Any				=    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)	,				1	)
             a_   :						Optional[Any]				=    tf.concat([input_ids, eos_tensor]	,				axis=1	)

             a_   :						Union[str, Any]				=    ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size	)

             a_   :						List[str]				=    self.config_cls(
                 vocab_size=self.vocab_size	,				d_model=self.hidden_size	,				encoder_layers=self.num_hidden_layers	,				decoder_layers=self.num_hidden_layers	,				encoder_attention_heads=self.num_attention_heads	,				decoder_attention_heads=self.num_attention_heads	,				encoder_ffn_dim=self.intermediate_size	,				decoder_ffn_dim=self.intermediate_size	,				dropout=self.hidden_dropout_prob	,				attention_dropout=self.attention_probs_dropout_prob	,				max_position_embeddings=self.max_position_embeddings	,				eos_token_ids=[2]	,				bos_token_id=self.bos_token_id	,				pad_token_id=self.pad_token_id	,				decoder_start_token_id=self.pad_token_id	,				attention_window=self.attention_window	,				**self.config_updates	,				)
             a_   :						Optional[int]				=    prepare_led_inputs_dict(UpperCAmelCase_	,				UpperCAmelCase_	,				UpperCAmelCase_	)
             a_   :						Optional[int]				=    tf.concat(
                 [tf.zeros_like(UpperCAmelCase_	)[:, :-1], tf.ones_like(UpperCAmelCase_	)[:, -1:]]	,				axis=-1	,				)
             a_   :						List[str]				=    global_attention_mask
             return config, inputs_dict

      def 			SCREAMING_SNAKE_CASE				(			self							:			Any	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			Tuple	)						->     Tuple:
             a_   :						List[str]				=    TFLEDModel(config=UpperCAmelCase_	).get_decoder()
             a_   :						Any				=    inputs_dict['input_ids']

             a_   :						str				=    input_ids[:1, :]
             a_   :						List[Any]				=    inputs_dict['attention_mask'][:1, :]
             a_   :						int				=    1

             # first forward pass
             a_   :						Union[str, Any]				=    model(UpperCAmelCase_	,				attention_mask=UpperCAmelCase_	,				use_cache=UpperCAmelCase_	)

             a_				,						a_   :						Dict				=    outputs.to_tuple()

             # create hypothetical next token and extent to next_input_ids
             a_   :						str				=    ids_tensor((self.batch_size, 3)	,				config.vocab_size	)
             a_   :						Optional[Any]				=    tf.cast(ids_tensor((self.batch_size, 3)	,				2	)	,				tf.inta	)

             # append to next input_ids and
             a_   :						Optional[Any]				=    tf.concat([input_ids, next_tokens]	,				axis=-1	)
             a_   :						List[Any]				=    tf.concat([attention_mask, next_attn_mask]	,				axis=-1	)

             a_   :						str				=    model(UpperCAmelCase_	,				attention_mask=UpperCAmelCase_	)[0]
             a_   :						List[Any]				=    model(UpperCAmelCase_	,				attention_mask=UpperCAmelCase_	,				past_key_values=UpperCAmelCase_	)[0]

             self.parent.assertEqual(next_tokens.shape[1]	,				output_from_past.shape[1]	)

             # select random slice
             a_   :						int				=    int(ids_tensor((1,)	,				output_from_past.shape[-1]	)	)
             a_   :						int				=    output_from_no_past[:, -3:, random_slice_idx]
             a_   :						List[Any]				=    output_from_past[:, :, random_slice_idx]

             # test that outputs are equal for slice
             tf.debugging.assert_near(UpperCAmelCase_	,				UpperCAmelCase_	,				rtol=1E-3	)







def 			SCREAMING_SNAKE_CASE_		(     __A					:     str    ,       __A					:     List[Any]    ,       __A					:     int    ,       __A					:     List[Any]=None    ,       __A					:     List[str]=None    ,       __A					:     Tuple=None    ,       __A					:     List[str]=None    ,       )      ->						Optional[int]:

       """simple docstring"""

       if attention_mask is None:
              a_   :						Union[str, Any]				=    tf.cast(tf.math.not_equal(UpperCAmelCase_    ,       config.pad_token_id							)    ,       tf.inta							)
       if decoder_attention_mask is None:
              a_   :						Union[str, Any]				=    tf.concat(
                  [
                      tf.ones(decoder_input_ids[:, :1].shape    ,       dtype=tf.inta							),
                      tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]    ,       config.pad_token_id							)    ,       tf.inta							),
                  ]    ,       axis=-1    ,       )
       if head_mask is None:
              a_   :						int				=    tf.ones((config.encoder_layers, config.encoder_attention_heads)							)
       if decoder_head_mask is None:
              a_   :						int				=    tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
       return {
           "input_ids": input_ids,
           "attention_mask": attention_mask,
           "decoder_input_ids": decoder_input_ids,
           "decoder_attention_mask": decoder_attention_mask,
           "head_mask": head_mask,
           "decoder_head_mask": decoder_head_mask,
       }
@require_tf
class 					SCREAMING_SNAKE_CASE__    (							_lowerCAmelCase		,       _lowerCAmelCase		,       unittest.TestCase     ):
      snake_case__							:	Optional[Any]	  =		(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
      snake_case__							:	Tuple	  =		(TFLEDForConditionalGeneration,) if is_tf_available() else ()
      snake_case__							:	Optional[Any]	  =		(
          {
              '''conversational''': TFLEDForConditionalGeneration,
              '''feature-extraction''': TFLEDModel,
              '''summarization''': TFLEDForConditionalGeneration,
              '''text2text-generation''': TFLEDForConditionalGeneration,
              '''translation''': TFLEDForConditionalGeneration,
          }
          if is_tf_available()
          else {}
      )
      snake_case__							:	Any	  =		True
      snake_case__							:	List[str]	  =		False
      snake_case__							:	Tuple	  =		False
      snake_case__							:	Optional[Any]	  =		False
      def 			SCREAMING_SNAKE_CASE				(			self							:			str	)						->     str:
             a_   :						Any				=    TFLEDModelTester(self	)
             a_   :						List[Any]				=    ConfigTester(self	,				config_class=UpperCAmelCase_	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			List[Any]	)						->     int:
             self.config_tester.run_common_tests()
      def 			SCREAMING_SNAKE_CASE				(			self							:			int	)						->     Union[str, Any]:
             a_   :						Union[str, Any]				=    self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.check_decoder_model_past_large_inputs(*UpperCAmelCase_	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Union[str, Any]	)						->     Dict:
             a_				,						a_   :						Union[str, Any]				=    self.model_tester.prepare_config_and_inputs_for_common()
             a_   :						Tuple				=    tf.zeros_like(inputs_dict['attention_mask']	)
             a_   :						Union[str, Any]				=    2
             a_   :						Optional[Any]				=    tf.where(
                 tf.range(self.model_tester.seq_length	)[None, :] < num_global_attn_indices	,				1	,				inputs_dict['global_attention_mask']	,				)

             a_   :						Optional[int]				=    True
             a_   :						Union[str, Any]				=    self.model_tester.seq_length
             a_   :						Dict				=    self.model_tester.encoder_seq_length

             def check_decoder_attentions_output(SCREAMING_SNAKE_CASE__							:			Optional[Any]	):
                    a_   :						List[Any]				=    outputs.decoder_attentions
                    self.assertEqual(len(UpperCAmelCase_	)	,				self.model_tester.num_hidden_layers	)
                    self.assertListEqual(
                        list(decoder_attentions[0].shape[-3:]	)	,				[self.model_tester.num_attention_heads, seq_length, seq_length]	,				)

             def check_encoder_attentions_output(SCREAMING_SNAKE_CASE__							:			Union[str, Any]	):
                    a_   :						Optional[Any]				=    [t.numpy() for t in outputs.encoder_attentions]
                    a_   :						List[Any]				=    [t.numpy() for t in outputs.encoder_global_attentions]
                    self.assertEqual(len(UpperCAmelCase_	)	,				self.model_tester.num_hidden_layers	)
                    self.assertEqual(len(UpperCAmelCase_	)	,				self.model_tester.num_hidden_layers	)
                    self.assertListEqual(
                        list(attentions[0].shape[-3:]	)	,				[self.model_tester.num_attention_heads, seq_length, seq_length]	,				)
                    self.assertListEqual(
                        list(global_attentions[0].shape[-3:]	)	,				[self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices]	,				)

             for model_class in self.all_model_classes:
                    a_   :						Any				=    True
                    a_   :						Union[str, Any]				=    False
                    a_   :						List[Any]				=    False
                    a_   :						Tuple				=    model_class(UpperCAmelCase_	)
                    a_   :						Optional[int]				=    model(self._prepare_for_class(UpperCAmelCase_	,				UpperCAmelCase_	)	)
                    a_   :						Optional[int]				=    len(UpperCAmelCase_	)
                    self.assertEqual(config.output_hidden_states	,				UpperCAmelCase_	)
                    check_encoder_attentions_output(UpperCAmelCase_	)

                    if self.is_encoder_decoder:
                           a_   :						Union[str, Any]				=    model_class(UpperCAmelCase_	)
                           a_   :						Tuple				=    model(self._prepare_for_class(UpperCAmelCase_	,				UpperCAmelCase_	)	)
                           self.assertEqual(config.output_hidden_states	,				UpperCAmelCase_	)
                           check_decoder_attentions_output(UpperCAmelCase_	)

                    # Check that output attentions can also be changed via the config
                    del inputs_dict["output_attentions"]
                    a_   :						Optional[int]				=    True
                    a_   :						Tuple				=    model_class(UpperCAmelCase_	)
                    a_   :						int				=    model(self._prepare_for_class(UpperCAmelCase_	,				UpperCAmelCase_	)	)
                    self.assertEqual(config.output_hidden_states	,				UpperCAmelCase_	)
                    check_encoder_attentions_output(UpperCAmelCase_	)

                    # Check attention is always last and order is fine
                    a_   :						str				=    True
                    a_   :						Optional[Any]				=    True
                    a_   :						Optional[Any]				=    model_class(UpperCAmelCase_	)
                    a_   :						Optional[int]				=    model(self._prepare_for_class(UpperCAmelCase_	,				UpperCAmelCase_	)	)

                    self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)	,				len(UpperCAmelCase_	)	)
                    self.assertEqual(model.config.output_hidden_states	,				UpperCAmelCase_	)
                    check_encoder_attentions_output(UpperCAmelCase_	)
      @unittest.skip('LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			List[str]	)						->     str:
             pass

      def 			SCREAMING_SNAKE_CASE				(			self							:			Optional[int]	)						->     Optional[Any]:
             pass







def 			SCREAMING_SNAKE_CASE_		(     __A					:     Dict							)      ->						Tuple:

       """simple docstring"""

       return tf.constant(UpperCAmelCase_    ,       dtype=tf.intaa							)


UpperCAmelCase_ :					Tuple       =			1e-4
@slow
@require_tf
class 					SCREAMING_SNAKE_CASE__    (							unittest.TestCase     ):
      def 			SCREAMING_SNAKE_CASE				(			self							:			str	)						->     Optional[int]:
             a_   :						Tuple				=    TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'	).led

             # change to intended input here
             a_   :						List[str]				=    _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]	)
             a_   :						Dict				=    _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]	)
             a_   :						Union[str, Any]				=    prepare_led_inputs_dict(model.config	,				UpperCAmelCase_	,				UpperCAmelCase_	)
             a_   :						Tuple				=    model(**UpperCAmelCase_	)[0]
             a_   :						str				=    (1, 1_0_2_4, 7_6_8)
             self.assertEqual(output.shape	,				UpperCAmelCase_	)
             # change to expected output here
             a_   :						Union[str, Any]				=    tf.convert_to_tensor(
                 [[2.3050, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -2.2043]]	,				)
             tf.debugging.assert_near(output[:, :3, :3]	,				UpperCAmelCase_	,				atol=1E-3	)

      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	)						->     Optional[int]:
             a_   :						List[Any]				=    TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'	)

             # change to intended input here
             a_   :						Optional[Any]				=    _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]	)
             a_   :						List[Any]				=    _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]	)
             a_   :						str				=    prepare_led_inputs_dict(model.config	,				UpperCAmelCase_	,				UpperCAmelCase_	)
             a_   :						Tuple				=    model(**UpperCAmelCase_	)[0]
             a_   :						Dict				=    (1, 1_0_2_4, model.config.vocab_size)
             self.assertEqual(output.shape	,				UpperCAmelCase_	)
             # change to expected output here
             a_   :						Optional[int]				=    tf.convert_to_tensor(
                 [[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149, 4.2783]]	,				)
             tf.debugging.assert_near(output[:, :3, :3]	,				UpperCAmelCase_	,				atol=1E-3	,				rtol=1E-3	)




"""simple docstring"""


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


SCREAMING_SNAKE_CASE			  =  {
    """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 	__a	(   unittest.TestCase						):






       @classmethod
       def 						_SCREAMING_SNAKE_CASE							(	cls			:				Union[str, Any]      )->	List[Any]:







              """simple docstring"""
              UpperCamelCase       			=		TOKEN
              HfFolder.save_token(UpperCAmelCase_      )






       @classmethod
       def 						_SCREAMING_SNAKE_CASE							(	cls			:				str      )->	Dict:







              """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 						_SCREAMING_SNAKE_CASE							(	self			:				List[Any]      )->	int:







              """simple docstring"""
              UpperCamelCase       			=		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      )

              UpperCamelCase       			=		BertConfig.from_pretrained(f"{USER}/test-config"      )
              for k, v in config.to_dict().items():
                     if k != "transformers_version":
                            self.assertEqual(UpperCAmelCase_							,   getattr(UpperCAmelCase_							,   UpperCAmelCase_      )      )

        # 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(UpperCAmelCase_							,   repo_id="test-config"							,   push_to_hub=UpperCAmelCase_							,   use_auth_token=self._token      )

              UpperCamelCase       			=		BertConfig.from_pretrained(f"{USER}/test-config"      )
              for k, v in config.to_dict().items():
                     if k != "transformers_version":
                            self.assertEqual(UpperCAmelCase_							,   getattr(UpperCAmelCase_							,   UpperCAmelCase_      )      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Tuple      )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		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      )

              UpperCamelCase       			=		BertConfig.from_pretrained("valid_org/test-config-org"      )
              for k, v in config.to_dict().items():
                     if k != "transformers_version":
                            self.assertEqual(UpperCAmelCase_							,   getattr(UpperCAmelCase_							,   UpperCAmelCase_      )      )

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

              UpperCamelCase       			=		BertConfig.from_pretrained("valid_org/test-config-org"      )
              for k, v in config.to_dict().items():
                     if k != "transformers_version":
                            self.assertEqual(UpperCAmelCase_							,   getattr(UpperCAmelCase_							,   UpperCAmelCase_      )      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				int      )->	Union[str, Any]:







              """simple docstring"""
              CustomConfig.register_for_auto_class()
              UpperCamelCase       			=		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"}      )

              UpperCamelCase       			=		AutoConfig.from_pretrained(f"{USER}/test-dynamic-config"							,   trust_remote_code=UpperCAmelCase_      )
              # 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 	__a	(   unittest.TestCase						):






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any      )->	Any:







              """simple docstring"""
              UpperCamelCase       			=		GPTaConfig()

              # attempt to modify each of int/float/bool/str config records and verify they were updated
              UpperCamelCase       			=		c.n_embd + 1  # int
              UpperCamelCase       			=		c.resid_pdrop + 1.0  # float
              UpperCamelCase       			=		not c.scale_attn_weights  # bool
              UpperCamelCase       			=		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(UpperCAmelCase_							,   c.n_embd							,   "mismatch for key: n_embd"      )
              self.assertEqual(UpperCAmelCase_							,   c.resid_pdrop							,   "mismatch for key: resid_pdrop"      )
              self.assertEqual(UpperCAmelCase_							,   c.scale_attn_weights							,   "mismatch for key: scale_attn_weights"      )
              self.assertEqual(UpperCAmelCase_							,   c.summary_type							,   "mismatch for key: summary_type"      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any      )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		PretrainedConfig()
              UpperCamelCase       			=		[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(
                  UpperCAmelCase_							,   ["is_encoder_decoder", "_name_or_path", "_commit_hash", "transformers_version"]      )
              UpperCamelCase       			=		[key for key, value in config_common_kwargs.items() if value == getattr(UpperCAmelCase_							,   UpperCAmelCase_      )]
              if len(UpperCAmelCase_      ) > 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(UpperCAmelCase_      )}."      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]      )->	Optional[Any]:







              """simple docstring"""
              with self.assertRaises(UpperCAmelCase_      ):
                     # config is in subfolder, the following should not work without specifying the subfolder
                     UpperCamelCase       			=		BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert-subfolder"      )

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

              self.assertIsNotNone(UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any      )->	Any:







              """simple docstring"""
              # A mock response for an HTTP head request to emulate server down
              UpperCamelCase       			=		mock.Mock()
              UpperCamelCase       			=		500
              UpperCamelCase       			=		{}
              UpperCamelCase       			=		HTTPError
              UpperCamelCase       			=		{}

              # Download this model to make sure it's in the cache.
              UpperCamelCase       			=		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=UpperCAmelCase_      ) as mock_head:
                     UpperCamelCase       			=		BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert"      )
                     # This check we did call the fake head request
                     mock_head.assert_called()






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Tuple      )->	int:







              """simple docstring"""
              # This test is for deprecated behavior and can be removed in v5
              UpperCamelCase       			=		BertConfig.from_pretrained(
                  "https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json"      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Union[str, Any]      )->	Optional[int]:







              """simple docstring"""
              UpperCamelCase       			=		AutoConfig.from_pretrained("bert-base-cased"      )
              UpperCamelCase       			=		["config.4.0.0.json"]

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

                     # This should pick the new configuration file as the version of Transformers is > 4.0.0
                     UpperCamelCase       			=		AutoConfig.from_pretrained(UpperCAmelCase_      )
                     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
                     UpperCamelCase       			=		["config.42.0.0.json"]
                     UpperCamelCase       			=		768
                     configuration.save_pretrained(UpperCAmelCase_      )
                     shutil.move(os.path.join(UpperCAmelCase_							,   "config.4.0.0.json"      )							,   os.path.join(UpperCAmelCase_							,   "config.42.0.0.json"      )      )
                     UpperCamelCase       			=		AutoConfig.from_pretrained(UpperCAmelCase_      )
                     self.assertEqual(new_configuration.hidden_size							,   768      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[Any]      )->	Dict:







              """simple docstring"""
              # This repo has two configuration files, one for v4.0.0 and above with a different hidden size.
              UpperCamelCase       			=		"hf-internal-testing/test-two-configs"

              import transformers as new_transformers

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

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

              UpperCamelCase       			=		"v3.0.0"
              UpperCamelCase       			=		old_transformers.models.auto.AutoConfig.from_pretrained(UpperCAmelCase_      )
              self.assertEqual(old_configuration.hidden_size							,   768      )





