


'''simple docstring'''
import importlib
import inspect
import json
import os
from collections import OrderedDict

# Build the list of all feature extractors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...feature_extraction_utils import FeatureExtractionMixin
from ...image_processing_utils import ImageProcessingMixin
from ...tokenization_utils import TOKENIZER_CONFIG_FILE
from ...utils import FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)
from .feature_extraction_auto import AutoFeatureExtractor
from .image_processing_auto import AutoImageProcessor
from .tokenization_auto import AutoTokenizer


lowerCAmelCase  =				logging.get_logger(__name__)

lowerCAmelCase  =				OrderedDict(
    [
        ("""align""", """AlignProcessor"""),
        ("""altclip""", """AltCLIPProcessor"""),
        ("""bark""", """BarkProcessor"""),
        ("""blip""", """BlipProcessor"""),
        ("""blip-2""", """Blip2Processor"""),
        ("""bridgetower""", """BridgeTowerProcessor"""),
        ("""chinese_clip""", """ChineseCLIPProcessor"""),
        ("""clap""", """ClapProcessor"""),
        ("""clip""", """CLIPProcessor"""),
        ("""clipseg""", """CLIPSegProcessor"""),
        ("""flava""", """FlavaProcessor"""),
        ("""git""", """GitProcessor"""),
        ("""groupvit""", """CLIPProcessor"""),
        ("""hubert""", """Wav2Vec2Processor"""),
        ("""instructblip""", """InstructBlipProcessor"""),
        ("""layoutlmv2""", """LayoutLMv2Processor"""),
        ("""layoutlmv3""", """LayoutLMv3Processor"""),
        ("""markuplm""", """MarkupLMProcessor"""),
        ("""mctct""", """MCTCTProcessor"""),
        ("""mgp-str""", """MgpstrProcessor"""),
        ("""oneformer""", """OneFormerProcessor"""),
        ("""owlvit""", """OwlViTProcessor"""),
        ("""pix2struct""", """Pix2StructProcessor"""),
        ("""sam""", """SamProcessor"""),
        ("""sew""", """Wav2Vec2Processor"""),
        ("""sew-d""", """Wav2Vec2Processor"""),
        ("""speech_to_text""", """Speech2TextProcessor"""),
        ("""speech_to_text_2""", """Speech2Text2Processor"""),
        ("""speecht5""", """SpeechT5Processor"""),
        ("""trocr""", """TrOCRProcessor"""),
        ("""tvlt""", """TvltProcessor"""),
        ("""unispeech""", """Wav2Vec2Processor"""),
        ("""unispeech-sat""", """Wav2Vec2Processor"""),
        ("""vilt""", """ViltProcessor"""),
        ("""vision-text-dual-encoder""", """VisionTextDualEncoderProcessor"""),
        ("""wav2vec2""", """Wav2Vec2Processor"""),
        ("""wav2vec2-conformer""", """Wav2Vec2Processor"""),
        ("""wavlm""", """Wav2Vec2Processor"""),
        ("""whisper""", """WhisperProcessor"""),
        ("""xclip""", """XCLIPProcessor"""),
    ]
)

lowerCAmelCase  =				_LazyAutoMapping(CONFIG_MAPPING_NAMES, PROCESSOR_MAPPING_NAMES)

def 				lowerCamelCase_   (   __UpperCamelCase				:					str		)				->  str:







     """simple docstring"""
     for module_name, processors in PROCESSOR_MAPPING_NAMES.items():
          if class_name in processors:
               _A				=       model_type_to_module_name(__UpperCamelCase		)

               _A				=       importlib.import_module(F'.{module_name}'       ,						'transformers.models'		)
               try:
                    return getattr(__UpperCamelCase       ,						__UpperCamelCase		)
               except AttributeError:
                    continue

     for processor in PROCESSOR_MAPPING._extra_content.values():
          if getattr(__UpperCamelCase       ,						'__name__'       ,						__UpperCamelCase		) == class_name:
               return processor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
     _A				=       importlib.import_module('transformers'		)
     if hasattr(__UpperCamelCase       ,						__UpperCamelCase		):
          return getattr(__UpperCamelCase       ,						__UpperCamelCase		)

     return None






class 					lowerCAmelCase_     :






       def __init__(   self      )->			Tuple:
            raise EnvironmentError(
                'AutoProcessor is designed to be instantiated '
                'using the `AutoProcessor.from_pretrained(pretrained_model_name_or_path)` method.'      )






       @classmethod
       @replace_list_option_in_docstrings(_UpperCamelCase      )
       def      UpperCamelCase	(   cls ,    _UpperCamelCase ,    **_UpperCamelCase      )->			List[str]:
            _A				=       kwargs.pop('config' ,    _UpperCamelCase      )
            _A				=       kwargs.pop('trust_remote_code' ,    _UpperCamelCase      )
            _A				=       True

            _A				=       None
            _A				=       None

            # First, let's see if we have a preprocessor config.
            # Filter the kwargs for `get_file_from_repo`.
            _A				=       {
                key: kwargs[key] for key in inspect.signature(_UpperCamelCase      ).parameters.keys() if key in kwargs
            }
            # Let's start by checking whether the processor class is saved in an image processor
            _A				=       get_file_from_repo(
                _UpperCamelCase ,    _UpperCamelCase ,    **_UpperCamelCase      )
            if preprocessor_config_file is not None:
                 _A		,							_A				=       ImageProcessingMixin.get_image_processor_dict(_UpperCamelCase ,    **_UpperCamelCase      )
                 _A				=       config_dict.get('processor_class' ,    _UpperCamelCase      )
                 if "AutoProcessor" in config_dict.get('auto_map' ,    {}      ):
                      _A				=       config_dict['auto_map']['AutoProcessor']

        # If not found, let's check whether the processor class is saved in a feature extractor config
            if preprocessor_config_file is not None and processor_class is None:
                 _A		,							_A				=       FeatureExtractionMixin.get_feature_extractor_dict(_UpperCamelCase ,    **_UpperCamelCase      )
                 _A				=       config_dict.get('processor_class' ,    _UpperCamelCase      )
                 if "AutoProcessor" in config_dict.get('auto_map' ,    {}      ):
                      _A				=       config_dict['auto_map']['AutoProcessor']

            if processor_class is None:
                 # Next, let's check whether the processor class is saved in a tokenizer
                 _A				=       get_file_from_repo(
                     _UpperCamelCase ,    _UpperCamelCase ,    **_UpperCamelCase      )
                 if tokenizer_config_file is not None:
                      with open(_UpperCamelCase ,    encoding='utf-8'      ) as reader:
                           _A				=       json.load(_UpperCamelCase      )

                      _A				=       config_dict.get('processor_class' ,    _UpperCamelCase      )
                      if "AutoProcessor" in config_dict.get('auto_map' ,    {}      ):
                           _A				=       config_dict['auto_map']['AutoProcessor']

            if processor_class is None:
                 # Otherwise, load config, if it can be loaded.
                 if not isinstance(_UpperCamelCase ,    _UpperCamelCase      ):
                      _A				=       AutoConfig.from_pretrained(
                          _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )

                 # And check if the config contains the processor class.
                 _A				=       getattr(_UpperCamelCase ,    'processor_class' ,    _UpperCamelCase      )
                 if hasattr(_UpperCamelCase ,    'auto_map'      ) and "AutoProcessor" in config.auto_map:
                      _A				=       config.auto_map['AutoProcessor']

            if processor_class is not None:
                 _A				=       processor_class_from_name(_UpperCamelCase      )

            _A				=       processor_auto_map is not None
            _A				=       processor_class is not None or type(_UpperCamelCase      ) in PROCESSOR_MAPPING
            _A				=       resolve_trust_remote_code(
                _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )

            if has_remote_code and trust_remote_code:
                 _A				=       get_class_from_dynamic_module(
                     _UpperCamelCase ,    _UpperCamelCase ,    **_UpperCamelCase      )
                 _A				=       kwargs.pop('code_revision' ,    _UpperCamelCase      )
                 if os.path.isdir(_UpperCamelCase      ):
                      processor_class.register_for_auto_class()
                 return processor_class.from_pretrained(
                     _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )
            elif processor_class is not None:
                 return processor_class.from_pretrained(
                     _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )
            # Last try: we use the PROCESSOR_MAPPING.
            elif type(_UpperCamelCase      ) in PROCESSOR_MAPPING:
                 return PROCESSOR_MAPPING[type(_UpperCamelCase      )].from_pretrained(_UpperCamelCase ,    **_UpperCamelCase      )

            # At this stage, there doesn't seem to be a `Processor` class available for this model, so let's try a
            # tokenizer.
            try:
                 return AutoTokenizer.from_pretrained(
                     _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )
            except Exception:
                 try:
                      return AutoImageProcessor.from_pretrained(
                          _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )
                 except Exception:
                      pass

                 try:
                      return AutoFeatureExtractor.from_pretrained(
                          _UpperCamelCase ,    trust_remote_code=_UpperCamelCase ,    **_UpperCamelCase      )
                 except Exception:
                      pass

            raise ValueError(
                F'Unrecognized processing class in {pretrained_model_name_or_path}. Can\'t instantiate a processor, a '
                'tokenizer, an image processor or a feature extractor for this model. Make sure the repository contains'
                'the files of at least one of those processing classes.'      )






       @staticmethod
       def      UpperCamelCase	(   _UpperCamelCase ,    _UpperCamelCase      )->			Union[str, Any]:
            PROCESSOR_MAPPING.register(_UpperCamelCase ,    _UpperCamelCase      )









