

'''simple docstring'''



from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
     from transformers import (
         TFAutoModel,
         TFAutoModelForCausalLM,
         TFAutoModelForMaskedLM,
         TFAutoModelForPreTraining,
         TFAutoModelForQuestionAnswering,
         TFAutoModelForSeqaSeqLM,
         TFAutoModelForSequenceClassification,
         TFAutoModelForTableQuestionAnswering,
         TFAutoModelForTokenClassification,
         TFAutoModelWithLMHead,
         TFBertForMaskedLM,
         TFBertForPreTraining,
         TFBertForQuestionAnswering,
         TFBertForSequenceClassification,
         TFBertModel,
         TFFunnelBaseModel,
         TFFunnelModel,
         TFGPTaLMHeadModel,
         TFRobertaForMaskedLM,
         TFTaForConditionalGeneration,
         TFTapasForQuestionAnswering,
     )
     from transformers.models.auto.modeling_tf_auto import (
         TF_MODEL_FOR_CAUSAL_LM_MAPPING,
         TF_MODEL_FOR_MASKED_LM_MAPPING,
         TF_MODEL_FOR_PRETRAINING_MAPPING,
         TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
         TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
         TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
         TF_MODEL_MAPPING,
     )
     from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
     from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
     from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
     from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST



class 				_snake_case					(   __A			):
  SCREAMING_SNAKE_CASE			:       List[Any]				=     'new-model'


if is_tf_available():



     class 				_snake_case					(   __A			):
       SCREAMING_SNAKE_CASE			:       Optional[int]				=     NewModelConfig



