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							(				UpperCAmelCase_     ):


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



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  seq_length
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  use_input_mask
         UpperCAmelCase__								=  use_token_type_ids
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  vocab_size
         UpperCAmelCase__								=  hidden_size
         UpperCAmelCase__								=  num_hidden_layers
         UpperCAmelCase__								=  num_attention_heads
         UpperCAmelCase__								=  intermediate_size
         UpperCAmelCase__								=  hidden_act
         UpperCAmelCase__								=  hidden_dropout_prob
         UpperCAmelCase__								=  attention_probs_dropout_prob
         UpperCAmelCase__								=  max_position_embeddings
         UpperCAmelCase__								=  type_vocab_size
         UpperCAmelCase__								=  type_sequence_label_size
         UpperCAmelCase__								=  initializer_range
         UpperCAmelCase__								=  num_labels
         UpperCAmelCase__								=  num_choices
         UpperCAmelCase__								=  scope
         UpperCAmelCase__								=  q_groups
         UpperCAmelCase__								=  k_groups
         UpperCAmelCase__								=  v_groups
         UpperCAmelCase__								=  post_attention_groups
         UpperCAmelCase__								=  intermediate_groups
         UpperCAmelCase__								=  output_groups


   def 			lowercase_      (self     :						Union[str, Any]		)						->				List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size		)

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

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

         UpperCAmelCase__								=  self.get_config()

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


   def 			lowercase_      (self     :						Optional[int]		)						->				Union[str, Any]:
         """simple docstring"""



         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 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						List[str]		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  SqueezeBertModel(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase       ,  __UpperCAmelCase		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)		)


   def 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Optional[int]		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  SqueezeBertForMaskedLM(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase       ,  attention_mask=__UpperCAmelCase       ,  labels=__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)		)


   def 			lowercase_      (self     :						Tuple       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Union[str, Any]		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  SqueezeBertForQuestionAnswering(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(
             __UpperCAmelCase       ,  attention_mask=__UpperCAmelCase       ,  start_positions=__UpperCAmelCase       ,  end_positions=__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 			lowercase_      (self     :						List[Any]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						List[Any]		)						->				List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  SqueezeBertForSequenceClassification(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase       ,  attention_mask=__UpperCAmelCase       ,  labels=__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)		)


   def 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Union[str, Any]		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  SqueezeBertForTokenClassification(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()

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


   def 			lowercase_      (self     :						List[str]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Any		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_choices
         UpperCAmelCase__								=  SqueezeBertForMultipleChoice(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  input_ids.unsqueeze(1		).expand(-1       ,  self.num_choices       ,  -1		).contiguous()
         UpperCAmelCase__								=  input_mask.unsqueeze(1		).expand(-1       ,  self.num_choices       ,  -1		).contiguous()
         UpperCAmelCase__								=  model(
             __UpperCAmelCase       ,  attention_mask=__UpperCAmelCase       ,  labels=__UpperCAmelCase       ,  )
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices)		)





   def 			lowercase_      (self     :						Dict		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  self.prepare_config_and_inputs()
         ((UpperCAmelCase__)		,      (UpperCAmelCase__)		,      (UpperCAmelCase__)		,      (UpperCAmelCase__)		,      (UpperCAmelCase__)		,      (UpperCAmelCase__))								=  config_and_inputs
         UpperCAmelCase__								=  {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict





@require_torch
class      A							(				UpperCAmelCase_		,				UpperCAmelCase_		,				unittest.TestCase     ):
   __UpperCAmelCase : Optional[int]      =			(
       (
           SqueezeBertModel,
           SqueezeBertForMaskedLM,
           SqueezeBertForMultipleChoice,
           SqueezeBertForQuestionAnswering,
           SqueezeBertForSequenceClassification,
           SqueezeBertForTokenClassification,
       )
       if is_torch_available()
       else None
   )
   __UpperCAmelCase : Any      =			(
       {
           'feature-extraction': SqueezeBertModel,
           'fill-mask': SqueezeBertForMaskedLM,
           'question-answering': SqueezeBertForQuestionAnswering,
           'text-classification': SqueezeBertForSequenceClassification,
           'token-classification': SqueezeBertForTokenClassification,
           'zero-shot': SqueezeBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __UpperCAmelCase : str      =			False
   __UpperCAmelCase : Tuple      =			True
   __UpperCAmelCase : Any      =			False


   def 			lowercase_      (self     :						Optional[Any]		)						->				List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  SqueezeBertModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self       ,  config_class=__UpperCAmelCase       ,  dim=3_7		)


   def 			lowercase_      (self     :						Any		)						->				Union[str, Any]:
         """simple docstring"""



         self.config_tester.run_common_tests()


   def 			lowercase_      (self     :						List[str]		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_model(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Any		)						->				Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_masked_lm(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						str		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_question_answering(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Dict		)						->				Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Any		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_token_classification(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Dict		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_multiple_choice(*__UpperCAmelCase		)





   @slow
   def 			lowercase_      (self     :						List[str]		)						->				List[Any]:
         """simple docstring"""



         for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__								=  SqueezeBertModel.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)





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


   @slow
   def 			lowercase_      (self     :						int		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"		)

         UpperCAmelCase__								=  torch.tensor([[1, 2_9_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 1_3, 1_5_8_8, 2]]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)[0]
         UpperCAmelCase__								=  torch.Size((1, 3)		)
         self.assertEqual(output.shape       ,  __UpperCAmelCase		)
         UpperCAmelCase__								=  torch.tensor([[0.6401, -0.0349, -0.6041]]		)
         self.assertTrue(torch.allclose(__UpperCAmelCase       ,  __UpperCAmelCase       ,  atol=1E-4		)		)









def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       int	,							lowercase	:       int     ):


      '''simple docstring'''



      return int((input_a, input_a).count(1     ) != 0     )

def 			_SCREAMING_SNAKE_CASE			(							):


      '''simple docstring'''



      assert or_gate(0	,							0     ) == 0
      assert or_gate(0	,							1     ) == 1
      assert or_gate(1	,							0     ) == 1
      assert or_gate(1	,							1     ) == 1


if __name__ == "__main__":
 print(or_gate(0, 1))
 print(or_gate(1, 0))
 print(or_gate(0, 0))
 print(or_gate(1, 1))



