






'''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


__A    	=   logging.get_logger(__name__)

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




class a_    (    __UpperCAmelCase							):
   _snake_case        =							"""instructblip_vision_model"""







   def __init__(self					,   __a=1_4_0_8					,   __a=6_1_4_4					,   __a=3_9					,   __a=1_6					,   __a=2_2_4					,   __a=1_4					,   __a="gelu"					,   __a=1E-6					,   __a=0.0					,   __a=1E-10					,   __a=True					,   **__a					,   )			->      Dict:
      """simple docstring"""






      super().__init__(**lowerCAmelCase_)

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







   @classmethod
   def     SCREAMING_SNAKE_CASE__					(cls					,   __a					,   **__a)			->      Dict:
      """simple docstring"""






      cls._set_token_in_kwargs(lowerCAmelCase_)

      __snake_case							,__snake_case				:      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":
         __snake_case				:      Dict     							= 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 a_    (    __UpperCAmelCase							):
   _snake_case        =							"""instructblip_qformer"""







   def __init__(self					,   __a=3_0_5_2_2					,   __a=7_6_8					,   __a=1_2					,   __a=1_2					,   __a=3_0_7_2					,   __a="gelu"					,   __a=0.1					,   __a=0.1					,   __a=5_1_2					,   __a=0.02					,   __a=1E-12					,   __a=0					,   __a="absolute"					,   __a=2					,   __a=1_4_0_8					,   **__a					,   )			->      Optional[Any]:
      """simple docstring"""






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

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







   @classmethod
   def     SCREAMING_SNAKE_CASE__					(cls					,   __a					,   **__a)			->      Union[str, Any]:
      """simple docstring"""






      cls._set_token_in_kwargs(lowerCAmelCase_)

      __snake_case							,__snake_case				:      Any     							= cls.get_config_dict(lowerCAmelCase_					,   **lowerCAmelCase_)

      # get the qformer config dict if we are loading from InstructBlipConfig
      if config_dict.get('model_type') == "instructblip":
         __snake_case				:      str     							= 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 a_    (    __UpperCAmelCase							):
   _snake_case        =							"""instructblip"""
   _snake_case        =							True







   def __init__(self					,   __a=None					,   __a=None					,   __a=None					,   __a=3_2					,   **__a)			->      Union[str, Any]:
      """simple docstring"""






      super().__init__(**lowerCAmelCase_)

      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				:      Tuple     							= {}
         logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.')

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

      __snake_case				:      Tuple     							= InstructBlipVisionConfig(**lowerCAmelCase_)
      __snake_case				:      str     							= InstructBlipQFormerConfig(**lowerCAmelCase_)
      __snake_case				:      Any     							= text_config['model_type'] if 'model_type' in text_config else 'opt'
      __snake_case				:      str     							= CONFIG_MAPPING[text_model_type](**lowerCAmelCase_)

      __snake_case				:      Any     							= self.text_config.tie_word_embeddings
      __snake_case				:      Any     							= self.text_config.is_encoder_decoder

      __snake_case				:      Union[str, Any]     							= num_query_tokens
      __snake_case				:      List[str]     							= self.vision_config.hidden_size
      __snake_case				:      str     							= self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      __snake_case				:      int     							= 1.0
      __snake_case				:      List[str]     							= 0.02







   @classmethod
   def     SCREAMING_SNAKE_CASE__					(cls					,   __a					,   __a					,   __a					,   **__a					,   )			->      Dict:
      """simple docstring"""






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







   def     SCREAMING_SNAKE_CASE__					(self)			->      List[Any]:
      """simple docstring"""






      __snake_case				:      Optional[int]     							= copy.deepcopy(self.__dict__)
      __snake_case				:      Tuple     							= self.vision_config.to_dict()
      __snake_case				:      Tuple     							= self.qformer_config.to_dict()
      __snake_case				:      Optional[Any]     							= self.text_config.to_dict()
      __snake_case				:      Dict     							= self.__class__.model_type
      return output







'''simple docstring'''






import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()




class a_    (    UpperCamelCase_		,      UpperCamelCase_		,      unittest.TestCase							):
   _snake_case        =							VQModel
   _snake_case        =							"""sample"""







   @property
   def     SCREAMING_SNAKE_CASE__					(self					,   __a=(3_2, 3_2))			->      str:
      """simple docstring"""






      __snake_case				:      Dict     							= 4
      __snake_case				:      Optional[int]     							= 3

      __snake_case				:      str     							= floats_tensor((batch_size, num_channels) + sizes).to(__a)

      return {"sample": image}







   @property
   def     SCREAMING_SNAKE_CASE__					(self)			->      Union[str, Any]:
      """simple docstring"""






      return (3, 3_2, 3_2)







   @property
   def     SCREAMING_SNAKE_CASE__					(self)			->      List[Any]:
      """simple docstring"""






      return (3, 3_2, 3_2)







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[int]:
      """simple docstring"""






      __snake_case				:      Optional[Any]     							= {
          'block_out_channels': [3_2, 6_4],
          'in_channels': 3,
          'out_channels': 3,
          'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
          'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
          'latent_channels': 3,
      }
      __snake_case				:      List[Any]     							= self.dummy_input
      return init_dict, inputs_dict







   def     SCREAMING_SNAKE_CASE__					(self)			->      Any:
      """simple docstring"""






      pass







   def     SCREAMING_SNAKE_CASE__					(self)			->      int:
      """simple docstring"""






      pass







   def     SCREAMING_SNAKE_CASE__					(self)			->      Union[str, Any]:
      """simple docstring"""






      __snake_case							,__snake_case				:      List[Any]     							= VQModel.from_pretrained('fusing/vqgan-dummy'					,   output_loading_info=__a)
      self.assertIsNotNone(__a)
      self.assertEqual(len(loading_info['missing_keys'])					,   0)

      model.to(__a)
      __snake_case				:      Any     							= model(**self.dummy_input)

      assert image is not None, "Make sure output is not None"







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[int]:
      """simple docstring"""






      __snake_case				:      Union[str, Any]     							= VQModel.from_pretrained('fusing/vqgan-dummy')
      model.to(__a).eval()

      torch.manual_seed(0)
      if torch.cuda.is_available():
         torch.cuda.manual_seed_all(0)

      __snake_case				:      Tuple     							= torch.randn(1					,   model.config.in_channels					,   model.config.sample_size					,   model.config.sample_size)
      __snake_case				:      Optional[int]     							= image.to(__a)
      with torch.no_grad():
         __snake_case				:      List[Any]     							= model(__a).sample

      __snake_case				:      int     							= output[0, -1, -3:, -3:].flatten().cpu()
      # fmt: off
      __snake_case				:      int     							= torch.tensor([-0.0_153, -0.4_044, -0.1_880, -0.5_161, -0.2_418, -0.4_072, -0.1_612, -0.0_633, -0.0_143])
      # fmt: on
      self.assertTrue(torch.allclose(__a					,   __a					,   atol=1E-3))