






from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor



class 							SCREAMING_SNAKE_CASE__		:





					"""simple docstring"""






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



					def  _lowercase  (      self  :						int ):
						snake_case__				:   Optional[Any]     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

						snake_case__				:   List[str]     =						None
						if self.use_labels:
							snake_case__				:   Optional[int]     =						ids_tensor([self.batch_size]							,	self.num_labels )

						snake_case__				:   List[Any]     =						self.get_config()
						return config, pixel_values, labels



					def  _lowercase  (      self  :						List[str] ):
						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							,	)



					def  _lowercase  (      self  :						Optional[int]							,	__A  :						List[str]							,	__A  :						Optional[int]							,	__A  :						List[Any] ):
						snake_case__				:   Union[str, Any]     =						TFRegNetModel(config=__A )
						snake_case__				:   Any     =						model(__A							,	training=__A )
						# expected last hidden states: B, C, H // 32, W // 32
						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  _lowercase  (      self  :						Tuple							,	__A  :						Dict							,	__A  :						int							,	__A  :						Optional[Any] ):
						snake_case__				:   Tuple     =						self.num_labels
						snake_case__				:   Tuple     =						TFRegNetForImageClassification(__A )
						snake_case__				:   int     =						model(__A							,	labels=__A							,	training=__A )
						self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels) )






					def  _lowercase  (      self  :						int ):
						snake_case__				:   Tuple     =						self.prepare_config_and_inputs()
						snake_case__,		snake_case__,		snake_case__				:   Optional[Any]     =						config_and_inputs
						snake_case__				:   List[Any]     =						{"pixel_values": pixel_values}
						return config, inputs_dict



