



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 	lowerCamelCase__ (		UpperCamelCase_   ):


						def   _lowerCamelCase			( self						:  Tuple			):
													a__:       Optional[Any]											=self.config_class(**self.inputs_dict			)
													self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,       "hidden_sizes"			)			)
													self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,       "neck_hidden_sizes"			)			)
													self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE      ,       "num_attention_heads"			)			)


class 	lowerCamelCase__ :


						def __init__( self						:  Union[str, Any]      ,       _a						:  Any      ,       _a						:  Dict=1_3      ,       _a						:  Optional[Any]=3_2      ,       _a						:  List[Any]=2      ,       _a						:  str=3      ,       _a						:  Optional[Any]=6_4_0      ,       _a						:  Dict=4      ,       _a						:  Tuple="silu"      ,       _a						:  str=3      ,       _a						:  List[str]=3_2      ,       _a						:  List[Any]=0.1      ,       _a						:  Dict=0.1      ,       _a						:  List[str]=0.1      ,       _a						:  Dict=0.0_2      ,       _a						:  str=True      ,       _a						:  str=True      ,       _a						:  Optional[int]=1_0      ,       _a						:  List[str]=None      ,       ):
													a__:       int											=parent
													a__:       Dict											=batch_size
													a__:       int											=image_size
													a__:       str											=patch_size
													a__:       List[str]											=num_channels
													a__:       Optional[Any]											=last_hidden_size
													a__:       int											=num_attention_heads
													a__:       Optional[int]											=hidden_act
													a__:       Optional[int]											=conv_kernel_size
													a__:       Optional[int]											=output_stride
													a__:       List[Any]											=hidden_dropout_prob
													a__:       List[Any]											=attention_probs_dropout_prob
													a__:       str											=classifier_dropout_prob
													a__:       int											=use_labels
													a__:       Tuple											=is_training
													a__:       Optional[int]											=num_labels
													a__:       str											=initializer_range
													a__:       List[Any]											=scope


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Dict											=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

													a__:       Any											=None
													a__:       Union[str, Any]											=None
													if self.use_labels:
																				a__:       Dict											=ids_tensor([self.batch_size]      ,       self.num_labels			)
																				a__:       int											=ids_tensor([self.batch_size, self.image_size, self.image_size]      ,       self.num_labels			)

													a__:       Dict											=self.get_config()

													return config, pixel_values, labels, pixel_labels


						def   _lowerCamelCase			( self						:  Dict			):
													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   _lowerCamelCase			( self						:  str      ,       _a						:  str      ,       _a						:  List[str]      ,       _a						:  List[str]      ,       _a						:  int			):
													a__:       Optional[Any]											=TFMobileViTModel(config=__SCREAMING_SNAKE_CASE			)
													a__:       Union[str, Any]											=model(__SCREAMING_SNAKE_CASE      ,       training=__SCREAMING_SNAKE_CASE			)
													a__:       Tuple											=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   _lowerCamelCase			( self						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Optional[Any]      ,       _a						:  List[str]      ,       _a						:  int			):
													a__:       Optional[Any]											=self.num_labels
													a__:       Union[str, Any]											=TFMobileViTForImageClassification(__SCREAMING_SNAKE_CASE			)
													a__:       Optional[int]											=model(__SCREAMING_SNAKE_CASE      ,       labels=__SCREAMING_SNAKE_CASE      ,       training=__SCREAMING_SNAKE_CASE			)
													self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)			)


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  Any      ,       _a						:  List[str]      ,       _a						:  List[Any]      ,       _a						:  Optional[Any]			):
													a__:       Optional[int]											=self.num_labels
													a__:       Any											=TFMobileViTForSemanticSegmentation(__SCREAMING_SNAKE_CASE			)
													a__:       List[Any]											=self.image_size // self.output_stride

													a__:       Tuple											=model(__SCREAMING_SNAKE_CASE      ,       training=__SCREAMING_SNAKE_CASE			)
													self.parent.assertEqual(
													    result.logits.shape      ,       (self.batch_size, self.num_labels, expected_height, expected_width)			)

													a__:       Optional[Any]											=model(__SCREAMING_SNAKE_CASE      ,       labels=__SCREAMING_SNAKE_CASE      ,       training=__SCREAMING_SNAKE_CASE			)
													self.parent.assertEqual(
													    result.logits.shape      ,       (self.batch_size, self.num_labels, expected_height, expected_width)			)







						def   _lowerCamelCase			( self						:  str			):
													a__:       str											=self.prepare_config_and_inputs()
													a__							,			a__							,			a__							,			a__:       Optional[int]											=config_and_inputs
													a__:       List[Any]											={"pixel_values": pixel_values}
													return config, inputs_dict


