






import inspect
import unittest

from transformers import MobileViTVaConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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 MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, MobileViTVaModel
	from transformers.models.mobilevitva.modeling_mobilevitva import (
	    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
	    make_divisible,
	)


if is_vision_available():
	from PIL import Image

	from transformers import MobileViTImageProcessor

class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

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


												"""simple docstring"""

												__lowercase      :							Any				=     self.config_class(**self.inputs_dict							)
												self.parent.assertTrue(hasattr(__a		,     """width_multiplier"""							)							)







class 		lowerCAmelCase     :

					'''simple docstring'''

					def __init__(  self				:	Any		,     __a				:	List[Any]		,     __a				:	Tuple=13		,     __a				:	List[str]=64		,     __a				:	List[str]=2		,     __a				:	str=3		,     __a				:	Union[str, Any]="swish"		,     __a				:	List[str]=3		,     __a				:	str=32		,     __a				:	List[str]=0.1		,     __a				:	List[str]=0.02		,     __a				:	Any=True		,     __a				:	Optional[Any]=True		,     __a				:	List[Any]=10		,     __a				:	Any=None		,     __a				:	Optional[Any]=0.25		,     __a				:	Optional[int]=0.0		,     __a				:	Dict=0.0		,     )			->  List[str]:


												"""simple docstring"""

												__lowercase      :							int				=     parent
												__lowercase      :							Union[str, Any]				=     batch_size
												__lowercase      :							str				=     image_size
												__lowercase      :							Optional[Any]				=     patch_size
												__lowercase      :							Any				=     num_channels
												__lowercase      :							int				=     make_divisible(512 * width_multiplier		,     divisor=8							)
												__lowercase      :							Union[str, Any]				=     hidden_act
												__lowercase      :							Optional[Any]				=     conv_kernel_size
												__lowercase      :							Optional[int]				=     output_stride
												__lowercase      :							List[Any]				=     classifier_dropout_prob
												__lowercase      :							Tuple				=     use_labels
												__lowercase      :							List[Any]				=     is_training
												__lowercase      :							Dict				=     num_labels
												__lowercase      :							Optional[int]				=     initializer_range
												__lowercase      :							int				=     scope
												__lowercase      :							Optional[int]				=     width_multiplier
												__lowercase      :							Any				=     ffn_dropout
												__lowercase      :							Optional[int]				=     attn_dropout



					def       lowerCAmelCase				(  self				:	Any							)			->  int:


												"""simple docstring"""

												__lowercase      :							int				=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

												__lowercase      :							List[Any]				=     None
												__lowercase      :							Any				=     None
												if self.use_labels:
																			__lowercase      :							List[str]				=     ids_tensor([self.batch_size]		,     self.num_labels							)
																			__lowercase      :							Dict				=     ids_tensor([self.batch_size, self.image_size, self.image_size]		,     self.num_labels							)

												__lowercase      :							Optional[Any]				=     self.get_config()

												return config, pixel_values, labels, pixel_labels



					def       lowerCAmelCase				(  self				:	Dict							)			->  str:


												"""simple docstring"""

												return MobileViTVaConfig(
												    image_size=self.image_size		,     patch_size=self.patch_size		,     num_channels=self.num_channels		,     hidden_act=self.hidden_act		,     conv_kernel_size=self.conv_kernel_size		,     output_stride=self.output_stride		,     classifier_dropout_prob=self.classifier_dropout_prob		,     initializer_range=self.initializer_range		,     width_multiplier=self.width_multiplier		,     ffn_dropout=self.ffn_dropout_prob		,     attn_dropout=self.attn_dropout_prob		,     )



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	Tuple		,     __a				:	Tuple		,     __a				:	Any		,     __a				:	Dict							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							Tuple				=     MobileViTVaModel(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Tuple				=     model(__a							)
												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				:	Optional[Any]		,     __a				:	Optional[Any]		,     __a				:	str		,     __a				:	Optional[Any]		,     __a				:	int							)			->  str:


												"""simple docstring"""

												__lowercase      :							Any				=     self.num_labels
												__lowercase      :							Dict				=     MobileViTVaForImageClassification(__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Union[str, Any]				=     model(__a		,     labels=__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	Optional[Any]		,     __a				:	Dict		,     __a				:	Optional[Any]		,     __a				:	Optional[Any]		,     __a				:	Tuple							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Dict				=     self.num_labels
												__lowercase      :							Union[str, Any]				=     MobileViTVaForSemanticSegmentation(__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Optional[int]				=     model(__a							)
												self.parent.assertEqual(
												    result.logits.shape		,     (
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )		,     )
												__lowercase      :							List[str]				=     model(__a		,     labels=__a							)
												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				:	Dict							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							List[str]				=     self.prepare_config_and_inputs()
												__lowercase			,			__lowercase			,			__lowercase			,			__lowercase      :							Dict				=     config_and_inputs
												__lowercase      :							Dict				=     {"""pixel_values""": pixel_values}
												return config, inputs_dict







@require_torch
class 		lowerCAmelCase     ( __a      ,       __a      ,       unittest.TestCase			):

					'''simple docstring'''

					_A						:			List[str]									=      (
					    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
					    if is_torch_available()
					    else ()
					)

					_A						:			Optional[Any]									=      (
					    {
					        '''feature-extraction''': MobileViTVaModel,
					        '''image-classification''': MobileViTVaForImageClassification,
					        '''image-segmentation''': MobileViTVaForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					_A						:			Tuple									=      False
					_A						:			str									=      False
					_A						:			List[str]									=      False
					_A						:			Any									=      False



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


												"""simple docstring"""

												__lowercase      :							int				=     MobileViTVaModelTester(self							)
												__lowercase      :							List[str]				=     MobileViTVaConfigTester(self		,     config_class=__a		,     has_text_modality=__a							)



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


												"""simple docstring"""

												self.config_tester.run_common_tests()



					@unittest.skip(reason="""MobileViTV2 does not use inputs_embeds"""							)
					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""MobileViTV2 does not support input and output embeddings"""							)
					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""MobileViTV2 does not output attentions"""							)
					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Optional[Any]:


												"""simple docstring"""

												pass



					@require_torch_multi_gpu
					@unittest.skip(reason="""Got `CUDA error: misaligned address` for tests after this one being run."""							)
					def       lowerCAmelCase				(  self				:	int							)			->  str:


												"""simple docstring"""

												pass



					@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
					def       lowerCAmelCase				(  self				:	Any							)			->  str:


												"""simple docstring"""

												pass



					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  Any:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase      :							List[Any]				=     model_class(__a							)
																			__lowercase      :							Optional[Any]				=     inspect.signature(model.forward							)
																			# signature.parameters is an OrderedDict => so arg_names order is deterministic
																			__lowercase      :							Optional[Any]				=     [*signature.parameters.keys()]

																			__lowercase      :							Any				=     ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]		,     __a							)



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


												"""simple docstring"""

												__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def       lowerCAmelCase				(  self				:	Any							)			->  int:


												"""simple docstring"""

												def check_hidden_states_output(__a				:	Tuple		,     __a				:	Any		,     __a				:	List[str]							):
																			__lowercase      :							List[str]				=     model_class(__a							)
																			model.to(__a							)
																			model.eval()

																			with torch.no_grad():
																										__lowercase      :							List[Any]				=     model(**self._prepare_for_class(__a		,     __a							)							)

																			__lowercase      :							Optional[int]				=     outputs.hidden_states

																			__lowercase      :							Optional[int]				=     5
																			self.assertEqual(len(__a							)		,     __a							)

																			# MobileViTV2's feature maps are of shape (batch_size, num_channels, height, width)
																			# with the width and height being successively divided by 2.
																			__lowercase      :							List[str]				=     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							)

												__lowercase			,			__lowercase      :							Optional[Any]				=     self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase      :							List[Any]				=     True
																			check_hidden_states_output(__a		,     __a		,     __a							)

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			__lowercase      :							List[Any]				=     True

																			check_hidden_states_output(__a		,     __a		,     __a							)



					def       lowerCAmelCase				(  self				:	str							)			->  Optional[Any]:


												"""simple docstring"""

												__lowercase      :							str				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					def       lowerCAmelCase				(  self				:	int							)			->  str:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*__a							)



					@slow
					def       lowerCAmelCase				(  self				:	List[str]							)			->  List[Any]:


												"""simple docstring"""

												for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase      :							Union[str, Any]				=     MobileViTVaModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







