

"""simple docstring"""





import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

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


if is_flax_available():
							import jax
							import jax.numpy as jnp

							from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor







class 				SCREAMING_SNAKE_CASE					(		unittest.TestCase       ):






					"""simple docstring"""



					def __init__(       self			:				int   ,lowercase_			:				Tuple   ,lowercase_			:				int=3   ,lowercase_			:				Dict=3_2   ,lowercase_			:				Optional[int]=3   ,lowercase_			:				Tuple=1_0   ,lowercase_			:				Tuple=[1_0, 2_0, 3_0, 4_0]   ,lowercase_			:				List[str]=[1, 1, 2, 1]   ,lowercase_			:				Dict=True   ,lowercase_			:				str=True   ,lowercase_			:				List[Any]="relu"   ,lowercase_			:				List[str]=3   ,lowercase_			:				int=None   ,):
						lowerCAmelCase__	:						Tuple  				=	parent
						lowerCAmelCase__	:						Tuple  				=	batch_size
						lowerCAmelCase__	:						str  				=	image_size
						lowerCAmelCase__	:						Optional[Any]  				=	num_channels
						lowerCAmelCase__	:						Tuple  				=	embeddings_size
						lowerCAmelCase__	:						Any  				=	hidden_sizes
						lowerCAmelCase__	:						Any  				=	depths
						lowerCAmelCase__	:						List[Any]  				=	is_training
						lowerCAmelCase__	:						int  				=	use_labels
						lowerCAmelCase__	:						List[str]  				=	hidden_act
						lowerCAmelCase__	:						Any  				=	num_labels
						lowerCAmelCase__	:						Optional[int]  				=	scope
						lowerCAmelCase__	:						Union[str, Any]  				=	len(lowercase_		)







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

						lowerCAmelCase__	:						List[Any]  				=	self.get_config()

						return config, pixel_values







					def        __lowerCAmelCase (       self			:				Any		):
						return RegNetConfig(
						    num_channels=self.num_channels   ,embeddings_size=self.embeddings_size   ,hidden_sizes=self.hidden_sizes   ,depths=self.depths   ,hidden_act=self.hidden_act   ,num_labels=self.num_labels   ,image_size=self.image_size   ,)







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				Dict   ,lowercase_			:				Tuple		):
						lowerCAmelCase__	:						List[Any]  				=	FlaxRegNetModel(config=lowercase_		)
						lowerCAmelCase__	:						List[str]  				=	model(lowercase_		)

						# Output shape (b, c, h, w)
						self.parent.assertEqual(
						    result.last_hidden_state.shape   ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)   ,)







					def        __lowerCAmelCase (       self			:				Any   ,lowercase_			:				int   ,lowercase_			:				Dict		):
						lowerCAmelCase__	:						Optional[Any]  				=	self.num_labels
						lowerCAmelCase__	:						int  				=	FlaxRegNetForImageClassification(config=lowercase_		)
						lowerCAmelCase__	:						Tuple  				=	model(lowercase_		)
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)		)







					def        __lowerCAmelCase (       self			:				List[str]		):
						lowerCAmelCase__	:						Union[str, Any]  				=	self.prepare_config_and_inputs()
						lowerCAmelCase__				,lowerCAmelCase__	:						Tuple  				=	config_and_inputs
						lowerCAmelCase__	:						Dict  				=	{'''pixel_values''': pixel_values}
						return config, inputs_dict







