import unittest
from unittest.util import safe_repr

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

from ...generation.test_utils import GenerationTesterMixin
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 (
	    RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
	    RwkvForCausalLM,
	    RwkvModel,
	)
	from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
	lowerCamelCase_				:		Optional[int]       						=       False


class 						a__							:



			def __init__(						self      ,						UpperCAmelCase      ,						UpperCAmelCase=1_4      ,						UpperCAmelCase=7      ,						UpperCAmelCase=True      ,						UpperCAmelCase=False      ,						UpperCAmelCase=True      ,						UpperCAmelCase=True      ,						UpperCAmelCase=True      ,						UpperCAmelCase=9_9      ,						UpperCAmelCase=3_2      ,						UpperCAmelCase=5      ,						UpperCAmelCase=3_7      ,						UpperCAmelCase="gelu"      ,						UpperCAmelCase=0.1      ,						UpperCAmelCase=0.1      ,						UpperCAmelCase=5_1_2      ,						UpperCAmelCase=1_6      ,						UpperCAmelCase=2      ,						UpperCAmelCase=3      ,						UpperCAmelCase=4      ,						UpperCAmelCase=None      ,						) ->    Optional[Any]:
								__a							=   parent
								__a							=   batch_size
								__a							=   seq_length
								__a							=   is_training
								__a							=   use_token_type_ids
								__a							=   use_input_mask
								__a							=   use_labels
								__a							=   use_mc_token_ids
								__a							=   vocab_size
								__a							=   hidden_size
								__a							=   num_hidden_layers
								__a							=   intermediate_size
								__a							=   hidden_act
								__a							=   hidden_dropout_prob
								__a							=   attention_probs_dropout_prob
								__a							=   max_position_embeddings
								__a							=   type_vocab_size
								__a							=   type_sequence_label_size
								__a							=   num_labels
								__a							=   num_choices
								__a							=   scope
								__a							=   vocab_size - 1
								__a							=   vocab_size - 1
								__a							=   vocab_size - 1



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    int:
								return RwkvConfig.from_pretrained('sgugger/rwkv-4-pile-7b'    )



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase=False      ,						UpperCAmelCase=False      ,						UpperCAmelCase=False    ) ->    Tuple:
								__a							=   ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size    )

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

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

								__a							=   None
								if self.use_mc_token_ids:
													__a							=   ids_tensor([self.batch_size, self.num_choices]      ,						self.seq_length    )

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

								__a							=   self.get_config(
								    gradient_checkpointing=UpperCAmelCase      ,						scale_attn_by_inverse_layer_idx=UpperCAmelCase      ,						reorder_and_upcast_attn=UpperCAmelCase      ,						)

								return (
								    config,
								    input_ids,
								    input_mask,
								    None,
								    token_type_ids,
								    mc_token_ids,
								    sequence_labels,
								    token_labels,
								    choice_labels,
								)



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase=False      ,						UpperCAmelCase=False      ,						UpperCAmelCase=False    ) ->    Optional[Any]:
								return RwkvConfig(
								    vocab_size=self.vocab_size      ,						hidden_size=self.hidden_size      ,						num_hidden_layers=self.num_hidden_layers      ,						intermediate_size=self.intermediate_size      ,						activation_function=self.hidden_act      ,						resid_pdrop=self.hidden_dropout_prob      ,						attn_pdrop=self.attention_probs_dropout_prob      ,						n_positions=self.max_position_embeddings      ,						type_vocab_size=self.type_vocab_size      ,						use_cache=UpperCAmelCase      ,						bos_token_id=self.bos_token_id      ,						eos_token_id=self.eos_token_id      ,						pad_token_id=self.pad_token_id      ,						gradient_checkpointing=UpperCAmelCase      ,						scale_attn_by_inverse_layer_idx=UpperCAmelCase      ,						reorder_and_upcast_attn=UpperCAmelCase      ,						)



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Any:
								__a							=   self.get_config()
								__a							=   3_0_0
								return config



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Dict:
								(
								    (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      
								)							=   self.prepare_config_and_inputs()

								__a							=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]    )
								__a							=   ids_tensor([self.batch_size, self.seq_length]      ,						vocab_size=2    )

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



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						*UpperCAmelCase    ) ->    Union[str, Any]:
								__a							=   True
								__a							=   RwkvModel(config=UpperCAmelCase    )
								model.to(UpperCAmelCase    )
								model.eval()

								__a							=   model(UpperCAmelCase    )

								self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size)    )
								self.parent.assertEqual(len(result.hidden_states    )      ,						config.num_hidden_layers + 1    )



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						*UpperCAmelCase    ) ->    str:
								__a							=   RwkvForCausalLM(UpperCAmelCase    )
								model.to(UpperCAmelCase    )
								model.eval()

								__a							=   model(UpperCAmelCase      ,						labels=UpperCAmelCase    )
								self.parent.assertEqual(result.loss.shape      ,						()    )
								self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.vocab_size)    )



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						*UpperCAmelCase    ) ->    List[Any]:
								__a							=   RwkvModel(config=UpperCAmelCase    )
								model.to(UpperCAmelCase    )
								model.eval()

								__a							=   model(UpperCAmelCase    )
								__a							=   outputs.last_hidden_state

								__a							=   model(input_ids[:, :2]    )
								__a							=   outputs.last_hidden_state

								# Using the state computed on the first inputs, we will get the same output
								__a							=   model(input_ids[:, 2:]      ,						state=outputs.state    )
								__a							=   outputs.last_hidden_state

								self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]      ,						dim=1    )      ,						UpperCAmelCase      ,						atol=1e-5    )    )



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						*UpperCAmelCase      ,						UpperCAmelCase=False    ) ->    Optional[Any]:
								__a							=   RwkvForCausalLM(UpperCAmelCase    )
								model.to(UpperCAmelCase    )
								if gradient_checkpointing:
													model.gradient_checkpointing_enable()

								__a							=   model(UpperCAmelCase      ,						labels=UpperCAmelCase    )
								self.parent.assertEqual(result.loss.shape      ,						()    )
								self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.vocab_size)    )
								result.loss.backward()







			def __SCREAMING_SNAKE_CASE							(						self    ) ->    List[str]:
								__a							=   self.prepare_config_and_inputs()

								(
								    (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      (
								    __a
								)					,      
								)							=   config_and_inputs

								__a							=   {'input_ids': input_ids}

								return config, inputs_dict






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0      ,						reason='See https://github.com/huggingface/transformers/pull/24204'		)
@require_torch
class 						a__							(   __snake_case      ,						__snake_case      ,						__snake_case      ,						unittest.TestCase		):
			A__				:   List[str]								=			(RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
			A__				:   Any								=			(
			    {'feature-extraction': RwkvModel, 'text-generation': RwkvForCausalLM} if is_torch_available() else {}
			)
			# all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
			A__				:   int								=			False
			A__				:   Any								=			False
			A__				:   Tuple								=			False
			A__				:   Tuple								=			False
			A__				:   Optional[Any]								=			False  # Rwkv does not support head masking



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Union[str, Any]:
								__a							=   RwkvModelTester(self    )
								__a							=   ConfigTester(
								    self      ,						config_class=UpperCAmelCase      ,						n_embd=3_7      ,						common_properties=['hidden_size', 'num_hidden_layers']    )



			def __SCREAMING_SNAKE_CASE							(						self      ,						UpperCAmelCase      ,						UpperCAmelCase      ,						UpperCAmelCase=None    ) ->    Any:
								if isinstance(UpperCAmelCase      ,						torch.Tensor    ):
													__a					,      __a							=   member.max().item(), member.min().item()
								elif isinstance(UpperCAmelCase      ,						UpperCAmelCase    ) or isinstance(UpperCAmelCase      ,						UpperCAmelCase    ):
													__a					,      __a							=   max(UpperCAmelCase    ), min(UpperCAmelCase    )

								if not isinstance(UpperCAmelCase      ,						UpperCAmelCase    ):
													raise TypeError('container should be a list or tuple'    )
								elif len(UpperCAmelCase    ) != 2:
													raise ValueError('container should have 2 elements'    )

								__a					,      __a							=   container

								__a							=   (min_value >= expected_min) and (max_value <= expected_max)

								if not is_inside_interval:
													__a							=   '%s not found in %s' % (safe_repr(UpperCAmelCase    ), safe_repr(UpperCAmelCase    ))
													self.fail(self._formatMessage(UpperCAmelCase      ,						UpperCAmelCase    )    )



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Dict:
								self.config_tester.run_common_tests()



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    List[Any]:
								__a							=   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_rwkv_model(*UpperCAmelCase    )



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Union[str, Any]:
								__a							=   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_causl_lm(*UpperCAmelCase    )



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Union[str, Any]:
								__a							=   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_state_equivalency(*UpperCAmelCase    )



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Optional[int]:
								__a					,      __a							=   self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__a							=   model_class(config=UpperCAmelCase    )
													for name, param in model.named_parameters():
																		if "time_decay" in name:
																							if param.requires_grad:
																												self.assertTrue(param.data.max().item() == 3.0    )
																												self.assertTrue(param.data.min().item() == -5.0    )
																		elif "time_first" in name:
																							if param.requires_grad:
																												# check if it's a ones like
																												self.assertTrue(torch.allclose(param.data      ,						torch.ones_like(param.data    )      ,						atol=1e-5      ,						rtol=1e-5    )    )
																		elif any(x in name for x in ['time_mix_key', 'time_mix_receptance']    ):
																							if param.requires_grad:
																												self.assertInterval(
																												    param.data      ,						[0.0, 1.0]      ,						msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''      ,						)
																		elif "time_mix_value" in name:
																							if param.requires_grad:
																												self.assertInterval(
																												    param.data      ,						[0.0, 1.3]      ,						msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''      ,						)



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    str:
								__a					,      __a							=   self.model_tester.prepare_config_and_inputs_for_common()
								__a							=   True

								__a							=   getattr(self.model_tester      ,						'seq_length'      ,						UpperCAmelCase    )

								for model_class in self.all_model_classes:
													__a							=   True
													__a							=   False
													__a							=   True
													__a							=   model_class(UpperCAmelCase    )
													model.to(UpperCAmelCase    )
													model.eval()

													__a							=   self._prepare_for_class(UpperCAmelCase      ,						UpperCAmelCase    )
													__a							=   inputs['input_ids'].shape[0]
													with torch.no_grad():
																		__a							=   model(**UpperCAmelCase    )
													__a							=   outputs.attentions
													self.assertEqual(len(UpperCAmelCase    )      ,						self.model_tester.num_hidden_layers    )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													__a							=   True
													__a							=   model_class(UpperCAmelCase    )
													model.to(UpperCAmelCase    )
													model.eval()

													__a							=   self._prepare_for_class(UpperCAmelCase      ,						UpperCAmelCase    )
													__a							=   inputs['input_ids'].shape[0]
													with torch.no_grad():
																		__a							=   model(**UpperCAmelCase    )
													__a							=   outputs.attentions
													self.assertEqual(len(UpperCAmelCase    )      ,						self.model_tester.num_hidden_layers    )

													self.assertListEqual(
													    list(attentions[0].shape[-3:]    )      ,						[batch_size, seq_len, config.hidden_size]      ,						)
													__a							=   len(UpperCAmelCase    )

													# Check attention is always last and order is fine
													__a							=   True
													__a							=   True
													__a							=   model_class(UpperCAmelCase    )
													model.to(UpperCAmelCase    )
													model.eval()

													__a							=   self._prepare_for_class(UpperCAmelCase      ,						UpperCAmelCase    )
													__a							=   inputs['input_ids'].shape[0]
													with torch.no_grad():
																		__a							=   model(**UpperCAmelCase    )

													__a							=   1
													self.assertEqual(out_len + added_hidden_states      ,						len(UpperCAmelCase    )    )

													__a							=   outputs.attentions

													self.assertEqual(len(UpperCAmelCase    )      ,						self.model_tester.num_hidden_layers    )
													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]    )      ,						[batch_size, seq_len, config.hidden_size]      ,						)







			@slow
			def __SCREAMING_SNAKE_CASE							(						self    ) ->    List[Any]:
								for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a							=   RwkvModel.from_pretrained(UpperCAmelCase    )
													self.assertIsNotNone(UpperCAmelCase    )






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0      ,						reason='See https://github.com/huggingface/transformers/pull/24204'		)
@slow
class 						a__							(   unittest.TestCase		):



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Any:
								__a							=   'RWKV/rwkv-4-169m-pile'
								__a							=   AutoTokenizer.from_pretrained(self.model_id    )



			def __SCREAMING_SNAKE_CASE							(						self    ) ->    Optional[int]:
								__a							=   'Hello my name is Jasmine and I am a newbie to the'
								__a							=   RwkvForCausalLM.from_pretrained(self.model_id    ).to(UpperCAmelCase    )

								__a							=   self.tokenizer('Hello my name is'      ,						return_tensors='pt'    ).input_ids.to(UpperCAmelCase    )
								__a							=   model.generate(UpperCAmelCase      ,						max_new_tokens=1_0    )
								__a							=   self.tokenizer.decode(output[0].tolist()    )

								self.assertEqual(UpperCAmelCase      ,						UpperCAmelCase    )







			def __SCREAMING_SNAKE_CASE							(						self    ) ->    List[str]:
								__a							=   'Hello my name is Jasmine and I am a newbie to the'

								__a							=   self.tokenizer('Hello my name is'      ,						return_tensors='pt'    ).input_ids.to(UpperCAmelCase    )
								__a							=   RwkvForCausalLM.from_pretrained(self.model_id      ,						torch_dtype=torch.bfloataa    ).to(UpperCAmelCase    )

								__a							=   model.generate(UpperCAmelCase      ,						max_new_tokens=1_0    )
								__a							=   self.tokenizer.decode(output[0].tolist()    )

								self.assertEqual(UpperCAmelCase      ,						UpperCAmelCase    )









