


from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
			import numpy as np
			import tensorflow as tf

			from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
			from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor







class 					__lowercase					(       a_     ):



		"""simple docstring"""

		def 				__A      (   self  )     ->   Any:







					'''simple docstring'''







					lowerCamelCase								=     self.config_class(**self.inputs_dict  )
					self.parent.assertTrue(hasattr(A		,			"""hidden_sizes"""  )  )
					self.parent.assertTrue(hasattr(A		,			"""neck_hidden_sizes"""  )  )
					self.parent.assertTrue(hasattr(A		,			"""num_attention_heads"""  )  )







class 					__lowercase					:



		"""simple docstring"""

		def __init__(   self		,			A		,			A=13		,			A=32		,			A=2		,			A=3		,			A=6_40		,			A=4		,			A="silu"		,			A=3		,			A=32		,			A=0.1		,			A=0.1		,			A=0.1		,			A=0.02		,			A=True		,			A=True		,			A=10		,			A=None		,			)     ->   Optional[Any]:







					'''simple docstring'''







					lowerCamelCase								=     parent
					lowerCamelCase								=     batch_size
					lowerCamelCase								=     image_size
					lowerCamelCase								=     patch_size
					lowerCamelCase								=     num_channels
					lowerCamelCase								=     last_hidden_size
					lowerCamelCase								=     num_attention_heads
					lowerCamelCase								=     hidden_act
					lowerCamelCase								=     conv_kernel_size
					lowerCamelCase								=     output_stride
					lowerCamelCase								=     hidden_dropout_prob
					lowerCamelCase								=     attention_probs_dropout_prob
					lowerCamelCase								=     classifier_dropout_prob
					lowerCamelCase								=     use_labels
					lowerCamelCase								=     is_training
					lowerCamelCase								=     num_labels
					lowerCamelCase								=     initializer_range
					lowerCamelCase								=     scope







		def 				__A      (   self  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

					lowerCamelCase								=     None
					lowerCamelCase								=     None
					if self.use_labels:
								lowerCamelCase								=     ids_tensor([self.batch_size]		,			self.num_labels  )
								lowerCamelCase								=     ids_tensor([self.batch_size, self.image_size, self.image_size]		,			self.num_labels  )

					lowerCamelCase								=     self.get_config()

					return config, pixel_values, labels, pixel_labels







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







					'''simple docstring'''







					return MobileViTConfig(
					    image_size=self.image_size		,			patch_size=self.patch_size		,			num_channels=self.num_channels		,			num_attention_heads=self.num_attention_heads		,			hidden_act=self.hidden_act		,			conv_kernel_size=self.conv_kernel_size		,			output_stride=self.output_stride		,			hidden_dropout_prob=self.hidden_dropout_prob		,			attention_probs_dropout_prob=self.attention_probs_dropout_prob		,			classifier_dropout_prob=self.classifier_dropout_prob		,			initializer_range=self.initializer_range		,			)







		def 				__A      (   self		,			A		,			A		,			A		,			A  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     TFMobileViTModel(config=A  )
					lowerCamelCase								=     model(A		,			training=A  )
					lowerCamelCase								=     lowerCamelCase								=     self.image_size // self.output_stride
					self.parent.assertEqual(
					    result.last_hidden_state.shape		,			(self.batch_size, self.last_hidden_size, expected_height, expected_width)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A  )     ->   Optional[Any]:







					'''simple docstring'''







					lowerCamelCase								=     self.num_labels
					lowerCamelCase								=     TFMobileViTForImageClassification(A  )
					lowerCamelCase								=     model(A		,			labels=A		,			training=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A  )     ->   Optional[int]:







					'''simple docstring'''







					lowerCamelCase								=     self.num_labels
					lowerCamelCase								=     TFMobileViTForSemanticSegmentation(A  )
					lowerCamelCase								=     lowerCamelCase								=     self.image_size // self.output_stride

					lowerCamelCase								=     model(A		,			training=A  )
					self.parent.assertEqual(
					    result.logits.shape		,			(self.batch_size, self.num_labels, expected_height, expected_width)  )

					lowerCamelCase								=     model(A		,			labels=A		,			training=A  )
					self.parent.assertEqual(
					    result.logits.shape		,			(self.batch_size, self.num_labels, expected_height, expected_width)  )







		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     self.prepare_config_and_inputs()
					lowerCamelCase		,		lowerCamelCase		,		lowerCamelCase		,		lowerCamelCase								=     config_and_inputs
					lowerCamelCase								=     {"""pixel_values""": pixel_values}
					return config, inputs_dict







@require_tf
class 					__lowercase					(       a_ ,       a_ ,       unittest.TestCase     ):



		"""simple docstring"""

		UpperCamelCase    :				str 					=		(
		    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
		    if is_tf_available()
		    else ()
		)
		UpperCamelCase    :				Tuple 					=		(
		    {"feature-extraction": TFMobileViTModel, "image-classification": TFMobileViTForImageClassification}
		    if is_tf_available()
		    else {}
		)

		UpperCamelCase    :				Optional[int] 					=		False
		UpperCamelCase    :				Optional[Any] 					=		False
		UpperCamelCase    :				str 					=		False
		UpperCamelCase    :				Dict 					=		False
		UpperCamelCase    :				Any 					=		False







		def 				__A      (   self  )     ->   List[str]:







					'''simple docstring'''







					lowerCamelCase								=     TFMobileViTModelTester(self  )
					lowerCamelCase								=     TFMobileViTConfigTester(self		,			config_class=A		,			has_text_modality=A  )







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







					'''simple docstring'''







					self.config_tester.run_common_tests()







		@unittest.skip(reason="""MobileViT does not use inputs_embeds"""  )
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					pass







		@unittest.skip(reason="""MobileViT does not support input and output embeddings"""  )
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					pass







		@unittest.skip(reason="""MobileViT does not output attentions"""  )
		def 				__A      (   self  )     ->   List[str]:







					'''simple docstring'''







					pass







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







					'''simple docstring'''







					lowerCamelCase		,		lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								lowerCamelCase								=     model_class(A  )
								lowerCamelCase								=     inspect.signature(model.call  )
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								lowerCamelCase								=     [*signature.parameters.keys()]

								lowerCamelCase								=     ["""pixel_values"""]
								self.assertListEqual(arg_names[:1]		,			A  )







		def 				__A      (   self  )     ->   Optional[int]:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A  )







		def 				__A      (   self  )     ->   Any:







					'''simple docstring'''







					def check_hidden_states_output(A		,			A		,			A  ):
								lowerCamelCase								=     model_class(A  )

								lowerCamelCase								=     model(**self._prepare_for_class(A		,			A  )  )

								lowerCamelCase								=     outputs.hidden_states

								lowerCamelCase								=     5
								self.assertEqual(len(A  )		,			A  )

								# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
								# with the width and height being successively divided by 2.
								lowerCamelCase								=     2
								for i in range(len(A  )  ):
											self.assertListEqual(
											    list(hidden_states[i].shape[-2:]  )		,			[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]		,			)
											divisor *= 2

								self.assertEqual(self.model_tester.output_stride		,			divisor // 2  )

					lowerCamelCase		,		lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								lowerCamelCase								=     True
								check_hidden_states_output(A		,			A		,			A  )

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								lowerCamelCase								=     True

								check_hidden_states_output(A		,			A		,			A  )







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







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*A  )







		def 				__A      (   self  )     ->   List[str]:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*A  )







		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""  )  ) == 0		,			reason="""TF does not support backprop for grouped convolutions on CPU."""		,			)
		def 				__A      (   self  )     ->   List[Any]:







					'''simple docstring'''







					super().test_dataset_conversion()







		def 				__A      (   self		,			A		,			A		,			A=2e-1		,			A=2e-1  )     ->   Union[str, Any]:







					'''simple docstring'''







					self.assertTrue(np.allclose(A		,			A		,			atol=A		,			rtol=A  )  )







		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""  )  ) == 0		,			reason="""TF does not support backprop for grouped convolutions on CPU."""		,			)
		@slow
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase		,		lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								# Since `TFMobileViTModel` cannot operate with the default `fit()` method.
								if model_class.__name__ != "TFMobileViTModel":
											lowerCamelCase								=     model_class(A  )
											if getattr(A		,			"""hf_compute_loss"""		,			A  ):
														super().test_keras_fit()







		def 				__A      (   self  )     ->   Dict:







					'''simple docstring'''







					lowerCamelCase		,		lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_common()
					for model_class in self.all_model_classes:
								# set an ignore index to correctly test the masked loss used in
								# `TFMobileViTForSemanticSegmentation`.
								if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
											lowerCamelCase								=     5

								lowerCamelCase								=     model_class(A  )
								if getattr(A		,			"""hf_compute_loss"""		,			A  ):
											# The number of elements in the loss should be the same as the number of elements in the label
											lowerCamelCase								=     self._prepare_for_class(inputs_dict.copy()		,			A		,			return_labels=A  )
											lowerCamelCase								=     prepared_for_class[
											    sorted(prepared_for_class.keys() - inputs_dict.keys()		,			reverse=A  )[0]
											]
											lowerCamelCase								=     added_label.shape.as_list()[:1]

											# Test that model correctly compute the loss with kwargs
											lowerCamelCase								=     self._prepare_for_class(inputs_dict.copy()		,			A		,			return_labels=A  )
											lowerCamelCase								=     {"""input_ids""", """pixel_values""", """input_features"""}
											lowerCamelCase								=     possible_input_names.intersection(set(A  )  ).pop()
											lowerCamelCase								=     prepared_for_class.pop(A  )

											lowerCamelCase								=     model(A		,			**A  )[0]
											self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]  )

											# Test that model correctly compute the loss when we mask some positions
											lowerCamelCase								=     self._prepare_for_class(inputs_dict.copy()		,			A		,			return_labels=A  )
											lowerCamelCase								=     {"""input_ids""", """pixel_values""", """input_features"""}
											lowerCamelCase								=     possible_input_names.intersection(set(A  )  ).pop()
											lowerCamelCase								=     prepared_for_class.pop(A  )
											if "labels" in prepared_for_class:
														lowerCamelCase								=     prepared_for_class["""labels"""].numpy()
														if len(labels.shape  ) > 1 and labels.shape[1] != 1:
																	# labels[0] = -100
																	lowerCamelCase								=     tf.convert_to_tensor(A  )
																	lowerCamelCase								=     model(A		,			**A  )[0]
																	self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]  )
																	self.assertTrue(not np.any(np.isnan(loss.numpy()  )  )  )

                # Test that model correctly compute the loss with a dict
											lowerCamelCase								=     self._prepare_for_class(inputs_dict.copy()		,			A		,			return_labels=A  )
											lowerCamelCase								=     model(A  )[0]
											self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]  )

											# Test that model correctly compute the loss with a tuple
											lowerCamelCase								=     self._prepare_for_class(inputs_dict.copy()		,			A		,			return_labels=A  )

											# Get keys that were added with the _prepare_for_class function
											lowerCamelCase								=     prepared_for_class.keys() - inputs_dict.keys()
											lowerCamelCase								=     inspect.signature(model.call  ).parameters
											lowerCamelCase								=     list(signature.keys()  )

											# Create a dictionary holding the location of the tensors in the tuple
											lowerCamelCase								=     {0: input_name}
											for label_key in label_keys:
														lowerCamelCase								=     signature_names.index(A  )
														lowerCamelCase								=     label_key
											lowerCamelCase								=     sorted(tuple_index_mapping.items()  )
											# Initialize a list with their default values, update the values and convert to a tuple
											lowerCamelCase								=     []

											for name in signature_names:
														if name != "kwargs":
																	list_input.append(signature[name].default  )

											for index, value in sorted_tuple_index_mapping:
														lowerCamelCase								=     prepared_for_class[value]

											lowerCamelCase								=     tuple(A  )

											# Send to model
											lowerCamelCase								=     model(tuple_input[:-1]  )[0]

											self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]  )







		@slow
		def 				__A      (   self  )     ->   str:







					'''simple docstring'''







					for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCamelCase								=     TFMobileViTModel.from_pretrained(A  )
								self.assertIsNotNone(A  )






