


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


lowercase_	   =     logging.get_logger(__name__)

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


class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """blip_2_vision_model"""




      def __init__(    self:	str						,				a:	str=1408						,				a:	str=6144						,				a:	List[Any]=39						,				a:	Union[str, Any]=16						,				a:	Union[str, Any]=224						,				a:	Any=14						,				a:	int="gelu"						,				a:	str=0.0_0_0_0_1						,				a:	int=0.0						,				a:	str=1e-10						,				a:	str=True						,				**a:	Optional[int]						,				):
       super().__init__(**_lowerCAmelCase    )

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




      @classmethod
      def 					_snake_case     (    cls:	Tuple						,				a:	Union[str, os.PathLike]						,				**a:	Dict    ):
       cls._set_token_in_kwargs(_lowerCAmelCase    )

       __lowerCamelCase ,					__lowerCamelCase		:							int		 =						cls.get_config_dict(_lowerCAmelCase						,				**_lowerCAmelCase    )

       # get the vision config dict if we are loading from Blip2Config
       if config_dict.get('model_type'    ) == "blip-2":
        __lowerCamelCase		:							Optional[int]		 =						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   ):




      '''simple docstring'''


      __snake_case 	=       """blip_2_qformer"""




      def __init__(    self:	Any						,				a:	Tuple=3_0522						,				a:	List[Any]=768						,				a:	Any=12						,				a:	Dict=12						,				a:	Dict=3072						,				a:	Dict="gelu"						,				a:	Optional[Any]=0.1						,				a:	Optional[int]=0.1						,				a:	Optional[int]=512						,				a:	List[Any]=0.0_2						,				a:	Optional[int]=1e-12						,				a:	List[Any]=0						,				a:	Any="absolute"						,				a:	List[Any]=2						,				a:	Union[str, Any]=1408						,				**a:	Any						,				):
       super().__init__(pad_token_id=_lowerCAmelCase						,				**_lowerCAmelCase    )

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




      @classmethod
      def 					_snake_case     (    cls:	List[Any]						,				a:	Union[str, os.PathLike]						,				**a:	Any    ):
       cls._set_token_in_kwargs(_lowerCAmelCase    )

       __lowerCamelCase ,					__lowerCamelCase		:							str		 =						cls.get_config_dict(_lowerCAmelCase						,				**_lowerCAmelCase    )

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




      '''simple docstring'''


      __snake_case 	=       """blip-2"""
      __snake_case 	=       True




      def __init__(    self:	Optional[int]						,				a:	List[Any]=None						,				a:	Any=None						,				a:	List[Any]=None						,				a:	Optional[Any]=32						,				**a:	str    ):
       super().__init__(**_lowerCAmelCase    )

       if vision_config is None:
        __lowerCamelCase		:							Any		 =						{}
        logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'    )

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

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

       __lowerCamelCase		:							str		 =						BlipaVisionConfig(**_lowerCAmelCase    )
       __lowerCamelCase		:							int		 =						BlipaQFormerConfig(**_lowerCAmelCase    )
       __lowerCamelCase		:							int		 =						text_config['model_type'] if 'model_type' in text_config else 'opt'
       __lowerCamelCase		:							Optional[int]		 =						CONFIG_MAPPING[text_model_type](**_lowerCAmelCase    )

       __lowerCamelCase		:							Dict		 =						self.text_config.tie_word_embeddings
       __lowerCamelCase		:							Optional[Any]		 =						self.text_config.is_encoder_decoder

       __lowerCamelCase		:							List[str]		 =						num_query_tokens
       __lowerCamelCase		:							str		 =						self.vision_config.hidden_size
       __lowerCamelCase		:							Any		 =						self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
       __lowerCamelCase		:							Optional[Any]		 =						1.0
       __lowerCamelCase		:							List[Any]		 =						0.0_2




      @classmethod
      def 					_snake_case     (    cls:	str						,				a:	BlipaVisionConfig						,				a:	BlipaQFormerConfig						,				a:	PretrainedConfig						,				**a:	Union[str, Any]						,				):
       return cls(
           vision_config=vision_config.to_dict()						,				qformer_config=qformer_config.to_dict()						,				text_config=text_config.to_dict()						,				**_lowerCAmelCase						,				)




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							int		 =						copy.deepcopy(self.__dict__    )
       __lowerCamelCase		:							int		 =						self.vision_config.to_dict()
       __lowerCamelCase		:							Tuple		 =						self.qformer_config.to_dict()
       __lowerCamelCase		:							str		 =						self.text_config.to_dict()
       __lowerCamelCase		:							int		 =						self.__class__.model_type
       return output





