

'''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__						:       Union[str, Any]										=						logging.get_logger(__name__)

lowerCAmelCase__						:       Union[str, Any]										=						{
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}




class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    """blip_2_vision_model"""







						def __init__(							self					:      List[Any]			,			snake_case_					:      Tuple=1_4_0_8			,			snake_case_					:      Optional[int]=6_1_4_4			,			snake_case_					:      str=3_9			,			snake_case_					:      Optional[int]=1_6			,			snake_case_					:      List[Any]=2_2_4			,			snake_case_					:      Tuple=1_4			,			snake_case_					:      str="gelu"			,			snake_case_					:      Tuple=0.0_0_0_0_1			,			snake_case_					:      Optional[Any]=0.0			,			snake_case_					:      Optional[int]=1e-10			,			snake_case_					:      Tuple=True			,			**snake_case_					:      str			,			):



								'''simple docstring'''



								super().__init__(**snake_case_  )

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







						@classmethod
						def       __magic_name__							(							cls					:      List[Any]			,			snake_case_					:      Union[str, os.PathLike]			,			**snake_case_					:      Union[str, Any]  ):



								'''simple docstring'''



								cls._set_token_in_kwargs(snake_case_  )

								snake_case__						,       snake_case__						:       List[str]       						=  cls.get_config_dict(snake_case_			,			**snake_case_  )

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





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    """blip_2_qformer"""







						def __init__(							self					:      Dict			,			snake_case_					:      str=3_0_5_2_2			,			snake_case_					:      str=7_6_8			,			snake_case_					:      Optional[int]=1_2			,			snake_case_					:      Any=1_2			,			snake_case_					:      int=3_0_7_2			,			snake_case_					:      Any="gelu"			,			snake_case_					:      int=0.1			,			snake_case_					:      Optional[int]=0.1			,			snake_case_					:      List[Any]=5_1_2			,			snake_case_					:      Dict=0.0_2			,			snake_case_					:      Dict=1e-12			,			snake_case_					:      List[str]=0			,			snake_case_					:      Any="absolute"			,			snake_case_					:      int=2			,			snake_case_					:      Dict=1_4_0_8			,			**snake_case_					:      List[str]			,			):



								'''simple docstring'''



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

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







						@classmethod
						def       __magic_name__							(							cls					:      Any			,			snake_case_					:      Union[str, os.PathLike]			,			**snake_case_					:      int  ):



								'''simple docstring'''



								cls._set_token_in_kwargs(snake_case_  )

								snake_case__						,       snake_case__						:       int       						=  cls.get_config_dict(snake_case_			,			**snake_case_  )

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





class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    """blip-2"""
						__UpperCAmelCase			     =    True







						def __init__(							self					:      Optional[int]			,			snake_case_					:      int=None			,			snake_case_					:      Tuple=None			,			snake_case_					:      Optional[int]=None			,			snake_case_					:      Optional[int]=3_2			,			**snake_case_					:      Optional[int]  ):



								'''simple docstring'''



								super().__init__(**snake_case_  )

								if vision_config is None:
										snake_case__						:       Union[str, Any]       						=  {}
										logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.'''  )

								if qformer_config is None:
										snake_case__						:       Dict       						=  {}
										logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.'''  )

								if text_config is None:
										snake_case__						:       int       						=  {}
										logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).'''  )

								snake_case__						:       List[str]       						=  BlipaVisionConfig(**snake_case_  )
								snake_case__						:       int       						=  BlipaQFormerConfig(**snake_case_  )
								snake_case__						:       Optional[Any]       						=  text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
								snake_case__						:       Optional[Any]       						=  CONFIG_MAPPING[text_model_type](**snake_case_  )

								snake_case__						:       Dict       						=  self.text_config.tie_word_embeddings
								snake_case__						:       str       						=  self.text_config.is_encoder_decoder

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







						@classmethod
						def       __magic_name__							(							cls					:      List[Any]			,			snake_case_					:      BlipaVisionConfig			,			snake_case_					:      BlipaQFormerConfig			,			snake_case_					:      PretrainedConfig			,			**snake_case_					:      Tuple			,			):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



								snake_case__						:       Dict       						=  copy.deepcopy(self.__dict__  )
								snake_case__						:       Union[str, Any]       						=  self.vision_config.to_dict()
								snake_case__						:       str       						=  self.qformer_config.to_dict()
								snake_case__						:       Dict       						=  self.text_config.to_dict()
								snake_case__						:       Dict       						=  self.__class__.model_type
								return output






