
"""simple docstring"""
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    A__						( self				)       ->  Optional[int]:
													__lowerCAmelCase     =  self.config_class(**self.inputs_dict				)
													self.parent.assertTrue(hasattr(snake_case_    ,				"""width_multiplier"""				)				)
class       lowerCAmelCase_		:



							'''simple docstring'''





							def __init__( self    ,				snake_case_    ,				snake_case_=13    ,				snake_case_=64    ,				snake_case_=2    ,				snake_case_=3    ,				snake_case_="swish"    ,				snake_case_=3    ,				snake_case_=32    ,				snake_case_=0.1    ,				snake_case_=0.02    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=10    ,				snake_case_=None    ,				snake_case_=0.25    ,				snake_case_=0.0    ,				snake_case_=0.0    ,				)       ->  List[Any]:
													__lowerCAmelCase     =  parent
													__lowerCAmelCase     =  batch_size
													__lowerCAmelCase     =  image_size
													__lowerCAmelCase     =  patch_size
													__lowerCAmelCase     =  num_channels
													__lowerCAmelCase     =  make_divisible(512 * width_multiplier    ,				divisor=8				)
													__lowerCAmelCase     =  hidden_act
													__lowerCAmelCase     =  conv_kernel_size
													__lowerCAmelCase     =  output_stride
													__lowerCAmelCase     =  classifier_dropout_prob
													__lowerCAmelCase     =  use_labels
													__lowerCAmelCase     =  is_training
													__lowerCAmelCase     =  num_labels
													__lowerCAmelCase     =  initializer_range
													__lowerCAmelCase     =  scope
													__lowerCAmelCase     =  width_multiplier
													__lowerCAmelCase     =  ffn_dropout
													__lowerCAmelCase     =  attn_dropout




							def    A__						( 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    A__						( self				)       ->  Optional[Any]:
													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    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Union[str, Any]:
													__lowerCAmelCase     =  MobileViTVaModel(config=snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_				)
													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    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Optional[Any]:
													__lowerCAmelCase     =  self.num_labels
													__lowerCAmelCase     =  MobileViTVaForImageClassification(snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_    ,				labels=snake_case_				)
													self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels)				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Dict:
													__lowerCAmelCase     =  self.num_labels
													__lowerCAmelCase     =  MobileViTVaForSemanticSegmentation(snake_case_				)
													model.to(snake_case_				)
													model.eval()
													__lowerCAmelCase     =  model(snake_case_				)
													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(snake_case_    ,				labels=snake_case_				)
													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    A__						( self				)       ->  Optional[int]:
													__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       lowerCAmelCase_		(				A__				,				A__				,				unittest.TestCase ):



							'''simple docstring'''





							_snake_case						      =		(
							    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)

							_snake_case						      =		(
							    {
							        '''feature-extraction''': MobileViTVaModel,
							        '''image-classification''': MobileViTVaForImageClassification,
							        '''image-segmentation''': MobileViTVaForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							_snake_case						      =		False
							_snake_case						      =		False
							_snake_case						      =		False
							_snake_case						      =		False




							def    A__						( self				)       ->  Dict:
													__lowerCAmelCase     =  MobileViTVaModelTester(self				)
													__lowerCAmelCase     =  MobileViTVaConfigTester(self    ,				config_class=snake_case_    ,				has_text_modality=snake_case_				)




							def    A__						( self				)       ->  Union[str, Any]:
													self.config_tester.run_common_tests()




							@unittest.skip(reason="""MobileViTV2 does not use inputs_embeds"""				)
							def    A__						( self				)       ->  Optional[Any]:
													pass




							@unittest.skip(reason="""MobileViTV2 does not support input and output embeddings"""				)
							def    A__						( self				)       ->  List[str]:
													pass




							@unittest.skip(reason="""MobileViTV2 does not output attentions"""				)
							def    A__						( self				)       ->  List[Any]:
													pass




							@require_torch_multi_gpu
							@unittest.skip(reason="""Got `CUDA error: misaligned address` for tests after this one being run."""				)
							def    A__						( self				)       ->  Union[str, Any]:
													pass




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




							def    A__						( self				)       ->  int:
													__lowerCAmelCase   ,     __lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs_for_common()

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




							def    A__						( self				)       ->  Dict:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*snake_case_				)




							def    A__						( self				)       ->  int:
													def check_hidden_states_output(snake_case_    ,				snake_case_    ,				snake_case_				):
																			__lowerCAmelCase     =  model_class(snake_case_				)
																			model.to(snake_case_				)
																			model.eval()

																			with torch.no_grad():
																									__lowerCAmelCase     =  model(**self._prepare_for_class(snake_case_    ,				snake_case_				)				)

																			__lowerCAmelCase     =  outputs.hidden_states

																			__lowerCAmelCase     =  5
																			self.assertEqual(len(snake_case_				)    ,				snake_case_				)

																			# MobileViTV2'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(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				)

													__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(snake_case_    ,				snake_case_    ,				snake_case_				)

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

																			check_hidden_states_output(snake_case_    ,				snake_case_    ,				snake_case_				)




							def    A__						( self				)       ->  Optional[Any]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*snake_case_				)




							def    A__						( self				)       ->  List[str]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_				)




							@slow
							def    A__						( self				)       ->  Any:
													for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowerCAmelCase     =  MobileViTVaModel.from_pretrained(snake_case_				)
																			self.assertIsNotNone(snake_case_				)

