

'''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 			A_					:






     '''simple docstring'''



     def __init__(						self					:	str	,     lowercase_					:	Tuple	,     lowercase_					:	Any=13	,     lowercase_					:	int=7	,     lowercase_					:	str=True	,     lowercase_					:	List[Any]=True	,     lowercase_					:	Union[str, Any]=True	,     lowercase_					:	Any=99	,     lowercase_					:	str=32	,     lowercase_					:	List[Any]=5	,     lowercase_					:	Union[str, Any]=4	,     lowercase_					:	Optional[int]=37	,     lowercase_					:	Dict="gelu"	,     lowercase_					:	Dict=0.1	,     lowercase_					:	Union[str, Any]=0.1	,     lowercase_					:	Any=512	,     lowercase_					:	Optional[Any]=16	,     lowercase_					:	Dict=2	,     lowercase_					:	Any=0.02	,     lowercase_					:	Any=3	,     lowercase_					:	Tuple=4	,     lowercase_					:	Tuple=None	,     )		->      Dict:
      UpperCAmelCase			:      Any		   =					parent
      UpperCAmelCase			:      Union[str, Any]		   =					batch_size
      UpperCAmelCase			:      Dict		   =					seq_length
      UpperCAmelCase			:      Dict		   =					is_training
      UpperCAmelCase			:      Optional[int]		   =					use_input_mask
      UpperCAmelCase			:      Tuple		   =					use_labels
      UpperCAmelCase			:      List[Any]		   =					vocab_size
      UpperCAmelCase			:      int		   =					hidden_size
      UpperCAmelCase			:      Any		   =					num_hidden_layers
      UpperCAmelCase			:      List[str]		   =					num_attention_heads
      UpperCAmelCase			:      str		   =					intermediate_size
      UpperCAmelCase			:      Optional[Any]		   =					hidden_act
      UpperCAmelCase			:      int		   =					hidden_dropout_prob
      UpperCAmelCase			:      Optional[int]		   =					attention_probs_dropout_prob
      UpperCAmelCase			:      Optional[Any]		   =					max_position_embeddings
      UpperCAmelCase			:      Tuple		   =					type_vocab_size
      UpperCAmelCase			:      List[str]		   =					type_sequence_label_size
      UpperCAmelCase			:      Dict		   =					initializer_range
      UpperCAmelCase			:      Dict		   =					num_labels
      UpperCAmelCase			:      Any		   =					num_choices
      UpperCAmelCase			:      Dict		   =					scope


     def   UpperCAmelCase_			(						self					:	Tuple			)		->      Dict:
      UpperCAmelCase			:      Optional[Any]		   =					ids_tensor([self.batch_size, self.seq_length]	,     self.vocab_size			)

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

      UpperCAmelCase			:      Optional[int]		   =					None
      UpperCAmelCase			:      Dict		   =					None
      UpperCAmelCase			:      List[str]		   =					None
      if self.use_labels:
       UpperCAmelCase			:      Optional[int]		   =					ids_tensor([self.batch_size]	,     self.type_sequence_label_size			)
       UpperCAmelCase			:      Dict		   =					ids_tensor([self.batch_size, self.seq_length]	,     self.num_labels			)
       UpperCAmelCase			:      List[str]		   =					ids_tensor([self.batch_size]	,     self.num_choices			)

      UpperCAmelCase			:      str		   =					self.get_config()

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


     def   UpperCAmelCase_			(						self					:	Dict			)		->      Any:
      UpperCAmelCase			:      List[str]		   =					ids_tensor([self.batch_size, self.seq_length]	,     self.vocab_size			)

      UpperCAmelCase			:      Dict		   =					None
      if self.use_input_mask:
       UpperCAmelCase			:      List[str]		   =					random_attention_mask([self.batch_size, self.seq_length]			)
      UpperCAmelCase			:      Union[str, Any]		   =					self.get_config()

      return config, input_ids, input_mask


     def   UpperCAmelCase_			(						self					:	List[str]			)		->      List[Any]:
      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					:	Union[str, Any]	,     lowercase_					:	int	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[int]	,     lowercase_					:	List[str]	,     lowercase_					:	List[Any]	,     lowercase_					:	str			)		->      Tuple:
      UpperCAmelCase			:      List[str]		   =					ErnieMModel(config=__A			)
      model.to(__A			)
      model.eval()
      UpperCAmelCase			:      Optional[int]		   =					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					:	Optional[int]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Tuple	,     lowercase_					:	Any	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	List[str]	,     lowercase_					:	List[Any]			)		->      Union[str, Any]:
      UpperCAmelCase			:      str		   =					ErnieMForQuestionAnswering(config=__A			)
      model.to(__A			)
      model.eval()
      UpperCAmelCase			:      Union[str, Any]		   =					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[Any]	,     lowercase_					:	Optional[Any]	,     lowercase_					:	List[Any]	,     lowercase_					:	Any	,     lowercase_					:	str	,     lowercase_					:	List[Any]	,     lowercase_					:	List[str]			)		->      Union[str, Any]:
      UpperCAmelCase			:      Tuple		   =					ErnieMForInformationExtraction(config=__A			)
      model.to(__A			)
      model.eval()
      UpperCAmelCase			:      Dict		   =					torch.ones_like(__A	,     dtype=torch.floataa			)
      UpperCAmelCase			:      Tuple		   =					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	,     lowercase_					:	str	,     lowercase_					:	Optional[int]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[Any]			)		->      Any:
      UpperCAmelCase			:      Any		   =					self.num_labels
      UpperCAmelCase			:      Optional[int]		   =					ErnieMForSequenceClassification(__A			)
      model.to(__A			)
      model.eval()
      UpperCAmelCase			:      Tuple		   =					model(__A	,     attention_mask=__A	,     labels=__A			)
      self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.num_labels)			)


     def   UpperCAmelCase_			(						self					:	Dict	,     lowercase_					:	Any	,     lowercase_					:	List[Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[Any]			)		->      Tuple:
      UpperCAmelCase			:      Tuple		   =					self.num_labels
      UpperCAmelCase			:      List[str]		   =					ErnieMForTokenClassification(config=__A			)
      model.to(__A			)
      model.eval()
      input_ids.to(__A			)
      input_mask.to(__A			)
      token_labels.to(__A			)

      UpperCAmelCase			:      Optional[int]		   =					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					:	Tuple	,     lowercase_					:	Optional[int]	,     lowercase_					:	Dict	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Dict	,     lowercase_					:	List[Any]	,     lowercase_					:	Dict			)		->      str:
      UpperCAmelCase			:      List[str]		   =					self.num_choices
      UpperCAmelCase			:      str		   =					ErnieMForMultipleChoice(config=__A			)
      model.to(__A			)
      model.eval()
      UpperCAmelCase			:      str		   =					input_ids.unsqueeze(1			).expand(-1	,     self.num_choices	,     -1			).contiguous()
      UpperCAmelCase			:      Optional[Any]		   =					input_mask.unsqueeze(1			).expand(-1	,     self.num_choices	,     -1			).contiguous()
      UpperCAmelCase			:      Any		   =					model(
          __A	,     attention_mask=__A	,     labels=__A	,     )
      self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.num_choices)			)




     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      List[Any]:
      UpperCAmelCase			:      int		   =					self.prepare_config_and_inputs()
      (
          (
          UpperCAmelCase
      )   ,					(
          UpperCAmelCase
      )   ,					(
          UpperCAmelCase
      )   ,					(
          UpperCAmelCase
      )   ,					(
          UpperCAmelCase
      )   ,					(
          UpperCAmelCase
      )   ,					
      )			:      int		   =					config_and_inputs
      UpperCAmelCase			:      Dict		   =					{'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict




@require_torch
class 			A_					(							_A			,  _A			,  unittest.TestCase				):






     '''simple docstring'''



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


     def   UpperCAmelCase_			(						self					:	Dict	,     lowercase_					:	str	,     lowercase_					:	List[str]	,     lowercase_					:	int	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	List[str]			)		->      Tuple:





      if pipeline_test_casse_name == "QAPipelineTests":
       return True

      return False


     def   UpperCAmelCase_			(						self					:	int			)		->      int:
      UpperCAmelCase			:      str		   =					ErnieMModelTester(self			)
      UpperCAmelCase			:      Dict		   =					ConfigTester(self	,     config_class=__A	,     hidden_size=37			)


     def   UpperCAmelCase_			(						self					:	Union[str, Any]			)		->      Union[str, Any]:
      self.config_tester.run_common_tests()


     def   UpperCAmelCase_			(						self					:	str			)		->      str:
      UpperCAmelCase			:      List[str]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__A			)


     def   UpperCAmelCase_			(						self					:	Optional[Any]			)		->      Optional[int]:
      UpperCAmelCase			:      int		   =					self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
       UpperCAmelCase			:      int		   =					type
       self.model_tester.create_and_check_model(*__A			)


     def   UpperCAmelCase_			(						self					:	Any			)		->      Union[str, Any]:
      UpperCAmelCase			:      List[Any]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*__A			)


     def   UpperCAmelCase_			(						self					:	int			)		->      int:
      UpperCAmelCase			:      Optional[int]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__A			)


     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      List[Any]:
      UpperCAmelCase			:      Optional[int]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__A			)


     def   UpperCAmelCase_			(						self					:	Tuple			)		->      List[Any]:
      UpperCAmelCase			:      Union[str, Any]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_information_extraction(*__A			)


     def   UpperCAmelCase_			(						self					:	List[str]			)		->      Dict:
      UpperCAmelCase			:      Tuple		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__A			)




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





      for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase			:      Dict		   =					ErnieMModel.from_pretrained(__A			)
       self.assertIsNotNone(__A			)




@require_torch
class 			A_					(							unittest.TestCase				):






     '''simple docstring'''



     @slow
     def   UpperCAmelCase_			(						self					:	Tuple			)		->      Optional[int]:
      UpperCAmelCase			:      Union[str, Any]		   =					ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'			)
      model.eval()
      UpperCAmelCase			:      Any		   =					torch.tensor([[0, 1, 2, 3, 4, 5]]			)
      UpperCAmelCase			:      Optional[int]		   =					model(__A			)[0]

      # TODO Replace vocab size
      UpperCAmelCase			:      Any		   =					768

      UpperCAmelCase			:      Optional[Any]		   =					torch.Size((1, 6, hidden_size)			)
      self.assertEqual(output.shape	,     __A			)

      UpperCAmelCase			:      List[str]		   =					torch.tensor(
          [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]			)

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









'''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 DeiTImageProcessor

