
'''simple docstring'''



import unittest

from transformers import SqueezeBertConfig, is_torch_available
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 (
         SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
         SqueezeBertModel,
     )

class    _a				(_lowerCamelCase):





       """simple docstring"""




       def __init__(							self       ,							A__       ,							A__=13       ,							A__=7       ,							A__=True       ,							A__=True       ,							A__=False       ,							A__=True       ,							A__=99       ,							A__=32       ,							A__=5       ,							A__=4       ,							A__=64       ,							A__="gelu"       ,							A__=0.1       ,							A__=0.1       ,							A__=5_12       ,							A__=16       ,							A__=2       ,							A__=0.02       ,							A__=3       ,							A__=4       ,							A__=None       ,							A__=2       ,							A__=2       ,							A__=2       ,							A__=2       ,							A__=4       ,							A__=1       ,							)    ->			Union[str, Any]:
          _SCREAMING_SNAKE_CASE      =				parent
          _SCREAMING_SNAKE_CASE      =				batch_size
          _SCREAMING_SNAKE_CASE      =				seq_length
          _SCREAMING_SNAKE_CASE      =				is_training
          _SCREAMING_SNAKE_CASE      =				use_input_mask
          _SCREAMING_SNAKE_CASE      =				use_token_type_ids
          _SCREAMING_SNAKE_CASE      =				use_labels
          _SCREAMING_SNAKE_CASE      =				vocab_size
          _SCREAMING_SNAKE_CASE      =				hidden_size
          _SCREAMING_SNAKE_CASE      =				num_hidden_layers
          _SCREAMING_SNAKE_CASE      =				num_attention_heads
          _SCREAMING_SNAKE_CASE      =				intermediate_size
          _SCREAMING_SNAKE_CASE      =				hidden_act
          _SCREAMING_SNAKE_CASE      =				hidden_dropout_prob
          _SCREAMING_SNAKE_CASE      =				attention_probs_dropout_prob
          _SCREAMING_SNAKE_CASE      =				max_position_embeddings
          _SCREAMING_SNAKE_CASE      =				type_vocab_size
          _SCREAMING_SNAKE_CASE      =				type_sequence_label_size
          _SCREAMING_SNAKE_CASE      =				initializer_range
          _SCREAMING_SNAKE_CASE      =				num_labels
          _SCREAMING_SNAKE_CASE      =				num_choices
          _SCREAMING_SNAKE_CASE      =				scope
          _SCREAMING_SNAKE_CASE      =				q_groups
          _SCREAMING_SNAKE_CASE      =				k_groups
          _SCREAMING_SNAKE_CASE      =				v_groups
          _SCREAMING_SNAKE_CASE      =				post_attention_groups
          _SCREAMING_SNAKE_CASE      =				intermediate_groups
          _SCREAMING_SNAKE_CASE      =				output_groups






       def        UpperCamelCase				(							self      )    ->			List[Any]:
          _SCREAMING_SNAKE_CASE      =				ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size      )

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

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

          _SCREAMING_SNAKE_CASE      =				self.get_config()

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






       def        UpperCamelCase				(							self      )    ->			Tuple:
          return SqueezeBertConfig(
              embedding_size=self.hidden_size       ,							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       ,							hidden_act=self.hidden_act       ,							attention_probs_dropout_prob=self.hidden_dropout_prob       ,							attention_dropout=self.attention_probs_dropout_prob       ,							max_position_embeddings=self.max_position_embeddings       ,							initializer_range=self.initializer_range       ,							q_groups=self.q_groups       ,							k_groups=self.k_groups       ,							v_groups=self.v_groups       ,							post_attention_groups=self.post_attention_groups       ,							intermediate_groups=self.intermediate_groups       ,							output_groups=self.output_groups       ,							)






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			Any:
          _SCREAMING_SNAKE_CASE      =				SqueezeBertModel(config=A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				model(A__       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				model(A__      )
          self.parent.assertEqual(result.last_hidden_state.shape       ,							(self.batch_size, self.seq_length, self.hidden_size)      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			List[str]:
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForMaskedLM(config=A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				model(A__       ,							attention_mask=A__       ,							labels=A__      )
          self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.vocab_size)      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			int:
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForQuestionAnswering(config=A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				model(
              A__       ,							attention_mask=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        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE      =				self.num_labels
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForSequenceClassification(A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				model(A__       ,							attention_mask=A__       ,							labels=A__      )
          self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_labels)      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE      =				self.num_labels
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForTokenClassification(config=A__      )
          model.to(A__      )
          model.eval()

          _SCREAMING_SNAKE_CASE      =				model(A__       ,							attention_mask=A__       ,							labels=A__      )
          self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.num_labels)      )






       def        UpperCamelCase				(							self       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__       ,							A__      )    ->			List[Any]:
          _SCREAMING_SNAKE_CASE      =				self.num_choices
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForMultipleChoice(config=A__      )
          model.to(A__      )
          model.eval()
          _SCREAMING_SNAKE_CASE      =				input_ids.unsqueeze(1      ).expand(-1       ,							self.num_choices       ,							-1      ).contiguous()
          _SCREAMING_SNAKE_CASE      =				input_mask.unsqueeze(1      ).expand(-1       ,							self.num_choices       ,							-1      ).contiguous()
          _SCREAMING_SNAKE_CASE      =				model(
              A__       ,							attention_mask=A__       ,							labels=A__       ,							)
          self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_choices)      )






       def        UpperCamelCase				(							self      )    ->			int:
          _SCREAMING_SNAKE_CASE      =				self.prepare_config_and_inputs()
          ((_SCREAMING_SNAKE_CASE) ,							(_SCREAMING_SNAKE_CASE) ,							(_SCREAMING_SNAKE_CASE) ,							(_SCREAMING_SNAKE_CASE) ,							(_SCREAMING_SNAKE_CASE) ,							(_SCREAMING_SNAKE_CASE))      =				config_and_inputs
          _SCREAMING_SNAKE_CASE      =				{"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict






@require_torch
class    _a				(_lowerCamelCase  ,					_lowerCamelCase  ,					unittest.TestCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE      =  (
           (
               SqueezeBertModel,
               SqueezeBertForMaskedLM,
               SqueezeBertForMultipleChoice,
               SqueezeBertForQuestionAnswering,
               SqueezeBertForSequenceClassification,
               SqueezeBertForTokenClassification,
           )
           if is_torch_available()
           else None
       )
       SCREAMING_SNAKE_CASE      =  (
           {
               'feature-extraction': SqueezeBertModel,
               'fill-mask': SqueezeBertForMaskedLM,
               'question-answering': SqueezeBertForQuestionAnswering,
               'text-classification': SqueezeBertForSequenceClassification,
               'token-classification': SqueezeBertForTokenClassification,
               'zero-shot': SqueezeBertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE      =  False
       SCREAMING_SNAKE_CASE      =  True
       SCREAMING_SNAKE_CASE      =  False






       def        UpperCamelCase				(							self      )    ->			str:
          _SCREAMING_SNAKE_CASE      =				SqueezeBertModelTester(self      )
          _SCREAMING_SNAKE_CASE      =				ConfigTester(self       ,							config_class=A__       ,							dim=37      )






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






       def        UpperCamelCase				(							self      )    ->			Dict:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_model(*A__      )






       def        UpperCamelCase				(							self      )    ->			Union[str, Any]:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_masked_lm(*A__      )






       def        UpperCamelCase				(							self      )    ->			Dict:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_question_answering(*A__      )






       def        UpperCamelCase				(							self      )    ->			str:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_sequence_classification(*A__      )






       def        UpperCamelCase				(							self      )    ->			Dict:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_token_classification(*A__      )






       def        UpperCamelCase				(							self      )    ->			Dict:
          _SCREAMING_SNAKE_CASE      =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_multiple_choice(*A__      )






       @slow
       def        UpperCamelCase				(							self      )    ->			List[Any]:
          for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _SCREAMING_SNAKE_CASE      =				SqueezeBertModel.from_pretrained(A__      )
             self.assertIsNotNone(A__      )






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





       """simple docstring"""




       @slow
       def        UpperCamelCase				(							self      )    ->			Optional[int]:
          _SCREAMING_SNAKE_CASE      =				SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli"""      )

          _SCREAMING_SNAKE_CASE      =				torch.tensor([[1, 2_94_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 13, 15_88, 2]]      )
          _SCREAMING_SNAKE_CASE      =				model(A__      )[0]
          _SCREAMING_SNAKE_CASE      =				torch.Size((1, 3)      )
          self.assertEqual(output.shape       ,							A__      )
          _SCREAMING_SNAKE_CASE      =				torch.tensor([[0.6401, -0.0349, -0.6041]]      )
          self.assertTrue(torch.allclose(A__       ,							A__       ,							atol=1E-4      )      )








'''simple docstring'''



def    lowerCAmelCase_	(				SCREAMING_SNAKE_CASE_						)     ->     int:




   """simple docstring"""

   if not isinstance(SCREAMING_SNAKE_CASE_    ,      SCREAMING_SNAKE_CASE_						):
      raise ValueError("""multiplicative_persistence() only accepts integral values"""						)
   if num < 0:
      raise ValueError("""multiplicative_persistence() does not accept negative values"""						)

   _SCREAMING_SNAKE_CASE      =				0
   _SCREAMING_SNAKE_CASE      =				str(SCREAMING_SNAKE_CASE_						)

   while len(SCREAMING_SNAKE_CASE_						) != 1:
      _SCREAMING_SNAKE_CASE      =				[int(SCREAMING_SNAKE_CASE_						) for i in num_string]

      _SCREAMING_SNAKE_CASE      =				1
      for i in range(0    ,      len(SCREAMING_SNAKE_CASE_						)						):
         total *= numbers[i]

      _SCREAMING_SNAKE_CASE      =				str(SCREAMING_SNAKE_CASE_						)

      steps += 1
   return steps
def    lowerCAmelCase_	(				SCREAMING_SNAKE_CASE_						)     ->     int:




   """simple docstring"""

   if not isinstance(SCREAMING_SNAKE_CASE_    ,      SCREAMING_SNAKE_CASE_						):
      raise ValueError("""additive_persistence() only accepts integral values"""						)
   if num < 0:
      raise ValueError("""additive_persistence() does not accept negative values"""						)

   _SCREAMING_SNAKE_CASE      =				0
   _SCREAMING_SNAKE_CASE      =				str(SCREAMING_SNAKE_CASE_						)

   while len(SCREAMING_SNAKE_CASE_						) != 1:
      _SCREAMING_SNAKE_CASE      =				[int(SCREAMING_SNAKE_CASE_						) for i in num_string]

      _SCREAMING_SNAKE_CASE      =				0
      for i in range(0    ,      len(SCREAMING_SNAKE_CASE_						)						):
         total += numbers[i]

      _SCREAMING_SNAKE_CASE      =				str(SCREAMING_SNAKE_CASE_						)

      steps += 1
   return steps


if __name__ == "__main__":
     import doctest

     doctest.testmod()






