





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      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       tempfile.mkdtemp()
      __SCREAMING_SNAKE_CASE      							=       SamImageProcessor()
      __SCREAMING_SNAKE_CASE      							=       SamProcessor(lowerCamelCase    )
      processor.save_pretrained(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Any				,**lowerCamelCase				:			Optional[int]    ):

      '''simple docstring'''

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





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      shutil.rmtree(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Dict    ):

      '''simple docstring'''

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

      __SCREAMING_SNAKE_CASE      							=       [Image.fromarray(np.moveaxis(lowerCamelCase				,0				,-1    )    ) for x in image_inputs]

      return image_inputs





   def      UpperCAmelCase__     (					self				:			str    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=self.get_image_processor()    )
      processor.save_pretrained(self.tmpdirname    )

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor(do_normalize=lowerCamelCase				,padding_value=1.0    )

      __SCREAMING_SNAKE_CASE      							=       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      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       image_processor(lowerCamelCase				,return_tensors="""np"""    )
      __SCREAMING_SNAKE_CASE      							=       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      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       [torch.ones((1, 3, 5, 5)    )]

      __SCREAMING_SNAKE_CASE      							=       [[1764, 2646]]

      __SCREAMING_SNAKE_CASE      							=       [[683, 1024]]
      __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(lowerCamelCase				,lowerCamelCase				,lowerCamelCase    )
      self.assertEqual(masks[0].shape				,(1, 3, 1764, 2646)    )

      __SCREAMING_SNAKE_CASE      							=       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      							=       [np.ones((1, 3, 5, 5)    )]
      __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(lowerCamelCase				,np.array(lowerCamelCase    )				,np.array(lowerCamelCase    )    )

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

      __SCREAMING_SNAKE_CASE      							=       [[1, 0], [0, 1]]
      with self.assertRaises(lowerCamelCase    ):
         __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(lowerCamelCase				,np.array(lowerCamelCase    )				,np.array(lowerCamelCase    )    )







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





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       tempfile.mkdtemp()
      __SCREAMING_SNAKE_CASE      							=       SamImageProcessor()
      __SCREAMING_SNAKE_CASE      							=       SamProcessor(lowerCamelCase    )
      processor.save_pretrained(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Union[str, Any]				,**lowerCamelCase				:			List[Any]    ):

      '''simple docstring'''

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





   def      UpperCAmelCase__     (					self				:			Any    ):

      '''simple docstring'''

      shutil.rmtree(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

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

      __SCREAMING_SNAKE_CASE      							=       [Image.fromarray(np.moveaxis(lowerCamelCase				,0				,-1    )    ) for x in image_inputs]

      return image_inputs





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=self.get_image_processor()    )
      processor.save_pretrained(self.tmpdirname    )

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor(do_normalize=lowerCamelCase				,padding_value=1.0    )

      __SCREAMING_SNAKE_CASE      							=       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      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       image_processor(lowerCamelCase				,return_tensors="""np"""    )
      __SCREAMING_SNAKE_CASE      							=       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      UpperCAmelCase__     (					self				:			Any    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       [tf.ones((1, 3, 5, 5)    )]

      __SCREAMING_SNAKE_CASE      							=       [[1764, 2646]]

      __SCREAMING_SNAKE_CASE      							=       [[683, 1024]]
      __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(lowerCamelCase				,lowerCamelCase				,lowerCamelCase				,return_tensors="""tf"""    )
      self.assertEqual(masks[0].shape				,(1, 3, 1764, 2646)    )

      __SCREAMING_SNAKE_CASE      							=       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      							=       [np.ones((1, 3, 5, 5)    )]
      __SCREAMING_SNAKE_CASE      							=       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      							=       [[1, 0], [0, 1]]
      with self.assertRaises(tf.errors.InvalidArgumentError    ):
         __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(
             lowerCamelCase				,np.array(lowerCamelCase    )				,np.array(lowerCamelCase    )				,return_tensors="""tf"""    )







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





   def      UpperCAmelCase__     (					self				:			Tuple    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       tempfile.mkdtemp()
      __SCREAMING_SNAKE_CASE      							=       SamImageProcessor()
      __SCREAMING_SNAKE_CASE      							=       SamProcessor(lowerCamelCase    )
      processor.save_pretrained(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Any				,**lowerCamelCase				:			Optional[Any]    ):

      '''simple docstring'''

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





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      shutil.rmtree(self.tmpdirname    )





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

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

      __SCREAMING_SNAKE_CASE      							=       [Image.fromarray(np.moveaxis(lowerCamelCase				,0				,-1    )    ) for x in image_inputs]

      return image_inputs





   @is_pt_tf_cross_test
   def      UpperCAmelCase__     (					self				:			Dict    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       np.random.randint(0				,2				,size=(1, 3, 5, 5)    ).astype(np.floataa    )
      __SCREAMING_SNAKE_CASE      							=       [tf.convert_to_tensor(lowerCamelCase    )]
      __SCREAMING_SNAKE_CASE      							=       [torch.tensor(lowerCamelCase    )]

      __SCREAMING_SNAKE_CASE      							=       [[1764, 2646]]

      __SCREAMING_SNAKE_CASE      							=       [[683, 1024]]
      __SCREAMING_SNAKE_CASE      							=       processor.post_process_masks(
          lowerCamelCase				,lowerCamelCase				,lowerCamelCase				,return_tensors="""tf"""    )
      __SCREAMING_SNAKE_CASE      							=       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      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.get_image_processor()

      __SCREAMING_SNAKE_CASE      							=       SamProcessor(image_processor=lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE      							=       image_processor(lowerCamelCase				,return_tensors="""pt"""    )["pixel_values"].numpy()
      __SCREAMING_SNAKE_CASE      							=       processor(images=lowerCamelCase				,return_tensors="""pt"""    )["pixel_values"].numpy()

      __SCREAMING_SNAKE_CASE      							=       image_processor(lowerCamelCase				,return_tensors="""tf"""    )["pixel_values"].numpy()
      __SCREAMING_SNAKE_CASE      							=       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 collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a        =						logging.get_logger(__name__)

a        =						{
    "camembert-base": "https://huggingface.co/camembert-base/resolve/main/config.json",
    "umberto-commoncrawl-cased-v1": (
        "https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json"
    ),
    "umberto-wikipedia-uncased-v1": (
        "https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json"
    ),
}







class __a (   _snake_case			):
   __UpperCamelCase   : Tuple	      =	'camembert'





   def __init__(					self				:			int				,lowerCamelCase				:			List[Any]=3_0522				,lowerCamelCase				:			List[Any]=768				,lowerCamelCase				:			str=12				,lowerCamelCase				:			List[str]=12				,lowerCamelCase				:			Optional[Any]=3072				,lowerCamelCase				:			Tuple="gelu"				,lowerCamelCase				:			List[str]=0.1				,lowerCamelCase				:			Tuple=0.1				,lowerCamelCase				:			Union[str, Any]=512				,lowerCamelCase				:			Dict=2				,lowerCamelCase				:			Tuple=0.02				,lowerCamelCase				:			List[Any]=1E-1_2				,lowerCamelCase				:			Union[str, Any]=1				,lowerCamelCase				:			Optional[Any]=0				,lowerCamelCase				:			List[Any]=2				,lowerCamelCase				:			List[str]="absolute"				,lowerCamelCase				:			int=True				,lowerCamelCase				:			Any=None				,**lowerCamelCase				:			Optional[Any]				,):

      '''simple docstring'''

      super().__init__(pad_token_id=lowerCamelCase				,bos_token_id=lowerCamelCase				,eos_token_id=lowerCamelCase				,**lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       vocab_size
      __SCREAMING_SNAKE_CASE      							=       hidden_size
      __SCREAMING_SNAKE_CASE      							=       num_hidden_layers
      __SCREAMING_SNAKE_CASE      							=       num_attention_heads
      __SCREAMING_SNAKE_CASE      							=       hidden_act
      __SCREAMING_SNAKE_CASE      							=       intermediate_size
      __SCREAMING_SNAKE_CASE      							=       hidden_dropout_prob
      __SCREAMING_SNAKE_CASE      							=       attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE      							=       max_position_embeddings
      __SCREAMING_SNAKE_CASE      							=       type_vocab_size
      __SCREAMING_SNAKE_CASE      							=       initializer_range
      __SCREAMING_SNAKE_CASE      							=       layer_norm_eps
      __SCREAMING_SNAKE_CASE      							=       position_embedding_type
      __SCREAMING_SNAKE_CASE      							=       use_cache
      __SCREAMING_SNAKE_CASE      							=       classifier_dropout







class __a (   _snake_case			):





   @property
   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      if self.task == "multiple-choice":
         __SCREAMING_SNAKE_CASE      							=       {0: """batch""", 1: """choice""", 2: """sequence"""}
      else:
         __SCREAMING_SNAKE_CASE      							=       {0: """batch""", 1: """sequence"""}
      return OrderedDict(
          [
              ("""input_ids""", dynamic_axis),
              ("""attention_mask""", dynamic_axis),
          ]    )





