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







    def __init__(	self       ,     UpperCamelCase__       ,     UpperCamelCase__=1_3       ,     UpperCamelCase__=7       ,     UpperCamelCase__=True       ,     UpperCamelCase__=True       ,     UpperCamelCase__=True       ,     UpperCamelCase__=9_9       ,     UpperCamelCase__=3_2       ,     UpperCamelCase__=5       ,     UpperCamelCase__=4       ,     UpperCamelCase__=3_7       ,     UpperCamelCase__="gelu"       ,     UpperCamelCase__=0.1       ,     UpperCamelCase__=0.1       ,     UpperCamelCase__=5_1_2       ,     UpperCamelCase__=1_6       ,     UpperCamelCase__=2       ,     UpperCamelCase__=0.02       ,     UpperCamelCase__=3       ,     UpperCamelCase__=4       ,     UpperCamelCase__=None       ,     )				->       Union[str, Any]:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

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

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

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

     UpperCAmelCase_				       =					self.get_config()

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







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





     """simple docstring"""

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

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

     return config, input_ids, input_mask







    def   lowerCamelCase_    (	self				)				->       Any:





     """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   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       List[Any]:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       List[Any]:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       Tuple:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       List[str]:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       Optional[Any]:





     """simple docstring"""

     UpperCAmelCase_				       =					self.num_labels
     UpperCAmelCase_				       =					ErnieMForTokenClassification(config=UpperCamelCase__				)
     model.to(UpperCamelCase__				)
     model.eval()
     input_ids.to(UpperCamelCase__				)
     input_mask.to(UpperCamelCase__				)
     token_labels.to(UpperCamelCase__				)

     UpperCAmelCase_				       =					model(UpperCamelCase__       ,     attention_mask=UpperCamelCase__       ,     labels=UpperCamelCase__				)

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       List[Any]:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self				)				->       int:





     """simple docstring"""

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


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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       Optional[int]:





     """simple docstring"""

     if pipeline_test_casse_name == "QAPipelineTests":
      return True

     return False







    def   lowerCamelCase_    (	self				)				->       str:





     """simple docstring"""

     UpperCAmelCase_				       =					ErnieMModelTester(self				)
     UpperCAmelCase_				       =					ConfigTester(self       ,     config_class=UpperCamelCase__       ,     hidden_size=3_7				)







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





     """simple docstring"""

     self.config_tester.run_common_tests()







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





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase__				)







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





     """simple docstring"""

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







    def   lowerCamelCase_    (	self				)				->       List[Any]:





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__				)







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





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__				)







    def   lowerCamelCase_    (	self				)				->       Dict:





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__				)







    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase__				)







    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

     UpperCAmelCase_				       =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__				)







    @slow
    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

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


@require_torch
class 							lowercase_    (     unittest.TestCase						):







    @slow
    def   lowerCamelCase_    (	self				)				->       Dict:





     """simple docstring"""

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

     # TODO Replace vocab size
     UpperCAmelCase_				       =					7_6_8

     UpperCAmelCase_				       =					torch.Size((1, 6, hidden_size)				)
     self.assertEqual(output.shape       ,     UpperCamelCase__				)

     UpperCAmelCase_				       =					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]       ,     UpperCamelCase__       ,     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


__snake_case       :   Any  		=    logging.get_logger(__name__)


if is_vision_available():
      import PIL
class 							lowercase_    (     _A						):
    a_             =   ["""pixel_values"""]







    def __init__(	self       ,     UpperCamelCase__ = True       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = PILImageResampling.BICUBIC       ,     UpperCamelCase__ = True       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = True       ,     UpperCamelCase__ = 1 / 2_5_5       ,     UpperCamelCase__ = True       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = True       ,     **UpperCamelCase__       ,     )				->       None:





     """simple docstring"""

     super().__init__(**UpperCamelCase__				)
     UpperCAmelCase_				       =					size if size is not None else {"shortest_edge": 2_2_4}
     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__       ,     default_to_square=UpperCamelCase__				)
     UpperCAmelCase_				       =					crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__				)

     UpperCAmelCase_				       =					do_resize
     UpperCAmelCase_				       =					size
     UpperCAmelCase_				       =					resample
     UpperCAmelCase_				       =					do_center_crop
     UpperCAmelCase_				       =					crop_size
     UpperCAmelCase_				       =					do_rescale
     UpperCAmelCase_				       =					rescale_factor
     UpperCAmelCase_				       =					do_normalize
     UpperCAmelCase_				       =					image_mean if image_mean is not None else OPENAI_CLIP_MEAN
     UpperCAmelCase_				       =					image_std if image_std is not None else OPENAI_CLIP_STD
     UpperCAmelCase_				       =					do_convert_rgb







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__ = PILImageResampling.BICUBIC       ,     UpperCamelCase__ = None       ,     **UpperCamelCase__       ,     )				->       np.ndarray:





     """simple docstring"""

     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__       ,     default_to_square=UpperCamelCase__				)
     UpperCAmelCase_				       =					get_resize_output_image_size(
         UpperCamelCase__       ,     size=(size["height"], size["width"])       ,     default_to_square=UpperCamelCase__				)
     return resize(UpperCamelCase__       ,     size=UpperCamelCase__       ,     resample=UpperCamelCase__       ,     data_format=UpperCamelCase__       ,     **UpperCamelCase__				)







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__ = None       ,     **UpperCamelCase__       ,     )				->       np.ndarray:





     """simple docstring"""

     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__				)
     return center_crop(UpperCamelCase__       ,     size=(size["height"], size["width"])       ,     data_format=UpperCamelCase__       ,     **UpperCamelCase__				)







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__ = None       ,     **UpperCamelCase__       ,     )				->       int:





     """simple docstring"""

     return rescale(UpperCamelCase__       ,     scale=UpperCamelCase__       ,     data_format=UpperCamelCase__       ,     **UpperCamelCase__				)







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__ = None       ,     **UpperCamelCase__       ,     )				->       np.ndarray:





     """simple docstring"""

     return normalize(UpperCamelCase__       ,     mean=UpperCamelCase__       ,     std=UpperCamelCase__       ,     data_format=UpperCamelCase__       ,     **UpperCamelCase__				)







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = None       ,     UpperCamelCase__ = ChannelDimension.FIRST       ,     **UpperCamelCase__       ,     )				->       PIL.Image.Image:





     """simple docstring"""

     UpperCAmelCase_				       =					do_resize if do_resize is not None else self.do_resize
     UpperCAmelCase_				       =					size if size is not None else self.size
     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__       ,     default_to_square=UpperCamelCase__				)
     UpperCAmelCase_				       =					resample if resample is not None else self.resample
     UpperCAmelCase_				       =					do_center_crop if do_center_crop is not None else self.do_center_crop
     UpperCAmelCase_				       =					crop_size if crop_size is not None else self.crop_size
     UpperCAmelCase_				       =					get_size_dict(UpperCamelCase__				)
     UpperCAmelCase_				       =					do_rescale if do_rescale is not None else self.do_rescale
     UpperCAmelCase_				       =					rescale_factor if rescale_factor is not None else self.rescale_factor
     UpperCAmelCase_				       =					do_normalize if do_normalize is not None else self.do_normalize
     UpperCAmelCase_				       =					image_mean if image_mean is not None else self.image_mean
     UpperCAmelCase_				       =					image_std if image_std is not None else self.image_std
     UpperCAmelCase_				       =					do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

     UpperCAmelCase_				       =					make_list_of_images(UpperCamelCase__				)

     if not valid_images(UpperCamelCase__				):
      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:
      UpperCAmelCase_				       =					[convert_to_rgb(UpperCamelCase__				) for image in images]

     # All transformations expect numpy arrays.
     UpperCAmelCase_				       =					[to_numpy_array(UpperCamelCase__				) for image in images]

     if do_resize:
      UpperCAmelCase_				       =					[self.resize(image=UpperCamelCase__       ,     size=UpperCamelCase__       ,     resample=UpperCamelCase__				) for image in images]

     if do_center_crop:
      UpperCAmelCase_				       =					[self.center_crop(image=UpperCamelCase__       ,     size=UpperCamelCase__				) for image in images]

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

     if do_normalize:
      UpperCAmelCase_				       =					[self.normalize(image=UpperCamelCase__       ,     mean=UpperCamelCase__       ,     std=UpperCamelCase__				) for image in images]

     UpperCAmelCase_				       =					[to_channel_dimension_format(UpperCamelCase__       ,     UpperCamelCase__				) for image in images]

     UpperCAmelCase_				       =					{"pixel_values": images}
     return BatchFeature(data=UpperCamelCase__       ,     tensor_type=UpperCamelCase__				)
