





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







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__=1_3		,				lowerCAmelCase__=3_0		,				lowerCAmelCase__=2		,				lowerCAmelCase__=3		,				lowerCAmelCase__=True		,				lowerCAmelCase__=True		,				lowerCAmelCase__=3_2		,				lowerCAmelCase__=5		,				lowerCAmelCase__=4		,				lowerCAmelCase__=3_7		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=1_0		,				lowerCAmelCase__=0.02		,				)       ->						List[str]:



        '''simple docstring'''




        a__     : int  =parent
        a__     : str  =batch_size
        a__     : Any  =image_size
        a__     : Any  =patch_size
        a__     : Tuple  =num_channels
        a__     : Optional[int]  =is_training
        a__     : List[str]  =use_labels
        a__     : List[Any]  =hidden_size
        a__     : Tuple  =num_hidden_layers
        a__     : Optional[int]  =num_attention_heads
        a__     : Union[str, Any]  =intermediate_size
        a__     : str  =hidden_act
        a__     : Union[str, Any]  =hidden_dropout_prob
        a__     : List[str]  =attention_probs_dropout_prob
        a__     : Dict  =type_sequence_label_size
        a__     : Union[str, Any]  =initializer_range

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







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        a__     : Any  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

        a__     : List[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=lowerCAmelCase__		,				initializer_range=self.initializer_range		,				)

        return config, pixel_values







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : Optional[Any]  =FlaxViTModel(config=lowerCAmelCase__							)
        a__     : Union[str, Any]  =model(lowerCAmelCase__							)
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        a__     : Optional[Any]  =(self.image_size, self.image_size)
        a__     : Tuple  =(self.patch_size, self.patch_size)
        a__     : Optional[int]  =(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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						Dict:



        '''simple docstring'''




        a__     : int  =self.type_sequence_label_size
        a__     : str  =FlaxViTForImageClassification(config=lowerCAmelCase__							)
        a__     : Any  =model(lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)							)

        # test greyscale images
        a__     : List[Any]  =1
        a__     : int  =FlaxViTForImageClassification(lowerCAmelCase__							)

        a__     : Optional[Any]  =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
        a__     : str  =model(lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						List[Any]:



        '''simple docstring'''




        a__     : Dict  =self.prepare_config_and_inputs()
        (
            a__							
        )     : Optional[Any]  =config_and_inputs
        a__     : Dict  ={"pixel_values": pixel_values}
        return config, inputs_dict







@require_flax
class     __lowerCAmelCase	(   UpperCamelCase__						,    unittest.TestCase):
      _lowercase    : Any            =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()







      def        _lowercase  (   self							)       ->						None:



        '''simple docstring'''




        a__     : str  =FlaxViTModelTester(self							)
        a__     : List[Any]  =ConfigTester(self		,				config_class=lowerCAmelCase__		,				has_text_modality=lowerCAmelCase__		,				hidden_size=3_7							)







      def        _lowercase  (   self							)       ->						Optional[Any]:



        '''simple docstring'''




        self.config_tester.run_common_tests()







      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : List[Any]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__     : List[Any]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__     : Optional[int]  =self.model_tester.prepare_config_and_inputs_for_common()

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

          a__     : int  =["pixel_values"]
          self.assertListEqual(arg_names[:1]		,				lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__     : 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__							):
            a__     : Dict  =self._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__							)
            a__     : str  =model_class(lowerCAmelCase__							)

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

            with self.subTest("JIT Enabled"							):
              a__     : List[str]  =model_jitted(**lowerCAmelCase__							).to_tuple()

            with self.subTest("JIT Disabled"							):
              with jax.disable_jit():
                a__     : Dict  =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        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        for model_class_name in self.all_model_classes:
          a__     : Any  =model_class_name.from_pretrained("google/vit-base-patch16-224"							)
          a__     : Any  =model(np.ones((1, 3, 2_2_4, 2_2_4)							)							)
          self.assertIsNotNone(lowerCAmelCase__							)













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


UpperCAmelCase     :     List[Any]							=	logging.get_logger(__name__)

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







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_vision_model"""







      def __init__(   self		,				lowerCAmelCase__=1_4_0_8		,				lowerCAmelCase__=6_1_4_4		,				lowerCAmelCase__=3_9		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=2_2_4		,				lowerCAmelCase__=1_4		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=1E-6		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=1E-10		,				lowerCAmelCase__=True		,				**lowerCAmelCase__		,				)       ->						Optional[Any]:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : Optional[Any]  =cls.get_config_dict(lowerCAmelCase__		,				**lowerCAmelCase__							)

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







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_qformer"""







      def __init__(   self		,				lowerCAmelCase__=3_0_5_2_2		,				lowerCAmelCase__=7_6_8		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=3_0_7_2		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=5_1_2		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=1E-12		,				lowerCAmelCase__=0		,				lowerCAmelCase__="absolute"		,				lowerCAmelCase__=2		,				lowerCAmelCase__=1_4_0_8		,				**lowerCAmelCase__		,				)       ->						Dict:



        '''simple docstring'''




        super().__init__(pad_token_id=lowerCAmelCase__		,				**lowerCAmelCase__							)

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : str  =cls.get_config_dict(lowerCAmelCase__		,				**lowerCAmelCase__							)

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







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip"""
      _lowercase    : List[Any]            =      True







      def __init__(   self		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=3_2		,				**lowerCAmelCase__							)       ->						str:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

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

        if qformer_config is None:
          a__     : Tuple  ={}
          logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."							)

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

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

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

        a__     : str  =num_query_tokens
        a__     : List[Any]  =self.vision_config.hidden_size
        a__     : str  =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
        a__     : List[Any]  =1.0
        a__     : List[str]  =0.02







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				**lowerCAmelCase__		,				)       ->						int:



        '''simple docstring'''




        return cls(
            vision_config=vision_config.to_dict()		,				qformer_config=qformer_config.to_dict()		,				text_config=text_config.to_dict()		,				**lowerCAmelCase__		,				)







      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : int  =copy.deepcopy(self.__dict__							)
        a__     : int  =self.vision_config.to_dict()
        a__     : str  =self.qformer_config.to_dict()
        a__     : str  =self.text_config.to_dict()
        a__     : List[str]  =self.__class__.model_type
        return output






