
import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402


_A     =  get_tests_dir("fixtures/dummy-config.json")





class 			_lowerCAmelCase      (			unittest.TestCase ):




				def   __a		(      self  )       ->   Optional[int]:
							lowerCAmelCase_      			=			0




				def   __a		(      self  )       ->   Optional[Any]:
							self.assertIsNotNone(transformers.models.auto.__spec__  )
							self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"  )  )




				def   __a		(      self  )       ->   Dict:
							lowerCAmelCase_      			=			AutoConfig.from_pretrained("bert-base-uncased"  )
							self.assertIsInstance(_UpperCamelCase       ,       _UpperCamelCase  )




				def   __a		(      self  )       ->   Optional[int]:
							lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase  )
							self.assertIsInstance(_UpperCamelCase       ,       _UpperCamelCase  )




				def   __a		(      self  )       ->   Optional[Any]:
							lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase  )
							self.assertIsInstance(_UpperCamelCase       ,       _UpperCamelCase  )




				def   __a		(      self  )       ->   Optional[Any]:
							lowerCAmelCase_      			=			AutoConfig.for_model("roberta"  )
							self.assertIsInstance(_UpperCamelCase       ,       _UpperCamelCase  )




				def   __a		(      self  )       ->   Optional[int]:
							with tempfile.TemporaryDirectory() as tmp_dir:
										# This model name contains bert and roberta, but roberta ends up being picked.
										lowerCAmelCase_      			=			os.path.join(_UpperCamelCase       ,       "fake-roberta"  )
										os.makedirs(_UpperCamelCase       ,       exist_ok=_UpperCamelCase  )
										with open(os.path.join(_UpperCamelCase       ,       "config.json"  )       ,       "w"  ) as f:
													f.write(json.dumps({}  )  )
										lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase  )
										self.assertEqual(type(_UpperCamelCase  )       ,       _UpperCamelCase  )




				def   __a		(      self  )       ->   int:
							try:
										AutoConfig.register("custom"       ,       _UpperCamelCase  )
										# Wrong model type will raise an error
										with self.assertRaises(_UpperCamelCase  ):
													AutoConfig.register("model"       ,       _UpperCamelCase  )
										# Trying to register something existing in the Transformers library will raise an error
										with self.assertRaises(_UpperCamelCase  ):
													AutoConfig.register("bert"       ,       _UpperCamelCase  )

										# Now that the config is registered, it can be used as any other config with the auto-API
										lowerCAmelCase_      			=			CustomConfig()
										with tempfile.TemporaryDirectory() as tmp_dir:
													config.save_pretrained(_UpperCamelCase  )
													lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase  )
													self.assertIsInstance(_UpperCamelCase       ,       _UpperCamelCase  )

							finally:
										if "custom" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["custom"]




				def   __a		(      self  )       ->   Optional[int]:
							with self.assertRaisesRegex(
							    _UpperCamelCase       ,       "bert-base is not a local folder and is not a valid model identifier"  ):
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("bert-base"  )




				def   __a		(      self  )       ->   str:
							with self.assertRaisesRegex(
							    _UpperCamelCase       ,       r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"  ):
										lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase       ,       revision="aaaaaa"  )




				def   __a		(      self  )       ->   Any:
							with self.assertRaisesRegex(
							    _UpperCamelCase       ,       "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."       ,       ):
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"  )




				def   __a		(      self  )       ->   int:
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(_UpperCamelCase  ):
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"  )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(_UpperCamelCase  ):
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,       trust_remote_code=_UpperCamelCase  )

							lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,       trust_remote_code=_UpperCamelCase  )
							self.assertEqual(config.__class__.__name__       ,       "NewModelConfig"  )

							# Test config can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
										config.save_pretrained(_UpperCamelCase  )
										lowerCAmelCase_      			=			AutoConfig.from_pretrained(_UpperCamelCase       ,       trust_remote_code=_UpperCamelCase  )
							self.assertEqual(reloaded_config.__class__.__name__       ,       "NewModelConfig"  )




				def   __a		(      self  )       ->   Optional[Any]:





							class 			_lowerCAmelCase      (			__a ):
											_lowercase    	='''new-model'''

							try:
										AutoConfig.register("new-model"       ,       _UpperCamelCase  )
										# If remote code is not set, the default is to use local
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"  )
										self.assertEqual(config.__class__.__name__       ,       "NewModelConfigLocal"  )

										# If remote code is disabled, we load the local one.
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,       trust_remote_code=_UpperCamelCase  )
										self.assertEqual(config.__class__.__name__       ,       "NewModelConfigLocal"  )

										# If remote is enabled, we load from the Hub
										lowerCAmelCase_      			=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,       trust_remote_code=_UpperCamelCase  )
										self.assertEqual(config.__class__.__name__       ,       "NewModelConfig"  )

							finally:
										if "new-model" in CONFIG_MAPPING._extra_content:
													del CONFIG_MAPPING._extra_content["new-model"]





