

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_	:			Optional[Any]  	=      get_tests_dir('fixtures/dummy-config.json')



class    _a						(unittest.TestCase    ):


					'''simple docstring'''






					def   __A      (			self      ):
						A__							:   Optional[Any]					       =		0






					def   __A      (			self      ):
						self.assertIsNotNone(transformers.models.auto.__spec__      )
						self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""      )      )






					def   __A      (			self      ):
						A__							:   str					       =		AutoConfig.from_pretrained("""bert-base-uncased"""      )
						self.assertIsInstance(A__   ,	A__      )






					def   __A      (			self      ):
						A__							:   List[Any]					       =		AutoConfig.from_pretrained(A__      )
						self.assertIsInstance(A__   ,	A__      )






					def   __A      (			self      ):
						A__							:   List[Any]					       =		AutoConfig.from_pretrained(A__      )
						self.assertIsInstance(A__   ,	A__      )






					def   __A      (			self      ):
						A__							:   List[Any]					       =		AutoConfig.for_model("""roberta"""      )
						self.assertIsInstance(A__   ,	A__      )






					def   __A      (			self      ):
						with tempfile.TemporaryDirectory() as tmp_dir:
							# This model name contains bert and roberta, but roberta ends up being picked.
							A__							:   str					       =		os.path.join(A__   ,	"""fake-roberta"""      )
							os.makedirs(A__   ,	exist_ok=A__      )
							with open(os.path.join(A__   ,	"""config.json"""      )   ,	"""w"""      ) as f:
								f.write(json.dumps({}      )      )
							A__							:   Tuple					       =		AutoConfig.from_pretrained(A__      )
							self.assertEqual(type(A__      )   ,	A__      )






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

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

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






					def   __A      (			self      ):
						with self.assertRaisesRegex(
						    A__   ,	"""bert-base is not a local folder and is not a valid model identifier"""      ):
							A__							:   Any					       =		AutoConfig.from_pretrained("""bert-base"""      )






					def   __A      (			self      ):
						with self.assertRaisesRegex(
						    A__   ,	r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""      ):
							A__							:   Tuple					       =		AutoConfig.from_pretrained(A__   ,	revision="""aaaaaa"""      )






					def   __A      (			self      ):
						with self.assertRaisesRegex(
						    A__   ,	"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""   ,	):
							A__							:   List[Any]					       =		AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""      )






					def   __A      (			self      ):
						# If remote code is not set, we will time out when asking whether to load the model.
						with self.assertRaises(A__      ):
							A__							:   Dict					       =		AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""      )
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(A__      ):
							A__							:   Optional[Any]					       =		AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,	trust_remote_code=A__      )

						A__							:   List[str]					       =		AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,	trust_remote_code=A__      )
						self.assertEqual(config.__class__.__name__   ,	"""NewModelConfig"""      )

						# Test config can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
							config.save_pretrained(A__      )
							A__							:   Union[str, Any]					       =		AutoConfig.from_pretrained(A__   ,	trust_remote_code=A__      )
						self.assertEqual(reloaded_config.__class__.__name__   ,	"""NewModelConfig"""      )






					def   __A      (			self      ):



						class    _a						(__magic_name__    ):


											'''simple docstring'''






											UpperCAmelCase__:       Union[str, Any]  							=	'''new-model'''

						try:
							AutoConfig.register("""new-model"""   ,	A__      )
							# If remote code is not set, the default is to use local
							A__							:   Union[str, Any]					       =		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.
							A__							:   Dict					       =		AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,	trust_remote_code=A__      )
							self.assertEqual(config.__class__.__name__   ,	"""NewModelConfigLocal"""      )

							# If remote is enabled, we load from the Hub
							A__							:   List[str]					       =		AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""   ,	trust_remote_code=A__      )
							self.assertEqual(config.__class__.__name__   ,	"""NewModelConfig"""      )

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






from collections import OrderedDict

from ...utils import logging
from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update
from .configuration_auto import CONFIG_MAPPING_NAMES


A_	:			Union[str, Any]  	=      logging.get_logger(__name__)


