



"""simple docstring"""
import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
   import torch

if is_tf_available():
   import tensorflow as tf
@require_vision
@require_torchvision
class     a		(     unittest.TestCase				):



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE_:       Tuple   	=SamImageProcessor()
     SCREAMING_SNAKE_CASE_:       List[Any]   	=SamProcessor(__lowerCAmelCase						)
     processor.save_pretrained(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Union[str, Any]				,						**lowerCAmelCase       :    Any						)   ->    int:


     '''simple docstring'''




     return AutoProcessor.from_pretrained(self.tmpdirname				,						**__lowerCAmelCase						).image_processor



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


     '''simple docstring'''




     shutil.rmtree(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Dict						)   ->    str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       List[Any]   	=[np.random.randint(255				,						size=(3, 30, 400)				,						dtype=np.uinta						)]

     SCREAMING_SNAKE_CASE_:       str   	=[Image.fromarray(np.moveaxis(__lowerCAmelCase				,						0				,						-1						)						) for x in image_inputs]

     return image_inputs



  def 		lowerCamelCase__				(  self       :    Any						)   ->    List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=SamProcessor(image_processor=self.get_image_processor()						)
     processor.save_pretrained(self.tmpdirname						)

     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=self.get_image_processor(do_normalize=__lowerCAmelCase				,						padding_value=1.0						)

     SCREAMING_SNAKE_CASE_:       Optional[int]   	=SamProcessor.from_pretrained(self.tmpdirname				,						do_normalize=__lowerCAmelCase				,						padding_value=1.0						)

     self.assertEqual(processor.image_processor.to_json_string()				,						image_processor_add_kwargs.to_json_string()						)
     self.assertIsInstance(processor.image_processor				,						__lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=SamProcessor(image_processor=__lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:       int   	=self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE_:       Any   	=image_processor(__lowerCAmelCase				,						return_tensors="""np"""						)
     SCREAMING_SNAKE_CASE_:       Any   	=processor(images=__lowerCAmelCase				,						return_tensors="""np"""						)

     input_feat_extract.pop("""original_sizes"""						)  # pop original_sizes as it is popped in the processor
     input_feat_extract.pop("""reshaped_input_sizes"""						)  # pop original_sizes as it is popped in the processor

     for key in input_feat_extract.keys():
        self.assertAlmostEqual(input_feat_extract[key].sum()				,						input_processor[key].sum()				,						delta=1E-2						)





  @require_torch
  def 		lowerCamelCase__				(  self       :    Any						)   ->    Optional[int]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Tuple   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       Optional[int]   	=SamProcessor(image_processor=__lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:       List[str]   	=[torch.ones((1, 3, 5, 5)						)]

     SCREAMING_SNAKE_CASE_:       str   	=[[1764, 2646]]

     SCREAMING_SNAKE_CASE_:       Tuple   	=[[683, 1024]]
     SCREAMING_SNAKE_CASE_:       int   	=processor.post_process_masks(__lowerCAmelCase				,						__lowerCAmelCase				,						__lowerCAmelCase						)
     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=processor.post_process_masks(
         __lowerCAmelCase				,						torch.tensor(__lowerCAmelCase						)				,						torch.tensor(__lowerCAmelCase						)						)
     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     # should also work with np
     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=[np.ones((1, 3, 5, 5)						)]
     SCREAMING_SNAKE_CASE_:       List[Any]   	=processor.post_process_masks(__lowerCAmelCase				,						np.array(__lowerCAmelCase						)				,						np.array(__lowerCAmelCase						)						)

     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     SCREAMING_SNAKE_CASE_:       Dict   	=[[1, 0], [0, 1]]
     with self.assertRaises(__lowerCAmelCase						):
        SCREAMING_SNAKE_CASE_:       str   	=processor.post_process_masks(__lowerCAmelCase				,						np.array(__lowerCAmelCase						)				,						np.array(__lowerCAmelCase						)						)







@require_vision
@require_tf
class     a		(     unittest.TestCase				):



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE_:       str   	=SamImageProcessor()
     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=SamProcessor(__lowerCAmelCase						)
     processor.save_pretrained(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Optional[Any]				,						**lowerCAmelCase       :    str						)   ->    Union[str, Any]:


     '''simple docstring'''




     return AutoProcessor.from_pretrained(self.tmpdirname				,						**__lowerCAmelCase						).image_processor



  def 		lowerCamelCase__				(  self       :    int						)   ->    Any:


     '''simple docstring'''




     shutil.rmtree(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Any						)   ->    Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Any   	=[np.random.randint(255				,						size=(3, 30, 400)				,						dtype=np.uinta						)]

     SCREAMING_SNAKE_CASE_:       int   	=[Image.fromarray(np.moveaxis(__lowerCAmelCase				,						0				,						-1						)						) for x in image_inputs]

     return image_inputs



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Dict   	=SamProcessor(image_processor=self.get_image_processor()						)
     processor.save_pretrained(self.tmpdirname						)

     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=self.get_image_processor(do_normalize=__lowerCAmelCase				,						padding_value=1.0						)

     SCREAMING_SNAKE_CASE_:       Any   	=SamProcessor.from_pretrained(self.tmpdirname				,						do_normalize=__lowerCAmelCase				,						padding_value=1.0						)

     self.assertEqual(processor.image_processor.to_json_string()				,						image_processor_add_kwargs.to_json_string()						)
     self.assertIsInstance(processor.image_processor				,						__lowerCAmelCase						)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       int   	=SamProcessor(image_processor=__lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:       str   	=self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE_:       Tuple   	=image_processor(__lowerCAmelCase				,						return_tensors="""np"""						)
     SCREAMING_SNAKE_CASE_:       str   	=processor(images=__lowerCAmelCase				,						return_tensors="""np"""						)

     input_feat_extract.pop("""original_sizes"""						)  # pop original_sizes as it is popped in the processor
     input_feat_extract.pop("""reshaped_input_sizes"""						)  # pop reshaped_input_sizes as it is popped in the processor

     for key in input_feat_extract.keys():
        self.assertAlmostEqual(input_feat_extract[key].sum()				,						input_processor[key].sum()				,						delta=1E-2						)





  @require_tf
  def 		lowerCamelCase__				(  self       :    Any						)   ->    Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       int   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       Optional[int]   	=SamProcessor(image_processor=__lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=[tf.ones((1, 3, 5, 5)						)]

     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=[[1764, 2646]]

     SCREAMING_SNAKE_CASE_:       str   	=[[683, 1024]]
     SCREAMING_SNAKE_CASE_:       Any   	=processor.post_process_masks(__lowerCAmelCase				,						__lowerCAmelCase				,						__lowerCAmelCase				,						return_tensors="""tf"""						)
     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     SCREAMING_SNAKE_CASE_:       List[Any]   	=processor.post_process_masks(
         __lowerCAmelCase				,						tf.convert_to_tensor(__lowerCAmelCase						)				,						tf.convert_to_tensor(__lowerCAmelCase						)				,						return_tensors="""tf"""				,						)
     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     # should also work with np
     SCREAMING_SNAKE_CASE_:       Any   	=[np.ones((1, 3, 5, 5)						)]
     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=processor.post_process_masks(
         __lowerCAmelCase				,						np.array(__lowerCAmelCase						)				,						np.array(__lowerCAmelCase						)				,						return_tensors="""tf"""						)

     self.assertEqual(masks[0].shape				,						(1, 3, 1764, 2646)						)

     SCREAMING_SNAKE_CASE_:       Optional[int]   	=[[1, 0], [0, 1]]
     with self.assertRaises(tf.errors.InvalidArgumentError						):
        SCREAMING_SNAKE_CASE_:       List[Any]   	=processor.post_process_masks(
            __lowerCAmelCase				,						np.array(__lowerCAmelCase						)				,						np.array(__lowerCAmelCase						)				,						return_tensors="""tf"""						)







@require_vision
@require_torchvision
class     a		(     unittest.TestCase				):



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE_:       Union[str, Any]   	=SamImageProcessor()
     SCREAMING_SNAKE_CASE_:       Tuple   	=SamProcessor(__lowerCAmelCase						)
     processor.save_pretrained(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Optional[int]				,						**lowerCAmelCase       :    Optional[int]						)   ->    Optional[Any]:


     '''simple docstring'''




     return AutoProcessor.from_pretrained(self.tmpdirname				,						**__lowerCAmelCase						).image_processor



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


     '''simple docstring'''




     shutil.rmtree(self.tmpdirname						)



  def 		lowerCamelCase__				(  self       :    Union[str, Any]						)   ->    Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       Optional[int]   	=[np.random.randint(255				,						size=(3, 30, 400)				,						dtype=np.uinta						)]

     SCREAMING_SNAKE_CASE_:       str   	=[Image.fromarray(np.moveaxis(__lowerCAmelCase				,						0				,						-1						)						) for x in image_inputs]

     return image_inputs



  @is_pt_tf_cross_test
  def 		lowerCamelCase__				(  self       :    List[Any]						)   ->    Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       str   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       Any   	=SamProcessor(image_processor=__lowerCAmelCase						)
     SCREAMING_SNAKE_CASE_:       Tuple   	=np.random.randint(0				,						2				,						size=(1, 3, 5, 5)						).astype(np.floataa						)
     SCREAMING_SNAKE_CASE_:       Optional[int]   	=[tf.convert_to_tensor(__lowerCAmelCase						)]
     SCREAMING_SNAKE_CASE_:       str   	=[torch.tensor(__lowerCAmelCase						)]

     SCREAMING_SNAKE_CASE_:       Any   	=[[1764, 2646]]

     SCREAMING_SNAKE_CASE_:       str   	=[[683, 1024]]
     SCREAMING_SNAKE_CASE_:       str   	=processor.post_process_masks(
         __lowerCAmelCase				,						__lowerCAmelCase				,						__lowerCAmelCase				,						return_tensors="""tf"""						)
     SCREAMING_SNAKE_CASE_:       List[str]   	=processor.post_process_masks(
         __lowerCAmelCase				,						__lowerCAmelCase				,						__lowerCAmelCase				,						return_tensors="""pt"""						)

     self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()						)						)





  @is_pt_tf_cross_test
  def 		lowerCamelCase__				(  self       :    List[str]						)   ->    Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_:       str   	=self.get_image_processor()

     SCREAMING_SNAKE_CASE_:       str   	=SamProcessor(image_processor=__lowerCAmelCase						)

     SCREAMING_SNAKE_CASE_:       List[str]   	=self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE_:       Dict   	=image_processor(__lowerCAmelCase				,						return_tensors="""pt"""						)["""pixel_values"""].numpy()
     SCREAMING_SNAKE_CASE_:       int   	=processor(images=__lowerCAmelCase				,						return_tensors="""pt"""						)["""pixel_values"""].numpy()

     SCREAMING_SNAKE_CASE_:       Optional[int]   	=image_processor(__lowerCAmelCase				,						return_tensors="""tf"""						)["""pixel_values"""].numpy()
     SCREAMING_SNAKE_CASE_:       Optional[Any]   	=processor(images=__lowerCAmelCase				,						return_tensors="""tf"""						)["""pixel_values"""].numpy()

     self.assertTrue(np.allclose(__lowerCAmelCase				,						__lowerCAmelCase						)						)
     self.assertTrue(np.allclose(__lowerCAmelCase				,						__lowerCAmelCase						)						)
     self.assertTrue(np.allclose(__lowerCAmelCase				,						__lowerCAmelCase						)						)




'''simple docstring'''




from __future__ import annotations

from typing import Dict

from ...configuration_utils import PretrainedConfig


__lowercase          =  {
    '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''',
    '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''',
}






class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     List[str]      =       '''ernie_m'''
			UpperCAmelCase_			:     Dict[str, str]      =       {"dropout": "classifier_dropout", "num_classes": "num_labels"}



			def __init__(   self       ,    __lowerCAmelCase = 250002       ,    __lowerCAmelCase = 768       ,    __lowerCAmelCase = 12       ,    __lowerCAmelCase = 12       ,    __lowerCAmelCase = 3072       ,    __lowerCAmelCase = "gelu"       ,    __lowerCAmelCase = 0.1       ,    __lowerCAmelCase = 0.1       ,    __lowerCAmelCase = 514       ,    __lowerCAmelCase = 0.02       ,    __lowerCAmelCase = 1       ,    __lowerCAmelCase = 1E-0_5       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=False       ,    __lowerCAmelCase=0.0       ,    **__lowerCAmelCase       ,    ):




					"""simple docstring"""


					super().__init__(pad_token_id=__lowerCAmelCase       ,    **__lowerCAmelCase)
					lowerCAmelCase   					=  vocab_size
					lowerCAmelCase   					=  hidden_size
					lowerCAmelCase   					=  num_hidden_layers
					lowerCAmelCase   					=  num_attention_heads
					lowerCAmelCase   					=  intermediate_size
					lowerCAmelCase   					=  hidden_act
					lowerCAmelCase   					=  hidden_dropout_prob
					lowerCAmelCase   					=  attention_probs_dropout_prob
					lowerCAmelCase   					=  max_position_embeddings
					lowerCAmelCase   					=  initializer_range
					lowerCAmelCase   					=  layer_norm_eps
					lowerCAmelCase   					=  classifier_dropout
					lowerCAmelCase   					=  is_decoder
					lowerCAmelCase   					=  act_dropout




