



"""simple docstring"""







from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import numpy as np
 import tensorflow as tf

 from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
 from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import MobileViTImageProcessor

class       UpperCamelCase  (					a				):





     """simple docstring"""






     def 						A__  (					self :							Optional[Any] ):
          A__				=   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       UpperCamelCase  :





     """simple docstring"""






     def __init__(					self :							int	,      _lowerCamelCase :							List[str]	,      _lowerCamelCase :							List[Any]=1_3	,      _lowerCamelCase :							Union[str, Any]=3_2	,      _lowerCamelCase :							Optional[int]=2	,      _lowerCamelCase :							int=3	,      _lowerCamelCase :							str=6_4_0	,      _lowerCamelCase :							int=4	,      _lowerCamelCase :							Union[str, Any]="silu"	,      _lowerCamelCase :							str=3	,      _lowerCamelCase :							Tuple=3_2	,      _lowerCamelCase :							Optional[int]=0.1	,      _lowerCamelCase :							int=0.1	,      _lowerCamelCase :							Any=0.1	,      _lowerCamelCase :							List[str]=0.02	,      _lowerCamelCase :							List[str]=True	,      _lowerCamelCase :							Optional[int]=True	,      _lowerCamelCase :							Any=1_0	,      _lowerCamelCase :							Union[str, Any]=None	,      ):
          A__				=   parent
          A__				=   batch_size
          A__				=   image_size
          A__				=   patch_size
          A__				=   num_channels
          A__				=   last_hidden_size
          A__				=   num_attention_heads
          A__				=   hidden_act
          A__				=   conv_kernel_size
          A__				=   output_stride
          A__				=   hidden_dropout_prob
          A__				=   attention_probs_dropout_prob
          A__				=   classifier_dropout_prob
          A__				=   use_labels
          A__				=   is_training
          A__				=   num_labels
          A__				=   initializer_range
          A__				=   scope





     def 						A__  (					self :							Union[str, Any] ):
          A__				=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

          A__				=   None
          A__				=   None
          if self.use_labels:
               A__				=   ids_tensor([self.batch_size]	,      self.num_labels )
               A__				=   ids_tensor([self.batch_size, self.image_size, self.image_size]	,      self.num_labels )

          A__				=   self.get_config()

          return config, pixel_values, labels, pixel_labels





     def 						A__  (					self :							Dict ):
          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 						A__  (					self :							Optional[int]	,      _lowerCamelCase :							Union[str, Any]	,      _lowerCamelCase :							Optional[Any]	,      _lowerCamelCase :							List[Any]	,      _lowerCamelCase :							Optional[int] ):
          A__				=   TFMobileViTModel(config=_lowerCamelCase )
          A__				=   model(_lowerCamelCase	,      training=_lowerCamelCase )
          A__				=   A__				=   self.image_size // self.output_stride
          self.parent.assertEqual(
              result.last_hidden_state.shape	,      (self.batch_size, self.last_hidden_size, expected_height, expected_width) )





     def 						A__  (					self :							Dict	,      _lowerCamelCase :							Union[str, Any]	,      _lowerCamelCase :							int	,      _lowerCamelCase :							Tuple	,      _lowerCamelCase :							Tuple ):
          A__				=   self.num_labels
          A__				=   TFMobileViTForImageClassification(_lowerCamelCase )
          A__				=   model(_lowerCamelCase	,      labels=_lowerCamelCase	,      training=_lowerCamelCase )
          self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.num_labels) )





     def 						A__  (					self :							Optional[Any]	,      _lowerCamelCase :							Any	,      _lowerCamelCase :							Union[str, Any]	,      _lowerCamelCase :							Union[str, Any]	,      _lowerCamelCase :							int ):
          A__				=   self.num_labels
          A__				=   TFMobileViTForSemanticSegmentation(_lowerCamelCase )
          A__				=   A__				=   self.image_size // self.output_stride

          A__				=   model(_lowerCamelCase	,      training=_lowerCamelCase )
          self.parent.assertEqual(
              result.logits.shape	,      (self.batch_size, self.num_labels, expected_height, expected_width) )

          A__				=   model(_lowerCamelCase	,      labels=_lowerCamelCase	,      training=_lowerCamelCase )
          self.parent.assertEqual(
              result.logits.shape	,      (self.batch_size, self.num_labels, expected_height, expected_width) )





     def 						A__  (					self :							Union[str, Any] ):
          A__				=   self.prepare_config_and_inputs()
          A__	,     A__	,     A__	,     A__				=   config_and_inputs
          A__				=   {'''pixel_values''': pixel_values}
          return config, inputs_dict