A_	:			int  	=      OrderedDict(
    [
        # Base model mapping
        ('albert', 'FlaxAlbertModel'),
        ('bart', 'FlaxBartModel'),
        ('beit', 'FlaxBeitModel'),
        ('bert', 'FlaxBertModel'),
        ('big_bird', 'FlaxBigBirdModel'),
        ('blenderbot', 'FlaxBlenderbotModel'),
        ('blenderbot-small', 'FlaxBlenderbotSmallModel'),
        ('clip', 'FlaxCLIPModel'),
        ('distilbert', 'FlaxDistilBertModel'),
        ('electra', 'FlaxElectraModel'),
        ('gpt-sw3', 'FlaxGPT2Model'),
        ('gpt2', 'FlaxGPT2Model'),
        ('gpt_neo', 'FlaxGPTNeoModel'),
        ('gptj', 'FlaxGPTJModel'),
        ('longt5', 'FlaxLongT5Model'),
        ('marian', 'FlaxMarianModel'),
        ('mbart', 'FlaxMBartModel'),
        ('mt5', 'FlaxMT5Model'),
        ('opt', 'FlaxOPTModel'),
        ('pegasus', 'FlaxPegasusModel'),
        ('regnet', 'FlaxRegNetModel'),
        ('resnet', 'FlaxResNetModel'),
        ('roberta', 'FlaxRobertaModel'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormModel'),
        ('roformer', 'FlaxRoFormerModel'),
        ('t5', 'FlaxT5Model'),
        ('vision-text-dual-encoder', 'FlaxVisionTextDualEncoderModel'),
        ('vit', 'FlaxViTModel'),
        ('wav2vec2', 'FlaxWav2Vec2Model'),
        ('whisper', 'FlaxWhisperModel'),
        ('xglm', 'FlaxXGLMModel'),
        ('xlm-roberta', 'FlaxXLMRobertaModel'),
    ]
)

A_	:			Tuple  	=      OrderedDict(
    [
        # Model for pre-training mapping
        ('albert', 'FlaxAlbertForPreTraining'),
        ('bart', 'FlaxBartForConditionalGeneration'),
        ('bert', 'FlaxBertForPreTraining'),
        ('big_bird', 'FlaxBigBirdForPreTraining'),
        ('electra', 'FlaxElectraForPreTraining'),
        ('longt5', 'FlaxLongT5ForConditionalGeneration'),
        ('mbart', 'FlaxMBartForConditionalGeneration'),
        ('mt5', 'FlaxMT5ForConditionalGeneration'),
        ('roberta', 'FlaxRobertaForMaskedLM'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'),
        ('roformer', 'FlaxRoFormerForMaskedLM'),
        ('t5', 'FlaxT5ForConditionalGeneration'),
        ('wav2vec2', 'FlaxWav2Vec2ForPreTraining'),
        ('whisper', 'FlaxWhisperForConditionalGeneration'),
        ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'),
    ]
)

A_	:			Tuple  	=      OrderedDict(
    [
        # Model for Masked LM mapping
        ('albert', 'FlaxAlbertForMaskedLM'),
        ('bart', 'FlaxBartForConditionalGeneration'),
        ('bert', 'FlaxBertForMaskedLM'),
        ('big_bird', 'FlaxBigBirdForMaskedLM'),
        ('distilbert', 'FlaxDistilBertForMaskedLM'),
        ('electra', 'FlaxElectraForMaskedLM'),
        ('mbart', 'FlaxMBartForConditionalGeneration'),
        ('roberta', 'FlaxRobertaForMaskedLM'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMaskedLM'),
        ('roformer', 'FlaxRoFormerForMaskedLM'),
        ('xlm-roberta', 'FlaxXLMRobertaForMaskedLM'),
    ]
)

A_	:			Any  	=      OrderedDict(
    [
        # Model for Seq2Seq Causal LM mapping
        ('bart', 'FlaxBartForConditionalGeneration'),
        ('blenderbot', 'FlaxBlenderbotForConditionalGeneration'),
        ('blenderbot-small', 'FlaxBlenderbotSmallForConditionalGeneration'),
        ('encoder-decoder', 'FlaxEncoderDecoderModel'),
        ('longt5', 'FlaxLongT5ForConditionalGeneration'),
        ('marian', 'FlaxMarianMTModel'),
        ('mbart', 'FlaxMBartForConditionalGeneration'),
        ('mt5', 'FlaxMT5ForConditionalGeneration'),
        ('pegasus', 'FlaxPegasusForConditionalGeneration'),
        ('t5', 'FlaxT5ForConditionalGeneration'),
    ]
)

A_	:			Union[str, Any]  	=      OrderedDict(
    [
        # Model for Image-classsification
        ('beit', 'FlaxBeitForImageClassification'),
        ('regnet', 'FlaxRegNetForImageClassification'),
        ('resnet', 'FlaxResNetForImageClassification'),
        ('vit', 'FlaxViTForImageClassification'),
    ]
)

A_	:			Union[str, Any]  	=      OrderedDict(
    [
        ('vision-encoder-decoder', 'FlaxVisionEncoderDecoderModel'),
    ]
)

A_	:			Tuple  	=      OrderedDict(
    [
        # Model for Causal LM mapping
        ('bart', 'FlaxBartForCausalLM'),
        ('bert', 'FlaxBertForCausalLM'),
        ('big_bird', 'FlaxBigBirdForCausalLM'),
        ('electra', 'FlaxElectraForCausalLM'),
        ('gpt-sw3', 'FlaxGPT2LMHeadModel'),
        ('gpt2', 'FlaxGPT2LMHeadModel'),
        ('gpt_neo', 'FlaxGPTNeoForCausalLM'),
        ('gptj', 'FlaxGPTJForCausalLM'),
        ('opt', 'FlaxOPTForCausalLM'),
        ('roberta', 'FlaxRobertaForCausalLM'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForCausalLM'),
        ('xglm', 'FlaxXGLMForCausalLM'),
        ('xlm-roberta', 'FlaxXLMRobertaForCausalLM'),
    ]
)

A_	:			Optional[int]  	=      OrderedDict(
    [
        # Model for Sequence Classification mapping
        ('albert', 'FlaxAlbertForSequenceClassification'),
        ('bart', 'FlaxBartForSequenceClassification'),
        ('bert', 'FlaxBertForSequenceClassification'),
        ('big_bird', 'FlaxBigBirdForSequenceClassification'),
        ('distilbert', 'FlaxDistilBertForSequenceClassification'),
        ('electra', 'FlaxElectraForSequenceClassification'),
        ('mbart', 'FlaxMBartForSequenceClassification'),
        ('roberta', 'FlaxRobertaForSequenceClassification'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForSequenceClassification'),
        ('roformer', 'FlaxRoFormerForSequenceClassification'),
        ('xlm-roberta', 'FlaxXLMRobertaForSequenceClassification'),
    ]
)

A_	:			Any  	=      OrderedDict(
    [
        # Model for Question Answering mapping
        ('albert', 'FlaxAlbertForQuestionAnswering'),
        ('bart', 'FlaxBartForQuestionAnswering'),
        ('bert', 'FlaxBertForQuestionAnswering'),
        ('big_bird', 'FlaxBigBirdForQuestionAnswering'),
        ('distilbert', 'FlaxDistilBertForQuestionAnswering'),
        ('electra', 'FlaxElectraForQuestionAnswering'),
        ('mbart', 'FlaxMBartForQuestionAnswering'),
        ('roberta', 'FlaxRobertaForQuestionAnswering'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForQuestionAnswering'),
        ('roformer', 'FlaxRoFormerForQuestionAnswering'),
        ('xlm-roberta', 'FlaxXLMRobertaForQuestionAnswering'),
    ]
)

A_	:			Dict  	=      OrderedDict(
    [
        # Model for Token Classification mapping
        ('albert', 'FlaxAlbertForTokenClassification'),
        ('bert', 'FlaxBertForTokenClassification'),
        ('big_bird', 'FlaxBigBirdForTokenClassification'),
        ('distilbert', 'FlaxDistilBertForTokenClassification'),
        ('electra', 'FlaxElectraForTokenClassification'),
        ('roberta', 'FlaxRobertaForTokenClassification'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForTokenClassification'),
        ('roformer', 'FlaxRoFormerForTokenClassification'),
        ('xlm-roberta', 'FlaxXLMRobertaForTokenClassification'),
    ]
)

A_	:			List[str]  	=      OrderedDict(
    [
        # Model for Multiple Choice mapping
        ('albert', 'FlaxAlbertForMultipleChoice'),
        ('bert', 'FlaxBertForMultipleChoice'),
        ('big_bird', 'FlaxBigBirdForMultipleChoice'),
        ('distilbert', 'FlaxDistilBertForMultipleChoice'),
        ('electra', 'FlaxElectraForMultipleChoice'),
        ('roberta', 'FlaxRobertaForMultipleChoice'),
        ('roberta-prelayernorm', 'FlaxRobertaPreLayerNormForMultipleChoice'),
        ('roformer', 'FlaxRoFormerForMultipleChoice'),
        ('xlm-roberta', 'FlaxXLMRobertaForMultipleChoice'),
    ]
)

A_	:			List[str]  	=      OrderedDict(
    [
        ('bert', 'FlaxBertForNextSentencePrediction'),
    ]
)

A_	:			Optional[Any]  	=      OrderedDict(
    [
        ('speech-encoder-decoder', 'FlaxSpeechEncoderDecoderModel'),
        ('whisper', 'FlaxWhisperForConditionalGeneration'),
    ]
)

A_	:			Optional[Any]  	=      OrderedDict(
    [
        ('whisper', 'FlaxWhisperForAudioClassification'),
    ]
)

A_	:			List[str]  	=      _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES)
A_	:			int  	=      _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES)
A_	:			Tuple  	=      _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES)
A_	:			Optional[Any]  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES
)
A_	:			Any  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES
)
A_	:			Optional[Any]  	=      _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES)
A_	:			Optional[int]  	=      _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES)
A_	:			int  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES
)
A_	:			Optional[int]  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES
)
A_	:			Optional[int]  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES
)
A_	:			Tuple  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES
)
A_	:			Union[str, Any]  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES
)
A_	:			int  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES
)
A_	:			int  	=      _LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       str  							=	FLAX_MODEL_MAPPING