@require_tf
class 	lowerCamelCase__ (		UpperCamelCase_				,					UpperCamelCase_				,					unittest.TestCase   ):
						_lowerCAmelCase								= (
						    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
						    if is_tf_available()
						    else ()
						)
						_lowerCAmelCase								= (
						    {"""feature-extraction""": TFMobileViTModel, """image-classification""": TFMobileViTForImageClassification}
						    if is_tf_available()
						    else {}
						)

						_lowerCAmelCase								= False
						_lowerCAmelCase								= False
						_lowerCAmelCase								= False
						_lowerCAmelCase								= False
						_lowerCAmelCase								= False


						def   _lowerCamelCase			( self						:  str			):
													a__:       Optional[Any]											=TFMobileViTModelTester(self			)
													a__:       Tuple											=TFMobileViTConfigTester(self      ,       config_class=__SCREAMING_SNAKE_CASE      ,       has_text_modality=__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  int			):
													self.config_tester.run_common_tests()


						@unittest.skip(reason="MobileViT does not use inputs_embeds"			)
						def   _lowerCamelCase			( self						:  List[str]			):
													pass


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


						@unittest.skip(reason="MobileViT does not output attentions"			)
						def   _lowerCamelCase			( self						:  List[Any]			):
													pass


						def   _lowerCamelCase			( self						:  Optional[int]			):
													a__							,			a__:       Optional[Any]											=self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				a__:       Tuple											=model_class(__SCREAMING_SNAKE_CASE			)
																				a__:       Dict											=inspect.signature(model.call			)
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				a__:       Optional[int]											=[*signature.parameters.keys()]

																				a__:       Any											=["pixel_values"]
																				self.assertListEqual(arg_names[:1]      ,       __SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  Any			):
													a__:       Any											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													def check_hidden_states_output(_a						:  List[str]      ,       _a						:  List[str]      ,       _a						:  int			):
																				a__:       Union[str, Any]											=model_class(__SCREAMING_SNAKE_CASE			)

																				a__:       Any											=model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE			)			)

																				a__:       str											=outputs.hidden_states

																				a__:       Dict											=5
																				self.assertEqual(len(__SCREAMING_SNAKE_CASE			)      ,       __SCREAMING_SNAKE_CASE			)

																				# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
																				# with the width and height being successively divided by 2.
																				a__:       List[str]											=2
																				for i in range(len(__SCREAMING_SNAKE_CASE			)			):
																											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			)

													a__							,			a__:       List[Any]											=self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				a__:       List[str]											=True
																				check_hidden_states_output(__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE			)

																				# check that output_hidden_states also work using config
																				del inputs_dict["output_hidden_states"]
																				a__:       int											=True

																				check_hidden_states_output(__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Optional[Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE			)


						def   _lowerCamelCase			( self						:  int			):
													a__:       List[Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE			)


						@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   _lowerCamelCase			( self						:  List[Any]			):
													super().test_dataset_conversion()


						def   _lowerCamelCase			( self						:  Any      ,       _a						:  Optional[Any]      ,       _a						:  Any      ,       _a						:  Optional[int]=2e-1      ,       _a						:  Union[str, Any]=2e-1			):
													self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       atol=__SCREAMING_SNAKE_CASE      ,       rtol=__SCREAMING_SNAKE_CASE			)			)


						@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   _lowerCamelCase			( self						:  Optional[int]			):
													a__							,			a__:       int											=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":
																											a__:       Any											=model_class(__SCREAMING_SNAKE_CASE			)
																											if getattr(__SCREAMING_SNAKE_CASE      ,       "hf_compute_loss"      ,       __SCREAMING_SNAKE_CASE			):
																																		super().test_keras_fit()


						def   _lowerCamelCase			( self						:  int			):
													a__							,			a__:       Optional[int]											=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":
																											a__:       Tuple											=5

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

																											# Test that model correctly compute the loss with kwargs
																											a__:       str											=self._prepare_for_class(inputs_dict.copy()      ,       __SCREAMING_SNAKE_CASE      ,       return_labels=__SCREAMING_SNAKE_CASE			)
																											a__:       Dict											={"input_ids", "pixel_values", "input_features"}
																											a__:       str											=possible_input_names.intersection(set(__SCREAMING_SNAKE_CASE			)			).pop()
																											a__:       Tuple											=prepared_for_class.pop(__SCREAMING_SNAKE_CASE			)

																											a__:       str											=model(__SCREAMING_SNAKE_CASE      ,       **__SCREAMING_SNAKE_CASE			)[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
																											a__:       List[Any]											=self._prepare_for_class(inputs_dict.copy()      ,       __SCREAMING_SNAKE_CASE      ,       return_labels=__SCREAMING_SNAKE_CASE			)
																											a__:       List[str]											={"input_ids", "pixel_values", "input_features"}
																											a__:       Optional[int]											=possible_input_names.intersection(set(__SCREAMING_SNAKE_CASE			)			).pop()
																											a__:       Optional[Any]											=prepared_for_class.pop(__SCREAMING_SNAKE_CASE			)
																											if "labels" in prepared_for_class:
																																		a__:       int											=prepared_for_class["labels"].numpy()
																																		if len(labels.shape			) > 1 and labels.shape[1] != 1:
																																									# labels[0] = -100
																																									a__:       Optional[int]											=tf.convert_to_tensor(__SCREAMING_SNAKE_CASE			)
																																									a__:       List[str]											=model(__SCREAMING_SNAKE_CASE      ,       **__SCREAMING_SNAKE_CASE			)[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
																											a__:       Optional[int]											=self._prepare_for_class(inputs_dict.copy()      ,       __SCREAMING_SNAKE_CASE      ,       return_labels=__SCREAMING_SNAKE_CASE			)
																											a__:       Any											=model(__SCREAMING_SNAKE_CASE			)[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
																											a__:       int											=self._prepare_for_class(inputs_dict.copy()      ,       __SCREAMING_SNAKE_CASE      ,       return_labels=__SCREAMING_SNAKE_CASE			)

																											# Get keys that were added with the _prepare_for_class function
																											a__:       List[str]											=prepared_for_class.keys() - inputs_dict.keys()
																											a__:       int											=inspect.signature(model.call			).parameters
																											a__:       List[str]											=list(signature.keys()			)

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

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

																											for index, value in sorted_tuple_index_mapping:
																																		a__:       Union[str, Any]											=prepared_for_class[value]

																											a__:       Optional[int]											=tuple(__SCREAMING_SNAKE_CASE			)

																											# Send to model
																											a__:       Optional[Any]											=model(tuple_input[:-1]			)[0]

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







						@slow
						def   _lowerCamelCase			( self						:  Tuple			):
													for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				a__:       Dict											=TFMobileViTModel.from_pretrained(__SCREAMING_SNAKE_CASE			)
																				self.assertIsNotNone(__SCREAMING_SNAKE_CASE			)





def 						__lowerCamelCase       (      ):
							a__:       Dict											=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
							return image







@require_tf
class 	lowerCamelCase__ (		unittest.TestCase   ):


						@slow
						def   _lowerCamelCase			( self						:  str			):
													a__:       Union[str, Any]											=TFMobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"			)

													a__:       Union[str, Any]											=MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"			)
													a__:       Dict											=prepare_img()
													a__:       Any											=image_processor(images=__SCREAMING_SNAKE_CASE      ,       return_tensors="tf"			)

													# forward pass
													a__:       Tuple											=model(**__SCREAMING_SNAKE_CASE      ,       training=__SCREAMING_SNAKE_CASE			)

													# verify the logits
													a__:       List[str]											=tf.TensorShape((1, 1_0_0_0)			)
													self.assertEqual(outputs.logits.shape      ,       __SCREAMING_SNAKE_CASE			)

													a__:       int											=tf.constant([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]			)

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







						@slow
						def   _lowerCamelCase			( self						:  Tuple			):
													# `from_pt` will be removed
													a__:       int											=TFMobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"			)

													a__:       Optional[int]											=MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"			)

													a__:       Dict											=prepare_img()
													a__:       Union[str, Any]											=image_processor(images=__SCREAMING_SNAKE_CASE      ,       return_tensors="tf"			)

													# forward pass
													a__:       Optional[int]											=model(inputs.pixel_values      ,       training=__SCREAMING_SNAKE_CASE			)
													a__:       List[Any]											=outputs.logits

													# verify the logits
													a__:       Tuple											=tf.TensorShape((1, 2_1, 3_2, 3_2)			)
													self.assertEqual(logits.shape      ,       __SCREAMING_SNAKE_CASE			)

													a__:       List[Any]											=tf.constant(
													    [
													        [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
													        [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
													        [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
													    ]			)

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











from ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
		from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline





