

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







   '''simple docstring'''




   def __init__(       self					:			str     , __lowerCamelCase					:			str     , __lowerCamelCase					:			Tuple=13     , __lowerCamelCase					:			Tuple=30     , __lowerCamelCase					:			Any=2     , __lowerCamelCase					:			Union[str, Any]=3     , __lowerCamelCase					:			int=True     , __lowerCamelCase					:			Optional[int]=True     , __lowerCamelCase					:			List[Any]=32     , __lowerCamelCase					:			Dict=5     , __lowerCamelCase					:			Union[str, Any]=4     , __lowerCamelCase					:			Optional[Any]=37     , __lowerCamelCase					:			Union[str, Any]="gelu"     , __lowerCamelCase					:			str=0.1     , __lowerCamelCase					:			Any=0.1     , __lowerCamelCase					:			Union[str, Any]=10     , __lowerCamelCase					:			int=0.02     , ):
       SCREAMING_SNAKE_CASE										=   parent
       SCREAMING_SNAKE_CASE										=   batch_size
       SCREAMING_SNAKE_CASE										=   image_size
       SCREAMING_SNAKE_CASE										=   patch_size
       SCREAMING_SNAKE_CASE										=   num_channels
       SCREAMING_SNAKE_CASE										=   is_training
       SCREAMING_SNAKE_CASE										=   use_labels
       SCREAMING_SNAKE_CASE										=   hidden_size
       SCREAMING_SNAKE_CASE										=   num_hidden_layers
       SCREAMING_SNAKE_CASE										=   num_attention_heads
       SCREAMING_SNAKE_CASE										=   intermediate_size
       SCREAMING_SNAKE_CASE										=   hidden_act
       SCREAMING_SNAKE_CASE										=   hidden_dropout_prob
       SCREAMING_SNAKE_CASE										=   attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE										=   type_sequence_label_size
       SCREAMING_SNAKE_CASE										=   initializer_range

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





   def      _snake_case		(       self					:			int			):
       SCREAMING_SNAKE_CASE										=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

       SCREAMING_SNAKE_CASE										=   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=__lowerCamelCase     , initializer_range=self.initializer_range     , )

       return config, pixel_values





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

       # test greyscale images
       SCREAMING_SNAKE_CASE										=   1
       SCREAMING_SNAKE_CASE										=   FlaxViTForImageClassification(__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
       SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase			)





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





@require_flax
class 			_SCREAMING_SNAKE_CASE (   __snake_case					,       unittest.TestCase		):







   '''simple docstring'''




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





   def      _snake_case		(       self					:			Any			):
       SCREAMING_SNAKE_CASE										=   FlaxViTModelTester(self			)
       SCREAMING_SNAKE_CASE										=   ConfigTester(self     , config_class=__lowerCamelCase     , has_text_modality=__lowerCamelCase     , hidden_size=37			)





   def      _snake_case		(       self					:			Dict			):
       self.config_tester.run_common_tests()





   def      _snake_case		(       self					:			Union[str, Any]			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCamelCase			)





   def      _snake_case		(       self					:			Any			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[int]			):
       SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs_for_common()

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

           SCREAMING_SNAKE_CASE										=   ["pixel_values"]
           self.assertListEqual(arg_names[:1]     , __lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[Any]			):
       SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           with self.subTest(model_class.__name__			):
               SCREAMING_SNAKE_CASE										=   self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase			)
               SCREAMING_SNAKE_CASE										=   model_class(__lowerCamelCase			)

               @jax.jit
               def model_jitted(__lowerCamelCase					:			str     , **__lowerCamelCase					:			Union[str, Any]			):
                   return model(pixel_values=__lowerCamelCase     , **__lowerCamelCase			)

               with self.subTest("JIT Enabled"			):
                   SCREAMING_SNAKE_CASE										=   model_jitted(**__lowerCamelCase			).to_tuple()

               with self.subTest("JIT Disabled"			):
                   with jax.disable_jit():
                       SCREAMING_SNAKE_CASE										=   model_jitted(**__lowerCamelCase			).to_tuple()

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





   @slow
   def      _snake_case		(       self					:			Dict			):
       for model_class_name in self.all_model_classes:
           SCREAMING_SNAKE_CASE										=   model_class_name.from_pretrained("google/vit-base-patch16-224"			)
           SCREAMING_SNAKE_CASE										=   model(np.ones((1, 3, 224, 224)			)			)
           self.assertIsNotNone(__lowerCamelCase			)
from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
					import tensorflow as tf

					from transformers import TFBlipTextModel
					from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST
class     lowerCAmelCase_     :







							def __init__(   self      :     Any ,			_A      :     int ,			_A      :     int=12 ,			_A      :     int=7 ,			_A      :     Tuple=True ,			_A      :     Optional[int]=True ,			_A      :     Union[str, Any]=True ,			_A      :     str=99 ,			_A      :     str=32 ,			_A      :     int=32 ,			_A      :     Optional[Any]=2 ,			_A      :     Dict=4 ,			_A      :     int=37 ,			_A      :     List[Any]=0.1 ,			_A      :     str=0.1 ,			_A      :     Any=512 ,			_A      :     int=0.02 ,			_A      :     Optional[Any]=0 ,			_A      :     Dict=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     seq_length
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_input_mask
												_UpperCamelCase			     =     use_labels
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     hidden_size
												_UpperCamelCase			     =     projection_dim
												_UpperCamelCase			     =     num_hidden_layers
												_UpperCamelCase			     =     num_attention_heads
												_UpperCamelCase			     =     intermediate_size
												_UpperCamelCase			     =     dropout
												_UpperCamelCase			     =     attention_dropout
												_UpperCamelCase			     =     max_position_embeddings
												_UpperCamelCase			     =     initializer_range
												_UpperCamelCase			     =     scope
												_UpperCamelCase			     =     bos_token_id







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size		)

												_UpperCamelCase			     =     None
												if self.use_input_mask:
																	_UpperCamelCase			     =     random_attention_mask([self.batch_size, self.seq_length]		)

												if input_mask is not None:
																	_UpperCamelCase			     =     input_mask.numpy()
																	_UpperCamelCase     ,    _UpperCamelCase			     =     input_mask.shape
																	_UpperCamelCase			     =     np.random.randint(1 ,			seq_length - 1 ,			size=(batch_size,)		)
																	for batch_idx, start_index in enumerate(_A		):
																						_UpperCamelCase			     =     1
																						_UpperCamelCase			     =     0

												_UpperCamelCase			     =     self.get_config()

												return config, input_ids, tf.convert_to_tensor(_A		)







							def  UpperCamelCase_						(   self      :     str		):
												return BlipTextConfig(
												    vocab_size=self.vocab_size ,			hidden_size=self.hidden_size ,			projection_dim=self.projection_dim ,			num_hidden_layers=self.num_hidden_layers ,			num_attention_heads=self.num_attention_heads ,			intermediate_size=self.intermediate_size ,			dropout=self.dropout ,			attention_dropout=self.attention_dropout ,			max_position_embeddings=self.max_position_embeddings ,			initializer_range=self.initializer_range ,			bos_token_id=self.bos_token_id ,			)







							def  UpperCamelCase_						(   self      :     List[str] ,			_A      :     Tuple ,			_A      :     str ,			_A      :     Optional[Any]		):
												_UpperCamelCase			     =     TFBlipTextModel(config=_A		)
												_UpperCamelCase			     =     model(_A ,			attention_mask=_A ,			training=_A		)
												_UpperCamelCase			     =     model(_A ,			training=_A		)
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)		)
												self.parent.assertEqual(result.pooler_output.shape ,			(self.batch_size, self.hidden_size)		)







							def  UpperCamelCase_						(   self      :     Tuple		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												_UpperCamelCase     ,    _UpperCamelCase     ,    _UpperCamelCase			     =     config_and_inputs
												_UpperCamelCase			     =     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict




@require_tf
class     lowerCAmelCase_     (       __lowercase,  unittest.TestCase ):
							UpperCAmelCase				   =     (TFBlipTextModel,) if is_tf_available() else ()
							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False
							UpperCAmelCase				   =     False







							def  UpperCamelCase_						(   self      :     Dict		):
												_UpperCamelCase			     =     BlipTextModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			hidden_size=37		)







							def  UpperCamelCase_						(   self      :     Dict		):
												self.config_tester.run_common_tests()







							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_A		)







							def  UpperCamelCase_						(   self      :     List[Any]		):
												pass







							def  UpperCamelCase_						(   self      :     Tuple		):
												pass







							@unittest.skip(reason='''Blip does not use inputs_embeds'''		)
							def  UpperCamelCase_						(   self      :     Dict		):
												pass







							@unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''		)
							def  UpperCamelCase_						(   self      :     Dict		):
												pass







							@unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''		)
							def  UpperCamelCase_						(   self      :     List[str]		):
												pass







							@slow
							def  UpperCamelCase_						(   self      :     Optional[int]		):
												for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_UpperCamelCase			     =     TFBlipTextModel.from_pretrained(_A		)
																	self.assertIsNotNone(_A		)







							def  UpperCamelCase_						(   self      :     int ,			_A      :     Optional[int]=True		):
												super().test_pt_tf_model_equivalence(allow_missing_keys=_A		)
