




import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate




class A						(     UpperCAmelCase_  ):
      UpperCamelCase__							:						Dict   =CONFIG_NAME






      @register_to_config
      def __init__(      self		:  Optional[int]    ,     lowercase_		:  Dict = True    ,     lowercase_		:  int = 8    ,     lowercase_		:  str = "lanczos"    ,     lowercase_		:  List[str] = True    ,     lowercase_		:  List[Any] = False    ,     )      ->		int:
             """simple docstring"""




             super().__init__()






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  List[Any]				)      ->		PIL.Image.Image:
             """simple docstring"""




             if images.ndim == 3:
                    _lowerCamelCase :	Union[str, Any]						=images[None, ...]
             _lowerCamelCase :	Dict						=(images * 255).round().astype('uint8'				)
             if images.shape[-1] == 1:
                    # special case for grayscale (single channel) images
                    _lowerCamelCase :	List[Any]						=[Image.fromarray(image.squeeze()    ,     mode='L'				) for image in images]
             else:
                    _lowerCamelCase :	Any						=[Image.fromarray(lowercase_				) for image in images]

             return pil_images






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  str				)      ->		np.ndarray:
             """simple docstring"""




             if not isinstance(lowercase_    ,     lowercase_				):
                    _lowerCamelCase :	Any						=[images]
             _lowerCamelCase :	Optional[int]						=[np.array(lowercase_				).astype(np.floataa				) / 255.0 for image in images]
             _lowerCamelCase :	Union[str, Any]						=np.stack(lowercase_    ,     axis=0				)

             return images






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  Any				)      ->		torch.FloatTensor:
             """simple docstring"""




             if images.ndim == 3:
                    _lowerCamelCase :	Dict						=images[..., None]

             _lowerCamelCase :	Optional[int]						=torch.from_numpy(images.transpose(0    ,     3    ,     1    ,     2				)				)
             return images






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  str				)      ->		np.ndarray:
             """simple docstring"""




             _lowerCamelCase :	str						=images.cpu().permute(0    ,     2    ,     3    ,     1				).float().numpy()
             return images






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  Optional[int]				)      ->		Union[str, Any]:
             """simple docstring"""




             return 2.0 * images - 1.0






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  int				)      ->		Any:
             """simple docstring"""




             return (images / 2 + 0.5).clamp(0    ,     1				)






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  Any				)      ->		PIL.Image.Image:
             """simple docstring"""




             _lowerCamelCase :	Any						=image.convert('RGB'				)
             return image






      def  lowerCamelCase (      self		:  List[str]    ,     lowercase_		:  str    ,     lowercase_		:  List[Any] = None    ,     lowercase_		:  Optional[Any] = None    ,     )      ->		PIL.Image.Image:
             """simple docstring"""




             if height is None:
                    _lowerCamelCase :	int						=image.height
             if width is None:
                    _lowerCamelCase :	Dict						=image.width

             _lowerCamelCase			,  _lowerCamelCase :	Any						=(
                 x - x % self.config.vae_scale_factor for x in (width, height)
             )  # resize to integer multiple of vae_scale_factor
             _lowerCamelCase :	List[str]						=image.resize((width, height)    ,     resample=PIL_INTERPOLATION[self.config.resample]				)
             return image






      def  lowerCamelCase (      self		:  Optional[int]    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  Union[str, Any] = None    ,     lowercase_		:  List[str] = None    ,     )      ->		torch.Tensor:
             """simple docstring"""




             _lowerCamelCase :	int						=(PIL.Image.Image, np.ndarray, torch.Tensor)
             if isinstance(lowercase_    ,     lowercase_				):
                    _lowerCamelCase :	int						=[image]
             elif not (isinstance(lowercase_    ,     lowercase_				) and all(isinstance(lowercase_    ,     lowercase_				) for i in image				)):
                    raise ValueError(
                        F'''Input is in incorrect format: {[type(lowercase_				) for i in image]}. Currently, we only support {', '.join(lowercase_				)}'''				)

             if isinstance(image[0]    ,     PIL.Image.Image				):
                    if self.config.do_convert_rgb:
                           _lowerCamelCase :	str						=[self.convert_to_rgb(lowercase_				) for i in image]
                    if self.config.do_resize:
                           _lowerCamelCase :	Dict						=[self.resize(lowercase_    ,     lowercase_    ,     lowercase_				) for i in image]
                    _lowerCamelCase :	Dict						=self.pil_to_numpy(lowercase_				)  # to np
                    _lowerCamelCase :	Optional[int]						=self.numpy_to_pt(lowercase_				)  # to pt

             elif isinstance(image[0]    ,     np.ndarray				):
                    _lowerCamelCase :	List[Any]						=np.concatenate(lowercase_    ,     axis=0				) if image[0].ndim == 4 else np.stack(lowercase_    ,     axis=0				)
                    _lowerCamelCase :	Optional[Any]						=self.numpy_to_pt(lowercase_				)
                    _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase :	str						=image.shape
                    if self.config.do_resize and (
                        height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
                    ):
                           raise ValueError(
                               F'''Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}'''
                               F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''				)

             elif isinstance(image[0]    ,     torch.Tensor				):
                    _lowerCamelCase :	Optional[int]						=torch.cat(lowercase_    ,     axis=0				) if image[0].ndim == 4 else torch.stack(lowercase_    ,     axis=0				)
                    _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase :	Union[str, Any]						=image.shape

                    # don't need any preprocess if the image is latents
                    if channel == 4:
                           return image

                    if self.config.do_resize and (
                        height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
                    ):
                           raise ValueError(
                               F'''Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}'''
                               F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''				)

                               # expected range [0,1], normalize to [-1,1]
             _lowerCamelCase :	Any						=self.config.do_normalize
             if image.min() < 0:
                    warnings.warn(
                        'Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] '
                        F'''when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]'''    ,     lowercase_    ,     )
                    _lowerCamelCase :	Optional[Any]						=False

             if do_normalize:
                    _lowerCamelCase :	int						=self.normalize(lowercase_				)

             return image






      def  lowerCamelCase (      self		:  Dict    ,     lowercase_		:  Tuple    ,     lowercase_		:  Optional[Any] = "pil"    ,     lowercase_		:  Optional[int] = None    ,     )      ->		Optional[Any]:
             """simple docstring"""




             if not isinstance(lowercase_    ,     torch.Tensor				):
                    raise ValueError(
                        F'''Input for postprocessing is in incorrect format: {type(lowercase_				)}. We only support pytorch tensor'''				)
             if output_type not in ["latent", "pt", "np", "pil"]:
                    _lowerCamelCase :	Any						=(
                        F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
                        '`pil`, `np`, `pt`, `latent`'
                    )
                    deprecate('Unsupported output_type'    ,     '1.0.0'    ,     lowercase_    ,     standard_warn=lowercase_				)
                    _lowerCamelCase :	str						='np'

             if output_type == "latent":
                    return image

             if do_denormalize is None:
                    _lowerCamelCase :	Any						=[self.config.do_normalize] * image.shape[0]

             _lowerCamelCase :	Dict						=torch.stack(
                 [self.denormalize(image[i]				) if do_denormalize[i] else image[i] for i in range(image.shape[0]				)]				)

             if output_type == "pt":
                    return image

             _lowerCamelCase :	List[str]						=self.pt_to_numpy(lowercase_				)

             if output_type == "np":
                    return image

             if output_type == "pil":
                    return self.numpy_to_pil(lowercase_				)




