
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	:	List[Any]			):
								"""simple docstring"""







								__lowerCAmelCase    =				tempfile.mkdtemp()
								__lowerCAmelCase    =				SamImageProcessor()
								__lowerCAmelCase    =				SamProcessor(lowerCamelCase_			)
								processor.save_pretrained(self.tmpdirname			)







							def    UpperCAmelCase__       (    self	:	Union[str, Any]   ,					**snake_case__	:	List[str]			):
								"""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"""







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

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

								return image_inputs







							def    UpperCAmelCase__       (    self	:	List[str]			):
								"""simple docstring"""







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

								__lowerCAmelCase    =				self.get_image_processor(do_normalize=lowerCamelCase_   ,					padding_value=1.0			)

								__lowerCAmelCase    =				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	:	Tuple			):
								"""simple docstring"""







								__lowerCAmelCase    =				self.get_image_processor()

								__lowerCAmelCase    =				SamProcessor(image_processor=lowerCamelCase_			)

								__lowerCAmelCase    =				self.prepare_image_inputs()

								__lowerCAmelCase    =				image_processor(lowerCamelCase_   ,					return_tensors="np"			)
								__lowerCAmelCase    =				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"""







								__lowerCAmelCase    =				self.get_image_processor()

								__lowerCAmelCase    =				SamProcessor(image_processor=lowerCamelCase_			)
								__lowerCAmelCase    =				[torch.ones((1, 3, 5, 5)			)]

								__lowerCAmelCase    =				[[1_764, 2_646]]

								__lowerCAmelCase    =				[[683, 1_024]]
								__lowerCAmelCase    =				processor.post_process_masks(lowerCamelCase_   ,					lowerCamelCase_   ,					lowerCamelCase_			)
								self.assertEqual(masks[0].shape   ,					(1, 3, 1_764, 2_646)			)

								__lowerCAmelCase    =				processor.post_process_masks(
								    lowerCamelCase_   ,					torch.tensor(lowerCamelCase_			)   ,					torch.tensor(lowerCamelCase_			)			)
								self.assertEqual(masks[0].shape   ,					(1, 3, 1_764, 2_646)			)

								# should also work with np
								__lowerCAmelCase    =				[np.ones((1, 3, 5, 5)			)]
								__lowerCAmelCase    =				processor.post_process_masks(lowerCamelCase_   ,					np.array(lowerCamelCase_			)   ,					np.array(lowerCamelCase_			)			)

								self.assertEqual(masks[0].shape   ,					(1, 3, 1_764, 2_646)			)

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

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







							def    UpperCAmelCase__       (    self	:	Optional[int]			):
								"""simple docstring"""







								__lowerCAmelCase    =				tempfile.mkdtemp()
								__lowerCAmelCase    =				SamImageProcessor()
								__lowerCAmelCase    =				SamProcessor(lowerCamelCase_			)
								processor.save_pretrained(self.tmpdirname			)







							def    UpperCAmelCase__       (    self	:	Dict   ,					**snake_case__	:	List[Any]			):
								"""simple docstring"""







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







							def    UpperCAmelCase__       (    self	:	int			):
								"""simple docstring"""







								shutil.rmtree(self.tmpdirname			)







							def    UpperCAmelCase__       (    self	:	List[str]			):
								"""simple docstring"""







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

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

								return image_inputs







							def    UpperCAmelCase__       (    self	:	List[str]			):
								"""simple docstring"""







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

								__lowerCAmelCase    =				self.get_image_processor(do_normalize=lowerCamelCase_   ,					padding_value=1.0			)

								__lowerCAmelCase    =				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[str]			):
								"""simple docstring"""







								__lowerCAmelCase    =				self.get_image_processor()

								__lowerCAmelCase    =				SamProcessor(image_processor=lowerCamelCase_			)

								__lowerCAmelCase    =				self.prepare_image_inputs()

								__lowerCAmelCase    =				image_processor(lowerCamelCase_   ,					return_tensors="np"			)
								__lowerCAmelCase    =				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	:	Optional[Any]			):
								"""simple docstring"""







								__lowerCAmelCase    =				self.get_image_processor()

								__lowerCAmelCase    =				SamProcessor(image_processor=lowerCamelCase_			)
								__lowerCAmelCase    =				[tf.ones((1, 3, 5, 5)			)]

								__lowerCAmelCase    =				[[1_764, 2_646]]

								__lowerCAmelCase    =				[[683, 1_024]]
								__lowerCAmelCase    =				processor.post_process_masks(lowerCamelCase_   ,					lowerCamelCase_   ,					lowerCamelCase_   ,					return_tensors="tf"			)
								self.assertEqual(masks[0].shape   ,					(1, 3, 1_764, 2_646)			)

								__lowerCAmelCase    =				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, 1_764, 2_646)			)

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

								self.assertEqual(masks[0].shape   ,					(1, 3, 1_764, 2_646)			)

								__lowerCAmelCase    =				[[1, 0], [0, 1]]
								with self.assertRaises(tf.errors.InvalidArgumentError			):
									__lowerCAmelCase    =				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	:	Union[str, Any]			):
								"""simple docstring"""







								__lowerCAmelCase    =				tempfile.mkdtemp()
								__lowerCAmelCase    =				SamImageProcessor()
								__lowerCAmelCase    =				SamProcessor(lowerCamelCase_			)
								processor.save_pretrained(self.tmpdirname			)







							def    UpperCAmelCase__       (    self	:	Union[str, Any]   ,					**snake_case__	:	Union[str, Any]			):
								"""simple docstring"""







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







							def    UpperCAmelCase__       (    self	:	List[str]			):
								"""simple docstring"""







								shutil.rmtree(self.tmpdirname			)







							def    UpperCAmelCase__       (    self	:	int			):
								"""simple docstring"""







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

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

								return image_inputs







							@is_pt_tf_cross_test
							def    UpperCAmelCase__       (    self	:	List[str]			):
								"""simple docstring"""







								__lowerCAmelCase    =				self.get_image_processor()

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

								__lowerCAmelCase    =				[[1_764, 2_646]]

								__lowerCAmelCase    =				[[683, 1_024]]
								__lowerCAmelCase    =				processor.post_process_masks(
								    lowerCamelCase_   ,					lowerCamelCase_   ,					lowerCamelCase_   ,					return_tensors="tf"			)
								__lowerCAmelCase    =				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	:	Optional[Any]			):
								"""simple docstring"""







								__lowerCAmelCase    =				self.get_image_processor()

								__lowerCAmelCase    =				SamProcessor(image_processor=lowerCamelCase_			)

								__lowerCAmelCase    =				self.prepare_image_inputs()

								__lowerCAmelCase    =				image_processor(lowerCamelCase_   ,					return_tensors="pt"			)['''pixel_values'''].numpy()
								__lowerCAmelCase    =				processor(images=lowerCamelCase_   ,					return_tensors="pt"			)['''pixel_values'''].numpy()

								__lowerCAmelCase    =				image_processor(lowerCamelCase_   ,					return_tensors="tf"			)['''pixel_values'''].numpy()
								__lowerCAmelCase    =				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


UpperCamelCase__  :						List[str]					=      logging.get_logger(__name__)

UpperCamelCase__  :						str					=      {
    '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 					_lowercase						(   lowerCAmelCase ):
      '''simple docstring'''


      UpperCAmelCase_   :   Dict   				= '''camembert'''




      def __init__(					self       ,lowerCamelCase_=30522       ,lowerCamelCase_=768       ,lowerCamelCase_=12       ,lowerCamelCase_=12       ,lowerCamelCase_=3072       ,lowerCamelCase_="gelu"       ,lowerCamelCase_=0.1       ,lowerCamelCase_=0.1       ,lowerCamelCase_=512       ,lowerCamelCase_=2       ,lowerCamelCase_=0.02       ,lowerCamelCase_=1e-12       ,lowerCamelCase_=1       ,lowerCamelCase_=0       ,lowerCamelCase_=2       ,lowerCamelCase_="absolute"       ,lowerCamelCase_=True       ,lowerCamelCase_=None       ,**lowerCamelCase_       ,)      ->      str:



         '''simple docstring'''



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

         UpperCAmelCase__				:					int 					=	vocab_size
         UpperCAmelCase__				:					List[Any] 					=	hidden_size
         UpperCAmelCase__				:					Tuple 					=	num_hidden_layers
         UpperCAmelCase__				:					List[str] 					=	num_attention_heads
         UpperCAmelCase__				:					int 					=	hidden_act
         UpperCAmelCase__				:					Tuple 					=	intermediate_size
         UpperCAmelCase__				:					List[Any] 					=	hidden_dropout_prob
         UpperCAmelCase__				:					Tuple 					=	attention_probs_dropout_prob
         UpperCAmelCase__				:					str 					=	max_position_embeddings
         UpperCAmelCase__				:					Union[str, Any] 					=	type_vocab_size
         UpperCAmelCase__				:					int 					=	initializer_range
         UpperCAmelCase__				:					Optional[Any] 					=	layer_norm_eps
         UpperCAmelCase__				:					Optional[Any] 					=	position_embedding_type
         UpperCAmelCase__				:					str 					=	use_cache
         UpperCAmelCase__				:					List[Any] 					=	classifier_dropout




class 					_lowercase						(   lowerCAmelCase ):
      '''simple docstring'''


      @property
      def 		lowerCAmelCase__       (					self				)      ->      Mapping[str, Mapping[int, str]]:



         '''simple docstring'''



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




