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







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







			def       SCREAMING_SNAKE_CASE_	(      self							:    Tuple       ):
										lowercase_   :     Any									=			ids_tensor([self.batch_size, self.seq_length]							,     self.vocab_size       )

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

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

										lowercase_   :     str									=			self.get_config()

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







			def       SCREAMING_SNAKE_CASE_	(      self							:    Tuple       ):
										lowercase_   :     Optional[Any]									=			ids_tensor([self.batch_size, self.seq_length]							,     self.vocab_size       )

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

										return config, input_ids, input_mask







			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										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       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]							,     lowercase_							:    Optional[Any]							,     lowercase_							:    str							,     lowercase_							:    str							,     lowercase_							:    List[Any]							,     lowercase_							:    Any							,     lowercase_							:    Any       ):
										lowercase_   :     Union[str, Any]									=			ErnieMModel(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										lowercase_   :     Union[str, Any]									=			model(lowercase_							,     return_dict=lowercase_       )
										self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.hidden_size)       )







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







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[str]							,     lowercase_							:    Any							,     lowercase_							:    List[Any]							,     lowercase_							:    str							,     lowercase_							:    Dict							,     lowercase_							:    str							,     lowercase_							:    int       ):
										lowercase_   :     Optional[Any]									=			self.num_labels
										lowercase_   :     Any									=			ErnieMForTokenClassification(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										input_ids.to(lowercase_       )
										input_mask.to(lowercase_       )
										token_labels.to(lowercase_       )

										lowercase_   :     str									=			model(lowercase_							,     attention_mask=lowercase_							,     labels=lowercase_       )

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







			def       SCREAMING_SNAKE_CASE_	(      self							:    Dict							,     lowercase_							:    Optional[Any]							,     lowercase_							:    Tuple							,     lowercase_							:    str							,     lowercase_							:    str							,     lowercase_							:    List[Any]							,     lowercase_							:    Tuple       ):
										lowercase_   :     Any									=			self.num_choices
										lowercase_   :     Dict									=			ErnieMForMultipleChoice(config=lowercase_       )
										model.to(lowercase_       )
										model.eval()
										lowercase_   :     Optional[Any]									=			input_ids.unsqueeze(1       ).expand(-1							,     self.num_choices							,     -1       ).contiguous()
										lowercase_   :     Optional[int]									=			input_mask.unsqueeze(1       ).expand(-1							,     self.num_choices							,     -1       ).contiguous()
										lowercase_   :     Union[str, Any]									=			model(
										    lowercase_							,     attention_mask=lowercase_							,     labels=lowercase_							,     )
										self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.num_choices)       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[str]       ):
										lowercase_   :     Any									=			self.prepare_config_and_inputs()
										(
										    (
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		(
										    lowercase_
										)		,		
										)   :     List[Any]									=			config_and_inputs
										lowercase_   :     Any									=			{"""input_ids""": input_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_torch
class        __magic_name__		(       _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       SCREAMING_SNAKE_CASE_	(      self							:    str							,     lowercase_							:    str							,     lowercase_							:    Tuple							,     lowercase_							:    Optional[Any]							,     lowercase_							:    Tuple							,     lowercase_							:    Optional[int]       ):
										if pipeline_test_casse_name == "QAPipelineTests":
																	return True

										return False







			def       SCREAMING_SNAKE_CASE_	(      self							:    Optional[int]       ):
										lowercase_   :     Any									=			ErnieMModelTester(self       )
										lowercase_   :     Dict									=			ConfigTester(self							,     config_class=lowercase_							,     hidden_size=37       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										self.config_tester.run_common_tests()







			def       SCREAMING_SNAKE_CASE_	(      self							:    Tuple       ):
										lowercase_   :     Dict									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    Optional[int]       ):
										lowercase_   :     List[str]									=			self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																	lowercase_   :     Union[str, Any]									=			type
																	self.model_tester.create_and_check_model(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    Optional[Any]       ):
										lowercase_   :     Any									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    str       ):
										lowercase_   :     Any									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    str       ):
										lowercase_   :     Union[str, Any]									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										lowercase_   :     Union[str, Any]									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    int       ):
										lowercase_   :     Optional[Any]									=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowercase_       )







			@slow
			def       SCREAMING_SNAKE_CASE_	(      self							:    str       ):
										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_   :     int									=			ErnieMModel.from_pretrained(lowercase_       )
																	self.assertIsNotNone(lowercase_       )





