




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase								=	logging.get_logger(__name__)

UpperCamelCase								=	{
    'Salesforce/blip-vqa-base': 'https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json',
    'Salesforce/blip-vqa-capfit-large': (
        'https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-base': (
        'https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-large': (
        'https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json'
    ),
    'Salesforce/blip-itm-base-coco': 'https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json',
    'Salesforce/blip-itm-large-coco': 'https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json',
    'Salesforce/blip-itm-base-flikr': 'https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json',
    'Salesforce/blip-itm-large-flikr': (
        'https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json'
    ),
}





class 					__lowerCamelCase			(       UpperCamelCase__       ):




    """simple docstring"""

    snake_case__   = "blip_text_model"


    def __init__(			self	:	Union[str, Any]  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]=30_524  ,      SCREAMING_SNAKE_CASE__	:	Optional[int]=768  ,      SCREAMING_SNAKE_CASE__	:	Any=768  ,      SCREAMING_SNAKE_CASE__	:	Any=3_072  ,      SCREAMING_SNAKE_CASE__	:	Dict=768  ,      SCREAMING_SNAKE_CASE__	:	int=12  ,      SCREAMING_SNAKE_CASE__	:	int=8  ,      SCREAMING_SNAKE_CASE__	:	Union[str, Any]=512  ,      SCREAMING_SNAKE_CASE__	:	Dict="gelu"  ,      SCREAMING_SNAKE_CASE__	:	Dict=1e-1_2  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]=0.0  ,      SCREAMING_SNAKE_CASE__	:	Tuple=0.0  ,      SCREAMING_SNAKE_CASE__	:	str=0.02  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]=30_522  ,      SCREAMING_SNAKE_CASE__	:	str=2  ,      SCREAMING_SNAKE_CASE__	:	Optional[Any]=0  ,      SCREAMING_SNAKE_CASE__	:	int=102  ,      SCREAMING_SNAKE_CASE__	:	int=True  ,      SCREAMING_SNAKE_CASE__	:	int=True  ,      **SCREAMING_SNAKE_CASE__	:	int  ,      ) ->			Dict:
         super().__init__(
             pad_token_id=SCREAMING_SNAKE_CASE__  ,      bos_token_id=SCREAMING_SNAKE_CASE__  ,      eos_token_id=SCREAMING_SNAKE_CASE__  ,      sep_token_id=SCREAMING_SNAKE_CASE__  ,      **SCREAMING_SNAKE_CASE__  ,      )

         lowerCAmelCase__            =					vocab_size
         lowerCAmelCase__            =					hidden_size
         lowerCAmelCase__            =					encoder_hidden_size
         lowerCAmelCase__            =					intermediate_size
         lowerCAmelCase__            =					projection_dim
         lowerCAmelCase__            =					hidden_dropout_prob
         lowerCAmelCase__            =					num_hidden_layers
         lowerCAmelCase__            =					num_attention_heads
         lowerCAmelCase__            =					max_position_embeddings
         lowerCAmelCase__            =					layer_norm_eps
         lowerCAmelCase__            =					hidden_act
         lowerCAmelCase__            =					initializer_range
         lowerCAmelCase__            =					attention_probs_dropout_prob
         lowerCAmelCase__            =					is_decoder
         lowerCAmelCase__            =					use_cache


    @classmethod
    def   a (			cls	:	Tuple  ,      SCREAMING_SNAKE_CASE__	:	Union[str, os.PathLike]  ,      **SCREAMING_SNAKE_CASE__	:	Any  ) ->			"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__      ,						lowerCAmelCase__            =					cls.get_config_dict(SCREAMING_SNAKE_CASE__  ,      **SCREAMING_SNAKE_CASE__  )

         # get the text config dict if we are loading from BlipConfig
         if config_dict.get("model_type"  ) == "blip":
              lowerCAmelCase__            =					config_dict["text_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(SCREAMING_SNAKE_CASE__  ,      **SCREAMING_SNAKE_CASE__  )






class 					__lowerCamelCase			(       UpperCamelCase__       ):




    """simple docstring"""

    snake_case__   = "blip_vision_model"


    def __init__(			self	:	Any  ,      SCREAMING_SNAKE_CASE__	:	Dict=768  ,      SCREAMING_SNAKE_CASE__	:	Union[str, Any]=3_072  ,      SCREAMING_SNAKE_CASE__	:	Optional[int]=512  ,      SCREAMING_SNAKE_CASE__	:	Optional[int]=12  ,      SCREAMING_SNAKE_CASE__	:	Any=12  ,      SCREAMING_SNAKE_CASE__	:	str=384  ,      SCREAMING_SNAKE_CASE__	:	Union[str, Any]=16  ,      SCREAMING_SNAKE_CASE__	:	List[str]="gelu"  ,      SCREAMING_SNAKE_CASE__	:	Dict=1e-5  ,      SCREAMING_SNAKE_CASE__	:	Any=0.0  ,      SCREAMING_SNAKE_CASE__	:	List[Any]=1e-1_0  ,      **SCREAMING_SNAKE_CASE__	:	List[str]  ,      ) ->			int:
         super().__init__(**SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__            =					hidden_size
         lowerCAmelCase__            =					intermediate_size
         lowerCAmelCase__            =					projection_dim
         lowerCAmelCase__            =					num_hidden_layers
         lowerCAmelCase__            =					num_attention_heads
         lowerCAmelCase__            =					patch_size
         lowerCAmelCase__            =					image_size
         lowerCAmelCase__            =					initializer_range
         lowerCAmelCase__            =					attention_dropout
         lowerCAmelCase__            =					layer_norm_eps
         lowerCAmelCase__            =					hidden_act


    @classmethod
    def   a (			cls	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	Union[str, os.PathLike]  ,      **SCREAMING_SNAKE_CASE__	:	str  ) ->			"PretrainedConfig":
         cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__      ,						lowerCAmelCase__            =					cls.get_config_dict(SCREAMING_SNAKE_CASE__  ,      **SCREAMING_SNAKE_CASE__  )

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






class 					__lowerCamelCase			(       UpperCamelCase__       ):




    """simple docstring"""

    snake_case__   = "blip"
    snake_case__   = True


    def __init__(			self	:	Optional[Any]  ,      SCREAMING_SNAKE_CASE__	:	str=None  ,      SCREAMING_SNAKE_CASE__	:	Tuple=None  ,      SCREAMING_SNAKE_CASE__	:	Any=512  ,      SCREAMING_SNAKE_CASE__	:	Union[str, Any]=2.6_592  ,      SCREAMING_SNAKE_CASE__	:	Tuple=256  ,      **SCREAMING_SNAKE_CASE__	:	Optional[int]  ,      ) ->			List[str]:
         super().__init__(**SCREAMING_SNAKE_CASE__  )

         if text_config is None:
              lowerCAmelCase__            =					{}
              logger.info("`text_config` is `None`. Initializing the `BlipTextConfig` with default values."  )

         if vision_config is None:
              lowerCAmelCase__            =					{}
              logger.info("`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values."  )

         lowerCAmelCase__            =					BlipTextConfig(**SCREAMING_SNAKE_CASE__  )
         lowerCAmelCase__            =					BlipVisionConfig(**SCREAMING_SNAKE_CASE__  )

         lowerCAmelCase__            =					self.vision_config.hidden_size

         lowerCAmelCase__            =					projection_dim
         lowerCAmelCase__            =					logit_scale_init_value
         lowerCAmelCase__            =					1.0
         lowerCAmelCase__            =					0.02
         lowerCAmelCase__            =					image_text_hidden_size


    @classmethod
    def   a (			cls	:	Union[str, Any]  ,      SCREAMING_SNAKE_CASE__	:	BlipTextConfig  ,      SCREAMING_SNAKE_CASE__	:	BlipVisionConfig  ,      **SCREAMING_SNAKE_CASE__	:	str  ) ->			Union[str, Any]:
         return cls(text_config=text_config.to_dict()  ,      vision_config=vision_config.to_dict()  ,      **SCREAMING_SNAKE_CASE__  )


    def   a (			self	:	Tuple  ) ->			Optional[int]:
         lowerCAmelCase__            =					copy.deepcopy(self.__dict__  )
         lowerCAmelCase__            =					self.text_config.to_dict()
         lowerCAmelCase__            =					self.vision_config.to_dict()
         lowerCAmelCase__            =					self.__class__.model_type
         return output







'''simple docstring'''




from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block


@dataclass
class 					SCREAMING_SNAKE_CASE       (a__				):
     lowerCAmelCase						     =			42




class 					SCREAMING_SNAKE_CASE       (a__   ,						a__				):


     @register_to_config
     def __init__(		self		,      _UpperCAmelCase = 6_5536		,      _UpperCAmelCase = None		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = 2		,      _UpperCAmelCase = 0		,      _UpperCAmelCase = "fourier"		,      _UpperCAmelCase = True		,      _UpperCAmelCase = False		,      _UpperCAmelCase = 0.0		,      _UpperCAmelCase = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")		,      _UpperCAmelCase = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")		,      _UpperCAmelCase = "UNetMidBlock1D"		,      _UpperCAmelCase = None		,      _UpperCAmelCase = (32, 32, 64)		,      _UpperCAmelCase = None		,      _UpperCAmelCase = 8		,      _UpperCAmelCase = 1		,      _UpperCAmelCase = False		,      ):

          '''simple docstring'''






          super().__init__()
          __A		:					Dict						=			sample_size

          # time
          if time_embedding_type == "fourier":
               __A		:					int						=			GaussianFourierProjection(
                   embedding_size=8		,      set_W_to_weight=_UpperCAmelCase		,      log=_UpperCAmelCase		,      flip_sin_to_cos=_UpperCAmelCase)
               __A		:					Any						=			2 * block_out_channels[0]
          elif time_embedding_type == "positional":
               __A		:					List[str]						=			Timesteps(
                   block_out_channels[0]		,      flip_sin_to_cos=_UpperCAmelCase		,      downscale_freq_shift=_UpperCAmelCase)
               __A		:					List[str]						=			block_out_channels[0]

          if use_timestep_embedding:
               __A		:					Optional[Any]						=			block_out_channels[0] * 4
               __A		:					Optional[int]						=			TimestepEmbedding(
                   in_channels=_UpperCAmelCase		,      time_embed_dim=_UpperCAmelCase		,      act_fn=_UpperCAmelCase		,      out_dim=block_out_channels[0]		,      )

          __A		:					Dict						=			nn.ModuleList([])
          __A		:					Dict						=			None
          __A		:					Tuple						=			nn.ModuleList([])
          __A		:					Tuple						=			None

          # down
          __A		:					Any						=			in_channels
          for i, down_block_type in enumerate(_UpperCAmelCase):
               __A		:					Tuple						=			output_channel
               __A		:					Optional[Any]						=			block_out_channels[i]

               if i == 0:
                    input_channel += extra_in_channels

               __A		:					List[str]						=			i == len(_UpperCAmelCase) - 1

               __A		:					int						=			get_down_block(
                   _UpperCAmelCase		,      num_layers=_UpperCAmelCase		,      in_channels=_UpperCAmelCase		,      out_channels=_UpperCAmelCase		,      temb_channels=block_out_channels[0]		,      add_downsample=not is_final_block or downsample_each_block		,      )
               self.down_blocks.append(_UpperCAmelCase)

          # mid
          __A		:					str						=			get_mid_block(
              _UpperCAmelCase		,      in_channels=block_out_channels[-1]		,      mid_channels=block_out_channels[-1]		,      out_channels=block_out_channels[-1]		,      embed_dim=block_out_channels[0]		,      num_layers=_UpperCAmelCase		,      add_downsample=_UpperCAmelCase		,      )

          # up
          __A		:					Optional[int]						=			list(reversed(_UpperCAmelCase))
          __A		:					Optional[int]						=			reversed_block_out_channels[0]
          if out_block_type is None:
               __A		:					str						=			out_channels
          else:
               __A		:					List[Any]						=			block_out_channels[0]

          for i, up_block_type in enumerate(_UpperCAmelCase):
               __A		:					Optional[Any]						=			output_channel
               __A		:					Optional[Any]						=			(
                   reversed_block_out_channels[i + 1] if i < len(_UpperCAmelCase) - 1 else final_upsample_channels
               )

               __A		:					Dict						=			i == len(_UpperCAmelCase) - 1

               __A		:					str						=			get_up_block(
                   _UpperCAmelCase		,      num_layers=_UpperCAmelCase		,      in_channels=_UpperCAmelCase		,      out_channels=_UpperCAmelCase		,      temb_channels=block_out_channels[0]		,      add_upsample=not is_final_block		,      )
               self.up_blocks.append(_UpperCAmelCase)
               __A		:					Optional[int]						=			output_channel

          # out
          __A		:					str						=			norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4		,      32)
          __A		:					Optional[Any]						=			get_out_block(
              out_block_type=_UpperCAmelCase		,      num_groups_out=_UpperCAmelCase		,      embed_dim=block_out_channels[0]		,      out_channels=_UpperCAmelCase		,      act_fn=_UpperCAmelCase		,      fc_dim=block_out_channels[-1] // 4		,      )


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase = True		,      ):

          '''simple docstring'''






          __A		:					Any						=			timestep
          if not torch.is_tensor(_UpperCAmelCase):
               __A		:					Any						=			torch.tensor([timesteps]		,      dtype=torch.long		,      device=sample.device)
          elif torch.is_tensor(_UpperCAmelCase) and len(timesteps.shape) == 0:
               __A		:					Any						=			timesteps[None].to(sample.device)

          __A		:					List[Any]						=			self.time_proj(_UpperCAmelCase)
          if self.config.use_timestep_embedding:
               __A		:					Dict						=			self.time_mlp(_UpperCAmelCase)
          else:
               __A		:					Dict						=			timestep_embed[..., None]
               __A		:					Tuple						=			timestep_embed.repeat([1, 1, sample.shape[2]]).to(sample.dtype)
               __A		:					List[Any]						=			timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]))

          # 2. down
          __A		:					int						=			()
          for downsample_block in self.down_blocks:
               __A					,__A		:					int						=			downsample_block(hidden_states=_UpperCAmelCase		,      temb=_UpperCAmelCase)
               down_block_res_samples += res_samples

          # 3. mid
          if self.mid_block:
               __A		:					Optional[int]						=			self.mid_block(_UpperCAmelCase		,      _UpperCAmelCase)

          # 4. up
          for i, upsample_block in enumerate(self.up_blocks):
               __A		:					Any						=			down_block_res_samples[-1:]
               __A		:					Optional[int]						=			down_block_res_samples[:-1]
               __A		:					Any						=			upsample_block(_UpperCAmelCase		,      res_hidden_states_tuple=_UpperCAmelCase		,      temb=_UpperCAmelCase)

          # 5. post-process
          if self.out_block:
               __A		:					Dict						=			self.out_block(_UpperCAmelCase		,      _UpperCAmelCase)

          if not return_dict:
               return (sample,)

          return UNetaDOutput(sample=_UpperCAmelCase)