


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   A__							:




						def __init__(    self   : Union[str, Any]			,      _a   : Tuple			,      _a   : str=13			,      _a   : Optional[Any]=7			,      _a   : str=True			,      _a   : Union[str, Any]=True			,      _a   : List[str]=True			,      _a   : Optional[int]=99			,      _a   : List[str]=32			,      _a   : int=5			,      _a   : Any=4			,      _a   : Union[str, Any]=37			,      _a   : List[Any]="gelu"			,      _a   : Dict=0.1			,      _a   : List[Any]=0.1			,      _a   : Union[str, Any]=512			,      _a   : Optional[int]=16			,      _a   : int=2			,      _a   : Union[str, Any]=0.02			,      _a   : int=3			,      _a   : Optional[Any]=4			,      _a   : Union[str, Any]=None			,      )							->       Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=parent
										_SCREAMING_SNAKE_CASE     					=batch_size
										_SCREAMING_SNAKE_CASE     					=seq_length
										_SCREAMING_SNAKE_CASE     					=is_training
										_SCREAMING_SNAKE_CASE     					=use_input_mask
										_SCREAMING_SNAKE_CASE     					=use_labels
										_SCREAMING_SNAKE_CASE     					=vocab_size
										_SCREAMING_SNAKE_CASE     					=hidden_size
										_SCREAMING_SNAKE_CASE     					=num_hidden_layers
										_SCREAMING_SNAKE_CASE     					=num_attention_heads
										_SCREAMING_SNAKE_CASE     					=intermediate_size
										_SCREAMING_SNAKE_CASE     					=hidden_act
										_SCREAMING_SNAKE_CASE     					=hidden_dropout_prob
										_SCREAMING_SNAKE_CASE     					=attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE     					=max_position_embeddings
										_SCREAMING_SNAKE_CASE     					=type_vocab_size
										_SCREAMING_SNAKE_CASE     					=type_sequence_label_size
										_SCREAMING_SNAKE_CASE     					=initializer_range
										_SCREAMING_SNAKE_CASE     					=num_labels
										_SCREAMING_SNAKE_CASE     					=num_choices
										_SCREAMING_SNAKE_CASE     					=scope




						def 							__UpperCamelCase			(    self   : Dict					)							->       str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ids_tensor([self.batch_size, self.seq_length]			,      self.vocab_size					)

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

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

										_SCREAMING_SNAKE_CASE     					=self.get_config()

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




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ids_tensor([self.batch_size, self.seq_length]			,      self.vocab_size					)

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

										return config, input_ids, input_mask




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


										"""simple docstring"""





										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 							__UpperCamelCase			(    self   : Optional[Any]			,      _a   : Union[str, Any]			,      _a   : List[str]			,      _a   : Optional[Any]			,      _a   : int			,      _a   : Any			,      _a   : Tuple					)							->       int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ErnieMModel(config=_a					)
										model.to(_a					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=model(_a			,      return_dict=_a					)
										self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.hidden_size)					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ErnieMForQuestionAnswering(config=_a					)
										model.to(_a					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=model(
										    _a			,      attention_mask=_a			,      start_positions=_a			,      end_positions=_a			,      )
										self.parent.assertEqual(result.start_logits.shape			,      (self.batch_size, self.seq_length)					)
										self.parent.assertEqual(result.end_logits.shape			,      (self.batch_size, self.seq_length)					)




						def 							__UpperCamelCase			(    self   : Tuple			,      _a   : Union[str, Any]			,      _a   : str			,      _a   : Tuple			,      _a   : Tuple			,      _a   : Optional[int]			,      _a   : Dict					)							->       Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ErnieMForInformationExtraction(config=_a					)
										model.to(_a					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=torch.ones_like(_a			,      dtype=torch.floataa					)
										_SCREAMING_SNAKE_CASE     					=model(
										    _a			,      attention_mask=_a			,      start_positions=_a			,      end_positions=_a			,      )
										self.parent.assertEqual(result.start_logits.shape			,      (self.batch_size, self.seq_length)					)
										self.parent.assertEqual(result.end_logits.shape			,      (self.batch_size, self.seq_length)					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.num_labels
										_SCREAMING_SNAKE_CASE     					=ErnieMForSequenceClassification(_a					)
										model.to(_a					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=model(_a			,      attention_mask=_a			,      labels=_a					)
										self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels)					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.num_labels
										_SCREAMING_SNAKE_CASE     					=ErnieMForTokenClassification(config=_a					)
										model.to(_a					)
										model.eval()
										input_ids.to(_a					)
										input_mask.to(_a					)
										token_labels.to(_a					)

										_SCREAMING_SNAKE_CASE     					=model(_a			,      attention_mask=_a			,      labels=_a					)

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




						def 							__UpperCamelCase			(    self   : int			,      _a   : Dict			,      _a   : List[str]			,      _a   : Dict			,      _a   : List[str]			,      _a   : int			,      _a   : int					)							->       Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.num_choices
										_SCREAMING_SNAKE_CASE     					=ErnieMForMultipleChoice(config=_a					)
										model.to(_a					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=input_ids.unsqueeze(1					).expand(-1			,      self.num_choices			,      -1					).contiguous()
										_SCREAMING_SNAKE_CASE     					=input_mask.unsqueeze(1					).expand(-1			,      self.num_choices			,      -1					).contiguous()
										_SCREAMING_SNAKE_CASE     					=model(
										    _a			,      attention_mask=_a			,      labels=_a			,      )
										self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_choices)					)




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


										"""simple docstring"""





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

@require_torch
class   A__							(						UpperCamelCase__				,       UpperCamelCase__				,       unittest.TestCase					):
						UpperCAmelCase     =  (
						    (
						        ErnieMModel,
						        ErnieMForMultipleChoice,
						        ErnieMForQuestionAnswering,
						        ErnieMForSequenceClassification,
						        ErnieMForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase     =  ()
						UpperCAmelCase     =  (
						    {
						        "feature-extraction": ErnieMModel,
						        "question-answering": ErnieMForQuestionAnswering,
						        "text-classification": ErnieMForSequenceClassification,
						        "token-classification": ErnieMForTokenClassification,
						        "zero-shot": ErnieMForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCAmelCase     =  False




						def 							__UpperCamelCase			(    self   : Dict			,      _a   : List[Any]			,      _a   : Dict			,      _a   : Dict			,      _a   : List[Any]			,      _a   : Union[str, Any]					)							->       Optional[int]:


										"""simple docstring"""





										if pipeline_test_casse_name == "QAPipelineTests":
														return True

										return False




						def 							__UpperCamelCase			(    self   : Any					)							->       int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ErnieMModelTester(self					)
										_SCREAMING_SNAKE_CASE     					=ConfigTester(self			,      config_class=_a			,      hidden_size=37					)




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


										"""simple docstring"""





										self.config_tester.run_common_tests()




						def 							__UpperCamelCase			(    self   : List[str]					)							->       Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_a					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
														_SCREAMING_SNAKE_CASE     					=type
														self.model_tester.create_and_check_model(*_a					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*_a					)




						def 							__UpperCamelCase			(    self   : str					)							->       List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*_a					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*_a					)




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*_a					)




						def 							__UpperCamelCase			(    self   : str					)							->       int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_a					)




						@slow
						def 							__UpperCamelCase			(    self   : Any					)							->       Optional[int]:


										"""simple docstring"""





										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_SCREAMING_SNAKE_CASE     					=ErnieMModel.from_pretrained(_a					)
														self.assertIsNotNone(_a					)

@require_torch
class   A__							(						unittest.TestCase					):




						@slow
						def 							__UpperCamelCase			(    self   : Optional[int]					)							->       List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''					)
										model.eval()
										_SCREAMING_SNAKE_CASE     					=torch.tensor([[0, 1, 2, 3, 4, 5]]					)
										_SCREAMING_SNAKE_CASE     					=model(_a					)[0]

										# TODO Replace vocab size
										_SCREAMING_SNAKE_CASE     					=768

										_SCREAMING_SNAKE_CASE     					=torch.Size((1, 6, hidden_size)					)
										self.assertEqual(output.shape			,      _a					)

										_SCREAMING_SNAKE_CASE     					=torch.tensor(
										    [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]]					)

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



import math

class   A__							:




						def __init__(    self   : Optional[int]			,      _a   : int			,      _a   : List[Any]			,      _a   : int					)							->       Optional[int]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=x
										_SCREAMING_SNAKE_CASE     					=y
										_SCREAMING_SNAKE_CASE     					=z




						def __repr__(    self   : List[str]					)							->       str:


										"""simple docstring"""





										return f"Point({self.x}, {self.y}, {self.z})"







def   lowerCamelCase(				a__		,a__):
				return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))


if __name__ == "__main__":
							import doctest

							doctest.testmod()