@require_tf
class       UpperCamelCase  (					a      , a      , unittest.TestCase				):





     """simple docstring"""






     _lowerCamelCase					: Tuple				   =(
         (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
         if is_tf_available()
         else ()
     )
     _lowerCamelCase					: Optional[int]				   =(
         {"feature-extraction": TFMobileViTModel, "image-classification": TFMobileViTForImageClassification}
         if is_tf_available()
         else {}
     )

     _lowerCamelCase					: Dict				   =False
     _lowerCamelCase					: Tuple				   =False
     _lowerCamelCase					: Any				   =False
     _lowerCamelCase					: Dict				   =False
     _lowerCamelCase					: Optional[int]				   =False





     def 						A__  (					self :							List[str] ):
          A__				=   TFMobileViTModelTester(self )
          A__				=   TFMobileViTConfigTester(self	,      config_class=_lowerCamelCase	,      has_text_modality=_lowerCamelCase )





     def 						A__  (					self :							int ):
          self.config_tester.run_common_tests()





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





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





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





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

          for model_class in self.all_model_classes:
               A__				=   model_class(_lowerCamelCase )
               A__				=   inspect.signature(model.call )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               A__				=   [*signature.parameters.keys()]

               A__				=   ['''pixel_values''']
               self.assertListEqual(arg_names[:1]	,      _lowerCamelCase )





     def 						A__  (					self :							Tuple ):
          A__				=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_lowerCamelCase )





     def 						A__  (					self :							Dict ):
          def check_hidden_states_output(_lowerCamelCase :							Any	,      _lowerCamelCase :							List[str]	,      _lowerCamelCase :							int ):
               A__				=   model_class(_lowerCamelCase )

               A__				=   model(**self._prepare_for_class(_lowerCamelCase	,      _lowerCamelCase ) )

               A__				=   outputs.hidden_states

               A__				=   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.
               A__				=   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 )

          A__	,     A__				=   self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               A__				=   True
               check_hidden_states_output(_lowerCamelCase	,      _lowerCamelCase	,      _lowerCamelCase )

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

               check_hidden_states_output(_lowerCamelCase	,      _lowerCamelCase	,      _lowerCamelCase )





     def 						A__  (					self :							List[Any] ):
          A__				=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase )





     def 						A__  (					self :							int ):
          A__				=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase )





     @unittest.skipIf(
         not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0	,      reason='''TF does not support backprop for grouped convolutions on CPU.'''	,      )
     def 						A__  (					self :							Optional[int] ):
          super().test_dataset_conversion()





     def 						A__  (					self :							Tuple	,      _lowerCamelCase :							Optional[int]	,      _lowerCamelCase :							str	,      _lowerCamelCase :							Optional[Any]=2E-1	,      _lowerCamelCase :							Optional[int]=2E-1 ):
          self.assertTrue(np.allclose(_lowerCamelCase	,      _lowerCamelCase	,      atol=_lowerCamelCase	,      rtol=_lowerCamelCase ) )





     @unittest.skipIf(
         not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0	,      reason='''TF does not support backprop for grouped convolutions on CPU.'''	,      )
     @slow
     def 						A__  (					self :							List[Any] ):
          A__	,     A__				=   self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               # Since `TFMobileViTModel` cannot operate with the default `fit()` method.
               if model_class.__name__ != "TFMobileViTModel":
                    A__				=   model_class(_lowerCamelCase )
                    if getattr(_lowerCamelCase	,      '''hf_compute_loss'''	,      _lowerCamelCase ):
                         super().test_keras_fit()





     def 						A__  (					self :							List[str] ):
          A__	,     A__				=   self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
               # set an ignore index to correctly test the masked loss used in
               # `TFMobileViTForSemanticSegmentation`.
               if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
                    A__				=   5

               A__				=   model_class(_lowerCamelCase )
               if getattr(_lowerCamelCase	,      '''hf_compute_loss'''	,      _lowerCamelCase ):
                    # The number of elements in the loss should be the same as the number of elements in the label
                    A__				=   self._prepare_for_class(inputs_dict.copy()	,      _lowerCamelCase	,      return_labels=_lowerCamelCase )
                    A__				=   prepared_for_class[
                        sorted(prepared_for_class.keys() - inputs_dict.keys()	,      reverse=_lowerCamelCase )[0]
                    ]
                    A__				=   added_label.shape.as_list()[:1]

                    # Test that model correctly compute the loss with kwargs
                    A__				=   self._prepare_for_class(inputs_dict.copy()	,      _lowerCamelCase	,      return_labels=_lowerCamelCase )
                    A__				=   {'''input_ids''', '''pixel_values''', '''input_features'''}
                    A__				=   possible_input_names.intersection(set(_lowerCamelCase ) ).pop()
                    A__				=   prepared_for_class.pop(_lowerCamelCase )

                    A__				=   model(_lowerCamelCase	,      **_lowerCamelCase )[0]
                    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] )

                    # Test that model correctly compute the loss when we mask some positions
                    A__				=   self._prepare_for_class(inputs_dict.copy()	,      _lowerCamelCase	,      return_labels=_lowerCamelCase )
                    A__				=   {'''input_ids''', '''pixel_values''', '''input_features'''}
                    A__				=   possible_input_names.intersection(set(_lowerCamelCase ) ).pop()
                    A__				=   prepared_for_class.pop(_lowerCamelCase )
                    if "labels" in prepared_for_class:
                         A__				=   prepared_for_class['''labels'''].numpy()
                         if len(labels.shape ) > 1 and labels.shape[1] != 1:
                              # labels[0] = -100
                              A__				=   tf.convert_to_tensor(_lowerCamelCase )
                              A__				=   model(_lowerCamelCase	,      **_lowerCamelCase )[0]
                              self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] )
                              self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) )

                # Test that model correctly compute the loss with a dict
                    A__				=   self._prepare_for_class(inputs_dict.copy()	,      _lowerCamelCase	,      return_labels=_lowerCamelCase )
                    A__				=   model(_lowerCamelCase )[0]
                    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] )

                    # Test that model correctly compute the loss with a tuple
                    A__				=   self._prepare_for_class(inputs_dict.copy()	,      _lowerCamelCase	,      return_labels=_lowerCamelCase )

                    # Get keys that were added with the _prepare_for_class function
                    A__				=   prepared_for_class.keys() - inputs_dict.keys()
                    A__				=   inspect.signature(model.call ).parameters
                    A__				=   list(signature.keys() )

                    # Create a dictionary holding the location of the tensors in the tuple
                    A__				=   {0: input_name}
                    for label_key in label_keys:
                         A__				=   signature_names.index(_lowerCamelCase )
                         A__				=   label_key
                    A__				=   sorted(tuple_index_mapping.items() )
                    # Initialize a list with their default values, update the values and convert to a tuple
                    A__				=   []

                    for name in signature_names:
                         if name != "kwargs":
                              list_input.append(signature[name].default )

                    for index, value in sorted_tuple_index_mapping:
                         A__				=   prepared_for_class[value]

                    A__				=   tuple(_lowerCamelCase )

                    # Send to model
                    A__				=   model(tuple_input[:-1] )[0]

                    self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] )





     @slow
     def 						A__  (					self :							List[str] ):
          for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               A__				=   TFMobileViTModel.from_pretrained(_lowerCamelCase )
               self.assertIsNotNone(_lowerCamelCase )



