


'''simple docstring'''






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 		a__				(							a__  ):





							'''simple docstring'''






							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Any:
														lowerCAmelCase__          =	self.config_class(**self.inputs_dict   )
														self.parent.assertTrue(hasattr(lowerCamelCase_					,    '''hidden_sizes'''   )   )
														self.parent.assertTrue(hasattr(lowerCamelCase_					,    '''neck_hidden_sizes'''   )   )
														self.parent.assertTrue(hasattr(lowerCamelCase_					,    '''num_attention_heads'''   )   )




class 		a__				:





							'''simple docstring'''






							def __init__(		self					,    lowerCamelCase_					,    lowerCamelCase_=13					,    lowerCamelCase_=32					,    lowerCamelCase_=2					,    lowerCamelCase_=3					,    lowerCamelCase_=6_40					,    lowerCamelCase_=4					,    lowerCamelCase_="silu"					,    lowerCamelCase_=3					,    lowerCamelCase_=32					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.02					,    lowerCamelCase_=True					,    lowerCamelCase_=True					,    lowerCamelCase_=10					,    lowerCamelCase_=None					,    )       ->							Union[str, Any]:
														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   __SCREAMING_SNAKE_CASE							(		self   )       ->							Optional[Any]:
														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   __SCREAMING_SNAKE_CASE							(		self   )       ->							Optional[Any]:
														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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Any:
														lowerCAmelCase__          =	TFMobileViTModel(config=lowerCamelCase_   )
														lowerCAmelCase__          =	model(lowerCamelCase_					,    training=lowerCamelCase_   )
														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   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							List[str]:
														lowerCAmelCase__          =	self.num_labels
														lowerCAmelCase__          =	TFMobileViTForImageClassification(lowerCamelCase_   )
														lowerCAmelCase__          =	model(lowerCamelCase_					,    labels=lowerCamelCase_					,    training=lowerCamelCase_   )
														self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.num_labels)   )







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Optional[int]:
														lowerCAmelCase__          =	self.num_labels
														lowerCAmelCase__          =	TFMobileViTForSemanticSegmentation(lowerCamelCase_   )
														lowerCAmelCase__          =	lowerCAmelCase__          =	self.image_size // self.output_stride

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

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







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														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 		a__				(							a__				,				a__				,				unittest.TestCase  ):





							'''simple docstring'''






							lowercase__			:   Tuple							=      (
							    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
							    if is_tf_available()
							    else ()
							)
							lowercase__			:   List[Any]							=      (
							    {"feature-extraction": TFMobileViTModel, "image-classification": TFMobileViTForImageClassification}
							    if is_tf_available()
							    else {}
							)

							lowercase__			:   Any							=      False
							lowercase__			:   Dict							=      False
							lowercase__			:   Optional[int]							=      False
							lowercase__			:   Any							=      False
							lowercase__			:   Tuple							=      False







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														lowerCAmelCase__          =	TFMobileViTModelTester(self   )
														lowerCAmelCase__          =	TFMobileViTConfigTester(self					,    config_class=lowerCamelCase_					,    has_text_modality=lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Optional[int]:
														self.config_tester.run_common_tests()







							@unittest.skip(reason='''MobileViT does not use inputs_embeds'''   )
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Tuple:
														pass







							@unittest.skip(reason='''MobileViT does not support input and output embeddings'''   )
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														pass







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







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__   ,					lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__          =	model_class(lowerCamelCase_   )
																					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]					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														def check_hidden_states_output(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   ):
																					lowerCAmelCase__          =	model_class(lowerCamelCase_   )

																					lowerCAmelCase__          =	model(**self._prepare_for_class(lowerCamelCase_					,    lowerCamelCase_   )   )

																					lowerCAmelCase__          =	outputs.hidden_states

																					lowerCAmelCase__          =	5
																					self.assertEqual(len(lowerCamelCase_   )					,    lowerCamelCase_   )

																					# 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(lowerCamelCase_   )   ):
																												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(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )

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

																					check_hidden_states_output(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase_   )







							@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   __SCREAMING_SNAKE_CASE							(		self   )       ->							str:
														super().test_dataset_conversion()







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_=2e-1					,    lowerCamelCase_=2e-1   )       ->							List[Any]:
														self.assertTrue(np.allclose(lowerCamelCase_					,    lowerCamelCase_					,    atol=lowerCamelCase_					,    rtol=lowerCamelCase_   )   )







							@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   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[str]:
														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(lowerCamelCase_   )
																												if getattr(lowerCamelCase_					,    '''hf_compute_loss'''					,    lowerCamelCase_   ):
																																			super().test_keras_fit()







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[Any]:
														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(lowerCamelCase_   )
																					if getattr(lowerCamelCase_					,    '''hf_compute_loss'''					,    lowerCamelCase_   ):
																												# 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()					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )
																												lowerCAmelCase__          =	prepared_for_class[
																												    sorted(prepared_for_class.keys() - inputs_dict.keys()					,    reverse=lowerCamelCase_   )[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()					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )
																												lowerCAmelCase__          =	{'''input_ids''', '''pixel_values''', '''input_features'''}
																												lowerCAmelCase__          =	possible_input_names.intersection(set(lowerCamelCase_   )   ).pop()
																												lowerCAmelCase__          =	prepared_for_class.pop(lowerCamelCase_   )

																												lowerCAmelCase__          =	model(lowerCamelCase_					,    **lowerCamelCase_   )[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()					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )
																												lowerCAmelCase__          =	{'''input_ids''', '''pixel_values''', '''input_features'''}
																												lowerCAmelCase__          =	possible_input_names.intersection(set(lowerCamelCase_   )   ).pop()
																												lowerCAmelCase__          =	prepared_for_class.pop(lowerCamelCase_   )
																												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(lowerCamelCase_   )
																																										lowerCAmelCase__          =	model(lowerCamelCase_					,    **lowerCamelCase_   )[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()					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )
																												lowerCAmelCase__          =	model(lowerCamelCase_   )[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()					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )

																												# 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(lowerCamelCase_   )
																																			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(lowerCamelCase_   )

																												# 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   __SCREAMING_SNAKE_CASE							(		self   )       ->							Tuple:
														for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__          =	TFMobileViTModel.from_pretrained(lowerCamelCase_   )
																					self.assertIsNotNone(lowerCamelCase_   )


def 					_snake_case   (     )						->    Optional[Any]:
							lowerCAmelCase__          =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
							return image






@require_tf
class 		a__				(							unittest.TestCase  ):





							'''simple docstring'''






							@slow
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	TFMobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''   )

														lowerCAmelCase__          =	MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''   )
														lowerCAmelCase__          =	prepare_img()
														lowerCAmelCase__          =	image_processor(images=lowerCamelCase_					,    return_tensors='''tf'''   )

														# forward pass
														lowerCAmelCase__          =	model(**lowerCamelCase_					,    training=lowerCamelCase_   )

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

														lowerCAmelCase__          =	tf.constant([-1.9_364, -1.2_327, -0.4_653]   )

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







							@slow
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Tuple:
														# `from_pt` will be removed
														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=lowerCamelCase_					,    return_tensors='''tf'''   )

														# forward pass
														lowerCAmelCase__          =	model(inputs.pixel_values					,    training=lowerCamelCase_   )
														lowerCAmelCase__          =	outputs.logits

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

														lowerCAmelCase__          =	tf.constant(
														    [
														        [[6.9_713, 6.9_786, 7.2_422], [7.2_893, 7.2_825, 7.4_446], [7.6_580, 7.8_797, 7.9_420]],
														        [[-10.6_869, -10.3_250, -10.3_471], [-10.4_228, -9.9_868, -9.7_132], [-11.0_405, -11.0_221, -10.7_318]],
														        [[-3.3_089, -2.8_539, -2.6_740], [-3.2_706, -2.5_621, -2.5_108], [-3.2_534, -2.6_615, -2.6_651]],
														    ]   )

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



'''simple docstring'''






def 					_snake_case   (     A ,    A       )						->    float:
							return a + b


if __name__ == "__main__":
		__UpperCAmelCase    				=							5
		__UpperCAmelCase    				=							6
		print(f"""The sum of {a} + {b} is {add(a, b)}""")