@require_tf
class 				_snake_case					(   unittest.TestCase			):

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							"""bert-base-cased"""
      lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
      self.assertIsNotNone(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)

      lowerCAmelCase 						=							TFAutoModel.from_pretrained(snake_case_	)
      self.assertIsNotNone(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							"""bert-base-cased"""
      lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
      self.assertIsNotNone(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)

      lowerCAmelCase 						=							TFAutoModelForPreTraining.from_pretrained(snake_case_	)
      self.assertIsNotNone(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForCausalLM.from_pretrained(snake_case_	)
          lowerCAmelCase 						=							TFAutoModelForCausalLM.from_pretrained(snake_case_      , output_loading_info=snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelWithLMHead.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForMaskedLM.from_pretrained(snake_case_	)
          lowerCAmelCase 						=							TFAutoModelForMaskedLM.from_pretrained(snake_case_      , output_loading_info=snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForSeqaSeqLM.from_pretrained(snake_case_	)
          lowerCAmelCase 						=							TFAutoModelForSeqaSeqLM.from_pretrained(snake_case_      , output_loading_info=snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in ["bert-base-uncased"]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForSequenceClassification.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in ["bert-base-uncased"]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForQuestionAnswering.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  @slow
  @require_tensorflow_probability
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
          lowerCAmelCase 						=							AutoConfig.from_pretrained(snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

          lowerCAmelCase 						=							TFAutoModelForTableQuestionAnswering.from_pretrained(snake_case_	)
          lowerCAmelCase 						=							TFAutoModelForTableQuestionAnswering.from_pretrained(
              snake_case_      , output_loading_info=snake_case_	)
          self.assertIsNotNone(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFAutoModelWithLMHead.from_pretrained(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)
      self.assertEqual(model.num_parameters()      , 1_44_10	)
      self.assertEqual(model.num_parameters(only_trainable=snake_case_	)      , 1_44_10	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFAutoModelWithLMHead.from_pretrained(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)
      self.assertEqual(model.num_parameters()      , 1_44_10	)
      self.assertEqual(model.num_parameters(only_trainable=snake_case_	)      , 1_44_10	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFAutoModel.from_pretrained('sgugger/funnel-random-tiny'	)
      self.assertIsInstance(snake_case_      , snake_case_	)

      lowerCAmelCase 						=							copy.deepcopy(model.config	)
      lowerCAmelCase 						=							["""FunnelBaseModel"""]
      lowerCAmelCase 						=							TFAutoModel.from_config(snake_case_	)
      self.assertIsInstance(snake_case_      , snake_case_	)

      with tempfile.TemporaryDirectory() as tmp_dir:
          model.save_pretrained(snake_case_	)
          lowerCAmelCase 						=							TFAutoModel.from_pretrained(snake_case_	)
          self.assertIsInstance(snake_case_      , snake_case_	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      try:
          AutoConfig.register('new-model'      , snake_case_	)

          lowerCAmelCase 						=							[
              TFAutoModel,
              TFAutoModelForCausalLM,
              TFAutoModelForMaskedLM,
              TFAutoModelForPreTraining,
              TFAutoModelForQuestionAnswering,
              TFAutoModelForSequenceClassification,
              TFAutoModelForTokenClassification,
          ]

          for auto_class in auto_classes:
              with self.subTest(auto_class.__name__	):
                  # Wrong config class will raise an error
                  with self.assertRaises(snake_case_	):
                      auto_class.register(snake_case_      , snake_case_	)
                  auto_class.register(snake_case_      , snake_case_	)
                  # Trying to register something existing in the Transformers library will raise an error
                  with self.assertRaises(snake_case_	):
                      auto_class.register(snake_case_      , snake_case_	)

                  # Now that the config is registered, it can be used as any other config with the auto-API
                  lowerCAmelCase 						=							BertModelTester(self	).get_config()
                  lowerCAmelCase 						=							NewModelConfig(**tiny_config.to_dict()	)
                  lowerCAmelCase 						=							auto_class.from_config(snake_case_	)
                  self.assertIsInstance(snake_case_      , snake_case_	)

                  with tempfile.TemporaryDirectory() as tmp_dir:
                      model.save_pretrained(snake_case_	)
                      lowerCAmelCase 						=							auto_class.from_pretrained(snake_case_	)
                      self.assertIsInstance(snake_case_      , snake_case_	)

      finally:
          if "new-model" in CONFIG_MAPPING._extra_content:
              del CONFIG_MAPPING._extra_content["new-model"]
          for mapping in (
              TF_MODEL_MAPPING,
              TF_MODEL_FOR_PRETRAINING_MAPPING,
              TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
              TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
              TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
              TF_MODEL_FOR_CAUSAL_LM_MAPPING,
              TF_MODEL_FOR_MASKED_LM_MAPPING,
          ):
              if NewModelConfig in mapping._extra_content:
                  del mapping._extra_content[NewModelConfig]

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      with self.assertRaisesRegex(
          snake_case_      , 'bert-base is not a local folder and is not a valid model identifier'	):
          lowerCAmelCase 						=							TFAutoModel.from_pretrained('bert-base'	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      with self.assertRaisesRegex(
          snake_case_      , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'	):
          lowerCAmelCase 						=							TFAutoModel.from_pretrained(snake_case_      , revision='aaaaaa'	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      with self.assertRaisesRegex(
          snake_case_      , 'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'      , ):
          lowerCAmelCase 						=							TFAutoModel.from_pretrained('hf-internal-testing/config-no-model'	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      with self.assertRaisesRegex(snake_case_      , 'Use `from_pt=True` to load this model'	):
          lowerCAmelCase 						=							TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert'	)
      with RequestCounter() as counter:
          lowerCAmelCase 						=							TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert'	)
          self.assertEqual(counter.get_request_count      , 0	)
          self.assertEqual(counter.head_request_count      , 1	)
          self.assertEqual(counter.other_request_count      , 0	)

      # With a sharded checkpoint
      lowerCAmelCase 						=							TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded'	)
      with RequestCounter() as counter:
          lowerCAmelCase 						=							TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded'	)
          self.assertEqual(counter.get_request_count      , 0	)
          self.assertEqual(counter.head_request_count      , 1	)
          self.assertEqual(counter.other_request_count      , 0	)






"""simple docstring"""



import functools
import operator

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


_UpperCamelCase								=		logging.get_logger(__name__)

_UpperCamelCase								=		{
    "asapp/sew-tiny-100k": "https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json",
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class 						__UpperCAmelCase    (__A     ):







			'''simple docstring'''
			_UpperCamelCase  :						Dict					     =				'sew'





			def __init__(			self   ,      snake_case_=32   ,      snake_case_=768   ,      snake_case_=12   ,      snake_case_=12   ,      snake_case_=3_072   ,      snake_case_=2   ,      snake_case_="gelu"   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.0   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=0.02   ,      snake_case_=1E-5   ,      snake_case_="group"   ,      snake_case_="gelu"   ,      snake_case_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)   ,      snake_case_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)   ,      snake_case_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)   ,      snake_case_=False   ,      snake_case_=128   ,      snake_case_=16   ,      snake_case_=True   ,      snake_case_=0.05   ,      snake_case_=10   ,      snake_case_=2   ,      snake_case_=0.0   ,      snake_case_=10   ,      snake_case_=0   ,      snake_case_="mean"   ,      snake_case_=False   ,      snake_case_=False   ,      snake_case_=256   ,      snake_case_=0   ,      snake_case_=1   ,      snake_case_=2   ,      **snake_case_   ,      ):







										'''simple docstring'''


										super().__init__(**snake_case_   ,      pad_token_id=snake_case_   ,      bos_token_id=snake_case_   ,      eos_token_id=snake_case_							)
										A__       :     Dict								=			hidden_size
										A__       :     Dict								=			feat_extract_norm
										A__       :     str								=			feat_extract_activation
										A__       :     Optional[Any]								=			list(snake_case_							)
										A__       :     str								=			list(snake_case_							)
										A__       :     Any								=			list(snake_case_							)
										A__       :     Any								=			conv_bias
										A__       :     Any								=			num_conv_pos_embeddings
										A__       :     Any								=			num_conv_pos_embedding_groups
										A__       :     str								=			len(self.conv_dim							)
										A__       :     Tuple								=			num_hidden_layers
										A__       :     int								=			intermediate_size
										A__       :     Union[str, Any]								=			squeeze_factor
										A__       :     Union[str, Any]								=			hidden_act
										A__       :     List[str]								=			num_attention_heads
										A__       :     List[str]								=			hidden_dropout
										A__       :     Dict								=			attention_dropout
										A__       :     Tuple								=			activation_dropout
										A__       :     Optional[int]								=			feat_proj_dropout
										A__       :     Optional[Any]								=			final_dropout
										A__       :     int								=			layerdrop
										A__       :     List[Any]								=			layer_norm_eps
										A__       :     int								=			initializer_range
										A__       :     Dict								=			vocab_size

										if (
										    (len(self.conv_stride							) != self.num_feat_extract_layers)
										    or (len(self.conv_kernel							) != self.num_feat_extract_layers)
										    or (len(self.conv_dim							) != self.num_feat_extract_layers)
										):
																	raise ValueError(
																	    """Configuration for convolutional layers is incorrect."""
																	    """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
																	    F'''but is `len(config.conv_dim) = {len(self.conv_dim							)}`, `len(config.conv_stride)'''
																	    F'''= {len(self.conv_stride							)}`, `len(config.conv_kernel) = {len(self.conv_kernel							)}`.'''							)

										# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
										A__       :     Optional[Any]								=			apply_spec_augment
										A__       :     int								=			mask_time_prob
										A__       :     Tuple								=			mask_time_length
										A__       :     Optional[Any]								=			mask_time_min_masks
										A__       :     Any								=			mask_feature_prob
										A__       :     List[Any]								=			mask_feature_length
										A__       :     Any								=			mask_feature_min_masks

										# ctc loss
										A__       :     str								=			ctc_loss_reduction
										A__       :     List[Any]								=			ctc_zero_infinity

										# sequence classification
										A__       :     Union[str, Any]								=			use_weighted_layer_sum
										A__       :     str								=			classifier_proj_size





			@property
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										return functools.reduce(operator.mul   ,      self.conv_stride   ,      1							)





