


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


a_					:    int          =    logging.get_logger(__name__)

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




class  a     (     _SCREAMING_SNAKE_CASE       ):
 _lowerCAmelCase 					=	"""blip_2_vision_model"""
 def __init__( self      ,				__magic_name__=14_08      ,				__magic_name__=61_44      ,				__magic_name__=39      ,				__magic_name__=16      ,				__magic_name__=2_24      ,				__magic_name__=14      ,				__magic_name__="gelu"      ,				__magic_name__=0.0_0_0_0_1      ,				__magic_name__=0.0      ,				__magic_name__=1e-10      ,				__magic_name__=True      ,				**__magic_name__      ,				)					->							List[str]:
      super().__init__(**__magic_name__						)

      _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 							__UpperCAmelCase		( cls      ,				__magic_name__      ,				**__magic_name__						)					->							"PretrainedConfig":
      cls._set_token_in_kwargs(__magic_name__						)

      _a      ,   _a			      =							cls.get_config_dict(__magic_name__      ,				**__magic_name__						)

      # 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(__magic_name__      ,				**__magic_name__						)





class  a     (     _SCREAMING_SNAKE_CASE       ):
 _lowerCAmelCase 					=	"""blip_2_qformer"""
 def __init__( self      ,				__magic_name__=3_05_22      ,				__magic_name__=7_68      ,				__magic_name__=12      ,				__magic_name__=12      ,				__magic_name__=30_72      ,				__magic_name__="gelu"      ,				__magic_name__=0.1      ,				__magic_name__=0.1      ,				__magic_name__=5_12      ,				__magic_name__=0.0_2      ,				__magic_name__=1e-12      ,				__magic_name__=0      ,				__magic_name__="absolute"      ,				__magic_name__=2      ,				__magic_name__=14_08      ,				**__magic_name__      ,				)					->							Union[str, Any]:
      super().__init__(pad_token_id=__magic_name__      ,				**__magic_name__						)

      _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 							__UpperCAmelCase		( cls      ,				__magic_name__      ,				**__magic_name__						)					->							"PretrainedConfig":
      cls._set_token_in_kwargs(__magic_name__						)

      _a      ,   _a			      =							cls.get_config_dict(__magic_name__      ,				**__magic_name__						)

      # 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(__magic_name__      ,				**__magic_name__						)





class  a     (     _SCREAMING_SNAKE_CASE       ):
 _lowerCAmelCase 					=	"""blip-2"""
 _lowerCAmelCase 					=	True
 def __init__( self      ,				__magic_name__=None      ,				__magic_name__=None      ,				__magic_name__=None      ,				__magic_name__=32      ,				**__magic_name__						)					->							Union[str, Any]:
      super().__init__(**__magic_name__						)

      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(**__magic_name__						)
      _a			      =							BlipaQFormerConfig(**__magic_name__						)
      _a			      =							text_config['model_type'] if 'model_type' in text_config else 'opt'
      _a			      =							CONFIG_MAPPING[text_model_type](**__magic_name__						)

      _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.0_2
 @classmethod
 def 							__UpperCAmelCase		( cls      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				**__magic_name__      ,				)					->							List[Any]:
      return cls(
          vision_config=vision_config.to_dict()      ,				qformer_config=qformer_config.to_dict()      ,				text_config=text_config.to_dict()      ,				**__magic_name__      ,				)




 def 							__UpperCAmelCase		( self						)					->							List[str]:
      _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 logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format="%(message)s")






def 	_A				(lowerCAmelCase__  :np.ndarray		)   ->  np.ndarray:




     '''simple docstring'''



     return input_array.reshape((input_array.size, 1)		)






def 	_A				(lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :int		)   ->  np.ndarray:




     '''simple docstring'''



     _a			      =							np.nan
     for i in range(lowerCAmelCase__		):
          _a			      =							features[:, labels == i]
          _a			      =							data.mean(1		)
          # Centralize the data of class i
          _a			      =							data - column_reshape(lowerCAmelCase__		)
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += np.dot(lowerCAmelCase__				,						centered_data.T		)
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               _a			      =							np.dot(lowerCAmelCase__				,						centered_data.T		)

     return covariance_sum / features.shape[1]






