






'''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_												=					logging.get_logger(__name__)

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

class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						'''blip_2_vision_model'''

   def __init__(		self :	List[Any]      ,      __UpperCAmelCase :	Optional[int]=1408      ,      __UpperCAmelCase :	Optional[Any]=6144      ,      __UpperCAmelCase :	str=39      ,      __UpperCAmelCase :	Any=16      ,      __UpperCAmelCase :	Tuple=224      ,      __UpperCAmelCase :	int=14      ,      __UpperCAmelCase :	Dict="gelu"      ,      __UpperCAmelCase :	int=0.00001      ,      __UpperCAmelCase :	Tuple=0.0      ,      __UpperCAmelCase :	int=1E-10      ,      __UpperCAmelCase :	List[Any]=True      ,      **__UpperCAmelCase :	Tuple      ,      ):





         '''simple docstring'''





         super().__init__(**__UpperCAmelCase		)

         _A			     =	hidden_size
         _A			     =	intermediate_size
         _A			     =	num_hidden_layers
         _A			     =	num_attention_heads
         _A			     =	patch_size
         _A			     =	image_size
         _A			     =	initializer_range
         _A			     =	attention_dropout
         _A			     =	layer_norm_eps
         _A			     =	hidden_act
         _A			     =	qkv_bias

   @classmethod
   def 						lowerCAmelCase (		cls :	Optional[int]      ,      __UpperCAmelCase :	Union[str, os.PathLike]      ,      **__UpperCAmelCase :	Any		):





         '''simple docstring'''





         cls._set_token_in_kwargs(__UpperCAmelCase		)

         _A     ,	_A			     =	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":
               _A			     =	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    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						'''blip_2_qformer'''

   def __init__(		self :	Tuple      ,      __UpperCAmelCase :	List[str]=30522      ,      __UpperCAmelCase :	Optional[int]=768      ,      __UpperCAmelCase :	List[str]=12      ,      __UpperCAmelCase :	Optional[int]=12      ,      __UpperCAmelCase :	List[str]=3072      ,      __UpperCAmelCase :	List[str]="gelu"      ,      __UpperCAmelCase :	Tuple=0.1      ,      __UpperCAmelCase :	int=0.1      ,      __UpperCAmelCase :	List[str]=512      ,      __UpperCAmelCase :	str=0.02      ,      __UpperCAmelCase :	List[Any]=1E-12      ,      __UpperCAmelCase :	str=0      ,      __UpperCAmelCase :	Union[str, Any]="absolute"      ,      __UpperCAmelCase :	Optional[int]=2      ,      __UpperCAmelCase :	List[str]=1408      ,      **__UpperCAmelCase :	int      ,      ):





         '''simple docstring'''





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

         _A			     =	vocab_size
         _A			     =	hidden_size
         _A			     =	num_hidden_layers
         _A			     =	num_attention_heads
         _A			     =	hidden_act
         _A			     =	intermediate_size
         _A			     =	hidden_dropout_prob
         _A			     =	attention_probs_dropout_prob
         _A			     =	max_position_embeddings
         _A			     =	initializer_range
         _A			     =	layer_norm_eps
         _A			     =	position_embedding_type
         _A			     =	cross_attention_frequency
         _A			     =	encoder_hidden_size

   @classmethod
   def 						lowerCAmelCase (		cls :	Dict      ,      __UpperCAmelCase :	Union[str, os.PathLike]      ,      **__UpperCAmelCase :	int		):





         '''simple docstring'''





         cls._set_token_in_kwargs(__UpperCAmelCase		)

         _A     ,	_A			     =	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":
               _A			     =	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    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						'''blip-2'''
   snake_case    				=						True

   def __init__(		self :	Any      ,      __UpperCAmelCase :	Optional[int]=None      ,      __UpperCAmelCase :	List[str]=None      ,      __UpperCAmelCase :	Tuple=None      ,      __UpperCAmelCase :	int=32      ,      **__UpperCAmelCase :	int		):





         '''simple docstring'''





         super().__init__(**__UpperCAmelCase		)

         if vision_config is None:
               _A			     =	{}
               logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."		)

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

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

         _A			     =	BlipaVisionConfig(**__UpperCAmelCase		)
         _A			     =	BlipaQFormerConfig(**__UpperCAmelCase		)
         _A			     =	text_config["model_type"] if "model_type" in text_config else "opt"
         _A			     =	CONFIG_MAPPING[text_model_type](**__UpperCAmelCase		)

         _A			     =	self.text_config.tie_word_embeddings
         _A			     =	self.text_config.is_encoder_decoder

         _A			     =	num_query_tokens
         _A			     =	self.vision_config.hidden_size
         _A			     =	self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
         _A			     =	1.0
         _A			     =	0.02

   @classmethod
   def 						lowerCAmelCase (		cls :	Optional[int]      ,      __UpperCAmelCase :	BlipaVisionConfig      ,      __UpperCAmelCase :	BlipaQFormerConfig      ,      __UpperCAmelCase :	PretrainedConfig      ,      **__UpperCAmelCase :	Any      ,      ):





         '''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 :	str		):





         '''simple docstring'''





         _A			     =	copy.deepcopy(self.__dict__		)
         _A			     =	self.vision_config.to_dict()
         _A			     =	self.qformer_config.to_dict()
         _A			     =	self.text_config.to_dict()
         _A			     =	self.__class__.model_type
         return output










