



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

					"""simple docstring"""






					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   Optional[Any]													=					tempfile.mkdtemp()
									lowercase__					:   Any													=					SamImageProcessor()
									lowercase__					:   List[Any]													=					SamProcessor(SCREAMING_SNAKE_CASE					)
									processor.save_pretrained(self.tmpdirname					)







					def 			snake_case					(							self :     Optional[int]				,       **SCREAMING_SNAKE_CASE :     List[str]					):
									return AutoProcessor.from_pretrained(self.tmpdirname				,       **SCREAMING_SNAKE_CASE					).image_processor







					def 			snake_case					(							self :     List[Any]					):
									shutil.rmtree(self.tmpdirname					)







					def 			snake_case					(							self :     int					):
									lowercase__					:   List[str]													=					[np.random.randint(255				,       size=(3, 30, 400)				,       dtype=np.uinta					)]

									lowercase__					:   Dict													=					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE				,       0				,       -1					)					) for x in image_inputs]

									return image_inputs







					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   Optional[Any]													=					SamProcessor(image_processor=self.get_image_processor()					)
									processor.save_pretrained(self.tmpdirname					)

									lowercase__					:   Optional[Any]													=					self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE				,       padding_value=1.0					)

									lowercase__					:   List[Any]													=					SamProcessor.from_pretrained(self.tmpdirname				,       do_normalize=SCREAMING_SNAKE_CASE				,       padding_value=1.0					)

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







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   int													=					self.get_image_processor()

									lowercase__					:   Dict													=					SamProcessor(image_processor=SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   List[str]													=					image_processor(SCREAMING_SNAKE_CASE				,       return_tensors="np"					)
									lowercase__					:   List[Any]													=					processor(images=SCREAMING_SNAKE_CASE				,       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 :     str					):
									lowercase__					:   Union[str, Any]													=					self.get_image_processor()

									lowercase__					:   Any													=					SamProcessor(image_processor=SCREAMING_SNAKE_CASE					)
									lowercase__					:   Tuple													=					[torch.ones((1, 3, 5, 5)					)]

									lowercase__					:   List[Any]													=					[[1_764, 2_646]]

									lowercase__					:   str													=					[[683, 1_024]]
									lowercase__					:   List[str]													=					processor.post_process_masks(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)
									self.assertEqual(masks[0].shape				,       (1, 3, 1_764, 2_646)					)

									lowercase__					:   Dict													=					processor.post_process_masks(
									    SCREAMING_SNAKE_CASE				,       torch.tensor(SCREAMING_SNAKE_CASE					)				,       torch.tensor(SCREAMING_SNAKE_CASE					)					)
									self.assertEqual(masks[0].shape				,       (1, 3, 1_764, 2_646)					)

									# should also work with np
									lowercase__					:   Tuple													=					[np.ones((1, 3, 5, 5)					)]
									lowercase__					:   Optional[Any]													=					processor.post_process_masks(SCREAMING_SNAKE_CASE				,       np.array(SCREAMING_SNAKE_CASE					)				,       np.array(SCREAMING_SNAKE_CASE					)					)

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

									lowercase__					:   Optional[Any]													=					[[1, 0], [0, 1]]
									with self.assertRaises(SCREAMING_SNAKE_CASE					):
													lowercase__					:   Any													=					processor.post_process_masks(SCREAMING_SNAKE_CASE				,       np.array(SCREAMING_SNAKE_CASE					)				,       np.array(SCREAMING_SNAKE_CASE					)					)






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

					"""simple docstring"""






					def 			snake_case					(							self :     str					):
									lowercase__					:   Optional[Any]													=					tempfile.mkdtemp()
									lowercase__					:   Tuple													=					SamImageProcessor()
									lowercase__					:   str													=					SamProcessor(SCREAMING_SNAKE_CASE					)
									processor.save_pretrained(self.tmpdirname					)







					def 			snake_case					(							self :     Dict				,       **SCREAMING_SNAKE_CASE :     List[str]					):
									return AutoProcessor.from_pretrained(self.tmpdirname				,       **SCREAMING_SNAKE_CASE					).image_processor







					def 			snake_case					(							self :     Dict					):
									shutil.rmtree(self.tmpdirname					)







					def 			snake_case					(							self :     str					):
									lowercase__					:   Dict													=					[np.random.randint(255				,       size=(3, 30, 400)				,       dtype=np.uinta					)]

									lowercase__					:   Dict													=					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE				,       0				,       -1					)					) for x in image_inputs]

									return image_inputs







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   str													=					SamProcessor(image_processor=self.get_image_processor()					)
									processor.save_pretrained(self.tmpdirname					)

									lowercase__					:   Dict													=					self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE				,       padding_value=1.0					)

									lowercase__					:   Union[str, Any]													=					SamProcessor.from_pretrained(self.tmpdirname				,       do_normalize=SCREAMING_SNAKE_CASE				,       padding_value=1.0					)

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







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Tuple													=					self.get_image_processor()

									lowercase__					:   str													=					SamProcessor(image_processor=SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   str													=					image_processor(SCREAMING_SNAKE_CASE				,       return_tensors="np"					)
									lowercase__					:   List[Any]													=					processor(images=SCREAMING_SNAKE_CASE				,       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 :     int					):
									lowercase__					:   Optional[Any]													=					self.get_image_processor()

									lowercase__					:   List[str]													=					SamProcessor(image_processor=SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[int]													=					[tf.ones((1, 3, 5, 5)					)]

									lowercase__					:   str													=					[[1_764, 2_646]]

									lowercase__					:   Optional[Any]													=					[[683, 1_024]]
									lowercase__					:   Any													=					processor.post_process_masks(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_tensors="tf"					)
									self.assertEqual(masks[0].shape				,       (1, 3, 1_764, 2_646)					)

									lowercase__					:   Dict													=					processor.post_process_masks(
									    SCREAMING_SNAKE_CASE				,       tf.convert_to_tensor(SCREAMING_SNAKE_CASE					)				,       tf.convert_to_tensor(SCREAMING_SNAKE_CASE					)				,       return_tensors="tf"				,       )
									self.assertEqual(masks[0].shape				,       (1, 3, 1_764, 2_646)					)

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

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

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






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

					"""simple docstring"""






					def 			snake_case					(							self :     List[Any]					):
									lowercase__					:   List[str]													=					tempfile.mkdtemp()
									lowercase__					:   str													=					SamImageProcessor()
									lowercase__					:   Optional[Any]													=					SamProcessor(SCREAMING_SNAKE_CASE					)
									processor.save_pretrained(self.tmpdirname					)







					def 			snake_case					(							self :     str				,       **SCREAMING_SNAKE_CASE :     List[str]					):
									return AutoProcessor.from_pretrained(self.tmpdirname				,       **SCREAMING_SNAKE_CASE					).image_processor







					def 			snake_case					(							self :     Optional[Any]					):
									shutil.rmtree(self.tmpdirname					)







					def 			snake_case					(							self :     Optional[Any]					):
									lowercase__					:   List[Any]													=					[np.random.randint(255				,       size=(3, 30, 400)				,       dtype=np.uinta					)]

									lowercase__					:   Any													=					[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE				,       0				,       -1					)					) for x in image_inputs]

									return image_inputs







					@is_pt_tf_cross_test
					def 			snake_case					(							self :     Tuple					):
									lowercase__					:   List[str]													=					self.get_image_processor()

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

									lowercase__					:   Tuple													=					[[1_764, 2_646]]

									lowercase__					:   Dict													=					[[683, 1_024]]
									lowercase__					:   Optional[int]													=					processor.post_process_masks(
									    SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_tensors="tf"					)
									lowercase__					:   int													=					processor.post_process_masks(
									    SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)

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







					@is_pt_tf_cross_test
					def 			snake_case					(							self :     str					):
									lowercase__					:   Union[str, Any]													=					self.get_image_processor()

									lowercase__					:   List[str]													=					SamProcessor(image_processor=SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   int													=					image_processor(SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)["pixel_values"].numpy()
									lowercase__					:   List[Any]													=					processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)["pixel_values"].numpy()

									lowercase__					:   Optional[Any]													=					image_processor(SCREAMING_SNAKE_CASE				,       return_tensors="tf"					)["pixel_values"].numpy()
									lowercase__					:   List[str]													=					processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="tf"					)["pixel_values"].numpy()

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









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase__					  =  {
    '''configuration_deberta''': ['''DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DebertaConfig''', '''DebertaOnnxConfig'''],
    '''tokenization_deberta''': ['''DebertaTokenizer'''],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowerCAmelCase__					  =  ['''DebertaTokenizerFast''']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowerCAmelCase__					  =  [
				    '''DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''DebertaForMaskedLM''',
				    '''DebertaForQuestionAnswering''',
				    '''DebertaForSequenceClassification''',
				    '''DebertaForTokenClassification''',
				    '''DebertaModel''',
				    '''DebertaPreTrainedModel''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowerCAmelCase__					  =  [
				    '''TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFDebertaForMaskedLM''',
				    '''TFDebertaForQuestionAnswering''',
				    '''TFDebertaForSequenceClassification''',
				    '''TFDebertaForTokenClassification''',
				    '''TFDebertaModel''',
				    '''TFDebertaPreTrainedModel''',
				]


if TYPE_CHECKING:
				from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig
				from .tokenization_deberta import DebertaTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_deberta_fast import DebertaTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_deberta import (
								    DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
								    DebertaForMaskedLM,
								    DebertaForQuestionAnswering,
								    DebertaForSequenceClassification,
								    DebertaForTokenClassification,
								    DebertaModel,
								    DebertaPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_deberta import (
								    TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFDebertaForMaskedLM,
								    TFDebertaForQuestionAnswering,
								    TFDebertaForSequenceClassification,
								    TFDebertaForTokenClassification,
								    TFDebertaModel,
								    TFDebertaPreTrainedModel,
								)


else:
				import sys

				lowerCAmelCase__					  =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




