"""simple docstring"""
from collections import OrderedDict

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


SCREAMING_SNAKE_CASE__   =					logging.get_logger(__name__)


SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					OrderedDict(
    [
        # Model for Image-classsification
        ("beit", "FlaxBeitForImageClassification"),
        ("regnet", "FlaxRegNetForImageClassification"),
        ("resnet", "FlaxResNetForImageClassification"),
        ("vit", "FlaxViTForImageClassification"),
    ]
)

SCREAMING_SNAKE_CASE__   =					OrderedDict(
    [
        ("vision-encoder-decoder", "FlaxVisionEncoderDecoderModel"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					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"),
    ]
)

SCREAMING_SNAKE_CASE__   =					OrderedDict(
    [
        ("bert", "FlaxBertForNextSentencePrediction"),
    ]
)

SCREAMING_SNAKE_CASE__   =					OrderedDict(
    [
        ("speech-encoder-decoder", "FlaxSpeechEncoderDecoderModel"),
        ("whisper", "FlaxWhisperForConditionalGeneration"),
    ]
)

SCREAMING_SNAKE_CASE__   =					OrderedDict(
    [
        ("whisper", "FlaxWhisperForAudioClassification"),
    ]
)

SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES
)
SCREAMING_SNAKE_CASE__   =					_LazyAutoMapping(
    CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  FLAX_MODEL_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModel)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[str]     					=  FLAX_MODEL_FOR_PRETRAINING_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForPreTraining, head_doc="pretraining")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Tuple     					=  FLAX_MODEL_FOR_CAUSAL_LM_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForCausalLM, head_doc="causal language modeling")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  FLAX_MODEL_FOR_MASKED_LM_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForMaskedLM, head_doc="masked language modeling")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      str     					=  FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForSeqaSeqLM, head_doc="sequence-to-sequence language modeling", checkpoint_for_example="t5-base"
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForSequenceClassification, head_doc="sequence classification"
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[str]     					=  FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="question answering")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Optional[Any]     					=  FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForTokenClassification, head_doc="token classification"
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[str]     					=  FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="multiple choice")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Any     					=  FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForNextSentencePrediction, head_doc="next sentence prediction"
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[Any]     					=  FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForImageClassification, head_doc="image classification"
)



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Dict     					=  FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="vision-to-text modeling")