from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A     =  logging.get_logger(__name__)

_A     =  {
    "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json",
}





class 			_lowerCAmelCase      (			__a ):
				_lowercase    	='''transfo-xl'''
				_lowercase    	=['''mems''']
				_lowercase    	={
				    '''n_token''': '''vocab_size''',
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''n_head''',
				    '''num_hidden_layers''': '''n_layer''',
				}




				def __init__(      self       ,       _UpperCamelCase=267_735       ,       _UpperCamelCase=[20_000, 40_000, 200_000]       ,       _UpperCamelCase=1_024       ,       _UpperCamelCase=1_024       ,       _UpperCamelCase=16       ,       _UpperCamelCase=64       ,       _UpperCamelCase=4_096       ,       _UpperCamelCase=4       ,       _UpperCamelCase=False       ,       _UpperCamelCase=18       ,       _UpperCamelCase=1_600       ,       _UpperCamelCase=1_000       ,       _UpperCamelCase=True       ,       _UpperCamelCase=True       ,       _UpperCamelCase=0       ,       _UpperCamelCase=-1       ,       _UpperCamelCase=True       ,       _UpperCamelCase=0.1       ,       _UpperCamelCase=0.0       ,       _UpperCamelCase=True       ,       _UpperCamelCase="normal"       ,       _UpperCamelCase=0.01       ,       _UpperCamelCase=0.01       ,       _UpperCamelCase=0.02       ,       _UpperCamelCase=1e-5       ,       _UpperCamelCase=0       ,       **_UpperCamelCase       ,       )       ->   Dict:
							lowerCAmelCase_      			=			vocab_size
							lowerCAmelCase_      			=			[]
							self.cutoffs.extend(_UpperCamelCase  )
							if proj_share_all_but_first:
										lowerCAmelCase_      			=			[False] + [True] * len(self.cutoffs  )
							else:
										lowerCAmelCase_      			=			[False] + [False] * len(self.cutoffs  )
							lowerCAmelCase_      			=			d_model
							lowerCAmelCase_      			=			d_embed
							lowerCAmelCase_      			=			d_head
							lowerCAmelCase_      			=			d_inner
							lowerCAmelCase_      			=			div_val
							lowerCAmelCase_      			=			pre_lnorm
							lowerCAmelCase_      			=			n_layer
							lowerCAmelCase_      			=			n_head
							lowerCAmelCase_      			=			mem_len
							lowerCAmelCase_      			=			same_length
							lowerCAmelCase_      			=			attn_type
							lowerCAmelCase_      			=			clamp_len
							lowerCAmelCase_      			=			sample_softmax
							lowerCAmelCase_      			=			adaptive
							lowerCAmelCase_      			=			dropout
							lowerCAmelCase_      			=			dropatt
							lowerCAmelCase_      			=			untie_r
							lowerCAmelCase_      			=			init
							lowerCAmelCase_      			=			init_range
							lowerCAmelCase_      			=			proj_init_std
							lowerCAmelCase_      			=			init_std
							lowerCAmelCase_      			=			layer_norm_epsilon
							super().__init__(eos_token_id=_UpperCamelCase       ,       **_UpperCamelCase  )




				@property
				def   __a		(      self  )       ->   List[Any]:
							# Message copied from Transformer-XL documentation
							logger.info(f"""The model {self.model_type} is one of the few models that has no sequence length limit."""  )
							return -1




				@max_position_embeddings.setter
				def   __a		(      self       ,       _UpperCamelCase  )       ->   str:
							# Message copied from Transformer-XL documentation
							raise NotImplementedError(
							    f"""The model {self.model_type} is one of the few models that has no sequence length limit."""  )



