





import inspect
import unittest

from transformers import MobileViTConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
			from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				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=1_3       ,  __lowerCAmelCase=3_2       ,  __lowerCAmelCase=2       ,  __lowerCAmelCase=3       ,  __lowerCAmelCase=6_4_0       ,  __lowerCAmelCase=4       ,  __lowerCAmelCase="silu"       ,  __lowerCAmelCase=3       ,  __lowerCAmelCase=3_2       ,  __lowerCAmelCase=0.1       ,  __lowerCAmelCase=0.1       ,  __lowerCAmelCase=0.1       ,  __lowerCAmelCase=0.02       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=1_0       ,  __lowerCAmelCase=None       ,  ):



				'''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 		__lowerCamelCase				(    self							):



				'''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 		__lowerCamelCase				(    self							):



				'''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 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		MobileViTModel(config=__lowerCAmelCase							)
				model.to(__lowerCAmelCase							)
				model.eval()
				lowerCamelCase__								=		model(__lowerCAmelCase							)
				self.parent.assertEqual(
				    result.last_hidden_state.shape       ,  (
				        self.batch_size,
				        self.last_hidden_size,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )       ,  )





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		self.num_labels
				lowerCamelCase__								=		MobileViTForImageClassification(__lowerCAmelCase							)
				model.to(__lowerCAmelCase							)
				model.eval()
				lowerCamelCase__								=		model(__lowerCAmelCase       ,  labels=__lowerCAmelCase							)
				self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		self.num_labels
				lowerCamelCase__								=		MobileViTForSemanticSegmentation(__lowerCAmelCase							)
				model.to(__lowerCAmelCase							)
				model.eval()
				lowerCamelCase__								=		model(__lowerCAmelCase							)
				self.parent.assertEqual(
				    result.logits.shape       ,  (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )       ,  )
				lowerCamelCase__								=		model(__lowerCAmelCase       ,  labels=__lowerCAmelCase							)
				self.parent.assertEqual(
				    result.logits.shape       ,  (
				        self.batch_size,
				        self.num_labels,
				        self.image_size // self.output_stride,
				        self.image_size // self.output_stride,
				    )       ,  )





			def 		__lowerCamelCase				(    self							):



				'''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_torch
class 					__A    (    lowerCAmelCase			,				lowerCAmelCase			,				unittest.TestCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						(
			    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			lowerCAmelCase_							     =						(
			    {
			        """feature-extraction""": MobileViTModel,
			        """image-classification""": MobileViTForImageClassification,
			        """image-segmentation""": MobileViTForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						False
			lowerCAmelCase_							     =						False





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		MobileViTModelTester(self							)
				lowerCamelCase__								=		MobileViTConfigTester(self       ,  config_class=__lowerCAmelCase       ,  has_text_modality=__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				self.config_tester.run_common_tests()





			@unittest.skip(reason='''MobileViT does not use inputs_embeds'''							)
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				pass





			@unittest.skip(reason='''MobileViT does not support input and output embeddings'''							)
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				pass





			@unittest.skip(reason='''MobileViT does not output attentions'''							)
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				pass





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				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.forward							)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					lowerCamelCase__								=		[*signature.parameters.keys()]

					lowerCamelCase__								=		['''pixel_values''']
					self.assertListEqual(arg_names[:1]       ,  __lowerCAmelCase							)





			@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''							)
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				pass





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				def check_hidden_states_output(__lowerCAmelCase       ,  __lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		model_class(__lowerCAmelCase							)
					model.to(__lowerCAmelCase							)
					model.eval()

					with torch.no_grad():
						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 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase							)





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCamelCase__								=		MobileViTModel.from_pretrained(__lowerCAmelCase							)
					self.assertIsNotNone(__lowerCAmelCase							)





def lowerCAmelCase__()				->      Optional[Any]:







	'''simple docstring'''







	lowerCamelCase__								=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
	return image






@require_torch
@require_vision
class 					__A    (    unittest.TestCase ):

			'''simple docstring'''







			@cached_property
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''							) if is_vision_available() else None





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''							).to(__lowerCAmelCase							)

				lowerCamelCase__								=		self.default_image_processor
				lowerCamelCase__								=		prepare_img()
				lowerCamelCase__								=		image_processor(images=__lowerCAmelCase       ,  return_tensors='''pt'''							).to(__lowerCAmelCase							)

				# forward pass
				with torch.no_grad():
					lowerCamelCase__								=		model(**__lowerCAmelCase							)

				# verify the logits
				lowerCamelCase__								=		torch.Size((1, 1_0_0_0)							)
				self.assertEqual(outputs.logits.shape       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		torch.tensor([-1.9364, -1.2327, -0.4653]							).to(__lowerCAmelCase							)

				self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,  __lowerCAmelCase       ,  atol=1E-4							)							)





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)
				lowerCamelCase__								=		model.to(__lowerCAmelCase							)

				lowerCamelCase__								=		MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)

				lowerCamelCase__								=		prepare_img()
				lowerCamelCase__								=		image_processor(images=__lowerCAmelCase       ,  return_tensors='''pt'''							).to(__lowerCAmelCase							)

				# forward pass
				with torch.no_grad():
					lowerCamelCase__								=		model(**__lowerCAmelCase							)
				lowerCamelCase__								=		outputs.logits

				# verify the logits
				lowerCamelCase__								=		torch.Size((1, 2_1, 3_2, 3_2)							)
				self.assertEqual(logits.shape       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		torch.tensor(
				    [
				        [[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]],
				    ]       ,  device=__lowerCAmelCase       ,  )

				self.assertTrue(torch.allclose(logits[0, :3, :3, :3]       ,  __lowerCAmelCase       ,  atol=1E-4							)							)





			@slow
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)
				lowerCamelCase__								=		model.to(__lowerCAmelCase							)

				lowerCamelCase__								=		MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)

				lowerCamelCase__								=		prepare_img()
				lowerCamelCase__								=		image_processor(images=__lowerCAmelCase       ,  return_tensors='''pt'''							).to(__lowerCAmelCase							)

				# forward pass
				with torch.no_grad():
					lowerCamelCase__								=		model(**__lowerCAmelCase							)

				lowerCamelCase__								=		outputs.logits.detach().cpu()

				lowerCamelCase__								=		image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase       ,  target_sizes=[(5_0, 6_0)]							)
				lowerCamelCase__								=		torch.Size((5_0, 6_0)							)
				self.assertEqual(segmentation[0].shape       ,  __lowerCAmelCase							)

				lowerCamelCase__								=		image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase							)
				lowerCamelCase__								=		torch.Size((3_2, 3_2)							)
				self.assertEqual(segmentation[0].shape       ,  __lowerCAmelCase							)










_a 						=      65_521





def lowerCAmelCase__(__snake_case							)				->      int:







	'''simple docstring'''







	lowerCamelCase__								=		1
	lowerCamelCase__								=		0
	for plain_chr in plain_text:
		lowerCamelCase__								=		(a + ord(__snake_case							)) % MOD_ADLER
		lowerCamelCase__								=		(b + a) % MOD_ADLER
	return (b << 16) | a



