


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






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






     def  a_					(			self		)       ->    Dict:
          _a						=		ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size		)

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

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

          _a						=		self.get_config()

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






     def  a_					(			self		)       ->    List[str]:
          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     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    int:
          _a						=		SqueezeBertModel(config=UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()
          _a						=		model(UpperCamelCase__     ,							UpperCamelCase__		)
          _a						=		model(UpperCamelCase__		)
          self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)		)






     def  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Any:
          _a						=		SqueezeBertForMaskedLM(config=UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()
          _a						=		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     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    List[str]:
          _a						=		SqueezeBertForQuestionAnswering(config=UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()
          _a						=		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     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Tuple:
          _a						=		self.num_labels
          _a						=		SqueezeBertForSequenceClassification(UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()
          _a						=		model(UpperCamelCase__     ,							attention_mask=UpperCamelCase__     ,							labels=UpperCamelCase__		)
          self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)		)






     def  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Optional[Any]:
          _a						=		self.num_labels
          _a						=		SqueezeBertForTokenClassification(config=UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()

          _a						=		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     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    List[str]:
          _a						=		self.num_choices
          _a						=		SqueezeBertForMultipleChoice(config=UpperCamelCase__		)
          model.to(UpperCamelCase__		)
          model.eval()
          _a						=		input_ids.unsqueeze(1		).expand(-1     ,							self.num_choices     ,							-1		).contiguous()
          _a						=		input_mask.unsqueeze(1		).expand(-1     ,							self.num_choices     ,							-1		).contiguous()
          _a						=		model(
              UpperCamelCase__     ,							attention_mask=UpperCamelCase__     ,							labels=UpperCamelCase__     ,							)
          self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_choices)		)






     def  a_					(			self		)       ->    Dict:
          _a						=		self.prepare_config_and_inputs()
          (_a)						=		config_and_inputs
          _a						=		{'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict







@require_torch
class 						__SCREAMING_SNAKE_CASE					(      __A ,			__A ,			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  a_					(			self		)       ->    Optional[int]:
          _a						=		SqueezeBertModelTester(self		)
          _a						=		ConfigTester(self     ,							config_class=UpperCamelCase__     ,							dim=37		)






     def  a_					(			self		)       ->    Tuple:
          self.config_tester.run_common_tests()






     def  a_					(			self		)       ->    List[str]:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_model(*UpperCamelCase__		)






     def  a_					(			self		)       ->    List[Any]:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCamelCase__		)






     def  a_					(			self		)       ->    int:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCamelCase__		)






     def  a_					(			self		)       ->    Union[str, Any]:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCamelCase__		)






     def  a_					(			self		)       ->    Union[str, Any]:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCamelCase__		)






     def  a_					(			self		)       ->    Union[str, Any]:
          _a						=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCamelCase__		)






     @slow
     def  a_					(			self		)       ->    str:
          for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a						=		SqueezeBertModel.from_pretrained(UpperCamelCase__		)
               self.assertIsNotNone(UpperCamelCase__		)







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






     @slow
     def  a_					(			self		)       ->    List[str]:
          _a						=		SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"		)

          _a						=		torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]		)
          _a						=		model(UpperCamelCase__		)[0]
          _a						=		torch.Size((1, 3)		)
          self.assertEqual(output.shape     ,							UpperCamelCase__		)
          _a						=		torch.tensor([[0.64_01, -0.03_49, -0.60_41]]		)
          self.assertTrue(torch.allclose(UpperCamelCase__     ,							UpperCamelCase__     ,							atol=1e-4		)		)





'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__				     =		logging.get_logger(__name__)

lowercase__				     =		{
    "YituTech/conv-bert-base": "https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json",
    "YituTech/conv-bert-medium-small": (
        "https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json"
    ),
    "YituTech/conv-bert-small": "https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json",
    # See all ConvBERT models at https://huggingface.co/models?filter=convbert
}





class 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ):
     UpperCAmelCase 					=				'''convbert'''






     def __init__(			self     ,							__UpperCamelCase=30_522     ,							__UpperCamelCase=768     ,							__UpperCamelCase=12     ,							__UpperCamelCase=12     ,							__UpperCamelCase=3_072     ,							__UpperCamelCase="gelu"     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=0.1     ,							__UpperCamelCase=512     ,							__UpperCamelCase=2     ,							__UpperCamelCase=0.02     ,							__UpperCamelCase=1e-12     ,							__UpperCamelCase=1     ,							__UpperCamelCase=0     ,							__UpperCamelCase=2     ,							__UpperCamelCase=768     ,							__UpperCamelCase=2     ,							__UpperCamelCase=9     ,							__UpperCamelCase=1     ,							__UpperCamelCase=None     ,							**__UpperCamelCase     ,							)       ->    Optional[int]:
          super().__init__(
              pad_token_id=__UpperCamelCase     ,							bos_token_id=__UpperCamelCase     ,							eos_token_id=__UpperCamelCase     ,							**__UpperCamelCase     ,							)

          _a						=		vocab_size
          _a						=		hidden_size
          _a						=		num_hidden_layers
          _a						=		num_attention_heads
          _a						=		intermediate_size
          _a						=		hidden_act
          _a						=		hidden_dropout_prob
          _a						=		attention_probs_dropout_prob
          _a						=		max_position_embeddings
          _a						=		type_vocab_size
          _a						=		initializer_range
          _a						=		layer_norm_eps
          _a						=		embedding_size
          _a						=		head_ratio
          _a						=		conv_kernel_size
          _a						=		num_groups
          _a						=		classifier_dropout







class 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ):






     @property
     def  a_					(			self		)       ->    Mapping[str, Mapping[int, str]]:
          if self.task == "multiple-choice":
               _a						=		{0: "batch", 1: "choice", 2: "sequence"}
          else:
               _a						=		{0: "batch", 1: "sequence"}
          return OrderedDict(
              [
                  ("input_ids", dynamic_axis),
                  ("attention_mask", dynamic_axis),
                  ("token_type_ids", dynamic_axis),
              ]		)

