


"""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							( snake_case_      ):







   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




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







class _lowerCAmelCase							:







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

       '''simple docstring'''




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







   def 					lowerCamelCase						(      self		)      ->						Tuple:

       '''simple docstring'''




       snake_case       : str    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

       snake_case       : int    =       self.get_config()

       return config, pixel_values, labels, pixel_labels







   def 					lowerCamelCase						(      self		)      ->						int:

       '''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		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)      ->						Optional[Any]:

       '''simple docstring'''




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

       '''simple docstring'''




       snake_case       : Any    =       self.num_labels
       snake_case       : Any    =       MobileViTVaForImageClassification(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       snake_case       : int    =       model(UpperCamelCase__		,      labels=UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)		)







   def 					lowerCamelCase						(      self		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)      ->						str:

       '''simple docstring'''




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

       '''simple docstring'''




       snake_case       : int    =       self.prepare_config_and_inputs()
       snake_case       : Any    =       config_and_inputs
       snake_case       : int    =       {"pixel_values": pixel_values}
       return config, inputs_dict







@require_torch
class _lowerCAmelCase							( snake_case_       ,				snake_case_       ,				unittest.TestCase      ):
   __UpperCAmelCase					:			List[Any]				    =    (
       (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
       if is_torch_available()
       else ()
   )

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

   __UpperCAmelCase					:			str				    =    False
   __UpperCAmelCase					:			int				    =    False
   __UpperCAmelCase					:			Tuple				    =    False
   __UpperCAmelCase					:			List[str]				    =    False







   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       snake_case       : Tuple    =       MobileViTVaModelTester(self		)
       snake_case       : List[str]    =       MobileViTVaConfigTester(self		,      config_class=UpperCamelCase__		,      has_text_modality=UpperCamelCase__		)







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

       '''simple docstring'''




       self.config_tester.run_common_tests()







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

       '''simple docstring'''




       pass







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

       '''simple docstring'''




       pass







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

       '''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		)      ->						List[str]:

       '''simple docstring'''




       pass







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

       '''simple docstring'''




       pass







   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




       snake_case       : Tuple    =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case       : Union[str, Any]    =       model_class(UpperCamelCase__		)
           snake_case       : Tuple    =       inspect.signature(model.forward		)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           snake_case       : int    =       [*signature.parameters.keys()]

           snake_case       : Dict    =       ["pixel_values"]
           self.assertListEqual(arg_names[:1]		,      UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						Tuple:

       '''simple docstring'''




       snake_case       : str    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__		)







   def 					lowerCamelCase						(      self		)      ->						str:

       '''simple docstring'''




       def check_hidden_states_output(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		):
           snake_case       : List[str]    =       model_class(UpperCamelCase__		)
           model.to(UpperCamelCase__		)
           model.eval()

           with torch.no_grad():
               snake_case       : Optional[int]    =       model(**self._prepare_for_class(UpperCamelCase__		,      UpperCamelCase__		)		)

           snake_case       : str    =       outputs.hidden_states

           snake_case       : List[Any]    =       5
           self.assertEqual(len(UpperCamelCase__		)		,      UpperCamelCase__		)

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

       snake_case       : Tuple    =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           snake_case       : int    =       True
           check_hidden_states_output(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)

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

           check_hidden_states_output(UpperCamelCase__		,      UpperCamelCase__		,      UpperCamelCase__		)







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

       '''simple docstring'''




       snake_case       : str    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)







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

       '''simple docstring'''




       snake_case       : str    =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase__		)







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

       '''simple docstring'''




       for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case       : Dict    =       MobileViTVaModel.from_pretrained(UpperCamelCase__		)
           self.assertIsNotNone(UpperCamelCase__		)







def     __lowerCAmelCase		(		)      ->		Optional[int]:


    """simple docstring"""





    snake_case       : Optional[Any]    =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
    return image







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







   @cached_property
   def 					lowerCamelCase						(      self		)      ->						Optional[int]:

       '''simple docstring'''




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







   @slow
   def 					lowerCamelCase						(      self		)      ->						Dict:

       '''simple docstring'''




       snake_case       : Dict    =       MobileViTVaForImageClassification.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256"		).to(
           UpperCamelCase__		)

       snake_case       : Union[str, Any]    =       self.default_image_processor
       snake_case       : Union[str, Any]    =       prepare_img()
       snake_case       : List[str]    =       image_processor(images=UpperCamelCase__		,      return_tensors="pt"		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
           snake_case       : str    =       model(**UpperCamelCase__		)

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

       snake_case       : Optional[int]    =       torch.tensor([-1.6336e00, -7.3204e-02, -5.1883e-01]		).to(UpperCamelCase__		)

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







   @slow
   def 					lowerCamelCase						(      self		)      ->						Any:

       '''simple docstring'''




       snake_case       : List[str]    =       MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"		)
       snake_case       : Tuple    =       model.to(UpperCamelCase__		)

       snake_case       : Optional[Any]    =       MobileViTImageProcessor.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"		)

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

       # forward pass
       with torch.no_grad():
           snake_case       : Any    =       model(**UpperCamelCase__		)
       snake_case       : List[str]    =       outputs.logits

       # verify the logits
       snake_case       : Tuple    =       torch.Size((1, 21, 32, 32)		)
       self.assertEqual(logits.shape		,      UpperCamelCase__		)

       snake_case       : Dict    =       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=UpperCamelCase__		,      )

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







   @slow
   def 					lowerCamelCase						(      self		)      ->						int:

       '''simple docstring'''




       snake_case       : Optional[int]    =       MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"		)
       snake_case       : List[Any]    =       model.to(UpperCamelCase__		)

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

       snake_case       : Optional[int]    =       prepare_img()
       snake_case       : List[Any]    =       image_processor(images=UpperCamelCase__		,      return_tensors="pt"		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
           snake_case       : Union[str, Any]    =       model(**UpperCamelCase__		)

       snake_case       : List[Any]    =       outputs.logits.detach().cpu()

       snake_case       : Optional[int]    =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__		,      target_sizes=[(50, 60)]		)
       snake_case       : Union[str, Any]    =       torch.Size((50, 60)		)
       self.assertEqual(segmentation[0].shape		,      UpperCamelCase__		)

       snake_case       : Any    =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__		)
       snake_case       : Optional[int]    =       torch.Size((32, 32)		)
       self.assertEqual(segmentation[0].shape		,      UpperCamelCase__		)








"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_UpperCamelCase		     =    logging.get_logger(__name__)

_UpperCamelCase		     =    {
    """microsoft/focalnet-tiny""": """https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json""",
}






class 			__a					(						__magic_name__       ,	__magic_name__       ):





       """simple docstring"""


       __UpperCamelCase					:      Optional[Any]    =						'focalnet'






       def __init__(   self			,					snake_case=224			,					snake_case=4			,					snake_case=3			,					snake_case=96			,					snake_case=False			,					snake_case=[192, 384, 768, 768]			,					snake_case=[2, 2, 6, 2]			,					snake_case=[2, 2, 2, 2]			,					snake_case=[3, 3, 3, 3]			,					snake_case="gelu"			,					snake_case=4.0			,					snake_case=0.0			,					snake_case=0.1			,					snake_case=False			,					snake_case=1e-4			,					snake_case=False			,					snake_case=False			,					snake_case=False			,					snake_case=0.02			,					snake_case=1e-5			,					snake_case=32			,					snake_case=None			,					snake_case=None			,					**snake_case			,					):





        """simple docstring"""

        super().__init__(**snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				image_size
        lowerCAmelCase__   :				Union[str, Any] 			=				patch_size
        lowerCAmelCase__   :				List[str] 			=				num_channels
        lowerCAmelCase__   :				List[str] 			=				embed_dim
        lowerCAmelCase__   :				List[Any] 			=				use_conv_embed
        lowerCAmelCase__   :				List[str] 			=				hidden_sizes
        lowerCAmelCase__   :				List[Any] 			=				depths
        lowerCAmelCase__   :				Union[str, Any] 			=				focal_levels
        lowerCAmelCase__   :				Union[str, Any] 			=				focal_windows
        lowerCAmelCase__   :				int 			=				hidden_act
        lowerCAmelCase__   :				List[Any] 			=				mlp_ratio
        lowerCAmelCase__   :				str 			=				hidden_dropout_prob
        lowerCAmelCase__   :				Union[str, Any] 			=				drop_path_rate
        lowerCAmelCase__   :				Tuple 			=				use_layerscale
        lowerCAmelCase__   :				Tuple 			=				layerscale_value
        lowerCAmelCase__   :				str 			=				use_post_layernorm
        lowerCAmelCase__   :				str 			=				use_post_layernorm_in_modulation
        lowerCAmelCase__   :				Union[str, Any] 			=				normalize_modulator
        lowerCAmelCase__   :				Optional[int] 			=				initializer_range
        lowerCAmelCase__   :				List[Any] 			=				layer_norm_eps
        lowerCAmelCase__   :				str 			=				encoder_stride
        lowerCAmelCase__   :				Union[str, Any] 			=				["stem"] + [F"""stage{idx}""" for idx in range(1			,					len(self.depths   ) + 1   )]
        lowerCAmelCase__			,						lowerCAmelCase__   :				Union[str, Any] 			=				get_aligned_output_features_output_indices(
            out_features=snake_case			,					out_indices=snake_case			,					stage_names=self.stage_names   )