def   __lowerCamelCase	(       ):





			'''simple docstring'''




			lowerCamelCase								=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
			return image







@require_tf
class 					__lowercase					(       unittest.TestCase     ):



		"""simple docstring"""

		@slow
		def 				__A      (   self  )     ->   Optional[Any]:







					'''simple docstring'''







					lowerCamelCase								=     TFMobileViTForImageClassification.from_pretrained("""apple/mobilevit-xx-small"""  )

					lowerCamelCase								=     MobileViTImageProcessor.from_pretrained("""apple/mobilevit-xx-small"""  )
					lowerCamelCase								=     prepare_img()
					lowerCamelCase								=     image_processor(images=A		,			return_tensors="""tf"""  )

					# forward pass
					lowerCamelCase								=     model(**A		,			training=A  )

					# verify the logits
					lowerCamelCase								=     tf.TensorShape((1, 10_00)  )
					self.assertEqual(outputs.logits.shape		,			A  )

					lowerCamelCase								=     tf.constant([-1.9364, -1.2327, -0.4653]  )

					tf.debugging.assert_near(outputs.logits[0, :3]		,			A		,			atol=1e-4		,			rtol=1e-0_4  )







		@slow
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     TFMobileViTForSemanticSegmentation.from_pretrained("""apple/deeplabv3-mobilevit-xx-small"""  )

					lowerCamelCase								=     MobileViTImageProcessor.from_pretrained("""apple/deeplabv3-mobilevit-xx-small"""  )

					lowerCamelCase								=     prepare_img()
					lowerCamelCase								=     image_processor(images=A		,			return_tensors="""tf"""  )

					# forward pass
					lowerCamelCase								=     model(inputs.pixel_values		,			training=A  )
					lowerCamelCase								=     outputs.logits

					# verify the logits
					lowerCamelCase								=     tf.TensorShape((1, 21, 32, 32)  )
					self.assertEqual(logits.shape		,			A  )

					lowerCamelCase								=     tf.constant(
					    [
					        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
					        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
					        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
					    ]  )

					tf.debugging.assert_near(logits[0, :3, :3, :3]		,			A		,			rtol=1e-4		,			atol=1e-4  )





