


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


__snake_case         =					logging.get_logger(__name__)

__snake_case         =					{
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}







class _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: Optional[Any]													=						'''blip_2_vision_model'''







   def __init__(			self		,			UpperCamelCase__=1408		,			UpperCamelCase__=6144		,			UpperCamelCase__=39		,			UpperCamelCase__=16		,			UpperCamelCase__=224		,			UpperCamelCase__=14		,			UpperCamelCase__="gelu"		,			UpperCamelCase__=0.00001		,			UpperCamelCase__=0.0		,			UpperCamelCase__=1e-10		,			UpperCamelCase__=True		,			**UpperCamelCase__		,			)			->	Optional[Any]:

       '''simple docstring'''




       super().__init__(**UpperCamelCase__		)

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







   @classmethod
   def 				lowerCamelCase	(			cls		,			UpperCamelCase__		,			**UpperCamelCase__		)			->	"PretrainedConfig":

       '''simple docstring'''




       cls._set_token_in_kwargs(UpperCamelCase__		)

       snake_case      ,snake_case					:   List[str]         =					cls.get_config_dict(UpperCamelCase__		,			**UpperCamelCase__		)

       # get the vision config dict if we are loading from Blip2Config
       if config_dict.get("model_type"		) == "blip-2":
           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(UpperCamelCase__		,			**UpperCamelCase__		)







