






"""simple docstring"""


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_UpperCamelCase    :   Dict   			=						logging.get_logger(__name__)

_UpperCamelCase    :   List[str]   			=						{
    'google/mobilenet_v1_1.0_224': 'https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json',
    'google/mobilenet_v1_0.75_192': 'https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}






class 						snake_case  (  UpperCAmelCase	):
    __magic_name__   					=					'''mobilenet_v1'''





    def __init__(							self     : Any    ,		A     : Optional[int]=3    ,		A     : str=2_2_4    ,		A     : Union[str, Any]=1.0    ,		A     : Optional[int]=8    ,		A     : Any="relu6"    ,		A     : List[Any]=True    ,		A     : Any=0.9_99    ,		A     : int=0.02    ,		A     : int=0.0_01    ,		**A     : Any    ,		):


      '''simple docstring'''




      super().__init__(**A      )

      if depth_multiplier <= 0:
        raise ValueError('depth_multiplier must be greater than zero.'      )

      a					:      Any    =       num_channels
      a					:      List[Any]    =       image_size
      a					:      Dict    =       depth_multiplier
      a					:      Union[str, Any]    =       min_depth
      a					:      Any    =       hidden_act
      a					:      Dict    =       tf_padding
      a					:      str    =       classifier_dropout_prob
      a					:      List[str]    =       initializer_range
      a					:      Any    =       layer_norm_eps






class 						snake_case  (  UpperCAmelCase	):
    __magic_name__   					=					version.parse('''1.11'''	)





    @property
    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      return OrderedDict([('pixel_values', {0: 'batch'})]      )





    @property
    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      if self.task == "image-classification":
        return OrderedDict([('logits', {0: 'batch'})]      )
      else:
        return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})]      )





    @property
    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      return 1E-4












"""simple docstring"""


import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
   import jax

   from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel






class 						snake_case  (  unittest.TestCase	):





    def __init__(							self     : List[str]    ,		A     : Union[str, Any]    ,		A     : Optional[Any]=1_3    ,		A     : List[Any]=3_0    ,		A     : List[Any]=2    ,		A     : Optional[Any]=3    ,		A     : Union[str, Any]=True    ,		A     : Union[str, Any]=True    ,		A     : Optional[int]=3_2    ,		A     : Tuple=5    ,		A     : List[str]=4    ,		A     : List[Any]=3_7    ,		A     : Optional[Any]="gelu"    ,		A     : Any=0.1    ,		A     : Tuple=0.1    ,		A     : Optional[int]=1_0    ,		A     : Union[str, Any]=0.02    ,		):


      '''simple docstring'''




      a					:      Optional[Any]    =       parent
      a					:      Tuple    =       batch_size
      a					:      int    =       image_size
      a					:      str    =       patch_size
      a					:      List[str]    =       num_channels
      a					:      List[str]    =       is_training
      a					:      List[str]    =       use_labels
      a					:      Optional[int]    =       hidden_size
      a					:      Optional[Any]    =       num_hidden_layers
      a					:      Optional[int]    =       num_attention_heads
      a					:      str    =       intermediate_size
      a					:      List[str]    =       hidden_act
      a					:      List[str]    =       hidden_dropout_prob
      a					:      Union[str, Any]    =       attention_probs_dropout_prob
      a					:      List[Any]    =       type_sequence_label_size
      a					:      Optional[Any]    =       initializer_range

      # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
      a					:      Optional[int]    =       (image_size // patch_size) ** 2
      a					:      List[Any]    =       num_patches + 1





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      a					:      List[Any]    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

      a					:      str    =       ViTConfig(
          image_size=self.image_size    ,		patch_size=self.patch_size    ,		num_channels=self.num_channels    ,		hidden_size=self.hidden_size    ,		num_hidden_layers=self.num_hidden_layers    ,		num_attention_heads=self.num_attention_heads    ,		intermediate_size=self.intermediate_size    ,		hidden_act=self.hidden_act    ,		hidden_dropout_prob=self.hidden_dropout_prob    ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,		is_decoder=A    ,		initializer_range=self.initializer_range    ,		)

      return config, pixel_values





    def     lowerCamelCase__				(							self     : Union[str, Any]    ,		A     : str    ,		A     : Union[str, Any]      ):


      '''simple docstring'''




      a					:      Tuple    =       FlaxViTModel(config=A      )
      a					:      int    =       model(A      )
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      a					:      Optional[Any]    =       (self.image_size, self.image_size)
      a					:      List[str]    =       (self.patch_size, self.patch_size)
      a					:      Optional[Any]    =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, num_patches + 1, self.hidden_size)      )





    def     lowerCamelCase__				(							self     : Tuple    ,		A     : Dict    ,		A     : Optional[int]      ):


      '''simple docstring'''




      a					:      Optional[Any]    =       self.type_sequence_label_size
      a					:      List[Any]    =       FlaxViTForImageClassification(config=A      )
      a					:      Tuple    =       model(A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.type_sequence_label_size)      )

      # test greyscale images
      a					:      Dict    =       1
      a					:      Tuple    =       FlaxViTForImageClassification(A      )

      a					:      List[Any]    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
      a					:      Optional[int]    =       model(A      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.prepare_config_and_inputs()
      (
          (
          a
      ),					(
          a
      ),					
      )					:      Dict    =       config_and_inputs
      a					:      List[Any]    =       {'pixel_values': pixel_values}
      return config, inputs_dict






@require_flax
class 						snake_case  (  UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Any    =       FlaxViTModelTester(self      )
      a					:      List[str]    =       ConfigTester(self    ,		config_class=A    ,		has_text_modality=A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A      )





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      a					:      Union[str, Any]    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*A      )





    def     lowerCamelCase__				(							self     : Dict      ):


      '''simple docstring'''




      a,					a					:      Optional[Any]    =       self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        a					:      Tuple    =       model_class(A      )
        a					:      str    =       inspect.signature(model.__call__      )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        a					:      List[str]    =       [*signature.parameters.keys()]

        a					:      Dict    =       ['pixel_values']
        self.assertListEqual(arg_names[:1]    ,		A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




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

      for model_class in self.all_model_classes:
        with self.subTest(model_class.__name__      ):
          a					:      List[Any]    =       self._prepare_for_class(A    ,		A      )
          a					:      Tuple    =       model_class(A      )

          @jax.jit
          def model_jitted(A     : Tuple    ,		**A     : int      ):
            return model(pixel_values=A    ,		**A      )

          with self.subTest('JIT Enabled'      ):
            a					:      List[str]    =       model_jitted(**A      ).to_tuple()

          with self.subTest('JIT Disabled'      ):
            with jax.disable_jit():
              a					:      List[str]    =       model_jitted(**A      ).to_tuple()

          self.assertEqual(len(A      )    ,		len(A      )      )
          for jitted_output, output in zip(A    ,		A      ):
            self.assertEqual(jitted_output.shape    ,		output.shape      )





    @slow
    def     lowerCamelCase__				(							self     : List[str]      ):


      '''simple docstring'''




      for model_class_name in self.all_model_classes:
        a					:      List[str]    =       model_class_name.from_pretrained('google/vit-base-patch16-224'      )
        a					:      Optional[Any]    =       model(np.ones((1, 3, 2_2_4, 2_2_4)      )      )
        self.assertIsNotNone(A      )




