


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 			_lowercase (							lowercase__):


       """simple docstring"""




       def 						lowerCAmelCase     (   self							:			List[str]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  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 			_lowercase :


       """simple docstring"""




       def __init__(   self							:			List[Any]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			List[Any]=13					,      __lowerCamelCase							:			List[str]=32					,      __lowerCamelCase							:			int=2					,      __lowerCamelCase							:			Union[str, Any]=3					,      __lowerCamelCase							:			Tuple=640					,      __lowerCamelCase							:			List[str]=4					,      __lowerCamelCase							:			int="silu"					,      __lowerCamelCase							:			Optional[Any]=3					,      __lowerCamelCase							:			str=32					,      __lowerCamelCase							:			Optional[int]=0.1					,      __lowerCamelCase							:			Optional[int]=0.1					,      __lowerCamelCase							:			str=0.1					,      __lowerCamelCase							:			List[str]=0.0_2					,      __lowerCamelCase							:			Dict=True					,      __lowerCamelCase							:			Any=True					,      __lowerCamelCase							:			Any=10					,      __lowerCamelCase							:			Any=None					,      ):


             '''simple docstring'''



             lowerCamelCase__		:				str    =  parent
             lowerCamelCase__		:				Any    =  batch_size
             lowerCamelCase__		:				Dict    =  image_size
             lowerCamelCase__		:				Union[str, Any]    =  patch_size
             lowerCamelCase__		:				Union[str, Any]    =  num_channels
             lowerCamelCase__		:				str    =  last_hidden_size
             lowerCamelCase__		:				int    =  num_attention_heads
             lowerCamelCase__		:				Optional[int]    =  hidden_act
             lowerCamelCase__		:				List[Any]    =  conv_kernel_size
             lowerCamelCase__		:				Any    =  output_stride
             lowerCamelCase__		:				Tuple    =  hidden_dropout_prob
             lowerCamelCase__		:				Optional[int]    =  attention_probs_dropout_prob
             lowerCamelCase__		:				str    =  classifier_dropout_prob
             lowerCamelCase__		:				Optional[int]    =  use_labels
             lowerCamelCase__		:				Optional[int]    =  is_training
             lowerCamelCase__		:				Optional[int]    =  num_labels
             lowerCamelCase__		:				str    =  initializer_range
             lowerCamelCase__		:				Optional[Any]    =  scope





       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



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

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

             lowerCamelCase__		:				List[Any]    =  self.get_config()

             return config, pixel_values, labels, pixel_labels





       def 						lowerCAmelCase     (   self							:			int       ):


             '''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							:			Union[str, Any]					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Dict       ):


             '''simple docstring'''



             lowerCamelCase__		:				Tuple    =  MobileViTModel(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				List[str]    =  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							:			List[str]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			Union[str, Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  self.num_labels
             lowerCamelCase__		:				int    =  MobileViTForImageClassification(__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				List[Any]    =  model(__lowerCamelCase					,      labels=__lowerCamelCase       )
             self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)       )





       def 						lowerCAmelCase     (   self							:			List[str]					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			List[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  self.num_labels
             lowerCamelCase__		:				Optional[Any]    =  MobileViTForSemanticSegmentation(__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				Any    =  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__		:				Optional[int]    =  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							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  self.prepare_config_and_inputs()
             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				str    =  config_and_inputs
             lowerCamelCase__		:				str    =  {"pixel_values": pixel_values}
             return config, inputs_dict







@require_torch
class 			_lowercase (							lowercase__							,  lowercase__							,  unittest.TestCase):


       """simple docstring"""




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

       A__											=			False
       A__											=			False
       A__											=			False
       A__											=			False





       def 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Tuple    =  MobileViTModelTester(self       )
             lowerCamelCase__		:				int    =  MobileViTConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       @unittest.skip(reason="MobileViT does not use inputs_embeds"       )
       def 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             pass





       @unittest.skip(reason="MobileViT does not support input and output embeddings"       )
       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             pass





       @unittest.skip(reason="MobileViT does not output attentions"       )
       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             pass





       def 						lowerCAmelCase     (   self							:			List[str]       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__		:				Tuple    =  self.model_tester.prepare_config_and_inputs_for_common()

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

                   lowerCamelCase__		:				int    =  ["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							:			int       ):


             '''simple docstring'''



             pass





       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[Any]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			List[str]       ):


             '''simple docstring'''



             def check_hidden_states_output(__lowerCamelCase							:			Dict					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Any       ):
                   lowerCamelCase__		:				Optional[int]    =  model_class(__lowerCamelCase       )
                   model.to(__lowerCamelCase       )
                   model.eval()

                   with torch.no_grad():
                         lowerCamelCase__		:				Union[str, Any]    =  model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase       )       )

                   lowerCamelCase__		:				Optional[Any]    =  outputs.hidden_states

                   lowerCamelCase__		:				Any    =  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__		:				int    =  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__		:				Dict    =  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   lowerCamelCase__		:				Tuple    =  True
                   check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )

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

                   check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[str]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCamelCase       )





       @slow
       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



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



def       lowercase_		(		):

      """simple docstring"""




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







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


       """simple docstring"""




       @cached_property
       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



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





       @slow
       def 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[str]    =  MobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"       ).to(__lowerCamelCase       )

             lowerCamelCase__		:				Union[str, Any]    =  self.default_image_processor
             lowerCamelCase__		:				int    =  prepare_img()
             lowerCamelCase__		:				Dict    =  image_processor(images=__lowerCamelCase					,      return_tensors="pt"       ).to(__lowerCamelCase       )

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

             # verify the logits
             lowerCamelCase__		:				Union[str, Any]    =  torch.Size((1, 1000)       )
             self.assertEqual(outputs.logits.shape					,      __lowerCamelCase       )

             lowerCamelCase__		:				List[str]    =  torch.tensor([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]       ).to(__lowerCamelCase       )

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





       @slow
       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             lowerCamelCase__		:				Union[str, Any]    =  MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )
             lowerCamelCase__		:				Optional[Any]    =  model.to(__lowerCamelCase       )

             lowerCamelCase__		:				Dict    =  MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )

             lowerCamelCase__		:				List[str]    =  prepare_img()
             lowerCamelCase__		:				Any    =  image_processor(images=__lowerCamelCase					,      return_tensors="pt"       ).to(__lowerCamelCase       )

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

             # verify the logits
             lowerCamelCase__		:				Any    =  torch.Size((1, 21, 32, 32)       )
             self.assertEqual(logits.shape					,      __lowerCamelCase       )

             lowerCamelCase__		:				str    =  torch.tensor(
                 [
                     [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
                     [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
                     [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
                 ]					,      device=__lowerCamelCase					,      )

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





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


             '''simple docstring'''



             lowerCamelCase__		:				str    =  MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )
             lowerCamelCase__		:				int    =  model.to(__lowerCamelCase       )

             lowerCamelCase__		:				Tuple    =  MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"       )

             lowerCamelCase__		:				Tuple    =  prepare_img()
             lowerCamelCase__		:				str    =  image_processor(images=__lowerCamelCase					,      return_tensors="pt"       ).to(__lowerCamelCase       )

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

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

             lowerCamelCase__		:				List[str]    =  image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase					,      target_sizes=[(50, 60)]       )
             lowerCamelCase__		:				str    =  torch.Size((50, 60)       )
             self.assertEqual(segmentation[0].shape					,      __lowerCamelCase       )

             lowerCamelCase__		:				List[Any]    =  image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase       )
             lowerCamelCase__		:				List[str]    =  torch.Size((32, 32)       )
             self.assertEqual(segmentation[0].shape					,      __lowerCamelCase       )






from __future__ import annotations

from typing import Any

def       lowercase_		(		_A			:    list[Any]						):

      """simple docstring"""




      create_state_space_tree(_A    ,					[]    ,					0						)

def       lowercase_		(		_A			:    list[Any]    ,					_A			:    list[Any]    ,					_A			:    int						):

      """simple docstring"""




      if index == len(_A						):
            print(_A						)
            return

      create_state_space_tree(_A    ,					_A    ,					index + 1						)
      current_subsequence.append(sequence[index]						)
      create_state_space_tree(_A    ,					_A    ,					index + 1						)
      current_subsequence.pop()


if __name__ == "__main__":
    A      :    list[Any]		=					[3, 1, 2, 4]
    generate_all_subsequences(seq)

    seq.clear()
    seq.extend(["A", "B", "C"])
    generate_all_subsequences(seq)