A_	:			Any  	=      auto_class_update(FlaxAutoModel)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       List[Any]  							=	FLAX_MODEL_FOR_PRETRAINING_MAPPING



A_	:			Union[str, Any]  	=      auto_class_update(FlaxAutoModelForPreTraining, head_doc='pretraining')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Any  							=	FLAX_MODEL_FOR_CAUSAL_LM_MAPPING



A_	:			Union[str, Any]  	=      auto_class_update(FlaxAutoModelForCausalLM, head_doc='causal language modeling')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Optional[Any]  							=	FLAX_MODEL_FOR_MASKED_LM_MAPPING



A_	:			Tuple  	=      auto_class_update(FlaxAutoModelForMaskedLM, head_doc='masked language modeling')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Dict  							=	FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING



A_	:			Dict  	=      auto_class_update(
    FlaxAutoModelForSeqaSeqLM, head_doc='sequence-to-sequence language modeling', checkpoint_for_example='t5-base'
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Dict  							=	FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING



A_	:			Tuple  	=      auto_class_update(
    FlaxAutoModelForSequenceClassification, head_doc='sequence classification'
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Any  							=	FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING



A_	:			int  	=      auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc='question answering')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Tuple  							=	FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING



A_	:			Dict  	=      auto_class_update(
    FlaxAutoModelForTokenClassification, head_doc='token classification'
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       List[Any]  							=	FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING



A_	:			Union[str, Any]  	=      auto_class_update(FlaxAutoModelForMultipleChoice, head_doc='multiple choice')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       str  							=	FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING



A_	:			Optional[Any]  	=      auto_class_update(
    FlaxAutoModelForNextSentencePrediction, head_doc='next sentence prediction'
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       str  							=	FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



A_	:			List[Any]  	=      auto_class_update(
    FlaxAutoModelForImageClassification, head_doc='image classification'
)



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       Dict  							=	FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING



A_	:			Union[str, Any]  	=      auto_class_update(FlaxAutoModelForVisionaSeq, head_doc='vision-to-text modeling')



class    _a						(_BaseAutoModelClass    ):


					'''simple docstring'''






					UpperCAmelCase__:       int  							=	FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING



A_	:			List[str]  	=      auto_class_update(
    FlaxAutoModelForSpeechSeqaSeq, head_doc='sequence-to-sequence speech-to-text modeling'
)



