






"""simple docstring"""


import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       MobileBertForMaskedLM,
       MobileBertForMultipleChoice,
       MobileBertForNextSentencePrediction,
       MobileBertForPreTraining,
       MobileBertForQuestionAnswering,
       MobileBertForSequenceClassification,
       MobileBertForTokenClassification,
       MobileBertModel,
   )






class 						snake_case  :





    def __init__(							self     : int    ,		A     : Dict    ,		A     : str=1_3    ,		A     : Dict=7    ,		A     : Union[str, Any]=True    ,		A     : List[str]=True    ,		A     : Tuple=True    ,		A     : Dict=True    ,		A     : Optional[Any]=9_9    ,		A     : Optional[Any]=6_4    ,		A     : List[str]=3_2    ,		A     : Optional[Any]=5    ,		A     : Union[str, Any]=4    ,		A     : Optional[Any]=3_7    ,		A     : Optional[int]="gelu"    ,		A     : str=0.1    ,		A     : Tuple=0.1    ,		A     : Any=5_1_2    ,		A     : int=1_6    ,		A     : Any=2    ,		A     : List[str]=0.02    ,		A     : List[Any]=3    ,		A     : Optional[int]=4    ,		A     : Tuple=None    ,		):


      '''simple docstring'''




      a					:      Optional[Any]    =       parent
      a					:      Optional[int]    =       batch_size
      a					:      Dict    =       seq_length
      a					:      List[str]    =       is_training
      a					:      Union[str, Any]    =       use_input_mask
      a					:      Any    =       use_token_type_ids
      a					:      int    =       use_labels
      a					:      Optional[int]    =       vocab_size
      a					:      Optional[Any]    =       hidden_size
      a					:      Optional[int]    =       embedding_size
      a					:      Dict    =       num_hidden_layers
      a					:      Optional[int]    =       num_attention_heads
      a					:      Union[str, Any]    =       intermediate_size
      a					:      List[str]    =       hidden_act
      a					:      int    =       hidden_dropout_prob
      a					:      Tuple    =       attention_probs_dropout_prob
      a					:      List[str]    =       max_position_embeddings
      a					:      Union[str, Any]    =       type_vocab_size
      a					:      Tuple    =       type_sequence_label_size
      a					:      Any    =       initializer_range
      a					:      Optional[int]    =       num_labels
      a					:      List[str]    =       num_choices
      a					:      List[str]    =       scope





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      Tuple    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size      )

      a					:      List[Any]    =       None
      if self.use_input_mask:
        a					:      Tuple    =       random_attention_mask([self.batch_size, self.seq_length]      )

      a					:      str    =       None
      if self.use_token_type_ids:
        a					:      Optional[int]    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size      )

      a					:      str    =       None
      a					:      List[Any]    =       None
      a					:      Tuple    =       None
      if self.use_labels:
        a					:      Union[str, Any]    =       ids_tensor([self.batch_size]    ,		self.type_sequence_label_size      )
        a					:      Dict    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels      )
        a					:      List[str]    =       ids_tensor([self.batch_size]    ,		self.num_choices      )

      a					:      int    =       self.get_config()

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





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      return MobileBertConfig(
          vocab_size=self.vocab_size    ,		hidden_size=self.hidden_size    ,		num_hidden_layers=self.num_hidden_layers    ,		num_attention_heads=self.num_attention_heads    ,		intermediate_size=self.intermediate_size    ,		embedding_size=self.embedding_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    ,		is_decoder=A    ,		initializer_range=self.initializer_range    ,		)





    def     lowerCamelCase__				(							self     : str    ,		A     : Tuple    ,		A     : Optional[Any]    ,		A     : List[Any]    ,		A     : Any    ,		A     : str    ,		A     : int    ,		A     : List[str]      ):


      '''simple docstring'''




      a					:      List[str]    =       MobileBertModel(config=A      )
      model.to(A      )
      model.eval()
      a					:      Optional[int]    =       model(A    ,		attention_mask=A    ,		token_type_ids=A      )
      a					:      Dict    =       model(A    ,		token_type_ids=A      )
      a					:      List[str]    =       model(A      )

      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     lowerCamelCase__				(							self     : Optional[Any]    ,		A     : Optional[Any]    ,		A     : Tuple    ,		A     : str    ,		A     : Dict    ,		A     : Tuple    ,		A     : List[Any]    ,		A     : Any      ):


      '''simple docstring'''




      a					:      Optional[Any]    =       MobileBertForMaskedLM(config=A      )
      model.to(A      )
      model.eval()
      a					:      Optional[Any]    =       model(A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)      )





    def     lowerCamelCase__				(							self     : Tuple    ,		A     : Optional[Any]    ,		A     : Any    ,		A     : str    ,		A     : Tuple    ,		A     : Tuple    ,		A     : Dict    ,		A     : int      ):


      '''simple docstring'''




      a					:      Optional[int]    =       MobileBertForNextSentencePrediction(config=A      )
      model.to(A      )
      model.eval()
      a					:      Union[str, Any]    =       model(
          A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A    ,		)
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, 2)      )





    def     lowerCamelCase__				(							self     : str    ,		A     : Tuple    ,		A     : Optional[Any]    ,		A     : str    ,		A     : Union[str, Any]    ,		A     : int    ,		A     : Union[str, Any]    ,		A     : Optional[int]      ):


      '''simple docstring'''




      a					:      List[Any]    =       MobileBertForPreTraining(config=A      )
      model.to(A      )
      model.eval()
      a					:      List[Any]    =       model(
          A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A    ,		next_sentence_label=A    ,		)
      self.parent.assertEqual(result.prediction_logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)      )
      self.parent.assertEqual(result.seq_relationship_logits.shape    ,		(self.batch_size, 2)      )





    def     lowerCamelCase__				(							self     : Optional[Any]    ,		A     : Dict    ,		A     : List[Any]    ,		A     : List[str]    ,		A     : Any    ,		A     : List[Any]    ,		A     : str    ,		A     : List[str]      ):


      '''simple docstring'''




      a					:      Dict    =       MobileBertForQuestionAnswering(config=A      )
      model.to(A      )
      model.eval()
      a					:      Union[str, Any]    =       model(
          A    ,		attention_mask=A    ,		token_type_ids=A    ,		start_positions=A    ,		end_positions=A    ,		)
      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     lowerCamelCase__				(							self     : Optional[int]    ,		A     : int    ,		A     : List[str]    ,		A     : Dict    ,		A     : List[str]    ,		A     : Any    ,		A     : List[Any]    ,		A     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Tuple    =       self.num_labels
      a					:      Optional[int]    =       MobileBertForSequenceClassification(A      )
      model.to(A      )
      model.eval()
      a					:      List[str]    =       model(A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)      )





    def     lowerCamelCase__				(							self     : str    ,		A     : List[Any]    ,		A     : str    ,		A     : str    ,		A     : int    ,		A     : Tuple    ,		A     : Any    ,		A     : str      ):


      '''simple docstring'''




      a					:      Optional[Any]    =       self.num_labels
      a					:      List[Any]    =       MobileBertForTokenClassification(config=A      )
      model.to(A      )
      model.eval()
      a					:      List[str]    =       model(A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)      )





    def     lowerCamelCase__				(							self     : Union[str, Any]    ,		A     : Optional[Any]    ,		A     : Optional[Any]    ,		A     : Optional[Any]    ,		A     : Optional[int]    ,		A     : str    ,		A     : Tuple    ,		A     : Optional[int]      ):


      '''simple docstring'''




      a					:      List[str]    =       self.num_choices
      a					:      Optional[int]    =       MobileBertForMultipleChoice(config=A      )
      model.to(A      )
      model.eval()
      a					:      List[Any]    =       input_ids.unsqueeze(1      ).expand(-1    ,		self.num_choices    ,		-1      ).contiguous()
      a					:      List[str]    =       token_type_ids.unsqueeze(1      ).expand(-1    ,		self.num_choices    ,		-1      ).contiguous()
      a					:      int    =       input_mask.unsqueeze(1      ).expand(-1    ,		self.num_choices    ,		-1      ).contiguous()
      a					:      Any    =       model(
          A    ,		attention_mask=A    ,		token_type_ids=A    ,		labels=A    ,		)
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_choices)      )





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.prepare_config_and_inputs()
      (
          (
          a
      ),					(
          a
      ),					(
          a
      ),					(
          a
      ),					(
          a
      ),					(
          a
      ),					(
          a
      ),					
      )					:      Union[str, Any]    =       config_and_inputs
      a					:      int    =       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class 						snake_case  (  UpperCAmelCase       ,			UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					(
        (
            MobileBertModel,
            MobileBertForMaskedLM,
            MobileBertForMultipleChoice,
            MobileBertForNextSentencePrediction,
            MobileBertForPreTraining,
            MobileBertForQuestionAnswering,
            MobileBertForSequenceClassification,
            MobileBertForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    __magic_name__   					=					(
        {
            '''feature-extraction''': MobileBertModel,
            '''fill-mask''': MobileBertForMaskedLM,
            '''question-answering''': MobileBertForQuestionAnswering,
            '''text-classification''': MobileBertForSequenceClassification,
            '''token-classification''': MobileBertForTokenClassification,
            '''zero-shot''': MobileBertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __magic_name__   					=					True





    def     lowerCamelCase__				(							self     : Tuple    ,		A     : List[str]    ,		A     : Tuple    ,		A     : Any=False      ):


      '''simple docstring'''




      a					:      List[str]    =       super()._prepare_for_class(A    ,		A    ,		return_labels=A      )

      if return_labels:
        if model_class in get_values(A      ):
          a					:      List[Any]    =       torch.zeros(
              (self.model_tester.batch_size, self.model_tester.seq_length)    ,		dtype=torch.long    ,		device=A      )
          a					:      Optional[Any]    =       torch.zeros(
              self.model_tester.batch_size    ,		dtype=torch.long    ,		device=A      )
      return inputs_dict





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Dict    =       MobileBertModelTester(self      )
      a					:      List[Any]    =       ConfigTester(self    ,		config_class=A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      a					:      str    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*A      )





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      Tuple    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*A      )





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A      )





    def     lowerCamelCase__				(							self     : List[str]      ):


      '''simple docstring'''




      a					:      Any    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A      )





    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      a					:      Tuple    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*A      )





    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      a					:      Optional[Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*A      )





    def     lowerCamelCase__				(							self     : str      ):


      '''simple docstring'''




      a					:      List[Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      List[str]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_token_classification(*A      )




def       snake_case  (A_				:Dict				):







  '''simple docstring'''

  return torch.tensor(
      A_     ,      dtype=torch.long     ,      device=A_     ,      )


_UpperCamelCase    :   Tuple   			=						1e-3






@require_torch
@require_sentencepiece
@require_tokenizers
class 						snake_case  (  unittest.TestCase	):





    @slow
    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      List[str]    =       MobileBertModel.from_pretrained('google/mobilebert-uncased'      ).to(A      )
      a					:      List[str]    =       _long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]]      )
      with torch.no_grad():
        a					:      Optional[int]    =       model(A      )[0]
      a					:      Tuple    =       torch.Size((1, 9, 5_1_2)      )
      self.assertEqual(output.shape    ,		A      )
      a					:      Any    =       torch.tensor(
          [
              [
                  [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
                  [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
                  [2.604_7359E00, 1.567_7652E00, -1.732_4188E-01],
              ]
          ]    ,		device=A    ,		)

      # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
      # ~1 difference, it's therefore not a good idea to measure using addition.
      # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
      # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
      a					:      List[str]    =       torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE      )
      a					:      List[Any]    =       torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE      )

      self.assertTrue(lower_bound and upper_bound      )