def   a_	(			):
     A__				=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
     return image

@require_tf
class       UpperCamelCase  (					unittest.TestCase				):





     """simple docstring"""






     @slow
     def 						A__  (					self :							List[Any] ):
          A__				=   TFMobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small''' )

          A__				=   MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small''' )
          A__				=   prepare_img()
          A__				=   image_processor(images=_lowerCamelCase	,      return_tensors='''tf''' )

          # forward pass
          A__				=   model(**_lowerCamelCase	,      training=_lowerCamelCase )

          # verify the logits
          A__				=   tf.TensorShape((1, 1_0_0_0) )
          self.assertEqual(outputs.logits.shape	,      _lowerCamelCase )

          A__				=   tf.constant([-1.9_364, -1.2_327, -0.4_653] )

          tf.debugging.assert_near(outputs.logits[0, :3]	,      _lowerCamelCase	,      atol=1E-4	,      rtol=1E-04 )





     @slow
     def 						A__  (					self :							Tuple ):
          # `from_pt` will be removed
          A__				=   TFMobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''' )

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

          A__				=   prepare_img()
          A__				=   image_processor(images=_lowerCamelCase	,      return_tensors='''tf''' )

          # forward pass
          A__				=   model(inputs.pixel_values	,      training=_lowerCamelCase )
          A__				=   outputs.logits

          # verify the logits
          A__				=   tf.TensorShape((1, 2_1, 3_2, 3_2) )
          self.assertEqual(logits.shape	,      _lowerCamelCase )

          A__				=   tf.constant(
              [
                  [[6.9_713, 6.9_786, 7.2_422], [7.2_893, 7.2_825, 7.4_446], [7.6_580, 7.8_797, 7.9_420]],
                  [[-10.6_869, -10.3_250, -10.3_471], [-10.4_228, -9.9_868, -9.7_132], [-11.0_405, -11.0_221, -10.7_318]],
                  [[-3.3_089, -2.8_539, -2.6_740], [-3.2_706, -2.5_621, -2.5_108], [-3.2_534, -2.6_615, -2.6_651]],
              ] )

          tf.debugging.assert_near(logits[0, :3, :3, :3]	,      _lowerCamelCase	,      rtol=1E-4	,      atol=1E-4 )








"""simple docstring"""







from math import factorial



def   a_	(			__a		,							__a      ):
     return factorial(__a      ) / (factorial(__a      ) * factorial(n - r      ))



def   a_	(			):
     A__				=   0

     for i in range(1		,							101      ):
          for j in range(1		,							i + 1      ):
               if combinations(__a		,							__a      ) > 1e6:
                    total += 1
     return total


if __name__ == "__main__":
 print(solution())