'''simple docstring'''


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

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin




@dataclass
class     a			(				SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    42
						__UpperCAmelCase			     =    42
						__UpperCAmelCase			     =    None





class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    2







						@register_to_config
						def __init__(							self					:      int			,			snake_case_					:      float = 0.0_2			,			snake_case_					:      float = 1_0_0			,			snake_case_					:      float = 1.0_0_7			,			snake_case_					:      float = 8_0			,			snake_case_					:      float = 0.0_5			,			snake_case_					:      float = 5_0			,			):



								'''simple docstring'''



								snake_case__						:       List[Any]       						=  sigma_max

								# setable values
								snake_case__						:       int       						=  None
								snake_case__						:       np.IntTensor       						=  None
								snake_case__						:       torch.FloatTensor       						=  None  # sigma(t_i)







						def       __magic_name__							(							self					:      Any			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      Optional[int] = None  ):



								'''simple docstring'''



								return sample







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      int			,			snake_case_					:      Union[str, torch.device] = None  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  num_inference_steps
								snake_case__						:       Tuple       						=  np.arange(0			,			self.num_inference_steps  )[::-1].copy()
								snake_case__						:       int       						=  torch.from_numpy(snake_case_  ).to(snake_case_  )
								snake_case__						:       List[str]       						=  [
								    (
								        self.config.sigma_max**2
								        * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
								    )
								    for i in self.timesteps
								]
								snake_case__						:       Union[str, Any]       						=  torch.tensor(snake_case_			,			dtype=torch.floataa			,			device=snake_case_  )







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      Optional[torch.Generator] = None  ):



								'''simple docstring'''



								if self.config.s_min <= sigma <= self.config.s_max:
										snake_case__						:       List[Any]       						=  min(self.config.s_churn / self.num_inference_steps			,			2**0.5 - 1  )
								else:
										snake_case__						:       List[Any]       						=  0

								# sample eps ~ N(0, S_noise^2 * I)
								snake_case__						:       Any       						=  self.config.s_noise * randn_tensor(sample.shape			,			generator=snake_case_  ).to(sample.device  )
								snake_case__						:       Tuple       						=  sigma + gamma * sigma
								snake_case__						:       Dict       						=  sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps)

								return sample_hat, sigma_hat







						def       __magic_name__							(							self					:      Optional[int]			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      float			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      bool = True			,			):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  sample_hat + sigma_hat * model_output
								snake_case__						:       List[Any]       						=  (sample_hat - pred_original_sample) / sigma_hat
								snake_case__						:       Any       						=  sample_hat + (sigma_prev - sigma_hat) * derivative

								if not return_dict:
										return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=snake_case_			,			derivative=snake_case_			,			pred_original_sample=snake_case_  )







						def       __magic_name__							(							self					:      Tuple			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      float			,			snake_case_					:      float			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      torch.FloatTensor			,			snake_case_					:      bool = True			,			):



								'''simple docstring'''



								snake_case__						:       str       						=  sample_prev + sigma_prev * model_output
								snake_case__						:       List[Any]       						=  (sample_prev - pred_original_sample) / sigma_prev
								snake_case__						:       Any       						=  sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr)

								if not return_dict:
										return (sample_prev, derivative)

								return KarrasVeOutput(
								    prev_sample=snake_case_			,			derivative=snake_case_			,			pred_original_sample=snake_case_  )







						def       __magic_name__							(							self					:      str			,			snake_case_					:      List[Any]			,			snake_case_					:      Optional[int]			,			snake_case_					:      Tuple  ):



								'''simple docstring'''



								raise NotImplementedError()



