



"""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 							_UpperCAmelCase							( unittest.TestCase      ):





     '''simple docstring'''



     def __init__(					self       ,	A       ,	A=1_3       ,	A=3_0       ,	A=2       ,	A=3       ,	A=True       ,	A=True       ,	A=3_2       ,	A=5       ,	A=4       ,	A=3_7       ,	A="gelu"       ,	A=0.1       ,	A=0.1       ,	A=1_0       ,	A=0.02       ,	)							->					Dict:
         _UpperCAmelCase							:  Union[str, Any]											=    parent
         _UpperCAmelCase							:  Any											=    batch_size
         _UpperCAmelCase							:  Union[str, Any]											=    image_size
         _UpperCAmelCase							:  Optional[int]											=    patch_size
         _UpperCAmelCase							:  Union[str, Any]											=    num_channels
         _UpperCAmelCase							:  List[str]											=    is_training
         _UpperCAmelCase							:  List[Any]											=    use_labels
         _UpperCAmelCase							:  Optional[Any]											=    hidden_size
         _UpperCAmelCase							:  List[Any]											=    num_hidden_layers
         _UpperCAmelCase							:  Union[str, Any]											=    num_attention_heads
         _UpperCAmelCase							:  int											=    intermediate_size
         _UpperCAmelCase							:  Dict											=    hidden_act
         _UpperCAmelCase							:  List[str]											=    hidden_dropout_prob
         _UpperCAmelCase							:  Dict											=    attention_probs_dropout_prob
         _UpperCAmelCase							:  Dict											=    type_sequence_label_size
         _UpperCAmelCase							:  Union[str, Any]											=    initializer_range

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





     def  __lowerCAmelCase	(					self		)							->					Tuple:
         _UpperCAmelCase							:  Any											=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         _UpperCAmelCase							:  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       ,	A       ,	A		)							->					List[str]:
         _UpperCAmelCase							:  str											=    FlaxViTModel(config=A		)
         _UpperCAmelCase							:  List[str]											=    model(A		)
         # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
         _UpperCAmelCase							:  Tuple											=    (self.image_size, self.image_size)
         _UpperCAmelCase							:  List[Any]											=    (self.patch_size, self.patch_size)
         _UpperCAmelCase							:  List[str]											=    (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       ,	A       ,	A		)							->					List[str]:
         _UpperCAmelCase							:  Any											=    self.type_sequence_label_size
         _UpperCAmelCase							:  Tuple											=    FlaxViTForImageClassification(config=A		)
         _UpperCAmelCase							:  List[Any]											=    model(A		)
         self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size)		)

         # test greyscale images
         _UpperCAmelCase							:  Dict											=    1
         _UpperCAmelCase							:  List[str]											=    FlaxViTForImageClassification(A		)

         _UpperCAmelCase							:  Tuple											=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
         _UpperCAmelCase							:  Union[str, Any]											=    model(A		)





     def  __lowerCAmelCase	(					self		)							->					List[str]:
         _UpperCAmelCase							:  int											=    self.prepare_config_and_inputs()
         (
             (
             _UpperCAmelCase
         )							,				(
             _UpperCAmelCase
         )							,				
         )							:  Any											=    config_and_inputs
         _UpperCAmelCase							:  Optional[int]											=    {'''pixel_values''': pixel_values}
         return config, inputs_dict



@require_flax
class 							_UpperCAmelCase							( a		,unittest.TestCase      ):





     '''simple docstring'''



     a__									=(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





     def  __lowerCAmelCase	(					self		)							->					None:
         _UpperCAmelCase							:  List[str]											=    FlaxViTModelTester(self		)
         _UpperCAmelCase							:  str											=    ConfigTester(self       ,	config_class=A       ,	has_text_modality=A       ,	hidden_size=3_7		)





     def  __lowerCAmelCase	(					self		)							->					int:
         self.config_tester.run_common_tests()





     def  __lowerCAmelCase	(					self		)							->					Any:
         _UpperCAmelCase							:  Optional[Any]											=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A		)





     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         _UpperCAmelCase							:  Optional[Any]											=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*A		)





     def  __lowerCAmelCase	(					self		)							->					Any:
         _UpperCAmelCase							,				_UpperCAmelCase							:  Dict											=    self.model_tester.prepare_config_and_inputs_for_common()

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

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





     def  __lowerCAmelCase	(					self		)							->					Optional[int]:
         _UpperCAmelCase							,				_UpperCAmelCase							:  str											=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             with self.subTest(model_class.__name__		):
                 _UpperCAmelCase							:  Dict											=    self._prepare_for_class(A       ,	A		)
                 _UpperCAmelCase							:  Union[str, Any]											=    model_class(A		)

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

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

                 with self.subTest('''JIT Disabled'''		):
                     with jax.disable_jit():
                         _UpperCAmelCase							:  Tuple											=    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		)							->					str:
         for model_class_name in self.all_model_classes:
             _UpperCAmelCase							:  Union[str, Any]											=    model_class_name.from_pretrained('''google/vit-base-patch16-224'''		)
             _UpperCAmelCase							:  Tuple											=    model(np.ones((1, 3, 2_2_4, 2_2_4)		)		)
             self.assertIsNotNone(A		)







"""simple docstring"""
def  lowerCamelCase_						(UpperCamelCase__		:		int     ,       UpperCamelCase__		:		int							):
    if a < 0 or b < 0:
        raise ValueError('''the value of both inputs must be positive'''							)
    _UpperCAmelCase							:  List[str]											=    str(bin(UpperCamelCase__							)							)[2:]  # remove the leading "0b"
    _UpperCAmelCase							:  str											=    str(bin(UpperCamelCase__							)							)[2:]
    _UpperCAmelCase							:  List[str]											=    max(len(UpperCamelCase__							)     ,       len(UpperCamelCase__							)							)
    return "0b" + "".join(
        str(int('''1''' in (char_a, char_b)							)							)
        for char_a, char_b in zip(a_binary.zfill(UpperCamelCase__							)     ,       b_binary.zfill(UpperCamelCase__							)							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()


