


from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_MODEL_FOR_PRETRAINING_MAPPING,
				    TFMobileBertForMaskedLM,
				    TFMobileBertForMultipleChoice,
				    TFMobileBertForNextSentencePrediction,
				    TFMobileBertForPreTraining,
				    TFMobileBertForQuestionAnswering,
				    TFMobileBertForSequenceClassification,
				    TFMobileBertForTokenClassification,
				    TFMobileBertModel,
				)



@require_tf
class 							_UpperCamelCase (						SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_,unittest.TestCase						):

			'''simple docstring'''







			a_				:							Union[str, Any]    = (
			    (
			        TFMobileBertModel,
			        TFMobileBertForMaskedLM,
			        TFMobileBertForNextSentencePrediction,
			        TFMobileBertForPreTraining,
			        TFMobileBertForQuestionAnswering,
			        TFMobileBertForSequenceClassification,
			        TFMobileBertForTokenClassification,
			        TFMobileBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			a_				:							List[str]    = (
			    {
			        "feature-extraction": TFMobileBertModel,
			        "fill-mask": TFMobileBertForMaskedLM,
			        "question-answering": TFMobileBertForQuestionAnswering,
			        "text-classification": TFMobileBertForSequenceClassification,
			        "token-classification": TFMobileBertForTokenClassification,
			        "zero-shot": TFMobileBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			a_				:							Dict    = False
			a_				:							Optional[int]    = False

			def   _snake_case    (  self  : List[str]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : List[str]=False							):






									'''simple docstring'''



									__lowerCamelCase     :      List[str]       						=						super()._prepare_for_class(UpperCamelCase__       ,  UpperCamelCase__       ,  return_labels=UpperCamelCase__							)

									if return_labels:
															if model_class in get_values(UpperCamelCase__							):
																					__lowerCamelCase     :      Tuple       						=						tf.zeros(self.model_tester.batch_size       ,  dtype=tf.intaa							)

									return inputs_dict



			class 							_UpperCamelCase (						SCREAMING_SNAKE_CASE_						):

						'''simple docstring'''







						def __init__(  self  : Any       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Tuple=1_3       ,  _lowerCamelCase  : Optional[int]=7       ,  _lowerCamelCase  : Any=True       ,  _lowerCamelCase  : Tuple=True       ,  _lowerCamelCase  : List[str]=True       ,  _lowerCamelCase  : Optional[Any]=True       ,  _lowerCamelCase  : Dict=9_9       ,  _lowerCamelCase  : int=3_2       ,  _lowerCamelCase  : List[Any]=3_2       ,  _lowerCamelCase  : Dict=2       ,  _lowerCamelCase  : str=4       ,  _lowerCamelCase  : str=3_7       ,  _lowerCamelCase  : Optional[Any]="gelu"       ,  _lowerCamelCase  : Optional[int]=0.1       ,  _lowerCamelCase  : Dict=0.1       ,  _lowerCamelCase  : Dict=5_1_2       ,  _lowerCamelCase  : str=1_6       ,  _lowerCamelCase  : Optional[Any]=2       ,  _lowerCamelCase  : List[Any]=0.02       ,  _lowerCamelCase  : List[Any]=3       ,  _lowerCamelCase  : Union[str, Any]=4       ,  _lowerCamelCase  : str=None       ,  ):






												'''simple docstring'''



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

						def   _snake_case    (  self  : Any							):






												'''simple docstring'''



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

												__lowerCamelCase     :      Optional[Any]       						=						None
												if self.use_input_mask:
																		__lowerCamelCase     :      List[Any]       						=						random_attention_mask([self.batch_size, self.seq_length]							)

												__lowerCamelCase     :      Optional[int]       						=						None
												if self.use_token_type_ids:
																		__lowerCamelCase     :      str       						=						ids_tensor([self.batch_size, self.seq_length]       ,  self.type_vocab_size							)

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

												__lowerCamelCase     :      Dict       						=						MobileBertConfig(
												    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       ,  embedding_size=self.embedding_size       ,  )

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

						def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Dict							):






												'''simple docstring'''



												__lowerCamelCase     :      str       						=						TFMobileBertModel(config=UpperCamelCase__							)
												__lowerCamelCase     :      Optional[int]       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      List[str]       						=						model(UpperCamelCase__							)

												__lowerCamelCase     :      List[Any]       						=						[input_ids, input_mask]
												__lowerCamelCase     :      Optional[Any]       						=						model(UpperCamelCase__							)

												__lowerCamelCase     :      Tuple       						=						model(UpperCamelCase__							)

												self.parent.assertEqual(
												    result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)							)
												self.parent.assertEqual(result.pooler_output.shape       ,  (self.batch_size, self.hidden_size)							)

						def   _snake_case    (  self  : List[str]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : str							):






												'''simple docstring'''



												__lowerCamelCase     :      int       						=						TFMobileBertForMaskedLM(config=UpperCamelCase__							)
												__lowerCamelCase     :      Any       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      List[str]       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)							)

						def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : List[str]							):






												'''simple docstring'''



												__lowerCamelCase     :      Union[str, Any]       						=						TFMobileBertForNextSentencePrediction(config=UpperCamelCase__							)
												__lowerCamelCase     :      Union[str, Any]       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      Dict       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, 2)							)

						def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Union[str, Any]							):






												'''simple docstring'''



												__lowerCamelCase     :      int       						=						TFMobileBertForPreTraining(config=UpperCamelCase__							)
												__lowerCamelCase     :      List[str]       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      int       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(
												    result.prediction_logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)							)
												self.parent.assertEqual(result.seq_relationship_logits.shape       ,  (self.batch_size, 2)							)

						def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Optional[Any]							):






												'''simple docstring'''



												__lowerCamelCase     :      str       						=						self.num_labels
												__lowerCamelCase     :      int       						=						TFMobileBertForSequenceClassification(config=UpperCamelCase__							)
												__lowerCamelCase     :      List[str]       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      Union[str, Any]       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)							)

						def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Optional[int]							):






												'''simple docstring'''



												__lowerCamelCase     :      Optional[int]       						=						self.num_choices
												__lowerCamelCase     :      str       						=						TFMobileBertForMultipleChoice(config=UpperCamelCase__							)
												__lowerCamelCase     :      Any       						=						tf.tile(tf.expand_dims(UpperCamelCase__       ,  1							)       ,  (1, self.num_choices, 1)							)
												__lowerCamelCase     :      Dict       						=						tf.tile(tf.expand_dims(UpperCamelCase__       ,  1							)       ,  (1, self.num_choices, 1)							)
												__lowerCamelCase     :      List[Any]       						=						tf.tile(tf.expand_dims(UpperCamelCase__       ,  1							)       ,  (1, self.num_choices, 1)							)
												__lowerCamelCase     :      Tuple       						=						{
												    '''input_ids''': multiple_choice_inputs_ids,
												    '''attention_mask''': multiple_choice_input_mask,
												    '''token_type_ids''': multiple_choice_token_type_ids,
												}
												__lowerCamelCase     :      Optional[Any]       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices)							)

						def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : str							):






												'''simple docstring'''



												__lowerCamelCase     :      Tuple       						=						self.num_labels
												__lowerCamelCase     :      Optional[int]       						=						TFMobileBertForTokenClassification(config=UpperCamelCase__							)
												__lowerCamelCase     :      int       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      Optional[Any]       						=						model(UpperCamelCase__							)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)							)

						def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : Dict							):






												'''simple docstring'''



												__lowerCamelCase     :      Dict       						=						TFMobileBertForQuestionAnswering(config=UpperCamelCase__							)
												__lowerCamelCase     :      str       						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
												__lowerCamelCase     :      Optional[Any]       						=						model(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   _snake_case    (  self  : List[str]							):






												'''simple docstring'''



												__lowerCamelCase     :      List[str]       						=						self.prepare_config_and_inputs()
												(
												    __lowerCamelCase							
												)     :      Optional[int]       						=						config_and_inputs
												__lowerCamelCase     :      str       						=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
												return config, inputs_dict

			def   _snake_case    (  self  : Optional[int]							):






									'''simple docstring'''



									__lowerCamelCase     :      str       						=						TFMobileBertModelTest.TFMobileBertModelTester(self							)
									__lowerCamelCase     :      Tuple       						=						ConfigTester(self       ,  config_class=UpperCamelCase__       ,  hidden_size=3_7							)

			def   _snake_case    (  self  : Optional[int]							):






									'''simple docstring'''



									self.config_tester.run_common_tests()

			def   _snake_case    (  self  : int							):






									'''simple docstring'''



									__lowerCamelCase     :      List[str]       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase__							)

			def   _snake_case    (  self  : List[str]							):






									'''simple docstring'''



									__lowerCamelCase     :      Union[str, Any]       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase__							)

			def   _snake_case    (  self  : Dict							):






									'''simple docstring'''



									__lowerCamelCase     :      int       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase__							)

			def   _snake_case    (  self  : Tuple							):






									'''simple docstring'''



									__lowerCamelCase     :      Any       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase__							)

			def   _snake_case    (  self  : List[Any]							):






									'''simple docstring'''



									__lowerCamelCase     :      Tuple       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase__							)

			def   _snake_case    (  self  : Any							):






									'''simple docstring'''



									__lowerCamelCase     :      Any       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase__							)

			def   _snake_case    (  self  : Dict							):






									'''simple docstring'''



									__lowerCamelCase     :      List[str]       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase__							)

			def   _snake_case    (  self  : Tuple							):






									'''simple docstring'''



									__lowerCamelCase     :      List[str]       						=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase__							)


			@slow
			def   _snake_case    (  self  : List[str]							):






									'''simple docstring'''



									for model_name in ["google/mobilebert-uncased"]:
															__lowerCamelCase     :      int       						=						TFMobileBertModel.from_pretrained(UpperCamelCase__							)
															self.assertIsNotNone(UpperCamelCase__							)



