

'''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 						__magic_name__    (       _snake_case    ):


 def __init__(						self		: Optional[Any]			,						lowerCAmelCase__		: List[str]			,						lowerCAmelCase__		: Union[str, Any]=1_3			,						lowerCAmelCase__		: List[Any]=7			,						lowerCAmelCase__		: Dict=True			,						lowerCAmelCase__		: int=True			,						lowerCAmelCase__		: List[str]=False			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: str=9_9			,						lowerCAmelCase__		: int=3_2			,						lowerCAmelCase__		: Optional[Any]=5			,						lowerCAmelCase__		: Tuple=4			,						lowerCAmelCase__		: Optional[int]=6_4			,						lowerCAmelCase__		: int="gelu"			,						lowerCAmelCase__		: Optional[int]=0.1			,						lowerCAmelCase__		: int=0.1			,						lowerCAmelCase__		: Any=5_1_2			,						lowerCAmelCase__		: Dict=1_6			,						lowerCAmelCase__		: List[str]=2			,						lowerCAmelCase__		: Tuple=0.02			,						lowerCAmelCase__		: Any=3			,						lowerCAmelCase__		: Optional[int]=4			,						lowerCAmelCase__		: List[str]=None			,						lowerCAmelCase__		: Union[str, Any]=2			,						lowerCAmelCase__		: Dict=2			,						lowerCAmelCase__		: Dict=2			,						lowerCAmelCase__		: int=2			,						lowerCAmelCase__		: List[str]=4			,						lowerCAmelCase__		: Any=1			,						)    ->      List[str]:
   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 _UpperCamelCase	(						self		: Optional[Any]    )    ->      str:
   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 _UpperCamelCase	(						self		: str    )    ->      Optional[int]:
   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		: Union[str, Any]			,						lowerCAmelCase__		: List[str]			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Tuple			,						lowerCAmelCase__		: str			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: Any    )    ->      Optional[Any]:
   UpperCAmelCase					      =  SqueezeBertModel(config=lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )
   model.eval()
   UpperCAmelCase					      =  model(lowerCAmelCase__			,						lowerCAmelCase__    )
   UpperCAmelCase					      =  model(lowerCAmelCase__    )
   self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)    )


 def _UpperCamelCase	(						self		: Tuple			,						lowerCAmelCase__		: List[Any]			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: Dict			,						lowerCAmelCase__		: Optional[Any]			,						lowerCAmelCase__		: Optional[Any]			,						lowerCAmelCase__		: Dict    )    ->      Optional[int]:
   UpperCAmelCase					      =  SqueezeBertForMaskedLM(config=lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )
   model.eval()
   UpperCAmelCase					      =  model(lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						labels=lowerCAmelCase__    )
   self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size)    )


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


 def _UpperCamelCase	(						self		: str			,						lowerCAmelCase__		: Optional[int]			,						lowerCAmelCase__		: Optional[int]			,						lowerCAmelCase__		: List[Any]			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: Union[str, Any]    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  self.num_labels
   UpperCAmelCase					      =  SqueezeBertForTokenClassification(config=lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )
   model.eval()

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


 def _UpperCamelCase	(						self		: Optional[int]			,						lowerCAmelCase__		: List[str]			,						lowerCAmelCase__		: Dict			,						lowerCAmelCase__		: Union[str, Any]			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Optional[Any]			,						lowerCAmelCase__		: Dict    )    ->      Any:
   UpperCAmelCase					      =  self.num_choices
   UpperCAmelCase					      =  SqueezeBertForMultipleChoice(config=lowerCAmelCase__    )
   model.to(lowerCAmelCase__    )
   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(
       lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						labels=lowerCAmelCase__			,						)
   self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices)    )


 def _UpperCamelCase	(						self		: int    )    ->      Dict:
   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 						__magic_name__    (       _snake_case	,						_snake_case	,						unittest.TestCase    ):
 UpperCAmelCase							    =						(
     (
         SqueezeBertModel,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 UpperCAmelCase							    =						(
     {
         """feature-extraction""": SqueezeBertModel,
         """fill-mask""": SqueezeBertForMaskedLM,
         """question-answering""": SqueezeBertForQuestionAnswering,
         """text-classification""": SqueezeBertForSequenceClassification,
         """token-classification""": SqueezeBertForTokenClassification,
         """zero-shot""": SqueezeBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase							    =						False
 UpperCAmelCase							    =						True
 UpperCAmelCase							    =						False


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Optional[Any]:
   UpperCAmelCase					      =  SqueezeBertModelTester(self    )
   UpperCAmelCase					      =  ConfigTester(self			,						config_class=lowerCAmelCase__			,						dim=3_7    )


 def _UpperCamelCase	(						self		: str    )    ->      Any:
   self.config_tester.run_common_tests()


 def _UpperCamelCase	(						self		: Any    )    ->      Optional[int]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_model(*lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: Union[str, Any]    )    ->      int:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[Any]    )    ->      Dict:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: Optional[Any]    )    ->      List[str]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[str]    )    ->      str:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[Any]    )    ->      List[str]:
   UpperCAmelCase					      =  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCAmelCase__    )


 @slow
 def _UpperCamelCase	(						self		: Dict    )    ->      Optional[int]:
   for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase					      =  SqueezeBertModel.from_pretrained(lowerCAmelCase__    )
     self.assertIsNotNone(lowerCAmelCase__    )






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


 @slow
 def _UpperCamelCase	(						self		: Any    )    ->      Any:
   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(lowerCAmelCase__    )[0]
   UpperCAmelCase					      =  torch.Size((1, 3)    )
   self.assertEqual(output.shape			,						lowerCAmelCase__    )
   UpperCAmelCase					      =  torch.tensor([[0.6_401, -0.0_349, -0.6_041]]    )
   self.assertTrue(torch.allclose(lowerCAmelCase__			,						lowerCAmelCase__			,						atol=1e-4    )    )






lowerCAmelCase__       = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

lowerCAmelCase__       = [{"type": "code", "content": INSTALL_CONTENT}]
lowerCAmelCase__       = {
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}