@require_torch
class        __magic_name__		(       unittest.TestCase):







			@slow
			def       SCREAMING_SNAKE_CASE_	(      self							:    Any       ):
										lowercase_   :     Tuple									=			ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""       )
										model.eval()
										lowercase_   :     List[str]									=			torch.tensor([[0, 1, 2, 3, 4, 5]]       )
										lowercase_   :     Any									=			model(lowercase_       )[0]

										# TODO Replace vocab size
										lowercase_   :     str									=			768

										lowercase_   :     Dict									=			torch.Size((1, 6, hidden_size)       )
										self.assertEqual(output.shape							,     lowercase_       )

										lowercase_   :     List[Any]									=			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]							,     lowercase_							,     atol=1E-3       )       )






'''simple docstring'''




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


_lowercase		:					Optional[int]													=		logging.get_logger(__name__)


if is_vision_available():
						import PIL





class        __magic_name__		(       _UpperCAmelCase):
			UpperCamelCase__							=			['''pixel_values''']







			def __init__(      self							:    Any							,     lowercase_							:    bool = True							,     lowercase_							:    Dict[str, int] = None							,     lowercase_							:    PILImageResampling = PILImageResampling.BICUBIC							,     lowercase_							:    bool = True							,     lowercase_							:    Dict[str, int] = None							,     lowercase_							:    bool = True							,     lowercase_							:    Union[int, float] = 1 / 255							,     lowercase_							:    bool = True							,     lowercase_							:    Optional[Union[float, List[float]]] = None							,     lowercase_							:    Optional[Union[float, List[float]]] = None							,     lowercase_							:    bool = True							,     **lowercase_							:    Optional[Any]							,     ):
										super().__init__(**lowercase_       )
										lowercase_   :     List[Any]									=			size if size is not None else {"""shortest_edge""": 224}
										lowercase_   :     int									=			get_size_dict(lowercase_							,     default_to_square=lowercase_       )
										lowercase_   :     List[Any]									=			crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
										lowercase_   :     int									=			get_size_dict(lowercase_       )

										lowercase_   :     Tuple									=			do_resize
										lowercase_   :     int									=			size
										lowercase_   :     List[Any]									=			resample
										lowercase_   :     str									=			do_center_crop
										lowercase_   :     Optional[int]									=			crop_size
										lowercase_   :     str									=			do_rescale
										lowercase_   :     List[Any]									=			rescale_factor
										lowercase_   :     Dict									=			do_normalize
										lowercase_   :     List[str]									=			image_mean if image_mean is not None else OPENAI_CLIP_MEAN
										lowercase_   :     Any									=			image_std if image_std is not None else OPENAI_CLIP_STD
										lowercase_   :     List[str]									=			do_convert_rgb







			def       SCREAMING_SNAKE_CASE_	(      self							:    Union[str, Any]							,     lowercase_							:    np.ndarray							,     lowercase_							:    Dict[str, int]							,     lowercase_							:    PILImageResampling = PILImageResampling.BICUBIC							,     lowercase_							:    Optional[Union[str, ChannelDimension]] = None							,     **lowercase_							:    Dict							,     ):
										lowercase_   :     Tuple									=			get_size_dict(lowercase_							,     default_to_square=lowercase_       )
										lowercase_   :     List[Any]									=			get_resize_output_image_size(
										    lowercase_							,     size=(size["""height"""], size["""width"""])							,     default_to_square=lowercase_       )
										return resize(lowercase_							,     size=lowercase_							,     resample=lowercase_							,     data_format=lowercase_							,     **lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]							,     lowercase_							:    np.ndarray							,     lowercase_							:    Dict[str, int]							,     lowercase_							:    Optional[Union[str, ChannelDimension]] = None							,     **lowercase_							:    Any							,     ):
										lowercase_   :     int									=			get_size_dict(lowercase_       )
										return center_crop(lowercase_							,     size=(size["""height"""], size["""width"""])							,     data_format=lowercase_							,     **lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    Tuple							,     lowercase_							:    np.ndarray							,     lowercase_							:    Union[int, float]							,     lowercase_							:    Optional[Union[str, ChannelDimension]] = None							,     **lowercase_							:    List[Any]							,     ):
										return rescale(lowercase_							,     scale=lowercase_							,     data_format=lowercase_							,     **lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    int							,     lowercase_							:    np.ndarray							,     lowercase_							:    Union[float, List[float]]							,     lowercase_							:    Union[float, List[float]]							,     lowercase_							:    Optional[Union[str, ChannelDimension]] = None							,     **lowercase_							:    Optional[Any]							,     ):
										return normalize(lowercase_							,     mean=lowercase_							,     std=lowercase_							,     data_format=lowercase_							,     **lowercase_       )







			def       SCREAMING_SNAKE_CASE_	(      self							:    List[Any]							,     lowercase_							:    ImageInput							,     lowercase_							:    bool = None							,     lowercase_							:    Dict[str, int] = None							,     lowercase_							:    PILImageResampling = None							,     lowercase_							:    bool = None							,     lowercase_							:    int = None							,     lowercase_							:    bool = None							,     lowercase_							:    float = None							,     lowercase_							:    bool = None							,     lowercase_							:    Optional[Union[float, List[float]]] = None							,     lowercase_							:    Optional[Union[float, List[float]]] = None							,     lowercase_							:    bool = None							,     lowercase_							:    Optional[Union[str, TensorType]] = None							,     lowercase_							:    Optional[ChannelDimension] = ChannelDimension.FIRST							,     **lowercase_							:    Optional[int]							,     ):
										lowercase_   :     Optional[Any]									=			do_resize if do_resize is not None else self.do_resize
										lowercase_   :     Union[str, Any]									=			size if size is not None else self.size
										lowercase_   :     Optional[int]									=			get_size_dict(lowercase_							,     default_to_square=lowercase_       )
										lowercase_   :     int									=			resample if resample is not None else self.resample
										lowercase_   :     str									=			do_center_crop if do_center_crop is not None else self.do_center_crop
										lowercase_   :     int									=			crop_size if crop_size is not None else self.crop_size
										lowercase_   :     int									=			get_size_dict(lowercase_       )
										lowercase_   :     Dict									=			do_rescale if do_rescale is not None else self.do_rescale
										lowercase_   :     List[str]									=			rescale_factor if rescale_factor is not None else self.rescale_factor
										lowercase_   :     Optional[Any]									=			do_normalize if do_normalize is not None else self.do_normalize
										lowercase_   :     List[Any]									=			image_mean if image_mean is not None else self.image_mean
										lowercase_   :     str									=			image_std if image_std is not None else self.image_std
										lowercase_   :     Any									=			do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

										lowercase_   :     int									=			make_list_of_images(lowercase_       )

										if not valid_images(lowercase_       ):
																	raise ValueError(
																	    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
																	    """torch.Tensor, tf.Tensor or jax.ndarray."""       )

										if do_resize and size is None:
																	raise ValueError("""Size must be specified if do_resize is True."""       )

										if do_center_crop and crop_size is None:
																	raise ValueError("""Crop size must be specified if do_center_crop is True."""       )

										if do_rescale and rescale_factor is None:
																	raise ValueError("""Rescale factor must be specified if do_rescale is True."""       )

										if do_normalize and (image_mean is None or image_std is None):
																	raise ValueError("""Image mean and std must be specified if do_normalize is True."""       )

										# PIL RGBA images are converted to RGB
										if do_convert_rgb:
																	lowercase_   :     Dict									=			[convert_to_rgb(lowercase_       ) for image in images]

										# All transformations expect numpy arrays.
										lowercase_   :     Any									=			[to_numpy_array(lowercase_       ) for image in images]

										if do_resize:
																	lowercase_   :     Union[str, Any]									=			[self.resize(image=lowercase_							,     size=lowercase_							,     resample=lowercase_       ) for image in images]

										if do_center_crop:
																	lowercase_   :     List[str]									=			[self.center_crop(image=lowercase_							,     size=lowercase_       ) for image in images]

										if do_rescale:
																	lowercase_   :     int									=			[self.rescale(image=lowercase_							,     scale=lowercase_       ) for image in images]

										if do_normalize:
																	lowercase_   :     List[Any]									=			[self.normalize(image=lowercase_							,     mean=lowercase_							,     std=lowercase_       ) for image in images]

										lowercase_   :     List[Any]									=			[to_channel_dimension_format(lowercase_							,     lowercase_       ) for image in images]

										lowercase_   :     Any									=			{"""pixel_values""": images}
										return BatchFeature(data=lowercase_							,     tensor_type=lowercase_       )