def  lowercase     ():
						__lowerCAmelCase     =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
						return image



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



							'''simple docstring'''





							@cached_property
							def    A__						( self				)       ->  Any:
													return (
													    MobileViTImageProcessor.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""				)
													    if is_vision_available()
													    else None
													)




							@slow
							def    A__						( self				)       ->  Optional[Any]:
													__lowerCAmelCase     =  MobileViTVaForImageClassification.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256"""				).to(
													    snake_case_				)

													__lowerCAmelCase     =  self.default_image_processor
													__lowerCAmelCase     =  prepare_img()
													__lowerCAmelCase     =  image_processor(images=snake_case_    ,				return_tensors="""pt"""				).to(snake_case_				)

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

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

													__lowerCAmelCase     =  torch.tensor([-1.6_3_3_6e0_0, -7.3_2_0_4e-0_2, -5.1_8_8_3e-0_1]				).to(snake_case_				)

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




							@slow
							def    A__						( self				)       ->  List[str]:
													__lowerCAmelCase     =  MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)
													__lowerCAmelCase     =  model.to(snake_case_				)

													__lowerCAmelCase     =  MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)

													__lowerCAmelCase     =  prepare_img()
													__lowerCAmelCase     =  image_processor(images=snake_case_    ,				return_tensors="""pt"""				).to(snake_case_				)

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

													# verify the logits
													__lowerCAmelCase     =  torch.Size((1, 21, 32, 32)				)
													self.assertEqual(logits.shape    ,				snake_case_				)

													__lowerCAmelCase     =  torch.tensor(
													    [
													        [[7.0_863, 7.1_525, 6.8_201], [6.6_931, 6.8_770, 6.8_933], [6.2_978, 7.0_366, 6.9_636]],
													        [[-3.7_134, -3.6_712, -3.6_675], [-3.5_825, -3.3_549, -3.4_777], [-3.3_435, -3.3_979, -3.2_857]],
													        [[-2.9_329, -2.8_003, -2.7_369], [-3.0_564, -2.4_780, -2.0_207], [-2.6_889, -1.9_298, -1.7_640]],
													    ]    ,				device=snake_case_    ,				)

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




							@slow
							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)
													__lowerCAmelCase     =  model.to(snake_case_				)

													__lowerCAmelCase     =  MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3"""				)

													__lowerCAmelCase     =  prepare_img()
													__lowerCAmelCase     =  image_processor(images=snake_case_    ,				return_tensors="""pt"""				).to(snake_case_				)

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

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

													__lowerCAmelCase     =  image_processor.post_process_semantic_segmentation(outputs=snake_case_    ,				target_sizes=[(50, 60)]				)
													__lowerCAmelCase     =  torch.Size((50, 60)				)
													self.assertEqual(segmentation[0].shape    ,				snake_case_				)

													__lowerCAmelCase     =  image_processor.post_process_semantic_segmentation(outputs=snake_case_				)
													__lowerCAmelCase     =  torch.Size((32, 32)				)
													self.assertEqual(segmentation[0].shape    ,				snake_case_				)


"""simple docstring"""
from typing import List

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_					     =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_					     =	{
    '''snap-research/efficientformer-l1-300''': (
        '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'''
    ),
}
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''efficientformer'''




							def __init__( self    ,				snake_case_ = [3, 2, 6, 4]    ,				snake_case_ = [48, 96, 224, 448]    ,				snake_case_ = [True, True, True, True]    ,				snake_case_ = 448    ,				snake_case_ = 32    ,				snake_case_ = 4    ,				snake_case_ = 7    ,				snake_case_ = 5    ,				snake_case_ = 8    ,				snake_case_ = 4    ,				snake_case_ = 0.0    ,				snake_case_ = 16    ,				snake_case_ = 3    ,				snake_case_ = 3    ,				snake_case_ = 3    ,				snake_case_ = 2    ,				snake_case_ = 1    ,				snake_case_ = 0.0    ,				snake_case_ = 1    ,				snake_case_ = True    ,				snake_case_ = True    ,				snake_case_ = 1e-5    ,				snake_case_ = "gelu"    ,				snake_case_ = 0.02    ,				snake_case_ = 1e-1_2    ,				snake_case_ = 224    ,				snake_case_ = 1e-0_5    ,				**snake_case_    ,				)       ->  None:
													super().__init__(**snake_case_				)

													__lowerCAmelCase     =  hidden_act
													__lowerCAmelCase     =  hidden_dropout_prob
													__lowerCAmelCase     =  hidden_sizes
													__lowerCAmelCase     =  num_hidden_layers
													__lowerCAmelCase     =  num_attention_heads
													__lowerCAmelCase     =  initializer_range
													__lowerCAmelCase     =  layer_norm_eps
													__lowerCAmelCase     =  patch_size
													__lowerCAmelCase     =  num_channels
													__lowerCAmelCase     =  depths
													__lowerCAmelCase     =  mlp_expansion_ratio
													__lowerCAmelCase     =  downsamples
													__lowerCAmelCase     =  dim
													__lowerCAmelCase     =  key_dim
													__lowerCAmelCase     =  attention_ratio
													__lowerCAmelCase     =  resolution
													__lowerCAmelCase     =  pool_size
													__lowerCAmelCase     =  downsample_patch_size
													__lowerCAmelCase     =  downsample_stride
													__lowerCAmelCase     =  downsample_pad
													__lowerCAmelCase     =  drop_path_rate
													__lowerCAmelCase     =  num_metaad_blocks
													__lowerCAmelCase     =  distillation
													__lowerCAmelCase     =  use_layer_scale
													__lowerCAmelCase     =  layer_scale_init_value
													__lowerCAmelCase     =  image_size
													__lowerCAmelCase     =  batch_norm_eps