"""simple docstring"""







import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


lowercase__		:				Optional[Any]          =	logging.get_logger(__name__)







class       UpperCamelCase__   (	lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE  				=     """linear"""
      _SCREAMING_SNAKE_CASE  				=     """cosine"""
      _SCREAMING_SNAKE_CASE  				=     """cosine_with_restarts"""
      _SCREAMING_SNAKE_CASE  				=     """polynomial"""
      _SCREAMING_SNAKE_CASE  				=     """constant"""
      _SCREAMING_SNAKE_CASE  				=     """constant_with_warmup"""
      _SCREAMING_SNAKE_CASE  				=     """piecewise_constant"""




def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				int = -1						)						->   Tuple:

  """simple docstring"""




  return LambdaLR(lowerCAmelCase__    ,   lambda lowerCAmelCase__						: 1    ,   last_epoch=lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				int    ,   lowerCAmelCase__      :				int = -1						)						->   str:

  """simple docstring"""




  def lr_lambda(lowerCAmelCase__      :				int						):
    if current_step < num_warmup_steps:
      return float(lowerCAmelCase__						) / float(max(1.0    ,   lowerCAmelCase__						)						)
    return 1.0

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				str    ,   lowerCAmelCase__      :				int = -1						)						->   int:

  """simple docstring"""




  lowerCAmelCase_   :				str								= {}
  lowerCAmelCase_   :				Optional[Any]								= step_rules.split(','						)
  for rule_str in rule_list[:-1]:
    lowerCAmelCase_						,lowerCAmelCase_   :				Optional[int]								= rule_str.split(':'						)
    lowerCAmelCase_   :				List[str]								= int(lowerCAmelCase__						)
    lowerCAmelCase_   :				Optional[int]								= float(lowerCAmelCase__						)
    lowerCAmelCase_   :				Optional[Any]								= value
  lowerCAmelCase_   :				str								= float(rule_list[-1]						)

  def create_rules_function(lowerCAmelCase__      :				Optional[Any]    ,   lowerCAmelCase__      :				int						):
    def rule_func(lowerCAmelCase__      :				int						) -> float:
      lowerCAmelCase_   :				str								= sorted(rules_dict.keys()						)
      for i, sorted_step in enumerate(lowerCAmelCase__						):
        if steps < sorted_step:
          return rules_dict[sorted_steps[i]]
      return last_lr_multiple

    return rule_func

  lowerCAmelCase_   :				Dict								= create_rules_function(lowerCAmelCase__    ,   lowerCAmelCase__						)

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optional[int]    ,   lowerCAmelCase__      :				Optional[int]    ,   lowerCAmelCase__      :				List[str]    ,   lowerCAmelCase__      :				Union[str, Any]=-1						)						->   Any:

  """simple docstring"""




  def lr_lambda(lowerCAmelCase__      :				int						):
    if current_step < num_warmup_steps:
      return float(lowerCAmelCase__						) / float(max(1    ,   lowerCAmelCase__						)						)
    return max(
        0.0    ,   float(num_training_steps - current_step						) / float(max(1    ,   num_training_steps - num_warmup_steps						)						)						)

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				int    ,   lowerCAmelCase__      :				int    ,   lowerCAmelCase__      :				float = 0.5    ,   lowerCAmelCase__      :				int = -1						)						->   Union[str, Any]:

  """simple docstring"""




  def lr_lambda(lowerCAmelCase__      :				Any						):
    if current_step < num_warmup_steps:
      return float(lowerCAmelCase__						) / float(max(1    ,   lowerCAmelCase__						)						)
    lowerCAmelCase_   :				List[Any]								= float(current_step - num_warmup_steps						) / float(max(1    ,   num_training_steps - num_warmup_steps						)						)
    return max(0.0    ,   0.5 * (1.0 + math.cos(math.pi * float(lowerCAmelCase__						) * 2.0 * progress						))						)

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				int    ,   lowerCAmelCase__      :				int    ,   lowerCAmelCase__      :				int = 1    ,   lowerCAmelCase__      :				int = -1						)						->   int:

  """simple docstring"""




  def lr_lambda(lowerCAmelCase__      :				str						):
    if current_step < num_warmup_steps:
      return float(lowerCAmelCase__						) / float(max(1    ,   lowerCAmelCase__						)						)
    lowerCAmelCase_   :				Dict								= float(current_step - num_warmup_steps						) / float(max(1    ,   num_training_steps - num_warmup_steps						)						)
    if progress >= 1.0:
      return 0.0
    return max(0.0    ,   0.5 * (1.0 + math.cos(math.pi * ((float(lowerCAmelCase__						) * progress) % 1.0)						))						)

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   lowerCAmelCase__						)