"""simple docstring"""


import argparse
import os
import re

import packaging.version


_UpperCamelCase    :   Optional[Any]   			=						'examples/'
_UpperCamelCase    :   Any   			=						{
    'examples': (re.compile(r'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'),
    'init': (re.compile(r'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'),
    'setup': (re.compile(r'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), r'\1version="VERSION",'),
    'doc': (re.compile(r'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'),
}
_UpperCamelCase    :   List[str]   			=						{
    'init': 'src/diffusers/__init__.py',
    'setup': 'setup.py',
}
_UpperCamelCase    :   List[str]   			=						'README.md'




def       snake_case  (A_				:str     ,      A_				:Optional[Any]     ,      A_				:Any				):







  '''simple docstring'''

  with open(A_     ,      'r'     ,      encoding='utf-8'     ,      newline='\n'				) as f:
    a					:      Tuple    =       f.read()
  a,					a					:      Any    =       REPLACE_PATTERNS[pattern]
  a					:      Dict    =       replace.replace('VERSION'     ,      A_				)
  a					:      Union[str, Any]    =       re_pattern.sub(A_     ,      A_				)
  with open(A_     ,      'w'     ,      encoding='utf-8'     ,      newline='\n'				) as f:
    f.write(A_				)




def       snake_case  (A_				:List[Any]				):







  '''simple docstring'''

  for folder, directories, fnames in os.walk(A_				):
    # Removing some of the folders with non-actively maintained examples from the walk
    if "research_projects" in directories:
      directories.remove('research_projects'				)
    if "legacy" in directories:
      directories.remove('legacy'				)
    for fname in fnames:
      if fname.endswith('.py'				):
        update_version_in_file(os.path.join(A_     ,      A_				)     ,      A_     ,      pattern='examples'				)




def       snake_case  (A_				:Tuple     ,      A_				:Optional[Any]=False				):







  '''simple docstring'''

  for pattern, fname in REPLACE_FILES.items():
    update_version_in_file(A_     ,      A_     ,      A_				)
  if not patch:
    update_version_in_examples(A_				)




def       snake_case  ():







  '''simple docstring'''

  a					:      str    =       '🤗 Transformers currently provides the following architectures'
  a					:      Dict    =       '1. Want to contribute a new model?'
  with open(A_     ,      'r'     ,      encoding='utf-8'     ,      newline='\n'				) as f:
    a					:      Optional[Any]    =       f.readlines()

  # Find the start of the list.
  a					:      List[str]    =       0
  while not lines[start_index].startswith(_start_prompt				):
    start_index += 1
  start_index += 1

  a					:      Optional[Any]    =       start_index
  # Update the lines in the model list.
  while not lines[index].startswith(_end_prompt				):
    if lines[index].startswith('1.'				):
      a					:      int    =       lines[index].replace(
          'https://huggingface.co/docs/diffusers/main/model_doc'     ,      'https://huggingface.co/docs/diffusers/model_doc'     ,      )
    index += 1

  with open(A_     ,      'w'     ,      encoding='utf-8'     ,      newline='\n'				) as f:
    f.writelines(A_				)




def       snake_case  ():







  '''simple docstring'''

  with open(REPLACE_FILES['init']     ,      'r'				) as f:
    a					:      List[str]    =       f.read()
  a					:      str    =       REPLACE_PATTERNS['init'][0].search(A_				).groups()[0]
  return packaging.version.parse(A_				)




def       snake_case  (A_				:Optional[Any]=False				):







  '''simple docstring'''

  a					:      Optional[int]    =       get_version()
  if patch and default_version.is_devrelease:
    raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!'				)
  if default_version.is_devrelease:
    a					:      Tuple    =       default_version.base_version
  elif patch:
    a					:      Union[str, Any]    =       f'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}'''
  else:
    a					:      Optional[Any]    =       f'''{default_version.major}.{default_version.minor + 1}.0'''

  # Now let's ask nicely if that's the right one.
  a					:      Union[str, Any]    =       input(f'''Which version are you releasing? [{default_version}]'''				)
  if len(A_				) == 0:
    a					:      int    =       default_version

  print(f'''Updating version to {version}.'''				)
  global_version_update(A_     ,      patch=A_				)




def       snake_case  ():







  '''simple docstring'''

  a					:      str    =       get_version()
  a					:      Optional[int]    =       f'''{current_version.major}.{current_version.minor + 1}.0.dev0'''
  a					:      Optional[int]    =       current_version.base_version

  # Check with the user we got that right.
  a					:      str    =       input(f'''Which version are we developing now? [{dev_version}]'''				)
  if len(A_				) == 0:
    a					:      Union[str, Any]    =       dev_version

  print(f'''Updating version to {version}.'''				)
  global_version_update(A_				)


#    print("Cleaning main README, don't forget to run `make fix-copies`.")
#    clean_main_ref_in_model_list()


if __name__ == "__main__":
   _UpperCamelCase    :   Dict   			=						argparse.ArgumentParser()
   parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.')
   parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.')
   _UpperCamelCase    :   Optional[Any]   			=						parser.parse_args()
   if not args.post_release:
      pre_release_work(patch=args.patch)
   elif args.patch:
      print('Nothing to do after a patch :-)')
   else:
      post_release_work()