@require_flax
class 				SCREAMING_SNAKE_CASE					(		a_    ,							unittest.TestCase       ):






					"""simple docstring"""



					lowercase__				 =       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

					lowercase__				 =       False
					lowercase__				 =       False
					lowercase__				 =       False







					def        __lowerCAmelCase (       self			:				List[Any]		):
						lowerCAmelCase__	:						Any  				=	FlaxRegNetModelTester(self		)
						lowerCAmelCase__	:						Tuple  				=	ConfigTester(self   ,config_class=lowercase_   ,has_text_modality=lowercase_		)







					def        __lowerCAmelCase (       self			:				Optional[int]		):
						self.create_and_test_config_common_properties()
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()







					def        __lowerCAmelCase (       self			:				int		):
						return







					def        __lowerCAmelCase (       self			:				Optional[Any]		):
						lowerCAmelCase__	:						Optional[int]  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase_		)







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__	:						List[str]  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowercase_		)







					@unittest.skip(reason='''RegNet does not use inputs_embeds'''		)
					def        __lowerCAmelCase (       self			:				Any		):
						pass







					@unittest.skip(reason='''RegNet does not support input and output embeddings'''		)
					def        __lowerCAmelCase (       self			:				Optional[int]		):
						pass







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__				,lowerCAmelCase__	:						Union[str, Any]  				=	self.model_tester.prepare_config_and_inputs_for_common()

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

							lowerCAmelCase__	:						int  				=	['''pixel_values''']
							self.assertListEqual(arg_names[:1]   ,lowercase_		)







					def        __lowerCAmelCase (       self			:				Dict		):
						def check_hidden_states_output(lowercase_			:				Union[str, Any]   ,lowercase_			:				Tuple   ,lowercase_			:				int		):
							lowerCAmelCase__	:						List[str]  				=	model_class(lowercase_		)

							lowerCAmelCase__	:						int  				=	model(**self._prepare_for_class(lowercase_   ,lowercase_		)		)

							lowerCAmelCase__	:						Optional[int]  				=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							lowerCAmelCase__	:						int  				=	self.model_tester.num_stages
							self.assertEqual(len(lowercase_		)   ,expected_num_stages + 1		)

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

						for model_class in self.all_model_classes:
							lowerCAmelCase__	:						Tuple  				=	True
							check_hidden_states_output(lowercase_   ,lowercase_   ,lowercase_		)

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

							check_hidden_states_output(lowercase_   ,lowercase_   ,lowercase_		)







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__				,lowerCAmelCase__	:						Union[str, Any]  				=	self.model_tester.prepare_config_and_inputs_for_common()

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

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

								with self.subTest('''JIT Enabled'''		):
									lowerCAmelCase__	:						int  				=	model_jitted(**lowercase_		).to_tuple()

								with self.subTest('''JIT Disabled'''		):
									with jax.disable_jit():
										lowerCAmelCase__	:						Tuple  				=	model_jitted(**lowercase_		).to_tuple()

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

def   __SCREAMING_SNAKE_CASE  (      ):
	lowerCAmelCase__	:						Optional[int]  				=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
	return image







@require_flax
class 				SCREAMING_SNAKE_CASE					(		unittest.TestCase       ):






					"""simple docstring"""



					@cached_property
					def        __lowerCAmelCase (       self			:				List[str]		):
						return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''		) if is_vision_available() else None







					@slow
					def        __lowerCAmelCase (       self			:				str		):
						lowerCAmelCase__	:						Tuple  				=	FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''		)

						lowerCAmelCase__	:						Optional[int]  				=	self.default_image_processor
						lowerCAmelCase__	:						List[Any]  				=	prepare_img()
						lowerCAmelCase__	:						List[Any]  				=	image_processor(images=lowercase_   ,return_tensors='''np'''		)

						lowerCAmelCase__	:						int  				=	model(**lowercase_		)

						# verify the logits
						lowerCAmelCase__	:						Optional[Any]  				=	(1, 1_0_0_0)
						self.assertEqual(outputs.logits.shape   ,lowercase_		)

						lowerCAmelCase__	:						Optional[int]  				=	jnp.array([-0.4180, -1.5051, -3.4836]		)

						self.assertTrue(jnp.allclose(outputs.logits[0, :3]   ,lowercase_   ,atol=1E-4		)		)








import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


_A		       =						logging.get_logger(__name__)