class _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: List[str]													=						'''blip_2_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.02		,			UpperCamelCase__=1e-12		,			UpperCamelCase__=0		,			UpperCamelCase__="absolute"		,			UpperCamelCase__=2		,			UpperCamelCase__=1408		,			**UpperCamelCase__		,			)			->	Tuple:

       '''simple docstring'''




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

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







   @classmethod
   def 				lowerCamelCase	(			cls		,			UpperCamelCase__		,			**UpperCamelCase__		)			->	"PretrainedConfig":

       '''simple docstring'''




       cls._set_token_in_kwargs(UpperCamelCase__		)

       snake_case      ,snake_case					:   List[str]         =					cls.get_config_dict(UpperCamelCase__		,			**UpperCamelCase__		)

       # get the qformer config dict if we are loading from Blip2Config
       if config_dict.get("model_type"		) == "blip-2":
           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(UpperCamelCase__		,			**UpperCamelCase__		)







class _lowerCAmelCase       (   snake_case_			):
   __UpperCAmelCase				: Union[str, Any]													=						'''blip-2'''
   __UpperCAmelCase				: Dict													=						True







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

       '''simple docstring'''




       super().__init__(**UpperCamelCase__		)

       if vision_config is None:
           snake_case					:   Optional[int]         =					{}
           logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."		)

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

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

       snake_case					:   str         =					BlipaVisionConfig(**UpperCamelCase__		)
       snake_case					:   Optional[Any]         =					BlipaQFormerConfig(**UpperCamelCase__		)
       snake_case					:   Dict         =					text_config["model_type"] if "model_type" in text_config else "opt"
       snake_case					:   List[Any]         =					CONFIG_MAPPING[text_model_type](**UpperCamelCase__		)

       snake_case					:   Any         =					self.text_config.tie_word_embeddings
       snake_case					:   List[Any]         =					self.text_config.is_encoder_decoder

       snake_case					:   Tuple         =					num_query_tokens
       snake_case					:   Dict         =					self.vision_config.hidden_size
       snake_case					:   Union[str, Any]         =					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
       snake_case					:   Tuple         =					1.0
       snake_case					:   Union[str, Any]         =					0.02







   @classmethod
   def 				lowerCamelCase	(			cls		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			**UpperCamelCase__		,			)			->	int:

       '''simple docstring'''




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







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

       '''simple docstring'''




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









"""simple docstring"""



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD







class _lowerCAmelCase       (   nn.Module			):
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: float													=						0.0
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: bool													=						True
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: jnp.dtype													=						jnp.floataa







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

       '''simple docstring'''




       snake_case					:   Optional[int]         =					[]
       snake_case					:   Tuple         =					[]

       for i in range(self.num_layers		):
           snake_case					:   Any         =					self.in_channels if i == 0 else self.out_channels

           snake_case					:   Optional[int]         =					FlaxResnetBlockaD(
               in_channels=UpperCamelCase__		,			out_channels=self.out_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
           resnets.append(UpperCamelCase__		)

           snake_case					:   Dict         =					FlaxTransformeraDModel(
               in_channels=self.out_channels		,			n_heads=self.num_attention_heads		,			d_head=self.out_channels // self.num_attention_heads		,			depth=1		,			use_linear_projection=self.use_linear_projection		,			only_cross_attention=self.only_cross_attention		,			use_memory_efficient_attention=self.use_memory_efficient_attention		,			dtype=self.dtype		,			)
           attentions.append(UpperCamelCase__		)

       snake_case					:   List[Any]         =					resnets
       snake_case					:   List[Any]         =					attentions

       if self.add_downsample:
           snake_case					:   Optional[Any]         =					FlaxDownsampleaD(self.out_channels		,			dtype=self.dtype		)







   def __call__(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=True		)			->	List[str]:

       '''simple docstring'''




       snake_case					:   Tuple         =					()

       for resnet, attn in zip(self.resnets		,			self.attentions		):
           snake_case					:   str         =					resnet(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)
           snake_case					:   int         =					attn(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)
           output_states += (hidden_states,)

       if self.add_downsample:
           snake_case					:   Any         =					self.downsamplers_a(UpperCamelCase__		)
           output_states += (hidden_states,)

       return hidden_states, output_states







class _lowerCAmelCase       (   nn.Module			):
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: float													=						0.0
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: bool													=						True
   __UpperCAmelCase				: jnp.dtype													=						jnp.floataa







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

       '''simple docstring'''




       snake_case					:   Dict         =					[]

       for i in range(self.num_layers		):
           snake_case					:   List[str]         =					self.in_channels if i == 0 else self.out_channels

           snake_case					:   str         =					FlaxResnetBlockaD(
               in_channels=UpperCamelCase__		,			out_channels=self.out_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
           resnets.append(UpperCamelCase__		)
       snake_case					:   str         =					resnets

       if self.add_downsample:
           snake_case					:   str         =					FlaxDownsampleaD(self.out_channels		,			dtype=self.dtype		)







   def __call__(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=True		)			->	List[Any]:

       '''simple docstring'''




       snake_case					:   Tuple         =					()

       for resnet in self.resnets:
           snake_case					:   Optional[Any]         =					resnet(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)
           output_states += (hidden_states,)

       if self.add_downsample:
           snake_case					:   Tuple         =					self.downsamplers_a(UpperCamelCase__		)
           output_states += (hidden_states,)

       return hidden_states, output_states







class _lowerCAmelCase       (   nn.Module			):
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: float													=						0.0
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: bool													=						True
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: jnp.dtype													=						jnp.floataa







   def 				lowerCamelCase	(			self		)			->	Optional[int]:

       '''simple docstring'''




       snake_case					:   Dict         =					[]
       snake_case					:   Union[str, Any]         =					[]

       for i in range(self.num_layers		):
           snake_case					:   Optional[Any]         =					self.in_channels if (i == self.num_layers - 1) else self.out_channels
           snake_case					:   Union[str, Any]         =					self.prev_output_channel if i == 0 else self.out_channels

           snake_case					:   Optional[Any]         =					FlaxResnetBlockaD(
               in_channels=resnet_in_channels + res_skip_channels		,			out_channels=self.out_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
           resnets.append(UpperCamelCase__		)

           snake_case					:   int         =					FlaxTransformeraDModel(
               in_channels=self.out_channels		,			n_heads=self.num_attention_heads		,			d_head=self.out_channels // self.num_attention_heads		,			depth=1		,			use_linear_projection=self.use_linear_projection		,			only_cross_attention=self.only_cross_attention		,			use_memory_efficient_attention=self.use_memory_efficient_attention		,			dtype=self.dtype		,			)
           attentions.append(UpperCamelCase__		)

       snake_case					:   str         =					resnets
       snake_case					:   List[Any]         =					attentions

       if self.add_upsample:
           snake_case					:   str         =					FlaxUpsampleaD(self.out_channels		,			dtype=self.dtype		)







   def __call__(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=True		)			->	Optional[int]:

       '''simple docstring'''




       for resnet, attn in zip(self.resnets		,			self.attentions		):
           # pop res hidden states
           snake_case					:   Dict         =					res_hidden_states_tuple[-1]
           snake_case					:   Optional[int]         =					res_hidden_states_tuple[:-1]
           snake_case					:   List[Any]         =					jnp.concatenate((hidden_states, res_hidden_states)		,			axis=-1		)

           snake_case					:   List[Any]         =					resnet(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)
           snake_case					:   Any         =					attn(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)

       if self.add_upsample:
           snake_case					:   int         =					self.upsamplers_a(UpperCamelCase__		)

       return hidden_states







class _lowerCAmelCase       (   nn.Module			):
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: int
   __UpperCAmelCase				: float													=						0.0
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: bool													=						True
   __UpperCAmelCase				: jnp.dtype													=						jnp.floataa







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

       '''simple docstring'''




       snake_case					:   str         =					[]

       for i in range(self.num_layers		):
           snake_case					:   Union[str, Any]         =					self.in_channels if (i == self.num_layers - 1) else self.out_channels
           snake_case					:   Optional[Any]         =					self.prev_output_channel if i == 0 else self.out_channels

           snake_case					:   Dict         =					FlaxResnetBlockaD(
               in_channels=resnet_in_channels + res_skip_channels		,			out_channels=self.out_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
           resnets.append(UpperCamelCase__		)

       snake_case					:   Optional[int]         =					resnets

       if self.add_upsample:
           snake_case					:   Dict         =					FlaxUpsampleaD(self.out_channels		,			dtype=self.dtype		)







   def __call__(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=True		)			->	int:

       '''simple docstring'''




       for resnet in self.resnets:
           # pop res hidden states
           snake_case					:   int         =					res_hidden_states_tuple[-1]
           snake_case					:   List[Any]         =					res_hidden_states_tuple[:-1]
           snake_case					:   Optional[int]         =					jnp.concatenate((hidden_states, res_hidden_states)		,			axis=-1		)

           snake_case					:   Dict         =					resnet(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)

       if self.add_upsample:
           snake_case					:   Optional[int]         =					self.upsamplers_a(UpperCamelCase__		)

       return hidden_states







class _lowerCAmelCase       (   nn.Module			):
   __UpperCAmelCase				: int
   __UpperCAmelCase				: float													=						0.0
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: int													=						1
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: bool													=						False
   __UpperCAmelCase				: jnp.dtype													=						jnp.floataa







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   Dict         =					[
           FlaxResnetBlockaD(
               in_channels=self.in_channels		,			out_channels=self.in_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
       ]

       snake_case					:   Any         =					[]

       for _ in range(self.num_layers		):
           snake_case					:   Union[str, Any]         =					FlaxTransformeraDModel(
               in_channels=self.in_channels		,			n_heads=self.num_attention_heads		,			d_head=self.in_channels // self.num_attention_heads		,			depth=1		,			use_linear_projection=self.use_linear_projection		,			use_memory_efficient_attention=self.use_memory_efficient_attention		,			dtype=self.dtype		,			)
           attentions.append(UpperCamelCase__		)

           snake_case					:   List[Any]         =					FlaxResnetBlockaD(
               in_channels=self.in_channels		,			out_channels=self.in_channels		,			dropout_prob=self.dropout		,			dtype=self.dtype		,			)
           resnets.append(UpperCamelCase__		)

       snake_case					:   Union[str, Any]         =					resnets
       snake_case					:   str         =					attentions







   def __call__(			self		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__		,			UpperCamelCase__=True		)			->	Any:

       '''simple docstring'''




       snake_case					:   Optional[Any]         =					self.resnets[0](UpperCamelCase__		,			UpperCamelCase__		)
       for attn, resnet in zip(self.attentions		,			self.resnets[1:]		):
           snake_case					:   Union[str, Any]         =					attn(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)
           snake_case					:   Optional[Any]         =					resnet(UpperCamelCase__		,			UpperCamelCase__		,			deterministic=UpperCamelCase__		)

       return hidden_states