@require_tf
class 							_UpperCamelCase (						unittest.TestCase						):

			'''simple docstring'''







			@slow
			def   _snake_case    (  self  : Union[str, Any]							):






									'''simple docstring'''



									__lowerCamelCase     :      Union[str, Any]       						=						TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""							)
									__lowerCamelCase     :      Union[str, Any]       						=						tf.constant([[0, 1, 2, 3, 4, 5]]							)
									__lowerCamelCase     :      List[str]       						=						model(UpperCamelCase__							)[0]

									__lowerCamelCase     :      int       						=						[1, 6, 3_0_5_2_2]
									self.assertEqual(output.shape       ,  UpperCamelCase__							)

									__lowerCamelCase     :      Union[str, Any]       						=						tf.constant(
									    [
									        [
									            [-4.5_919_547, -9.248_295, -9.645_256],
									            [-6.7_306_175, -6.440_284, -6.6_052_837],
									            [-7.2_743_506, -6.7_847_915, -6.024_673],
									        ]
									    ]							)
									tf.debugging.assert_near(output[:, :3, :3]       ,  UpperCamelCase__       ,  atol=1E-4							)







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 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      int       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Tuple       ,  **_lowerCamelCase  : Any							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

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

         return image_inputs

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Optional[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   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[str]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      List[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   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.get_image_processor()

         __lowerCamelCase     :      Union[str, Any]       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						[torch.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Any       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Any       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Tuple       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  torch.tensor(_lowerCamelCase							)       ,  torch.tensor(_lowerCamelCase							)							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Any       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      List[str]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(_lowerCamelCase							):
               __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)



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

   '''simple docstring'''







   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      int       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]       ,  **_lowerCamelCase  : List[Any]							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

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

         return image_inputs

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Dict       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Tuple       						=						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   _snake_case    (  self  : str							):






         '''simple docstring'''



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

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      Union[str, 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 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   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      str       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						[tf.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Optional[Any]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      List[str]       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Optional[int]       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Optional[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, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Tuple       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      str       						=						processor.post_process_masks(
             _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError							):
               __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
                   _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)



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

   '''simple docstring'''







   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

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

         return image_inputs

   @is_pt_tf_cross_test
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



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

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						np.random.randint(0       ,  2       ,  size=(1, 3, 5, 5)							).astype(np.floataa							)
         __lowerCamelCase     :      Tuple       						=						[tf.convert_to_tensor(_lowerCamelCase							)]
         __lowerCamelCase     :      List[str]       						=						[torch.tensor(_lowerCamelCase							)]

         __lowerCamelCase     :      Optional[int]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Tuple       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      Any       						=						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   _snake_case    (  self  : Dict							):






         '''simple docstring'''



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

         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      Any       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      Any       						=						processor(images=_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()

         __lowerCamelCase     :      Dict       						=						image_processor(_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      int       						=						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							)							)