class A						(     UpperCAmelCase_  ):
      UpperCamelCase__							:						Union[str, Any]   =CONFIG_NAME






      @register_to_config
      def __init__(      self		:  Dict    ,     lowercase_		:  Any = True    ,     lowercase_		:  int = 8    ,     lowercase_		:  Tuple = "lanczos"    ,     lowercase_		:  Optional[int] = True    ,     )      ->		Union[str, Any]:
             """simple docstring"""




             super().__init__()






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  int				)      ->		Any:
             """simple docstring"""




             if images.ndim == 3:
                    _lowerCamelCase :	List[Any]						=images[None, ...]
             _lowerCamelCase :	List[str]						=(images * 255).round().astype('uint8'				)
             if images.shape[-1] == 1:
                    # special case for grayscale (single channel) images
                    _lowerCamelCase :	Dict						=[Image.fromarray(image.squeeze()    ,     mode='L'				) for image in images]
             else:
                    _lowerCamelCase :	str						=[Image.fromarray(image[:, :, :3]				) for image in images]

             return pil_images






      @staticmethod
      def  lowerCamelCase (      lowercase_		:  Tuple				)      ->		Tuple:
             """simple docstring"""




             return image[:, :, 1] * 2**8 + image[:, :, 2]






      def  lowerCamelCase (      self		:  Tuple    ,     lowercase_		:  List[Any]				)      ->		int:
             """simple docstring"""




             if images.ndim == 3:
                    _lowerCamelCase :	Union[str, Any]						=images[None, ...]
             _lowerCamelCase :	str						=images[:, :, :, 3:]
             if images.shape[-1] == 6:
                    _lowerCamelCase :	Tuple						=(images_depth * 255).round().astype('uint8'				)
                    _lowerCamelCase :	str						=[
                        Image.fromarray(self.rgblike_to_depthmap(lowercase_				)    ,     mode='I;16'				) for image_depth in images_depth
                    ]
             elif images.shape[-1] == 4:
                    _lowerCamelCase :	Dict						=(images_depth * 6_5535.0).astype(np.uintaa				)
                    _lowerCamelCase :	Optional[int]						=[Image.fromarray(lowercase_    ,     mode='I;16'				) for image_depth in images_depth]
             else:
                    raise Exception('Not supported'				)

             return pil_images






      def  lowerCamelCase (      self		:  List[Any]    ,     lowercase_		:  str    ,     lowercase_		:  Union[str, Any] = "pil"    ,     lowercase_		:  Dict = None    ,     )      ->		List[Any]:
             """simple docstring"""




             if not isinstance(lowercase_    ,     torch.Tensor				):
                    raise ValueError(
                        F'''Input for postprocessing is in incorrect format: {type(lowercase_				)}. We only support pytorch tensor'''				)
             if output_type not in ["latent", "pt", "np", "pil"]:
                    _lowerCamelCase :	Union[str, Any]						=(
                        F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
                        '`pil`, `np`, `pt`, `latent`'
                    )
                    deprecate('Unsupported output_type'    ,     '1.0.0'    ,     lowercase_    ,     standard_warn=lowercase_				)
                    _lowerCamelCase :	List[str]						='np'

             if do_denormalize is None:
                    _lowerCamelCase :	str						=[self.config.do_normalize] * image.shape[0]

             _lowerCamelCase :	Tuple						=torch.stack(
                 [self.denormalize(image[i]				) if do_denormalize[i] else image[i] for i in range(image.shape[0]				)]				)

             _lowerCamelCase :	Any						=self.pt_to_numpy(lowercase_				)

             if output_type == "np":
                    if image.shape[-1] == 6:
                           _lowerCamelCase :	Optional[Any]						=np.stack([self.rgblike_to_depthmap(im[:, :, 3:]				) for im in image]    ,     axis=0				)
                    else:
                           _lowerCamelCase :	Optional[Any]						=image[:, :, :, 3:]
                    return image[:, :, :, :3], image_depth

             if output_type == "pil":
                    return self.numpy_to_pil(lowercase_				), self.numpy_to_depth(lowercase_				)
             else:
                    raise Exception(F'''This type {output_type} is not supported'''				)






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		:  int    ,     lowercase_		:  List[Any]    ,     lowercase_		:  Dict=13    ,     lowercase_		:  Optional[Any]=7    ,     lowercase_		:  List[str]=True    ,     lowercase_		:  List[str]=True    ,     lowercase_		:  Tuple=True    ,     lowercase_		:  Union[str, Any]=99    ,     lowercase_		:  Union[str, Any]=32    ,     lowercase_		:  Tuple=5    ,     lowercase_		:  List[Any]=4    ,     lowercase_		:  Any=37    ,     lowercase_		:  Union[str, Any]="gelu"    ,     lowercase_		:  Any=0.1    ,     lowercase_		:  Union[str, Any]=0.1    ,     lowercase_		:  List[str]=512    ,     lowercase_		:  Optional[int]=16    ,     lowercase_		:  int=2    ,     lowercase_		:  Optional[Any]=0.02    ,     lowercase_		:  Dict=3    ,     lowercase_		:  Any=4    ,     lowercase_		:  List[str]=None    ,     )      ->		Tuple:
             """simple docstring"""




             _lowerCamelCase :	Dict						=parent
             _lowerCamelCase :	Optional[int]						=batch_size
             _lowerCamelCase :	int						=seq_length
             _lowerCamelCase :	List[str]						=is_training
             _lowerCamelCase :	Optional[int]						=use_input_mask
             _lowerCamelCase :	Dict						=use_labels
             _lowerCamelCase :	Any						=vocab_size
             _lowerCamelCase :	Any						=hidden_size
             _lowerCamelCase :	Any						=num_hidden_layers
             _lowerCamelCase :	Optional[int]						=num_attention_heads
             _lowerCamelCase :	Optional[Any]						=intermediate_size
             _lowerCamelCase :	Union[str, Any]						=hidden_act
             _lowerCamelCase :	str						=hidden_dropout_prob
             _lowerCamelCase :	Tuple						=attention_probs_dropout_prob
             _lowerCamelCase :	Tuple						=max_position_embeddings
             _lowerCamelCase :	Optional[int]						=type_vocab_size
             _lowerCamelCase :	Optional[Any]						=type_sequence_label_size
             _lowerCamelCase :	List[str]						=initializer_range
             _lowerCamelCase :	Dict						=num_labels
             _lowerCamelCase :	List[str]						=num_choices
             _lowerCamelCase :	Optional[Any]						=scope






      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		Tuple:
             """simple docstring"""




             _lowerCamelCase :	Union[str, Any]						=ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size				)

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

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

             _lowerCamelCase :	Any						=self.get_config()

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






      def  lowerCamelCase (      self		:  Dict				)      ->		Dict:
             """simple docstring"""




             _lowerCamelCase :	List[str]						=ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size				)

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

             return config, input_ids, input_mask






      def  lowerCamelCase (      self		:  Any				)      ->		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		:  Optional[int]    ,     lowercase_		:  Tuple    ,     lowercase_		:  Tuple    ,     lowercase_		:  Any    ,     lowercase_		:  Dict    ,     lowercase_		:  int    ,     lowercase_		:  Tuple				)      ->		List[str]:
             """simple docstring"""




             _lowerCamelCase :	Tuple						=ErnieMModel(config=lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	str						=model(lowercase_    ,     return_dict=lowercase_				)
             self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.hidden_size)				)






      def  lowerCamelCase (      self		:  str    ,     lowercase_		:  Union[str, Any]    ,     lowercase_		:  Union[str, Any]    ,     lowercase_		:  List[str]    ,     lowercase_		:  str    ,     lowercase_		:  List[str]    ,     lowercase_		:  str				)      ->		str:
             """simple docstring"""




             _lowerCamelCase :	Any						=ErnieMForQuestionAnswering(config=lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	List[str]						=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  lowerCamelCase (      self		:  int    ,     lowercase_		:  List[Any]    ,     lowercase_		:  int    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  Optional[Any]    ,     lowercase_		:  Any    ,     lowercase_		:  Optional[int]				)      ->		Any:
             """simple docstring"""




             _lowerCamelCase :	Union[str, Any]						=ErnieMForInformationExtraction(config=lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	Optional[int]						=torch.ones_like(lowercase_    ,     dtype=torch.floataa				)
             _lowerCamelCase :	str						=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  lowerCamelCase (      self		:  Dict    ,     lowercase_		:  Dict    ,     lowercase_		:  Tuple    ,     lowercase_		:  List[str]    ,     lowercase_		:  str    ,     lowercase_		:  Any    ,     lowercase_		:  Tuple				)      ->		int:
             """simple docstring"""




             _lowerCamelCase :	Dict						=self.num_labels
             _lowerCamelCase :	Any						=ErnieMForSequenceClassification(lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	Optional[int]						=model(lowercase_    ,     attention_mask=lowercase_    ,     labels=lowercase_				)
             self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.num_labels)				)






      def  lowerCamelCase (      self		:  Dict    ,     lowercase_		:  List[str]    ,     lowercase_		:  Optional[Any]    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  List[str]    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  Dict				)      ->		int:
             """simple docstring"""




             _lowerCamelCase :	int						=self.num_labels
             _lowerCamelCase :	List[str]						=ErnieMForTokenClassification(config=lowercase_				)
             model.to(lowercase_				)
             model.eval()
             input_ids.to(lowercase_				)
             input_mask.to(lowercase_				)
             token_labels.to(lowercase_				)

             _lowerCamelCase :	List[Any]						=model(lowercase_    ,     attention_mask=lowercase_    ,     labels=lowercase_				)

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






      def  lowerCamelCase (      self		:  Union[str, Any]    ,     lowercase_		:  int    ,     lowercase_		:  Union[str, Any]    ,     lowercase_		:  int    ,     lowercase_		:  str    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  str				)      ->		Tuple:
             """simple docstring"""




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






      def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		List[str]:
             """simple docstring"""




             _lowerCamelCase :	Tuple						=self.prepare_config_and_inputs()
             (
                 (
                 _lowerCamelCase
             )			,  (
                 _lowerCamelCase
             )			,  (
                 _lowerCamelCase
             )			,  (
                 _lowerCamelCase
             )			,  (
                 _lowerCamelCase
             )			,  (
                 _lowerCamelCase
             )			,  
             ) :	List[Any]						=config_and_inputs
             _lowerCamelCase :	Optional[Any]						={'input_ids': input_ids, 'attention_mask': input_mask}
             return config, inputs_dict




@require_torch
class A						(     UpperCamelCase_					,							UpperCamelCase_					,							unittest.TestCase  ):
      UpperCamelCase__							:						str   =(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase__							:						Dict   =()
      UpperCamelCase__							:						List[str]   =(
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      UpperCamelCase__							:						List[str]   =False






      def  lowerCamelCase (      self		:  str    ,     lowercase_		:  List[Any]    ,     lowercase_		:  Any    ,     lowercase_		:  str    ,     lowercase_		:  Tuple    ,     lowercase_		:  Tuple				)      ->		Dict:
             """simple docstring"""




             if pipeline_test_casse_name == "QAPipelineTests":
                    return True

             return False






      def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		int:
             """simple docstring"""




             _lowerCamelCase :	int						=ErnieMModelTester(self				)
             _lowerCamelCase :	Optional[int]						=ConfigTester(self    ,     config_class=lowercase_    ,     hidden_size=37				)






      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		Union[str, Any]:
             """simple docstring"""




             self.config_tester.run_common_tests()






      def  lowerCamelCase (      self		:  Optional[int]				)      ->		Dict:
             """simple docstring"""




             _lowerCamelCase :	Union[str, Any]						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*lowercase_				)






      def  lowerCamelCase (      self		:  Dict				)      ->		Optional[Any]:
             """simple docstring"""




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






      def  lowerCamelCase (      self		:  Tuple				)      ->		Any:
             """simple docstring"""




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






      def  lowerCamelCase (      self		:  str				)      ->		Tuple:
             """simple docstring"""




             _lowerCamelCase :	Dict						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*lowercase_				)






      def  lowerCamelCase (      self		:  List[str]				)      ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase :	Any						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*lowercase_				)






      def  lowerCamelCase (      self		:  List[str]				)      ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase :	List[str]						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_information_extraction(*lowercase_				)






      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		Tuple:
             """simple docstring"""




             _lowerCamelCase :	str						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*lowercase_				)






      @slow
      def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		List[str]:
             """simple docstring"""




             for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _lowerCamelCase :	Union[str, Any]						=ErnieMModel.from_pretrained(lowercase_				)
                    self.assertIsNotNone(lowercase_				)




@require_torch
class A						(     unittest.TestCase  ):






      @slow
      def  lowerCamelCase (      self		:  List[str]				)      ->		Dict:
             """simple docstring"""




             _lowerCamelCase :	Dict						=ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'				)
             model.eval()
             _lowerCamelCase :	Optional[int]						=torch.tensor([[0, 1, 2, 3, 4, 5]]				)
             _lowerCamelCase :	Optional[int]						=model(lowercase_				)[0]

             # TODO Replace vocab size
             _lowerCamelCase :	Optional[Any]						=768

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

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