from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
       import torch

if is_torch_tpu_available(check_device=False):
       import torch_xla.core.xla_model as xm


lowercase_	   =     logging.get_logger(__name__)


@dataclass
class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       [
          """no_inference""",
          """no_cuda""",
          """no_tpu""",
          """no_speed""",
          """no_memory""",
          """no_env_print""",
          """no_multi_process""",
      ]




      def __init__(    self:	Any						,				**a:	Optional[Any]    ):
       for deprecated_arg in self.deprecated_args:
        if deprecated_arg in kwargs:
         __lowerCamelCase		:							str		 =						deprecated_arg[3:]
         setattr(self						,				a						,				not kwargs.pop(a    )    )
         logger.warning(
             F'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
             F' {positive_arg}={kwargs[positive_arg]}'    )

       __lowerCamelCase		:							str		 =						kwargs.pop('torchscript'						,				self.torchscript    )
       __lowerCamelCase		:							int		 =						kwargs.pop('torch_xla_tpu_print_metrics'						,				self.torch_xla_tpu_print_metrics    )
       __lowerCamelCase		:							Dict		 =						kwargs.pop('fp16_opt_level'						,				self.fpaa_opt_level    )
       super().__init__(**a    )

      __snake_case 	=       field(default=__UpperCamelCase ,				metadata={"""help""": """Trace the models using torchscript"""}   )
      __snake_case 	=       field(default=__UpperCamelCase ,				metadata={"""help""": """Print Xla/PyTorch tpu metrics"""}   )
      __snake_case 	=       field(
          default="""O1""" ,				metadata={
              """help""": (
                  """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """
                  """See details at https://nvidia.github.io/apex/amp.html"""
              )
          } ,				)




      @cached_property
      def 					_snake_case     (    self:	Dict    ):
       requires_backends(self						,				['torch']    )
       logger.info('PyTorch: setting up devices'    )
       if not self.cuda:
        __lowerCamelCase		:							Dict		 =						torch.device('cpu'    )
        __lowerCamelCase		:							str		 =						0
       elif is_torch_tpu_available():
        __lowerCamelCase		:							Optional[int]		 =						xm.xla_device()
        __lowerCamelCase		:							Dict		 =						0
       else:
        __lowerCamelCase		:							Any		 =						torch.device('cuda' if torch.cuda.is_available() else 'cpu'    )
        __lowerCamelCase		:							Union[str, Any]		 =						torch.cuda.device_count()
       return device, n_gpu




      @property
      def 					_snake_case     (    self:	Optional[Any]    ):
       return is_torch_tpu_available() and self.tpu




      @property
      def 					_snake_case     (    self:	Union[str, Any]    ):
       requires_backends(self						,				['torch']    )
       # TODO(PVP): currently only single GPU is supported
       return torch.cuda.current_device()




      @property
      def 					_snake_case     (    self:	int    ):
       requires_backends(self						,				['torch']    )
       return self._setup_devices[0]




      @property
      def 					_snake_case     (    self:	Union[str, Any]    ):
       requires_backends(self						,				['torch']    )
       return self._setup_devices[1]




      @property
      def 					_snake_case     (    self:	List[Any]    ):
       return self.n_gpu > 0

