





import inspect
import unittest

from transformers import MobileNetVaConfig
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 MobileNetVaForImageClassification, MobileNetVaModel
    from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import MobileNetVaImageProcessor



class       A__     ( __snake_case    ):





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      int 			=     self.config_class(**self.inputs_dict     )
       self.parent.assertTrue(hasattr(A_ ,							"tf_padding"     )     )
       self.parent.assertTrue(hasattr(A_ ,							"depth_multiplier"     )     )



class       A__     :





      def __init__(				self ,							A_ ,							A_=13 ,							A_=3 ,							A_=32 ,							A_=0.25 ,							A_=8 ,							A_=True ,							A_=1024 ,							A_=32 ,							A_="relu6" ,							A_=0.1 ,							A_=0.02 ,							A_=True ,							A_=True ,							A_=10 ,							A_=None ,							):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     parent
       UpperCamelCase			:      Optional[int] 			=     batch_size
       UpperCamelCase			:      Optional[int] 			=     num_channels
       UpperCamelCase			:      Tuple 			=     image_size
       UpperCamelCase			:      Any 			=     depth_multiplier
       UpperCamelCase			:      Any 			=     min_depth
       UpperCamelCase			:      Dict 			=     tf_padding
       UpperCamelCase			:      int 			=     int(last_hidden_size * depth_multiplier     )
       UpperCamelCase			:      Optional[int] 			=     output_stride
       UpperCamelCase			:      Any 			=     hidden_act
       UpperCamelCase			:      Any 			=     classifier_dropout_prob
       UpperCamelCase			:      Optional[int] 			=     use_labels
       UpperCamelCase			:      List[str] 			=     is_training
       UpperCamelCase			:      List[Any] 			=     num_labels
       UpperCamelCase			:      Optional[Any] 			=     initializer_range
       UpperCamelCase			:      Union[str, Any] 			=     scope





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      int 			=     None
       UpperCamelCase			:      Dict 			=     None
       if self.use_labels:
        UpperCamelCase			:      Any 			=     ids_tensor([self.batch_size] ,							self.num_labels     )
        UpperCamelCase			:      Union[str, Any] 			=     ids_tensor([self.batch_size, self.image_size, self.image_size] ,							self.num_labels     )

       UpperCamelCase			:      Union[str, Any] 			=     self.get_config()

       return config, pixel_values, labels, pixel_labels





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return MobileNetVaConfig(
           num_channels=self.num_channels ,							image_size=self.image_size ,							depth_multiplier=self.depth_multiplier ,							min_depth=self.min_depth ,							tf_padding=self.tf_padding ,							hidden_act=self.hidden_act ,							classifier_dropout_prob=self.classifier_dropout_prob ,							initializer_range=self.initializer_range ,							)





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     MobileNetVaModel(config=A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      List[Any] 			=     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  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      List[Any] 			=     self.num_labels
       UpperCamelCase			:      List[str] 			=     MobileNetVaForImageClassification(A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      Tuple 			=     model(A_ ,							labels=A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     self.prepare_config_and_inputs()
       UpperCamelCase			,					UpperCamelCase			,					UpperCamelCase			,					UpperCamelCase			:      int 			=     config_and_inputs
       UpperCamelCase			:      Optional[Any] 			=     {"pixel_values": pixel_values}
       return config, inputs_dict



@require_torch
class       A__     ( __snake_case ,     __snake_case ,     unittest.TestCase    ):
      _UpperCAmelCase			:List[Any]							=     (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
      _UpperCAmelCase			:Optional[int]							=     (
          {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
          if is_torch_available()
          else {}
      )

      _UpperCAmelCase			:Optional[Any]							=     False
      _UpperCAmelCase			:List[str]							=     False
      _UpperCAmelCase			:int							=     False
      _UpperCAmelCase			:int							=     False





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     MobileNetVaModelTester(self     )
       UpperCamelCase			:      List[str] 			=     MobileNetVaConfigTester(self ,							config_class=A_ ,							has_text_modality=A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      @unittest.skip(reason="MobileNetV1 does not use inputs_embeds"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      @unittest.skip(reason="MobileNetV1 does not support input and output embeddings"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      @unittest.skip(reason="MobileNetV1 does not output attentions"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			,					UpperCamelCase			:      Any 			=     self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCamelCase			:      Optional[Any] 			=     ["pixel_values"]
        self.assertListEqual(arg_names[:1] ,							A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       def check_hidden_states_output(A_ ,							A_ ,							A_     ):
        UpperCamelCase			:      Dict 			=     model_class(A_     )
        model.to(A_     )
        model.eval()

        with torch.no_grad():
         UpperCamelCase			:      str 			=     model(**self._prepare_for_class(A_ ,							A_     )     )

        UpperCamelCase			:      Union[str, Any] 			=     outputs.hidden_states

        UpperCamelCase			:      List[str] 			=     26
        self.assertEqual(len(A_     ) ,							A_     )

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

       for model_class in self.all_model_classes:
        UpperCamelCase			:      Optional[int] 			=     True
        check_hidden_states_output(A_ ,							A_ ,							A_     )

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

        check_hidden_states_output(A_ ,							A_ ,							A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase			:      Tuple 			=     MobileNetVaModel.from_pretrained(A_     )
        self.assertIsNotNone(A_     )



def 	A_       (  )		->				Any:
 UpperCamelCase			:      List[str] 			=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
 return image



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





      @cached_property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return (
           MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"     ) if is_vision_available() else None
       )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Tuple 			=     MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"     ).to(A_     )

       UpperCamelCase			:      List[Any] 			=     self.default_image_processor
       UpperCamelCase			:      Dict 			=     prepare_img()
       UpperCamelCase			:      Any 			=     image_processor(images=A_ ,							return_tensors="pt"     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      Any 			=     model(**A_     )

       # verify the logits
       UpperCamelCase			:      Optional[int] 			=     torch.Size((1, 1001)     )
       self.assertEqual(outputs.logits.shape ,							A_     )

       UpperCamelCase			:      Any 			=     torch.tensor([-4.17_39, -1.12_33, 3.12_05]     ).to(A_     )

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







from math import loga



def 	A_       (  _lowerCAmelCase  )		->				int:
 if a < 0:
  raise ValueError("Input value must be a positive integer"  )
 elif isinstance(_lowerCAmelCase							,     _lowerCAmelCase  ):
  raise TypeError("Input value must be a 'int' type"  )
 return 0 if (a == 0) else int(loga(a & -a  )  )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