class      lowerCamelCase__     (     unittest.TestCase				):






	'''simple docstring'''

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




								'''simple docstring'''





								lowerCAmelCase__  =   size if size is not None else {"""height""": 20, """width""": 20}
								lowerCAmelCase__  =   crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

								lowerCAmelCase__  =   parent
								lowerCAmelCase__  =   batch_size
								lowerCAmelCase__  =   num_channels
								lowerCAmelCase__  =   image_size
								lowerCAmelCase__  =   min_resolution
								lowerCAmelCase__  =   max_resolution
								lowerCAmelCase__  =   do_resize
								lowerCAmelCase__  =   size
								lowerCAmelCase__  =   do_center_crop
								lowerCAmelCase__  =   crop_size
								lowerCAmelCase__  =   do_normalize
								lowerCAmelCase__  =   image_mean
								lowerCAmelCase__  =   image_std




	def      lowercase__							(  self		:  Tuple					)     ->			Optional[int]:




								'''simple docstring'''





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





@require_torch
@require_vision
class      lowerCamelCase__     (     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	DeiTImageProcessor if is_vision_available() else None
	A__		=	True




	def      lowercase__							(  self		:  List[Any]					)     ->			Dict:




								'''simple docstring'''





								lowerCAmelCase__  =   DeiTImageProcessingTester(self					)




	@property
	def      lowercase__							(  self		:  Any					)     ->			int:




								'''simple docstring'''





								return self.image_processor_tester.prepare_image_processor_dict()




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class(**self.image_processor_dict					)
								self.assertTrue(hasattr(__A   ,		"""do_resize"""					)					)
								self.assertTrue(hasattr(__A   ,		"""size"""					)					)
								self.assertTrue(hasattr(__A   ,		"""do_center_crop"""					)					)
								self.assertTrue(hasattr(__A   ,		"""center_crop"""					)					)
								self.assertTrue(hasattr(__A   ,		"""do_normalize"""					)					)
								self.assertTrue(hasattr(__A   ,		"""image_mean"""					)					)
								self.assertTrue(hasattr(__A   ,		"""image_std"""					)					)




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class.from_dict(self.image_processor_dict					)
								self.assertEqual(image_processor.size   ,		{"""height""": 20, """width""": 20}					)
								self.assertEqual(image_processor.crop_size   ,		{"""height""": 18, """width""": 18}					)

								lowerCAmelCase__  =   self.image_processing_class.from_dict(self.image_processor_dict   ,		size=42   ,		crop_size=84					)
								self.assertEqual(image_processor.size   ,		{"""height""": 42, """width""": 42}					)
								self.assertEqual(image_processor.crop_size   ,		{"""height""": 84, """width""": 84}					)




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




								'''simple docstring'''





								pass




	def      lowercase__							(  self		:  Dict					)     ->			Tuple:




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class(**self.image_processor_dict					)
								# create random PIL images
								lowerCAmelCase__  =   prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=__A					)
								for image in image_inputs:
															self.assertIsInstance(__A   ,		Image.Image					)

								# Test not batched input
								lowerCAmelCase__  =   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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)

								# Test batched
								lowerCAmelCase__  =   image_processing(__A   ,		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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class(**self.image_processor_dict					)
								# create random numpy tensors
								lowerCAmelCase__  =   prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=__A   ,		numpify=__A					)
								for image in image_inputs:
															self.assertIsInstance(__A   ,		np.ndarray					)

								# Test not batched input
								lowerCAmelCase__  =   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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)

								# Test batched
								lowerCAmelCase__  =   image_processing(__A   ,		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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)




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




								'''simple docstring'''





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

								# Test not batched input
								lowerCAmelCase__  =   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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)

								# Test batched
								lowerCAmelCase__  =   image_processing(__A   ,		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.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )   ,		)





