






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


SCREAMING_SNAKE_CASE__		:							Dict 				=				logging.get_logger(__name__)

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







class 				UpperCAmelCase_  (		__lowerCamelCase		):
     __lowerCamelCase      				=  'blip_2_vision_model'




     def __init__(				self	,						_lowerCAmelCase=1408	,						_lowerCAmelCase=6144	,						_lowerCAmelCase=39	,						_lowerCAmelCase=16	,						_lowerCAmelCase=224	,						_lowerCAmelCase=14	,						_lowerCAmelCase="gelu"	,						_lowerCAmelCase=0.0_0_0_0_1	,						_lowerCAmelCase=0.0	,						_lowerCAmelCase=1e-10	,						_lowerCAmelCase=True	,						**_lowerCAmelCase	,						):
          super().__init__(**_lowerCAmelCase		)

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




     @classmethod
     def   __UpperCAmelCase  (				cls	,						_lowerCAmelCase	,						**_lowerCAmelCase		):
          cls._set_token_in_kwargs(_lowerCAmelCase		)

          UpperCAmelCase__      ,		UpperCAmelCase__      :  str				      =			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":
               UpperCAmelCase__      :  List[Any]				      =			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 				UpperCAmelCase_  (		__lowerCamelCase		):
     __lowerCamelCase      				=  'blip_2_qformer'




     def __init__(				self	,						_lowerCAmelCase=30522	,						_lowerCAmelCase=768	,						_lowerCAmelCase=12	,						_lowerCAmelCase=12	,						_lowerCAmelCase=3072	,						_lowerCAmelCase="gelu"	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase=0.1	,						_lowerCAmelCase=512	,						_lowerCAmelCase=0.0_2	,						_lowerCAmelCase=1e-12	,						_lowerCAmelCase=0	,						_lowerCAmelCase="absolute"	,						_lowerCAmelCase=2	,						_lowerCAmelCase=1408	,						**_lowerCAmelCase	,						):
          super().__init__(pad_token_id=_lowerCAmelCase	,						**_lowerCAmelCase		)

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




     @classmethod
     def   __UpperCAmelCase  (				cls	,						_lowerCAmelCase	,						**_lowerCAmelCase		):
          cls._set_token_in_kwargs(_lowerCAmelCase		)

          UpperCAmelCase__      ,		UpperCAmelCase__      :  Dict				      =			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":
               UpperCAmelCase__      :  Any				      =			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 				UpperCAmelCase_  (		__lowerCamelCase		):
     __lowerCamelCase      				=  'blip-2'
     __lowerCamelCase      				=  True




     def __init__(				self	,						_lowerCAmelCase=None	,						_lowerCAmelCase=None	,						_lowerCAmelCase=None	,						_lowerCAmelCase=32	,						**_lowerCAmelCase		):
          super().__init__(**_lowerCAmelCase		)

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

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

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

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

          UpperCAmelCase__      :  Tuple				      =			self.text_config.tie_word_embeddings
          UpperCAmelCase__      :  str				      =			self.text_config.is_encoder_decoder

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




     @classmethod
     def   __UpperCAmelCase  (				cls	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						**_lowerCAmelCase	,						):
          return cls(
              vision_config=vision_config.to_dict()	,						qformer_config=qformer_config.to_dict()	,						text_config=text_config.to_dict()	,						**_lowerCAmelCase	,						)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  str				      =			copy.deepcopy(self.__dict__		)
          UpperCAmelCase__      :  str				      =			self.vision_config.to_dict()
          UpperCAmelCase__      :  Dict				      =			self.qformer_config.to_dict()
          UpperCAmelCase__      :  List[Any]				      =			self.text_config.to_dict()
          UpperCAmelCase__      :  List[Any]				      =			self.__class__.model_type
          return output










'''simple docstring'''






from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


lowercase				=logging.get_logger(__name__)






