



"""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							:					int    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict=13    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=7    ,					_SCREAMING_SNAKE_CASE							:					Any=True    ,					_SCREAMING_SNAKE_CASE							:					int=True    ,					_SCREAMING_SNAKE_CASE							:					int=True    ,					_SCREAMING_SNAKE_CASE							:					int=True    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=99    ,					_SCREAMING_SNAKE_CASE							:					Any=32    ,					_SCREAMING_SNAKE_CASE							:					int=18    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=43    ,					_SCREAMING_SNAKE_CASE							:					int=5    ,					_SCREAMING_SNAKE_CASE							:					str=4    ,					_SCREAMING_SNAKE_CASE							:					Tuple=37    ,					_SCREAMING_SNAKE_CASE							:					Any="gelu"    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=0.1    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=0.1    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=512    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=16    ,					_SCREAMING_SNAKE_CASE							:					Any=2    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=0.0_2    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]=3    ,					_SCREAMING_SNAKE_CASE							:					Tuple=4    ,					_SCREAMING_SNAKE_CASE							:					int=None    ,					)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    parent
					SCREAMING_SNAKE_CASE	:       int 							=    batch_size
					SCREAMING_SNAKE_CASE	:       List[str] 							=    seq_length
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    is_training
					SCREAMING_SNAKE_CASE	:       int 							=    use_input_mask
					SCREAMING_SNAKE_CASE	:       List[str] 							=    use_token_type_ids
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    use_labels
					SCREAMING_SNAKE_CASE	:       Tuple 							=    vocab_size
					SCREAMING_SNAKE_CASE	:       int 							=    hidden_size
					SCREAMING_SNAKE_CASE	:       Tuple 							=    input_embedding_size
					SCREAMING_SNAKE_CASE	:       Any 							=    output_embedding_size
					SCREAMING_SNAKE_CASE	:       Tuple 							=    num_hidden_layers
					SCREAMING_SNAKE_CASE	:       Dict 							=    num_attention_heads
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    intermediate_size
					SCREAMING_SNAKE_CASE	:       int 							=    hidden_act
					SCREAMING_SNAKE_CASE	:       str 							=    hidden_dropout_prob
					SCREAMING_SNAKE_CASE	:       str 							=    attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE	:       Tuple 							=    max_position_embeddings
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    type_vocab_size
					SCREAMING_SNAKE_CASE	:       List[str] 							=    type_sequence_label_size
					SCREAMING_SNAKE_CASE	:       int 							=    initializer_range
					SCREAMING_SNAKE_CASE	:       List[str] 							=    num_labels
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    num_choices
					SCREAMING_SNAKE_CASE	:       Tuple 							=    scope





	def  _lowerCAmelCase    (			self							:					str						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

					SCREAMING_SNAKE_CASE	:       int 							=    None
					if self.use_input_mask:
									SCREAMING_SNAKE_CASE	:       str 							=    random_attention_mask([self.batch_size, self.seq_length]						)

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

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

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    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=_SCREAMING_SNAKE_CASE    ,					initializer_range=self.initializer_range    ,					)

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





	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Optional[Any]:





					"""simple docstring"""


					(
					    SCREAMING_SNAKE_CASE			
					)	:       int 							=    self.prepare_config_and_inputs()

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    True
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    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  _lowerCAmelCase    (			self							:					str    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    RemBertModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Any 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       str 							=    model(_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)						)





	def  _lowerCAmelCase    (			self							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    True
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    RemBertModel(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					encoder_hidden_states=_SCREAMING_SNAKE_CASE    ,					encoder_attention_mask=_SCREAMING_SNAKE_CASE    ,					)
					SCREAMING_SNAKE_CASE	:       int 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					encoder_hidden_states=_SCREAMING_SNAKE_CASE    ,					)
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.last_hidden_state.shape    ,					(self.batch_size, self.seq_length, self.hidden_size)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    RemBertForCausalLM(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)						)





	def  _lowerCAmelCase    (			self							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    RemBertForMaskedLM(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)						)





	def  _lowerCAmelCase    (			self							:					int    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    True
					SCREAMING_SNAKE_CASE	:       Tuple 							=    True
					SCREAMING_SNAKE_CASE	:       List[str] 							=    RemBertForCausalLM(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()

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

					# create hypothetical multiple next token and extent to next_input_ids
					SCREAMING_SNAKE_CASE	:       List[str] 							=    ids_tensor((self.batch_size, 3)    ,					config.vocab_size						)
					SCREAMING_SNAKE_CASE	:       Dict 							=    ids_tensor((self.batch_size, 3)    ,					vocab_size=2						)

					# append to next input_ids and
					SCREAMING_SNAKE_CASE	:       Dict 							=    torch.cat([input_ids, next_tokens]    ,					dim=-1						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    torch.cat([input_mask, next_mask]    ,					dim=-1						)

					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					encoder_hidden_states=_SCREAMING_SNAKE_CASE    ,					encoder_attention_mask=_SCREAMING_SNAKE_CASE    ,					output_hidden_states=_SCREAMING_SNAKE_CASE    ,					)['hidden_states'][0]
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					encoder_hidden_states=_SCREAMING_SNAKE_CASE    ,					encoder_attention_mask=_SCREAMING_SNAKE_CASE    ,					past_key_values=_SCREAMING_SNAKE_CASE    ,					output_hidden_states=_SCREAMING_SNAKE_CASE    ,					)['hidden_states'][0]

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





	def  _lowerCAmelCase    (			self							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    RemBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					start_positions=_SCREAMING_SNAKE_CASE    ,					end_positions=_SCREAMING_SNAKE_CASE    ,					)
					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							:					int    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					List[Any]						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    RemBertForSequenceClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Any 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					List[str]						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    self.num_labels
					SCREAMING_SNAKE_CASE	:       Tuple 							=    RemBertForTokenClassification(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    model(_SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.num_labels)						)





	def  _lowerCAmelCase    (			self							:					str    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Any    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Tuple						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.num_choices
					SCREAMING_SNAKE_CASE	:       Dict 							=    RemBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Any 							=    input_ids.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    token_type_ids.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    input_mask.unsqueeze(1						).expand(-1    ,					self.num_choices    ,					-1						).contiguous()
					SCREAMING_SNAKE_CASE	:       str 							=    model(
					    _SCREAMING_SNAKE_CASE    ,					attention_mask=_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE    ,					)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_choices)						)







	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    self.prepare_config_and_inputs()
					(
					    SCREAMING_SNAKE_CASE			
					)	:       int 							=    config_and_inputs
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
					return config, inputs_dict




@require_torch
class 						lowerCAmelCase__	(  snake_case__       ,       snake_case__       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		int     	=   (
	    (
	        RemBertModel,
	        RemBertForMaskedLM,
	        RemBertForCausalLM,
	        RemBertForMultipleChoice,
	        RemBertForQuestionAnswering,
	        RemBertForSequenceClassification,
	        RemBertForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_SCREAMING_SNAKE_CASE  :		Tuple     	=   (RemBertForCausalLM,) if is_torch_available() else ()
	_SCREAMING_SNAKE_CASE  :		List[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  _lowerCAmelCase    (			self							:					Dict						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    RemBertModelTester(self						)
					SCREAMING_SNAKE_CASE	:       str 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE    ,					hidden_size=37						)





	def  _lowerCAmelCase    (			self							:					Optional[int]						)	->  str:





					"""simple docstring"""


					self.config_tester.run_common_tests()





	def  _lowerCAmelCase    (			self							:					List[Any]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Any						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    type
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					int						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_decoder_model_past_large_inputs(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					int						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					int						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_model_as_decoder(*_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  List[Any]:





					"""simple docstring"""


					(
					    SCREAMING_SNAKE_CASE			
					)	:       int 							=    self.model_tester.prepare_config_and_inputs_for_decoder()

					SCREAMING_SNAKE_CASE	:       str 							=    None

					self.model_tester.create_and_check_model_as_decoder(
					    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					)







	@slow
	def  _lowerCAmelCase    (			self							:					Any						)	->  int:





					"""simple docstring"""


					for model_name in REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE	:       int 							=    RemBertModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
									self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)




@require_torch
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	@slow
	def  _lowerCAmelCase    (			self							:					Dict						)	->  Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    RemBertModel.from_pretrained('google/rembert'						)
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    torch.tensor([[312, 56_498, 313, 2_125, 313]]						)
					SCREAMING_SNAKE_CASE	:       Any 							=    torch.tensor([[0, 0, 0, 1, 1]]						)
					with torch.no_grad():
									SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					token_type_ids=_SCREAMING_SNAKE_CASE    ,					output_hidden_states=_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    1_152

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    torch.Size((1, 5, hidden_size)						)
					self.assertEqual(output['last_hidden_state'].shape    ,					_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       Tuple 							=    torch.tensor(
					    [
					        [
					            [0.0_7_5_4, -0.2_0_2_2, 0.1_9_0_4],
					            [-0.3_3_5_4, -0.3_6_9_2, -0.4_7_9_1],
					            [-0.2_3_1_4, -0.6_7_2_9, -0.0_7_4_9],
					            [-0.0_3_9_6, -0.3_1_0_5, -0.4_2_3_4],
					            [-0.1_5_7_1, -0.0_5_2_5, 0.5_3_5_3],
					        ]
					    ]						)

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










"""simple docstring"""

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import ConvNextImageProcessor
class 						lowerCAmelCase__	(  unittest.TestCase   ):


	'''simple docstring'''







	def __init__(			self							:					Tuple    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=7    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=3    ,					_SCREAMING_SNAKE_CASE							:					List[str]=18    ,					_SCREAMING_SNAKE_CASE							:					str=30    ,					_SCREAMING_SNAKE_CASE							:					str=400    ,					_SCREAMING_SNAKE_CASE							:					Any=True    ,					_SCREAMING_SNAKE_CASE							:					int=None    ,					_SCREAMING_SNAKE_CASE							:					Dict=0.8_7_5    ,					_SCREAMING_SNAKE_CASE							:					List[str]=True    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=[0.5, 0.5, 0.5]    ,					_SCREAMING_SNAKE_CASE							:					str=[0.5, 0.5, 0.5]    ,					)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       int 							=    size if size is not None else {'shortest_edge': 20}
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    parent
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    batch_size
					SCREAMING_SNAKE_CASE	:       Any 							=    num_channels
					SCREAMING_SNAKE_CASE	:       Any 							=    image_size
					SCREAMING_SNAKE_CASE	:       str 							=    min_resolution
					SCREAMING_SNAKE_CASE	:       Dict 							=    max_resolution
					SCREAMING_SNAKE_CASE	:       Dict 							=    do_resize
					SCREAMING_SNAKE_CASE	:       List[str] 							=    size
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    crop_pct
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    do_normalize
					SCREAMING_SNAKE_CASE	:       Dict 							=    image_mean
					SCREAMING_SNAKE_CASE	:       str 							=    image_std







	def  _lowerCAmelCase    (			self							:					Dict						)	->  Any:





					"""simple docstring"""


					return {
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_normalize": self.do_normalize,
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "crop_pct": self.crop_pct,
					}




@require_torch
@require_vision
class 						lowerCAmelCase__	(  _lowerCamelCase       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Optional[Any]     	=   ConvNextImageProcessor if is_vision_available() else None





	def  _lowerCAmelCase    (			self							:					Any						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ConvNextImageProcessingTester(self						)





	@property
	def  _lowerCAmelCase    (			self							:					Optional[int]						)	->  Dict:





					"""simple docstring"""


					return self.image_processor_tester.prepare_image_processor_dict()





	def  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.image_processing_class(**self.image_processor_dict						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'do_resize'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'size'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'crop_pct'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'do_normalize'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'image_mean'						)						)
					self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE    ,					'image_std'						)						)





	def  _lowerCAmelCase    (			self							:					Union[str, Any]						)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.image_processing_class.from_dict(self.image_processor_dict						)
					self.assertEqual(image_processor.size    ,					{'shortest_edge': 20}						)

					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.image_processing_class.from_dict(self.image_processor_dict    ,					size=42						)
					self.assertEqual(image_processor.size    ,					{'shortest_edge': 42}						)





	def  _lowerCAmelCase    (			self							:					int						)	->  str:





					"""simple docstring"""


					pass





	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random PIL images
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					Image.Image						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       str 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       str 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)





	def  _lowerCAmelCase    (			self							:					int						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random numpy tensors
					SCREAMING_SNAKE_CASE	:       Tuple 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE    ,					numpify=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					np.ndarray						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       int 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)







	def  _lowerCAmelCase    (			self							:					Optional[Any]						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    self.image_processing_class(**self.image_processor_dict						)
					# create random PyTorch tensors
					SCREAMING_SNAKE_CASE	:       Tuple 							=    prepare_image_inputs(self.image_processor_tester    ,					equal_resolution=_SCREAMING_SNAKE_CASE    ,					torchify=_SCREAMING_SNAKE_CASE						)
					for image in image_inputs:
									self.assertIsInstance(_SCREAMING_SNAKE_CASE    ,					torch.Tensor						)

					# Test not batched input
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    image_processing(image_inputs[0]    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)

					# Test batched
					SCREAMING_SNAKE_CASE	:       Any 							=    image_processing(_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape    ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.size['shortest_edge'],
					        self.image_processor_tester.size['shortest_edge'],
					    )    ,					)