'''simple docstring'''
from __future__ import annotations

import unittest

from transformers import AlbertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
    import tensorflow as tf

    from transformers import TF_MODEL_FOR_PRETRAINING_MAPPING
    from transformers.models.albert.modeling_tf_albert import (
        TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFAlbertForMaskedLM,
        TFAlbertForMultipleChoice,
        TFAlbertForPreTraining,
        TFAlbertForQuestionAnswering,
        TFAlbertForSequenceClassification,
        TFAlbertForTokenClassification,
        TFAlbertModel,
    )






class 					lowerCAmelCase_     :






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase=13 ,    _UpperCamelCase=7 ,    _UpperCamelCase=True ,    _UpperCamelCase=True ,    _UpperCamelCase=True ,    _UpperCamelCase=True ,    _UpperCamelCase=99 ,    _UpperCamelCase=16 ,    _UpperCamelCase=32 ,    _UpperCamelCase=2 ,    _UpperCamelCase=4 ,    _UpperCamelCase=37 ,    _UpperCamelCase="gelu" ,    _UpperCamelCase=0.1 ,    _UpperCamelCase=0.1 ,    _UpperCamelCase=512 ,    _UpperCamelCase=16 ,    _UpperCamelCase=2 ,    _UpperCamelCase=0.02 ,    _UpperCamelCase=3 ,    _UpperCamelCase=4 ,    _UpperCamelCase=None ,    )->			List[str]:
            _A				=       parent
            _A				=       13
            _A				=       7
            _A				=       True
            _A				=       True
            _A				=       True
            _A				=       True
            _A				=       99
            _A				=       16
            _A				=       32
            _A				=       2
            _A				=       4
            _A				=       37
            _A				=       'gelu'
            _A				=       0.1
            _A				=       0.1
            _A				=       512
            _A				=       16
            _A				=       2
            _A				=       0.02
            _A				=       3
            _A				=       4
            _A				=       None






       def      UpperCamelCase	(   self      )->			str:
            _A				=       ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size      )

            _A				=       None
            if self.use_input_mask:
                 _A				=       random_attention_mask([self.batch_size, self.seq_length]      )

            _A				=       None
            if self.use_token_type_ids:
                 _A				=       ids_tensor([self.batch_size, self.seq_length] ,    self.type_vocab_size      )

            _A				=       None
            _A				=       None
            _A				=       None
            if self.use_labels:
                 _A				=       ids_tensor([self.batch_size] ,    self.type_sequence_label_size      )
                 _A				=       ids_tensor([self.batch_size, self.seq_length] ,    self.num_labels      )
                 _A				=       ids_tensor([self.batch_size] ,    self.num_choices      )

            _A				=       AlbertConfig(
                vocab_size=self.vocab_size ,    hidden_size=self.hidden_size ,    embedding_size=self.embedding_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 ,    max_position_embeddings=self.max_position_embeddings ,    type_vocab_size=self.type_vocab_size ,    initializer_range=self.initializer_range ,    )

            return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			int:
            _A				=       TFAlbertModel(config=_UpperCamelCase      )
            # inputs = {'input_ids': input_ids,
            #           'attention_mask': input_mask,
            #           'token_type_ids': token_type_ids}
            # sequence_output, pooled_output = model(**inputs)
            _A				=       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A				=       model(_UpperCamelCase      )

            _A				=       [input_ids, input_mask]
            _A				=       model(_UpperCamelCase      )

            _A				=       model(_UpperCamelCase      )

            self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)      )
            self.parent.assertEqual(result.pooler_output.shape ,    (self.batch_size, self.hidden_size)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Tuple:
            _A				=       self.num_labels
            _A				=       TFAlbertForPreTraining(config=_UpperCamelCase      )
            _A				=       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A				=       model(_UpperCamelCase      )
            self.parent.assertEqual(result.prediction_logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)      )
            self.parent.assertEqual(result.sop_logits.shape ,    (self.batch_size, self.num_labels)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			List[Any]:
            _A				=       TFAlbertForMaskedLM(config=_UpperCamelCase      )
            _A				=       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A				=       model(_UpperCamelCase      )
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			int:
            _A				=       self.num_labels
            _A				=       TFAlbertForSequenceClassification(config=_UpperCamelCase      )
            _A				=       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A				=       model(_UpperCamelCase      )
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Optional[Any]:
            _A				=       TFAlbertForQuestionAnswering(config=_UpperCamelCase      )
            _A				=       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
            _A				=       model(_UpperCamelCase      )
            self.parent.assertEqual(result.start_logits.shape ,    (self.batch_size, self.seq_length)      )
            self.parent.assertEqual(result.end_logits.shape ,    (self.batch_size, self.seq_length)      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Tuple:
            _A				=       self.num_choices
            _A				=       TFAlbertForMultipleChoice(config=_UpperCamelCase      )
            _A				=       tf.tile(tf.expand_dims(_UpperCamelCase ,    1      ) ,    (1, self.num_choices, 1)      )
            _A				=       tf.tile(tf.expand_dims(_UpperCamelCase ,    1      ) ,    (1, self.num_choices, 1)      )
            _A				=       tf.tile(tf.expand_dims(_UpperCamelCase ,    1      ) ,    (1, self.num_choices, 1)      )
            _A				=       {
                'input_ids': multiple_choice_inputs_ids,
                'attention_mask': multiple_choice_input_mask,
                'token_type_ids': multiple_choice_token_type_ids,
            }
            _A				=       model(_UpperCamelCase      )
            self.parent.assertListEqual(list(result['logits'].shape      ) ,    [self.batch_size, self.num_choices]      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase      )->			Any:
            _A				=       self.num_labels
            _A				=       TFAlbertForTokenClassification(config=_UpperCamelCase      )
            _A				=       {
                'input_ids': input_ids,
                'attention_mask': input_mask,
                'token_type_ids': token_type_ids,
            }
            _A				=       model(_UpperCamelCase      )
            self.parent.assertListEqual(list(result['logits'].shape      ) ,    [self.batch_size, self.seq_length, self.num_labels]      )






       def      UpperCamelCase	(   self      )->			Any:
            _A				=       self.prepare_config_and_inputs()
            (
                (
                _A
            )		,							(
                _A
            )		,							(
                _A
            )		,							(
                _A
            )		,							(
                _A
            )		,							(
                _A
            )		,							(
                _A
            )		,							
            )				=       config_and_inputs
            _A				=       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
            return config, inputs_dict






@require_tf
class 					lowerCAmelCase_     (    UpperCAmelCase			,       UpperCAmelCase			,       unittest.TestCase  ):
       __UpperCAmelCase				=(
           (
               TFAlbertModel,
               TFAlbertForPreTraining,
               TFAlbertForMaskedLM,
               TFAlbertForSequenceClassification,
               TFAlbertForQuestionAnswering,
               TFAlbertForTokenClassification,
               TFAlbertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       __UpperCAmelCase				=(
           {
               'feature-extraction': TFAlbertModel,
               'fill-mask': TFAlbertForMaskedLM,
               'question-answering': TFAlbertForQuestionAnswering,
               'text-classification': TFAlbertForSequenceClassification,
               'token-classification': TFAlbertForTokenClassification,
               'zero-shot': TFAlbertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __UpperCAmelCase				=False
       __UpperCAmelCase				=False






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase=False      )->			Tuple:
            _A				=       super()._prepare_for_class(_UpperCamelCase ,    _UpperCamelCase ,    return_labels=_UpperCamelCase      )

            if return_labels:
                 if model_class in get_values(_UpperCamelCase      ):
                      _A				=       tf.zeros(self.model_tester.batch_size ,    dtype=tf.intaa      )

            return inputs_dict






       def      UpperCamelCase	(   self      )->			Tuple:
            _A				=       TFAlbertModelTester(self      )
            _A				=       ConfigTester(self ,    config_class=_UpperCamelCase ,    hidden_size=37      )






       def      UpperCamelCase	(   self      )->			Optional[Any]:
            self.config_tester.run_common_tests()






       def      UpperCamelCase	(   self      )->			Optional[Any]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_model(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			int:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_for_pretraining(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			List[Any]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_for_masked_lm(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			str:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_for_multiple_choice(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			Any:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_for_sequence_classification(*_UpperCamelCase      )






       def      UpperCamelCase	(   self      )->			Optional[int]:
            _A				=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_albert_for_question_answering(*_UpperCamelCase      )






       @slow
       def      UpperCamelCase	(   self      )->			List[Any]:
            for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A				=       TFAlbertModel.from_pretrained(_UpperCamelCase      )
                 self.assertIsNotNone(_UpperCamelCase      )






@require_tf
class 					lowerCAmelCase_     (    unittest.TestCase  ):






       @slow
       def      UpperCamelCase	(   self      )->			List[str]:
            _A				=       TFAlbertForPreTraining.from_pretrained('albert-base-v2'      )
            _A				=       tf.constant([[0, 1, 2, 3, 4, 5]]      )
            _A				=       model(_UpperCamelCase      )[0]

            _A				=       [1, 6, 3_0000]
            self.assertEqual(output.shape ,    _UpperCamelCase      )

            _A				=       tf.constant(
                [
                    [
                        [4.59_5668, 0.7446_2754, -1.81_8147],
                        [4.595_4347, 0.745_4184, -1.818_8258],
                        [4.595_4905, 0.744_8235, -1.818_2316],
                    ]
                ]      )
            tf.debugging.assert_near(output[:, :3, :3] ,    _UpperCamelCase ,    atol=1e-4      )





