





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    _UpperCAmelCase							(		_snake_case     ):
      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__=512      ,       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      ,       )     ->   List[str]:
         A_     :				int			      =	parent
         A_     :				Optional[Any]			      =	batch_size
         A_     :				List[str]			      =	seq_length
         A_     :				str			      =	is_training
         A_     :				Any			      =	use_input_mask
         A_     :				Dict			      =	use_token_type_ids
         A_     :				Optional[Any]			      =	use_labels
         A_     :				Dict			      =	vocab_size
         A_     :				Dict			      =	hidden_size
         A_     :				Tuple			      =	num_hidden_layers
         A_     :				int			      =	num_attention_heads
         A_     :				Any			      =	intermediate_size
         A_     :				Union[str, Any]			      =	hidden_act
         A_     :				int			      =	hidden_dropout_prob
         A_     :				Optional[Any]			      =	attention_probs_dropout_prob
         A_     :				int			      =	max_position_embeddings
         A_     :				Dict			      =	type_vocab_size
         A_     :				Optional[Any]			      =	type_sequence_label_size
         A_     :				Union[str, Any]			      =	initializer_range
         A_     :				int			      =	num_labels
         A_     :				List[Any]			      =	num_choices
         A_     :				int			      =	scope
         A_     :				List[str]			      =	q_groups
         A_     :				Optional[Any]			      =	k_groups
         A_     :				Union[str, Any]			      =	v_groups
         A_     :				int			      =	post_attention_groups
         A_     :				Any			      =	intermediate_groups
         A_     :				Tuple			      =	output_groups
      def 		_lowerCamelCase      (		self       )     ->   str:
         A_     :				Optional[int]			      =	ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size       )

         A_     :				str			      =	None
         if self.use_input_mask:
            A_     :				Union[str, Any]			      =	random_attention_mask([self.batch_size, self.seq_length]       )

         A_     :				List[Any]			      =	None
         A_     :				Any			      =	None
         A_     :				Optional[Any]			      =	None
         if self.use_labels:
            A_     :				int			      =	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size       )
            A_     :				int			      =	ids_tensor([self.batch_size, self.seq_length]      ,       self.num_labels       )
            A_     :				List[Any]			      =	ids_tensor([self.batch_size]      ,       self.num_choices       )

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

         return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 		_lowerCamelCase      (		self       )     ->   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 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   Optional[Any]:
         A_     :				Union[str, Any]			      =	SqueezeBertModel(config=lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()
         A_     :				Any			      =	model(lowerCAmelCase__      ,       lowerCAmelCase__       )
         A_     :				Optional[Any]			      =	model(lowerCAmelCase__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   Optional[int]:
         A_     :				Optional[Any]			      =	SqueezeBertForMaskedLM(config=lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()
         A_     :				Optional[Any]			      =	model(lowerCAmelCase__      ,       attention_mask=lowerCAmelCase__      ,       labels=lowerCAmelCase__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   int:
         A_     :				Tuple			      =	SqueezeBertForQuestionAnswering(config=lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()
         A_     :				Optional[int]			      =	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 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   List[Any]:
         A_     :				Optional[Any]			      =	self.num_labels
         A_     :				List[Any]			      =	SqueezeBertForSequenceClassification(lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()
         A_     :				str			      =	model(lowerCAmelCase__      ,       attention_mask=lowerCAmelCase__      ,       labels=lowerCAmelCase__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   Union[str, Any]:
         A_     :				Optional[Any]			      =	self.num_labels
         A_     :				Union[str, Any]			      =	SqueezeBertForTokenClassification(config=lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()

         A_     :				Optional[Any]			      =	model(lowerCAmelCase__      ,       attention_mask=lowerCAmelCase__      ,       labels=lowerCAmelCase__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       )     ->   Any:
         A_     :				Optional[Any]			      =	self.num_choices
         A_     :				Any			      =	SqueezeBertForMultipleChoice(config=lowerCAmelCase__       )
         model.to(lowerCAmelCase__       )
         model.eval()
         A_     :				Any			      =	input_ids.unsqueeze(1       ).expand(-1      ,       self.num_choices      ,       -1       ).contiguous()
         A_     :				str			      =	input_mask.unsqueeze(1       ).expand(-1      ,       self.num_choices      ,       -1       ).contiguous()
         A_     :				Optional[int]			      =	model(
             lowerCAmelCase__      ,       attention_mask=lowerCAmelCase__      ,       labels=lowerCAmelCase__      ,       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices)       )






      def 		_lowerCamelCase      (		self       )     ->   Dict:
         A_     :				str			      =	self.prepare_config_and_inputs()
         ((A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_)      ,				(A_))     :				str			      =	config_and_inputs
         A_     :				str			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
         return config, inputs_dict






@require_torch
class    _UpperCAmelCase							(		_snake_case			,       _snake_case			,       unittest.TestCase     ):
      a						       =							(
          (
              SqueezeBertModel,
              SqueezeBertForMaskedLM,
              SqueezeBertForMultipleChoice,
              SqueezeBertForQuestionAnswering,
              SqueezeBertForSequenceClassification,
              SqueezeBertForTokenClassification,
          )
          if is_torch_available()
          else None
      )
      a						       =							(
          {
              '''feature-extraction''': SqueezeBertModel,
              '''fill-mask''': SqueezeBertForMaskedLM,
              '''question-answering''': SqueezeBertForQuestionAnswering,
              '''text-classification''': SqueezeBertForSequenceClassification,
              '''token-classification''': SqueezeBertForTokenClassification,
              '''zero-shot''': SqueezeBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      a						       =							False
      a						       =							True
      a						       =							False
      def 		_lowerCamelCase      (		self       )     ->   Optional[Any]:
         A_     :				str			      =	SqueezeBertModelTester(self       )
         A_     :				Optional[int]			      =	ConfigTester(self      ,       config_class=lowerCAmelCase__      ,       dim=37       )
      def 		_lowerCamelCase      (		self       )     ->   Any:
         self.config_tester.run_common_tests()
      def 		_lowerCamelCase      (		self       )     ->   Optional[int]:
         A_     :				str			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_model(*lowerCAmelCase__       )
      def 		_lowerCamelCase      (		self       )     ->   int:
         A_     :				Tuple			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCAmelCase__       )
      def 		_lowerCamelCase      (		self       )     ->   Dict:
         A_     :				Optional[int]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCAmelCase__       )
      def 		_lowerCamelCase      (		self       )     ->   List[str]:
         A_     :				Any			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCAmelCase__       )
      def 		_lowerCamelCase      (		self       )     ->   str:
         A_     :				Optional[int]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCAmelCase__       )
      def 		_lowerCamelCase      (		self       )     ->   List[str]:
         A_     :				List[Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCAmelCase__       )






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






@require_sentencepiece
@require_tokenizers
@require_torch
class    _UpperCAmelCase							(		unittest.TestCase     ):
      @slow
      def 		_lowerCamelCase      (		self       )     ->   Any:
         A_     :				Dict			      =	SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli"""       )

         A_     :				Any			      =	torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]]       )
         A_     :				Any			      =	model(lowerCAmelCase__       )[0]
         A_     :				List[str]			      =	torch.Size((1, 3)       )
         self.assertEqual(output.shape      ,       lowerCAmelCase__       )
         A_     :				Dict			      =	torch.tensor([[0.6401, -0.0349, -0.6041]]       )
         self.assertTrue(torch.allclose(lowerCAmelCase__      ,       lowerCAmelCase__      ,       atol=1E-4       )       )













from ....configuration_utils import PretrainedConfig
from ....utils import logging


_lowerCAmelCase 				=     logging.get_logger(__name__)

# TODO: upload to AWS
_lowerCAmelCase 				=     {
    """yjernite/retribert-base-uncased""": (
        """https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json"""
    ),
}

class    _UpperCAmelCase							(		_lowerCamelCase     ):
      a						       =							'''retribert'''
      def __init__(		self      ,       a__=30522      ,       a__=768      ,       a__=8      ,       a__=12      ,       a__=3072      ,       a__="gelu"      ,       a__=0.1      ,       a__=0.1      ,       a__=512      ,       a__=2      ,       a__=0.02      ,       a__=1E-12      ,       a__=True      ,       a__=128      ,       a__=0      ,       **a__      ,       ):
         super().__init__(pad_token_id=a__      ,       **a__       )

         A_     :				Union[str, Any]			      =	vocab_size
         A_     :				Optional[Any]			      =	hidden_size
         A_     :				Optional[Any]			      =	num_hidden_layers
         A_     :				List[str]			      =	num_attention_heads
         A_     :				Optional[int]			      =	hidden_act
         A_     :				Any			      =	intermediate_size
         A_     :				Tuple			      =	hidden_dropout_prob
         A_     :				Optional[Any]			      =	attention_probs_dropout_prob
         A_     :				Dict			      =	max_position_embeddings
         A_     :				Dict			      =	type_vocab_size
         A_     :				List[str]			      =	initializer_range
         A_     :				Optional[int]			      =	layer_norm_eps
         A_     :				Optional[Any]			      =	share_encoders
         A_     :				Dict			      =	projection_dim