def       snake_case_		(     ):
							__lowercase      :							List[str]				=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
							return image



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

					'''simple docstring'''

					@cached_property
					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  List[str]:


												"""simple docstring"""

												return (
												    MobileViTImageProcessor.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""							)
												    if is_vision_available()
												    else None
												)



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


												"""simple docstring"""

												__lowercase      :							Tuple				=     MobileViTVaForImageClassification.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""							).to(
												    __a							)

												__lowercase      :							Optional[int]				=     self.default_image_processor
												__lowercase      :							List[Any]				=     prepare_img()
												__lowercase      :							Dict				=     image_processor(images=__a		,     return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase      :							List[Any]				=     model(**__a							)

												# verify the logits
												__lowercase      :							int				=     torch.Size((1, 1000)							)
												self.assertEqual(outputs.logits.shape		,     __a							)

												__lowercase      :							Union[str, Any]				=     torch.tensor([-1.6_336E00, -7.3_204E-02, -5.1_883E-01]							).to(__a							)

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



					@slow
					def       lowerCAmelCase				(  self				:	int							)			->  Dict:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""							)
												__lowercase      :							Tuple				=     model.to(__a							)

												__lowercase      :							List[str]				=     MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""							)

												__lowercase      :							Union[str, Any]				=     prepare_img()
												__lowercase      :							Union[str, Any]				=     image_processor(images=__a		,     return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase      :							Union[str, Any]				=     model(**__a							)
												__lowercase      :							Union[str, Any]				=     outputs.logits

												# verify the logits
												__lowercase      :							str				=     torch.Size((1, 21, 32, 32)							)
												self.assertEqual(logits.shape		,     __a							)

												__lowercase      :							List[str]				=     torch.tensor(
												    [
												        [[7.0863, 7.1525, 6.8201], [6.6931, 6.8770, 6.8933], [6.2978, 7.0366, 6.9636]],
												        [[-3.7134, -3.6712, -3.6675], [-3.5825, -3.3549, -3.4777], [-3.3435, -3.3979, -3.2857]],
												        [[-2.9329, -2.8003, -2.7369], [-3.0564, -2.4780, -2.0207], [-2.6889, -1.9298, -1.7640]],
												    ]		,     device=__a		,     )

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



					@slow
					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""							)
												__lowercase      :							Dict				=     model.to(__a							)

												__lowercase      :							str				=     MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""							)

												__lowercase      :							Tuple				=     prepare_img()
												__lowercase      :							List[str]				=     image_processor(images=__a		,     return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase      :							List[str]				=     model(**__a							)

												__lowercase      :							Optional[Any]				=     outputs.logits.detach().cpu()

												__lowercase      :							Any				=     image_processor.post_process_semantic_segmentation(outputs=__a		,     target_sizes=[(50, 60)]							)
												__lowercase      :							Optional[Any]				=     torch.Size((50, 60)							)
												self.assertEqual(segmentation[0].shape		,     __a							)

												__lowercase      :							Dict				=     image_processor.post_process_semantic_segmentation(outputs=__a							)
												__lowercase      :							int				=     torch.Size((32, 32)							)
												self.assertEqual(segmentation[0].shape		,     __a							)







def       snake_case_		(     lowerCAmelCase_       :						int       ,       lowerCAmelCase_       :						int      ):
							return int((input_a, input_a).count(1      ) != 0      )
def       snake_case_		(     ):
							assert or_gate(0       ,       0      ) == 0
							assert or_gate(0       ,       1      ) == 1
							assert or_gate(1       ,       0      ) == 1
							assert or_gate(1       ,       1      ) == 1


if __name__ == "__main__":
	print(or_gate(0, 1))
	print(or_gate(1, 0))
	print(or_gate(0, 0))
	print(or_gate(1, 1))