class    __magic_name__				(						lowerCAmelCase							):
       UpperCAmelCase		 =["audio_values", "audio_mask"]






       def __init__(     self    ,				snake_case=2_0_4_8    ,				snake_case=1    ,				snake_case=[1_6, 1_6]    ,				snake_case=1_2_8    ,				snake_case=4_4_1_0_0    ,				snake_case=8_6    ,				snake_case=2_0_4_8    ,				snake_case=0.0    ,				**snake_case    ,				)		->    Optional[int]:




          '''simple docstring'''
          super().__init__(
              feature_size=snake_case    ,				sampling_rate=snake_case    ,				padding_value=snake_case    ,				**snake_case    ,				)

          _UpperCAmelCase  :  Dict				       =spectrogram_length
          _UpperCAmelCase  :  Any				       =num_channels
          _UpperCAmelCase  :  Optional[int]				       =patch_size
          _UpperCAmelCase  :  List[str]				       =feature_size // self.patch_size[1]
          _UpperCAmelCase  :  Optional[int]				       =n_fft
          _UpperCAmelCase  :  int				       =sampling_rate // hop_length_to_sampling_rate
          _UpperCAmelCase  :  int				       =sampling_rate
          _UpperCAmelCase  :  List[Any]				       =padding_value
          _UpperCAmelCase  :  Optional[int]				       =mel_filter_bank(
              num_frequency_bins=1 + n_fft // 2    ,				num_mel_filters=snake_case    ,				min_frequency=0.0    ,				max_frequency=2_20_50.0    ,				sampling_rate=snake_case    ,				norm='slaney'    ,				mel_scale='slaney'    ,				).T






       def      lowerCAmelCase						(     self    ,				snake_case)		->    np.ndarray:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =spectrogram(
              snake_case    ,				window_function(self.n_fft    ,				'hann')    ,				frame_length=self.n_fft    ,				hop_length=self.hop_length    ,				power=2.0    ,				mel_filters=self.mel_filters.T    ,				log_mel='dB'    ,				db_range=80.0    ,				)
          _UpperCAmelCase  :  Any				       =log_spec[:, :-1]
          _UpperCAmelCase  :  List[str]				       =log_spec - 20.0
          _UpperCAmelCase  :  Union[str, Any]				       =np.clip(log_spec / 40.0    ,				-2.0    ,				0.0) + 1.0
          return log_spec






       def __call__(     self    ,				snake_case    ,				snake_case = None    ,				snake_case = True    ,				snake_case = None    ,				snake_case = False    ,				snake_case = False    ,				**snake_case    ,				)		->    BatchFeature:




          '''simple docstring'''

          if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                raise ValueError(
                    'This feature extractor is set to support sampling rate'
                    f" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"
                    f" with {self.sampling_rate} and not {sampling_rate}.")
          else:
             logger.warning(
                 'It is strongly recommended to pass the `sampling_rate` argument to this function. '
                 'Failing to do so can result in silent errors that might be hard to debug.')

          _UpperCAmelCase  :  Tuple				       =isinstance(snake_case    ,				np.ndarray) and len(raw_speech.shape) > 1
          if is_batched_numpy and len(raw_speech.shape) > 2:
             raise ValueError(f"Only mono-channel audio is supported for input to {self}")
          _UpperCAmelCase  :  str				       =is_batched_numpy or (
              isinstance(snake_case    ,				(list, tuple)) and (isinstance(raw_speech[0]    ,				(np.ndarray, tuple, list)))
          )
          if is_batched:
             _UpperCAmelCase  :  List[str]				       =[np.asarray([speech]    ,				dtype=np.floataa).T for speech in raw_speech]
          elif not is_batched and not isinstance(snake_case    ,				np.ndarray):
             _UpperCAmelCase  :  List[Any]				       =np.asarray(snake_case    ,				dtype=np.floataa)
          elif isinstance(snake_case    ,				np.ndarray) and raw_speech.dtype is np.dtype(np.floataa):
             _UpperCAmelCase  :  List[Any]				       =raw_speech.astype(np.floataa)
          # always return batch
          if not is_batched:
             _UpperCAmelCase  :  Dict				       =[np.asarray([raw_speech]).T]

          # Convert audio signals to log mel spectrograms, truncate by time axis
          _UpperCAmelCase  :  List[Any]				       =[
              self._np_extract_fbank_features(waveform.squeeze()).T[: self.spectrogram_length] for waveform in raw_speech
          ]
          if isinstance(audio_features[0]    ,				snake_case):
             _UpperCAmelCase  :  Dict				       =[np.asarray(snake_case    ,				dtype=np.floataa) for feature in audio_features]

          # Create audio attention mask
          _UpperCAmelCase  :  Tuple				       =max(
              [ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len for feature in audio_features])  # The maximum number of audio patches in a batch
          if return_attention_mask:
             _UpperCAmelCase  :  Any				       =[
                 (ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [1]
                 + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]) * self.freq_len) * [0]
                 for feature in audio_features
             ]
             _UpperCAmelCase  :  int				       =np.array(snake_case).astype(np.floataa)

          # convert into correct format for padding
          _UpperCAmelCase  :  List[Any]				       =max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
          _UpperCAmelCase  :  List[str]				       =np.ones([len(snake_case), 1, max_time_len, self.feature_size]).astype(np.floataa)
          _UpperCAmelCase  :  Any				       =padded_audio_features * self.padding_value
          for i in range(len(snake_case)):
             _UpperCAmelCase  :  List[str]				       =audio_features[i]
             _UpperCAmelCase  :  Dict				       =feature

          # return as BatchFeature
          if return_attention_mask:
             _UpperCAmelCase  :  List[str]				       ={'audio_values': padded_audio_features, 'audio_mask': audio_mask}
          else:
             _UpperCAmelCase  :  Optional[int]				       ={'audio_values': padded_audio_features}

          _UpperCAmelCase  :  Any				       =BatchFeature(data=snake_case    ,				tensor_type=snake_case)
          return encoded_inputs

