"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           RemBertConfig,
           RemBertForCausalLM,
           RemBertForMaskedLM,
           RemBertForMultipleChoice,
           RemBertForQuestionAnswering,
           RemBertForSequenceClassification,
           RemBertForTokenClassification,
           RemBertModel,
       )
       from transformers.models.rembert.modeling_rembert import REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST


class 					UpperCAmelCase_     :


      def __init__(       self		,      a		,      a=1_3		,      a=7		,      a=True		,      a=True		,      a=True		,      a=True		,      a=9_9		,      a=3_2		,      a=1_8		,      a=4_3		,      a=5		,      a=4		,      a=3_7		,      a="gelu"		,      a=0.1		,      a=0.1		,      a=5_1_2		,      a=1_6		,      a=2		,      a=0.02		,      a=3		,      a=4		,      a=None		,      )	-> Optional[Any]:
             lowercase__      :		Tuple          =     parent
             lowercase__      :		int          =     batch_size
             lowercase__      :		str          =     seq_length
             lowercase__      :		Dict          =     is_training
             lowercase__      :		List[str]          =     use_input_mask
             lowercase__      :		Tuple          =     use_token_type_ids
             lowercase__      :		Optional[int]          =     use_labels
             lowercase__      :		Tuple          =     vocab_size
             lowercase__      :		Any          =     hidden_size
             lowercase__      :		str          =     input_embedding_size
             lowercase__      :		str          =     output_embedding_size
             lowercase__      :		Tuple          =     num_hidden_layers
             lowercase__      :		Any          =     num_attention_heads
             lowercase__      :		Any          =     intermediate_size
             lowercase__      :		Dict          =     hidden_act
             lowercase__      :		Optional[Any]          =     hidden_dropout_prob
             lowercase__      :		str          =     attention_probs_dropout_prob
             lowercase__      :		Union[str, Any]          =     max_position_embeddings
             lowercase__      :		List[Any]          =     type_vocab_size
             lowercase__      :		Dict          =     type_sequence_label_size
             lowercase__      :		Optional[Any]          =     initializer_range
             lowercase__      :		List[Any]          =     num_labels
             lowercase__      :		Optional[int]          =     num_choices
             lowercase__      :		Union[str, Any]          =     scope


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		List[Any]          =     ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size							)

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

             lowercase__      :		Any          =     None
             if self.use_token_type_ids:
                    lowercase__      :		Optional[Any]          =     ids_tensor([self.batch_size, self.seq_length]		,      self.type_vocab_size							)

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

             lowercase__      :		Dict          =     RemBertConfig(
                 vocab_size=self.vocab_size		,      hidden_size=self.hidden_size		,      input_embedding_size=self.input_embedding_size		,      output_embedding_size=self.output_embedding_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		,      hidden_dropout_prob=self.hidden_dropout_prob		,      attention_probs_dropout_prob=self.attention_probs_dropout_prob		,      max_position_embeddings=self.max_position_embeddings		,      type_vocab_size=self.type_vocab_size		,      is_decoder=a		,      initializer_range=self.initializer_range		,      )

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


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             (
                 (
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				
             )      :		str          =     self.prepare_config_and_inputs()

             lowercase__      :		Optional[Any]          =     True
             lowercase__      :		Union[str, Any]          =     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
             lowercase__      :		List[str]          =     ids_tensor([self.batch_size, self.seq_length]		,      vocab_size=2							)

             return (
                 config,
                 input_ids,
                 token_type_ids,
                 input_mask,
                 sequence_labels,
                 token_labels,
                 choice_labels,
                 encoder_hidden_states,
                 encoder_attention_mask,
             )


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> Optional[int]:
             lowercase__      :		Optional[Any]          =     RemBertModel(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Union[str, Any]          =     model(a		,      attention_mask=a		,      token_type_ids=a							)
             lowercase__      :		Tuple          =     model(a		,      token_type_ids=a							)
             lowercase__      :		List[str]          =     model(a							)
             self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      )	-> str:
             lowercase__      :		str          =     True
             lowercase__      :		Tuple          =     RemBertModel(a							)
             model.to(a							)
             model.eval()
             lowercase__      :		int          =     model(
                 a		,      attention_mask=a		,      token_type_ids=a		,      encoder_hidden_states=a		,      encoder_attention_mask=a		,      )
             lowercase__      :		List[str]          =     model(
                 a		,      attention_mask=a		,      token_type_ids=a		,      encoder_hidden_states=a		,      )
             lowercase__      :		str          =     model(a		,      attention_mask=a		,      token_type_ids=a							)
             self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      )	-> Dict:
             lowercase__      :		Tuple          =     RemBertForCausalLM(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Optional[Any]          =     model(a		,      attention_mask=a		,      token_type_ids=a		,      labels=a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> List[Any]:
             lowercase__      :		Dict          =     RemBertForMaskedLM(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Tuple          =     model(a		,      attention_mask=a		,      token_type_ids=a		,      labels=a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      )	-> Optional[int]:
             lowercase__      :		Optional[int]          =     True
             lowercase__      :		Optional[Any]          =     True
             lowercase__      :		Any          =     RemBertForCausalLM(config=a							)
             model.to(a							)
             model.eval()

             # first forward pass
             lowercase__      :		List[str]          =     model(
                 a		,      attention_mask=a		,      encoder_hidden_states=a		,      encoder_attention_mask=a		,      use_cache=a		,      )
             lowercase__      :		int          =     outputs.past_key_values

             # create hypothetical multiple next token and extent to next_input_ids
             lowercase__      :		Tuple          =     ids_tensor((self.batch_size, 3)		,      config.vocab_size							)
             lowercase__      :		Any          =     ids_tensor((self.batch_size, 3)		,      vocab_size=2							)

             # append to next input_ids and
             lowercase__      :		str          =     torch.cat([input_ids, next_tokens]		,      dim=-1							)
             lowercase__      :		int          =     torch.cat([input_mask, next_mask]		,      dim=-1							)

             lowercase__      :		Optional[Any]          =     model(
                 a		,      attention_mask=a		,      encoder_hidden_states=a		,      encoder_attention_mask=a		,      output_hidden_states=a		,      )['hidden_states'][0]
             lowercase__      :		int          =     model(
                 a		,      attention_mask=a		,      encoder_hidden_states=a		,      encoder_attention_mask=a		,      past_key_values=a		,      output_hidden_states=a		,      )['hidden_states'][0]

             # select random slice
             lowercase__      :		Union[str, Any]          =     ids_tensor((1,)		,      output_from_past.shape[-1]							).item()
             lowercase__      :		str          =     output_from_no_past[:, -3:, random_slice_idx].detach()
             lowercase__      :		Any          =     output_from_past[:, :, random_slice_idx].detach()

             self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]							)

             # test that outputs are equal for slice
             self.parent.assertTrue(torch.allclose(a		,      a		,      atol=1e-3							)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> int:
             lowercase__      :		int          =     RemBertForQuestionAnswering(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Optional[int]          =     model(
                 a		,      attention_mask=a		,      token_type_ids=a		,      start_positions=a		,      end_positions=a		,      )
             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		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> Any:
             lowercase__      :		int          =     self.num_labels
             lowercase__      :		Tuple          =     RemBertForSequenceClassification(a							)
             model.to(a							)
             model.eval()
             lowercase__      :		List[Any]          =     model(a		,      attention_mask=a		,      token_type_ids=a		,      labels=a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> List[str]:
             lowercase__      :		Optional[Any]          =     self.num_labels
             lowercase__      :		List[str]          =     RemBertForTokenClassification(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		List[str]          =     model(a		,      attention_mask=a		,      token_type_ids=a		,      labels=a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a		,      a		,      a		,      a							)	-> Union[str, Any]:
             lowercase__      :		Any          =     self.num_choices
             lowercase__      :		List[str]          =     RemBertForMultipleChoice(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		int          =     input_ids.unsqueeze(1							).expand(-1		,      self.num_choices		,      -1							).contiguous()
             lowercase__      :		Union[str, Any]          =     token_type_ids.unsqueeze(1							).expand(-1		,      self.num_choices		,      -1							).contiguous()
             lowercase__      :		Tuple          =     input_mask.unsqueeze(1							).expand(-1		,      self.num_choices		,      -1							).contiguous()
             lowercase__      :		int          =     model(
                 a		,      attention_mask=a		,      token_type_ids=a		,      labels=a		,      )
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_choices)							)


      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__      :		Any          =     self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				
             )      :		Any          =     config_and_inputs
             lowercase__      :		Dict          =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
             return config, inputs_dict





@require_torch
class 					UpperCAmelCase_     (   _a			,							_a			,							unittest.TestCase):
      lowerCamelCase__		:   Dict			  =  (
          (
              RemBertModel,
              RemBertForMaskedLM,
              RemBertForCausalLM,
              RemBertForMultipleChoice,
              RemBertForQuestionAnswering,
              RemBertForSequenceClassification,
              RemBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      lowerCamelCase__		:   int			  =  (RemBertForCausalLM,) if is_torch_available() else ()
      lowerCamelCase__		:   int			  =  (
          {
              "feature-extraction": RemBertModel,
              "fill-mask": RemBertForMaskedLM,
              "question-answering": RemBertForQuestionAnswering,
              "text-classification": RemBertForSequenceClassification,
              "text-generation": RemBertForCausalLM,
              "token-classification": RemBertForTokenClassification,
              "zero-shot": RemBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Dict          =     RemBertModelTester(self							)
             lowercase__      :		int          =     ConfigTester(self		,      config_class=a		,      hidden_size=3_7							)


      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             self.config_tester.run_common_tests()


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             lowercase__      :		List[str]          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__      :		List[str]          =     self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                    lowercase__      :		List[Any]          =     type
                    self.model_tester.create_and_check_model(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             lowercase__      :		Optional[int]          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*a							)


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             lowercase__      :		int          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__      :		Tuple          =     self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_decoder_model_past_large_inputs(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Dict          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Tuple          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Optional[Any]          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             lowercase__      :		int          =     self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_model_as_decoder(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Any:
             # This regression test was failing with PyTorch < 1.3
             (
                 (
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				
             )      :		List[str]          =     self.model_tester.prepare_config_and_inputs_for_decoder()

             lowercase__      :		List[Any]          =     None

             self.model_tester.create_and_check_model_as_decoder(
                 a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      a		,      )


      @slow
      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             for model_name in REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__      :		str          =     RemBertModel.from_pretrained(a							)
                    self.assertIsNotNone(a							)





@require_torch
class 					UpperCAmelCase_     (   unittest.TestCase):


      @slow
      def 		_UpperCAmelCase	(       self							)	-> str:
             # Test exact values at the last hidden layer
             lowercase__      :		Optional[int]          =     RemBertModel.from_pretrained('google/rembert'							)
             lowercase__      :		Optional[int]          =     torch.tensor([[3_1_2, 5_6_4_9_8, 3_1_3, 2_1_2_5, 3_1_3]]							)
             lowercase__      :		Tuple          =     torch.tensor([[0, 0, 0, 1, 1]]							)
             with torch.no_grad():
                    lowercase__      :		List[str]          =     model(a		,      token_type_ids=a		,      output_hidden_states=a							)

             lowercase__      :		Optional[Any]          =     1_1_5_2

             lowercase__      :		Optional[Any]          =     torch.Size((1, 5, hidden_size)							)
             self.assertEqual(output['last_hidden_state'].shape		,      a							)

             lowercase__      :		int          =     torch.tensor(
                 [
                     [
                         [0.0_754, -0.2_022, 0.1_904],
                         [-0.3_354, -0.3_692, -0.4_791],
                         [-0.2_314, -0.6_729, -0.0_749],
                         [-0.0_396, -0.3_105, -0.4_234],
                         [-0.1_571, -0.0_525, 0.5_353],
                     ]
                 ]							)

             # Running on the original tf implementation gives slightly different results here.
             # Not clear why this variations is present
             # TODO: Find reason for discrepancy
             # expected_original_implementation = [[
             #     [0.07630594074726105, -0.20146065950393677, 0.19107051193714142],
             #     [-0.3405614495277405, -0.36971670389175415, -0.4808273911476135],
             #     [-0.22587086260318756, -0.6656315922737122, -0.07844287157058716],
             #     [-0.04145475849509239, -0.3077218234539032, -0.42316967248916626],
             #     [-0.15887849032878876, -0.054529931396245956, 0.5356100797653198]
             # ]]

             self.assertTrue(torch.allclose(output['last_hidden_state'][:, :, :3]		,      a		,      atol=1e-4							)							)


"""simple docstring"""





import torch

from transformers import PreTrainedModel

from .custom_configuration import CustomConfig, NoSuperInitConfig


class 					UpperCAmelCase_     (   _a):
      lowerCamelCase__		:   List[str]			  =  CustomConfig


      def __init__(       self		,      a							)	-> Optional[int]:
             super().__init__(a							)
             lowercase__      :		Any          =     torch.nn.Linear(config.hidden_size		,      config.hidden_size							)


      def 		_UpperCAmelCase	(       self		,      a							)	-> Union[str, Any]:
             return self.linear(a							)


      def 		_UpperCAmelCase	(       self		,      a							)	-> Tuple:
             pass





class 					UpperCAmelCase_     (   _a):
      lowerCamelCase__		:   Optional[int]			  =  NoSuperInitConfig


      def __init__(       self		,      a							)	-> int:
             super().__init__(a							)
             lowercase__      :		List[Any]          =     torch.nn.Linear(config.attribute		,      config.attribute							)


      def 		_UpperCAmelCase	(       self		,      a							)	-> Optional[int]:
             return self.linear(a							)


      def 		_UpperCAmelCase	(       self		,      a							)	-> Optional[Any]:
             pass

