






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  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


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


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size      )

       SCREAMING_SNAKE_CASE       :      Optional[int]							=    None
       if self.use_input_mask:
         SCREAMING_SNAKE_CASE       :      int							=    random_attention_mask([self.batch_size, self.seq_length]      )

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

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.get_config()

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


     def 				_A       (				self       :	List[Any]      ):
       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 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Any      ):
       SCREAMING_SNAKE_CASE       :      List[Any]							=    SqueezeBertModel(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      Dict							=    model(UpperCAmelCase_					, UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    model(UpperCAmelCase_      )
       self.parent.assertEqual(result.last_hidden_state.shape					, (self.batch_size, self.seq_length, self.hidden_size)      )


     def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	str      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    SqueezeBertForMaskedLM(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      List[str]							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.vocab_size)      )


     def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Optional[int]      ):
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    SqueezeBertForQuestionAnswering(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    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 				_A       (				self       :	Optional[Any]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	List[str]      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.num_labels
       SCREAMING_SNAKE_CASE       :      int							=    SqueezeBertForSequenceClassification(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)      )


     def 				_A       (				self       :	List[Any]					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      List[Any]							=    self.num_labels
       SCREAMING_SNAKE_CASE       :      List[str]							=    SqueezeBertForTokenClassification(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()

       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model(UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.num_labels)      )


     def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Tuple      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.num_choices
       SCREAMING_SNAKE_CASE       :      Tuple							=    SqueezeBertForMultipleChoice(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      str							=    input_ids.unsqueeze(1      ).expand(-1					, self.num_choices					, -1      ).contiguous()
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    input_mask.unsqueeze(1      ).expand(-1					, self.num_choices					, -1      ).contiguous()
       SCREAMING_SNAKE_CASE       :      List[str]							=    model(
           UpperCAmelCase_					, attention_mask=UpperCAmelCase_					, labels=UpperCAmelCase_					, )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_choices)      )






     def 				_A       (				self       :	Any      ):
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.prepare_config_and_inputs()
       ((SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE)						,  (SCREAMING_SNAKE_CASE))       :      List[Any]							=    config_and_inputs
       SCREAMING_SNAKE_CASE       :      Any							=    {"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict





@require_torch
class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase      ,		lowerCAmelCase      ,		unittest.TestCase					):
     '''simple docstring'''


     UpperCamelCase_							:		Tuple			=	(
         (
             SqueezeBertModel,
             SqueezeBertForMaskedLM,
             SqueezeBertForMultipleChoice,
             SqueezeBertForQuestionAnswering,
             SqueezeBertForSequenceClassification,
             SqueezeBertForTokenClassification,
         )
         if is_torch_available()
         else None
     )
     UpperCamelCase_							:		List[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_							:		List[Any]			=	False
     UpperCamelCase_							:		Optional[Any]			=	True
     UpperCamelCase_							:		Optional[Any]			=	False


     def 				_A       (				self       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    SqueezeBertModelTester(self      )
       SCREAMING_SNAKE_CASE       :      str							=    ConfigTester(self					, config_class=UpperCAmelCase_					, dim=37      )


     def 				_A       (				self       :	str      ):
       self.config_tester.run_common_tests()


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_model(*UpperCAmelCase_      )


     def 				_A       (				self       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCAmelCase_      )


     def 				_A       (				self       :	Union[str, Any]      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCAmelCase_      )


     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCAmelCase_      )


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCAmelCase_      )


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCAmelCase_      )






     @slow
     def 				_A       (				self       :	Dict      ):
       for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    SqueezeBertModel.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )





@require_sentencepiece
@require_tokenizers
@require_torch
class  SCREAMING_SNAKE_CASE			(			unittest.TestCase					):
     '''simple docstring'''


     @slow
     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      str							=    SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"      )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]      )
       SCREAMING_SNAKE_CASE       :      Any							=    model(UpperCAmelCase_      )[0]
       SCREAMING_SNAKE_CASE       :      str							=    torch.Size((1, 3)      )
       self.assertEqual(output.shape					, UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Dict							=    torch.tensor([[0.6_401, -0.0_349, -0.6_041]]      )
       self.assertTrue(torch.allclose(UpperCAmelCase_					, UpperCAmelCase_					, atol=1E-4      )      )














def   lowerCamelCase__			(     lowercase						,     lowercase				):

  """simple docstring"""






  return numa ^ numa < 0


if __name__ == "__main__":
   import doctest

   doctest.testmod()