class 					lowerCAmelCase_      (     _BaseAutoModelClass   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[Any]     					=  FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING





SCREAMING_SNAKE_CASE__   =					auto_class_update(
    FlaxAutoModelForSpeechSeqaSeq, head_doc="sequence-to-sequence speech-to-text modeling"
)



"""simple docstring"""
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


SCREAMING_SNAKE_CASE__   =					"hf-internal-testing/tiny-random-bert"
SCREAMING_SNAKE_CASE__   =					os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
SCREAMING_SNAKE_CASE__   =					"9b8c223d42b2188cb49d29af482996f9d0f3e5a6"



class 					lowerCAmelCase_      (     unittest.TestCase   ):



				"""simple docstring"""

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     cached_file(lowerCAmelCase						,      lowerCAmelCase      )
										# Should have downloaded the file in here
										self.assertTrue(os.path.isdir(lowerCAmelCase      )      )
										# Cache should contain at least those three subfolders:
										for subfolder in ["blobs", "refs", "snapshots"]:
																self.assertTrue(os.path.isdir(os.path.join(lowerCAmelCase						,      lowerCAmelCase      )      )      )
										with open(os.path.join(lowerCAmelCase						,      'refs'						,      'main'      )      ) as f:
																snake_case        =     f.read()
										self.assertEqual(lowerCAmelCase						,      os.path.join(lowerCAmelCase						,      'snapshots'						,      lowerCAmelCase						,      lowerCAmelCase      )      )
										self.assertTrue(os.path.isfile(lowerCAmelCase      )      )

										# File is cached at the same place the second time.
										snake_case        =     cached_file(lowerCAmelCase						,      lowerCAmelCase      )
										self.assertEqual(lowerCAmelCase						,      lowerCAmelCase      )

										# Using a specific revision to test the full commit hash.
										snake_case        =     cached_file(lowerCAmelCase						,      lowerCAmelCase						,      revision='9b8c223'      )
										self.assertEqual(lowerCAmelCase						,      os.path.join(lowerCAmelCase						,      'snapshots'						,      lowerCAmelCase						,      lowerCAmelCase      )      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										with self.assertRaisesRegex(lowerCAmelCase						,      'is not a valid model identifier'      ):
																snake_case        =     cached_file('tiny-random-bert'						,      lowerCAmelCase      )

										with self.assertRaisesRegex(lowerCAmelCase						,      'is not a valid git identifier'      ):
																snake_case        =     cached_file(lowerCAmelCase						,      lowerCAmelCase						,      revision='aaaa'      )

										with self.assertRaisesRegex(lowerCAmelCase						,      'does not appear to have a file named'      ):
																snake_case        =     cached_file(lowerCAmelCase						,      'conf'      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										with self.assertRaisesRegex(lowerCAmelCase						,      'does not appear to have a file named'      ):
																snake_case        =     cached_file(lowerCAmelCase						,      'conf'      )

										with open(os.path.join(lowerCAmelCase						,      'refs'						,      'main'      )      ) as f:
																snake_case        =     f.read()
										self.assertTrue(os.path.isfile(os.path.join(lowerCAmelCase						,      '.no_exist'						,      lowerCAmelCase						,      'conf'      )      )      )

										snake_case        =     cached_file(lowerCAmelCase						,      'conf'						,      _raise_exceptions_for_missing_entries=lowerCAmelCase      )
										self.assertIsNone(lowerCAmelCase      )

										snake_case        =     cached_file(lowerCAmelCase						,      'conf'						,      local_files_only=lowerCAmelCase						,      _raise_exceptions_for_missing_entries=lowerCAmelCase      )
										self.assertIsNone(lowerCAmelCase      )

										snake_case        =     mock.Mock()
										snake_case        =     5_00
										snake_case        =     {}
										snake_case        =     HTTPError
										snake_case        =     {}

										# Under the mock environment we get a 500 error when trying to reach the tokenizer.
										with mock.patch('requests.Session.request'						,      return_value=lowerCAmelCase      ) as mock_head:
																snake_case        =     cached_file(lowerCAmelCase						,      'conf'						,      _raise_exceptions_for_connection_errors=lowerCAmelCase      )
																self.assertIsNone(lowerCAmelCase      )
																# This check we did call the fake head request
																mock_head.assert_called()

				def 						snake_case	(			self      ):






										"""simple docstring"""

										self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'						,      lowerCAmelCase      )      )
										self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,      lowerCAmelCase      )      )
										self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,      lowerCAmelCase      )      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										self.assertIsNone(get_file_from_repo('bert-base-cased'						,      'ahah.txt'      )      )

										# The function raises if the repository does not exist.
										with self.assertRaisesRegex(lowerCAmelCase						,      'is not a valid model identifier'      ):
																get_file_from_repo('bert-base-case'						,      lowerCAmelCase      )

										# The function raises if the revision does not exist.
										with self.assertRaisesRegex(lowerCAmelCase						,      'is not a valid git identifier'      ):
																get_file_from_repo('bert-base-cased'						,      lowerCAmelCase						,      revision='ahaha'      )

										snake_case        =     get_file_from_repo('bert-base-cased'						,      lowerCAmelCase      )
										# The name is the cached name which is not very easy to test, so instead we load the content.
										snake_case        =     json.loads(open(lowerCAmelCase						,      'r'      ).read()      )
										self.assertEqual(config['hidden_size']						,      7_68      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										with tempfile.TemporaryDirectory() as tmp_dir:
																snake_case        =     Path(lowerCAmelCase      ) / 'a.txt'
																filename.touch()
																self.assertEqual(get_file_from_repo(lowerCAmelCase						,      'a.txt'      )						,      str(lowerCAmelCase      )      )

																self.assertIsNone(get_file_from_repo(lowerCAmelCase						,      'b.txt'      )      )