def 	_A				(lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :int		)   ->  np.ndarray:




     '''simple docstring'''



     _a			      =							features.mean(1		)
     _a			      =							np.nan
     for i in range(lowerCAmelCase__		):
          _a			      =							features[:, labels == i]
          _a			      =							data.shape[1]
          _a			      =							data.mean(1		)
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += device_data * np.dot(
                   column_reshape(lowerCAmelCase__		) - column_reshape(lowerCAmelCase__		)				,						(column_reshape(lowerCAmelCase__		) - column_reshape(lowerCAmelCase__		)).T				,						)
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               _a			      =							device_data * np.dot(
                   column_reshape(lowerCAmelCase__		) - column_reshape(lowerCAmelCase__		)				,						(column_reshape(lowerCAmelCase__		) - column_reshape(lowerCAmelCase__		)).T				,						)

     return covariance_sum / features.shape[1]






def 	_A				(lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :int		)   ->  np.ndarray:




     '''simple docstring'''



     if features.any():
          _a			      =							features.mean(1		)
          # Center the dataset
          _a			      =							features - np.reshape(lowerCAmelCase__				,						(data_mean.size, 1)		)
          _a			      =							np.dot(lowerCAmelCase__				,						centered_data.T		) / features.shape[1]
          _a      ,   _a			      =							np.linalg.eigh(lowerCAmelCase__		)
          # Take all the columns in the reverse order (-1), and then takes only the first
          _a			      =							eigenvectors[:, ::-1][:, 0:dimensions]
          # Project the database on the new space
          _a			      =							np.dot(filtered_eigenvectors.T				,						lowerCAmelCase__		)
          logging.info('Principal Component Analysis computed'		)

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR				,						format='%(message)s'				,						force=lowerCAmelCase__		)
          logging.error('Dataset empty'		)
          raise AssertionError






def 	_A				(lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :np.ndarray				,						lowerCAmelCase__  :int				,						lowerCAmelCase__  :int		)   ->  np.ndarray:




     '''simple docstring'''



     assert classes > dimensions

     # Check if features have been already loaded
     if features.any:
          _a      ,   _a			      =							eigh(
              covariance_between_classes(lowerCAmelCase__				,						lowerCAmelCase__				,						lowerCAmelCase__		)				,						covariance_within_classes(lowerCAmelCase__				,						lowerCAmelCase__				,						lowerCAmelCase__		)				,						)
          _a			      =							eigenvectors[:, ::-1][:, :dimensions]
          _a      ,   _a      ,   _a			      =							np.linalg.svd(lowerCAmelCase__		)
          _a			      =							svd_matrix[:, 0:dimensions]
          _a			      =							np.dot(filtered_svd_matrix.T				,						lowerCAmelCase__		)
          logging.info('Linear Discriminant Analysis computed'		)

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR				,						format='%(message)s'				,						force=lowerCAmelCase__		)
          logging.error('Dataset empty'		)
          raise AssertionError






def 	_A				()   ->  None:




     '''simple docstring'''



     _a			      =							np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]		)
     _a			      =							np.array([0, 0, 0, 1, 1]		)
     _a			      =							2
     _a			      =							2

     # Assert that the function raises an AssertionError if dimensions > classes
     with pytest.raises(lowerCAmelCase__		) as error_info:
          _a			      =							linear_discriminant_analysis(
              lowerCAmelCase__				,						lowerCAmelCase__				,						lowerCAmelCase__				,						lowerCAmelCase__		)
          if isinstance(lowerCAmelCase__				,						np.ndarray		):
               raise AssertionError(
                   'Did not raise AssertionError for dimensions > classes'		)
          assert error_info.type is AssertionError






def 	_A				()   ->  None:




     '''simple docstring'''



     _a			      =							np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]		)
     _a			      =							2
     _a			      =							np.array([[6.9_2_8_2_0_3_2_3, 8.6_6_0_2_5_4_0_4, 1_0.3_9_2_3_0_4_8_5], [3.0, 3.0, 3.0]]		)

     with pytest.raises(lowerCAmelCase__		) as error_info:
          _a			      =							principal_component_analysis(lowerCAmelCase__				,						lowerCAmelCase__		)
          if not np.allclose(lowerCAmelCase__				,						lowerCAmelCase__		):
               raise AssertionError
          assert error_info.type is AssertionError


if __name__ == "__main__":
 import doctest

 doctest.testmod()