def 				UpperCamelCase_    (    lowerCAmelCase__      :				str    ,   lowerCAmelCase__      :				List[Any]    ,   lowerCAmelCase__      :				List[str]    ,   lowerCAmelCase__      :				Tuple=1e-7    ,   lowerCAmelCase__      :				Union[str, Any]=1.0    ,   lowerCAmelCase__      :				int=-1						)						->   Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_   :				Any								= optimizer.defaults['lr']
  if not (lr_init > lr_end):
    raise ValueError(f"lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})"						)

  def lr_lambda(lowerCAmelCase__      :				int						):
    if current_step < num_warmup_steps:
      return float(lowerCAmelCase__						) / float(max(1    ,   lowerCAmelCase__						)						)
    elif current_step > num_training_steps:
      return lr_end / lr_init  # as LambdaLR multiplies by lr_init
    else:
      lowerCAmelCase_   :				Any								= lr_init - lr_end
      lowerCAmelCase_   :				int								= num_training_steps - num_warmup_steps
      lowerCAmelCase_   :				Dict								= 1 - (current_step - num_warmup_steps) / decay_steps
      lowerCAmelCase_   :				Dict								= lr_range * pct_remaining**power + lr_end
      return decay / lr_init  # as LambdaLR multiplies by lr_init

  return LambdaLR(lowerCAmelCase__    ,   lowerCAmelCase__    ,   lowerCAmelCase__						)


lowercase__		:				Any          =	{
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Union[str, SchedulerType]    ,   lowerCAmelCase__      :				Optimizer    ,   lowerCAmelCase__      :				Optional[str] = None    ,   lowerCAmelCase__      :				Optional[int] = None    ,   lowerCAmelCase__      :				Optional[int] = None    ,   lowerCAmelCase__      :				int = 1    ,   lowerCAmelCase__      :				float = 1.0    ,   lowerCAmelCase__      :				int = -1    ,   )						->   int:

  """simple docstring"""




  lowerCAmelCase_   :				List[str]								= SchedulerType(lowerCAmelCase__						)
  lowerCAmelCase_   :				List[Any]								= TYPE_TO_SCHEDULER_FUNCTION[name]
  if name == SchedulerType.CONSTANT:
    return schedule_func(lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)

  if name == SchedulerType.PIECEWISE_CONSTANT:
    return schedule_func(lowerCAmelCase__    ,   step_rules=lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)

  # All other schedulers require `num_warmup_steps`
  if num_warmup_steps is None:
    raise ValueError(f"{name} requires `num_warmup_steps`, please provide that argument."						)

  if name == SchedulerType.CONSTANT_WITH_WARMUP:
    return schedule_func(lowerCAmelCase__    ,   num_warmup_steps=lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)

  # All other schedulers require `num_training_steps`
  if num_training_steps is None:
    raise ValueError(f"{name} requires `num_training_steps`, please provide that argument."						)

  if name == SchedulerType.COSINE_WITH_RESTARTS:
    return schedule_func(
        lowerCAmelCase__    ,   num_warmup_steps=lowerCAmelCase__    ,   num_training_steps=lowerCAmelCase__    ,   num_cycles=lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__    ,   )

  if name == SchedulerType.POLYNOMIAL:
    return schedule_func(
        lowerCAmelCase__    ,   num_warmup_steps=lowerCAmelCase__    ,   num_training_steps=lowerCAmelCase__    ,   power=lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__    ,   )

  return schedule_func(
      lowerCAmelCase__    ,   num_warmup_steps=lowerCAmelCase__    ,   num_training_steps=lowerCAmelCase__    ,   last_epoch=lowerCAmelCase__						)





