



'''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 	lowerCAmelCase_(    unittest.TestCase					):
  '''simple docstring'''




  def __init__(   self  ,__UpperCAmelCase  ,__UpperCAmelCase=3  ,__UpperCAmelCase=32  ,__UpperCAmelCase=3  ,__UpperCAmelCase=10  ,__UpperCAmelCase=[10, 20, 30, 40]  ,__UpperCAmelCase=[1, 1, 2, 1]  ,__UpperCAmelCase=True  ,__UpperCAmelCase=True  ,__UpperCAmelCase="relu"  ,__UpperCAmelCase=3  ,__UpperCAmelCase=None  ,)   ->		Any:
      lowerCAmelCase__			:  Union[str, Any]											= parent
      lowerCAmelCase__			:  Union[str, Any]											= batch_size
      lowerCAmelCase__			:  Optional[int]											= image_size
      lowerCAmelCase__			:  Union[str, Any]											= num_channels
      lowerCAmelCase__			:  Union[str, Any]											= embeddings_size
      lowerCAmelCase__			:  Tuple											= hidden_sizes
      lowerCAmelCase__			:  List[str]											= depths
      lowerCAmelCase__			:  List[str]											= is_training
      lowerCAmelCase__			:  Any											= use_labels
      lowerCAmelCase__			:  int											= hidden_act
      lowerCAmelCase__			:  Optional[int]											= num_labels
      lowerCAmelCase__			:  List[str]											= scope
      lowerCAmelCase__			:  Dict											= len(__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		Dict:
      lowerCAmelCase__			:  Tuple											= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

      lowerCAmelCase__			:  Tuple											= self.get_config()

      return config, pixel_values




  def 							UpperCAmelCase_     (   self				)   ->		Tuple:
      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 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		int:
      lowerCAmelCase__			:  Dict											= FlaxRegNetModel(config=__UpperCAmelCase				)
      lowerCAmelCase__			:  int											= model(__UpperCAmelCase				)

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




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		Optional[Any]:
      lowerCAmelCase__			:  Union[str, Any]											= self.num_labels
      lowerCAmelCase__			:  Optional[Any]											= FlaxRegNetForImageClassification(config=__UpperCAmelCase				)
      lowerCAmelCase__			:  List[str]											= model(__UpperCAmelCase				)
      self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)				)






  def 							UpperCAmelCase_     (   self				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Tuple											= self.prepare_config_and_inputs()
      lowerCAmelCase__				,       lowerCAmelCase__			:  Dict											= config_and_inputs
      lowerCAmelCase__			:  Optional[Any]											= {"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_flax
class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			unittest.TestCase					):
  '''simple docstring'''




  __lowercase							:				str           =				(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

  __lowercase							:				Dict           =				False
  __lowercase							:				Dict           =				False
  __lowercase							:				Any           =				False




  def 							UpperCAmelCase_     (   self				)   ->		None:
      lowerCAmelCase__			:  int											= FlaxRegNetModelTester(self				)
      lowerCAmelCase__			:  int											= ConfigTester(self  ,config_class=__UpperCAmelCase  ,has_text_modality=__UpperCAmelCase				)




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




  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__			:  Union[str, Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Dict											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)




  @unittest.skip(reason="""RegNet does not use inputs_embeds"""				)
  def 							UpperCAmelCase_     (   self				)   ->		Union[str, Any]:
      pass




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




  def 							UpperCAmelCase_     (   self				)   ->		List[Any]:
      lowerCAmelCase__				,       lowerCAmelCase__			:  Any											= self.model_tester.prepare_config_and_inputs_for_common()

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

          lowerCAmelCase__			:  List[str]											= ["""pixel_values"""]
          self.assertListEqual(arg_names[:1]  ,__UpperCAmelCase				)




  def 							UpperCAmelCase_     (   self				)   ->		int:
      def check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				):
          lowerCAmelCase__			:  Union[str, Any]											= model_class(__UpperCAmelCase				)

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

          lowerCAmelCase__			:  str											= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          lowerCAmelCase__			:  Union[str, Any]											= self.model_tester.num_stages
          self.assertEqual(len(__UpperCAmelCase				)  ,expected_num_stages + 1				)

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

      for model_class in self.all_model_classes:
          lowerCAmelCase__			:  Union[str, Any]											= True
          check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)

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

          check_hidden_states_output(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)






  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= 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(__UpperCAmelCase  ,__UpperCAmelCase				)
              lowerCAmelCase__			:  Any											= model_class(__UpperCAmelCase				)

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

              with self.subTest("""JIT Enabled"""				):
                  lowerCAmelCase__			:  Optional[Any]											= model_jitted(**__UpperCAmelCase				).to_tuple()

              with self.subTest("""JIT Disabled"""				):
                  with jax.disable_jit():
                      lowerCAmelCase__			:  int											= model_jitted(**__UpperCAmelCase				).to_tuple()

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







def        _SCREAMING_SNAKE_CASE  (	):



    """simple docstring"""





    lowerCAmelCase__			:  Optional[Any]											= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
    return image






@require_flax
class 	lowerCAmelCase_(    unittest.TestCase					):
  '''simple docstring'''




  @cached_property
  def 							UpperCAmelCase_     (   self				)   ->		str:
      return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""				) if is_vision_available() else None






  @slow
  def 							UpperCAmelCase_     (   self				)   ->		Optional[int]:
      lowerCAmelCase__			:  Dict											= FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""				)

      lowerCAmelCase__			:  str											= self.default_image_processor
      lowerCAmelCase__			:  Dict											= prepare_img()
      lowerCAmelCase__			:  Tuple											= image_processor(images=__UpperCAmelCase  ,return_tensors="""np"""				)

      lowerCAmelCase__			:  Tuple											= model(**__UpperCAmelCase				)

      # verify the logits
      lowerCAmelCase__			:  Dict											= (1, 1000)
      self.assertEqual(outputs.logits.shape  ,__UpperCAmelCase				)

      lowerCAmelCase__			:  Dict											= jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]				)

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







'''simple docstring'''





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										=     logging.get_logger(__name__)

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






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				'''instructblip_vision_model'''




  def __init__(   self  ,__UpperCAmelCase=1408  ,__UpperCAmelCase=6144  ,__UpperCAmelCase=39  ,__UpperCAmelCase=16  ,__UpperCAmelCase=224  ,__UpperCAmelCase=14  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=1E-6  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=1E-10  ,__UpperCAmelCase=True  ,**__UpperCAmelCase  ,)   ->		List[Any]:
      super().__init__(**__UpperCAmelCase				)

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






  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase				)

      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase				)

      # get the vision config dict if we are loading from InstructBlipConfig
      if config_dict.get("""model_type"""				) == "instructblip":
          lowerCAmelCase__			:  Optional[int]											= 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(__UpperCAmelCase  ,**__UpperCAmelCase				)







class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				int           =				'''instructblip_qformer'''




  def __init__(   self  ,__UpperCAmelCase=3_0522  ,__UpperCAmelCase=768  ,__UpperCAmelCase=12  ,__UpperCAmelCase=12  ,__UpperCAmelCase=3072  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=512  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-12  ,__UpperCAmelCase=0  ,__UpperCAmelCase="absolute"  ,__UpperCAmelCase=2  ,__UpperCAmelCase=1408  ,**__UpperCAmelCase  ,)   ->		Tuple:
      super().__init__(pad_token_id=__UpperCAmelCase  ,**__UpperCAmelCase				)

      lowerCAmelCase__			:  List[Any]											= vocab_size
      lowerCAmelCase__			:  Optional[Any]											= hidden_size
      lowerCAmelCase__			:  Any											= num_hidden_layers
      lowerCAmelCase__			:  int											= num_attention_heads
      lowerCAmelCase__			:  Tuple											= hidden_act
      lowerCAmelCase__			:  int											= intermediate_size
      lowerCAmelCase__			:  Tuple											= hidden_dropout_prob
      lowerCAmelCase__			:  Optional[Any]											= attention_probs_dropout_prob
      lowerCAmelCase__			:  Optional[Any]											= max_position_embeddings
      lowerCAmelCase__			:  Any											= initializer_range
      lowerCAmelCase__			:  Tuple											= layer_norm_eps
      lowerCAmelCase__			:  Dict											= position_embedding_type
      lowerCAmelCase__			:  int											= cross_attention_frequency
      lowerCAmelCase__			:  List[Any]											= encoder_hidden_size






  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase				)

      lowerCAmelCase__				,       lowerCAmelCase__			:  Any											= cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase				)

      # get the qformer config dict if we are loading from InstructBlipConfig
      if config_dict.get("""model_type"""				) == "instructblip":
          lowerCAmelCase__			:  Tuple											= 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(__UpperCAmelCase  ,**__UpperCAmelCase				)







class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Any           =				'''instructblip'''
  __lowercase							:				str           =				True




  def __init__(   self  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=32  ,**__UpperCAmelCase				)   ->		Any:
      super().__init__(**__UpperCAmelCase				)

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

      if qformer_config is None:
          lowerCAmelCase__			:  List[str]											= {}
          logger.info("""qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."""				)

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

      lowerCAmelCase__			:  Any											= InstructBlipVisionConfig(**__UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= InstructBlipQFormerConfig(**__UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= text_config["""model_type"""] if """model_type""" in text_config else """opt"""
      lowerCAmelCase__			:  Any											= CONFIG_MAPPING[text_model_type](**__UpperCAmelCase				)

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

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




  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,**__UpperCAmelCase  ,)   ->		Union[str, Any]:
      return cls(
          vision_config=vision_config.to_dict()  ,qformer_config=qformer_config.to_dict()  ,text_config=text_config.to_dict()  ,**__UpperCAmelCase  ,)






  def 							UpperCAmelCase_     (   self				)   ->		str:
      lowerCAmelCase__			:  List[Any]											= copy.deepcopy(self.__dict__				)
      lowerCAmelCase__			:  str											= self.vision_config.to_dict()
      lowerCAmelCase__			:  Union[str, Any]											= self.qformer_config.to_dict()
      lowerCAmelCase__			:  Union[str, Any]											= self.text_config.to_dict()
      lowerCAmelCase__			:  str											= self.__class__.model_type
      return output


