'''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     __UpperCAmelCase       :





  def __init__(	self    ,     _lowerCamelCase    ,     _lowerCamelCase=13    ,     _lowerCamelCase=7    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=True    ,     _lowerCamelCase=99    ,     _lowerCamelCase=64    ,     _lowerCamelCase=32    ,     _lowerCamelCase=5    ,     _lowerCamelCase=4    ,     _lowerCamelCase=37    ,     _lowerCamelCase="gelu"    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=0.1    ,     _lowerCamelCase=512    ,     _lowerCamelCase=16    ,     _lowerCamelCase=2    ,     _lowerCamelCase=0.02    ,     _lowerCamelCase=3    ,     _lowerCamelCase=4    ,     _lowerCamelCase=None    ,     ):
     lowerCAmelCase_			=      parent
     lowerCAmelCase_			=      batch_size
     lowerCAmelCase_			=      seq_length
     lowerCAmelCase_			=      is_training
     lowerCAmelCase_			=      use_input_mask
     lowerCAmelCase_			=      use_token_type_ids
     lowerCAmelCase_			=      use_labels
     lowerCAmelCase_			=      vocab_size
     lowerCAmelCase_			=      hidden_size
     lowerCAmelCase_			=      embedding_size
     lowerCAmelCase_			=      num_hidden_layers
     lowerCAmelCase_			=      num_attention_heads
     lowerCAmelCase_			=      intermediate_size
     lowerCAmelCase_			=      hidden_act
     lowerCAmelCase_			=      hidden_dropout_prob
     lowerCAmelCase_			=      attention_probs_dropout_prob
     lowerCAmelCase_			=      max_position_embeddings
     lowerCAmelCase_			=      type_vocab_size
     lowerCAmelCase_			=      type_sequence_label_size
     lowerCAmelCase_			=      initializer_range
     lowerCAmelCase_			=      num_labels
     lowerCAmelCase_			=      num_choices
     lowerCAmelCase_			=      scope





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size )

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

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

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

     lowerCAmelCase_			=      self.get_config()

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





  def 						UpperCAmelCase_	(	self ):
     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=_lowerCamelCase    ,     initializer_range=self.initializer_range    ,     )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      MobileBertModel(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase )
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     token_type_ids=_lowerCamelCase )
     lowerCAmelCase_			=      model(_lowerCamelCase )

     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    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      MobileBertForMaskedLM(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.vocab_size) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      MobileBertForNextSentencePrediction(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase    ,     )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, 2) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      MobileBertForPreTraining(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase    ,     next_sentence_label=_lowerCamelCase    ,     )
     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 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      MobileBertForQuestionAnswering(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     start_positions=_lowerCamelCase    ,     end_positions=_lowerCamelCase    ,     )
     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    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      self.num_labels
     lowerCAmelCase_			=      MobileBertForSequenceClassification(_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_labels) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      self.num_labels
     lowerCAmelCase_			=      MobileBertForTokenClassification(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      model(_lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.num_labels) )





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase ):
     lowerCAmelCase_			=      self.num_choices
     lowerCAmelCase_			=      MobileBertForMultipleChoice(config=_lowerCamelCase )
     model.to(_lowerCamelCase )
     model.eval()
     lowerCAmelCase_			=      input_ids.unsqueeze(1 ).expand(-1    ,     self.num_choices    ,     -1 ).contiguous()
     lowerCAmelCase_			=      token_type_ids.unsqueeze(1 ).expand(-1    ,     self.num_choices    ,     -1 ).contiguous()
     lowerCAmelCase_			=      input_mask.unsqueeze(1 ).expand(-1    ,     self.num_choices    ,     -1 ).contiguous()
     lowerCAmelCase_			=      model(
         _lowerCamelCase    ,     attention_mask=_lowerCamelCase    ,     token_type_ids=_lowerCamelCase    ,     labels=_lowerCamelCase    ,     )
     self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_choices) )







  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.prepare_config_and_inputs()
     (
         (
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,(
         lowerCAmelCase_
     )	,
     )			=      config_and_inputs
     lowerCAmelCase_			=      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






@require_torch
class     __UpperCAmelCase       ( __a						,       __a						,       unittest.TestCase		):
  __A :				Optional[Any]											= (
      (
          MobileBertModel,
          MobileBertForMaskedLM,
          MobileBertForMultipleChoice,
          MobileBertForNextSentencePrediction,
          MobileBertForPreTraining,
          MobileBertForQuestionAnswering,
          MobileBertForSequenceClassification,
          MobileBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  __A :				Tuple											= (
      {
          'feature-extraction': MobileBertModel,
          'fill-mask': MobileBertForMaskedLM,
          'question-answering': MobileBertForQuestionAnswering,
          'text-classification': MobileBertForSequenceClassification,
          'token-classification': MobileBertForTokenClassification,
          'zero-shot': MobileBertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  __A :				str											= True





  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase=False ):
     lowerCAmelCase_			=      super()._prepare_for_class(_lowerCamelCase    ,     _lowerCamelCase    ,     return_labels=_lowerCamelCase )

     if return_labels:
        if model_class in get_values(_lowerCamelCase ):
           lowerCAmelCase_			=      torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)    ,     dtype=torch.long    ,     device=_lowerCamelCase )
           lowerCAmelCase_			=      torch.zeros(
               self.model_tester.batch_size    ,     dtype=torch.long    ,     device=_lowerCamelCase )
     return inputs_dict





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      MobileBertModelTester(self )
     lowerCAmelCase_			=      ConfigTester(self    ,     config_class=_lowerCamelCase    ,     hidden_size=37 )





  def 						UpperCAmelCase_	(	self ):
     self.config_tester.run_common_tests()





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_model(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCamelCase )





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCamelCase )







  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCamelCase )






def snake_case_      (					__snake_case  :							List[Any])			->					Union[str, Any]:
   return torch.tensor(
       __snake_case	,   dtype=torch.long	,   device=__snake_case	,   )


A_     :						Dict    							=1e-3






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





  @slow
  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      MobileBertModel.from_pretrained('''google/mobilebert-uncased''' ).to(_lowerCamelCase )
     lowerCAmelCase_			=      _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]] )
     with torch.no_grad():
        lowerCAmelCase_			=      model(_lowerCamelCase )[0]
     lowerCAmelCase_			=      torch.Size((1, 9, 512) )
     self.assertEqual(output.shape    ,     _lowerCamelCase )
     lowerCAmelCase_			=      torch.tensor(
         [
             [
                 [-2.4736526E07, 8.2691656E04, 1.6521838E05],
                 [-5.7541704E-01, 3.9056022E00, 4.4011507E00],
                 [2.6047359E00, 1.5677652E00, -1.7324188E-01],
             ]
         ]    ,     device=_lowerCamelCase    ,     )

     # 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
     lowerCAmelCase_			=      torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE )
     lowerCAmelCase_			=      torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE )

     self.assertTrue(lower_bound and upper_bound )






'''simple docstring'''

from __future__ import annotations

from typing import Any



def snake_case_      (					__snake_case  :							list[Any])			->					None:
   create_state_space_tree(__snake_case	,   []	,   0)



def snake_case_      (					__snake_case  :							list[Any]	,   __snake_case  :							list[Any]	,   __snake_case  :							int)			->					None:

   if index == len(__snake_case):
      print(__snake_case)
      return

   create_state_space_tree(__snake_case	,   __snake_case	,   index + 1)
   current_subsequence.append(sequence[index])
   create_state_space_tree(__snake_case	,   __snake_case	,   index + 1)
   current_subsequence.pop()


if __name__ == "__main__":
 A_     :						list[Any]    							=[3, 1, 2, 4]
 generate_all_subsequences(seq)

 seq.clear()
 seq.extend(['''A''', '''B''', '''C'''])
 generate_all_subsequences(seq)