_A		       =						{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  "instructblip_vision_model"






    def __init__(			self					,   A_=1408					,   A_=6144					,   A_=39					,   A_=16					,   A_=224					,   A_=14					,   A_="gelu"					,   A_=1E-6					,   A_=0.0					,   A_=1E-10					,   A_=True					,   **A_					,   )  ->       Tuple:
         super().__init__(**A_    )

         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=patch_size
         __UpperCamelCase    							=image_size
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=attention_dropout
         __UpperCamelCase    							=layer_norm_eps
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=qkv_bias






    @classmethod
    def _a					(			cls					,   A_					,   **A_    )  ->       "PretrainedConfig":
         cls._set_token_in_kwargs(A_    )

         __UpperCamelCase							,      __UpperCamelCase    							=cls.get_config_dict(A_					,   **A_    )

         # get the vision config dict if we are loading from InstructBlipConfig
         if config_dict.get('model_type'    ) == "instructblip":
              __UpperCamelCase    							=config_dict['vision_config']

         if "model_type" in config_dict and hasattr(cls					,   'model_type'    ) and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                  f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

         return cls.from_dict(A_					,   **A_    )






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[Any]      =  "instructblip_qformer"






    def __init__(			self					,   A_=30522					,   A_=768					,   A_=12					,   A_=12					,   A_=3072					,   A_="gelu"					,   A_=0.1					,   A_=0.1					,   A_=512					,   A_=0.02					,   A_=1E-12					,   A_=0					,   A_="absolute"					,   A_=2					,   A_=1408					,   **A_					,   )  ->       Optional[Any]:
         super().__init__(pad_token_id=A_					,   **A_    )

         __UpperCamelCase    							=vocab_size
         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=hidden_dropout_prob
         __UpperCamelCase    							=attention_probs_dropout_prob
         __UpperCamelCase    							=max_position_embeddings
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=layer_norm_eps
         __UpperCamelCase    							=position_embedding_type
         __UpperCamelCase    							=cross_attention_frequency
         __UpperCamelCase    							=encoder_hidden_size






    @classmethod
    def _a					(			cls					,   A_					,   **A_    )  ->       "PretrainedConfig":
         cls._set_token_in_kwargs(A_    )

         __UpperCamelCase							,      __UpperCamelCase    							=cls.get_config_dict(A_					,   **A_    )

         # get the qformer config dict if we are loading from InstructBlipConfig
         if config_dict.get('model_type'    ) == "instructblip":
              __UpperCamelCase    							=config_dict['qformer_config']

         if "model_type" in config_dict and hasattr(cls					,   'model_type'    ) and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                  f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

         return cls.from_dict(A_					,   **A_    )






class     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]      =  "instructblip"
    UpperCAmelCase__						:		Optional[Any]      =  True






    def __init__(			self					,   A_=None					,   A_=None					,   A_=None					,   A_=32					,   **A_    )  ->       List[str]:
         super().__init__(**A_    )

         if vision_config is None:
              __UpperCamelCase    							={}
              logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'    )

         if qformer_config is None:
              __UpperCamelCase    							={}
              logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'    )

         if text_config is None:
              __UpperCamelCase    							={}
              logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'    )

         __UpperCamelCase    							=InstructBlipVisionConfig(**A_    )
         __UpperCamelCase    							=InstructBlipQFormerConfig(**A_    )
         __UpperCamelCase    							=text_config['model_type'] if 'model_type' in text_config else 'opt'
         __UpperCamelCase    							=CONFIG_MAPPING[text_model_type](**A_    )

         __UpperCamelCase    							=self.text_config.tie_word_embeddings
         __UpperCamelCase    							=self.text_config.is_encoder_decoder

         __UpperCamelCase    							=num_query_tokens
         __UpperCamelCase    							=self.vision_config.hidden_size
         __UpperCamelCase    							=self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
         __UpperCamelCase    							=1.0
         __UpperCamelCase    							=0.02






    @classmethod
    def _a					(			cls					,   A_					,   A_					,   A_					,   **A_					,   )  ->       Optional[Any]:
         return cls(
             vision_config=vision_config.to_dict()					,   qformer_config=qformer_config.to_dict()					,   text_config=text_config.to_dict()					,   **A_					,   )






    def _a					(			self    )  ->       Optional[Any]:
         __UpperCamelCase    							=copy.deepcopy(self.__dict__    )
         __UpperCamelCase    							=self.vision_config.to_dict()
         __UpperCamelCase    							=self.qformer_config.to_dict()
         __UpperCamelCase    							=self.text_config.to_dict()
         __UpperCamelCase    							=self.__class__.model_type
         return output