@require_tf
class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_				,     UpperCamelCase_				,     unittest.TestCase		):





					"""simple docstring"""






					a_			=	(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
					a_			=	(
					    {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification}
					    if is_tf_available()
					    else {}
					)

					a_			=	False
					a_			=	False
					a_			=	False
					a_			=	False
					a_			=	False



					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   Tuple     =						TFRegNetModelTester(self )
						snake_case__				:   List[Any]     =						ConfigTester(self							,	config_class=__A							,	has_text_modality=__A )



					def  _lowercase  (      self  :						Dict ):
						return



					@unittest.skip(reason="RegNet does not use inputs_embeds" )
					def  _lowercase  (      self  :						List[Any] ):
						pass



					@unittest.skipIf(
					    not is_tf_available() or len(tf.config.list_physical_devices("GPU" ) ) == 0							,	reason="TF does not support backprop for grouped convolutions on CPU."							,	)
					@slow
					def  _lowercase  (      self  :						List[Any] ):
						super().test_keras_fit()



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



					def  _lowercase  (      self  :						Tuple ):
						snake_case__,		snake_case__				:   Dict     =						self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							snake_case__				:   Any     =						model_class(__A )
							snake_case__				:   List[str]     =						inspect.signature(model.call )
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							snake_case__				:   Union[str, Any]     =						[*signature.parameters.keys()]

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



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   str     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__A )



					def  _lowercase  (      self  :						Tuple ):
						def check_hidden_states_output(__A  :						List[str]							,	__A  :						Tuple							,	__A  :						str ):
							snake_case__				:   List[Any]     =						model_class(__A )
							snake_case__				:   List[str]     =						model(**self._prepare_for_class(__A							,	__A )							,	training=__A )

							snake_case__				:   Optional[Any]     =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							snake_case__				:   Optional[Any]     =						self.model_tester.num_stages
							self.assertEqual(len(__A )							,	expected_num_stages + 1 )

							# RegNet's feature maps are of shape (batch_size, num_channels, height, width)
							self.assertListEqual(
							    list(hidden_states[0].shape[-2:] )							,	[self.model_tester.image_size // 2, self.model_tester.image_size // 2]							,	)

						snake_case__,		snake_case__				:   str     =						self.model_tester.prepare_config_and_inputs_for_common()
						snake_case__				:   int     =						["basic", "bottleneck"]
						for model_class in self.all_model_classes:
							for layer_type in layers_type:
								snake_case__				:   Optional[int]     =						layer_type
								snake_case__				:   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"]
								snake_case__				:   Dict     =						True

								check_hidden_states_output(__A							,	__A							,	__A )



					def  _lowercase  (      self  :						List[str] ):
						snake_case__,		snake_case__				:   str     =						self.model_tester.prepare_config_and_inputs_for_common()

						def check_equivalence(__A  :						List[Any]							,	__A  :						str							,	__A  :						List[Any]							,	__A  :						Tuple={} ):
							snake_case__				:   Tuple     =						model(__A							,	return_dict=__A							,	**__A )
							snake_case__				:   str     =						model(__A							,	return_dict=__A							,	**__A ).to_tuple()

							def recursive_check(__A  :						Tuple							,	__A  :						Dict ):
								if isinstance(__A							,	(List, Tuple) ):
									for tuple_iterable_value, dict_iterable_value in zip(__A							,	__A ):
										recursive_check(__A							,	__A )
								elif tuple_object is None:
									return
								else:
									self.assertTrue(
									    all(tf.equal(__A							,	__A ) )							,	msg=(
									        "Tuple and dict output are not equal. Difference:"
									        f''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object ) )}'''
									    )							,	)

								recursive_check(__A							,	__A )

						for model_class in self.all_model_classes:
							snake_case__				:   List[str]     =						model_class(__A )

							snake_case__				:   Union[str, Any]     =						self._prepare_for_class(__A							,	__A )
							snake_case__				:   Tuple     =						self._prepare_for_class(__A							,	__A )
							check_equivalence(__A							,	__A							,	__A )

							snake_case__				:   Union[str, Any]     =						self._prepare_for_class(__A							,	__A							,	return_labels=__A )
							snake_case__				:   Any     =						self._prepare_for_class(__A							,	__A							,	return_labels=__A )
							check_equivalence(__A							,	__A							,	__A )

							snake_case__				:   List[Any]     =						self._prepare_for_class(__A							,	__A )
							snake_case__				:   List[str]     =						self._prepare_for_class(__A							,	__A )
							check_equivalence(__A							,	__A							,	__A							,	{"output_hidden_states": True} )

							snake_case__				:   Dict     =						self._prepare_for_class(__A							,	__A							,	return_labels=__A )
							snake_case__				:   Any     =						self._prepare_for_class(__A							,	__A							,	return_labels=__A )
							check_equivalence(__A							,	__A							,	__A							,	{"output_hidden_states": True} )



					def  _lowercase  (      self  :						Optional[Any] ):
						snake_case__				:   List[str]     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__A )






					@slow
					def  _lowercase  (      self  :						Optional[Any] ):
						for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							snake_case__				:   Dict     =						TFRegNetModel.from_pretrained(__A )
							self.assertIsNotNone(__A )







def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   List[Any]     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
	return image





@require_tf
@require_vision
class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					@cached_property
					def  _lowercase  (      self  :						Dict ):
						return (
						    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )
						    if is_vision_available()
						    else None
						)






					@slow
					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   Tuple     =						TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

						snake_case__				:   Optional[int]     =						self.default_image_processor
						snake_case__				:   List[Any]     =						prepare_img()
						snake_case__				:   List[Any]     =						image_processor(images=__A							,	return_tensors="tf" )

						# forward pass
						snake_case__				:   Dict     =						model(**__A							,	training=__A )

						# verify the logits
						snake_case__				:   Any     =						tf.TensorShape((1, 1_0_0_0) )
						self.assertEqual(outputs.logits.shape							,	__A )

						snake_case__				:   int     =						tf.constant([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6] )

						tf.debugging.assert_near(outputs.logits[0, :3]							,	__A							,	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


__lowerCamelCase      :    Union[str, Any]   		=   logging.get_logger(__name__)

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



class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	"instructblip_vision_model"



					def __init__(      self  :						List[Any]							,	__A  :						Dict=1_4_0_8							,	__A  :						Tuple=6_1_4_4							,	__A  :						str=3_9							,	__A  :						int=1_6							,	__A  :						str=2_2_4							,	__A  :						Any=1_4							,	__A  :						Dict="gelu"							,	__A  :						List[Any]=1e-6							,	__A  :						Any=0.0							,	__A  :						List[Any]=1e-1_0							,	__A  :						Union[str, Any]=True							,	**__A  :						Tuple							,	):
						super().__init__(**__A )

						snake_case__				:   List[str]     =						hidden_size
						snake_case__				:   Optional[int]     =						intermediate_size
						snake_case__				:   List[str]     =						num_hidden_layers
						snake_case__				:   List[Any]     =						num_attention_heads
						snake_case__				:   str     =						patch_size
						snake_case__				:   int     =						image_size
						snake_case__				:   int     =						initializer_range
						snake_case__				:   Optional[int]     =						attention_dropout
						snake_case__				:   str     =						layer_norm_eps
						snake_case__				:   Optional[Any]     =						hidden_act
						snake_case__				:   Tuple     =						qkv_bias






					@classmethod
					def  _lowercase  (      cls  :						List[str]							,	__A  :						Union[str, os.PathLike]							,	**__A  :						Optional[Any] ):
						cls._set_token_in_kwargs(__A )

						snake_case__,		snake_case__				:   str     =						cls.get_config_dict(__A							,	**__A )

						# get the vision config dict if we are loading from InstructBlipConfig
						if config_dict.get("model_type" ) == "instructblip":
							snake_case__				:   Union[str, Any]     =						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 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	"instructblip_qformer"



					def __init__(      self  :						Any							,	__A  :						Union[str, Any]=3_0_5_2_2							,	__A  :						Union[str, Any]=7_6_8							,	__A  :						Optional[int]=1_2							,	__A  :						Dict=1_2							,	__A  :						Dict=3_0_7_2							,	__A  :						List[str]="gelu"							,	__A  :						Union[str, Any]=0.1							,	__A  :						Tuple=0.1							,	__A  :						Any=5_1_2							,	__A  :						Optional[int]=0.0_2							,	__A  :						List[str]=1e-1_2							,	__A  :						Any=0							,	__A  :						Optional[Any]="absolute"							,	__A  :						str=2							,	__A  :						Any=1_4_0_8							,	**__A  :						List[str]							,	):
						super().__init__(pad_token_id=__A							,	**__A )

						snake_case__				:   Dict     =						vocab_size
						snake_case__				:   Optional[int]     =						hidden_size
						snake_case__				:   Optional[Any]     =						num_hidden_layers
						snake_case__				:   str     =						num_attention_heads
						snake_case__				:   int     =						hidden_act
						snake_case__				:   Optional[Any]     =						intermediate_size
						snake_case__				:   Union[str, Any]     =						hidden_dropout_prob
						snake_case__				:   List[Any]     =						attention_probs_dropout_prob
						snake_case__				:   List[Any]     =						max_position_embeddings
						snake_case__				:   int     =						initializer_range
						snake_case__				:   Dict     =						layer_norm_eps
						snake_case__				:   str     =						position_embedding_type
						snake_case__				:   Dict     =						cross_attention_frequency
						snake_case__				:   List[str]     =						encoder_hidden_size






					@classmethod
					def  _lowercase  (      cls  :						List[Any]							,	__A  :						Union[str, os.PathLike]							,	**__A  :						Optional[int] ):
						cls._set_token_in_kwargs(__A )

						snake_case__,		snake_case__				:   Tuple     =						cls.get_config_dict(__A							,	**__A )

						# get the qformer config dict if we are loading from InstructBlipConfig
						if config_dict.get("model_type" ) == "instructblip":
							snake_case__				:   List[Any]     =						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 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	"instructblip"
					a_			=	True



					def __init__(      self  :						List[str]							,	__A  :						Optional[Any]=None							,	__A  :						Tuple=None							,	__A  :						Optional[int]=None							,	__A  :						Optional[Any]=3_2							,	**__A  :						Optional[int] ):
						super().__init__(**__A )

						if vision_config is None:
							snake_case__				:   Any     =						{}
							logger.info("vision_config is None. initializing the InstructBlipVisionConfig with default values." )

						if qformer_config is None:
							snake_case__				:   Optional[Any]     =						{}
							logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values." )

						if text_config is None:
							snake_case__				:   Optional[int]     =						{}
							logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)." )

						snake_case__				:   List[Any]     =						InstructBlipVisionConfig(**__A )
						snake_case__				:   Union[str, Any]     =						InstructBlipQFormerConfig(**__A )
						snake_case__				:   Dict     =						text_config["model_type"] if "model_type" in text_config else "opt"
						snake_case__				:   List[Any]     =						CONFIG_MAPPING[text_model_type](**__A )

						snake_case__				:   Union[str, Any]     =						self.text_config.tie_word_embeddings
						snake_case__				:   Tuple     =						self.text_config.is_encoder_decoder

						snake_case__				:   str     =						num_query_tokens
						snake_case__				:   Dict     =						self.vision_config.hidden_size
						snake_case__				:   List[Any]     =						self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						snake_case__				:   int     =						1.0
						snake_case__				:   Optional[int]     =						0.0_2



					@classmethod
					def  _lowercase  (      cls  :						List[str]							,	__A  :						InstructBlipVisionConfig							,	__A  :						InstructBlipQFormerConfig							,	__A  :						PretrainedConfig							,	**__A  :						int							,	):
						return cls(
						    vision_config=vision_config.to_dict()							,	qformer_config=qformer_config.to_dict()							,	text_config=text_config.to_dict()							,	**__A							,	)






					def  _lowercase  (      self  :						Optional[int] ):
						snake_case__				:   Any     =						copy.deepcopy(self.__dict__ )
						snake_case__				:   Optional[Any]     =						self.vision_config.to_dict()
						snake_case__				:   List[str]     =						self.qformer_config.to_dict()
						snake_case__				:   List[Any]     =						self.text_config.to_dict()
						snake_case__				:   List[Any]     =						self.__class__.model_type
						return output