from typing import TYPE_CHECKING

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


UpperCAmelCase      :						List[str]    =		{
    "configuration_big_bird": ["BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP", "BigBirdConfig", "BigBirdOnnxConfig"],
}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase      :						str    =		["BigBirdTokenizer"]

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase      :						str    =		["BigBirdTokenizerFast"]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase      :						Dict    =		[
		    "BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "BigBirdForCausalLM",
		    "BigBirdForMaskedLM",
		    "BigBirdForMultipleChoice",
		    "BigBirdForPreTraining",
		    "BigBirdForQuestionAnswering",
		    "BigBirdForSequenceClassification",
		    "BigBirdForTokenClassification",
		    "BigBirdLayer",
		    "BigBirdModel",
		    "BigBirdPreTrainedModel",
		    "load_tf_weights_in_big_bird",
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCAmelCase      :						Dict    =		[
		    "FlaxBigBirdForCausalLM",
		    "FlaxBigBirdForMaskedLM",
		    "FlaxBigBirdForMultipleChoice",
		    "FlaxBigBirdForPreTraining",
		    "FlaxBigBirdForQuestionAnswering",
		    "FlaxBigBirdForSequenceClassification",
		    "FlaxBigBirdForTokenClassification",
		    "FlaxBigBirdModel",
		    "FlaxBigBirdPreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_big_bird import BIG_BIRD_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdConfig, BigBirdOnnxConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_big_bird import BigBirdTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_big_bird_fast import BigBirdTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_big_bird import (
				    BIG_BIRD_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BigBirdForCausalLM,
				    BigBirdForMaskedLM,
				    BigBirdForMultipleChoice,
				    BigBirdForPreTraining,
				    BigBirdForQuestionAnswering,
				    BigBirdForSequenceClassification,
				    BigBirdForTokenClassification,
				    BigBirdLayer,
				    BigBirdModel,
				    BigBirdPreTrainedModel,
				    load_tf_weights_in_big_bird,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_big_bird import (
				    FlaxBigBirdForCausalLM,
				    FlaxBigBirdForMaskedLM,
				    FlaxBigBirdForMultipleChoice,
				    FlaxBigBirdForPreTraining,
				    FlaxBigBirdForQuestionAnswering,
				    FlaxBigBirdForSequenceClassification,
				    FlaxBigBirdForTokenClassification,
				    FlaxBigBirdModel,
				    FlaxBigBirdPreTrainedModel,
				)

else:
		import sys

		UpperCAmelCase      :						Dict    =		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

