

"""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       _lowerCAmelCase						:







 """simple docstring"""







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


 def       _lowercase				(    self							:    List[str]			):
    __lowercase			=		ids_tensor([self.batch_size, self.seq_length],      self.vocab_size			)

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

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

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

    __lowercase			=		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=UpperCAmelCase__,      initializer_range=self.initializer_range,      )

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


 def       _lowercase				(    self							:    Dict			):
    (
        (
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,
    )			=		self.prepare_config_and_inputs()

    __lowercase			=		True
    __lowercase			=		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
    __lowercase			=		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       _lowercase				(    self							:    str,      UpperCAmelCase__							:    Any,      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    List[str],      UpperCAmelCase__							:    Tuple,      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Tuple,      UpperCAmelCase__							:    Dict			):
    __lowercase			=		RemBertModel(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__			)
    __lowercase			=		model(UpperCAmelCase__,      token_type_ids=UpperCAmelCase__			)
    __lowercase			=		model(UpperCAmelCase__			)
    self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size)			)


 def       _lowercase				(    self							:    List[Any],      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    List[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    List[Any],      UpperCAmelCase__							:    str,      UpperCAmelCase__							:    str,      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    Union[str, Any],      UpperCAmelCase__							:    List[Any],      ):
    __lowercase			=		True
    __lowercase			=		RemBertModel(UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      encoder_hidden_states=UpperCAmelCase__,      encoder_attention_mask=UpperCAmelCase__,      )
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      encoder_hidden_states=UpperCAmelCase__,      )
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__			)
    self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size)			)


 def       _lowercase				(    self							:    Tuple,      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    List[Any],      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    int,      ):
    __lowercase			=		RemBertForCausalLM(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      labels=UpperCAmelCase__			)
    self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.vocab_size)			)


 def       _lowercase				(    self							:    Optional[int],      UpperCAmelCase__							:    str,      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Tuple,      UpperCAmelCase__							:    List[str],      UpperCAmelCase__							:    List[str],      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Dict			):
    __lowercase			=		RemBertForMaskedLM(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      labels=UpperCAmelCase__			)
    self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.vocab_size)			)


 def       _lowercase				(    self							:    List[Any],      UpperCAmelCase__							:    Any,      UpperCAmelCase__							:    List[str],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    str,      UpperCAmelCase__							:    List[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    int,      ):
    __lowercase			=		True
    __lowercase			=		True
    __lowercase			=		RemBertForCausalLM(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()

    # first forward pass
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      encoder_hidden_states=UpperCAmelCase__,      encoder_attention_mask=UpperCAmelCase__,      use_cache=UpperCAmelCase__,      )
    __lowercase			=		outputs.past_key_values

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

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

    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      encoder_hidden_states=UpperCAmelCase__,      encoder_attention_mask=UpperCAmelCase__,      output_hidden_states=UpperCAmelCase__,      )["hidden_states"][0]
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      encoder_hidden_states=UpperCAmelCase__,      encoder_attention_mask=UpperCAmelCase__,      past_key_values=UpperCAmelCase__,      output_hidden_states=UpperCAmelCase__,      )["hidden_states"][0]

    # select random slice
    __lowercase			=		ids_tensor((1,),      output_from_past.shape[-1]			).item()
    __lowercase			=		output_from_no_past[:, -3:, random_slice_idx].detach()
    __lowercase			=		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(UpperCAmelCase__,      UpperCAmelCase__,      atol=1E-3			)			)


 def       _lowercase				(    self							:    List[str],      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Tuple,      UpperCAmelCase__							:    Union[str, Any],      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Union[str, Any]			):
    __lowercase			=		RemBertForQuestionAnswering(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=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       _lowercase				(    self							:    List[Any],      UpperCAmelCase__							:    Any,      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    List[str],      UpperCAmelCase__							:    List[Any],      UpperCAmelCase__							:    str			):
    __lowercase			=		self.num_labels
    __lowercase			=		RemBertForSequenceClassification(UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      labels=UpperCAmelCase__			)
    self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.num_labels)			)


 def       _lowercase				(    self							:    List[str],      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Dict,      UpperCAmelCase__							:    Union[str, Any],      UpperCAmelCase__							:    int,      UpperCAmelCase__							:    Optional[int]			):
    __lowercase			=		self.num_labels
    __lowercase			=		RemBertForTokenClassification(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		model(UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      labels=UpperCAmelCase__			)
    self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.num_labels)			)


 def       _lowercase				(    self							:    Optional[int],      UpperCAmelCase__							:    Any,      UpperCAmelCase__							:    Union[str, Any],      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Union[str, Any],      UpperCAmelCase__							:    Optional[Any],      UpperCAmelCase__							:    Optional[int],      UpperCAmelCase__							:    Union[str, Any]			):
    __lowercase			=		self.num_choices
    __lowercase			=		RemBertForMultipleChoice(config=UpperCAmelCase__			)
    model.to(UpperCAmelCase__			)
    model.eval()
    __lowercase			=		input_ids.unsqueeze(1			).expand(-1,      self.num_choices,      -1			).contiguous()
    __lowercase			=		token_type_ids.unsqueeze(1			).expand(-1,      self.num_choices,      -1			).contiguous()
    __lowercase			=		input_mask.unsqueeze(1			).expand(-1,      self.num_choices,      -1			).contiguous()
    __lowercase			=		model(
        UpperCAmelCase__,      attention_mask=UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      labels=UpperCAmelCase__,      )
    self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.num_choices)			)



 def       _lowercase				(    self							:    List[Any]			):
    __lowercase			=		self.prepare_config_and_inputs()
    (
        (
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,
    )			=		config_and_inputs
    __lowercase			=		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
    return config, inputs_dict







@require_torch
class       _lowerCAmelCase						(			lowercase  ,lowercase  ,unittest.TestCase     ):







 """simple docstring"""







 __UpperCAmelCase			:      Dict     	=			(
     (
         RemBertModel,
         RemBertForMaskedLM,
         RemBertForCausalLM,
         RemBertForMultipleChoice,
         RemBertForQuestionAnswering,
         RemBertForSequenceClassification,
         RemBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __UpperCAmelCase			:      Tuple     	=			(RemBertForCausalLM,) if is_torch_available() else ()
 __UpperCAmelCase			:      Any     	=			(
     {
         "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       _lowercase				(    self							:    Union[str, Any]			):
    __lowercase			=		RemBertModelTester(self			)
    __lowercase			=		ConfigTester(self,      config_class=UpperCAmelCase__,      hidden_size=3_7			)


 def       _lowercase				(    self							:    Any			):
    self.config_tester.run_common_tests()


 def       _lowercase				(    self							:    str			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Union[str, Any]			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       __lowercase			=		type
       self.model_tester.create_and_check_model(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    List[str]			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Optional[int]			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Union[str, Any]			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_decoder_model_past_large_inputs(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    List[str]			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Any			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Dict			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    int			):
    __lowercase			=		self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_model_as_decoder(*UpperCAmelCase__			)


 def       _lowercase				(    self							:    Tuple			):
    # This regression test was failing with PyTorch < 1.3
    (
        (
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,(
        __lowercase
    )	,
    )			=		self.model_tester.prepare_config_and_inputs_for_decoder()

    __lowercase			=		None

    self.model_tester.create_and_check_model_as_decoder(
        UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      UpperCAmelCase__,      )



 @slow
 def       _lowercase				(    self							:    List[Any]			):
    for model_name in REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowercase			=		RemBertModel.from_pretrained(UpperCAmelCase__			)
       self.assertIsNotNone(UpperCAmelCase__			)







@require_torch
class       _lowerCAmelCase						(			unittest.TestCase     ):







 """simple docstring"""







 @slow
 def       _lowercase				(    self							:    List[Any]			):
    # Test exact values at the last hidden layer
    __lowercase			=		RemBertModel.from_pretrained("google/rembert"			)
    __lowercase			=		torch.tensor([[3_1_2, 5_6_4_9_8, 3_1_3, 2_1_2_5, 3_1_3]]			)
    __lowercase			=		torch.tensor([[0, 0, 0, 1, 1]]			)
    with torch.no_grad():
       __lowercase			=		model(UpperCAmelCase__,      token_type_ids=UpperCAmelCase__,      output_hidden_states=UpperCAmelCase__			)

    __lowercase			=		1_1_5_2

    __lowercase			=		torch.Size((1, 5, hidden_size)			)
    self.assertEqual(output["last_hidden_state"].shape,      UpperCAmelCase__			)

    __lowercase			=		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],      UpperCAmelCase__,      atol=1E-4			)			)







"""simple docstring"""





def 						_A						(						UpperCamelCase_  : float,    UpperCamelCase_  : float)			->		float:
   '''simple docstring'''


   __lowercase			=		float()
   try:
      __lowercase			=		mass * acceleration
   except Exception:
      return -0.0
   return force


if __name__ == "__main__":
   import doctest

   # run doctest
   doctest.testmod()

   # demo
   _a				   =							12.5
   _a				   =							10
   _a				   =							newtons_second_law_of_motion(mass, acceleration)
   print('The force is ', force, 'N')