"""simple docstring"""
import unittest

from transformers import ErnieMConfig, 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, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    ErnieMForInformationExtraction,
			    ErnieMForMultipleChoice,
			    ErnieMForQuestionAnswering,
			    ErnieMForSequenceClassification,
			    ErnieMForTokenClassification,
			    ErnieMModel,
			)
			from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(			self ,				lowerCamelCase__ ,				lowerCamelCase__=1_3 ,				lowerCamelCase__=7 ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=9_9 ,				lowerCamelCase__=3_2 ,				lowerCamelCase__=5 ,				lowerCamelCase__=4 ,				lowerCamelCase__=3_7 ,				lowerCamelCase__="gelu" ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=5_1_2 ,				lowerCamelCase__=1_6 ,				lowerCamelCase__=2 ,				lowerCamelCase__=0.0_2 ,				lowerCamelCase__=3 ,				lowerCamelCase__=4 ,				lowerCamelCase__=None ,				):
									_lowerCamelCase					     = parent
									_lowerCamelCase					     = batch_size
									_lowerCamelCase					     = seq_length
									_lowerCamelCase					     = is_training
									_lowerCamelCase					     = use_input_mask
									_lowerCamelCase					     = use_labels
									_lowerCamelCase					     = vocab_size
									_lowerCamelCase					     = hidden_size
									_lowerCamelCase					     = num_hidden_layers
									_lowerCamelCase					     = num_attention_heads
									_lowerCamelCase					     = intermediate_size
									_lowerCamelCase					     = hidden_act
									_lowerCamelCase					     = hidden_dropout_prob
									_lowerCamelCase					     = attention_probs_dropout_prob
									_lowerCamelCase					     = max_position_embeddings
									_lowerCamelCase					     = type_vocab_size
									_lowerCamelCase					     = type_sequence_label_size
									_lowerCamelCase					     = initializer_range
									_lowerCamelCase					     = num_labels
									_lowerCamelCase					     = num_choices
									_lowerCamelCase					     = scope

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

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

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

									_lowerCamelCase					     = self.get_config()

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

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

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

									return config, input_ids, input_mask

					def 	snake_case__					(			self      ):
									return ErnieMConfig(
									    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 ,				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 ,				initializer_range=self.initializer_range ,				)

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMModel(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				return_dict=lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMForQuestionAnswering(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = 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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMForInformationExtraction(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = torch.ones_like(lowerCamelCase__ ,				dtype=torch.floataa      )
									_lowerCamelCase					     = 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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = ErnieMForSequenceClassification(lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = ErnieMForTokenClassification(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									input_ids.to(lowerCamelCase__      )
									input_mask.to(lowerCamelCase__      )
									token_labels.to(lowerCamelCase__      )

									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__      )

									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_choices
									_lowerCamelCase					     = ErnieMForMultipleChoice(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = input_ids.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = input_mask.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__ ,				)
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices)      )





					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.prepare_config_and_inputs()
									(
									    (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   
									)					     = config_and_inputs
									_lowerCamelCase					     = {'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict


@require_torch
class 							lowerCamelCase_(      A__, A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				Optional[Any]        =       (
					    (
					        ErnieMModel,
					        ErnieMForMultipleChoice,
					        ErnieMForQuestionAnswering,
					        ErnieMForSequenceClassification,
					        ErnieMForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowercase__      :				List[Any]        =       ()
					lowercase__      :				Union[str, Any]        =       (
					    {
					        'feature-extraction': ErnieMModel,
					        'question-answering': ErnieMForQuestionAnswering,
					        'text-classification': ErnieMForSequenceClassification,
					        'token-classification': ErnieMForTokenClassification,
					        'zero-shot': ErnieMForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					lowercase__      :				Optional[int]        =       False

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									if pipeline_test_casse_name == "QAPipelineTests":
													return True

									return False

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ErnieMModelTester(self      )
									_lowerCamelCase					     = ConfigTester(self ,				config_class=lowerCamelCase__ ,				hidden_size=3_7      )

					def 	snake_case__					(			self      ):
									self.config_tester.run_common_tests()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													_lowerCamelCase					     = type
													self.model_tester.create_and_check_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__      )





					@slow
					def 	snake_case__					(			self      ):
									for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowerCamelCase					     = ErnieMModel.from_pretrained(lowerCamelCase__      )
													self.assertIsNotNone(lowerCamelCase__      )


@require_torch
class 							lowerCamelCase_(      unittest.TestCase			):





					'''simple docstring'''



					@slow
					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''      )
									model.eval()
									_lowerCamelCase					     = torch.tensor([[0, 1, 2, 3, 4, 5]]      )
									_lowerCamelCase					     = model(lowerCamelCase__      )[0]

									# TODO Replace vocab size
									_lowerCamelCase					     = 7_6_8

									_lowerCamelCase					     = torch.Size((1, 6, hidden_size)      )
									self.assertEqual(output.shape ,				lowerCamelCase__      )

									_lowerCamelCase					     = torch.tensor(
									    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]      )

									self.assertTrue(torch.allclose(output[:, :3, :3] ,				lowerCamelCase__ ,				atol=1e-3      )      )
